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