Skip to content

Commit 7b41e1e

Browse files
committed
Somewhat saner update mechanism
1 parent c5d94bf commit 7b41e1e

2 files changed

Lines changed: 52 additions & 41 deletions

File tree

Source/WatchDogInstallChecker/Startup.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,25 @@ namespace WatchDog.InstallChecker
2525
[KSPAddon(KSPAddon.Startup.Instantly, true)]
2626
internal class Startup : MonoBehaviour
2727
{
28-
private const string MYSELF_NAME = "Module Manager WatchDog Main Assembly";
29-
private readonly string MYSELF_SOURCE_FN = System.IO.Path.Combine("ModuleManagerWatchDog",
30-
System.IO.Path.Combine("Plugins",
31-
System.IO.Path.Combine("PluginData", "ModuleManagerWatchDog.dll")
32-
)
33-
);
34-
private const string MYSELF_TARGET_FN = "666_ModuleManagerWatchDog.dll";
35-
36-
private const string MML_NAME = "Module Manager";
37-
private readonly string MML_SOURCE_FN = System.IO.Path.Combine("ModuleManager",
38-
System.IO.Path.Combine("PluginData", "ModuleManager.dll")
39-
);
40-
private const string MML_TARGET_FN = "ModuleManager.dll";
41-
42-
private const string SCALE_NAME = "TweakScale's Scale_Redist";
43-
private readonly string SCALE_SOURCE_FN = System.IO.Path.Combine("TweakScale",
44-
System.IO.Path.Combine("Plugins",
45-
System.IO.Path.Combine("PluginData", "Scale_Redist.dll")
46-
)
47-
);
48-
private const string SCALE_TARGET_FN = "999_Scale_Redist.dll";
28+
private readonly SanityLib.UpdateData[] UPDATEABLES = new SanityLib.UpdateData[] {
29+
new SanityLib.UpdateData(
30+
"Module Manager WatchDog Main Assembly",
31+
System.IO.Path.Combine("ModuleManagerWatchDog",
32+
System.IO.Path.Combine("Plugins",
33+
System.IO.Path.Combine("PluginData", "ModuleManagerWatchDog.dll"))),
34+
"666_ModuleManagerWatchDog.dll"),
35+
new SanityLib.UpdateData(
36+
"Module Manager",
37+
System.IO.Path.Combine("ModuleManager",
38+
System.IO.Path.Combine("PluginData", "ModuleManager.dll")),
39+
"ModuleManager.dll"),
40+
new SanityLib.UpdateData(
41+
"TweakScale's Scale_Redist",
42+
System.IO.Path.Combine("TweakScale",
43+
System.IO.Path.Combine("Plugins",
44+
System.IO.Path.Combine("PluginData", "Scale_Redist.dll"))),
45+
"999_Scale_Redist.dll")
46+
};
4947

5048
private void Start()
5149
{
@@ -67,16 +65,13 @@ private void Start()
6765

6866
try
6967
{
70-
List<String> msgs = new List<string>();
71-
72-
String msg = SanityLib.UpdateIfNeeded(MYSELF_NAME, MYSELF_SOURCE_FN, MYSELF_TARGET_FN);
73-
if (null != msg) msgs.Add(msg);
74-
75-
msg = SanityLib.UpdateIfNeeded(MML_NAME, MML_SOURCE_FN, MML_TARGET_FN);
76-
if (null != msg) msgs.Add(msg);
77-
78-
msg = SanityLib.UpdateIfNeeded(SCALE_NAME, SCALE_SOURCE_FN, SCALE_TARGET_FN);
79-
if (null != msg) msgs.Add(msg);
68+
List<string> msgs = new List<string>();
69+
string msg;
70+
foreach (SanityLib.UpdateData ud in UPDATEABLES)
71+
{
72+
msg = SanityLib.UpdateIfNeeded(ud);
73+
if (null != msg) msgs.Add(msg);
74+
}
8075

8176
msg = string.Join("\n\n", msgs.ToArray());
8277
if ( !string.Empty.Equals(msg) )

Source/WatchDogInstallChecker/Util/SanityLib.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,25 @@ select a
3636
;
3737
}
3838

39-
internal static string UpdateIfNeeded(string name, string sourceFilename, string targetFilename)
39+
internal struct UpdateData
4040
{
41-
sourceFilename = System.IO.Path.Combine(SanityLib.CalcGameData(), sourceFilename);
42-
targetFilename = System.IO.Path.Combine(SanityLib.CalcGameData(), targetFilename);
41+
public readonly string name;
42+
public readonly string sourceFilename;
43+
public readonly string targetFilename;
44+
45+
internal UpdateData(string name, string sourceFilename, string targetFilename)
46+
{
47+
this.name = name;
48+
this.sourceFilename = sourceFilename;
49+
this.targetFilename = targetFilename;
50+
}
51+
}
52+
internal static string UpdateIfNeeded(UpdateData ud)
53+
{
54+
string sourceFilename = System.IO.Path.Combine(SanityLib.CalcGameData(), ud.sourceFilename);
55+
string targetFilename = System.IO.Path.Combine(SanityLib.CalcGameData(), ud.targetFilename);
56+
57+
Log.dbg("UpdateIfNeeded from {0} to {1}", sourceFilename, targetFilename);
4358
if (System.IO.File.Exists(sourceFilename))
4459
{
4560
if (System.IO.File.Exists(targetFilename))
@@ -55,19 +70,19 @@ internal static string UpdateIfNeeded(string name, string sourceFilename, string
5570

5671
if (!sane)
5772
{
58-
Log.info("File {0} is not compatible with {1}. This is going to cause trouble, replacing it!", targetFilename, name);
73+
Log.info("File {0} is not compatible with {1}. This is going to cause trouble, replacing it!", targetFilename, ud.name);
5974
Delete(targetFilename); // Remove the file and update it no matter what!
60-
return Update(name, sourceFilename, targetFilename);
75+
return Update(ud.name, sourceFilename, targetFilename);
6176
}
6277
}
6378
{
6479
System.Reflection.Assembly sourceAsm = System.Reflection.Assembly.LoadFile(sourceFilename);
6580
System.Reflection.Assembly targetAsm = System.Reflection.Assembly.LoadFile(targetFilename);
6681
if (!sourceAsm.GetName().Version.Equals(targetAsm.GetName().Version))
6782
{
68-
Log.info("File {0} is older then {1}. This is going to cause trouble, updating it!", targetFilename, name);
83+
Log.info("File {0} is older then {1}. This is going to cause trouble, updating it!", targetFilename, ud.name);
6984
Delete(targetFilename); // Remove the file or the update will not work.
70-
return Update(name, sourceFilename, targetFilename);
85+
return Update(ud.name, sourceFilename, targetFilename);
7186
}
7287
else
7388
{
@@ -76,18 +91,18 @@ internal static string UpdateIfNeeded(string name, string sourceFilename, string
7691
}
7792
}
7893
}
79-
else return SanityLib.Update(name, sourceFilename, targetFilename);
94+
else return SanityLib.Update(ud.name, sourceFilename, targetFilename);
8095
}
8196
// Nothing to do. If this is an error, someone else will yell about.
8297
return null;
8398
}
8499

85100
private static string Update(string name, string sourceFilename, string targetFilename)
86101
{
102+
Log.dbg("Update from {0} to {1}", sourceFilename, targetFilename);
87103
try
88104
{
89105
Copy(sourceFilename, targetFilename);
90-
Log.dbg("Deleting {0}", sourceFilename);
91106
Delete(sourceFilename);
92107
return string.Format("{0} was updated.", name);
93108
}
@@ -107,7 +122,8 @@ private static void Copy(string sourceFilename, string targetFilename)
107122
private static void Delete(string filename)
108123
{
109124
Log.dbg("Deleting {0}", filename);
110-
System.IO.File.Delete(filename);
125+
if (System.IO.File.Exists(filename))
126+
System.IO.File.Delete(filename);
111127
}
112128

113129
private static string GAMEDATA = null;

0 commit comments

Comments
 (0)