From ab28f5867f752f0945c448d64d73df8862e9ff02 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 10 May 2011 18:31:24 +1200 Subject: [PATCH] Disallow watching replays with zero duration. --- .../Widgets/CncReplayBrowserLogic.cs | 66 +++++++++---------- .../Delegates/ReplayBrowserDelegate.cs | 2 + 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/OpenRA.Mods.Cnc/Widgets/CncReplayBrowserLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncReplayBrowserLogic.cs index 638e972607..5e35c9e50e 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncReplayBrowserLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncReplayBrowserLogic.cs @@ -37,7 +37,6 @@ namespace OpenRA.Mods.Cnc.Widgets var replayDir = Path.Combine(Platform.SupportDir, "Replays"); var template = panel.GetWidget("REPLAY_TEMPLATE"); - CurrentReplay = null; rl.RemoveChildren(); if (Directory.Exists(replayDir)) @@ -46,54 +45,49 @@ namespace OpenRA.Mods.Cnc.Widgets foreach (var replayFile in files) AddReplay(rl, replayFile, template); - CurrentReplay = files.FirstOrDefault(); + SelectReplay(files.FirstOrDefault()); } var watch = panel.GetWidget("WATCH_BUTTON"); - watch.IsDisabled = () => currentReplay == null || currentMap == null; + watch.IsDisabled = () => currentSummary == null || currentMap == null || currentSummary.Duration == 0; watch.OnClick = () => { - if (currentReplay != null) + if (currentSummary != null) { - Game.JoinReplay(CurrentReplay); + Game.JoinReplay(currentSummary.Filename); onStart(); } }; - panel.GetWidget("REPLAY_INFO").IsVisible = () => currentReplay != null; + panel.GetWidget("REPLAY_INFO").IsVisible = () => currentSummary != null; } - - string currentReplay = null; - Map currentMap = null; - string CurrentReplay + + ReplaySummary currentSummary; + Map currentMap; + void SelectReplay(string filename) { - get { return currentReplay; } - set + if (filename == null) + return; + + try { - currentReplay = value; - if (currentReplay != null) - { - try - { - var summary = new ReplaySummary(currentReplay); - currentMap = summary.Map(); + currentSummary = new ReplaySummary(filename); + currentMap = currentSummary.Map(); - panel.GetWidget("DURATION").GetText = - () => WidgetUtils.FormatTime(summary.Duration * 3 /* todo: 3:1 ratio isnt always true. */); - panel.GetWidget("MAP_PREVIEW").Map = () => currentMap; - panel.GetWidget("MAP_TITLE").GetText = - () => currentMap != null ? currentMap.Title : "(Unknown Map)"; + panel.GetWidget("DURATION").GetText = + () => WidgetUtils.FormatTime(currentSummary.Duration * 3 /* todo: 3:1 ratio isnt always true. */); + panel.GetWidget("MAP_PREVIEW").Map = () => currentMap; + panel.GetWidget("MAP_TITLE").GetText = + () => currentMap != null ? currentMap.Title : "(Unknown Map)"; - var players = summary.LobbyInfo.Slots.Count(s => summary.LobbyInfo.ClientInSlot(s) != null || s.Bot != null); - panel.GetWidget("PLAYERS").GetText = () => players.ToString(); - } - catch(Exception e) - { - Log.Write("debug", "Exception while parsing replay: {0}", e.ToString()); - currentReplay = null; - currentMap = null; - } - } + var players = currentSummary.LobbyInfo.Slots.Count(s => currentSummary.LobbyInfo.ClientInSlot(s) != null || s.Bot != null); + panel.GetWidget("PLAYERS").GetText = () => players.ToString(); + } + catch(Exception e) + { + Log.Write("debug", "Exception while parsing replay: {0}", e.ToString()); + currentSummary = null; + currentMap = null; } } @@ -102,8 +96,8 @@ namespace OpenRA.Mods.Cnc.Widgets var entry = template.Clone() as ContainerWidget; var f = Path.GetFileName(filename); entry.GetWidget("TITLE").GetText = () => f; - entry.GetBackground = () => (entry.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (CurrentReplay == filename) ? "button-pressed" : null); - entry.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; CurrentReplay = filename; return true; }; + entry.GetBackground = () => (entry.RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" : (currentSummary != null && currentSummary.Filename == filename) ? "button-pressed" : null); + entry.OnMouseDown = mi => { if (mi.Button != MouseButton.Left) return false; SelectReplay(filename); return true; }; entry.IsVisible = () => true; list.AddChild(entry); } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs index 7a5e44bdf8..543e5c8ca0 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs @@ -102,11 +102,13 @@ namespace OpenRA.Mods.RA.Widgets.Delegates /* a maze of twisty little hacks,... */ public class ReplaySummary { + public readonly string Filename; public readonly int Duration; public readonly Session LobbyInfo; public ReplaySummary(string filename) { + Filename = filename; var lastFrame = 0; var hasSeenGameStart = false; var lobbyInfo = null as Session;