-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
287 lines (238 loc) · 11.8 KB
/
Copy pathProgram.cs
File metadata and controls
287 lines (238 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Mutagen.Bethesda;
using Mutagen.Bethesda.Environments;
using Mutagen.Bethesda.Fallout4;
using Mutagen.Bethesda.Oblivion;
using Mutagen.Bethesda.Plugins;
using Mutagen.Bethesda.Plugins.Cache;
using Mutagen.Bethesda.Plugins.Order;
using Newtonsoft.Json;
using IniParser;
using IniParser.Model;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace SS2Scraper;
class Program
{
static async Task Main(string[] args)
{
string? filePath = null;
bool doMO2 = false;
bool doLoadOrderMode = true;
bool doSingleMode = false;
bool doUnknowns = false;
string modListPath = "";
bool doNexusApi = false;
string nexusApiKey = "";
List<Export.ModMetadata> metadataCache = [];
for (int i = 0; i < args.Length; i++)
{
string nextArg = i + 1 < args.Length ? args[i + 1] : "";
switch (args[i].ToLower())
{
case "-mo2":
if (!File.Exists(nextArg) || !(Path.GetFileName(nextArg) == "modlist.txt")) throw new ArgumentException("-mo2 requires a valid path to modlist.txt as the next argument");
doMO2 = true;
modListPath = nextArg;
continue;
case "-data":
// strip quotes and slahes in case of "C:\foobar\"
string inputPath = nextArg.Trim().Trim('"').TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string fullPath = Path.GetFullPath(inputPath);
if (!File.Exists(fullPath) && !Directory.Exists(fullPath)) throw new ArgumentException("Plugin path is invalid");
filePath = fullPath;
continue;
case "-lomode":
doLoadOrderMode = true;
doSingleMode = false;
continue;
case "-singlemode":
doSingleMode = true;
doLoadOrderMode = false;
continue;
case "-unknowns":
doUnknowns = true;
continue;
case "-nexus":
doNexusApi = true;
nexusApiKey = nextArg;
continue;
}
}
List<string> modPaths = [];
// dedicated log for stuff more important than the general output
string logPath = "warnings.log";
if (!File.Exists(logPath)) // check for local path when debugging first
{
string exePath = AppDomain.CurrentDomain.BaseDirectory;
logPath = Path.Combine(exePath, logPath); // set to exe folder for published exe
}
string[] allowedExtensions = [".esm", ".esp", ".esl"];
if (doMO2)
{
//Console.WriteLine($"Checking dir for enabled mods {modListPath}");
List<string> enabledMods = [.. File.ReadLines(modListPath).Where(line => line.StartsWith('+'))];
foreach (string mod in enabledMods)
{
string modName = mod.StartsWith('+') ? mod[1..] : mod;
string modDir = Path.GetFullPath(Path.GetDirectoryName(modListPath) + "\\..\\..\\mods\\" + modName);
string iniPath = Path.GetFullPath(modDir + "\\meta.ini");
//Console.WriteLine($"Checking for {iniPath}");
if (!File.Exists(iniPath)) continue;
var iniParser = new FileIniDataParser();
IniData data = iniParser.ReadFile(iniPath);
int nexusId = int.Parse(data["General"]["modid"]);
if (nexusId <= 0) continue;
List<string> pluginFiles = [.. Directory.GetFiles(modDir).Where(file => allowedExtensions.Contains(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase))];
foreach (var pluginFile in pluginFiles)
{
//Console.WriteLine($"Found meta.ini for {Path.GetFileName(pluginFile)}");
Export.ModMetadata metadata = new()
{
pluginFile = Path.GetFileName(pluginFile),
nexusId = nexusId,
name = modName,
version = data["General"]["version"],
};
if (doNexusApi && nexusId > 0)
{
metadata.nexusData ??= new Export.NexusData();
NexusApi nexusApi = new(nexusApiKey);
JObject? result = await nexusApi.GetAsync($"games/fallout4/mods/{nexusId}");
if (result is not null)
{
metadata.nexusData.Name = result["name"]?.ToString() ?? "";
metadata.nexusData.Summary = result["summary"]?.ToString() ?? "";
metadata.nexusData.PictureUrl = result["picture_url"]?.ToString() ?? "";
metadata.nexusData.Version = result["version"]?.ToString() ?? "";
metadata.nexusData.UserId = result["user"]?["member_id"]?.Value<int>() ?? -1;
metadata.nexusData.Author = result["author"]?.ToString() ?? "";
metadata.nexusData.CreatedTime = result["created_time"]?.ToString() ?? "";
metadata.nexusData.UpdatedTime = result["updated_time"]?.ToString() ?? "";
}
await Task.Delay(34); // lazy rate limiting
}
metadataCache.Add(metadata);
}
}
}
if (Directory.Exists(filePath))
{
Console.WriteLine($"Directory mode: {filePath}");
modPaths = [.. Directory.GetFiles(filePath).Where(file => allowedExtensions.Contains(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase))];
}
else
{
throw new ArgumentException($"Invalid path: {filePath}");
}
if (doSingleMode)
{
Console.WriteLine($"Single mode: {filePath}");
foreach (string modPath in modPaths)
{
string pluginFile = Path.GetFileName(modPath);
Console.WriteLine($"Checking Plugin: {pluginFile}");
(var mod, var linkCache) = LoadMod(modPath);
if (mod is null || linkCache is null) continue;
Export export = new(mod, linkCache, doUnknowns);
var output = export.BuildOutput();
output.name = pluginFile;
output.metadata = metadataCache.FirstOrDefault(data => data.pluginFile == pluginFile);
Console.WriteLine($"Total SS2 Items: {output.totalItems}");
if (output.totalItems == 0) continue;
if (!Directory.Exists(".\\json")) Directory.CreateDirectory(".\\json");
var json = JsonConvert.SerializeObject(output);
File.WriteAllText($".\\json\\{pluginFile}.json", json);
}
}
if (doLoadOrderMode)
{
Console.WriteLine($"LO mode: {filePath}");
var linkCache = LoadMods(filePath, modPaths);
foreach (string modPath in modPaths)
{
string pluginDir = Path.GetDirectoryName(modPath) ?? throw new ArgumentException("Invalid plugin path");
string pluginFile = Path.GetFileName(modPath) ?? throw new ArgumentException("Invalid plugin path");
var activeMod = Fallout4Mod.CreateFromBinaryOverlay(Path.Combine(pluginDir, pluginFile), Fallout4Release.Fallout4);
Console.WriteLine($"Checking Plugin: {pluginFile}");
Export export = new(activeMod, linkCache, doUnknowns);
var output = export.BuildOutput();
output.name = pluginFile;
output.metadata = metadataCache.FirstOrDefault(data => data.pluginFile == pluginFile);
Console.WriteLine($"Total SS2 Items: {output.totalItems}");
if (output.totalItems == 0)
{
Console.WriteLine("No SS2 items found in this plugin. Skipping json export.");
continue;
}
if (!Directory.Exists(".\\json")) Directory.CreateDirectory(".\\json");
var json = JsonConvert.SerializeObject(output);
File.WriteAllText($".\\json\\{pluginFile}.json", json);
}
}
}
private static void Log(string message)
{
string logPath = "log.txt";
File.AppendAllText(logPath, $"{DateTime.Now}: {message}" + Environment.NewLine);
}
private static ILinkCache LoadMods(string pluginDir, List<string> plugins)
{
var listings = new List<LoadOrderListing>();
foreach (string plugin in plugins)
{
listings.Add(new(ModKey.FromFileName(Path.GetFileName(plugin)), enabled: true));
}
listings = [.. listings.Distinct()];
var loadOrder = LoadOrder.Import<IFallout4ModGetter>(listings, GameRelease.Fallout4);
var env = GameEnvironment.Typical.Builder<IFallout4Mod, IFallout4ModGetter>(GameRelease.Fallout4)
.WithTargetDataFolder(pluginDir)
.WithLoadOrder(loadOrder)
.Build();
ILinkCache linkCache = env.LoadOrder.ToImmutableLinkCache();
return linkCache;
}
private static (IFallout4ModDisposableGetter?, ILinkCache?) LoadMod(string pluginFullpath)
{
string pluginDir = Path.GetDirectoryName(pluginFullpath) ?? throw new ArgumentException("Invalid plugin path");
string pluginFile = Path.GetFileName(pluginFullpath) ?? throw new ArgumentException("Invalid plugin path");
// Console.WriteLine("Loading " + pluginFile);
var activeMod = Fallout4Mod.CreateFromBinaryOverlay(Path.Combine(pluginDir, pluginFile), Fallout4Release.Fallout4);
if (!activeMod.ModHeader.MasterReferences.Any(m => m.Master.FileName == "SS2.esm") && pluginFile != "SS2.esm")
{
Console.WriteLine("Plugin does not require SS2.esm, skipping.");
return (null, null);
}
// force load DLCs as some addons (even SS2.esm) reference them without having them as master
List<string> dlcs = [
"DLCRobot.esm",
"DLCCoast.esm",
"DLCNukaWorld.esm",
"DLCworkshop01.esm",
"DLCworkshop02.esm",
"DLCworkshop03.esm",
];
var listings = new List<LoadOrderListing>();
foreach (var dlc in dlcs) listings.Add(new(ModKey.FromFileName(dlc), enabled: true));
foreach (var masterFile in activeMod.ModHeader.MasterReferences)
{
string masterPath = Path.Combine(pluginDir, masterFile.Master.FileName);
if (!File.Exists(masterPath))
{
Console.WriteLine("Could not find required master: " + masterPath);
Log($"Missing master '{masterPath}' for plugin '{pluginFile}'");
return (null, null);
}
listings.Add(new(ModKey.FromFileName(masterFile.Master.FileName), enabled: true));
}
listings.Add(new(ModKey.FromFileName(pluginFile), enabled: true));
listings = [.. listings.Distinct()];
var loadOrder = LoadOrder.Import<IFallout4ModGetter>(listings, GameRelease.Fallout4);
var env = GameEnvironment.Typical.Builder<IFallout4Mod, IFallout4ModGetter>(GameRelease.Fallout4)
.WithTargetDataFolder(pluginDir)
.WithLoadOrder(loadOrder)
.Build();
//env.LoadOrder.ListedOrder.ToList().ForEach(item => { Console.WriteLine("Load Order: "+item.FileName); });
ILinkCache linkCache = env.LoadOrder.ToImmutableLinkCache();
return (activeMod, linkCache);
}
}