Merge pull request #3647 from Mailaender/ip2geo

Show locations of client and servers using a local GeoIP database
This commit is contained in:
Paul Chote
2013-08-11 03:21:23 -07:00
26 changed files with 6293 additions and 15 deletions

View File

@@ -27,9 +27,16 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var latency = widget.Get<LabelWidget>("LATENCY");
var latencyFont = Game.Renderer.Fonts[latency.Font];
var latencyPrefix = widget.Get<LabelWidget>("LATENCY_PREFIX");
var latencyPrefixFont = Game.Renderer.Fonts[latencyPrefix.Font];
var ip = widget.Get<LabelWidget>("IP");
var ipFont = Game.Renderer.Fonts[ip.Font];
var location = widget.Get<LabelWidget>("LOCATION");
var locationFont = Game.Renderer.Fonts[location.Font];
var locationOffset = location.Bounds.Y;
var ipOffset = ip.Bounds.Y;
var latencyOffset = latency.Bounds.Y;
var tooltipHeight = widget.Bounds.Height;
@@ -39,27 +46,42 @@ namespace OpenRA.Mods.RA.Widgets.Logic
tooltipContainer.IsVisible = () => (orderManager.LobbyInfo.ClientWithIndex(clientIndex) != null);
tooltipContainer.BeforeRender = () =>
{
var width = Math.Max(adminFont.Measure(admin.GetText()).X, Math.Max(ipFont.Measure(ip.GetText()).X, latencyFont.Measure(latency.GetText()).X));
widget.Bounds.Width = width + 2*margin;
var latencyPrefixSize = latencyPrefix.Bounds.X + latencyPrefixFont.Measure(latencyPrefix.GetText()+" ").X;
var width = Math.Max(locationFont.Measure(location.GetText()).X, (Math.Max(adminFont.Measure(admin.GetText()).X,
Math.Max(ipFont.Measure(ip.GetText()).X, latencyPrefixSize + latencyFont.Measure(latency.GetText()).X))));
widget.Bounds.Width = width + 2 * margin;
latency.Bounds.Width = widget.Bounds.Width;
ip.Bounds.Width = widget.Bounds.Width;
admin.Bounds.Width = widget.Bounds.Width;
location.Bounds.Width = widget.Bounds.Width;
ip.Bounds.Y = ipOffset;
latency.Bounds.Y = latencyOffset;
location.Bounds.Y = locationOffset;
widget.Bounds.Height = tooltipHeight;
if (admin.IsVisible())
{
ip.Bounds.Y += admin.Bounds.Height;
latency.Bounds.Y += admin.Bounds.Height;
location.Bounds.Y += admin.Bounds.Height;
widget.Bounds.Height += admin.Bounds.Height;
}
latencyPrefix.Bounds.Y = latency.Bounds.Y;
latency.Bounds.X = latencyPrefixSize;
};
admin.IsVisible = () => orderManager.LobbyInfo.ClientWithIndex(clientIndex).IsAdmin;
latency.GetText = () => "Latency: {0}".F(LobbyUtils.LatencyDescription(orderManager.LobbyInfo.ClientWithIndex(clientIndex).Latency));
ip.GetText = () => LobbyUtils.DescriptiveIpAddress(orderManager.LobbyInfo.ClientWithIndex(clientIndex).IpAddress);
latency.GetText = () => LobbyUtils.LatencyDescription(orderManager.LobbyInfo.ClientWithIndex(clientIndex).Latency);
latency.GetColor = () => LobbyUtils.LatencyColor(orderManager.LobbyInfo.ClientWithIndex(clientIndex).Latency);
var ipAddress = orderManager.LobbyInfo.ClientWithIndex(clientIndex).IpAddress;
if ((ipAddress == null || ipAddress == "127.0.0.1") && UPnP.NatDevice != null)
ipAddress = UPnP.NatDevice.GetExternalIP().ToString();
var cachedDescriptiveIP = LobbyUtils.DescriptiveIpAddress(ipAddress);
ip.GetText = () => cachedDescriptiveIP;
var cachedCountryLookup = LobbyUtils.LookupCountry(ipAddress);
location.GetText = () => cachedCountryLookup;
}
}
}

View File

@@ -194,6 +194,13 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return ip;
}
public static string LookupCountry(string ip)
{
var ip2geo = new GeoIP.LookupService("GeoIP.dat", GeoIP.LookupService.GEOIP_MEMORY_CACHE);
var country = ip2geo.getCountry(ip);
return country.getName();
}
public static void SetupClientWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, bool visible)
{
parent.Get("ADMIN_INDICATOR").IsVisible = () => c.IsAdmin;

View File

@@ -238,6 +238,11 @@ namespace OpenRA.Mods.RA.Widgets.Logic
version.GetText = () => GenerateModsLabel(game);
version.IsVisible = () => !game.CompatibleVersion();
var location = item.Get<LabelWidget>("LOCATION");
var cachedServerLocation = LobbyUtils.LookupCountry(game.Address.Split(':')[0]);
location.GetText = () => cachedServerLocation;
location.IsVisible = () => game.CompatibleVersion();
if (!canJoin)
{
title.GetColor = () => Color.Gray;
@@ -246,6 +251,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
state.GetColor = () => Color.Gray;
ip.GetColor = () => Color.Gray;
version.GetColor = () => Color.Gray;
location.GetColor = () => Color.Gray;
}
if (!Filtered(game))