-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: code cleanup and optimize AchievementPatches
- Loading branch information
Showing
8 changed files
with
71 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
global using static COTL_API.Helpers.LogHelper; | ||
global using OnInteractHelper = COTL_API.Helpers.OnInteractHelper; | ||
global using TextureHelper = COTL_API.Helpers.TextureHelper; | ||
global using PluginPaths = COTL_API.Helpers.PluginPaths; | ||
global using OnInteractHelper = COTL_API.Helpers.OnInteractHelper; | ||
global using ItemPickUp = COTL_API.Helpers.ItemPickUp; | ||
global using TaskUtils = COTL_API.Helpers.TaskUtils; | ||
global using Debugging = COTL_API.Helpers.Debugging; | ||
global using HashCode = COTL_API.Helpers.HashCode; | ||
global using static COTL_API.Helpers.Extensions; | ||
global using ItemPickUp = COTL_API.Helpers.ItemPickUp; | ||
global using Debugging = COTL_API.Helpers.Debugging; | ||
global using static COTL_API.Helpers.LogHelper; |
This file contains 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 |
---|---|---|
@@ -1,59 +1,63 @@ | ||
using HarmonyLib; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using Unify; | ||
|
||
namespace COTL_API.Patches; | ||
|
||
[HarmonyPatch] | ||
public static class AchievementsWrapperPatches | ||
{ | ||
[HarmonyPatch(typeof(AchievementsWrapper), nameof(AchievementsWrapper.UnlockAchievement))] | ||
[HarmonyPrefix] | ||
private static bool AchievementsWrapper_UnlockAchievement(ref Achievement achievementId) | ||
private static IEnumerable<CodeInstruction> patchAchievementPlayerPerfs(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
if (Plugin.Instance == null || !Plugin.Instance.DontSaveAchievement) | ||
return true; | ||
|
||
if (AchievementsWrapper.unlockedAchievements.Contains(achievementId.id)) | ||
return false; | ||
|
||
AchievementsWrapper.unlockedAchievements.Add(achievementId.id); | ||
|
||
var achievementUnlocked = AchievementsWrapper.OnAchievementUnlocked; | ||
achievementUnlocked?.Invoke(achievementId.label); | ||
|
||
if (achievementId == Achievements.Instance.Lookup("platinum")) | ||
return false; | ||
return new CodeMatcher(instructions) | ||
.MatchForward(false, | ||
new CodeMatch(OpCodes.Ldstr, "unlockedAchievements"), | ||
new CodeMatch(OpCodes.Ldsfld, | ||
AccessTools.Field(typeof(AchievementsWrapper), nameof(AchievementsWrapper.unlockedAchievements))), | ||
new CodeMatch(OpCodes.Callvirt, AccessTools.Method(typeof(object), nameof(ToString))), | ||
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(PlayerPrefs), nameof(PlayerPrefs.SetString))) | ||
) | ||
.SetInstructionAndAdvance(Transpilers.EmitDelegate(() => | ||
{ | ||
if (!Plugin.Instance || !Plugin.Instance!.DisableAchievement) | ||
PlayerPrefs.SetString("unlockedAchievements", | ||
AchievementsWrapper.unlockedAchievements.ToString()); | ||
})) | ||
.SetAndAdvance(OpCodes.Nop, null) | ||
.SetAndAdvance(OpCodes.Nop, null) | ||
.SetAndAdvance(OpCodes.Nop, null) | ||
.MatchForward(false, | ||
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(PlayerPrefs), nameof(PlayerPrefs.Save)))) | ||
.SetInstructionAndAdvance(Transpilers.EmitDelegate(() => | ||
{ | ||
if (!Plugin.Instance || !Plugin.Instance!.DisableAchievement) | ||
PlayerPrefs.Save(); | ||
})) | ||
.InstructionEnumeration(); | ||
} | ||
|
||
AchievementsWrapper.compareAchievements(); | ||
return false; | ||
[HarmonyPatch(typeof(AchievementsWrapper), nameof(AchievementsWrapper.UnlockAchievement))] | ||
[HarmonyTranspiler] | ||
[HarmonyDebug] | ||
private static IEnumerable<CodeInstruction> AchievementsWrapper_UnlockAchievement( | ||
IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return patchAchievementPlayerPerfs(instructions); | ||
} | ||
|
||
[HarmonyPatch(typeof(AchievementsWrapper), nameof(AchievementsWrapper.GetAchievementProgress))] | ||
[HarmonyPrefix] | ||
private static bool AchievementsWrapper_GetAchievementProgress(ref List<AchievementProgress> result) | ||
[HarmonyTranspiler] | ||
[HarmonyDebug] | ||
private static IEnumerable<CodeInstruction> AchievementsWrapper_GetAchievementProgress( | ||
IEnumerable<CodeInstruction> instructions) | ||
{ | ||
if (Plugin.Instance == null || !Plugin.Instance.DontSaveAchievement) | ||
return true; | ||
|
||
// Expect Error | ||
if (result == null || result.Count == 0) | ||
return false; | ||
|
||
foreach (var achievementProgress in result.Where(achievementProgress => achievementProgress.progress >= 100 && | ||
!AchievementsWrapper.unlockedAchievements.Contains(achievementProgress.id))) | ||
{ | ||
AchievementsWrapper.unlockedAchievements.Add(achievementProgress.id); | ||
var achievementUnlocked = AchievementsWrapper.OnAchievementUnlocked; | ||
achievementUnlocked?.Invoke(achievementProgress.name); | ||
} | ||
|
||
return false; | ||
return patchAchievementPlayerPerfs(instructions); | ||
} | ||
|
||
[HarmonyPatch(typeof(AchievementsWrapper), nameof(AchievementsWrapper.UnlockPlatinum))] | ||
[HarmonyPrefix] | ||
private static bool AchievementsWrapper_UnlockPlatinum() | ||
{ | ||
return Plugin.Instance == null || !Plugin.Instance.DontSaveAchievement; | ||
return Plugin.Instance == null || !Plugin.Instance.DisableAchievement; | ||
} | ||
} |
This file contains 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 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 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