#if UNITY_EDITOR
using System;
using System.IO;
using UnityEngine;
namespace Kamgam.SkyClouds.URP
{
public static class VersionHelper
{
public static string VersionFileName = "." + typeof(VersionHelper).FullName + ".txt";
public static Version DefaultVersion = new Version(0, 0, 0, 1);
public delegate bool UpgradeVersionDelegate(Version oldVersion, Version newVersion);
public static Version Parse(string version)
{
if (string.IsNullOrEmpty(version))
return DefaultVersion;
if (Version.TryParse(version, out var versionObj))
return versionObj;
else
return VersionHelper.DefaultVersion;
}
///
/// Returns false if upgrading was not necessary, True otherwise.
///
/// Change this to return the version of the software.
/// This is a separate method because your version may be stored in another class or a file.
/// The return value if this is compared against the install version marker.
/// Use this to execute some custom code before upgrading
/// the installed version info. If this returns false then the installed version will NOT be changed.
///
public static bool UpgradeVersion(Func getVersionFunc, UpgradeVersionDelegate upgradeVersionFunc = null)
{
return UpgradeVersion(getVersionFunc, out _, out _, upgradeVersionFunc);
}
///
/// Returns false if upgrading was not necessary, True otherwise.
/// Upgrades the version number only if the version info file path is valid. Otherwise it will abort.
///
/// Change this to return the version of the software.
/// This is a separate method because your version may be stored in another class or a file.
/// The return value if this is compared against the install version marker.
///
///
/// Use this to execute some custom code before upgrading
/// the installed version info. If this returns false then the installed version will NOT be changed.
/// Returns false if upgrading was not necessary (or impossible). Returns true if an upgrade is needed (and possible).
public static bool UpgradeVersion(Func getVersionFunc, out Version oldVersion, out Version newVersion, UpgradeVersionDelegate upgradeVersionFunc = null)
{
oldVersion = GetInstalledVersion();
newVersion = getVersionFunc();
// Abort upgrades if version info can not be retrieved.
if (!VersionInfoPathIsValid())
{
// We abort if the dir is not found because we assume the
// user has moved the asset and thus any upgrade attempts
// will probably fail anyways.
Logger.LogWarning(
"Could not find version info directory: '" + getVersionFileDir() + "'. Aborting upgrade. Did you move the asset?\n" +
"If you want auto-upgrades to work again then please restore the asset to the original directory (" + Installer.AssetRootPath + ").");
return false;
}
// Notice: this also cover downgrades.
if (oldVersion != newVersion)
{
if (upgradeVersionFunc != null)
{
bool upgradeSucceeded = upgradeVersionFunc(oldVersion, newVersion);
if (upgradeSucceeded)
SetInstalledVersion(newVersion);
return upgradeSucceeded;
}
else
{
SetInstalledVersion(newVersion);
}
return true;
}
return false;
}
public static bool VersionInfoPathIsValid()
{
return System.IO.Directory.Exists(getVersionFileDir());
}
public static void SetInstalledVersion(Version version)
{
if (version == null)
return;
if (!VersionInfoPathIsValid())
return;
string versionString = version.ToString();
string filePath = getVersionFilePath();
string tmpPath = filePath + ".tmp";
if (File.Exists(tmpPath))
{
File.Delete(tmpPath);
}
File.WriteAllText(tmpPath, versionString);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.Move(tmpPath, filePath);
}
public static Version GetInstalledVersion()
{
string filePath = getVersionFilePath();
if (!File.Exists(filePath))
{
return DefaultVersion;
}
string version = File.ReadAllText(filePath);
return Parse(version);
}
static string getVersionFilePath()
{
string dir = getVersionFileDir();
return dir + VersionFileName;
}
static string getVersionFileDir()
{
string dir = Installer.AssetRootPath.Trim();
// fix empty dir path
if (string.IsNullOrEmpty(dir))
{
dir = "Assets/";
}
// Fix missing ending slash
if (!dir.EndsWith("/") && !dir.EndsWith("\\"))
{
dir = dir + "/";
}
return getBasePath() + dir;
}
///
/// Returns the path to project root (the parent dir of Assets).
///
///
static string getBasePath()
{
// Unity Editor: /Assets
// See: https://docs.unity3d.com/ScriptReference/Application-dataPath.html
string basePath = Application.dataPath.Replace("/Assets", "/");
basePath = basePath.Replace("\\Assets", "\\");
return basePath;
}
}
}
#endif