Skip to content

Commit

Permalink
chore: code cleanup and optimize AchievementPatches
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Jan 21, 2024
1 parent 10fb6a3 commit 004a72f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion COTL_API.Common.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net472</TargetFramework>
<Version>0.2.2</Version>
<LangVersion>latest</LangVersion>
<DebugType>portable</DebugType>
Expand Down
6 changes: 4 additions & 2 deletions COTL_API/CustomMission/CustomMissionPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ public static bool GetReward(ref InventoryItem.ITEM_TYPE type, ref float chance,
}

if (chance > num)
__result = new[]
{ new InventoryItem(CustomMissionList[type].RewardType, CustomMissionList[type].RewardRange.Random()) };
__result =
[
new InventoryItem(CustomMissionList[type].RewardType, CustomMissionList[type].RewardRange.Random())
];

return false;
}
Expand Down
10 changes: 6 additions & 4 deletions COTL_API/CustomSkins/CustomSkinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,18 @@ private static Skin CreateTarotSkin(Skin template, string skinName)
customAttachment.SetRegion(atlasRegion);
atlasRegion.name = "CustomTarotSkin_" + atlasRegion.name;
customAttachment.HullLength = 4;
customAttachment.Triangles = new[] { 1, 2, 3, 1, 3, 0 };
customAttachment.Triangles = [1, 2, 3, 1, 3, 0];
float pw = atlasRegion.page.width;
float ph = atlasRegion.page.height;
float x = atlasRegion.x;
float y = atlasRegion.y;
float w = atlasRegion.width;
float h = atlasRegion.height;
customAttachment.UVs = new[]
{ (x + w) / pw, y / ph, (x + w) / pw, (y + h) / ph, x / pw, (y + h) / ph, x / pw, y / ph };
customAttachment.Vertices = new[] { minY, minX, 1, maxY, minX, 1, maxY, maxX, 1, minY, maxX, 1 };
customAttachment.UVs =
[
(x + w) / pw, y / ph, (x + w) / pw, (y + h) / ph, x / pw, (y + h) / ph, x / pw, y / ph
];
customAttachment.Vertices = [minY, minX, 1, maxY, minX, 1, maxY, maxX, 1, minY, maxX, 1];
customAttachment.WorldVerticesLength = 8;

skin.SetAttachment(front.SlotIndex, front.Name, customAttachment);
Expand Down
8 changes: 4 additions & 4 deletions COTL_API/Globals.cs
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;
80 changes: 42 additions & 38 deletions COTL_API/Patches/AchievementsWrapperPatches.cs
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;
}
}
12 changes: 6 additions & 6 deletions COTL_API/Patches/ModdedStructuresPatches.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Debugger = DG.Tweening.Core.Debugger;
using COTL_API.CustomStructures;
using System.Reflection.Emit;
using System.Diagnostics;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using BepInEx.Bootstrap;
using MMRoomGeneration;
using UnityEngine;
using COTL_API.CustomStructures;
using HarmonyLib;
using I2.Loc;
using MMRoomGeneration;
using UnityEngine;
using Debugger = DG.Tweening.Core.Debugger;
using Object = UnityEngine.Object;

namespace COTL_API.UI.Helpers;
Expand Down
14 changes: 7 additions & 7 deletions COTL_API/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public class Plugin : BaseUnityPlugin
private ConfigEntry<bool>? _skipSplashScreen { get; set; }
public bool SkipSplashScreen => _skipSplashScreen?.Value ?? false;

private ConfigEntry<bool>? _dontSaveAchievement { get; set; }
public bool DontSaveAchievement => _dontSaveAchievement?.Value ?? true;
private ConfigEntry<bool>? _disableAchievement { get; set; }
public bool DisableAchievement => _disableAchievement?.Value ?? true;

internal static bool Started { get; private set; }

Expand Down Expand Up @@ -96,8 +96,8 @@ private void Awake()

_skipSplashScreen = Config.Bind("Miscellaneous", "Skip Splash Screen", false,
"Should we skip the splash screen or not?");
_dontSaveAchievement = Config.Bind("Miscellaneous", "Dont Save Achievement", true,
"Should we save the achievement to the cloud or not? (Cloud being Steam / GOG / PSN)");
_disableAchievement = Config.Bind("Miscellaneous", "Disable Achievement", true,
"Should we disable the achievement system? (You will still be able to get achievement but it won't save)");

_debug = Config.Bind("Debug", "API Debug", false,
"API debug mode. Will add debug content to your game for testing. Not recommended for normal play.");
Expand Down Expand Up @@ -136,7 +136,7 @@ Skin S3()
});

CustomSettingsManager.AddBepInExConfig("API", "Skip Splash Screen", _skipSplashScreen);
CustomSettingsManager.AddBepInExConfig("API", "Don't Save Achievement", _dontSaveAchievement,
CustomSettingsManager.AddBepInExConfig("API", "Disable Achievement", _disableAchievement,
delegate(bool isActivated)
{
if (isActivated) return;
Expand Down Expand Up @@ -295,13 +295,13 @@ private void AddDebugContent()
test.InitialQuestText = "This is my custom quest text for this objective.";

CustomSettingsManager.AddDropdown("Debug", "Dropdown", "Option 1",
new[] { "Option 1", "Option 2", "Option 3" }, i => { LogDebug($"Dropdown selected {i}"); });
["Option 1", "Option 2", "Option 3"], i => { LogDebug($"Dropdown selected {i}"); });

CustomSettingsManager.AddKeyboardShortcutDropdown("Debug", "Keyboard Shortcut", KeyCode.None,
i => { LogDebug($"Keyboard Shortcut selected {i}"); });

CustomSettingsManager.AddHorizontalSelector("Debug", "Horizontal Selector", "Option 1",
new[] { "Option 1", "Option 2", "Option 3" },
["Option 1", "Option 2", "Option 3"],
i => { LogDebug($"Horizontal Selector selected {i}"); });

CustomSettingsManager.AddSlider("Debug", "Slider", 0, -100, 100, 1, MMSlider.ValueDisplayFormat.RawValue,
Expand Down
2 changes: 1 addition & 1 deletion COTL_API/UI/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
using UnityEngine;
using UnityEngine.UI;
using Dropdown = COTL_API.CustomSettings.Elements.Dropdown;
using Object = UnityEngine.Object;
using Slider = COTL_API.CustomSettings.Elements.Slider;
using Toggle = COTL_API.CustomSettings.Elements.Toggle;
using Object = UnityEngine.Object;

namespace COTL_API.UI;

Expand Down

0 comments on commit 004a72f

Please sign in to comment.