Skip to content

Commit e40426a

Browse files
committed
Extract GenerateModList
Unfortunately interacts with AssemblyLoader and the file system so not really testable
1 parent f3352db commit e40426a

3 files changed

Lines changed: 141 additions & 124 deletions

File tree

ModuleManager/MMPatchLoader.cs

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -127,129 +127,6 @@ public static void AddPostPatchCallback(ModuleManagerPostPatchCallback callback)
127127
postPatchCallbacks.Add(callback);
128128
}
129129

130-
private static IEnumerable<string> GenerateModList(IPatchProgress progress, IBasicLogger logger)
131-
{
132-
#region List of mods
133-
134-
//string envInfo = "ModuleManager env info\n";
135-
//envInfo += " " + Environment.OSVersion.Platform + " " + ModuleManager.intPtr.ToInt64().ToString("X16") + "\n";
136-
//envInfo += " " + Convert.ToString(ModuleManager.intPtr.ToInt64(), 2) + " " + Convert.ToString(ModuleManager.intPtr.ToInt64() >> 63, 2) + "\n";
137-
//string gamePath = Environment.GetCommandLineArgs()[0];
138-
//envInfo += " Args: " + gamePath.Split(Path.DirectorySeparatorChar).Last() + " " + string.Join(" ", Environment.GetCommandLineArgs().Skip(1).ToArray()) + "\n";
139-
//envInfo += " Executable SHA256 " + FileSHA(gamePath);
140-
//
141-
//log(envInfo);
142-
143-
List<string> mods = new List<string>();
144-
145-
StringBuilder modListInfo = new StringBuilder();
146-
147-
modListInfo.Append("compiling list of loaded mods...\nMod DLLs found:\n");
148-
149-
string format = " {0,-40}{1,-25}{2,-25}{3,-25}{4}\n";
150-
151-
modListInfo.AppendFormat(
152-
format,
153-
"Name",
154-
"Assembly Version",
155-
"Assembly File Version",
156-
"KSPAssembly Version",
157-
"SHA256"
158-
);
159-
160-
modListInfo.Append('\n');
161-
162-
foreach (AssemblyLoader.LoadedAssembly mod in AssemblyLoader.loadedAssemblies)
163-
{
164-
165-
if (string.IsNullOrEmpty(mod.assembly.Location)) //Diazo Edit for xEvilReeperx AssemblyReloader mod
166-
continue;
167-
168-
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(mod.assembly.Location);
169-
170-
AssemblyName assemblyName = mod.assembly.GetName();
171-
172-
string kspAssemblyVersion;
173-
if (mod.versionMajor == 0 && mod.versionMinor == 0)
174-
kspAssemblyVersion = "";
175-
else
176-
kspAssemblyVersion = mod.versionMajor + "." + mod.versionMinor;
177-
178-
string fileSha = "";
179-
try
180-
{
181-
fileSha = FileUtils.FileSHA(mod.assembly.Location);
182-
}
183-
catch(Exception e)
184-
{
185-
progress.Exception("Exception while generating SHA for assembly " + assemblyName.Name, e);
186-
}
187-
188-
modListInfo.AppendFormat(
189-
format,
190-
assemblyName.Name,
191-
assemblyName.Version,
192-
fileVersionInfo.FileVersion,
193-
kspAssemblyVersion,
194-
fileSha
195-
);
196-
197-
// modlist += String.Format(" {0,-50} SHA256 {1}\n", modInfo, FileSHA(mod.assembly.Location));
198-
199-
if (!mods.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase))
200-
mods.Add(assemblyName.Name);
201-
}
202-
203-
modListInfo.Append("Non-DLL mods added (:FOR[xxx]):\n");
204-
foreach (UrlDir.UrlConfig cfgmod in GameDatabase.Instance.root.AllConfigs)
205-
{
206-
if (CommandParser.Parse(cfgmod.type, out string name) != Command.Insert)
207-
{
208-
if (name.Contains(":FOR["))
209-
{
210-
name = name.RemoveWS();
211-
212-
// check for FOR[] blocks that don't match loaded DLLs and add them to the pass list
213-
try
214-
{
215-
string dependency = name.Substring(name.IndexOf(":FOR[") + 5);
216-
dependency = dependency.Substring(0, dependency.IndexOf(']'));
217-
if (!mods.Contains(dependency, StringComparer.OrdinalIgnoreCase))
218-
{
219-
// found one, now add it to the list.
220-
mods.Add(dependency);
221-
modListInfo.AppendFormat(" {0}\n", dependency);
222-
}
223-
}
224-
catch (ArgumentOutOfRangeException)
225-
{
226-
progress.Error(cfgmod, "Skipping :FOR init for line " + name +
227-
". The line most likely contains a space that should be removed");
228-
}
229-
}
230-
}
231-
}
232-
modListInfo.Append("Mods by directory (sub directories of GameData):\n");
233-
string gameData = Path.Combine(Path.GetFullPath(KSPUtil.ApplicationRootPath), "GameData");
234-
foreach (string subdir in Directory.GetDirectories(gameData))
235-
{
236-
string name = Path.GetFileName(subdir);
237-
string cleanName = name.RemoveWS();
238-
if (!mods.Contains(cleanName, StringComparer.OrdinalIgnoreCase))
239-
{
240-
mods.Add(cleanName);
241-
modListInfo.AppendFormat(" {0}\n", cleanName);
242-
}
243-
}
244-
logger.Info(modListInfo.ToString());
245-
246-
mods.Sort();
247-
248-
#endregion List of mods
249-
250-
return mods;
251-
}
252-
253130
private IEnumerator ProcessPatch()
254131
{
255132
status = "Checking Cache";
@@ -276,7 +153,7 @@ private IEnumerator ProcessPatch()
276153
IPatchProgress progress = new PatchProgress(logger);
277154
status = "Pre patch init";
278155
logger.Info(status);
279-
IEnumerable<string> mods = GenerateModList(progress, logger);
156+
IEnumerable<string> mods = ModListGenerator.GenerateModList(progress, logger);
280157

281158
yield return null;
282159

ModuleManager/ModListGenerator.cs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Diagnostics;
7+
using System.Reflection;
8+
using ModuleManager.Extensions;
9+
using ModuleManager.Logging;
10+
using ModuleManager.Utils;
11+
12+
namespace ModuleManager
13+
{
14+
public static class ModListGenerator
15+
{
16+
public static IEnumerable<string> GenerateModList(IPatchProgress progress, IBasicLogger logger)
17+
{
18+
#region List of mods
19+
20+
//string envInfo = "ModuleManager env info\n";
21+
//envInfo += " " + Environment.OSVersion.Platform + " " + ModuleManager.intPtr.ToInt64().ToString("X16") + "\n";
22+
//envInfo += " " + Convert.ToString(ModuleManager.intPtr.ToInt64(), 2) + " " + Convert.ToString(ModuleManager.intPtr.ToInt64() >> 63, 2) + "\n";
23+
//string gamePath = Environment.GetCommandLineArgs()[0];
24+
//envInfo += " Args: " + gamePath.Split(Path.DirectorySeparatorChar).Last() + " " + string.Join(" ", Environment.GetCommandLineArgs().Skip(1).ToArray()) + "\n";
25+
//envInfo += " Executable SHA256 " + FileSHA(gamePath);
26+
//
27+
//log(envInfo);
28+
29+
List<string> mods = new List<string>();
30+
31+
StringBuilder modListInfo = new StringBuilder();
32+
33+
modListInfo.Append("compiling list of loaded mods...\nMod DLLs found:\n");
34+
35+
string format = " {0,-40}{1,-25}{2,-25}{3,-25}{4}\n";
36+
37+
modListInfo.AppendFormat(
38+
format,
39+
"Name",
40+
"Assembly Version",
41+
"Assembly File Version",
42+
"KSPAssembly Version",
43+
"SHA256"
44+
);
45+
46+
modListInfo.Append('\n');
47+
48+
foreach (AssemblyLoader.LoadedAssembly mod in AssemblyLoader.loadedAssemblies)
49+
{
50+
51+
if (string.IsNullOrEmpty(mod.assembly.Location)) //Diazo Edit for xEvilReeperx AssemblyReloader mod
52+
continue;
53+
54+
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(mod.assembly.Location);
55+
56+
AssemblyName assemblyName = mod.assembly.GetName();
57+
58+
string kspAssemblyVersion;
59+
if (mod.versionMajor == 0 && mod.versionMinor == 0)
60+
kspAssemblyVersion = "";
61+
else
62+
kspAssemblyVersion = mod.versionMajor + "." + mod.versionMinor;
63+
64+
string fileSha = "";
65+
try
66+
{
67+
fileSha = FileUtils.FileSHA(mod.assembly.Location);
68+
}
69+
catch (Exception e)
70+
{
71+
progress.Exception("Exception while generating SHA for assembly " + assemblyName.Name, e);
72+
}
73+
74+
modListInfo.AppendFormat(
75+
format,
76+
assemblyName.Name,
77+
assemblyName.Version,
78+
fileVersionInfo.FileVersion,
79+
kspAssemblyVersion,
80+
fileSha
81+
);
82+
83+
// modlist += String.Format(" {0,-50} SHA256 {1}\n", modInfo, FileSHA(mod.assembly.Location));
84+
85+
if (!mods.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase))
86+
mods.Add(assemblyName.Name);
87+
}
88+
89+
modListInfo.Append("Non-DLL mods added (:FOR[xxx]):\n");
90+
foreach (UrlDir.UrlConfig cfgmod in GameDatabase.Instance.root.AllConfigs)
91+
{
92+
if (CommandParser.Parse(cfgmod.type, out string name) != Command.Insert)
93+
{
94+
if (name.Contains(":FOR["))
95+
{
96+
name = name.RemoveWS();
97+
98+
// check for FOR[] blocks that don't match loaded DLLs and add them to the pass list
99+
try
100+
{
101+
string dependency = name.Substring(name.IndexOf(":FOR[") + 5);
102+
dependency = dependency.Substring(0, dependency.IndexOf(']'));
103+
if (!mods.Contains(dependency, StringComparer.OrdinalIgnoreCase))
104+
{
105+
// found one, now add it to the list.
106+
mods.Add(dependency);
107+
modListInfo.AppendFormat(" {0}\n", dependency);
108+
}
109+
}
110+
catch (ArgumentOutOfRangeException)
111+
{
112+
progress.Error(cfgmod, "Skipping :FOR init for line " + name +
113+
". The line most likely contains a space that should be removed");
114+
}
115+
}
116+
}
117+
}
118+
modListInfo.Append("Mods by directory (sub directories of GameData):\n");
119+
string gameData = Path.Combine(Path.GetFullPath(KSPUtil.ApplicationRootPath), "GameData");
120+
foreach (string subdir in Directory.GetDirectories(gameData))
121+
{
122+
string name = Path.GetFileName(subdir);
123+
string cleanName = name.RemoveWS();
124+
if (!mods.Contains(cleanName, StringComparer.OrdinalIgnoreCase))
125+
{
126+
mods.Add(cleanName);
127+
modListInfo.AppendFormat(" {0}\n", cleanName);
128+
}
129+
}
130+
logger.Info(modListInfo.ToString());
131+
132+
mods.Sort();
133+
134+
#endregion List of mods
135+
136+
return mods;
137+
}
138+
}
139+
}

ModuleManager/ModuleManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="Logging\ModLogger.cs" />
4949
<Compile Include="MMPatchLoader.cs" />
5050
<Compile Include="ModuleManager.cs" />
51+
<Compile Include="ModListGenerator.cs" />
5152
<Compile Include="NeedsChecker.cs" />
5253
<Compile Include="PatchContext.cs" />
5354
<Compile Include="PatchExtractor.cs" />

0 commit comments

Comments
 (0)