Skip to content

Commit

Permalink
The 'Connected' options is now named 'Connect to <server>' with name …
Browse files Browse the repository at this point in the history
…of Server setting. If multiple connection attempts fail and Server is not the DefaultServer, a button pops up right below the Connect toggle to connect back to official default server.
  • Loading branch information
RedFlames committed Aug 24, 2024
1 parent d0d9f6a commit 456efdf
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
35 changes: 29 additions & 6 deletions CelesteNet.Client/CelesteNetClientModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ public class CelesteNetClientModule : EverestModule {

public bool MayReconnect => (DateTime.UtcNow - LastConnectionAttempt).TotalSeconds > ReconnectWaitTime;

// currently used to show a warning/reset button to connect to default server again,
// when connecting to a different server fails repeatedly
private int _FailedReconnectCount = 0;
public int FailedReconnectCount {
get => _FailedReconnectCount;
private set {
_FailedReconnectCount = value;

if (value >= FailedReconnectThreshold && Settings.Server != CelesteNetClientSettings.DefaultServer) {
Settings.ConnectDefaultVisible = true;
Settings.WantsToBeConnected = false;
}
else
Settings.ConnectDefaultVisible = false;
}
}
public const int FailedReconnectThreshold = 3;

public VirtualRenderTarget UIRenderTarget;

// This should ideally be part of the "emote module" if emotes were a fully separate thing.
Expand Down Expand Up @@ -314,7 +332,7 @@ public void Start() {

if (_StartThread?.IsAlive ?? false) {
Logger.Log(LogLevel.DEV, "lifecycle", $"CelesteNetClientModule Start: StartThread.Join...");
_StartThread.Join();
_StartThread?.Join();
Logger.Log(LogLevel.DEV, "lifecycle", $"CelesteNetClientModule Start: StartThread Join done");
}
_StartTokenSource?.Dispose();
Expand Down Expand Up @@ -347,9 +365,7 @@ public void Start() {

// fully reset wait time
if ((DateTime.UtcNow - LastConnectionAttempt).TotalSeconds > FastReconnectResetAfter && ReconnectWaitTime > 0) {
Logger.Log(LogLevel.INF, "reconnect-attempt", $"CelesteNetClientModule Start: Resetting reconnect delay from {ReconnectWaitTime} seconds to 0... (started {ReconnectDelayingSince})");
ReconnectWaitTime = 0;
ReconnectWaitRepetitions = 0;
ResetReconnectPenalty();
}

lock (ClientLock) {
Expand Down Expand Up @@ -403,12 +419,12 @@ public void Start() {
if (context.Status.Spin)
context.Status.Set("Connected", 1f);

FailedReconnectCount = 0;
} catch (Exception e) when (e is ThreadInterruptedException || e is OperationCanceledException) {
Logger.Log(LogLevel.CRI, "clientmod", "Startup interrupted.");
_StartThread = null;
Stop();
context.Status.Set("Interrupted", 3f, false);

} catch (ThreadAbortException) {
Logger.Log(LogLevel.VVV, "main", $"Client Start thread: ThreadAbortException caught");
_StartThread = null;
Expand Down Expand Up @@ -436,6 +452,7 @@ public void Start() {
// Instead, dispose the client and let the context do the rest.
context.Client.SafeDisposeTriggered = true;
context.Status.Set("Connection failed", 3f, false);
FailedReconnectCount++;
}

} finally {
Expand Down Expand Up @@ -466,7 +483,7 @@ public void Stop() {

if (_StartThread?.IsAlive ?? false) {
Logger.Log(LogLevel.DEV, "lifecycle", $"CelesteNetClientModule Stop: Joining StartThread...");
_StartThread.Join();
_StartThread?.Join();
Logger.Log(LogLevel.DEV, "lifecycle", $"CelesteNetClientModule Stop: Joining done");
}
_StartTokenSource?.Dispose();
Expand All @@ -483,5 +500,11 @@ public void Stop() {
}
}

public void ResetReconnectPenalty() {
Logger.Log(LogLevel.INF, "reconnect-attempt", $"CelesteNetClientModule Start: Resetting reconnect delay from {ReconnectWaitTime} seconds to 0... (started {ReconnectDelayingSince})");
ReconnectWaitTime = 0;
ReconnectWaitRepetitions = 0;
}

}
}
73 changes: 69 additions & 4 deletions CelesteNet.Client/CelesteNetClientSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public bool Connected {
[SettingIgnore, YamlIgnore]
public TextMenu.OnOff EnabledEntry { get; protected set; }

// A button that only shows when the "visible" bool below gets set, to easily allow connecting back to official server
[SettingIgnore, YamlIgnore]
public TextMenu.Button ConnectDefaultButton { get; protected set; }
[SettingIgnore, YamlIgnore]
public TextMenuExt.EaseInSubHeaderExt ConnectDefaultButtonHint { get; protected set; }

private bool _ConnectDefaultVisible = false;
[SettingIgnore, YamlIgnore]
public bool ConnectDefaultVisible {
get => _ConnectDefaultVisible;
set {
_ConnectDefaultVisible = value;

if (ConnectDefaultButton != null)
ConnectDefaultButton.Visible = value;
}
}

public bool AutoReconnect { get; set; } = true;
[SettingIgnore, YamlIgnore]
public TextMenu.OnOff AutoReconnectEntry { get; protected set; }
Expand All @@ -90,16 +108,43 @@ public string Server {

_Server = value;

if (ServerEntry != null)
ServerEntry.Label = "modoptions_celestenetclient_server".DialogClean().Replace("((server))", value);
UpdateServerInDialogs();
}
}
private string _Server = DefaultServer;

// Any non-empty string will override Server property temporarily. (setting not saved)
// Currently only used for "connect locally" button (for Nucleus etc.)
[SettingIgnore, YamlIgnore]
public string ServerOverride { get; set; } = "";
public string ServerOverride {
get => _ServerOverride;
set {
if (string.IsNullOrWhiteSpace(value))
{
_ServerOverride = "";
} else {
_ServerOverride = value;
}
UpdateServerInDialogs();
}
}

// best way I can come up with to do this in various places, rather than a lot of erratic logic in Server & ServerOverride setters
public void UpdateServerInDialogs() {
if (ServerEntry != null)
ServerEntry.Label = "modoptions_celestenetclient_server".DialogClean().Replace("((server))", Server);

if (EnabledEntry != null)
EnabledEntry.Label = "modoptions_celestenetclient_connected".DialogClean().Replace("((server))", Server);

if (ConnectDefaultButton != null)
ConnectDefaultButton.Label = "modoptions_celestenetclient_connectdefault".DialogClean().Replace("((default))", DefaultServer);

if (ConnectDefaultButtonHint != null)
ConnectDefaultButtonHint.Title = "modoptions_celestenetclient_connectdefaulthint".DialogClean().Replace("((server))", Server);
}

private string _ServerOverride = "";

[SettingIgnore, YamlIgnore]
public TextMenu.Button ServerEntry { get; protected set; }
Expand Down Expand Up @@ -813,7 +858,7 @@ public TextMenu.Button CreateMenuStringInput(TextMenu menu, string dialogLabel,

public void CreateConnectedEntry(TextMenu menu, bool inGame) {
menu.Add(
(EnabledEntry = new TextMenu.OnOff("modoptions_celestenetclient_connected".DialogClean(), Connected))
(EnabledEntry = new TextMenu.OnOff("modoptions_celestenetclient_connected".DialogClean().Replace("((server))", Server), Connected))
.Change(v => Connected = v)
);
EnabledEntry.AddDescription(menu, "modoptions_celestenetclient_connectedhint".DialogClean());
Expand Down Expand Up @@ -944,6 +989,8 @@ public void CreateResetGeneralButtonEntry(TextMenu menu, bool inGame) {
ReceivePlayerAvatars = true;
ClientID = GenerateClientID();
ServerOverride = "";
KeyError = KeyErrors.None;
ConnectDefaultVisible = false;
});
ResetGeneralButton.AddDescription(menu, "modoptions_celestenetclient_resetgeneralhint".DialogClean());
ResetGeneralButton.Disabled = Connected;
Expand Down Expand Up @@ -974,6 +1021,24 @@ public void CreateReceivePlayerAvatarsEntry(TextMenu menu, bool inGame) {
ReceivePlayerAvatarsEntry.AddDescription(menu, "modoptions_celestenetclient_avatarshint".DialogClean());
}

public void CreateConnectDefaultButtonEntry(TextMenu menu, bool inGame) {
ConnectDefaultButton = CreateMenuButton(menu, "CONNECTDEFAULT", (label) => label.Replace("((default))", DefaultServer), () => {
ServerOverride = "";
Server = DefaultServer;
CelesteNetClientModule.Instance.ResetReconnectPenalty();
Connected = true;
});

ConnectDefaultButtonHint = null;
ConnectDefaultButton.AddDescription(menu, "modoptions_celestenetclient_connectdefaulthint".DialogClean().Replace("((server))", Server));

int descriptionIndex = menu.Items.IndexOf(ConnectDefaultButton) + 1;
if (descriptionIndex > 0 && descriptionIndex < menu.Items.Count && menu.Items[descriptionIndex] is TextMenuExt.EaseInSubHeaderExt desc)
ConnectDefaultButtonHint = desc;

ConnectDefaultButton.Visible = ConnectDefaultVisible;
}

#endregion

public static ulong GenerateClientID() {
Expand Down
4 changes: 3 additions & 1 deletion Dialog/English.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
# GhostNet Module Options
MODOPTIONS_CELESTENETCLIENT_TITLE= CelesteNet - Multiplayer

MODOPTIONS_CELESTENETCLIENT_CONNECTED= Connected
MODOPTIONS_CELESTENETCLIENT_CONNECTED= Connect to ((server))
MODOPTIONS_CELESTENETCLIENT_CONNECTEDHINT= Try setting "Receive Player Avatars" OFF if you can't connect
MODOPTIONS_CELESTENETCLIENT_CONNECTDEFAULT= :celestenet_warning: Connect to ((default))?
MODOPTIONS_CELESTENETCLIENT_CONNECTDEFAULTHINT= :celestenet_warning: Connection to "((server))" failed or timed out.
MODOPTIONS_CELESTENETCLIENT_AVATARS= Receive Player Avatars
MODOPTIONS_CELESTENETCLIENT_AVATARSHINT= Tells the server not to send you any profile pics during handshake.
MODOPTIONS_CELESTENETCLIENT_AUTORECONNECT= Auto Reconnect
Expand Down

0 comments on commit 456efdf

Please sign in to comment.