Skip to content

Commit

Permalink
feat: Protections to avoid crashing client after update with inventor…
Browse files Browse the repository at this point in the history
…y patch. Changes to BaseSettings and addition of LL settings file
  • Loading branch information
nt153133 committed Jul 8, 2024
1 parent a96a7ba commit 35bfef2
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 19 deletions.
12 changes: 10 additions & 2 deletions Hooks/PatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class PatchManager
//public static List<DirectAsmPatch> DirectAsmPatches { get; } = new();
public static bool Initialized { get; set; }

public static bool Initialize()
public static bool Initialize(bool skipInventory = false)
{
Log.Information("Initializing PatchManager");
if (Initialized)
Expand All @@ -23,7 +23,15 @@ public static bool Initialize()
}

//Hooks.Add(new InstanceQuestDungeonHook());
Hooks.Add(new InventoryUpdatePatch());

if (!skipInventory)
{
Hooks.Add(new InventoryUpdatePatch());
}
else
{
Log.Information("Skipping Inventory Patch");
}

foreach (var hook in Hooks)
{
Expand Down
80 changes: 66 additions & 14 deletions Memory/OffsetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You should have received a copy of the license along with this
using LlamaLibrary.Memory.Attributes;
using LlamaLibrary.Memory.PatternFinders;
using LlamaLibrary.RemoteAgents;
using LlamaLibrary.Settings;
using Newtonsoft.Json;
using LogLevel = LlamaLibrary.Logging.LogLevel;
using PatchManager = LlamaLibrary.Hooks.PatchManager;
Expand Down Expand Up @@ -64,13 +65,16 @@ public static class OffsetManager

private static string OffsetFile => Path.Combine(JsonSettings.SettingsPath, $"LL_Offsets_{Core.CurrentGameVer}.json");

public static LLogger Logger { get; } = new("LLOffsetManager", Colors.RosyBrown, LogLevel.Debug);
public static LLogger Logger { get; } = new("LLOffsetManager", Colors.RosyBrown, LogLevel.Information);

#if RB_CN
public const bool IsChinese = true;
#else
public const bool IsChinese = false;
#endif

private static bool _isNewGameBuild;

[Obsolete]
public static void Init()
{
Expand Down Expand Up @@ -179,12 +183,31 @@ private static async Task SearchAndSetLL()
{
OffsetCache.Clear();
OffsetCache["Version"] = _version;
_isNewGameBuild = true;
}
}

await SetOffsetObjectsAsync(llTypes);
}

internal static void ClearOffsetFromCache(MemberInfo? info)
{
if (info == null)
{
Logger.Error("MemberInfo is null");
return;
}

OffsetCache.TryRemove(info.MemberName(), out _);
File.WriteAllText(OffsetFile, JsonConvert.SerializeObject(OffsetCache));
}

internal static void ClearOffsetFromCache(string name)
{
OffsetCache.TryRemove(name, out _);
File.WriteAllText(OffsetFile, JsonConvert.SerializeObject(OffsetCache));
}

