Skip to content

Commit

Permalink
feat: use hkreflect
Browse files Browse the repository at this point in the history
  • Loading branch information
Clazex committed Mar 8, 2023
1 parent 5642f5a commit ede24e9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 60 deletions.
1 change: 1 addition & 0 deletions Osmi/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<InfoOf />
<HKReflect />
</Weavers>
94 changes: 40 additions & 54 deletions Osmi/Game/CharmUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,23 @@ public static int GetCharmCost(int charm) =>
/// <param name="charm">Charm ID</param>
/// <returns>Whether the operation is successful</returns>
/// <remarks>The caller should call <see cref="UpdateCharm"/> after changes to charms</remarks>
public static bool EquipCharm(int charm) => EquipCharmInternal(Ref.PD, charm);
public static bool EquipCharm(int charm) {
if (EquippedCharm(charm) || PlayerDataR.charmSlotsFilled >= PlayerDataR.charmSlots) {
return false;
}

PlayerDataR.charmSlotsFilled += GetCharmCost(charm);

if (PlayerDataR.charmSlots < PlayerDataR.charmSlotsFilled) {
PlayerDataR.canOvercharm = true;
PlayerDataR.overcharmed = true;
}

Ref.PD.SetBool($"equippedCharm_{charm}", true);
Ref.PD.EquipCharm(charm);

return true;
}

/// <summary>
/// Unequips specified charm for the player.
Expand All @@ -70,29 +86,40 @@ public static int GetCharmCost(int charm) =>
/// <param name="charm">Charm ID</param>
/// <returns>Whether the operation is successful</returns>
/// <remarks>The caller should call <see cref="UpdateCharm"/> after changes to charms</remarks>
public static bool UnequipCharm(int charm) => UnequipCharmInternal(Ref.PD, charm);
public static bool UnequipCharm(int charm) {
if (!EquippedCharm(charm)) {
return false;
}

PlayerDataR.charmSlotsFilled -= GetCharmCost(charm);

if (PlayerDataR.overcharmed && PlayerDataR.charmSlotsFilled <= PlayerDataR.charmSlots) {
PlayerDataR.overcharmed = false;
}

Ref.PD.SetBool($"equippedCharm_{charm}", false);
Ref.PD.UnequipCharm(charm);

return true;
}

/// <summary>
/// Equips a series of charms for the player.
/// </summary>
/// <param name="charms">Charm IDs</param>
/// <returns>Whether all operations are successful</returns>
/// <remarks>The caller should call <see cref="UpdateCharm"/> after changes to charms</remarks>
public static bool EquipCharms(params int[] charms) {
PlayerData pd = Ref.PD;
return charms.Every(charm => EquipCharmInternal(pd, charm));
}
public static bool EquipCharms(params int[] charms) =>
charms.Every(charm => EquipCharm(charm));

/// <summary>
/// Unequips a series of charms for the player.
/// </summary>
/// <param name="charms">Charm IDs</param>
/// <returns>Whether all operations are successful</returns>
/// <remarks>The caller should call <see cref="UpdateCharm"/> after changes to charms</remarks>
public static bool UnequipCharms(params int[] charms) {
PlayerData pd = Ref.PD;
return charms.Every(charm => UnequipCharmInternal(pd, charm));
}
public static bool UnequipCharms(params int[] charms) =>
charms.Every(charm => UnequipCharm(charm));

/// <summary>
/// Unequipps all charms the player are equipping.
Expand All @@ -102,7 +129,6 @@ public static void UnequipAllCharms() =>
UnequipCharms(Ref.PD.GetVariable<List<int>>(nameof(PlayerData.equippedCharms)).ToArray());



/// <inheritdoc cref="GotCharm(int)"/>
public static bool GotCharm(Charm charm) => GotCharm((int) charm);

Expand All @@ -125,44 +151,6 @@ public static void UnequipAllCharms() =>
public static bool UnequipCharms(params Charm[] charms) => UnequipCharms(charms.Map(i => (int) i).ToArray());


private static bool EquipCharmInternal(PlayerData pd, int charm) {
if (EquippedCharm(charm) || pd.GetInt(nameof(PlayerData.charmSlotsFilled)) >= pd.GetInt(nameof(PlayerData.charmSlots))) {
return false;
}

pd.IntAdd(nameof(PlayerData.charmSlotsFilled), GetCharmCost(charm));

if (pd.GetInt(nameof(PlayerData.charmSlots)) < pd.GetInt(nameof(PlayerData.charmSlotsFilled))) {
pd.SetBool(nameof(PlayerData.canOvercharm), true);
pd.SetBool(nameof(PlayerData.overcharmed), true);
}

pd.SetBool($"equippedCharm_{charm}", true);
pd.EquipCharm(charm);

return true;
}


