LegacyCompileChecker.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #if !UNITY_2019_1_OR_NEWER
  2. using System;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. namespace SingularityGroup.HotReload.Editor {
  7. class LegacyCompileChecker : ICompileChecker {
  8. const string timestampFilePath = PackageConst.LibraryCachePath + "/lastCompileTimestamp.txt";
  9. public bool hasCompileErrors { get; }
  10. const string assemblyPath = "Library/ScriptAssemblies";
  11. bool recompile;
  12. public LegacyCompileChecker() {
  13. Task.Run(() => {
  14. var info = new DirectoryInfo(assemblyPath);
  15. if(!info.Exists) {
  16. return;
  17. }
  18. var currentCompileTimestamp = default(DateTime);
  19. foreach (var file in info.GetFiles("*.dll")) {
  20. var fileWriteDate = file.LastWriteTimeUtc;
  21. if(fileWriteDate > currentCompileTimestamp) {
  22. currentCompileTimestamp = fileWriteDate;
  23. }
  24. }
  25. if(File.Exists(timestampFilePath)) {
  26. var lastTimestampStr = File.ReadAllText(timestampFilePath);
  27. var lastTimestamp = DateTime.ParseExact(lastTimestampStr, "o", CultureInfo.CurrentCulture).ToUniversalTime();
  28. if(currentCompileTimestamp > lastTimestamp) {
  29. ThreadUtility.RunOnMainThread(() => {
  30. recompile = true;
  31. _onCompilationFinished?.Invoke();
  32. });
  33. }
  34. }
  35. Directory.CreateDirectory(Path.GetDirectoryName(timestampFilePath));
  36. File.WriteAllText(timestampFilePath, currentCompileTimestamp.ToString("o"));
  37. });
  38. }
  39. Action _onCompilationFinished;
  40. public event Action onCompilationFinished {
  41. add {
  42. if(recompile && value != null) {
  43. value();
  44. }
  45. _onCompilationFinished += value;
  46. }
  47. remove {
  48. _onCompilationFinished -= value;
  49. }
  50. }
  51. }
  52. }
  53. #endif