Simplify the replay browser code.

This commit is contained in:
Paul Chote
2014-03-14 17:40:06 +13:00
parent c30b18a9d6
commit 4c4783262f
3 changed files with 27 additions and 64 deletions

View File

@@ -1,41 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
namespace OpenRA.Network
{
public class Replay
{
public readonly string Filename;
public readonly int Duration;
public readonly Session LobbyInfo;
public Replay(string filename)
{
Filename = filename;
using (var conn = new ReplayConnection(filename))
{
Duration = conn.TickCount * Game.NetTickScale;
LobbyInfo = conn.LobbyInfo;
}
}
public Map Map()
{
if (LobbyInfo == null)
return null;
var map = LobbyInfo.GlobalSettings.Map;
return Game.modData.MapCache[map].Map;
}
}
}

View File

@@ -129,7 +129,6 @@
<Compile Include="Network\Order.cs" /> <Compile Include="Network\Order.cs" />
<Compile Include="Network\OrderIO.cs" /> <Compile Include="Network\OrderIO.cs" />
<Compile Include="Network\OrderManager.cs" /> <Compile Include="Network\OrderManager.cs" />
<Compile Include="Network\Replay.cs" />
<Compile Include="Network\ReplayConnection.cs" /> <Compile Include="Network\ReplayConnection.cs" />
<Compile Include="Network\ServerList.cs" /> <Compile Include="Network\ServerList.cs" />
<Compile Include="Network\Session.cs" /> <Compile Include="Network\Session.cs" />

View File

@@ -19,6 +19,11 @@ namespace OpenRA.Mods.RA.Widgets.Logic
public class ReplayBrowserLogic public class ReplayBrowserLogic
{ {
Widget panel; Widget panel;
MapPreview selectedMap = MapCache.UnknownMap;
string selectedFilename;
string selectedDuration;
string selectedPlayers;
bool selectedValid;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public ReplayBrowserLogic(Widget widget, Action onExit, Action onStart) public ReplayBrowserLogic(Widget widget, Action onExit, Action onStart)
@@ -44,15 +49,16 @@ namespace OpenRA.Mods.RA.Widgets.Logic
} }
var watch = panel.Get<ButtonWidget>("WATCH_BUTTON"); var watch = panel.Get<ButtonWidget>("WATCH_BUTTON");
watch.IsDisabled = () => currentReplay == null || currentMap == null || currentReplay.Duration == 0; watch.IsDisabled = () => !selectedValid || selectedMap.Status != MapStatus.Available;
watch.OnClick = () => { WatchReplay(); onStart(); }; watch.OnClick = () => { WatchReplay(); onStart(); };
panel.Get("REPLAY_INFO").IsVisible = () => currentReplay != null; panel.Get("REPLAY_INFO").IsVisible = () => selectedFilename != null;;
panel.Get<LabelWidget>("DURATION").GetText = () => selectedDuration;
panel.Get<MapPreviewWidget>("MAP_PREVIEW").Preview = () => selectedMap;
panel.Get<LabelWidget>("MAP_TITLE").GetText = () => selectedMap.Title;
panel.Get<LabelWidget>("PLAYERS").GetText = () => selectedPlayers;
} }
Replay currentReplay;
MapPreview currentMap = MapCache.UnknownMap;
void SelectReplay(string filename) void SelectReplay(string filename)
{ {
if (filename == null) if (filename == null)
@@ -60,32 +66,31 @@ namespace OpenRA.Mods.RA.Widgets.Logic
try try
{ {
currentReplay = new Replay(filename); using (var conn = new ReplayConnection(filename))
currentMap = Game.modData.MapCache[currentReplay.LobbyInfo.GlobalSettings.Map]; {
selectedFilename = filename;
panel.Get<LabelWidget>("DURATION").GetText = selectedMap = Game.modData.MapCache[conn.LobbyInfo.GlobalSettings.Map];
() => WidgetUtils.FormatTime(currentReplay.Duration); selectedDuration = WidgetUtils.FormatTime(conn.TickCount * Game.NetTickScale);
panel.Get<MapPreviewWidget>("MAP_PREVIEW").Preview = () => currentMap; selectedPlayers = conn.LobbyInfo.Slots
panel.Get<LabelWidget>("MAP_TITLE").GetText = .Count(s => conn.LobbyInfo.ClientInSlot(s.Key) != null)
() => currentMap != null ? currentMap.Title : "(Unknown Map)"; .ToString();
selectedValid = conn.TickCount > 0;
var players = currentReplay.LobbyInfo.Slots }
.Count(s => currentReplay.LobbyInfo.ClientInSlot(s.Key) != null);
panel.Get<LabelWidget>("PLAYERS").GetText = () => players.ToString();
} }
catch (Exception e) catch (Exception e)
{ {
Log.Write("debug", "Exception while parsing replay: {0}", e); Log.Write("debug", "Exception while parsing replay: {0}", e);
currentReplay = null; selectedFilename = null;
currentMap = MapCache.UnknownMap; selectedValid = false;
selectedMap = MapCache.UnknownMap;
} }
} }
void WatchReplay() void WatchReplay()
{ {
if (currentReplay != null) if (selectedFilename != null)
{ {
Game.JoinReplay(currentReplay.Filename); Game.JoinReplay(selectedFilename);
Ui.CloseWindow(); Ui.CloseWindow();
} }
} }
@@ -93,7 +98,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
void AddReplay(ScrollPanelWidget list, string filename, ScrollItemWidget template) void AddReplay(ScrollPanelWidget list, string filename, ScrollItemWidget template)
{ {
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
() => currentReplay != null && currentReplay.Filename == filename, () => selectedFilename == filename,
() => SelectReplay(filename), () => SelectReplay(filename),
() => WatchReplay()); () => WatchReplay());
var f = Path.GetFileName(filename); var f = Path.GetFileName(filename);