Skip to content

Commit 6cbd80e

Browse files
committed
(Hopefuly) More sensible updating mechanism, coping with how Windows handles DLL files once they are loaded.
1 parent 37eb499 commit 6cbd80e

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

Source/WatchDogInstallChecker/Util/SanityLib.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace WatchDog.InstallChecker
2323
{
2424
public static class SanityLib
2525
{
26+
private const string DELETEME = ".delete-me";
27+
2628
/**
2729
* If you are interested only on assemblies that were properly loaded by KSP, this is the one you want.
2830
*/
@@ -54,6 +56,11 @@ internal static string UpdateIfNeeded(UpdateData ud)
5456
string sourceFilename = SIO.Path.Combine(SanityLib.CalcGameData(), ud.sourceFilename);
5557
string targetFilename = SIO.Path.Combine(SanityLib.CalcGameData(), ud.targetFilename);
5658

59+
{
60+
string tempFilename = targetFilename + DELETEME;
61+
if (SIO.File.Exists(tempFilename)) SIO.File.Delete(tempFilename);
62+
}
63+
5764
Log.dbg("UpdateIfNeeded from {0} to {1}", sourceFilename, targetFilename);
5865
if (SIO.File.Exists(sourceFilename))
5966
{
@@ -146,8 +153,18 @@ private static void Copy(string sourceFilename, string targetFilename)
146153
private static void Delete(string filename)
147154
{
148155
Log.dbg("Deleting {0}", filename);
149-
if (SIO.File.Exists(filename))
150-
SIO.File.Delete(filename);
156+
if (SIO.File.Exists(filename)) try
157+
{
158+
SIO.File.Delete(filename);
159+
}
160+
catch (Exception e) when (e is System.UnauthorizedAccessException || e is System.Security.SecurityException)
161+
{
162+
// Oukey, we are in Windows and it locks the DLL file once it's loaded.
163+
// But we can rename it, and delete it later.
164+
string tempname = filename + DELETEME;
165+
if (SIO.File.Exists(tempname)) SIO.File.Delete(tempname);
166+
SIO.File.Move(filename, tempname);
167+
}
151168
}
152169

153170
private static string GAMEDATA = null;

0 commit comments

Comments
 (0)