async server browser
This commit is contained in:
@@ -40,6 +40,13 @@ namespace OpenRA.FileFormats
|
||||
LoadField( self, x.Key, x.Value.Value );
|
||||
}
|
||||
|
||||
public static T Load<T>(MiniYaml y) where T : new()
|
||||
{
|
||||
var t = new T();
|
||||
Load(t, y);
|
||||
return t;
|
||||
}
|
||||
|
||||
public static void LoadFields( object self, Dictionary<string,MiniYaml> my, IEnumerable<string> fields )
|
||||
{
|
||||
foreach (var field in fields)
|
||||
|
||||
@@ -34,6 +34,7 @@ using OpenRA.Traits;
|
||||
|
||||
using Timer = OpenRA.Support.Timer;
|
||||
using XRandom = OpenRA.Thirdparty.Random;
|
||||
using OpenRA.Server;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
@@ -258,6 +259,8 @@ namespace OpenRA
|
||||
PerfHistory.items["batches"].Tick();
|
||||
PerfHistory.items["text"].Tick();
|
||||
PerfHistory.items["cursor"].Tick();
|
||||
|
||||
MasterServerQuery.Tick();
|
||||
}
|
||||
|
||||
public static void SyncLobbyInfo(string data)
|
||||
|
||||
@@ -4,20 +4,45 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using OpenRA.FileFormats;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenRA.Server
|
||||
{
|
||||
static class MasterServerQuery
|
||||
{
|
||||
public static IEnumerable<GameServer> GetGameList(string masterServerUrl)
|
||||
public static event Action<GameServer[]> OnComplete = _ => { };
|
||||
|
||||
static GameServer[] Games = { };
|
||||
static AutoResetEvent ev = new AutoResetEvent(false);
|
||||
|
||||
public static void Refresh(string masterServerUrl)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var wc = new WebClient();
|
||||
var data = wc.DownloadData(masterServerUrl + "list.php");
|
||||
var data = wc.DownloadData(new Uri(masterServerUrl + "list.php"));
|
||||
var str = Encoding.UTF8.GetString(data);
|
||||
|
||||
var yaml = MiniYaml.FromString(str);
|
||||
return yaml.Select(a => { var gs = new GameServer(); FieldLoader.Load(gs, a.Value); return gs; })
|
||||
.Where(gs => gs.Address != null);
|
||||
|
||||
Games = yaml.Select(a => FieldLoader.Load<GameServer>(a.Value))
|
||||
.Where(gs => gs.Address != null).ToArray();
|
||||
}
|
||||
catch
|
||||
{
|
||||
Games = null;
|
||||
}
|
||||
|
||||
ev.Set();
|
||||
}).Start();
|
||||
}
|
||||
|
||||
public static void Tick()
|
||||
{
|
||||
if (ev.WaitOne(TimeSpan.FromMilliseconds(0)))
|
||||
OnComplete(Games);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,6 @@ namespace OpenRA.Traits
|
||||
content[x, y].image = ChooseContent(content[x, y].type);
|
||||
}
|
||||
|
||||
|
||||
for (int x = map.XOffset; x < map.XOffset + map.Width; x++)
|
||||
for (int y = map.YOffset; y < map.YOffset + map.Height; y++)
|
||||
if (content[x, y].type != null)
|
||||
|
||||
@@ -22,41 +22,57 @@ using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Server;
|
||||
using OpenRA.FileFormats;
|
||||
using System;
|
||||
|
||||
namespace OpenRA.Widgets.Delegates
|
||||
{
|
||||
public class ServerBrowserDelegate : IWidgetDelegate
|
||||
{
|
||||
static GameServer[] GameList;
|
||||
static List<Widget> GameButtons = new List<Widget>();
|
||||
|
||||
public ServerBrowserDelegate()
|
||||
{
|
||||
var r = Chrome.rootWidget;
|
||||
r.GetWidget("MAINMENU_BUTTON_JOIN").OnMouseUp =
|
||||
mi => {
|
||||
var bg = r.OpenWindow("JOINSERVER_BG");
|
||||
|
||||
MasterServerQuery.OnComplete += games =>
|
||||
{
|
||||
var bg = r.GetWidget("JOINSERVER_BG");
|
||||
|
||||
if (games == null)
|
||||
{
|
||||
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
|
||||
r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Failed to contact master server.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (games.Length == 0)
|
||||
{
|
||||
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
|
||||
r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "No games found.";
|
||||
return;
|
||||
}
|
||||
|
||||
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = false;
|
||||
|
||||
int height = 50;
|
||||
int width = 300;
|
||||
int i = 0;
|
||||
GameList = MasterServerQuery.GetGameList(Game.Settings.MasterServer).ToArray();
|
||||
|
||||
bg.Children.RemoveAll(a => GameButtons.Contains(a));
|
||||
GameButtons.Clear();
|
||||
|
||||
foreach (var game in GameList)
|
||||
foreach (var game in games)
|
||||
{
|
||||
var g = game;
|
||||
var b = new ButtonWidget
|
||||
{
|
||||
Bounds = new Rectangle(bg.Bounds.X + 20, bg.Bounds.Y + height, width, 25),
|
||||
Id = "JOIN_GAME_{0}".F( i ),
|
||||
Text = "{0} ({1})".F( game.Name, game.Address ),
|
||||
Id = "JOIN_GAME_{0}".F(i),
|
||||
Text = "{0} ({1})".F(game.Name, game.Address),
|
||||
Delegate = "ServerBrowserDelegate",
|
||||
|
||||
OnMouseUp = nmi =>
|
||||
{
|
||||
r.GetWidget("JOINSERVER_BG").Visible = false;
|
||||
Game.JoinServer(GameList[i].Address.Split(':')[0], int.Parse(GameList[i].Address.Split(':')[1]));
|
||||
Game.JoinServer(g.Address.Split(':')[0], int.Parse(g.Address.Split(':')[1]));
|
||||
return true;
|
||||
},
|
||||
};
|
||||
@@ -67,16 +83,40 @@ namespace OpenRA.Widgets.Delegates
|
||||
height += 35;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
r.GetWidget("MAINMENU_BUTTON_JOIN").OnMouseUp = mi =>
|
||||
{
|
||||
var bg = r.OpenWindow("JOINSERVER_BG");
|
||||
|
||||
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
|
||||
r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Fetching game list...";
|
||||
|
||||
bg.Children.RemoveAll(a => GameButtons.Contains(a));
|
||||
GameButtons.Clear();
|
||||
|
||||
MasterServerQuery.Refresh(Game.Settings.MasterServer);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
r.GetWidget("JOINSERVER_BUTTON_DIRECTCONNECT").OnMouseUp = mi => {
|
||||
r.CloseWindow();
|
||||
Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort);
|
||||
r.GetWidget("JOINSERVER_BUTTON_REFRESH").OnMouseUp = mi =>
|
||||
{
|
||||
var bg = r.GetWidget("JOINSERVER_BG");
|
||||
|
||||
r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
|
||||
r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Fetching game list...";
|
||||
|
||||
bg.Children.RemoveAll(a => GameButtons.Contains(a));
|
||||
GameButtons.Clear();
|
||||
|
||||
MasterServerQuery.Refresh(Game.Settings.MasterServer);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
r.GetWidget("JOINSERVER_BUTTON_CANCEL").OnMouseUp = mi => {
|
||||
r.GetWidget("JOINSERVER_BUTTON_CANCEL").OnMouseUp = mi =>
|
||||
{
|
||||
r.CloseWindow();
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -43,6 +43,7 @@ namespace OpenRA.Widgets
|
||||
// Calculated internally
|
||||
public Rectangle Bounds;
|
||||
public Widget Parent = null;
|
||||
|
||||
static List<string> Delegates = new List<string>();
|
||||
public static Stack<string> WindowList = new Stack<string>();
|
||||
|
||||
|
||||
@@ -160,13 +160,21 @@ Container:
|
||||
Height:25
|
||||
Text:Quick'n'dirty Server Browser
|
||||
Align:Center
|
||||
Button@JOINSERVER_BUTTON_DIRECTCONNECT:
|
||||
Id:JOINSERVER_BUTTON_DIRECTCONNECT
|
||||
Label@JOINSERVER_PROGRESS_TITLE:
|
||||
Id:JOINSERVER_PROGRESS_TITLE
|
||||
X:0
|
||||
Y:PARENT_BOTTOM / 2 - HEIGHT
|
||||
Width:450
|
||||
Height:25
|
||||
Text:Fetching games...
|
||||
Align:Center
|
||||
Button@JOINSERVER_BUTTON_REFRESH:
|
||||
Id:JOINSERVER_BUTTON_REFRESH
|
||||
X:PARENT_RIGHT - 360
|
||||
Y:PARENT_BOTTOM - 45
|
||||
Width:160
|
||||
Height:25
|
||||
Text:Direct Connect
|
||||
Text:Refresh
|
||||
Button@JOINSERVER_BUTTON_CANCEL:
|
||||
Id:JOINSERVER_BUTTON_CANCEL
|
||||
X:PARENT_RIGHT - 180
|
||||
|
||||
Reference in New Issue
Block a user