summaryrefslogtreecommitdiff
path: root/Utility.cs
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-11-11 21:16:07 -0500
committerAndrew Opalach <andrew@akon.city> 2026-11-11 21:21:50 -0500
commit72083036f686f8d6e5b7def5db4b401e3f351aba (patch)
tree5da70c56cd15b28254aea89b6aa13016f593f636 /Utility.cs
downloadPlayerPoser-master.tar.gz
PlayerPoser-master.tar.bz2
PlayerPoser-master.zip
Branch off player related codeHEADmaster
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'Utility.cs')
-rw-r--r--Utility.cs214
1 files changed, 214 insertions, 0 deletions
diff --git a/Utility.cs b/Utility.cs
new file mode 100644
index 0000000..6ea08f1
--- /dev/null
+++ b/Utility.cs
@@ -0,0 +1,214 @@
+#define ENABLE_ASSERTS
+
+using System.Numerics;
+using System.Text;
+using System.Globalization;
+#if ENABLE_ASSERTS
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+#endif
+
+using SharpPluginLoader.Core;
+using SharpPluginLoader.Core.MtTypes;
+using SharpPluginLoader.Core.Memory;
+using SharpPluginLoader.Core.Entities;
+
+namespace PlayerPoser
+{
+ using static Plugin;
+
+ internal static unsafe class Utility
+ {
+ internal const float disableAlphaFactor = 0.6f;
+
+ private static bool loggedAssertFailed = false;
+ internal static void Assert(bool condition, [CallerLineNumber] int line = 0)
+ {
+ if (!condition && !loggedAssertFailed)
+ {
+ Log.Error($"Assert failed on line {line}, further asserts won't be logged.");
+ loggedAssertFailed = true;
+ }
+#if ENABLE_ASSERTS
+ Trace.Assert(condition);
+#endif
+ }
+
+ internal static byte ByteFlag(bool f) { return f ? (byte)0x1 : (byte)0x0; }
+
+ internal static T Get<T>(nint addr, nint offset = 0x0) where T : unmanaged
+ {
+ return MemoryUtil.Read<T>(addr + offset);
+ }
+
+ internal static T Get<T>(long addr, nint offset = 0x0) where T : unmanaged
+ {
+ return MemoryUtil.Read<T>((nint)addr + offset);
+ }
+
+ internal static T *GetPtrInline<T>(nint addr, nint offset = 0x0) where T : unmanaged
+ {
+ return (T *)(addr + offset);
+ }
+
+ internal static ref T GetRef<T>(nint addr, nint offset = 0x0) where T : unmanaged
+ {
+ return ref MemoryUtil.GetRef<T>(addr + offset);
+ }
+
+ internal static int ColorVectorToInt(Vector4 v)
+ {
+ return ((int)MathF.Round(v.X * 255.0f) |
+ ((int)MathF.Round(v.Y * 255.0f) << 8) |
+ ((int)MathF.Round(v.Z * 255.0f) << 16) |
+ ((int)MathF.Round(v.W * 255.0f) << 24));
+ }
+
+ internal static byte[] StringToCStr(string s)
+ {
+ return Encoding.ASCII.GetBytes(s + '\0');
+ }
+
+ internal static float StringToSingle(string s) { return Convert.ToSingle(s, CultureInfo.InvariantCulture); }
+
+ internal static Vector3 StringToVector3(string s, char delim = ',')
+ {
+ if (string.IsNullOrEmpty(s)) throw new FormatException();
+ string[] sp = s.Split(delim).Select((v) => v.Trim()).ToArray();
+ if (sp.Length != 3) throw new FormatException();
+ return new Vector3(StringToSingle(sp[0]), StringToSingle(sp[1]), StringToSingle(sp[2]));
+ }
+
+ internal static Vector4 StringToVector4(string s, char delim = ',')
+ {
+ if (string.IsNullOrEmpty(s)) throw new FormatException();
+ string[] sp = s.Split(delim).Select((v) => v.Trim()).ToArray();
+ if (sp.Length != 4) throw new FormatException();
+ return new Vector4(StringToSingle(sp[0]), StringToSingle(sp[1]), StringToSingle(sp[2]), StringToSingle(sp[3]));
+ }
+
+ internal static string Vector3ToString(Vector3 v)
+ {
+ return string.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", v.X, v.Y, v.Z);
+ }
+
+ internal static string Vector4ToString(Vector4 v)
+ {
+ return string.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", v.X, v.Y, v.Z, v.W);
+ }
+
+ internal static void getDirectionVectors(MtQuaternion rotation, out Vector3 forward, out Vector3 right, out Vector3 up)
+ {
+ Quaternion qRotation = new(rotation.X, rotation.Y, rotation.Z, rotation.W);
+ forward = Vector3.Transform(new(0.0f, 0.0f, 1.0f), qRotation);
+ right = Vector3.Transform(new(1.0f, 0.0f, 0.0f), qRotation);
+ up = Vector3.Transform(new(0.0f, 1.0f, 0.0f), qRotation);
+ }
+
+ private static Player? lastPlayer = null;
+ private static int lastSaveSlotIndex = -1;
+
+ private delegate nint DestroyPlayerObject(nint playerObjectInternal, int unknownInt);
+ private static Hook<DestroyPlayerObject>? destroyPlayerObjectHook;
+
+ private static NativeFunction<nint, nint> getSaveSlotGuiAddr;
+ private static NativeFunction<nint, nint> getPlayerSettingsAddr;
+
+ private static NativeFunction<nint, int, nint> getPalicoFunc;
+
+ internal static void OnUpdate()
+ {
+ (lastPlayer, lastSaveSlotIndex) = getPlayerWithSaveSlot();
+ }
+
+ internal static Player? getPlayer()
+ {
+ return lastPlayer;
+ }
+
+ internal static int getSaveSlotIndex()
+ {
+ return lastSaveSlotIndex;
+ }
+
+ internal static NativeWrapper getPlayerSettings()
+ {
+ Assert(lastPlayer != null);
+ if (lastSaveSlotIndex >= 0)
+ {
+ // MonsterHunterWorld.exe+ADC442 - imul rdx,rcx,0026CC00
+ nint rax = lastSaveSlotIndex * 0x26CC00;
+ rax += sUserData.Get<nint>(0xA8);
+ return new(rax);
+ }
+ // MonsterHunterWorld.exe+1B8DBB0 - movsxd rax,dword ptr [rcx+000000A0]
+ return new(getPlayerSettingsAddr.Invoke(sUserData.Instance));
+ }
+
+ internal static NativeWrapper getPalico()
+ {
+ return new(getPalicoFunc.Invoke(sOtomo!.Instance, 0));
+ }
+
+ private static (Player?, int) getPlayerFromSaveSlot()
+ {
+ nint guiAddr = getSaveSlotGuiAddr.Invoke(sMhArea.Instance);
+ if (guiAddr != 0x0)
+ {
+ // MonsterHunterWorld.exe+ADC42F - movsxd rcx,dword ptr [rax+00000334]
+ int saveSlotIndex = Get<int>(guiAddr, 0x334);
+ if (saveSlotIndex != -1)
+ {
+ // MonsterHunterWorld.exe+1B42047 - add r8,00000740
+ nint offset = (sPlayer!.Instance + 0x40) + (0x740 * saveSlotIndex);
+ if (offset != 0x0 && Get<nint>(offset) == 0x1433FE588)
+ {
+ nint playerAddr = Get<nint>(offset, 0x18);
+ if (playerAddr != 0x0)
+ {
+ return (new Player(playerAddr), saveSlotIndex);
+ }
+ }
+ }
+ }
+ return (null, -1);
+ }
+
+ private static (Player?, int) getPlayerWithSaveSlot()
+ {
+ Player? player = Player.MainPlayer;
+ int saveSlotIndex = -1;
+ if (player == null)
+ {
+ (player, saveSlotIndex) = getPlayerFromSaveSlot();
+ }
+ if (player != lastPlayer)
+ {
+ lastPlayer = player;
+ lastSaveSlotIndex = saveSlotIndex;
+ OnPlayerChange();
+ }
+ return (player, saveSlotIndex);
+ }
+
+ private static nint DestroyPlayerObjectHook(nint playerObjectInternal, int unknownInt)
+ {
+ if (playerObjectInternal == lastPlayer)
+ {
+ lastPlayer = null;
+ lastSaveSlotIndex = -1;
+ }
+ return destroyPlayerObjectHook!.Original(playerObjectInternal, unknownInt);
+ }
+
+ internal static void Initialize()
+ {
+ destroyPlayerObjectHook = Hook.Create<DestroyPlayerObject>(0x142033500, DestroyPlayerObjectHook);
+
+ getSaveSlotGuiAddr = new NativeFunction<nint, nint>(0x141AC0B60);
+ getPlayerSettingsAddr = new NativeFunction<nint, nint>(0x141B8DBB0);
+
+ getPalicoFunc = new NativeFunction<nint, int, nint>(0x141B32E00);
+ }
+ }
+}