diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 28b7d37741..d570f0e5ed 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -458,5 +458,29 @@ namespace OpenRA Game.JoinServer(host, port); } + + public static bool DownloadMap(string mapHash) + { + try + { + var mod = Game.CurrentMods.FirstOrDefault().Value.Id; + var dirPath = "{1}maps{0}{2}".F(Path.DirectorySeparatorChar, Platform.SupportDir, mod); + if(!Directory.Exists(dirPath)) + Directory.CreateDirectory(dirPath); + var mapPath = "{1}{0}{2}".F(Path.DirectorySeparatorChar, dirPath, mapHash+".oramap"); + Console.Write("Trying to download map to {0} ... ".F(mapPath)); + WebClient webClient = new WebClient(); + webClient.DownloadFile(Game.Settings.Game.MapRepository + mapHash, mapPath); + Game.modData.AvailableMaps.Add(mapHash, new Map(mapPath)); + Console.WriteLine("done"); + return true; + } + catch (WebException e) + { + Log.Write("debug", "Could not download map '{0}'", mapHash); + Log.Write("debug", e.ToString()); + return false; + } + } } } diff --git a/OpenRA.Game/GameRules/Settings.cs b/OpenRA.Game/GameRules/Settings.cs index 8be6cfa6a3..64ca77cb25 100644 --- a/OpenRA.Game/GameRules/Settings.cs +++ b/OpenRA.Game/GameRules/Settings.cs @@ -123,6 +123,8 @@ namespace OpenRA.GameRules public int Timestep = 40; public string ConnectTo = ""; + public bool AllowDownloading = true; + public string MapRepository = "http://content.open-ra.org/map/"; } public class KeySettings diff --git a/OpenRA.Game/Network/GameServer.cs b/OpenRA.Game/Network/GameServer.cs index 6bdd6bdb96..fed810d5fb 100644 --- a/OpenRA.Game/Network/GameServer.cs +++ b/OpenRA.Game/Network/GameServer.cs @@ -54,7 +54,8 @@ namespace OpenRA.Network // Don't have the map locally if (!Game.modData.AvailableMaps.ContainsKey(Map)) - return false; + if (!Game.Settings.Game.AllowDownloading) + return false; return CompatibleVersion(); } diff --git a/OpenRA.Game/Network/UnitOrders.cs b/OpenRA.Game/Network/UnitOrders.cs index a9c340e869..5d34febd7b 100755 --- a/OpenRA.Game/Network/UnitOrders.cs +++ b/OpenRA.Game/Network/UnitOrders.cs @@ -119,7 +119,12 @@ namespace OpenRA.Network throw new InvalidOperationException("Server's mod ({0}) and yours ({1}) don't match".F(localMods.FirstOrDefault().ToString().Split('@')[0], request.Mods.FirstOrDefault().ToString().Split('@')[0])); // Check that the map exists on the client if (!Game.modData.AvailableMaps.ContainsKey(request.Map)) - throw new InvalidOperationException("Missing map {0}".F(request.Map)); + { + if (Game.Settings.Game.AllowDownloading) + Game.DownloadMap(request.Map); + else + throw new InvalidOperationException("Missing map {0}".F(request.Map)); + } var info = new Session.Client() { diff --git a/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs index 8eadfec3f6..bc78db6714 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs @@ -328,8 +328,18 @@ namespace OpenRA.Mods.RA.Widgets.Logic void UpdateCurrentMap() { - if (MapUid == orderManager.LobbyInfo.GlobalSettings.Map) return; + if (MapUid == orderManager.LobbyInfo.GlobalSettings.Map) + return; MapUid = orderManager.LobbyInfo.GlobalSettings.Map; + + if (!Game.modData.AvailableMaps.ContainsKey (MapUid)) + if (Game.Settings.Game.AllowDownloading) + { + Game.DownloadMap (MapUid); + Game.Debug("A new map has been downloaded..."); + } + else + throw new InvalidOperationException("Server's new map doesn't exist on your system and Downloading turned off"); Map = new Map(Game.modData.AvailableMaps[MapUid].Path); var title = Ui.Root.Get("TITLE");