From 27265244ecea450b569bc227ff4c4dffa9773755 Mon Sep 17 00:00:00 2001 From: goat Date: Thu, 30 Jan 2020 22:47:47 +0900 Subject: [PATCH] feat: add DataManager, remote opcodes etc. --- Dalamud/Dalamud.cs | 7 ++++ Dalamud/Data/DataManager.cs | 51 +++++++++++++++++++++++++ Dalamud/Game/Network/NetworkHandlers.cs | 23 ++++------- 3 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 Dalamud/Data/DataManager.cs diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 3153de26c..2113c06ea 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; +using Dalamud.Data; using Dalamud.DiscordBot; using Dalamud.Game; using Dalamud.Game.Chat; @@ -52,6 +53,8 @@ namespace Dalamud { public readonly InterfaceManager InterfaceManager; + public readonly DataManager Data; + private readonly string assemblyVersion = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString(); public Dalamud(DalamudStartInfo info) { @@ -76,6 +79,10 @@ namespace Dalamud { ChatHandlers = new ChatHandlers(this); NetworkHandlers = new NetworkHandlers(this, this.Configuration.OptOutMbCollection); + this.Data = new DataManager(); + //Task.Run(() => ); + this.Data.Initialize(); + this.ClientState = new ClientState(this, info, this.SigScanner, this.targetModule); this.BotManager = new DiscordBotManager(this, this.Configuration.DiscordFeatureConfig); diff --git a/Dalamud/Data/DataManager.cs b/Dalamud/Data/DataManager.cs new file mode 100644 index 000000000..db7e107fd --- /dev/null +++ b/Dalamud/Data/DataManager.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Serilog; + +namespace Dalamud.Data +{ + /// + /// This class provides data for Dalamud-internal features, but can also be used by plugins if needed. + /// + public class DataManager { + private const string DataBaseUrl = "https://goaaats.github.io/ffxiv/tools/launcher/addons/Hooks/Data/"; + + public ReadOnlyDictionary OpCodes; + public ReadOnlyDictionary ContentFinderCondition; + + public DataManager() { + // Set up default values so plugins do not null-reference when data is being loaded. + this.OpCodes = new ReadOnlyDictionary(new Dictionary()); + this.ContentFinderCondition = new ReadOnlyDictionary(new Dictionary()); + } + + public async Task Initialize() { + Log.Verbose("Starting data download..."); + + // This is due to GitHub not supporting TLS 1.0 + System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; + + using var client = new HttpClient() { + BaseAddress = new Uri(DataBaseUrl) + }; + + var opCodeDict = JsonConvert.DeserializeObject>(await client.GetStringAsync(DataBaseUrl + "opcode.json")); + this.OpCodes = new ReadOnlyDictionary(opCodeDict); + + Log.Verbose("Loaded {0} OpCodes.", opCodeDict.Count); + + var cfcs = JsonConvert.DeserializeObject>(await client.GetStringAsync(DataBaseUrl + "contentfindercondition.json")); + this.ContentFinderCondition = new ReadOnlyDictionary(cfcs); + + Log.Verbose("Loaded {0} ContentFinderCondition.", cfcs.Count); + + System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault; + } + } +} diff --git a/Dalamud/Game/Network/NetworkHandlers.cs b/Dalamud/Game/Network/NetworkHandlers.cs index 752e92e0a..fa3b2c037 100644 --- a/Dalamud/Game/Network/NetworkHandlers.cs +++ b/Dalamud/Game/Network/NetworkHandlers.cs @@ -34,9 +34,9 @@ namespace Dalamud.Game.Network { } private void OnZonePacket(IntPtr dataPtr) { - var opCode = (ZoneOpCode) Marshal.ReadInt16(dataPtr, 2); + var opCode = (ushort) Marshal.ReadInt16(dataPtr, 2); - if (opCode == ZoneOpCode.CfNotifyPop) { + if (opCode == this.dalamud.Data.OpCodes["CfNotifyPop"]) { var data = new byte[64]; Marshal.Copy(dataPtr, data, 0, 64); @@ -60,7 +60,7 @@ namespace Dalamud.Game.Network { return; } - if (opCode == ZoneOpCode.CfPreferredRole) { + if (opCode == this.dalamud.Data.OpCodes["CfPreferredRole"]) { if (this.dalamud.Configuration.PreferredRoleReminders == null) return; @@ -115,7 +115,7 @@ namespace Dalamud.Game.Network { } if (!this.optOutMbUploads) { - if (opCode == ZoneOpCode.MarketBoardItemRequestStart) { + if (opCode == this.dalamud.Data.OpCodes["MarketBoardItemRequestStart"]) { var catalogId = (uint) Marshal.ReadInt32(dataPtr + 0x10); var amount = Marshal.ReadByte(dataPtr + 0x1B); @@ -130,7 +130,7 @@ namespace Dalamud.Game.Network { return; } - if (opCode == ZoneOpCode.MarketBoardOfferings) { + if (opCode == this.dalamud.Data.OpCodes["MarketBoardOfferings"]) { var listing = MarketBoardCurrentOfferings.Read(dataPtr + 0x10); var request = @@ -185,7 +185,7 @@ namespace Dalamud.Game.Network { return; } - if (opCode == ZoneOpCode.MarketBoardHistory) { + if (opCode == this.dalamud.Data.OpCodes["MarketBoardHistory"]) { var listing = MarketBoardHistory.Read(dataPtr + 0x10); var request = this.marketBoardRequests.LastOrDefault(r => r.CatalogId == listing.CatalogId); @@ -207,7 +207,7 @@ namespace Dalamud.Game.Network { Log.Verbose("Added history for item#{0}", listing.CatalogId); } - if (opCode == ZoneOpCode.MarketTaxRates) + if (opCode == this.dalamud.Data.OpCodes["MarketTaxRates"]) { var taxes = MarketTaxRates.Read(dataPtr + 0x10); @@ -225,15 +225,6 @@ namespace Dalamud.Game.Network { } } - private enum ZoneOpCode { - CfNotifyPop = 0x1F8, - CfPreferredRole = 0x32A, - MarketTaxRates = 0x25E, - MarketBoardItemRequestStart = 0x328, - MarketBoardOfferings = 0x15F, - MarketBoardHistory = 0x113 - } - private DalamudConfiguration.PreferredRole RoleKeyToPreferredRole(int key) => key switch { 1 => DalamudConfiguration.PreferredRole.Tank,