private static List<Type> GetTypes()
{
var q1 = (from t in Assembly.GetExecutingAssembly().GetTypes()
Expand Down Expand Up @@ -247,24 +270,53 @@ internal static void SetPostOffsets()
newStopwatch.Stop();
Logger.Debug($"OffsetManager AgentModule.TryAddAgent took {newStopwatch.ElapsedMilliseconds}ms");

if (LlamaLibrarySettings.Instance.TempDisableInventoryHook && InventoryUpdatePatch.Offsets.OrginalCall == InventoryUpdatePatch.Offsets.OriginalJump)
{
LlamaLibrarySettings.Instance.TempDisableInventoryHook = false;
}

if (Core.CurrentGameVer != LlamaLibrarySettings.Instance.LastRevision && InventoryUpdatePatch.Offsets.OrginalCall == InventoryUpdatePatch.Offsets.OriginalJump)
{
LlamaLibrarySettings.Instance.LastRevision = Core.CurrentGameVer;
Logger.Information($"Setting revision to {Core.CurrentGameVer} in {LlamaLibrarySettings.Instance.FilePath}");
}

var skipInventoryPatch = LlamaLibrarySettings.Instance.TempDisableInventoryHook || LlamaLibrarySettings.Instance.DisableInventoryHook;

Logger.Information($"TempDisableInventoryHook: {LlamaLibrarySettings.Instance.TempDisableInventoryHook} DisableInventoryHook: {LlamaLibrarySettings.Instance.DisableInventoryHook}");

if (!skipInventoryPatch)
{
if (InventoryUpdatePatch.Offsets.OrginalCall != InventoryUpdatePatch.Offsets.OriginalJump || InventoryUpdatePatch.Offsets.OrginalCall == IntPtr.Zero || InventoryUpdatePatch.Offsets.OriginalJump == IntPtr.Zero)
{
if (!_isNewGameBuild && InventoryUpdatePatch.Offsets.OrginalCall != IntPtr.Zero && InventoryUpdatePatch.Offsets.OriginalJump != IntPtr.Zero)
{
Logger.Information("Last patch not cleaned up, cleaning up now");
var asm = Core.Memory.Asm;
asm.Clear();
asm.AddLine("[org 0x{0:X16}]", (ulong)InventoryUpdatePatch.Offsets.PatchLocation);
asm.AddLine("JMP {0}", InventoryUpdatePatch.Offsets.OrginalCall);
var jzPatch = asm.Assemble();
Core.Memory.WriteBytes(InventoryUpdatePatch.Offsets.PatchLocation, jzPatch);
InventoryUpdatePatch.Offsets.OriginalJump = InventoryUpdatePatch.Offsets.OrginalCall;
}
else
{
Logger.Error("New game build and inventory patch offsets don't match");
var memberInfo = typeof(InventoryUpdatePatch.Offsets).GetMember("OrginalCall", BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault();
ClearOffsetFromCache(memberInfo);
LlamaLibrarySettings.Instance.TempDisableInventoryHook = true;
skipInventoryPatch = true;
}
}
}

newStopwatch.Restart();
File.WriteAllText(OffsetFile, JsonConvert.SerializeObject(OffsetCache));
newStopwatch.Stop();
Logger.Debug($"OffsetManager File.WriteAllText took {newStopwatch.ElapsedMilliseconds}ms");

if (InventoryUpdatePatch.Offsets.OrginalCall != InventoryUpdatePatch.Offsets.OriginalJump)
{
Logger.Information("Last patch not cleaned up, cleaning up now");
var asm = Core.Memory.Asm;
asm.Clear();
asm.AddLine("[org 0x{0:X16}]", (ulong)InventoryUpdatePatch.Offsets.PatchLocation);
asm.AddLine("JMP {0}", InventoryUpdatePatch.Offsets.OrginalCall);
var jzPatch = asm.Assemble();
Core.Memory.WriteBytes(InventoryUpdatePatch.Offsets.PatchLocation, jzPatch);
InventoryUpdatePatch.Offsets.OriginalJump = InventoryUpdatePatch.Offsets.OrginalCall;
}

PatchManager.Initialize();
PatchManager.Initialize(skipInventoryPatch);
}

public static void RegisterAgent(IAgent iagent)
Expand Down
15 changes: 13 additions & 2 deletions Settings/Base/BaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using System.Windows.Media;
using System.Windows.Threading;
using ff14bot.Forms.ugh;
using ff14bot.Helpers;
using LlamaLibrary.Logging;
using LlamaLibrary.Memory;
Expand All @@ -27,7 +28,16 @@ public abstract class BaseSettings : INotifyPropertyChanged

public BaseSettings(string path)
{
Dispatcher = Dispatcher.CurrentDispatcher;
Dispatcher = MainWpf.current.Dispatcher;
_saveDebounceDispatcher = new DebounceDispatcher(SaveLocal);
_logger = new LLogger($"{GetType().Name}", Colors.Peru, LogLevel.Debug);
FilePath = path;
LoadFrom(FilePath);
}

public BaseSettings(string path, Dispatcher dispatcher)
{
Dispatcher = dispatcher;
_saveDebounceDispatcher = new DebounceDispatcher(SaveLocal);
_logger = new LLogger($"{GetType().Name}", Colors.Peru, LogLevel.Debug);
FilePath = path;
Expand All @@ -43,7 +53,7 @@ public BaseSettings(string path)
protected Dispatcher Dispatcher { get; }

[JsonIgnore]
private string FilePath { get; }
public string FilePath { get; }

public static string GetSettingsFilePath(params string[] subPathParts)
{
Expand Down Expand Up @@ -216,6 +226,7 @@ public void SaveAs(string file)
{
if (!_loaded)
{
_logger.Information("Not loaded yet");
return;
}

Expand Down
43 changes: 42 additions & 1 deletion Settings/Base/BaseSettingsTyped.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ff14bot;
using System.Windows.Threading;
using ff14bot;
using LlamaLibrary.Extensions;

namespace LlamaLibrary.Settings.Base;
Expand All @@ -16,6 +17,14 @@ public BaseSettings(string settingsFilePath) : base(settingsFilePath)
{
}

public BaseSettings(Dispatcher dispatcher) : base(GetSettingsFilePath($"{typeof(T).Name}.json"), dispatcher)
{
}

public BaseSettings(string settingsFilePath, Dispatcher dispatcher) : base(settingsFilePath, dispatcher)
{
}

public static T Instance
{
get => _instance ??= new T();
Expand All @@ -38,6 +47,14 @@ public CharacterBaseSettings() : base(GetSettingsFilePath($"{Core.Me.Name}_{Core
public CharacterBaseSettings(string fileName) : base(GetSettingsFilePath($"{Core.Me.Name}_{Core.Me.PlayerId()}", fileName))
{
}

public CharacterBaseSettings(string fileName, Dispatcher dispatcher) : base(GetSettingsFilePath($"{Core.Me.Name}_{Core.Me.PlayerId()}", fileName), dispatcher)
{
}

public CharacterBaseSettings(Dispatcher dispatcher) : base(GetSettingsFilePath($"{Core.Me.Name}_{Core.Me.PlayerId()}", $"{typeof(T).Name}.json"), dispatcher)
{
}
}

public class AccountBaseSettings<T> : BaseSettings<T>
Expand All @@ -47,17 +64,33 @@ public AccountBaseSettings() : base(GetSettingsFilePath($"Account_{Core.Me.Accou
{
}

public AccountBaseSettings(Dispatcher dispatcher) : base(GetSettingsFilePath($"Account_{Core.Me.AccountId()}", $"{typeof(T).Name}.json"), dispatcher)
{
}

public AccountBaseSettings(string fileName) : base(GetSettingsFilePath($"Account_{Core.Me.AccountId()}", fileName))
{
}

public AccountBaseSettings(string fileName, Dispatcher dispatcher) : base(GetSettingsFilePath($"Account_{Core.Me.AccountId()}", fileName), dispatcher)
{
}

public AccountBaseSettings(int accountId, string fileName) : base(GetSettingsFilePath($"Account_{accountId}", fileName))
{
}

public AccountBaseSettings(int accountId, Dispatcher dispatcher) : base(GetSettingsFilePath($"Account_{accountId}", $"{typeof(T).Name}.json"), dispatcher)
{
}

public AccountBaseSettings(int accountId) : base(GetSettingsFilePath($"Account_{accountId}", $"{typeof(T).Name}.json"))
{
}

public AccountBaseSettings(int accountId, string fileName, Dispatcher dispatcher) : base(GetSettingsFilePath($"Account_{accountId}", fileName), dispatcher)
{
}
}

public class HomeWorldBaseSettings<T> : BaseSettings<T>
Expand All @@ -67,7 +100,15 @@ public HomeWorldBaseSettings() : base(GetSettingsFilePath($"HomeWorld_{Core.Me.H
{
}

public HomeWorldBaseSettings(Dispatcher dispatcher) : base(GetSettingsFilePath($"HomeWorld_{Core.Me.HomeWorld()}", $"{typeof(T).Name}.json"), dispatcher)
{
}

public HomeWorldBaseSettings(string fileName) : base(GetSettingsFilePath($"HomeWorld_{Core.Me.HomeWorld()}", fileName))
{
}

public HomeWorldBaseSettings(string fileName, Dispatcher dispatcher) : base(GetSettingsFilePath($"HomeWorld_{Core.Me.HomeWorld()}", fileName), dispatcher)
{
}
}
32 changes: 32 additions & 0 deletions Settings/LlamaLibrarySettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.ComponentModel;
using ff14bot;
using LlamaLibrary.Settings.Base;

namespace LlamaLibrary.Settings;

public class LlamaLibrarySettings : BaseSettings<LlamaLibrarySettings>
{
private int _lastRevision;
private bool _disableInventoryHook;
private bool _tempDisableInventoryHook;

public int LastRevision
{
get => _lastRevision;
set => SetField(ref _lastRevision, value);
}

[DefaultValue(false)]
public bool DisableInventoryHook
{
get => _disableInventoryHook;
set => SetField(ref _disableInventoryHook, value);
}

[DefaultValue(false)]
public bool TempDisableInventoryHook
{
get => _tempDisableInventoryHook;
set => SetField(ref _tempDisableInventoryHook, value);
}
}

0 comments on commit 35bfef2

Please sign in to comment.