VRResourceEntitySystem.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using Cysharp.Threading.Tasks;
  6. using KYFramework;
  7. namespace VRPlatform
  8. {
  9. public static class VRResourceEntitySystem
  10. {
  11. public static void Run(this VRResourceEntity self)
  12. {
  13. self.Process = new Process()
  14. {
  15. StartInfo = new ProcessStartInfo()
  16. {
  17. FileName = self.Exe,
  18. RedirectStandardOutput = true,
  19. RedirectStandardError = true,
  20. UseShellExecute = false,
  21. CreateNoWindow = true,
  22. WindowStyle = ProcessWindowStyle.Normal
  23. }
  24. };
  25. self.Process.Start();
  26. // Run resource
  27. Log.Info($"Run resource [{self.VRResourceId}]");
  28. }
  29. public static void Stop(this VRResourceEntity self)
  30. {
  31. self.Process?.Kill();
  32. self.Process = null;
  33. // Stop resource
  34. Log.Info($"Stop resource [{self.VRResourceId}]");
  35. }
  36. public static async UniTask Download(this VRResourceEntity self, Action<double> progressCallback = null)
  37. {
  38. // Download resource
  39. Downloader downloader = new Downloader();
  40. await downloader.DownloadFileAsync(self.Url, self.FilePath,progressCallback);
  41. // 解压缩 self.FilePath
  42. string extractPath = Path.Combine(Path.GetDirectoryName(self.FilePath), Path.GetFileNameWithoutExtension(self.FilePath));
  43. ZipFile.ExtractToDirectory(self.FilePath, extractPath);
  44. File.Delete(self.FilePath);
  45. Log.Info($"Download resource [{self.VRResourceId}]");
  46. }
  47. }
  48. }