2020-10-23 21:24:32 +00:00
|
|
|
|
using Dalamud.Game.Command;
|
|
|
|
|
using Dalamud.Hooking;
|
|
|
|
|
using Dalamud.Plugin;
|
|
|
|
|
using System;
|
2020-10-25 15:40:54 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-01 01:25:52 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-10-25 15:40:54 +00:00
|
|
|
|
#if DEBUG
|
2020-10-23 21:24:32 +00:00
|
|
|
|
using System.IO;
|
2020-10-25 15:40:54 +00:00
|
|
|
|
#endif
|
2020-10-23 21:24:32 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
2020-10-25 15:40:54 +00:00
|
|
|
|
// TODO: hostable relay server (run one but have option to run your own)?
|
|
|
|
|
|
2020-10-23 21:24:32 +00:00
|
|
|
|
namespace XIVChatPlugin {
|
|
|
|
|
public class Plugin : IDalamudPlugin {
|
|
|
|
|
private bool disposedValue;
|
|
|
|
|
|
|
|
|
|
public string Name => "XIVChat";
|
|
|
|
|
|
2020-11-01 01:25:52 +00:00
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
2020-10-23 21:24:32 +00:00
|
|
|
|
internal string Location { get; private set; } = Assembly.GetExecutingAssembly().Location;
|
|
|
|
|
|
2020-11-01 01:25:52 +00:00
|
|
|
|
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
2020-10-23 21:24:32 +00:00
|
|
|
|
private void SetLocation(string path) {
|
|
|
|
|
this.Location = path;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 01:25:52 +00:00
|
|
|
|
#pragma warning disable 8618
|
2020-10-23 21:24:32 +00:00
|
|
|
|
public DalamudPluginInterface Interface { get; private set; }
|
|
|
|
|
public Configuration Config { get; private set; }
|
2020-11-01 01:25:52 +00:00
|
|
|
|
private PluginUI Ui { get; set; }
|
2020-10-23 21:24:32 +00:00
|
|
|
|
public Server Server { get; private set; }
|
|
|
|
|
public GameFunctions Functions { get; private set; }
|
2020-11-01 01:25:52 +00:00
|
|
|
|
#pragma warning restore 8618
|
2020-10-23 21:24:32 +00:00
|
|
|
|
|
|
|
|
|
private delegate byte ChatChannelChangeDelegate(IntPtr a1, uint channel);
|
2020-11-01 01:25:52 +00:00
|
|
|
|
|
|
|
|
|
private Hook<ChatChannelChangeDelegate>? chatChannelChangeHook;
|
2020-10-23 21:24:32 +00:00
|
|
|
|
|
|
|
|
|
public void Initialize(DalamudPluginInterface pluginInterface) {
|
|
|
|
|
this.Interface = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface), "DalamudPluginInterface cannot be null");
|
|
|
|
|
|
|
|
|
|
// load libsodium.so from debug location if in debug mode
|
2020-11-01 01:25:52 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
string path = Environment.GetEnvironmentVariable("PATH")!;
|
|
|
|
|
string newPath = Path.GetDirectoryName(this.Location)!;
|
2020-10-23 21:24:32 +00:00
|
|
|
|
Environment.SetEnvironmentVariable("PATH", $"{path};{newPath}");
|
2020-11-01 01:25:52 +00:00
|
|
|
|
#endif
|
2020-10-23 21:24:32 +00:00
|
|
|
|
|
2020-11-01 01:25:52 +00:00
|
|
|
|
this.Config = (Configuration?)this.Interface.GetPluginConfig() ?? new Configuration();
|
2020-10-23 21:24:32 +00:00
|
|
|
|
this.Config.Initialise(this);
|
|
|
|
|
|
|
|
|
|
this.Functions = new GameFunctions(this);
|
2020-10-25 15:40:54 +00:00
|
|
|
|
try {
|
|
|
|
|
var funcPtr = this.Interface.TargetModuleScanner.ScanText("40 55 48 8D 6C 24 ?? 48 81 EC A0 00 00 00 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 45 ?? 48 8B 0D ?? ?? ?? ?? 33 C0 48 83 C1 10 89 45 ?? C7 45 ?? 01 00 00 00");
|
2020-10-23 21:24:32 +00:00
|
|
|
|
this.chatChannelChangeHook = new Hook<ChatChannelChangeDelegate>(funcPtr, new ChatChannelChangeDelegate(this.ChangeChatChannelDetour));
|
|
|
|
|
this.chatChannelChangeHook.Enable();
|
2020-10-25 15:40:54 +00:00
|
|
|
|
} catch (KeyNotFoundException) {
|
|
|
|
|
PluginLog.LogError("Could not sig chat channel change function");
|
2020-10-23 21:24:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Ui = new PluginUI(this);
|
|
|
|
|
|
|
|
|
|
this.Server = new Server(this);
|
|
|
|
|
this.Server.Spawn();
|
|
|
|
|
|
|
|
|
|
this.Interface.UiBuilder.OnBuildUi += this.Ui.Draw;
|
|
|
|
|
this.Interface.UiBuilder.OnOpenConfigUi += this.Ui.OpenSettings;
|
|
|
|
|
this.Interface.Framework.OnUpdateEvent += this.Server.OnFrameworkUpdate;
|
|
|
|
|
this.Interface.Framework.Gui.Chat.OnChatMessage += this.Server.OnChat;
|
|
|
|
|
this.Interface.ClientState.OnLogin += this.Server.OnLogIn;
|
|
|
|
|
this.Interface.ClientState.OnLogout += this.Server.OnLogOut;
|
|
|
|
|
this.Interface.ClientState.TerritoryChanged += this.Server.OnTerritoryChange;
|
|
|
|
|
this.Interface.CommandManager.AddHandler("/xivchat", new CommandInfo(this.OnCommand) {
|
|
|
|
|
HelpMessage = "Opens the config for the XIVChat plugin",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte ChangeChatChannelDetour(IntPtr a1, uint channel) {
|
|
|
|
|
// a1 + 0xfd0 is the chat channel byte (including for when clicking on shout)
|
|
|
|
|
this.Server.OnChatChannelChange(channel);
|
2020-11-01 01:25:52 +00:00
|
|
|
|
return this.chatChannelChangeHook!.Original(a1, channel);
|
2020-10-23 21:24:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnCommand(string command, string args) {
|
|
|
|
|
this.Ui.OpenSettings(null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 01:25:52 +00:00
|
|
|
|
[SuppressMessage("ReSharper", "DelegateSubtraction")]
|
2020-10-23 21:24:32 +00:00
|
|
|
|
protected virtual void Dispose(bool disposing) {
|
2020-11-01 01:25:52 +00:00
|
|
|
|
if (this.disposedValue) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (disposing) {
|
|
|
|
|
this.Server.Dispose();
|
|
|
|
|
|
|
|
|
|
this.Interface.UiBuilder.OnBuildUi -= this.Ui.Draw;
|
|
|
|
|
this.Interface.UiBuilder.OnOpenConfigUi -= this.Ui.OpenSettings;
|
|
|
|
|
this.Interface.Framework.OnUpdateEvent -= this.Server.OnFrameworkUpdate;
|
|
|
|
|
this.Interface.Framework.Gui.Chat.OnChatMessage -= this.Server.OnChat;
|
|
|
|
|
this.Interface.ClientState.OnLogin -= this.Server.OnLogIn;
|
|
|
|
|
this.Interface.ClientState.OnLogout -= this.Server.OnLogOut;
|
|
|
|
|
this.Interface.ClientState.TerritoryChanged -= this.Server.OnTerritoryChange;
|
|
|
|
|
this.Interface.CommandManager.RemoveHandler("/xivchat");
|
|
|
|
|
|
|
|
|
|
this.chatChannelChangeHook?.Dispose();
|
2020-10-23 21:24:32 +00:00
|
|
|
|
}
|
2020-11-01 01:25:52 +00:00
|
|
|
|
|
|
|
|
|
this.disposedValue = true;
|
2020-10-23 21:24:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
2020-11-01 01:25:52 +00:00
|
|
|
|
this.Dispose(true);
|
2020-10-23 21:24:32 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|