public static string GetModVersion(string modName) { var streamingModPath = Path.Combine(Application.streamingAssetsPath, "modules", modName, "cfg.json"); var persistentModPath = Path.Combine(Application.persistentDataPath, "modules", modName, "cfg.json"); ModuleOutputVersionCfg streamingVersion = null; ModuleOutputVersionCfg persistentVersion = null; DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ModuleOutputVersionCfg)); if (File.Exists(streamingModPath)) { var streamingModCfgJSONBytes = NetHelper.UnityWebRequestLocalGet(streamingModPath); if (streamingModCfgJSONBytes.Length > 1) { using (MemoryStream stream = new MemoryStream(streamingModCfgJSONBytes)) { streamingVersion = (ModuleOutputVersionCfg)ser.ReadObject(stream); } } } if (File.Exists(persistentModPath)) { var persistentModCfgJSONBytes = NetHelper.UnityWebRequestLocalGet(persistentModPath); if (persistentModCfgJSONBytes.Length > 1) { using (MemoryStream stream2 = new MemoryStream(persistentModCfgJSONBytes)) { persistentVersion = (ModuleOutputVersionCfg)ser.ReadObject(stream2); } } } if (streamingVersion == null && persistentVersion == null) { return(""); } if (streamingVersion == null) { return(persistentVersion.Version); } if (persistentVersion == null) { return(streamingVersion.Version); } var icmp = NetHelper.VersionCompare(streamingVersion.Version, persistentVersion.Version); if (icmp > 0) { return(streamingVersion.Version); } return(persistentVersion.Version); }
/// <summary> /// 读取并比较streamingAssetsPath和persistentDataPath目录下module的cfg.json文件,并比较版本号 /// </summary> /// <returns></returns> private bool IsStreamingAssetsPathModVersionNewer() { var streamingModPath = Path.Combine(Application.streamingAssetsPath, modName, "cfg.json"); var persistentModPath = Path.Combine(Application.persistentDataPath, modName, "cfg.json"); var streamingModCfgJSONBytes = NetHelper.UnityWebRequestLocalGet(streamingModPath); if (streamingModCfgJSONBytes == null || streamingModCfgJSONBytes.Length < 1) { // streaming 目录cfg.json读取不成功 return(false); } var persistentModCfgJSONBytes = NetHelper.UnityWebRequestLocalGet(persistentModPath); if (persistentModCfgJSONBytes == null || persistentModCfgJSONBytes.Length < 1) { // persistent 目录cfg.json读取不成功 return(true); } // JSON 读取version字符串 using (MemoryStream stream = new MemoryStream(streamingModCfgJSONBytes)) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ModuleOutputVersionCfg)); ModuleOutputVersionCfg streamingVersion = (ModuleOutputVersionCfg)ser.ReadObject(stream); using (MemoryStream stream2 = new MemoryStream(persistentModCfgJSONBytes)) { ModuleOutputVersionCfg persistentVersion = (ModuleOutputVersionCfg)ser.ReadObject(stream2); var icmp = NetHelper.VersionCompare(streamingVersion.Version, persistentVersion.Version); Debug.Log($"streamingVersion:{streamingVersion.Version}, persistentVersion:{persistentVersion.Version}, result:{icmp}"); return(icmp > 0); } } }