Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add port configuration #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WhosTalking/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed class Configuration: IPluginConfiguration {
public bool ShowUnmatchedUsers { get; set; } = true;
public IndicatorStyle IndicatorStyle { get; set; } = IndicatorStyle.Imgui;
public bool UseRoundedCorners { get; set; } = true;
public int Port { get; set; } = 6463;

// colours are ABGR
public uint ColourUnmatched { get; set; } = 0xFF00FFFF; // yellow
Expand Down
4 changes: 3 additions & 1 deletion WhosTalking/Discord/DiscordConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DiscordConnection(Plugin plugin) {
this.plugin = plugin;
this.AllUsers = new Dictionary<string, User>();
this.webSocket = new WebsocketClient(
new Uri($"ws://127.0.0.1:6463/?v=1&client_id={ClientId}"),
new Uri($"ws://127.0.0.1:{this.Port}/?v=1&client_id={ClientId}"),
() => {
var client = new ClientWebSocket();
client.Options.SetRequestHeader("Origin", "https://streamkit.discord.com");
Expand Down Expand Up @@ -95,6 +95,8 @@ private string? AccessToken {
this.plugin.Configuration.Save();
}
}

private int Port => this.plugin.Configuration.Port;

public void Send(string msg) {
this.webSocket.Send(msg);
Expand Down
28 changes: 26 additions & 2 deletions WhosTalking/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ IAddonLifecycle addonLifecycle
this.IpcSystem = new IpcSystem(this, pluginInterface);
this.disposeActions.Push(() => this.IpcSystem.Dispose());

this.CommandManager.AddHandler("/whostalking", new CommandInfo(this.OnCommand));
this.CommandManager.AddHandler("/whostalking", new CommandInfo(this.OnCommand)
{
HelpMessage = "Toggle Settings\n" + "/whostalking port <number> --> Set the port used",
});

this.disposeActions.Push(() => this.CommandManager.RemoveHandler("/whostalking"));

this.AddonLifecycle.RegisterListener(AddonEvent.PreDraw, "_PartyList", this.AtkDrawPartyList);
Expand Down Expand Up @@ -135,13 +139,33 @@ public void Dispose() {
}

private void OnCommand(string command, string args) {
this.ConfigWindow.IsOpen = !this.ConfigWindow.IsOpen;
if (args.Length != 0) {
var arguments = args.Split(" ");
if (arguments[0].ToLower().Equals("port") && arguments.Length > 1) {
if (int.TryParse(arguments[1], out var port)) {
Configuration.Port = port;
Configuration.Save();
this.PluginLog.Information($"Port set to {port}. Reconnecting...");
this.ReconnectDiscord();
} else {
this.PluginLog.Error("Port is not a valid number.");
}
}
} else {
this.ConfigWindow.IsOpen = !this.ConfigWindow.IsOpen;
}
}

public void OpenConfigUi() {
this.ConfigWindow.IsOpen = true;
}

public void ReconnectDiscord() {
this.Connection.Dispose();
this.Connection = new DiscordConnection(this);
this.disposeActions.Push(() => this.Connection.Dispose());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will cause this.Connection to be disposed multiple times, right? since the constructor already pushes one of these.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if the pushed one still works after replacing this.Connection with a new instance of DiscordConnection. I presumed it would not since the reference that was pushed into the stack points to a no-longer used instance of DiscordConnection. But i might be totally of the mark

}

private uint GetColour(User? user) {
if (user == null) {
return this.Configuration.ShowUnmatchedUsers ? this.Configuration.ColourUnmatched : 0;
Expand Down
22 changes: 22 additions & 0 deletions WhosTalking/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ ref showNonXivUsersAlways
this.plugin.Configuration.Save();
}

var discordPort = this.plugin.Configuration.Port;
if (ImGui.InputInt("Discord port", ref discordPort)) {
this.plugin.Configuration.Port = discordPort;
this.plugin.Configuration.Save();
}
if (ImGui.IsItemHovered()) {
ImGui.SetTooltip("Is your Discord running on a different port? Default: 6463.\n"
+ "Useful when running multiple clients or other software blocking the default port.\n"
+ "If a port is already in use increment the port by one and try again.\n"
+ "If in doubt, leave it/set to default 6463!");
}
if (ImGui.Button("Reconnect")) {
this.plugin.ReconnectDiscord();
}
if (ImGui.IsItemHovered()) {
ImGui.SetTooltip("Try to connect to the set port.");
}

ImGui.SameLine();
ImGui.TextUnformatted(this.plugin.Connection.IsConnected ? "Connected!" : "Failed to connect!");


if (ImGui.IsItemHovered() && !showIndicators) {
ImGui.SetTooltip(
"Because “Show voice activity indicators” is disabled,"
Expand Down