VersionHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace Kamgam.SkyClouds.URP
  6. {
  7. public static class VersionHelper
  8. {
  9. public static string VersionFileName = "." + typeof(VersionHelper).FullName + ".txt";
  10. public static Version DefaultVersion = new Version(0, 0, 0, 1);
  11. public delegate bool UpgradeVersionDelegate(Version oldVersion, Version newVersion);
  12. public static Version Parse(string version)
  13. {
  14. if (string.IsNullOrEmpty(version))
  15. return DefaultVersion;
  16. if (Version.TryParse(version, out var versionObj))
  17. return versionObj;
  18. else
  19. return VersionHelper.DefaultVersion;
  20. }
  21. /// <summary>
  22. /// Returns false if upgrading was not necessary, True otherwise.
  23. /// </summary>
  24. /// <param name="getVersionFunc">Change this to return the version of the software.<br />
  25. /// This is a separate method because your version may be stored in another class or a file.<br />
  26. /// The return value if this is compared against the install version marker.</param>
  27. /// <param name="upgradeVersionFunc">Use this to execute some custom code before upgrading
  28. /// the installed version info. If this returns false then the installed version will NOT be changed.</param>
  29. /// <returns></returns>
  30. public static bool UpgradeVersion(Func<Version> getVersionFunc, UpgradeVersionDelegate upgradeVersionFunc = null)
  31. {
  32. return UpgradeVersion(getVersionFunc, out _, out _, upgradeVersionFunc);
  33. }
  34. /// <summary>
  35. /// Returns false if upgrading was not necessary, True otherwise.<br />
  36. /// Upgrades the version number only if the version info file path is valid. Otherwise it will abort.
  37. /// </summary>
  38. /// <param name="getVersionFunc">Change this to return the version of the software.<br />
  39. /// This is a separate method because your version may be stored in another class or a file.<br />
  40. /// The return value if this is compared against the install version marker.</param>
  41. /// <param name="oldVersion"></param>
  42. /// <param name="newVersion"></param>
  43. /// <param name="upgradeVersionFunc">Use this to execute some custom code before upgrading
  44. /// the installed version info. If this returns false then the installed version will NOT be changed.</param>
  45. /// <returns>Returns false if upgrading was not necessary (or impossible). Returns true if an upgrade is needed (and possible).</returns>
  46. public static bool UpgradeVersion(Func<Version> getVersionFunc, out Version oldVersion, out Version newVersion, UpgradeVersionDelegate upgradeVersionFunc = null)
  47. {
  48. oldVersion = GetInstalledVersion();
  49. newVersion = getVersionFunc();
  50. // Abort upgrades if version info can not be retrieved.
  51. if (!VersionInfoPathIsValid())
  52. {
  53. // We abort if the dir is not found because we assume the
  54. // user has moved the asset and thus any upgrade attempts
  55. // will probably fail anyways.
  56. Logger.LogWarning(
  57. "Could not find version info directory: '" + getVersionFileDir() + "'. Aborting upgrade. Did you move the asset?\n" +
  58. "If you want auto-upgrades to work again then please restore the asset to the original directory (" + Installer.AssetRootPath + ").");
  59. return false;
  60. }
  61. // Notice: this also cover downgrades.
  62. if (oldVersion != newVersion)
  63. {
  64. if (upgradeVersionFunc != null)
  65. {
  66. bool upgradeSucceeded = upgradeVersionFunc(oldVersion, newVersion);
  67. if (upgradeSucceeded)
  68. SetInstalledVersion(newVersion);
  69. return upgradeSucceeded;
  70. }
  71. else
  72. {
  73. SetInstalledVersion(newVersion);
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. public static bool VersionInfoPathIsValid()
  80. {
  81. return System.IO.Directory.Exists(getVersionFileDir());
  82. }
  83. public static void SetInstalledVersion(Version version)
  84. {
  85. if (version == null)
  86. return;
  87. if (!VersionInfoPathIsValid())
  88. return;
  89. string versionString = version.ToString();
  90. string filePath = getVersionFilePath();
  91. string tmpPath = filePath + ".tmp";
  92. if (File.Exists(tmpPath))
  93. {
  94. File.Delete(tmpPath);
  95. }
  96. File.WriteAllText(tmpPath, versionString);
  97. if (File.Exists(filePath))
  98. {
  99. File.Delete(filePath);
  100. }
  101. File.Move(tmpPath, filePath);
  102. }
  103. public static Version GetInstalledVersion()
  104. {
  105. string filePath = getVersionFilePath();
  106. if (!File.Exists(filePath))
  107. {
  108. return DefaultVersion;
  109. }
  110. string version = File.ReadAllText(filePath);
  111. return Parse(version);
  112. }
  113. static string getVersionFilePath()
  114. {
  115. string dir = getVersionFileDir();
  116. return dir + VersionFileName;
  117. }
  118. static string getVersionFileDir()
  119. {
  120. string dir = Installer.AssetRootPath.Trim();
  121. // fix empty dir path
  122. if (string.IsNullOrEmpty(dir))
  123. {
  124. dir = "Assets/";
  125. }
  126. // Fix missing ending slash
  127. if (!dir.EndsWith("/") && !dir.EndsWith("\\"))
  128. {
  129. dir = dir + "/";
  130. }
  131. return getBasePath() + dir;
  132. }
  133. /// <summary>
  134. /// Returns the path to project root (the parent dir of Assets).
  135. /// </summary>
  136. /// <returns></returns>
  137. static string getBasePath()
  138. {
  139. // Unity Editor: <path to project folder>/Assets
  140. // See: https://docs.unity3d.com/ScriptReference/Application-dataPath.html
  141. string basePath = Application.dataPath.Replace("/Assets", "/");
  142. basePath = basePath.Replace("\\Assets", "\\");
  143. return basePath;
  144. }
  145. }
  146. }
  147. #endif