From 20458fc552923e4fa6654534ff83c23532d6c01c Mon Sep 17 00:00:00 2001 From: Matthew Bowra-Dean Date: Wed, 18 May 2011 20:55:25 +1200 Subject: [PATCH] Move Platform.cs to OpenRA.FileFormats, fix #765. --- OpenRA.FileFormats/Filesystem/FileSystem.cs | 3 +- OpenRA.FileFormats/OpenRA.FileFormats.csproj | 3 +- OpenRA.FileFormats/Platform.cs | 84 +++++++++++++++++++ OpenRA.Game/Game.cs | 33 +------- OpenRA.Game/ModData.cs | 2 +- OpenRA.Game/OpenRA.Game.csproj | 1 - OpenRA.Game/Platform.cs | 51 ----------- OpenRA.Game/Utilities.cs | 2 +- .../Widgets/Delegates/MapChooserDelegate.cs | 2 +- .../Delegates/ReplayBrowserDelegate.cs | 2 +- OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs | 2 +- 11 files changed, 96 insertions(+), 89 deletions(-) create mode 100644 OpenRA.FileFormats/Platform.cs delete mode 100644 OpenRA.Game/Platform.cs diff --git a/OpenRA.FileFormats/Filesystem/FileSystem.cs b/OpenRA.FileFormats/Filesystem/FileSystem.cs index d08a807201..28f61a4321 100644 --- a/OpenRA.FileFormats/Filesystem/FileSystem.cs +++ b/OpenRA.FileFormats/Filesystem/FileSystem.cs @@ -19,7 +19,6 @@ namespace OpenRA.FileFormats public static class FileSystem { static List mountedFolders = new List(); - public static string SupportDir = "."; // Default to "current dir" if we aren't told otherwise static Cache> allFiles = new Cache>( _ => new List() ); @@ -77,7 +76,7 @@ namespace OpenRA.FileFormats // paths starting with ^ are relative to the support dir if (name.StartsWith("^")) - name = FileSystem.SupportDir+name.Substring(1); + name = Platform.SupportDir+name.Substring(1); var a = (Action)(() => FileSystem.MountInner(OpenPackage(name))); diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index e5eb2d25b6..73d81a40b9 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -1,4 +1,4 @@ - + Debug @@ -75,6 +75,7 @@ + diff --git a/OpenRA.FileFormats/Platform.cs b/OpenRA.FileFormats/Platform.cs new file mode 100644 index 0000000000..10b3420317 --- /dev/null +++ b/OpenRA.FileFormats/Platform.cs @@ -0,0 +1,84 @@ +#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.Diagnostics; +using System.IO; +using OpenRA.FileFormats; + +namespace OpenRA +{ + public enum PlatformType + { + Unknown, + Windows, + OSX, + Linux + } + + public static class Platform + { + public static PlatformType CurrentPlatform + { + get + { + return currentPlatform.Value; + } + } + + static Lazy currentPlatform = new Lazy(GetCurrentPlatform); + + static PlatformType GetCurrentPlatform() + { + if (Environment.OSVersion.Platform == PlatformID.Win32NT) return PlatformType.Windows; + + try + { + var psi = new ProcessStartInfo("uname", "-s"); + psi.UseShellExecute = false; + psi.RedirectStandardOutput = true; + var p = Process.Start(psi); + var kernelName = p.StandardOutput.ReadToEnd(); + if (kernelName.Contains("Linux") || kernelName.Contains("BSD")) + return PlatformType.Linux; + if (kernelName.Contains("Darwin")) + return PlatformType.OSX; + } + catch { } + + return PlatformType.Unknown; + } + + public static string SupportDir + { + get + { + string dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal); + switch (CurrentPlatform) + { + case PlatformType.OSX: + dir += "/Library/Application Support/OpenRA"; + break; + case PlatformType.Linux: + dir += "/.openra"; + break; + default: + dir += Path.DirectorySeparatorChar + "OpenRA"; + break; + } + + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + return dir + Path.DirectorySeparatorChar; + } + } + } +} diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 05a39a181d..977d237d8e 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -45,7 +45,7 @@ namespace OpenRA public static Renderer Renderer; public static bool HasInputFocus = false; - + public static void MoveViewport(float2 loc) { viewport.Center(loc); @@ -54,7 +54,7 @@ namespace OpenRA public static void JoinServer(string host, int port) { var replayFilename = ChooseReplayFilename(); - string path = Path.Combine( Game.SupportDir, "Replays" ); + string path = Path.Combine( Platform.SupportDir, "Replays" ); if( !Directory.Exists( path ) ) Directory.CreateDirectory( path ); var replayFile = File.Create( Path.Combine( path, replayFilename ) ); @@ -223,18 +223,12 @@ namespace OpenRA AppDomain.CurrentDomain.AssemblyResolve += FileSystem.ResolveAssembly; - var defaultSupport = Environment.GetFolderPath(Environment.SpecialFolder.Personal) - + Path.DirectorySeparatorChar + "OpenRA"; - - SupportDir = args.GetValue("SupportDir", defaultSupport); - FileSystem.SupportDir = SupportDir; - Utilities = new Utilities(args.GetValue("UtilityPath", "OpenRA.Utility.exe")); - Settings = new Settings(SupportDir + "settings.yaml", args); + Settings = new Settings(Platform.SupportDir + "settings.yaml", args); Settings.Save(); - Log.LogPath = SupportDir + "Logs" + Path.DirectorySeparatorChar; + Log.LogPath = Platform.SupportDir + "Logs" + Path.DirectorySeparatorChar; Log.AddChannel("perf", "perf.log"); Log.AddChannel("debug", "debug.log"); Log.AddChannel("sync", "syncreport.log"); @@ -340,25 +334,6 @@ namespace OpenRA Widget.OpenWindow("MAINMENU_BG"); } - static string baseSupportDir = null; - public static string SupportDir - { - set - { - var dir = value; - - // Expand paths relative to the personal directory - if (dir.ElementAt(0) == '~') - dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + dir.Substring(1); - - if (!Directory.Exists(dir)) - Directory.CreateDirectory(dir); - - baseSupportDir = dir + Path.DirectorySeparatorChar; - } - get { return baseSupportDir; } - } - public static T CreateObject( string name ) { return modData.ObjectCreator.CreateObject( name ); diff --git a/OpenRA.Game/ModData.cs b/OpenRA.Game/ModData.cs index 752447b4ae..95a62dd3e1 100755 --- a/OpenRA.Game/ModData.cs +++ b/OpenRA.Game/ModData.cs @@ -102,7 +102,7 @@ namespace OpenRA Dictionary FindMaps(string[] mods) { var paths = mods.SelectMany(p => FindMapsIn("mods{0}{1}{0}maps{0}".F(Path.DirectorySeparatorChar, p))) - .Concat(mods.SelectMany(p => FindMapsIn("{1}maps{0}{2}{0}".F(Path.DirectorySeparatorChar, Game.SupportDir, p)))); + .Concat(mods.SelectMany(p => FindMapsIn("{1}maps{0}{2}{0}".F(Path.DirectorySeparatorChar, Platform.SupportDir, p)))); Dictionary ret = new Dictionary(); foreach (var path in paths) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 630fedc69d..6f79fa88d0 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -199,7 +199,6 @@ - diff --git a/OpenRA.Game/Platform.cs b/OpenRA.Game/Platform.cs deleted file mode 100644 index 8718d6bff8..0000000000 --- a/OpenRA.Game/Platform.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Diagnostics; -using OpenRA.FileFormats; - -namespace OpenRA -{ - enum PlatformType - { - Unknown, - Windows, - OSX, - Linux - } - - static class Platform - { - public static PlatformType CurrentPlatform - { - get - { - return currentPlatform.Value; - } - } - - static Lazy currentPlatform = new Lazy(GetCurrentPlatform); - - static PlatformType GetCurrentPlatform() - { - if (Environment.OSVersion.Platform == PlatformID.Win32NT) return PlatformType.Windows; - - try - { - var psi = new ProcessStartInfo("uname", "-s"); - psi.UseShellExecute = false; - psi.RedirectStandardOutput = true; - var p = Process.Start(psi); - var kernelName = p.StandardOutput.ReadToEnd(); - if (kernelName.Contains("Linux") || kernelName.Contains("BSD")) - return PlatformType.Linux; - if (kernelName.Contains("Darwin")) - return PlatformType.OSX; - } - catch { } - - return PlatformType.Unknown; - } - } -} diff --git a/OpenRA.Game/Utilities.cs b/OpenRA.Game/Utilities.cs index 505de31cd7..e5ae49f174 100644 --- a/OpenRA.Game/Utilities.cs +++ b/OpenRA.Game/Utilities.cs @@ -34,7 +34,7 @@ namespace OpenRA { Process p = new Process(); p.StartInfo.FileName = Utility; - p.StartInfo.Arguments = "{0} --SupportDir \"{1}\"".F(args, Game.SupportDir); + p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.EnableRaisingEvents = true; diff --git a/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs index 801f385abd..709aab2a12 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs @@ -97,7 +97,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates void InstallMapInner(string path) { var toPath = "{0}{1}maps{1}{2}{1}{3}" - .F(Game.SupportDir,Path.DirectorySeparatorChar, + .F(Platform.SupportDir,Path.DirectorySeparatorChar, Game.modData.Manifest.Mods[0], Path.GetFileName(path)); diff --git a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs index fe294c9717..56b197d226 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates /* find some replays? */ var rl = widget.GetWidget("REPLAY_LIST"); - var replayDir = Path.Combine(Game.SupportDir, "Replays"); + var replayDir = Path.Combine(Platform.SupportDir, "Replays"); var template = widget.GetWidget("REPLAY_TEMPLATE"); CurrentReplay = null; diff --git a/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs b/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs index 4f0b829d35..205ef02b43 100755 --- a/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs +++ b/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs @@ -23,7 +23,7 @@ namespace OpenRA.Mods.RA.Widgets public string PackagePath = ""; public string InstallMode = ""; - public string ResolvedPackagePath { get { return PackagePath.Replace("^", Game.SupportDir); } } + public string ResolvedPackagePath { get { return PackagePath.Replace("^", Platform.SupportDir); } } public override void DrawInner() {} }