Skip to content

Commit 640b0a1

Browse files
committed
Detects when the user removes the ModuleManager/L's and/or ModuleManagerWatchDog's directory(ies), and shut everything down accordingly.
For #10
1 parent 8d68b71 commit 640b0a1

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Source/ModuleManagerWatchDog/ModuleManagerWatchDog.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="GUI\WarningAlertbox.cs" />
5858
<Compile Include="InstallChecker.cs" />
5959
<Compile Include="Util\Toolbox.cs" />
60+
<Compile Include="Util\SelfCleaning.cs" />
6061
</ItemGroup>
6162
<ItemGroup>
6263
<None Include="Properties\Version.tt">

Source/ModuleManagerWatchDog/Startup.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ internal class Startup : MonoBehaviour
2424
private void Start()
2525
{
2626
Log.force("Version {0}", ModuleManagerWatchDog.Version.Text);
27+
{
28+
bool safeToKillMyself = true;
29+
// Check if ModuleManager /L should be uninstalled
30+
if (SelfCleaning.CheckUninstalled("ModuleManager", "ModuleManager.dll"))
31+
{
32+
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.");
33+
SelfCleaning.KillThis("ModuleManager.dll");
34+
safeToKillMyself = false;
35+
}
36+
37+
// Check if ModuleManagerWatchDog should be uninstalled
38+
if (SelfCleaning.CheckUninstalled("ModuleManagerWatchDog", "666_ModuleManagerWatchDog.dll"))
39+
{
40+
if (safeToKillMyself)
41+
{
42+
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.");
43+
SelfCleaning.KillThis("666_ModuleManagerWatchDog.dll");
44+
}
45+
else
46+
Log.warn("ModuleManagerWatchDog's directory was removed, but some housekeeping is still in progress. It will remove itself in the next boot.");
47+
// We are dead. No further actions should be allowed.
48+
return;
49+
}
50+
}
2751
{
2852
InstallChecker ic = new InstallChecker();
2953
ic.Execute();

Source/ModuleManagerWatchDog/Util/Log.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ internal static void info(string msg, params object[] @params)
3030
UnityEngine.Debug.LogFormat("[ModuleManagerWatchDog] INFO: " + msg, @params);
3131
}
3232

33+
internal static void warn(string msg, params object[] @params)
34+
{
35+
UnityEngine.Debug.LogWarningFormat("[ModuleManagerWatchDog] WARNING: " + msg, @params);
36+
}
37+
3338
internal static void detail(string msg, params object[] @params)
3439
{
3540
UnityEngine.Debug.LogFormat("[ModuleManagerWatchDog] DETAIL: " + msg, @params);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
This file is part of Module Manager Watch Dog
3+
©2020-2024 Lisias T : http://lisias.net <support@lisias.net>
4+
5+
Module Manager Watch Dog is licensed as follows:
6+
* SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt
7+
8+
Module Manager Watchdog is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
12+
You should have received a copy of the SKL Standard License 1.0
13+
along with Module Manager Watch Dog. If not, see
14+
<https://ksp.lisias.net/SKL-1_0.txt>.
15+
16+
*/
17+
using System;
18+
using SIO = System.IO;
19+
20+
namespace WatchDog.ModuleManager
21+
{
22+
public static class SelfCleaning
23+
{
24+
private const string DELETEME = ".delete-me";
25+
private static readonly string GAMEDATA = SanityLib.GetPathFor("GameData");
26+
27+
internal static bool CheckUninstalled(string directory, string dllName)
28+
{
29+
string dirpathname = SIO.Path.Combine(GAMEDATA, directory);
30+
string dllpathname = SIO.Path.Combine(GAMEDATA, dllName);
31+
return SIO.File.Exists(dllpathname) && !SIO.Directory.Exists(dirpathname);
32+
}
33+
34+
internal static bool KillThis(string dllName)
35+
{
36+
string pathname = SIO.Path.Combine(GAMEDATA, dllName);
37+
bool r = SIO.File.Exists(pathname);
38+
if (r) Delete(pathname);
39+
else
40+
{
41+
string tempname = pathname + DELETEME;
42+
if (SIO.File.Exists(tempname)) SIO.File.Delete(tempname);
43+
}
44+
return r;
45+
}
46+
47+
private static void Delete(string filename)
48+
{
49+
Log.dbg("Deleting {0}", filename);
50+
if (SIO.File.Exists(filename)) try
51+
{
52+
SIO.File.Delete(filename);
53+
}
54+
catch (Exception e) when (e is System.UnauthorizedAccessException || e is System.Security.SecurityException)
55+
{
56+
// Oukey, we are in Windows and it locks the DLL file once it's loaded.
57+
// But we can rename it, and delete it later.
58+
string tempname = filename + DELETEME;
59+
if (SIO.File.Exists(tempname)) SIO.File.Delete(tempname);
60+
SIO.File.Move(filename, tempname);
61+
}
62+
}
63+
64+
}
65+
}

0 commit comments

Comments
 (0)