diff options
| author | Pinapelz <yukais@pinapelz.com> | 2024-09-14 20:28:18 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2024-09-14 20:28:18 -0700 |
| commit | d00b782c7fa6b4b4e68754293a3236e35ed7ff90 (patch) | |
| tree | 914f331623c73e866291fdbf60eb83dde9a52f36 /DiscordToXIV | |
| parent | 610015a68d7ad9580c7ac9dc427ccace650a9b9e (diff) | |
implement initial BD websocket receiver
Diffstat (limited to 'DiscordToXIV')
| -rw-r--r-- | DiscordToXIV/Configuration.cs | 20 | ||||
| -rw-r--r-- | DiscordToXIV/Dalamud.Plugin.Bootstrap.targets | 11 | ||||
| -rw-r--r-- | DiscordToXIV/DiscordToXIV.csproj | 18 | ||||
| -rw-r--r-- | DiscordToXIV/DiscordToXIV.json | 12 | ||||
| -rw-r--r-- | DiscordToXIV/Plugin.cs | 158 | ||||
| -rw-r--r-- | DiscordToXIV/Windows/ConfigWindow.cs | 38 | ||||
| -rw-r--r-- | DiscordToXIV/packages.lock.json | 19 |
7 files changed, 276 insertions, 0 deletions
diff --git a/DiscordToXIV/Configuration.cs b/DiscordToXIV/Configuration.cs new file mode 100644 index 0000000..82d14e3 --- /dev/null +++ b/DiscordToXIV/Configuration.cs @@ -0,0 +1,20 @@ +using Dalamud.Configuration; +using Dalamud.Plugin; +using System; + +namespace DiscordToXIV; + +[Serializable] +public class Configuration : IPluginConfiguration +{ + public int Version { get; set; } = 0; + + public bool IsConfigWindowMovable { get; set; } = true; + public bool SomePropertyToBeSavedAndWithADefault { get; set; } = true; + + // the below exist just to make saving less cumbersome + public void Save() + { + Plugin.PluginInterface.SavePluginConfig(this); + } +} diff --git a/DiscordToXIV/Dalamud.Plugin.Bootstrap.targets b/DiscordToXIV/Dalamud.Plugin.Bootstrap.targets new file mode 100644 index 0000000..11eec9c --- /dev/null +++ b/DiscordToXIV/Dalamud.Plugin.Bootstrap.targets @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project> + <PropertyGroup> + <DalamudLibPath Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(appdata)\XIVLauncher\addon\Hooks\dev\</DalamudLibPath> + <DalamudLibPath Condition="$([MSBuild]::IsOSPlatform('Linux'))">$(HOME)/.xlcore/dalamud/Hooks/dev/</DalamudLibPath> + <DalamudLibPath Condition="$([MSBuild]::IsOSPlatform('OSX'))">$(HOME)/Library/Application Support/XIV on Mac/dalamud/Hooks/dev/</DalamudLibPath> + <DalamudLibPath Condition="$(DALAMUD_HOME) != ''">$(DALAMUD_HOME)/</DalamudLibPath> + </PropertyGroup> + + <Import Project="$(DalamudLibPath)/targets/Dalamud.Plugin.targets"/> +</Project> diff --git a/DiscordToXIV/DiscordToXIV.csproj b/DiscordToXIV/DiscordToXIV.csproj new file mode 100644 index 0000000..da2914f --- /dev/null +++ b/DiscordToXIV/DiscordToXIV.csproj @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project Sdk="Microsoft.NET.Sdk"> + <Import Project="Dalamud.Plugin.Bootstrap.targets"/> + + <PropertyGroup> + <Version>0.0.0.1</Version> + <Description>A sample plugin.</Description> + <PackageProjectUrl>https://github.com/goatcorp/DiscordToXIV</PackageProjectUrl> + <PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression> + <IsPackable>false</IsPackable> + <RootNamespace>DiscordToXIV</RootNamespace> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Fleck" Version="1.0.13" /> + </ItemGroup> + +</Project> diff --git a/DiscordToXIV/DiscordToXIV.json b/DiscordToXIV/DiscordToXIV.json new file mode 100644 index 0000000..b4469d7 --- /dev/null +++ b/DiscordToXIV/DiscordToXIV.json @@ -0,0 +1,12 @@ +{ + "Author": "pinapelz", + "Name": "DiscordToXIV", + "Punchline": "A short one-liner that shows up in /xlplugins.", + "Description": "A description that shows up in /xlplugins. List any major slash-command(s).", + "ApplicableVersion": "any", + "Tags": [ + "sample", + "plugin", + "goats" + ] +} diff --git a/DiscordToXIV/Plugin.cs b/DiscordToXIV/Plugin.cs new file mode 100644 index 0000000..bd8c977 --- /dev/null +++ b/DiscordToXIV/Plugin.cs @@ -0,0 +1,158 @@ +using System; +using Fleck; +using Dalamud.Game.Command; +using Dalamud.IoC; +using Dalamud.Plugin; +using Dalamud.Plugin.Services; +using System.Collections.Generic; +using System.Threading; +using System.Text.Json; +using System.Text.Json.Serialization; +using Dalamud.Interface.Windowing; +using DiscordToXIV.Windows; + +namespace DiscordToXIV; + + +public class Message +{ + [JsonPropertyName("author")] + public string Author { get; set; } + + [JsonPropertyName("author_name")] + public string AuthorName { get; set; } + + [JsonPropertyName("nickname")] + public string Nickname { get; set; } + + [JsonPropertyName("content")] + public string Content { get; set; } + + [JsonPropertyName("time")] + public string Time { get; set; } + + [JsonPropertyName("channel")] + public string Channel { get; set; } +} + + + +public sealed class Plugin : IDalamudPlugin +{ + [PluginService] internal static IDalamudPluginInterface PluginInterface { get; private set; } = null!; + [PluginService] internal static ICommandManager CommandManager { get; private set; } = null!; + [PluginService] internal static IChatGui ChatGui { get; private set; } = null!; + [PluginService] internal static IPluginLog PluginLog { get; private set; } = null!; + + private const string CommandName = "/pdiscordtoxiv"; + private const int DefaultPort = 8765; + + private WebSocketServer _webSocketServer; + private CancellationTokenSource _cancellationTokenSource; + private List<IWebSocketConnection> _connectedClients; + public Configuration Configuration { get; init; } + private ConfigWindow ConfigWindow { get; init; } + public readonly WindowSystem WindowSystem = new("SamplePlugin"); + + public Plugin() + { + Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); + ConfigWindow = new ConfigWindow(this); + WindowSystem.AddWindow(ConfigWindow); + _cancellationTokenSource = new CancellationTokenSource(); + _connectedClients = new List<IWebSocketConnection>(); + + CommandManager.AddHandler(CommandName, new CommandInfo(OnCommand) + { + HelpMessage = "Start WebSocket server. Usage: /pdiscordtoxiv [port]" + }); + PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI; + ChatGui.Print("Plugin starting..."); + } + + public void Dispose() + { + StopWebSocketServer(); + CommandManager.RemoveHandler(CommandName); + } + + private void OnCommand(string command, string args) + { + var port = DefaultPort; + if (!string.IsNullOrEmpty(args)) + { + if(args == "stop") + { + StopWebSocketServer(); + ChatGui.Print("WebSocket server stop requested"); + return; + } + if (!int.TryParse(args, out port) || port <= 0 || port > 65535) + { + ChatGui.PrintError($"Invalid port number: {args}. Using default port {DefaultPort}."); + port = DefaultPort; + } + } + + StartWebSocketServer(port); + ChatGui.Print($"WebSocket server started on port {port}"); + } + + private void StartWebSocketServer(int port) + { + StopWebSocketServer(); + + _webSocketServer = new WebSocketServer($"ws://0.0.0.0:{port}"); + + _webSocketServer.Start(socket => + { + socket.OnOpen = () => + { + PluginLog.Information("WebSocket connection opened."); + _connectedClients.Add(socket); + }; + + socket.OnClose = () => + { + PluginLog.Information("WebSocket connection closed."); + _connectedClients.Remove(socket); + }; + + socket.OnMessage = message => + { + try + { + Message receivedMessage = JsonSerializer.Deserialize<Message>(message); + //PluginLog.Information($"Message from {receivedMessage.Nickname}: {receivedMessage.Content}"); + ChatGui.Print($"[{receivedMessage.AuthorName}] {receivedMessage.Nickname}: {receivedMessage.Content}"); + } + catch (Exception ex) + { + PluginLog.Error($"Failed to deserialize message: {ex.Message}"); + } + }; + + }); + } + + private void StopWebSocketServer() + { + if (_webSocketServer != null) + { + PluginLog.Information("Stopping WebSocket server..."); + + foreach (var socket in _connectedClients) + { + if (socket.IsAvailable) + { + socket.Close(); + } + } + + _webSocketServer.Dispose(); + _cancellationTokenSource.Cancel(); + PluginLog.Information("WebSocket server stopped."); + } + } + public void ToggleConfigUI() => ConfigWindow.Toggle(); +} diff --git a/DiscordToXIV/Windows/ConfigWindow.cs b/DiscordToXIV/Windows/ConfigWindow.cs new file mode 100644 index 0000000..d386fc3 --- /dev/null +++ b/DiscordToXIV/Windows/ConfigWindow.cs @@ -0,0 +1,38 @@ +using System; +using System.Numerics; +using Dalamud.Interface.Windowing; +using ImGuiNET; + +namespace DiscordToXIV.Windows; + + +public class ConfigWindow : Window, IDisposable +{ + private Configuration Configuration; + + public ConfigWindow(Plugin plugin) : base("A Wonderful Configuration Window###With a constant ID") + { + Size = new Vector2(232, 90); + SizeCondition = ImGuiCond.Always; + Configuration = plugin.Configuration; + } + + public void Dispose() { } + + public override void Draw() + { + var configValue = Configuration.SomePropertyToBeSavedAndWithADefault; + if (ImGui.Checkbox("Random Config Bool", ref configValue)) + { + Configuration.SomePropertyToBeSavedAndWithADefault = configValue; + Configuration.Save(); + } + + var movable = Configuration.IsConfigWindowMovable; + if (ImGui.Checkbox("Movable Config Window", ref movable)) + { + Configuration.IsConfigWindowMovable = movable; + Configuration.Save(); + } + } +} diff --git a/DiscordToXIV/packages.lock.json b/DiscordToXIV/packages.lock.json new file mode 100644 index 0000000..5c97610 --- /dev/null +++ b/DiscordToXIV/packages.lock.json @@ -0,0 +1,19 @@ +{ + "version": 1, + "dependencies": { + "net8.0-windows7.0": { + "DalamudPackager": { + "type": "Direct", + "requested": "[2.1.13, )", + "resolved": "2.1.13", + "contentHash": "rMN1omGe8536f4xLMvx9NwfvpAc9YFFfeXJ1t4P4PE6Gu8WCIoFliR1sh07hM+bfODmesk/dvMbji7vNI+B/pQ==" + }, + "Fleck": { + "type": "Direct", + "requested": "[1.0.13, )", + "resolved": "1.1.0", + "contentHash": "X33W1PqRciRmh8Q23klEv3CiRS681IUJj/Ldlgi96lSfq1Uaux3l1qNewRDboiUxnHfoNtcIhFMIhc9AJ4cw6g==" + } + } + } +}
\ No newline at end of file |
