From 72083036f686f8d6e5b7def5db4b401e3f351aba Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 11 Nov 2026 21:16:07 -0500 Subject: Branch off player related code Signed-off-by: Andrew Opalach --- Models.cs | 568 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 568 insertions(+) create mode 100644 Models.cs (limited to 'Models.cs') diff --git a/Models.cs b/Models.cs new file mode 100644 index 0000000..4073716 --- /dev/null +++ b/Models.cs @@ -0,0 +1,568 @@ +using System.Numerics; +using System.Text.RegularExpressions; + +using ImGuiNET; +using SharpPluginLoader.Core.Entities; +using SharpPluginLoader.Core.Actions; +using SharpPluginLoader.Core.Components; + +namespace PlayerPoser +{ + using static Utility; + + internal static unsafe class Models + { + private static string modelFilter = ""; + private static bool modelFilterIsRegex = false; + private static Regex? modelRegex = null; + internal static bool modelOnlyAddressLod0 = false; + + private static string objectFilter = ""; + private static bool sortObjectsByDistanceToPlayer = false; + + private static int inputLmt = -1; + private static int inputAnimationId = -1; + private static int inputSet = -1; + private static int inputActionId = -1; + + internal static Vector4 rotateVector4(Vector4 rotation, Vector3 offset) + { + Quaternion rotationQ = new(rotation.X, rotation.Y, rotation.Z, rotation.W); + rotationQ *= Quaternion.CreateFromYawPitchRoll(offset.X, offset.Y, offset.Z); + return new(rotationQ.X, rotationQ.Y, rotationQ.Z, rotationQ.W); + } + + internal static bool drawRotate(string label, Vector4 rotation, out Vector4 result) + { + Vector3 offset = default; + if (ImGui.DragFloat3(label, ref offset, 0.0025f)) + { + result = rotateVector4(rotation, offset); + return true; + } + result = new(-1.0f); + return false; + } + + internal static string extractPartName(string fullString) + { + string partName = ""; + if (fullString.StartsWith("pl\\f_equip") || fullString.StartsWith("pl\\m_equip")) + { + partName = fullString.Split('\\')[3]; + } + else if (fullString.StartsWith("wp\\")) + { + string[] split = fullString.Split('\\'); + partName = (split[4] == "mod") ? split[5] : split[4]; + } + else if (fullString.StartsWith("pl\\hair")) + { + partName = fullString.Split('\\')[2]; + } + else if (fullString.StartsWith("pl\\f_face") || fullString.StartsWith("pl\\m_face")) + { + partName = fullString.Split('\\')[4].Substring(2); + } + return partName; + } + + internal static void drawModelParts(nint armorAddr, nint resourceAddr = 0x0, bool hideAll = false, bool showAll = false) + { + if (resourceAddr == 0x0) + { + if (armorAddr == 0x0 || (resourceAddr = Get(armorAddr, 0x2A0)) == 0x0) + { + return; + } + } + ImGui.PushID(resourceAddr); + string fullString = new(GetPtrInline(resourceAddr, 0xC)); + if (armorAddr != 0x0) + { + string partName = extractPartName(fullString); + ImGui.Text($"{partName} (0x{armorAddr:X}, {fullString} @ 0x{resourceAddr:X}):"); + } + else + { + ImGui.Text($"{fullString} @ 0x{resourceAddr:X}:"); + } + // MonsterHunterWorld.exe+222E651 - mov rax,[r8+000000C8] + nint baseLod = Get(resourceAddr, 0xC8); + nint baseOffset = Get(resourceAddr, 0x408 + (0x8 * 0x1)); + int numParts = Get(resourceAddr, 0x548 + (0x4 * 0x1)); + // Setting lodCount to 0 is probably the best way to completely hide a model. + int lodCount = Get(resourceAddr, 0xD0); + const int lodStructSize = 0x50; + const int partsPerLine = 10; + bool allOn = ImGui.Button("Show All") || showAll; + ImGui.SameLine(); + bool allOff = ImGui.Button("Hide All") || hideAll; + for (int i = 0; i < numParts; i++) + { + int lodOffset = Get(baseOffset, i * 0x4); + nint lod0 = baseLod + (lodOffset * lodStructSize); + ushort lod0Visible = Get(lod0); + bool isVisible = lod0Visible != 0; + bool togglePart = ImGui.Checkbox($"##Part #{i}", ref isVisible); + if (ImGui.BeginItemTooltip()) + { + ImGui.Text($"Part #{i} (0x{lod0:X})"); + ImGui.EndTooltip(); + } + int lastLod = (i < numParts - 1) ? Get(baseOffset, (i + 1) * 0x4) : lodCount; + int partLodCount = lastLod - lodOffset; + // This is not a "proper" way to hide a part. It abuses the fact that if the lower + // 16 bits (lodVisible) of these 4 bytes are 0, a check to skip rendering it will + // always pass. Also that +0xA appears to be unused/padding. + // MonsterHunterWorld.exe+222FCE3 - mov eax,[r8] + for (int j = 0; j < partLodCount; j++) + { + if (togglePart || (lod0Visible != 0 && allOff) || (lod0Visible == 0 && allOn)) + { + nint lod = lod0 + (j * lodStructSize); + ref ushort lodVisible = ref GetRef(lod); + ref ushort padSub = ref GetRef(lod, 0xA); + if (lod0Visible != 0) Assert(padSub == 0); + // Check against LOD0 to prevent an inconsistent state. + if ((lodVisible != 0) == (lod0Visible != 0)) + { + (lodVisible, padSub) = (padSub, lodVisible); + } + } + if (modelOnlyAddressLod0) break; + } + if (i != numParts - 1 && i % partsPerLine != partsPerLine - 1) + { + ImGui.SameLine(); + } + } + ImGui.PopID(); + } + + internal static void runAction(ActionController actionController, int actionSet, int actionId) + { + actionController.DoAction(actionSet, actionId); + /* + actionController.GetRef(0xC0) = actionId; + actionController.GetRef(0xBC) = actionSet; + */ + } + + internal static void drawAnimationInfo(Entity entity, float width, bool debug) + { + ActionController actionController = entity.ActionController; + ActionInfo currentActionInfo = actionController.CurrentAction; + SharpPluginLoader.Core.Actions.Action? currentAction = null; + if (currentActionInfo.ActionSet >= 0 && currentActionInfo.ActionSet <= 3) + { + ActionList actionList = actionController.GetActionList(currentActionInfo.ActionSet); + if (currentActionInfo.ActionId >= 0 && currentActionInfo.ActionId < actionList.Count) + { + currentAction = actionList[currentActionInfo.ActionId]; + } + } + if (debug) + { + ImGui.Text($"ActionController: 0x{actionController.Instance:X}"); + ImGui.Text($" Current: {currentAction} {currentActionInfo}"); + if (currentAction != null) + { + + ImGui.Text($" Active Time: {currentAction.ActiveTime}"); + ImGui.Text($" Delta Sec: {currentAction.DeltaSec}"); + } + } + AnimationId currentAnimation = entity.CurrentAnimation; + AnimationLayerComponent? animationLayer = entity.AnimationLayer; + if (animationLayer != null) + { + ImGui.Text(debug ? $"AnimationLayer: 0x{animationLayer.Instance:X}" : "Animation"); + + if (debug) + { + ImGui.Text($" Current: {currentAnimation}"); + ImGui.Text($" Frame: {animationLayer.CurrentFrame:0.000}/{animationLayer.MaxFrame:0.000}"); + } + else + { + ImGui.Text($" Current: {currentAnimation.Lmt}.{currentAnimation.Id}, {(currentAction != null ? currentAction.Name : "None")} ({currentActionInfo.ActionSet}.{currentActionInfo.ActionId})"); + if (ImGui.BeginItemTooltip()) + { + ImGui.Text("{Lmt}.{Id}, {Action} ({Set}.{Id})"); + ImGui.EndTooltip(); + } + ImGui.SetNextItemWidth(width * 0.85f); + ImGui.SliderFloat("Frame", ref animationLayer.CurrentFrame, 0.0f, animationLayer.MaxFrame, "%.3f"); + bool animationPaused = animationLayer.Paused; + if (ImGui.Checkbox("Paused", ref animationPaused)) + { + animationLayer.Paused = animationPaused; + } + ImGui.SameLine(); + float? lockedSpeed = animationLayer.LockedSpeed; + float speed = lockedSpeed ?? animationLayer.Speed; + bool animationLocked = lockedSpeed != null; + if (ImGui.Checkbox("Set", ref animationLocked)) + { + if (animationLocked) + { + animationLayer.LockSpeed(speed); + } + else + { + animationLayer.UnlockSpeed(); + } + } + ImGui.SameLine(); + ImGui.SetNextItemWidth(width * 0.2f); + if (ImGui.DragFloat($"Speed", ref speed, 0.01f, 0.0f, 0.0f, "%.3f")) + { + if (animationLocked) animationLayer.LockSpeed(speed); + } + } + + if (debug) + { + ImGui.PushItemWidth(width * 0.075f); + ImGui.PushID("Animation"); + bool animationValid = inputLmt >= 0 && inputAnimationId >= 0; + if (!animationValid) + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * 0.5f); + } + bool doAnimation = ImGui.Button("Do"); + if (!animationValid) + { + ImGui.PopStyleVar(); + ImGui.PopItemFlag(); + } + ImGui.SameLine(); + ImGui.InputInt("Lmt", ref inputLmt, 0); + ImGui.SameLine(); + ImGui.InputInt("Animation Id", ref inputAnimationId, 0); + ImGui.PopID(); + ImGui.PushID("Action"); + bool actionValid = inputSet >= 0 && inputActionId >= 0; + if (!actionValid) + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * 0.5f); + } + bool doAction = ImGui.Button("Do"); + if (!actionValid) + { + ImGui.PopStyleVar(); + ImGui.PopItemFlag(); + } + ImGui.SameLine(); + ImGui.InputInt("Set", ref inputSet, 0); + ImGui.SameLine(); + float widthDiff = ImGui.CalcTextSize("Lmt").X - ImGui.CalcTextSize("Set").X; + ImGui.SetCursorPos(ImGui.GetCursorPos() + new Vector2(widthDiff, 0.0f)); + ImGui.InputInt("Action Id", ref inputActionId, 0); + ImGui.PopID(); + if (!(animationValid && actionValid)) + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * 0.5f); + } + if (ImGui.Button("Do Both")) + { + doAnimation = true; + doAction = true; + } + if (!(animationValid && actionValid)) + { + ImGui.PopStyleVar(); + ImGui.PopItemFlag(); + } + if (doAction) + { + runAction(actionController, inputSet, inputActionId); + } + if (doAnimation) + { + animationLayer.DoAnimation(new AnimationId((uint)inputLmt, (uint)inputAnimationId)); + } + ImGui.PopItemWidth(); + } + } + } + + private static void iterList(nint baseAddr, Action func) + { + nint next = baseAddr; + nint head = next; + nint prev = Get(next, 0x10); + for (;next != 0x0;) + { + func(next + 0x40); + if (head == prev) + { + next = Get(next, 0x10); + } + else + { + next = Get(next, 0x18); + if (next == 0x0) + { + head = prev; + next = prev; + } + } + } + } + + internal static void drawModelList(float width, nint baseObject) + { + ImGui.PushID("Models"); + ImGui.Text("Filter"); + bool updateRegex = false; + bool invalidRegex = modelFilterIsRegex && modelFilter != "" && modelRegex == null; + if (invalidRegex) + { + ImGui.PushStyleColor(ImGuiCol.Text, 0xFF0000FF); + } + ImGui.SetNextItemWidth(width * 0.55f); + if (ImGui.InputText("##Filter", ref modelFilter, 256)) + { + if (modelFilterIsRegex) + { + updateRegex = true; + } + } + if (invalidRegex) + { + ImGui.PopStyleColor(); + } + ImGui.SameLine(); + if (ImGui.Button("Clear")) + { + modelFilter = ""; + if (modelFilterIsRegex) + { + updateRegex = true; + } + } + ImGui.SameLine(); + if (ImGui.Checkbox("Regex", ref modelFilterIsRegex)) + { + if (modelFilterIsRegex) + { + updateRegex = true; + } + } + if (updateRegex) + { + if (String.IsNullOrEmpty(modelFilter)) + { + modelRegex = null; + } + else + { + try + { + modelRegex = new Regex(modelFilter); + } + catch (ArgumentException) + { + modelRegex = null; + } + } + } + bool hideEverything = ImGui.Button("Hide Everything"); + ImGui.SameLine(); + bool showEverything = ImGui.Button("Show Everything"); + nint baseObjectResource = Get(baseObject, 0x2A0); + if (baseObjectResource != 0x0) + { + iterList(baseObjectResource - 0x40, (partAddr) => + { + ImGui.PushID(partAddr); + nint vTable = Get(partAddr); + if (vTable == 0x143507A18) + { + string fullString = new(GetPtrInline(partAddr, 0xC)); + bool filterMatch = false; + if (modelFilterIsRegex) + { + filterMatch = modelRegex == null || modelRegex.Match(fullString).Success; + } + else + { + filterMatch = String.IsNullOrEmpty(modelFilter) || fullString.StartsWith(modelFilter); + } + if (filterMatch) + { + drawModelParts(0x0, partAddr, hideEverything, showEverything); + } + } + ImGui.PopID(); + }); + } + ImGui.PopID(); + } + + private static void searchUnitList(long addr, List objects) + { + nint baseAddr = Get(addr); + if (baseAddr == 0x0) return; + baseAddr = Get(baseAddr, 0xB8); + if (baseAddr == 0x0) return; + iterList(baseAddr + 0x40, (objectAddr) => + { + nint vTable = Get(objectAddr); + if (vTable == 0x14341F728 || vTable == 0x1434A9720) // Instanced Model || uScrMotionModel + { + objects.Add(objectAddr); + } + }); + } + + internal static void drawObjectList(float width, Player player) + { + List objects = new(); + searchUnitList(0x1450ECA20, objects); + searchUnitList(0x1450EC980, objects); + ImGui.PushID("Objects"); + ImGui.Text("Filter"); + ImGui.SetNextItemWidth(width * 0.55f); + ImGui.InputText("##Filter", ref objectFilter, 256); + ImGui.Checkbox("Sort By Distance To Player", ref sortObjectsByDistanceToPlayer); + IEnumerable enumerator = objects; + if (sortObjectsByDistanceToPlayer) + { + enumerator = Enumerable.OrderBy(objects, (x) => x, Comparer.Create((x, y) => + { + Vector3 pos = player.Position; + Vector3 xPos = default, yPos = default; + nint vTableX = Get(x); + nint vTableY = Get(y); + if (vTableX == 0x1434A9720) + { + xPos = Get(x, 0x160); + } + else if (vTableX == 0x14341F728) + { + bool instanced = (Get(x, 0x2C8) & 0x10) != 0x10; + if (instanced) + { + xPos = Get(x, 0x500); + } + else + { + xPos = Get(x, 0x160); + } + } + if (vTableY == 0x1434A9720) + { + yPos = Get(y, 0x160); + } + else if (vTableY == 0x14341F728) + { + bool instanced = (Get(y, 0x2C8) & 0x10) != 0x10; + if (instanced) + { + yPos = Get(y, 0x500); + } + else + { + yPos = Get(y, 0x160); + } + } + float distX = Vector3.Distance(pos, xPos); + float distY = Vector3.Distance(pos, yPos); + if (distX < distY) return -1; + else if (distX > distY) return 1; + return 0; + })); + } + foreach (nint objectAddr in enumerator) + { + nint resourceAddr = Get(objectAddr, 0x2A0); + string fullString = new(GetPtrInline(resourceAddr, 0xC)); + bool filterMatch = string.IsNullOrEmpty(objectFilter) || (!string.IsNullOrEmpty(fullString) && fullString.StartsWith(objectFilter)); + if (!filterMatch) + { + continue; + } + ImGui.PushID(objectAddr); + if (ImGui.CollapsingHeader($"{fullString} @ 0x{objectAddr:X}")) + { + nint vTable = Get(objectAddr); + if (vTable == 0x1434A9720) + { + ImGui.DragFloat3("Position", ref GetRef(objectAddr, 0x160), 0.5f); + } + else + { + bool update = GetRef(objectAddr, 0x540) == 0x1; + if (ImGui.Checkbox("Update", ref update)) + { + GetRef(objectAddr, 0x540) = ByteFlag(update); + } + ImGui.SameLine(); + bool instanced = (GetRef(objectAddr, 0x2C8) & 0x10) == 0x10; + if (ImGui.Checkbox("Instanced", ref instanced)) + { + if (instanced) + { + GetRef(objectAddr, 0x2C8) |= 0x10; + } + else + { + GetRef(objectAddr, 0x2C8) &= 0xF; + GetRef(objectAddr, 0x160) = GetRef(objectAddr, 0x500); + } + } + if (instanced) + { + ImGui.DragFloat3("Position", ref GetRef(objectAddr, 0x500), 0.5f); + int instanceCount = Get(objectAddr, 0x4C0); + nint instances = Get(objectAddr, 0x4B8); + ImGui.Text($"Instances: {instanceCount} (0x{instances:X})"); + for (int i = 0; i < instanceCount; i++) + { + ImGui.PushID(instances); + ref Vector3 position = ref GetRef(instances, 0xF0); + ref Vector4 rotation = ref GetRef(instances, 0x100); + ref Vector3 scale = ref GetRef(instances, 0x110); + ImGui.DragFloat3("Position", ref position, 0.5f); + ImGui.SliderFloat4("Rotation", ref rotation, -1.0f, 1.0f); + if (drawRotate("Rotate", rotation, out Vector4 result)) + { + rotation = result; + } + ImGui.DragFloat3("Scale", ref scale, 0.025f); + ImGui.Separator(); + ImGui.PopID(); + instances += 0x1D0; + } + } + else + { + ref Vector3 position = ref GetRef(objectAddr, 0x160); + ref Vector4 rotation = ref GetRef(objectAddr, 0x170); + ref Vector3 scale = ref GetRef(objectAddr, 0x180); + ImGui.DragFloat3("Position", ref position, 0.5f); + if (ImGui.Button("Move to Player")) + { + position = new(player.Position.X, player.Position.Y - 150.0f, player.Position.Z); + } + ImGui.SliderFloat4("Rotation", ref rotation, -1.0f, 1.0f); + if (drawRotate("Rotate", rotation, out Vector4 result)) + { + rotation = result; + } + ImGui.DragFloat3("Scale", ref scale, 0.025f); + } + } + } + ImGui.PopID(); + } + ImGui.PopID(); + } + } +} -- cgit v1.2.3-101-g0448