Cache GameServer joinable/compatible state across render frames.

This commit is contained in:
Paul Chote
2014-12-13 10:34:54 +13:00
parent b17c49d12f
commit 199f80c8ed
2 changed files with 27 additions and 50 deletions

View File

@@ -26,42 +26,30 @@ namespace OpenRA.Network
public readonly bool Protected = false; public readonly bool Protected = false;
public readonly string Started = null; public readonly string Started = null;
public bool CanJoin() public readonly bool IsCompatible = false;
public readonly bool IsJoinable = false;
public readonly string ModLabel = "";
public readonly string ModVersion = "";
public GameServer(MiniYaml yaml)
{ {
// "waiting for players" FieldLoader.Load(this, yaml);
if (State != 1)
return false;
if (!CompatibleVersion()) ModMetadata mod;
return false;
// Don't have the map locally
// TODO: We allow joining, then drop on game start if the map isn't available
if (Game.modData.MapCache[Map].Status != MapStatus.Available && !Game.Settings.Game.AllowDownloading)
return false;
return true;
}
public bool CompatibleVersion()
{
// Invalid game listing - we require one entry of id@version
var modVersion = Mods.Split('@'); var modVersion = Mods.Split('@');
if (modVersion.Length != 2) if (modVersion.Length == 2 && ModMetadata.AllMods.TryGetValue(modVersion[0], out mod))
return false; {
ModLabel = "{0} ({1})".F(mod.Title, modVersion[1]);
ModVersion = modVersion[1];
var mod = Game.modData.Manifest.Mod; IsCompatible = Game.Settings.Debug.IgnoreVersionMismatch || (mod.Id == Game.modData.Manifest.Mod.Id && ModVersion == Game.modData.Manifest.Mod.Version);
}
else
ModLabel = "Unknown mod: {0}".F(Mods);
// Different mod var mapAvailable = Game.Settings.Game.AllowDownloading || Game.modData.MapCache[Map].Status == MapStatus.Available;
// TODO: Allow mod switch when joining server IsJoinable = IsCompatible && State == 1 && mapAvailable;
if (modVersion[0] != mod.Id)
return false;
// Same mod, but different version
if (modVersion[1] != mod.Version && !Game.Settings.Debug.IgnoreVersionMismatch)
return false;
return true;
} }
} }
} }

View File

@@ -69,7 +69,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = OpenCreateServerPanel; panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = OpenCreateServerPanel;
var join = panel.Get<ButtonWidget>("JOIN_BUTTON"); var join = panel.Get<ButtonWidget>("JOIN_BUTTON");
join.IsDisabled = () => currentServer == null || !currentServer.CanJoin(); join.IsDisabled = () => currentServer == null || !currentServer.IsJoinable;
join.OnClick = () => Join(currentServer); join.OnClick = () => Join(currentServer);
panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); }; panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
@@ -151,7 +151,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var data = Encoding.UTF8.GetString(i.Result); var data = Encoding.UTF8.GetString(i.Result);
var yaml = MiniYaml.FromString(data); var yaml = MiniYaml.FromString(data);
var games = yaml.Select(a => FieldLoader.Load<GameServer>(a.Value)) var games = yaml.Select(a => new GameServer(a.Value))
.Where(gs => gs.Address != null); .Where(gs => gs.Address != null);
RefreshServerListInner(games); RefreshServerListInner(games);
@@ -168,14 +168,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var rows = new List<Widget>(); var rows = new List<Widget>();
foreach (var loop in games.OrderByDescending(g => g.CanJoin()).ThenByDescending(g => g.Players)) foreach (var loop in games.OrderByDescending(g => g.IsJoinable).ThenByDescending(g => g.Players))
{ {
var game = loop; var game = loop;
if (game == null) if (game == null)
continue; continue;
var canJoin = game.CanJoin(); var canJoin = game.IsJoinable;
var compatible = game.CompatibleVersion(); var compatible = game.IsCompatible;
var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game, () => Join(game)); var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game, () => Join(game));
@@ -223,7 +223,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var version = item.GetOrNull<LabelWidget>("VERSION"); var version = item.GetOrNull<LabelWidget>("VERSION");
if (version != null) if (version != null)
{ {
version.GetText = () => GenerateModLabel(game); version.GetText = () => game.ModLabel;
version.IsVisible = () => !compatible; version.IsVisible = () => !compatible;
version.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : version.TextColor; version.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : version.TextColor;
} }
@@ -300,7 +300,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
void Join(GameServer server) void Join(GameServer server)
{ {
if (server == null || !server.CanJoin()) if (server == null || !server.IsJoinable)
return; return;
var host = server.Address.Split(':')[0]; var host = server.Address.Split(':')[0];
@@ -353,17 +353,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return label.TextColor; return label.TextColor;
} }
public static string GenerateModLabel(GameServer s)
{
ModMetadata mod;
var modVersion = s.Mods.Split('@');
if (modVersion.Length == 2 && ModMetadata.AllMods.TryGetValue(modVersion[0], out mod))
return "{0} ({1})".F(mod.Title, modVersion[1]);
return "Unknown mod: {0}".F(s.Mods);
}
bool Filtered(GameServer game) bool Filtered(GameServer game)
{ {
if ((game.State == (int)ServerState.GameStarted) && !showStarted) if ((game.State == (int)ServerState.GameStarted) && !showStarted)
@@ -375,7 +364,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
if ((game.Players == 0) && !showEmpty) if ((game.Players == 0) && !showEmpty)
return true; return true;
if (!game.CompatibleVersion() && !showIncompatible) if (!game.IsCompatible && !showIncompatible)
return true; return true;
if (game.Protected && !showProtected) if (game.Protected && !showProtected)