refactor: replace functions with xivcommon

This commit is contained in:
Anna 2021-04-11 09:46:18 -04:00
parent 07f7ea581c
commit 46f49156f9
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
6 changed files with 15 additions and 112 deletions

View File

@ -1,86 +0,0 @@
using Dalamud.Plugin;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
namespace Macrology {
public class GameFunctions {
private Macrology Plugin { get; }
private delegate IntPtr GetUiModuleDelegate(IntPtr basePtr);
private delegate void EasierProcessChatBoxDelegate(IntPtr uiModule, IntPtr message, IntPtr unused, byte a4);
private readonly GetUiModuleDelegate _getUiModule;
private readonly EasierProcessChatBoxDelegate _easierProcessChatBox;
private readonly IntPtr _uiModulePtr;
public GameFunctions(Macrology plugin) {
this.Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null");
var getUiModulePtr = this.Plugin.Interface.TargetModuleScanner.ScanText("E8 ?? ?? ?? ?? 48 83 7F ?? 00 48 8B F0");
var easierProcessChatBoxPtr = this.Plugin.Interface.TargetModuleScanner.ScanText("48 89 5C 24 ?? 57 48 83 EC 20 48 8B FA 48 8B D9 45 84 C9");
this._uiModulePtr = this.Plugin.Interface.TargetModuleScanner.GetStaticAddressFromSig("48 8B 0D ?? ?? ?? ?? 48 8D 54 24 ?? 48 83 C1 10 E8 ?? ?? ?? ??");
if (getUiModulePtr == IntPtr.Zero || easierProcessChatBoxPtr == IntPtr.Zero || this._uiModulePtr == IntPtr.Zero) {
PluginLog.Log($"getUIModulePtr: {getUiModulePtr.ToInt64():x}");
PluginLog.Log($"easierProcessChatBoxPtr: {easierProcessChatBoxPtr.ToInt64():x}");
PluginLog.Log($"this.uiModulePtr: {this._uiModulePtr.ToInt64():x}");
throw new ApplicationException("Got null pointers for game signature(s)");
}
this._getUiModule = Marshal.GetDelegateForFunctionPointer<GetUiModuleDelegate>(getUiModulePtr);
this._easierProcessChatBox = Marshal.GetDelegateForFunctionPointer<EasierProcessChatBoxDelegate>(easierProcessChatBoxPtr);
}
public void ProcessChatBox(string message) {
var uiModule = this._getUiModule(Marshal.ReadIntPtr(this._uiModulePtr));
if (uiModule == IntPtr.Zero) {
throw new ApplicationException("uiModule was null");
}
using var payload = new ChatPayload(message);
var mem1 = Marshal.AllocHGlobal(400);
Marshal.StructureToPtr(payload, mem1, false);
this._easierProcessChatBox(uiModule, mem1, IntPtr.Zero, 0);
Marshal.FreeHGlobal(mem1);
}
}
[StructLayout(LayoutKind.Explicit)]
[SuppressMessage("ReSharper", "PrivateFieldCanBeConvertedToLocalVariable")]
internal readonly struct ChatPayload : IDisposable {
[FieldOffset(0)]
private readonly IntPtr textPtr;
[FieldOffset(16)]
private readonly ulong textLen;
[FieldOffset(8)]
private readonly ulong unk1;
[FieldOffset(24)]
private readonly ulong unk2;
internal ChatPayload(string text) {
var stringBytes = Encoding.UTF8.GetBytes(text);
this.textPtr = Marshal.AllocHGlobal(stringBytes.Length + 30);
Marshal.Copy(stringBytes, 0, this.textPtr, stringBytes.Length);
Marshal.WriteByte(this.textPtr + stringBytes.Length, 0);
this.textLen = (ulong) (stringBytes.Length + 1);
this.unk1 = 64;
this.unk2 = 0;
}
public void Dispose() {
Marshal.FreeHGlobal(this.textPtr);
}
}
}

View File

@ -2,7 +2,9 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ILRepacker" AfterTargets="Build">
<ItemGroup>
<InputAssemblies Include="$(OutputPath)\*.dll"/>
<InputAssemblies Include="$(OutputPath)\$(AssemblyName).dll"/>
<InputAssemblies Include="$(OutputPath)\*.dll"
Exclude="$(OutputPath)\$(AssemblyName).dll"/>
</ItemGroup>
<ILRepack

View File

@ -144,7 +144,7 @@ namespace Macrology {
}
// send the message as if it were entered in the chat box
this.Plugin.Functions.ProcessChatBox(command);
this.Plugin.Common.Functions.Chat.SendMessage(command);
}
private static TimeSpan? ExtractWait(ref string command) {

View File

@ -1,6 +1,7 @@
using Dalamud.Game.Command;
using Dalamud.Plugin;
using System;
using XivCommon;
namespace Macrology {
public class Macrology : IDalamudPlugin {
@ -9,7 +10,7 @@ namespace Macrology {
public string Name => "Macrology";
public DalamudPluginInterface Interface { get; private set; } = null!;
public GameFunctions Functions { get; private set; } = null!;
public XivCommonBase Common { get; private set; } = null!;
public PluginUi Ui { get; private set; } = null!;
public MacroHandler MacroHandler { get; private set; } = null!;
public Configuration Config { get; private set; } = null!;
@ -17,7 +18,7 @@ namespace Macrology {
public void Initialize(DalamudPluginInterface pluginInterface) {
this.Interface = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface), "DalamudPluginInterface cannot be null");
this.Functions = new GameFunctions(this);
this.Common = new XivCommonBase(this.Interface);
this.Ui = new PluginUi(this);
this.MacroHandler = new MacroHandler(this);
this.Config = Configuration.Load(this) ?? new Configuration();

View File

@ -8,26 +8,27 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dalamud, Version=5.2.4.3, Culture=neutral, PublicKeyToken=null">
<Reference Include="Dalamud">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Dalamud.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ImGui.NET, Version=1.72.0.0, Culture=neutral, PublicKeyToken=null">
<Reference Include="ImGui.NET">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\ImGui.NET.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ImGuiScene, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<Reference Include="ImGuiScene">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\ImGuiScene.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<Reference Include="Newtonsoft.Json">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="DalamudPackager" Version="1.2.1"/>
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.18.2"/>
<PackageReference Include="System.Threading.Channels" Version="5.0.0"/>
<PackageReference Include="DalamudPackager" Version="1.2.1" />
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.18.2" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="XivCommon" Version="1.1.0" />
</ItemGroup>
</Project>

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>