ExtraChat/client/ExtraChat/Plugin.cs

198 lines
5.8 KiB
C#
Raw Normal View History

2022-07-08 02:58:32 +00:00
using ASodium;
using Dalamud.ContextMenu;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.SubKinds;
2022-07-09 05:44:02 +00:00
using Dalamud.Game.Text;
2022-07-08 02:58:32 +00:00
using Dalamud.Interface.Internal.Notifications;
using Dalamud.IoC;
using Dalamud.Plugin;
2023-09-28 01:44:39 +00:00
using Dalamud.Plugin.Services;
2022-07-10 18:24:35 +00:00
using ExtraChat.Integrations;
2022-07-08 02:58:32 +00:00
using ExtraChat.Ui;
using ExtraChat.Util;
namespace ExtraChat;
// ReSharper disable once ClassNeverInstantiated.Global
public class Plugin : IDalamudPlugin {
internal const ushort DefaultColour = 578;
2023-09-28 01:44:39 +00:00
internal static string Name => "ExtraChat";
[PluginService]
internal static IPluginLog Log { get; private set; }
2022-07-08 02:58:32 +00:00
[PluginService]
internal DalamudPluginInterface Interface { get; init; }
[PluginService]
2023-09-28 01:44:39 +00:00
internal IClientState ClientState { get; init; }
[PluginService]
internal ICommandManager CommandManager { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IChatGui ChatGui { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IDataManager DataManager { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IFramework Framework { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IGameGui GameGui { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IObjectTable ObjectTable { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal ITargetManager TargetManager { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
internal IGameInteropProvider GameInteropProvider { get; init; }
2022-07-08 02:58:32 +00:00
[PluginService]
2023-09-28 01:44:39 +00:00
private IToastGui ToastGui { get; init; }
2022-07-08 02:58:32 +00:00
internal Configuration Config { get; }
internal ConfigInfo ConfigInfo => this.Config.GetConfig(this.ClientState.LocalContentId);
internal Client Client { get; }
internal Commands Commands { get; }
internal PluginUi PluginUi { get; }
2022-07-18 21:32:42 +00:00
internal DalamudContextMenu ContextMenu { get; }
2022-07-08 02:58:32 +00:00
internal GameFunctions GameFunctions { get; }
internal Ipc Ipc { get; }
2022-07-10 18:24:35 +00:00
private IDisposable[] Integrations { get; }
2022-07-08 02:58:32 +00:00
private PlayerCharacter? _localPlayer;
private readonly Mutex _localPlayerLock = new();
internal PlayerCharacter? LocalPlayer {
get {
this._localPlayerLock.WaitOne();
var player = this._localPlayer;
this._localPlayerLock.ReleaseMutex();
return player;
}
private set {
this._localPlayerLock.WaitOne();
this._localPlayer = value;
this._localPlayerLock.ReleaseMutex();
}
}
public Plugin() {
SodiumInit.Init();
WorldUtil.Initialise(this.DataManager!);
2023-10-03 07:15:26 +00:00
this.ContextMenu = new DalamudContextMenu(this.Interface);
2022-07-08 02:58:32 +00:00
this.Config = this.Interface!.GetPluginConfig() as Configuration ?? new Configuration();
this.Client = new Client(this);
this.Commands = new Commands(this);
this.PluginUi = new PluginUi(this);
this.GameFunctions = new GameFunctions(this);
this.Ipc = new Ipc(this);
2022-07-10 18:24:35 +00:00
this.Integrations = new IDisposable[] {
new ChatTwo(this),
};
2022-07-08 02:58:32 +00:00
this.Framework!.Update += this.FrameworkUpdate;
2022-07-18 21:32:42 +00:00
this.ContextMenu.OnOpenGameObjectContextMenu += this.OnOpenGameObjectContextMenu;
2022-07-08 02:58:32 +00:00
}
public void Dispose() {
this.GameFunctions.ResetOverride();
2022-07-18 21:32:42 +00:00
this.ContextMenu.OnOpenGameObjectContextMenu -= this.OnOpenGameObjectContextMenu;
2022-07-08 02:58:32 +00:00
this.Framework.Update -= this.FrameworkUpdate;
this._localPlayerLock.Dispose();
2022-07-10 18:24:35 +00:00
foreach (var integration in this.Integrations) {
integration.Dispose();
}
2022-07-08 02:58:32 +00:00
this.Ipc.Dispose();
this.GameFunctions.Dispose();
this.PluginUi.Dispose();
this.Commands.Dispose();
this.Client.Dispose();
this.ContextMenu.Dispose();
}
2023-09-28 01:44:39 +00:00
private void FrameworkUpdate(IFramework framework) {
2022-07-08 02:58:32 +00:00
if (this.ClientState.LocalPlayer is { } player) {
this.LocalPlayer = player;
} else if (!this.ClientState.IsLoggedIn) {
// only set to null if not logged in
this.LocalPlayer = null;
}
}
private void OnOpenGameObjectContextMenu(GameObjectContextMenuOpenArgs args) {
2022-07-18 20:59:52 +00:00
if (!this.Config.ShowContextMenuItem) {
return;
}
2022-07-10 18:15:39 +00:00
if (args.ObjectId != 0xE0000000) {
this.ObjectContext(args);
2022-07-08 02:58:32 +00:00
return;
}
2022-07-10 18:15:39 +00:00
if (args.ObjectWorld == 0) {
return;
}
var name = args.Text?.TextValue;
if (name == null) {
return;
}
args.AddCustomItem(new GameObjectContextMenuItem("Invite to ExtraChat Linkshell", _ => {
this.PluginUi.InviteInfo = (name, args.ObjectWorld);
}));
}
private void ObjectContext(GameObjectContextMenuOpenArgs args) {
2022-07-08 02:58:32 +00:00
var obj = this.ObjectTable.SearchById(args.ObjectId);
if (obj is not PlayerCharacter chara) {
return;
}
args.AddCustomItem(new GameObjectContextMenuItem("Invite to ExtraChat Linkshell", _ => {
var name = chara.Name.TextValue;
this.PluginUi.InviteInfo = (name, (ushort) chara.HomeWorld.Id);
}));
}
internal void SaveConfig() {
this.Interface.SavePluginConfig(this.Config);
}
internal void ShowInfo(string message) {
if (this.Config.UseNativeToasts) {
this.ToastGui.ShowNormal(message);
} else {
2023-09-28 01:44:39 +00:00
this.Interface.UiBuilder.AddNotification(message, Name, NotificationType.Info);
2022-07-08 02:58:32 +00:00
}
2022-07-09 05:44:02 +00:00
2023-09-28 01:44:39 +00:00
this.ChatGui.Print(new XivChatEntry {
2022-07-09 05:44:02 +00:00
Type = XivChatType.SystemMessage,
Message = message,
});
2022-07-08 02:58:32 +00:00
}
internal void ShowError(string message) {
if (this.Config.UseNativeToasts) {
this.ToastGui.ShowError(message);
} else {
2023-09-28 01:44:39 +00:00
this.Interface.UiBuilder.AddNotification(message, Name, NotificationType.Error);
2022-07-08 02:58:32 +00:00
}
2022-07-09 05:44:02 +00:00
2023-09-28 01:44:39 +00:00
this.ChatGui.Print(new XivChatEntry {
2022-07-09 17:29:44 +00:00
Type = XivChatType.ErrorMessage,
2022-07-09 05:44:02 +00:00
Message = message,
});
2022-07-08 02:58:32 +00:00
}
}