|
| 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