complete password protected servers

closes #2290
This commit is contained in:
Matthias Mailänder
2013-10-05 13:02:47 +02:00
parent a6cdcea414
commit b618fc7cc2
17 changed files with 229 additions and 81 deletions

View File

@@ -90,6 +90,7 @@ namespace OpenRA.Mods.RA
case "StartGame":
case "Disconnected":
case "ServerError":
case "AuthenticationError":
case "SyncInfo":
return;
}

View File

@@ -16,7 +16,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{
public class ConnectionLogic
{
Action onConnect, onRetry, onAbort;
Action onConnect, onAbort;
Action<string> onRetry;
void ConnectionStateChanged(OrderManager om)
{
@@ -44,11 +45,11 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}
[ObjectCreator.UseCtor]
public ConnectionLogic(Widget widget, string host, int port, Action onConnect, Action onRetry, Action onAbort)
public ConnectionLogic(Widget widget, string host, int port, Action onConnect, Action onAbort, Action<string> onRetry)
{
this.onConnect = onConnect;
this.onRetry = onRetry;
this.onAbort = onAbort;
this.onRetry = onRetry;
Game.ConnectionStateChanged += ConnectionStateChanged;
@@ -59,31 +60,81 @@ namespace OpenRA.Mods.RA.Widgets.Logic
"Connecting to {0}:{1}...".F(host, port);
}
public static void Connect(string host, int port, Action onConnect, Action onAbort)
public static void Connect(string host, int port, string password, Action onConnect, Action onAbort)
{
Game.JoinServer(host, port);
Game.JoinServer(host, port, password);
Action<string> onRetry = newPassword => Connect(host, port, newPassword, onConnect, onAbort);
Ui.OpenWindow("CONNECTING_PANEL", new WidgetArgs()
{
{ "host", host },
{ "port", port },
{ "onConnect", onConnect },
{ "onAbort", onAbort },
{ "onRetry", () => Connect(host, port, onConnect, onAbort) }
{ "onRetry", onRetry }
});
}
}
public class ConnectionFailedLogic
{
PasswordFieldWidget passwordField;
bool passwordOffsetAdjusted;
[ObjectCreator.UseCtor]
public ConnectionFailedLogic(Widget widget, OrderManager orderManager, Action onRetry, Action onAbort)
public ConnectionFailedLogic(Widget widget, OrderManager orderManager, Action onAbort, Action<string> onRetry)
{
var panel = widget;
panel.Get<ButtonWidget>("ABORT_BUTTON").OnClick = () => { Ui.CloseWindow(); onAbort(); };
panel.Get<ButtonWidget>("RETRY_BUTTON").OnClick = () => { Ui.CloseWindow(); onRetry(); };
var abortButton = panel.Get<ButtonWidget>("ABORT_BUTTON");
var retryButton = panel.Get<ButtonWidget>("RETRY_BUTTON");
abortButton.OnClick = () => { Ui.CloseWindow(); onAbort(); };
retryButton.OnClick = () =>
{
var password = passwordField != null && passwordField.IsVisible() ? passwordField.Text : orderManager.Password;
Ui.CloseWindow();
onRetry(password);
};
widget.Get<LabelWidget>("CONNECTING_DESC").GetText = () =>
"Could not connect to {0}:{1}\n{2}".F(orderManager.Host, orderManager.Port, orderManager.ServerError);
"Could not connect to {0}:{1}".F(orderManager.Host, orderManager.Port);
var connectionError = widget.Get<LabelWidget>("CONNECTION_ERROR");
connectionError.GetText = () => orderManager.ServerError;
passwordField = panel.GetOrNull<PasswordFieldWidget>("PASSWORD");
if (passwordField != null)
{
passwordField.Text = orderManager.Password;
passwordField.IsVisible = () => orderManager.AuthenticationFailed;
var passwordLabel = widget.Get<LabelWidget>("PASSWORD_LABEL");
passwordLabel.IsVisible = passwordField.IsVisible;
}
passwordOffsetAdjusted = false;
var connectionFailedTicker = panel.GetOrNull<LogicTickerWidget>("CONNECTION_FAILED_TICKER");
if (connectionFailedTicker != null)
{
connectionFailedTicker.OnTick = () =>
{
// Adjust the dialog once the AuthenticationError is parsed.
if (passwordField.IsVisible() && !passwordOffsetAdjusted)
{
var offset = passwordField.Bounds.Y - connectionError.Bounds.Y;
abortButton.Bounds.Y += offset;
retryButton.Bounds.Y += offset;
panel.Bounds.Height += offset;
panel.Bounds.Y -= offset / 2;
var background = panel.GetOrNull("CONNECTION_BACKGROUND");
if (background != null)
background.Bounds.Height += offset;
passwordOffsetAdjusted = true;
}
};
}
}
}
}

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Game.Settings.Save();
Ui.CloseWindow();
ConnectionLogic.Connect(ipField.Text, port, openLobby, onExit);
ConnectionLogic.Connect(ipField.Text, port, "", openLobby, onExit);
};
panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };

View File

@@ -58,9 +58,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
});
};
Action onRetry = () =>
Action<string> onRetry = password =>
{
ConnectionLogic.Connect(om.Host, om.Port, onConnect, onExit);
ConnectionLogic.Connect(om.Host, om.Port, password, onConnect, onExit);
};
Ui.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs()

View File

@@ -109,13 +109,13 @@ namespace OpenRA.Mods.RA.Widgets.Logic
void Join(GameServer server)
{
if (server == null || !server.CanJoin())
return;
return;
var host = server.Address.Split(':')[0];
var port = int.Parse(server.Address.Split(':')[1]);
Ui.CloseWindow();
ConnectionLogic.Connect(host, port, OpenLobby, OnExit);
ConnectionLogic.Connect(host, port, "", OpenLobby, OnExit);
}
string GetPlayersLabel(GameServer game)

View File

@@ -32,6 +32,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
this.onExit = onExit;
var settings = Game.Settings;
panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = CreateAndJoin;
@@ -71,6 +72,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic
UPnPCheckbox.IsChecked = () => allowPortForward;
UPnPCheckbox.OnClick = () => allowPortForward ^= true;
UPnPCheckbox.IsDisabled = () => !Game.Settings.Server.NatDeviceAvailable;
var passwordField = panel.GetOrNull<PasswordFieldWidget>("PASSWORD");
if (passwordField != null)
passwordField.Text = Game.Settings.Server.Password;
}
void CreateAndJoin()
@@ -83,6 +88,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if (!int.TryParse(panel.Get<TextFieldWidget>("EXTERNAL_PORT").Text, out externalPort))
externalPort = 1234;
var passwordField = panel.GetOrNull<PasswordFieldWidget>("PASSWORD");
var password = passwordField != null ? passwordField.Text : "";
// Save new settings
Game.Settings.Server.Name = name;
Game.Settings.Server.ListenPort = listenPort;
@@ -90,6 +98,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Game.Settings.Server.AdvertiseOnline = advertiseOnline;
Game.Settings.Server.AllowPortForward = allowPortForward;
Game.Settings.Server.Map = map.Uid;
Game.Settings.Server.Password = password;
Game.Settings.Save();
// Take a copy so that subsequent changes don't affect the server
@@ -98,7 +107,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
// Create and join the server
Game.CreateServer(settings);
Ui.CloseWindow();
ConnectionLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, onCreate, onExit);
ConnectionLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, password, onCreate, onExit);
}
}
}