diff --git a/Source/KSPe/FatalErrors.cs b/Source/KSPe/FatalErrors.cs index afef6f5c..1d5c2f86 100644 --- a/Source/KSPe/FatalErrors.cs +++ b/Source/KSPe/FatalErrors.cs @@ -145,4 +145,48 @@ private void WindowFunc(int windowID) } } } + + internal static class NoGameDataFound + { + private static readonly string MSG = @"KSPe could not find a GameData folder from where you fired up your KSP game."; + + private static bool shown = false; + internal static void Show() + { + if (shown) return; + + Startup.QuitOnDestroy = shown = true; + if (null != GameObject.Find("KSPe.FatalError.NoGameDataFound")) return; // Already being shown. + + GameObject go = new GameObject("KSPe.FatalError.NoGameDataFound"); + FatalErrorMsgBox dlg = go.AddComponent(); + + dlg.Show( + MSG, + () => { Application.OpenURL("https://github.com/net-lisias-ksp/KSPAPIExtensions/issues/11"); Application.Quit(); } + ); + Debug.Log("[KSPe] Fatal Error NoGameDataFound was shown. Please visit https://github.com/net-lisias-ksp/KSPAPIExtensions/issues/11"); + } + } + + MSG, + () => { Application.OpenURL("https://github.com/net-lisias-ksp/KSPAPIExtensions/issues/11"); Application.Quit(); } + private static bool shown = false; + internal static void Show(string pwd, string origin) + { + if (shown) return; + + Startup.QuitOnDestroy = shown = true; + if (null != GameObject.Find("KSPe.FatalError.PwdIsNotOrigin")) return; // Already being shown. + + GameObject go = new GameObject("KSPe.FatalError.PwdIsNotOrigin"); + FatalErrorMsgBox dlg = go.AddComponent(); + + dlg.Show( + string.Format(MSG, pwd, origin), + () => { Application.OpenURL("https://github.com/net-lisias-ksp/KSPAPIExtensions/issues/12"); Application.Quit(); } + ); + Debug.Log("[KSPe] Fatal Error PwdIsNotOrigin was shown. Please visit https://github.com/net-lisias-ksp/KSPAPIExtensions/issues/12"); + } + } } } diff --git a/Source/KSPe/IO/Path.cs b/Source/KSPe/IO/Path.cs index a9520c51..5c5340d7 100644 --- a/Source/KSPe/IO/Path.cs +++ b/Source/KSPe/IO/Path.cs @@ -124,13 +124,44 @@ internal static string Origin() UnityEngine.Debug.LogFormat("[KSPe.IO.Path] Calculating Origin for {0}", typeof(KSPUtil).Assembly.Location); #endif - string path = typeof(KSPUtil).Assembly.Location - .Replace("KSP_x64_Data",".") // Win64 versions - .Replace("KSP_Data",".") // Linux and Win32 versions - .Replace("KSP.app/Contents/Resources/Data", ".") // Mac - .Replace("Managed", ".") // Everybody + // Look for the GameData folder. This is our root. + + // this one doesn't works, Mono reparse the damned thing and we lose the original pathname the user used to launch the thing! + //string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; + + string assemblypath = typeof(KSPUtil).Assembly.Location; + assemblypath = SIO.Path.GetDirectoryName(assemblypath); + string path = null; + + { // Let's try the quick & dirty way first! + path = SIO.Path.GetDirectoryName(assemblypath) + .Replace("KSP_x64_Data", ".") // Win64 versions + .Replace("KSP_Data", ".") // Linux and Win32 versions + .Replace("KSP.app/Contents/Resources/Data", ".") // Mac + .Replace("Managed", ".") // Everybody ; - path = GetDirectoryName(GetAbsolutePath(path)); + path = GetDirectoryName(GetAbsolutePath(path)); + if (!SIO.Directory.Exists(SIO.Path.Combine(path, "GameData"))) path = null; + } + + if (null == path) + { // Oukey, not a standard rig, perhaps a debug/development one? + #if DEBUG + UnityEngine.Debug.LogFormat("[KSPe.IO.Path] GameData not found the easy way. Trying the harder path. #TumDumTsss"); + #endif + path = SIO.Path.GetDirectoryName(assemblypath); + while (path.Length > 4) // The smaller relevant path for us is C:\ on Windows. Less than it, we are toasted, no GameData found! + { + #if DEBUG + UnityEngine.Debug.LogFormat("[KSPe.IO.Path] Trying {0}", path); + #endif + if (SIO.Directory.Exists(SIO.Path.Combine(path, "GameData"))) break; + path = SIO.Path.GetDirectoryName(path); + } + path = (path.Length < 4) ? null : GetAbsolutePath(path); + } + + if (null == path) KSPe.FatalErrors.NoGameDataFound.Show(); #if DEBUG UnityEngine.Debug.LogFormat("[KSPe.IO.Path] Normalized path {0}", path); diff --git a/Source/Tests/Mockups/KSPe/FatalErrors.cs b/Source/Tests/Mockups/KSPe/FatalErrors.cs index 185459de..b29c5115 100644 --- a/Source/Tests/Mockups/KSPe/FatalErrors.cs +++ b/Source/Tests/Mockups/KSPe/FatalErrors.cs @@ -23,4 +23,11 @@ You should have received a copy of the GNU General Public License 2.0 using System; namespace KSPe { namespace FatalErrors { + internal static class NoGameDataFound + { + internal static void Show() + { + throw new NotImplementedException(); + } + } } }