using System; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; using GameCore; using ResUpdate; using System.Linq; namespace GameLogic { public class Version { Hashtable info; public Version(string json) { info = MiniJSON.jsonDecode(json) as Hashtable; } public string ToJson() { return MiniJSON.jsonEncode(info); } public string GetInfo(string key) { if (info.ContainsKey(key)) { return info[key] as string; } return null; } public void RemoveInfo(string key) { if (info.ContainsKey(key)) { info.Remove(key); } } public void SetInfo(string key, string value) { info[key] = value; } } /// /// 版本管理器 /// public class VersionManager : Singleton { /// /// 版本号文件 /// const string VersionsFile = "version"; /// /// 版本文件路径 /// string VersionsFilePath { get { return AppConst.PersistentDataPath + VersionsFile + ".txt"; } } /// /// Stream版本文件路径 /// string VersionsStreamFilePath { get { return AppConst.StreamPath + VersionsFile + ".txt"; } } /// /// 版本号 /// Version VersionInfo; public IEnumerator Initialize() { if (File.Exists(VersionsFilePath)) { VersionInfo = new Version(File.ReadAllText(VersionsFilePath, System.Text.Encoding.UTF8)); } else { string url = AppConst.FilePathEx + VersionsStreamFilePath; XDebug.Log.l("[VersionLoader Start ]@" + url); WWW _www = new WWW(url); yield return _www; if (string.IsNullOrEmpty(_www.error)) { XDebug.Log.l("[VersionLoader OK ]=" + _www.text); VersionInfo = new Version(_www.text); } else { Debug.LogError("[VersionLoader ERR ]=" + _www.error); } } yield return null; } public void InitVersions() { } /// /// 保存版本号 /// /// public void SetInfo(string key, string value) { if (VersionInfo != null) { VersionInfo.SetInfo(key, value); return; } } /// /// 保存版本号 /// /// public void SaveVersion(string version) { if (VersionInfo != null) { VersionInfo.SetInfo("version", version); SaveToFiles(VersionInfo); return; } } /// /// 获取版本号 /// 1.优先从外部拿 /// 2.拿不到返回-1 /// /// public string GetLocalVersion() { if (VersionInfo != null) { return VersionInfo.GetInfo("version"); } return null; } /// /// 对比版本号,返回不一样的位数 -1表示对比异常,0表示相同,其余表示按位不同 /// /// /// /// public static int VersionCompare(string ver1, string ver2) { string[] vs1 = ver1.Split('.'); string[] vs2 = ver2.Split('.'); if(vs1.Length != vs2.Length) { return -1; } int v1, v2; for (int i = 0; i < vs1.Length; i++) { if (!int.TryParse(vs1[i], out v1)) { return -1; } if (!int.TryParse(vs2[i], out v2)) { return -1; } if (v1 != v2) { return i + 1; } } return 0; } /// /// 获取版本内容,优先拿外部 /// /// /// public string GetVersionInfo(string key) { if (VersionInfo != null) { return VersionInfo.GetInfo(key); } return null; } /// /// 保存到文件 /// void SaveToFiles(Version version) { string directoryPath = Path.GetDirectoryName(VersionsFilePath); if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); File.WriteAllText(VersionsFilePath, version.ToJson(), System.Text.Encoding.UTF8); } /// /// 检测包版本,用于比较本地包与线上包的差异,有差异则需要更换新包 /// public static bool CheckPackageVersionSame(string checkVersion) { string localPackageVersion = Instance.GetVersionInfo("packageVersion"); if(localPackageVersion != null && checkVersion != null) { return localPackageVersion.Equals(checkVersion); }else if(localPackageVersion == null && checkVersion == null) { return true; } return false; } } }