From 7374020f52229a5cef264dedaf24925ae37ee548 Mon Sep 17 00:00:00 2001 From: Amber Kortier Date: Sat, 27 Jul 2024 20:09:20 +0200 Subject: [PATCH] Fixes IPC crash (I'm an idiot c:) --- PetNicknames/PetNicknames/IPC/IpcProvider.cs | 30 ++++++++++++++----- .../DataParseResults/ClearParseResult.cs | 8 +++-- .../WritingAndParsing/DataParser.cs | 4 +-- .../IParseResults/IClearParseResult.cs | 3 +- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/PetNicknames/PetNicknames/IPC/IpcProvider.cs b/PetNicknames/PetNicknames/IPC/IpcProvider.cs index c4cfa3d5..0bdf2c4e 100644 --- a/PetNicknames/PetNicknames/IPC/IpcProvider.cs +++ b/PetNicknames/PetNicknames/IPC/IpcProvider.cs @@ -1,4 +1,6 @@ -using Dalamud.Plugin; +using Dalamud.Game.ClientState.Objects.SubKinds; +using Dalamud.Game.ClientState.Objects.Types; +using Dalamud.Plugin; using Dalamud.Plugin.Ipc; using FFXIVClientStructs.FFXIV.Client.Game.Character; using PetRenamer.PetNicknames.IPC.Interfaces; @@ -7,6 +9,7 @@ using PetRenamer.PetNicknames.Services; using PetRenamer.PetNicknames.WritingAndParsing.DataParseResults; using PetRenamer.PetNicknames.WritingAndParsing.Interfaces.IParseResults; +using System; namespace PetRenamer; @@ -129,8 +132,18 @@ void RegisterFunctions() // Actions public void SetPlayerDataDetour(string data) { - IDataParseResult result = DataReader.ParseData(data); - DalamudServices.Framework.Run(() => DataReader.ApplyParseData(result, true)); + try + { + DalamudServices.Framework.Run(() => + { + IDataParseResult result = DataReader.ParseData(data); + DataReader.ApplyParseData(result, true); + }); + } + catch (Exception e) + { + DalamudServices.PluginLog.Error(e, "Error in Set Player Data"); + } } public unsafe void ClearIPCDataDetour(ushort objectIndex) @@ -139,13 +152,16 @@ public unsafe void ClearIPCDataDetour(ushort objectIndex) { DalamudServices.Framework.Run(() => { - BattleChara* bChara = (BattleChara*)DalamudServices.ObjectTable.GetObjectAddress(objectIndex); - if (bChara == null) return; + if (DalamudServices.ObjectTable.Length <= objectIndex) return; + if (DalamudServices.ObjectTable[objectIndex] is not IPlayerCharacter pc) return; - DataReader.ApplyParseData(new ClearParseResult(bChara->ContentId), true); + DataReader.ApplyParseData(new ClearParseResult(pc.Name.TextValue, (ushort)pc.HomeWorld.Id), true); }); } - catch { } + catch(Exception e) + { + DalamudServices.PluginLog.Error(e, "Error in clear IPC"); + } } // Functions diff --git a/PetNicknames/PetNicknames/WritingAndParsing/DataParseResults/ClearParseResult.cs b/PetNicknames/PetNicknames/WritingAndParsing/DataParseResults/ClearParseResult.cs index d7afb84e..78148da6 100644 --- a/PetNicknames/PetNicknames/WritingAndParsing/DataParseResults/ClearParseResult.cs +++ b/PetNicknames/PetNicknames/WritingAndParsing/DataParseResults/ClearParseResult.cs @@ -4,10 +4,12 @@ namespace PetRenamer.PetNicknames.WritingAndParsing.DataParseResults; internal class ClearParseResult : IClearParseResult { - public ulong ContentID { get; } + public string Name { get; } + public ushort Homeworld { get; } - public ClearParseResult(ulong contentID) + public ClearParseResult(string name, ushort homeworld) { - ContentID = contentID; + Name = name; + Homeworld = homeworld; } } diff --git a/PetNicknames/PetNicknames/WritingAndParsing/DataParser.cs b/PetNicknames/PetNicknames/WritingAndParsing/DataParser.cs index a6ca961f..fcff8ce5 100644 --- a/PetNicknames/PetNicknames/WritingAndParsing/DataParser.cs +++ b/PetNicknames/PetNicknames/WritingAndParsing/DataParser.cs @@ -46,9 +46,9 @@ public unsafe bool ApplyParseData(IDataParseResult result, bool isFromIPC) if (result is IClearParseResult clearParseResult) { - if (clearParseResult.ContentID == 0) return false; + if (clearParseResult.Name.IsNullOrWhitespace() || clearParseResult.Homeworld == 0) return false; - Database.GetEntry(clearParseResult.ContentID).Clear(false); + Database.GetEntry(clearParseResult.Name, clearParseResult.Homeworld, false)?.Clear(false); return true; } diff --git a/PetNicknames/PetNicknames/WritingAndParsing/Interfaces/IParseResults/IClearParseResult.cs b/PetNicknames/PetNicknames/WritingAndParsing/Interfaces/IParseResults/IClearParseResult.cs index 4cfd4612..b780371d 100644 --- a/PetNicknames/PetNicknames/WritingAndParsing/Interfaces/IParseResults/IClearParseResult.cs +++ b/PetNicknames/PetNicknames/WritingAndParsing/Interfaces/IParseResults/IClearParseResult.cs @@ -2,5 +2,6 @@ internal interface IClearParseResult : IDataParseResult { - ulong ContentID { get; } + ushort Homeworld { get; } + string Name { get; } }