/// <summary> /// Parses the JSON file and looks up the version for the specified mod. /// </summary> /// <param name="mod">The mod's static ID.</param> /// <param name="versions">The data from the web JSON file.</param> /// <returns>The results of the update, or null if the mod could not be found in the /// JSON.</returns> private ModVersionCheckResults ParseModVersion(Mod mod, ModVersions versions) { ModVersionCheckResults result = null; string id = mod.staticID; if (versions.mods != null) { foreach (var modVersion in versions.mods) { if (modVersion != null && modVersion.staticID == id) { string newVersion = modVersion.version?.Trim(); if (string.IsNullOrEmpty(newVersion)) { result = new ModVersionCheckResults(id, true); } else { result = new ModVersionCheckResults(id, newVersion != PVersionCheck.GetCurrentVersion(mod), newVersion); } break; } } } return(result); }
/// <summary> /// Adds a warning to a mod version label if it is outdated. /// </summary> /// <param name="data">The updated mod version.</param> /// <param name="versionText">The current mod version label.</param> private void AddWarningIfOutdated(ModVersionCheckResults data, LocText versionText) { GameObject go; if (versionText != null && (go = versionText.gameObject) != null && !data. IsUpToDate) { string text = versionText.text; if (string.IsNullOrEmpty(text)) { text = PLibStrings.OUTDATED_WARNING; } else { text = text + " " + PLibStrings.OUTDATED_WARNING; } versionText.text = text; go.AddOrGet <ToolTip>().toolTip = string.Format(PLibStrings.OUTDATED_TOOLTIP, data.NewVersion ?? ""); } }
/// <summary> /// Records the result of the mod version check, and runs the next checker in /// line, from this mod or a different one. /// </summary> /// <param name="result">The results from the version check.</param> private void OnComplete(ModVersionCheckResults result) { method.OnVersionCheckCompleted -= OnComplete; if (result != null) { results.TryAdd(result.ModChecked, result); if (!result.IsUpToDate) { PUtil.LogWarning("Mod {0} is out of date! New version: {1}".F(result. ModChecked, result.NewVersion ?? "unknown")); } else { #if DEBUG PUtil.LogDebug("Mod {0} is up to date".F(result.ModChecked)); #endif } } else { RunNext(); } }
/// <summary> /// When a web request completes, triggers the handler for the next updater. /// </summary> /// <param name="request">The YAML web request data.</param> /// <param name="mod">The mod that needs to be checked.</param> private void OnRequestFinished(UnityWebRequest request, Mod mod) { ModVersionCheckResults result = null; if (request.result == UnityWebRequest.Result.Success) { // Parse the text var modInfo = YamlIO.Parse <Mod.PackagedModInfo>(request.downloadHandler. text, default); string newVersion = modInfo?.version; if (modInfo != null && !string.IsNullOrEmpty(newVersion)) { string curVersion = PVersionCheck.GetCurrentVersion(mod); #if DEBUG PUtil.LogDebug("Current version: {0} New YAML version: {1}".F( curVersion, newVersion)); #endif result = new ModVersionCheckResults(mod.staticID, newVersion != curVersion, newVersion); } } request.Dispose(); OnVersionCheckCompleted?.Invoke(result); }
/// <summary> /// When a web request completes, triggers the handler for the next updater. /// </summary> /// <param name="request">The JSON web request data.</param> /// <param name="mod">The mod that needs to be checked.</param> private void OnRequestFinished(UnityWebRequest request, Mod mod) { ModVersionCheckResults result = null; if (request.result == UnityWebRequest.Result.Success) { // Parse the text ModVersions versions; using (var reader = new StreamReader(new MemoryStream(request. downloadHandler.data))) { versions = new JsonSerializer() { MaxDepth = 4, DateTimeZoneHandling = DateTimeZoneHandling.Utc, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }.Deserialize <ModVersions>(new JsonTextReader(reader)); } if (versions != null) { result = ParseModVersion(mod, versions); } } request.Dispose(); OnVersionCheckCompleted?.Invoke(result); }