Skip to content

Commit

Permalink
Detects when the user removes the ModuleManager/L's and/or `ModuleM…
Browse files Browse the repository at this point in the history
…anagerWatchDog`'s directory(ies), and shut everything down accordingly.

For #10
  • Loading branch information
Lisias committed Mar 29, 2024
1 parent 8d68b71 commit 640b0a1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/ModuleManagerWatchDog/ModuleManagerWatchDog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="GUI\WarningAlertbox.cs" />
<Compile Include="InstallChecker.cs" />
<Compile Include="Util\Toolbox.cs" />
<Compile Include="Util\SelfCleaning.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\Version.tt">
Expand Down
24 changes: 24 additions & 0 deletions Source/ModuleManagerWatchDog/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ internal class Startup : MonoBehaviour
private void Start()
{
Log.force("Version {0}", ModuleManagerWatchDog.Version.Text);
{
bool safeToKillMyself = true;
// Check if ModuleManager /L should be uninstalled
if (SelfCleaning.CheckUninstalled("ModuleManager", "ModuleManager.dll"))
{
Log.warn("ModuleManager /L's directory was removed. ModuleManagerWatchDog is removing ModuleManager /L from the Loading System, but some `*.delete-me` files may be left in your `GameData` until the next boot. Nothing bad will happen by leaving them there, however.");
SelfCleaning.KillThis("ModuleManager.dll");
safeToKillMyself = false;
}

// Check if ModuleManagerWatchDog should be uninstalled
if (SelfCleaning.CheckUninstalled("ModuleManagerWatchDog", "666_ModuleManagerWatchDog.dll"))
{
if (safeToKillMyself)
{
Log.warn("ModuleManagerWatchDog's directory was removed. The bootstrap is removing itself from the Loading System, but you may need to delete manually some `*.delete-me` files in your `GameData`. Nothing bad will happen by leaving them there, however.");
SelfCleaning.KillThis("666_ModuleManagerWatchDog.dll");
}
else
Log.warn("ModuleManagerWatchDog's directory was removed, but some housekeeping is still in progress. It will remove itself in the next boot.");
// We are dead. No further actions should be allowed.
return;
}
}
{
InstallChecker ic = new InstallChecker();
ic.Execute();
Expand Down
5 changes: 5 additions & 0 deletions Source/ModuleManagerWatchDog/Util/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ internal static void info(string msg, params object[] @params)
UnityEngine.Debug.LogFormat("[ModuleManagerWatchDog] INFO: " + msg, @params);
}

internal static void warn(string msg, params object[] @params)
{
UnityEngine.Debug.LogWarningFormat("[ModuleManagerWatchDog] WARNING: " + msg, @params);
}

internal static void detail(string msg, params object[] @params)
{
UnityEngine.Debug.LogFormat("[ModuleManagerWatchDog] DETAIL: " + msg, @params);
Expand Down
65 changes: 65 additions & 0 deletions Source/ModuleManagerWatchDog/Util/SelfCleaning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
This file is part of Module Manager Watch Dog
©2020-2024 Lisias T : http://lisias.net <[email protected]>
Module Manager Watch Dog is licensed as follows:
* SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt
Module Manager Watchdog 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 Module Manager Watch Dog. If not, see
<https://ksp.lisias.net/SKL-1_0.txt>.
*/
using System;
using SIO = System.IO;

namespace WatchDog.ModuleManager
{
public static class SelfCleaning
{
private const string DELETEME = ".delete-me";
private static readonly string GAMEDATA = SanityLib.GetPathFor("GameData");

internal static bool CheckUninstalled(string directory, string dllName)
{
string dirpathname = SIO.Path.Combine(GAMEDATA, directory);
string dllpathname = SIO.Path.Combine(GAMEDATA, dllName);
return SIO.File.Exists(dllpathname) && !SIO.Directory.Exists(dirpathname);
}

internal static bool KillThis(string dllName)
{
string pathname = SIO.Path.Combine(GAMEDATA, dllName);
bool r = SIO.File.Exists(pathname);
if (r) Delete(pathname);
else
{
string tempname = pathname + DELETEME;
if (SIO.File.Exists(tempname)) SIO.File.Delete(tempname);
}
return r;
}

private static void Delete(string filename)
{
Log.dbg("Deleting {0}", filename);
if (SIO.File.Exists(filename)) try
{
SIO.File.Delete(filename);
}
catch (Exception e) when (e is System.UnauthorizedAccessException || e is System.Security.SecurityException)
{
// Oukey, we are in Windows and it locks the DLL file once it's loaded.
// But we can rename it, and delete it later.
string tempname = filename + DELETEME;
if (SIO.File.Exists(tempname)) SIO.File.Delete(tempname);
SIO.File.Move(filename, tempname);
}
}

}
}

0 comments on commit 640b0a1

Please sign in to comment.