private static bool UnequipCharmInternal(PlayerData pd, int charm) {
if (!EquippedCharm(charm)) {
return false;
}

pd.IntAdd(nameof(PlayerData.charmSlotsFilled), -GetCharmCost(charm));

if (pd.GetBool(nameof(PlayerData.overcharmed)) && pd.GetInt(nameof(PlayerData.charmSlotsFilled)) <= pd.GetInt(nameof(PlayerData.charmSlots))) {
pd.SetBool(nameof(PlayerData.overcharmed), false);
}

pd.SetBool($"equippedCharm_{charm}", false);
pd.UnequipCharm(charm);

return true;
}



/// <summary>
/// Updates Charms related UI to proper state.
/// Called by <see cref="UpdateCharm"/> internally.
Expand All @@ -171,12 +159,10 @@ public static void UpdateCharmUI() {
if (!CharmsPaneOpen) {
Ref.GC.gameObject
.Child("HudCamera", "Hud Canvas", "Health", "OC Backboard")!
.SetActive(Ref.PD.GetBool(nameof(PlayerData.overcharmed)));
.SetActive(PlayerDataR.overcharmed);
return;
}

PlayerData pd = Ref.PD;

GameObject charmsPane = CharmsPane;
GameObject equippedCharms = charmsPane.Child("Equipped Charms")!;
GameObject textEquipped = equippedCharms.Child("Text Equipped")!;
Expand All @@ -193,9 +179,9 @@ public static void UpdateCharmUI() {
ReflectionHelper.CallMethod(equippedCharms.GetComponent<BuildEquippedCharms>(), "BuildCharmList");
FSMUtility.SendEventToGameObject(equippedCharms, "UP");

int overSlots = pd.GetInt(nameof(PlayerData.charmSlotsFilled)) - pd.GetInt(nameof(PlayerData.charmSlots));
int overSlots = PlayerDataR.charmSlotsFilled - PlayerDataR.charmSlots;

if (pd.GetBool(nameof(PlayerData.overcharmed))) {
if (PlayerDataR.overcharmed) {
textOvercharmed.SetActive(true);
textEquipped.SetActive(false);
overCtrlFsm.FsmVariables.GetFsmInt("Cost").Value = overSlots;
Expand Down
1 change: 1 addition & 0 deletions Osmi/Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
global using USceneManager = UnityEngine.SceneManagement.SceneManager;

global using static Osmi.Logger;
global using static HKReflect.Singletons;
3 changes: 2 additions & 1 deletion Osmi/Osmi.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>Osmi</AssemblyTitle>
<Version>0.2.1</Version>
<Version>0.2.2</Version>
<Description>A Hollow Knight library mod</Description>
<Authors>Clazex</Authors>

Expand Down Expand Up @@ -112,6 +112,7 @@

<PackageReference Include="Fody" Version="6.6.4" PrivateAssets="all" />
<PackageReference Include="InfoOf.Fody" Version="2.1.1" PrivateAssets="all" />
<PackageReference Include="HKReflect.Fody" Version="0.2.0" PrivateAssets="all" />
</ItemGroup>

<Target Name="CopyMod" AfterTargets="PostBuildEvent">
Expand Down
10 changes: 5 additions & 5 deletions Osmi/OsmiHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static OsmiHooks() {
))
.Emit(OpCodes.Ldarg_0) // this
.Emit(OpCodes.Ldarg_1) // hitInstance
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), "Osmi.OsmiHooks", nameof(OnHitEnemy)));
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), nameof(Osmi) + '.' + nameof(OsmiHooks), nameof(OnHitEnemy)));

_ = new ILHook(
Info.OfMethod<GameManager>(nameof(GameManager.PauseGameToggle))
Expand All @@ -251,15 +251,15 @@ static OsmiHooks() {
.GotoNext(
MoveType.Before,
i => i.MatchLdcR4(0f),
i => i.MatchCallvirt(typeof(GameManager), "SetTimeScale")
i => i.MatchCallvirt(typeof(GameManager), nameof(HKReflect.GameManager.SetTimeScale))
)
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), "Osmi.OsmiHooks", nameof(OnGamePause)))
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), nameof(Osmi) + '.' + nameof(OsmiHooks), nameof(OnGamePause)))
.GotoNext(
MoveType.Before,
i => i.MatchLdcR4(1f),
i => i.MatchCallvirt(typeof(GameManager), "SetTimeScale")
i => i.MatchCallvirt(typeof(GameManager), nameof(HKReflect.GameManager.SetTimeScale))
)
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), "Osmi.OsmiHooks", nameof(OnGameUnpause)))
.Emit(OpCodes.Call, Info.OfMethod(nameof(Osmi), nameof(Osmi) + '.' + nameof(OsmiHooks), nameof(OnGameUnpause)))
);

On.HeroController.Start += (orig, self) => {
Expand Down

0 comments on commit ede24e9

Please sign in to comment.