-
Notifications
You must be signed in to change notification settings - Fork 4
v1.8.8: 新增版本更新检测功能 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System; | ||
|
|
||
| namespace DuckovCustomModel.Configs | ||
| { | ||
| public class UpdateInfoConfig : ConfigBase | ||
| { | ||
| public string LatestVersion { get; set; } = string.Empty; | ||
| public string LatestReleaseName { get; set; } = string.Empty; | ||
| public DateTime? LastCheckTime { get; set; } | ||
| public DateTime? LatestPublishedAt { get; set; } | ||
| public bool HasUpdate { get; set; } | ||
|
|
||
| public override void LoadDefault() | ||
| { | ||
| LatestVersion = string.Empty; | ||
| LatestReleaseName = string.Empty; | ||
| LastCheckTime = null; | ||
| LatestPublishedAt = null; | ||
| HasUpdate = false; | ||
| } | ||
|
|
||
| public override bool Validate() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public override void CopyFrom(IConfigBase other) | ||
| { | ||
| if (other is not UpdateInfoConfig otherConfig) return; | ||
| LatestVersion = otherConfig.LatestVersion; | ||
| LatestReleaseName = otherConfig.LatestReleaseName; | ||
| LastCheckTime = otherConfig.LastCheckTime; | ||
| LatestPublishedAt = otherConfig.LatestPublishedAt; | ||
| HasUpdate = otherConfig.HasUpdate; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
DuckovCustomModel/HarmonyPatches/GameVersionDisplayPatches.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| using Duckov; | ||
| using DuckovCustomModel.Localizations; | ||
| using DuckovCustomModel.Managers; | ||
| using HarmonyLib; | ||
| using TMPro; | ||
| using UnityEngine; | ||
|
|
||
| namespace DuckovCustomModel.HarmonyPatches | ||
| { | ||
| [HarmonyPatch] | ||
| internal static class GameVersionDisplayPatches | ||
| { | ||
| private const string UpdateColorHex = "#FF9900"; | ||
| private const string VersionObjectName = "DCMVersionDisplay"; | ||
| private static TextMeshProUGUI? _versionTextComponent; | ||
| private static GameObject? _versionObject; | ||
|
|
||
| public static void Initialize() | ||
| { | ||
| var gameVersionDisplay = Object.FindFirstObjectByType<GameVersionDisplay>(); | ||
| if (gameVersionDisplay == null) | ||
| return; | ||
|
|
||
| var parentTransform = gameVersionDisplay.transform.parent; | ||
| if (parentTransform == null) | ||
| return; | ||
|
|
||
| CreateVersionObject(parentTransform, gameVersionDisplay.gameObject); | ||
| UpdateVersionText(); | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(GameVersionDisplay), "Start")] | ||
| [HarmonyPostfix] | ||
| // ReSharper disable once InconsistentNaming | ||
| private static void GameVersionDisplay_Start_Postfix(GameVersionDisplay __instance) | ||
| { | ||
| if (__instance == null) | ||
| return; | ||
|
|
||
| if (IsVersionObjectValid()) | ||
| { | ||
| UpdateVersionText(); | ||
| return; | ||
| } | ||
|
|
||
| var parentTransform = __instance.transform.parent; | ||
| if (parentTransform == null) | ||
| return; | ||
|
|
||
| if (CreateVersionObject(parentTransform, __instance.gameObject)) | ||
| UpdateVersionText(); | ||
| } | ||
|
|
||
| private static bool IsVersionObjectValid() | ||
| { | ||
| return _versionObject != null && _versionTextComponent != null; | ||
| } | ||
|
|
||
| private static bool CreateVersionObject(Transform parentTransform, GameObject templateObject) | ||
| { | ||
| var cloneObject = Object.Instantiate(templateObject, parentTransform); | ||
| if (cloneObject == null) | ||
| return false; | ||
|
|
||
| cloneObject.name = VersionObjectName; | ||
|
|
||
| DeleteComponent(cloneObject, cloneObject.GetComponent<GameVersionDisplay>()); | ||
|
|
||
| var textComponent = cloneObject.GetComponent<TextMeshProUGUI>(); | ||
| if (textComponent == null) | ||
| { | ||
| Object.Destroy(cloneObject); | ||
| return false; | ||
| } | ||
|
|
||
| _versionObject = cloneObject; | ||
| _versionTextComponent = textComponent; | ||
| textComponent.richText = true; | ||
| textComponent.text = $"{Constant.ModName} v{Constant.ModVersion}"; | ||
| return true; | ||
| } | ||
|
|
||
| private static void UpdateVersionText() | ||
| { | ||
| if (_versionTextComponent == null) | ||
| return; | ||
|
|
||
| var updateChecker = UpdateChecker.Instance; | ||
| if (updateChecker == null) | ||
| { | ||
| _versionTextComponent.text = $"{Constant.ModName} v{Constant.ModVersion}"; | ||
| return; | ||
| } | ||
|
|
||
| var hasUpdate = updateChecker.HasUpdate(); | ||
| var latestVersion = updateChecker.GetLatestVersion(); | ||
|
|
||
| _versionTextComponent.text = hasUpdate && !string.IsNullOrEmpty(latestVersion) | ||
| ? $"{Constant.ModName} v{Constant.ModVersion} - <color={UpdateColorHex}>{Localization.UpdateAvailable} v{latestVersion}</color>" | ||
| : $"{Constant.ModName} v{Constant.ModVersion}"; | ||
| } | ||
|
|
||
| public static void RefreshUpdateVersionDisplay() | ||
| { | ||
| if (!IsVersionObjectValid()) | ||
| return; | ||
|
|
||
| UpdateVersionText(); | ||
| } | ||
|
|
||
| public static void Cleanup() | ||
| { | ||
| if (_versionObject != null) | ||
| { | ||
| Object.Destroy(_versionObject); | ||
| _versionObject = null; | ||
| } | ||
|
|
||
| _versionTextComponent = null; | ||
| } | ||
|
|
||
| private static void DeleteComponent(GameObject gameObject, Component? component) | ||
| { | ||
| if (component != null) Object.Destroy(component); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.