Skip to content

Commit

Permalink
Adding a slow search for the GameData for development/debuging time…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Apr 7, 2021
1 parent f02fb3b commit 9e4ca34
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
44 changes: 44 additions & 0 deletions Source/KSPe/FatalErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FatalErrorMsgBox>();

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<FatalErrorMsgBox>();

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");
}
}
} }
43 changes: 37 additions & 6 deletions Source/KSPe/IO/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Source/Tests/Mockups/KSPe/FatalErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
} }

0 comments on commit 9e4ca34

Please sign in to comment.