Allow the game root directory to be moved away from the binaries.

This commit is contained in:
Paul Chote
2020-11-21 15:40:57 +00:00
committed by teinarss
parent dd0b08d54a
commit 6ad5b9ebc4
7 changed files with 78 additions and 8 deletions

View File

@@ -121,7 +121,7 @@ namespace OpenRA
mods[key] = mod; mods[key] = mod;
} }
internal void Register(Manifest mod, string launchPath, ModRegistration registration) internal void Register(Manifest mod, string launchPath, IEnumerable<string> launchArgs, ModRegistration registration)
{ {
if (mod.Metadata.Hidden) if (mod.Metadata.Hidden)
return; return;
@@ -133,7 +133,7 @@ namespace OpenRA
new MiniYamlNode("Version", mod.Metadata.Version), new MiniYamlNode("Version", mod.Metadata.Version),
new MiniYamlNode("Title", mod.Metadata.Title), new MiniYamlNode("Title", mod.Metadata.Title),
new MiniYamlNode("LaunchPath", launchPath), new MiniYamlNode("LaunchPath", launchPath),
new MiniYamlNode("LaunchArgs", "Game.Mod=" + mod.Id) new MiniYamlNode("LaunchArgs", new[] { "Game.Mod=" + mod.Id }.Concat(launchArgs).JoinWith(", "))
})); }));
using (var stream = mod.Package.GetStream("icon.png")) using (var stream = mod.Package.GetStream("icon.png"))

View File

@@ -276,6 +276,10 @@ namespace OpenRA
static void Initialize(Arguments args) static void Initialize(Arguments args)
{ {
var engineDirArg = args.GetValue("Engine.EngineDir", null);
if (!string.IsNullOrEmpty(engineDirArg))
Platform.OverrideEngineDir(engineDirArg);
var supportDirArg = args.GetValue("Engine.SupportDir", null); var supportDirArg = args.GetValue("Engine.SupportDir", null);
if (!string.IsNullOrEmpty(supportDirArg)) if (!string.IsNullOrEmpty(supportDirArg))
Platform.OverrideSupportDir(supportDirArg); Platform.OverrideSupportDir(supportDirArg);
@@ -324,7 +328,7 @@ namespace OpenRA
Settings.Game.Platform = p; Settings.Game.Platform = p;
try try
{ {
var rendererPath = Path.Combine(Platform.EngineDir, "OpenRA.Platforms." + p + ".dll"); var rendererPath = Path.Combine(Platform.BinDir, "OpenRA.Platforms." + p + ".dll");
var assembly = Assembly.LoadFile(rendererPath); var assembly = Assembly.LoadFile(rendererPath);
var platformType = assembly.GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t)); var platformType = assembly.GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));
@@ -367,14 +371,24 @@ namespace OpenRA
if (modID != null && Mods.TryGetValue(modID, out _)) if (modID != null && Mods.TryGetValue(modID, out _))
{ {
var launchPath = args.GetValue("Engine.LaunchPath", Assembly.GetEntryAssembly().Location); var launchPath = args.GetValue("Engine.LaunchPath", null);
var launchArgs = new List<string>();
// Sanitize input from platform-specific launchers // Sanitize input from platform-specific launchers
// Process.Start requires paths to not be quoted, even if they contain spaces // Process.Start requires paths to not be quoted, even if they contain spaces
if (launchPath.First() == '"' && launchPath.Last() == '"') if (launchPath != null && launchPath.First() == '"' && launchPath.Last() == '"')
launchPath = launchPath.Substring(1, launchPath.Length - 2); launchPath = launchPath.Substring(1, launchPath.Length - 2);
ExternalMods.Register(Mods[modID], launchPath, ModRegistration.User); if (launchPath == null)
{
// When launching the assembly directly we must propagate the Engine.EngineDir argument if defined
// Platform-specific launchers are expected to manage this internally.
launchPath = Assembly.GetEntryAssembly().Location;
if (!string.IsNullOrEmpty(engineDirArg))
launchArgs.Add("Engine.EngineDir=\"" + engineDirArg + "\"");
}
ExternalMods.Register(Mods[modID], launchPath, launchArgs, ModRegistration.User);
if (ExternalMods.TryGetValue(ExternalMod.MakeKey(Mods[modID]), out var activeMod)) if (ExternalMods.TryGetValue(ExternalMod.MakeKey(Mods[modID]), out var activeMod))
ExternalMods.ClearInvalidRegistrations(activeMod, ModRegistration.User); ExternalMods.ClearInvalidRegistrations(activeMod, ModRegistration.User);

View File

@@ -27,6 +27,9 @@ namespace OpenRA
static Lazy<PlatformType> currentPlatform = Exts.Lazy(GetCurrentPlatform); static Lazy<PlatformType> currentPlatform = Exts.Lazy(GetCurrentPlatform);
static bool engineDirAccessed;
static string engineDir;
static bool supportDirInitialized; static bool supportDirInitialized;
static string systemSupportPath; static string systemSupportPath;
static string legacyUserSupportPath; static string legacyUserSupportPath;
@@ -170,6 +173,44 @@ namespace OpenRA
} }
public static string EngineDir public static string EngineDir
{
get
{
// Engine directory defaults to the location of the binaries,
// unless OverrideGameDir is called during startup.
if (!engineDirAccessed)
engineDir = BinDir;
engineDirAccessed = true;
return engineDir;
}
}
/// <summary>
/// Specify a custom engine directory that already exists on the filesystem.
/// Cannot be called after Platform.EngineDir has been accessed.
/// </summary>
public static void OverrideEngineDir(string path)
{
if (engineDirAccessed)
throw new InvalidOperationException("Attempted to override engine directory after it has already been accessed.");
// Note: Relative paths are interpreted as being relative to BinDir, not the current working dir.
if (!Path.IsPathRooted(path))
path = Path.Combine(BinDir, path);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException(path);
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) &&
!path.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal))
path += Path.DirectorySeparatorChar;
engineDirAccessed = true;
engineDir = path;
}
public static string BinDir
{ {
get get
{ {
@@ -194,12 +235,18 @@ namespace OpenRA
if (path == "^EngineDir") if (path == "^EngineDir")
return EngineDir; return EngineDir;
if (path == "^BinDir")
return BinDir;
if (path.StartsWith("^SupportDir|", StringComparison.Ordinal)) if (path.StartsWith("^SupportDir|", StringComparison.Ordinal))
path = SupportDir + path.Substring(12); path = SupportDir + path.Substring(12);
if (path.StartsWith("^EngineDir|", StringComparison.Ordinal)) if (path.StartsWith("^EngineDir|", StringComparison.Ordinal))
path = EngineDir + path.Substring(11); path = EngineDir + path.Substring(11);
if (path.StartsWith("^BinDir|", StringComparison.Ordinal))
path = BinDir + path.Substring(8);
return path; return path;
} }
} }

