Skip to content

Commit

Permalink
Detects when calling KSPUtil.ApplicationRootPath is unreliable and …
Browse files Browse the repository at this point in the history
…works around it.

Fixes #58
  • Loading branch information
Lisias committed Sep 17, 2023
1 parent a308ddb commit e1c71dd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/InstallChecker/KSPe.InstallChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<Compile Include="ModuleInitializer.cs" />
<Compile Include="InstallChecker.cs" />
<Compile Include="Properties\MyVersion.cs" />
<Compile Include="..\KSPe\Util\KSPUtil.cs">
<Link>Util\KSPUtil.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="Common\" />
Expand Down
2 changes: 1 addition & 1 deletion Source/KSPe/IO/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static string AppRoot()
{
if (null != app_root) return app_root;
Origin(); // Forces the calcultation of the unreparsing stuff!
app_root = SIO.Path.GetFullPath(Multiplatform.FileSystem.GetRealPathname(global::KSPUtil.ApplicationRootPath));
app_root = SIO.Path.GetFullPath(Multiplatform.FileSystem.GetRealPathname(KSPUtil.ApplicationRootPath));
app_root = EnsureTrailingSeparatorOnDir(app_root);

process_dir(app_root);
Expand Down
1 change: 1 addition & 0 deletions Source/KSPe/KSPe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="Util\Image.Local.cs" />
<Compile Include="Util\Stopwatch.cs" />
<Compile Include="Util\UrlTools.cs" />
<Compile Include="Util\KSPUtil.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\Version.tt">
Expand Down
60 changes: 60 additions & 0 deletions Source/KSPe/Util/KSPUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This file is part of KSPe, a component for KSP Enhanced /L
© 2018-2023 LisiasT : http://lisias.net <support@lisias.net>
KSP Enhanced /L is double licensed, as follows:
* SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt
* GPL 2.0 : https://www.gnu.org/licenses/gpl-2.0.txt
And you are allowed to choose the License that better suit your needs.
KSP Enhanced /L is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the SKL Standard License 1.0
along with KSP Enhanced /L. If not, see <https://ksp.lisias.net/SKL-1_0.txt>.
You should have received a copy of the GNU General Public License 2.0
along with KSP Enhanced /L. If not, see <https://www.gnu.org/licenses/>.
*/
using SIO = System.IO;
using SIR = System.Reflection;
namespace KSPe
{
/***
* Brute force replacement for global::KSPUtil from Squad, allowing it to work outside the UnityEngine (sometimes arbitrary
* and useless) limitations.
*/
public static class KSPUtil
{
private static string __ApplicationRootPath = null;
public static string ApplicationRootPath => __ApplicationRootPath ?? (__ApplicationRootPath = calculateApplicationRootPath());

private static string calculateApplicationRootPath()
{
string r;
try
{
r = global::KSPUtil.ApplicationRootPath;

// Playing safe
if (string.IsNullOrEmpty(r)) throw new System.NullReferenceException("global::KSPUtil.ApplicationRootPath returned an empty string!");
}
catch (System.Exception e)
{
#if DEBUG
UnityEngine.Debug.LogWarningFormat("Coldn't rely on KSPUtil.ApplicationRootPath due [[{0}]]. Going brute force.", e.Message);
#endif
r = SIO.Path.GetDirectoryName(System.Environment.GetCommandLineArgs()[0]);
if (r.Contains("KSP.app"))
r = r.Substring(0, r.IndexOf("KSP.app"));
}
#if DEBUG
UnityEngine.Debug.LogFormat("Calculated ApplicationRootPath {0}.", r);
return r;
#endif
}
}
}

0 comments on commit e1c71dd

Please sign in to comment.