From b1b94da6ef3cc76aa03f157369484216e85b15ed Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 29 Jan 2015 02:08:12 +0000 Subject: [PATCH] Lua Sessions - make scripts relative to the lua session instead of relative to the .exe, note: this wrecks existing luases files --- BizHawk.Client.Common/PathManager.cs | 10 ++++++++++ BizHawk.Client.Common/lua/LuaFileList.cs | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/BizHawk.Client.Common/PathManager.cs b/BizHawk.Client.Common/PathManager.cs index 07495bf9a1..ec4293a754 100644 --- a/BizHawk.Client.Common/PathManager.cs +++ b/BizHawk.Client.Common/PathManager.cs @@ -359,6 +359,16 @@ namespace BizHawk.Client.Common return absolutePath; } + public static string MakeRelativeTo(string absolutePath, string basePath) + { + if (IsSubfolder(basePath, absolutePath)) + { + return absolutePath.Replace(basePath, "."); + } + + return absolutePath; + } + //http://stackoverflow.com/questions/3525775/how-to-check-if-directory-1-is-a-subdirectory-of-dir2-and-vice-versa public static bool IsSubfolder(string parentPath, string childPath) { diff --git a/BizHawk.Client.Common/lua/LuaFileList.cs b/BizHawk.Client.Common/lua/LuaFileList.cs index e7e26fd447..12bc4f59d0 100644 --- a/BizHawk.Client.Common/lua/LuaFileList.cs +++ b/BizHawk.Client.Common/lua/LuaFileList.cs @@ -96,7 +96,14 @@ namespace BizHawk.Client.Common } else { - Add(new LuaFile(line.Substring(2, line.Length - 2)) + var scriptPath = line.Substring(2, line.Length - 2); + if (!Path.IsPathRooted(scriptPath)) + { + var directory = Path.GetDirectoryName(path); + scriptPath = Path.Combine(directory, scriptPath); + } + + Add(new LuaFile(scriptPath) { Enabled = !Global.Config.DisableLuaScriptsOnLoad && line.Substring(0, 1) == "1", @@ -138,7 +145,7 @@ namespace BizHawk.Client.Common sb .Append(file.Enabled ? "1" : "0") .Append(' ') - .Append(PathManager.TryMakeRelative(file.Path)) + .Append(PathManager.MakeRelativeTo(file.Path, Path.GetDirectoryName(path))) .AppendLine(); }