View File

@@ -31,7 +31,7 @@ namespace OpenRA.UtilityCommands
if (args[2] == "user" || args[2] == "both") if (args[2] == "user" || args[2] == "both")
type |= ModRegistration.User; type |= ModRegistration.User;
new ExternalMods().Register(utility.ModData.Manifest, args[1], type); new ExternalMods().Register(utility.ModData.Manifest, args[1], Enumerable.Empty<string>(), type);
} }
} }
} }

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
.ToArray(); .ToArray();
// Load the renderer assembly so we can check its dependencies // Load the renderer assembly so we can check its dependencies
Assembly.LoadFile(Path.Combine(Platform.EngineDir, "OpenRA.Platforms.Default.dll")); Assembly.LoadFile(Path.Combine(Platform.BinDir, "OpenRA.Platforms.Default.dll"));
var missing = new List<string>(); var missing = new List<string>();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) foreach (var a in AppDomain.CurrentDomain.GetAssemblies())

View File

@@ -23,6 +23,11 @@ namespace OpenRA.Server
static void Main(string[] args) static void Main(string[] args)
{ {
var arguments = new Arguments(args); var arguments = new Arguments(args);
var engineDirArg = arguments.GetValue("Engine.EngineDir", null);
if (!string.IsNullOrEmpty(engineDirArg))
Platform.OverrideEngineDir(engineDirArg);
var supportDirArg = arguments.GetValue("Engine.SupportDir", null); var supportDirArg = arguments.GetValue("Engine.SupportDir", null);
if (!string.IsNullOrEmpty(supportDirArg)) if (!string.IsNullOrEmpty(supportDirArg))
Platform.OverrideSupportDir(supportDirArg); Platform.OverrideSupportDir(supportDirArg);

View File

@@ -40,6 +40,10 @@ namespace OpenRA
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var engineDir = Environment.GetEnvironmentVariable("ENGINE_DIR");
if (!string.IsNullOrEmpty(engineDir))
Platform.OverrideEngineDir(engineDir);
Log.AddChannel("perf", null); Log.AddChannel("perf", null);
Log.AddChannel("debug", null); Log.AddChannel("debug", null);