Skip to content

Instantly share code, notes, and snippets.

@Mailaender
Created April 22, 2013 22:21
Show Gist options
  • Select an option

  • Save Mailaender/5439074 to your computer and use it in GitHub Desktop.

Select an option

Save Mailaender/5439074 to your computer and use it in GitHub Desktop.
Only show compatible replays when browsing them.
#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;
using System.IO;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Network;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class ReplayBrowserLogic
{
Widget panel;
[ObjectCreator.UseCtor]
public ReplayBrowserLogic(Widget widget, Action onExit, Action onStart)
{
panel = widget;
panel.Get<ButtonWidget>("CANCEL_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
var rl = panel.Get<ScrollPanelWidget>("REPLAY_LIST");
var replayDir = Path.Combine(Platform.SupportDir, "Replays");
var template = panel.Get<ScrollItemWidget>("REPLAY_TEMPLATE");
rl.RemoveChildren();
if (Directory.Exists(replayDir))
{
var files = Directory.GetFiles(replayDir, "*.rep").Reverse();
foreach (var replayFile in files)
{
try
{
var newReplay = new Replay(replayFile);
if (CompatibleReplay(newReplay))
AddReplay(rl, replayFile, template);
}
catch (Exception e)
{
Log.Write("debug", "Failed to parse replay: {0}", e);
}
}
SelectReplay(files.FirstOrDefault());
}
var watch = panel.Get<ButtonWidget>("WATCH_BUTTON");
watch.IsDisabled = () => !CompatibleReplay(currentReplay);
watch.OnClick = () =>
{
if (currentReplay != null)
{
Game.JoinReplay(currentReplay.Filename);
Ui.CloseWindow();
onStart();
}
};
panel.Get("REPLAY_INFO").IsVisible = () => currentReplay != null;
}
Replay currentReplay;
void SelectReplay(string filename)
{
if (filename == null)
return;
try
{
currentReplay = new Replay(filename);
panel.Get<LabelWidget>("SERVER").GetText =
() => currentReplay.LobbyInfo.GlobalSettings.ServerName;
panel.Get<LabelWidget>("DURATION").GetText =
() => WidgetUtils.FormatTime(currentReplay.Duration * 3 /* TODO: 3:1 ratio isnt always true. */);
panel.Get<MapPreviewWidget>("MAP_PREVIEW").Map = () => currentReplay.Map();
panel.Get<LabelWidget>("MAP_TITLE").GetText =
() => currentReplay.Map() != null ? currentReplay.Map().Title : "(Unknown Map)";
var players = currentReplay.LobbyInfo.Slots
.Count(s => currentReplay.LobbyInfo.ClientInSlot(s.Key) != null);
panel.Get<LabelWidget>("PLAYERS").GetText = () => players.ToString();
panel.Get<LabelWidget>("MOD").GetText =
() => currentReplay.LobbyInfo.GlobalSettings.Mods[0];
panel.Get<LabelWidget>("VERSION").GetText =
() => currentReplay.LobbyInfo.GlobalSettings.Version;
}
catch (Exception e)
{
Log.Write("debug", "Exception while parsing replay: {0}", e);
currentReplay = null;
}
}
void AddReplay(ScrollPanelWidget list, string filename, ScrollItemWidget template)
{
var item = ScrollItemWidget.Setup(template,
() => currentReplay != null && currentReplay.Filename == filename,
() => SelectReplay(filename));
var f = Path.GetFileName(filename);
item.Get<LabelWidget>("TITLE").GetText = () => f;
list.AddChild(item);
}
bool CompatibleReplay(Replay checkReplay)
{
if (checkReplay.Map() == null)
return false;
if (checkReplay.Duration == 0)
return false;
if (checkReplay.LobbyInfo.GlobalSettings.Mods[0] != WidgetUtils.ActiveModTitle())
return false;
if (checkReplay.LobbyInfo.GlobalSettings.Version != WidgetUtils.ActiveModVersion())
return false;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment