-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
354 lines (305 loc) · 13.6 KB
/
index.js
File metadata and controls
354 lines (305 loc) · 13.6 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @file zEdit Patcher - Randomizes the dragon shouts you receive from word walls.
* @author ChrRubin
* @version 1.0.2
* @license MIT
* @copyright ChrRubin 2020
*/
/* global info, xelib, registerPatcher, patcherUrl, fh, patcherPath */
const rwwLogPath = `${patcherPath}\\rwwLog.txt`;
class ChrCustomError extends Error {
constructor(message) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ChrCustomError);
}
this.name = "ChrCustomError";
}
}
class RwwSettingsJSON {
/**
* @typedef {Object} RwwSettingsData
* @property {string} pluginName
* @property {string[]} shortFormIDs
*/
/**
* Creates an instance of RwwSettingsJSON.
* @param {string[]} ignoredFiles UPF `settings.ignoredFiles`
* @param {*} helpers UPF `helpers`
* @memberof RwwSettingsJSON
*/
constructor(ignoredFiles, helpers) {
const rwwSettingsPath = `${patcherPath}\\rwwSettings.json`;
if (!fh.jetpack.exists(rwwSettingsPath)) {
throw new ChrCustomError("Unable to find settings.json!");
}
const settingsJson = fh.loadJsonFile(rwwSettingsPath);
function validateObject(obj, location, ...properties) {
properties.forEach(prop => {
if (!Object.prototype.hasOwnProperty.call(obj, prop)) {
throw new ChrCustomError(`Invalid settings JSON at ${location}!`);
}
});
}
const properties = ["dynamicWordWallTriggers", "dynamicBlacklist", "hardcodedWalls"];
properties.forEach(prop => {
validateObject(settingsJson, "root", prop);
settingsJson[prop].forEach(obj => validateObject(obj, prop, ...["pluginName", "shortFormIDs"]));
});
/** @type {RwwSettingsData[]} */
this.dynamicWordWallTriggers = settingsJson.dynamicWordWallTriggers;
/** @type {RwwSettingsData[]} */
this.dynamicBlacklist = settingsJson.dynamicBlacklist;
/** @type {RwwSettingsData[]} */
this.hardcodedWalls = settingsJson.hardcodedWalls;
this.ignoredFiles = ignoredFiles;
this.helpers = helpers;
}
/**
* Reduce settings data array into array of handles to records.
* @param {RwwSettingsData[]} dataArray Data array
* @param {boolean} [returnFormId=false] Set true to return array of FormIDs instead of handles
* @return {number[]} Array of handles
* @memberof RwwSettingsJSON
*/
reduceData(dataArray, returnFormId = false) {
return dataArray.reduce((result, currentObj) => {
if (this.ignoredFiles.some(file => file.trim().toUpperCase() === currentObj.pluginName.trim().toUpperCase())) {
this.helpers.logMessage(`${currentObj.pluginName} is ignored. Skipping...`);
return result;
}
const file = xelib.FileByName(currentObj.pluginName);
if (!file) {
this.helpers.logMessage(`${currentObj.pluginName} is not loaded. Skipping...`);
return result;
}
const loadOrder = xelib.Hex(xelib.GetFileLoadOrder(file), 2);
return result.concat(
currentObj.shortFormIDs.map(shortFormID => {
const formid = `${loadOrder}${shortFormID}`;
const handle = xelib.GetElement(file, formid);
if (!handle) {
throw new ChrCustomError(`Unable to load ${formid}!`);
}
if (returnFormId) {
return formid;
}
return handle;
})
);
}, []);
}
/**
* Get array of handles to loaded word wall triggers.
* @return {number[]}
* @memberof RwwSettingsJSON
*/
getWordWallTriggers() {
return this.reduceData(this.dynamicWordWallTriggers);
}
/**
* Get array of blacklisted FormIDs.
* @return {string[]}
* @memberof RwwSettingsJSON
*/
getBlacklist() {
return this.reduceData(this.dynamicBlacklist, true);
}
/**
* Get array of handles to loaded hardcoded walls.
* @return {number[]}
* @memberof RwwSettingsJSON
*/
getHardcodedWalls() {
return this.reduceData(this.hardcodedWalls);
}
}
class WordWallRefr {
constructor(refrHandle) {
this.handle = refrHandle;
this.formid = xelib.GetHexFormID(refrHandle);
this.script = xelib.GetElement(refrHandle, "VMAD\\Scripts\\[0]");
this.myWord01 = xelib.GetScriptProperty(this.script, "myWord01");
this.myWord02 = xelib.GetScriptProperty(this.script, "myWord02");
this.myWord03 = xelib.GetScriptProperty(this.script, "myWord03");
this.shoutGlobal = xelib.GetScriptProperty(this.script, "shoutGlobal");
if (!this.script) {
throw new ChrCustomError(`${this.formid} does not have a script.`);
}
else if (!this.myWord01) {
throw new ChrCustomError(`${this.formid}'s script does not have a myWord01 property.`);
}
else if (!this.myWord02) {
throw new ChrCustomError(`${this.formid}'s script does not have a myWord02 property.`);
}
else if (!this.myWord03) {
throw new ChrCustomError(`${this.formid}'s script does not have a myWord03 property.`);
}
else if (!this.shoutGlobal) {
throw new ChrCustomError(`${this.formid}'s script does not have a shoutGlobal property.`);
}
this.myWord01Value = this.getScriptPropertyValue(this.myWord01);
this.myWord02Value = this.getScriptPropertyValue(this.myWord02);
this.myWord03Value = this.getScriptPropertyValue(this.myWord03);
this.shoutGlobalValue = this.getScriptPropertyValue(this.shoutGlobal);
const cell = xelib.GetLinksTo(this.handle, "Cell");
if (!cell) {
this.cellName = "";
}
this.cellName = xelib.LongName(cell);
}
/**
* Retrieves script property value.
* Cause of course Bethesda made multiple object versions, causing issues with running SetElement on the script property itself.
* @param {number} scriptProperty Script property handle
* @returns {number} FormID value handle
*/
getScriptPropertyValue(scriptProperty) {
const valuePath1 = "Value\\Object Union\\Object v1\\FormID";
const valuePath2 = "Value\\Object Union\\Object v2\\FormID";
const value = xelib.GetElement(scriptProperty, valuePath1);
if (value) {
return value;
}
return xelib.GetElement(scriptProperty, valuePath2);
}
}
/**
* Shuffles array.
* Source: https://gist.github.com/guilhermepontes/17ae0cc71fa2b13ea8c20c94c5c35dc4
* @param {any[]} array Original array
* @returns {any[]} Shuffled array
*/
function shuffleArray(array) {
return array.map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]);
}
registerPatcher({
info: info,
gameModes: [xelib.gmSSE, xelib.gmTES5],
settings: {
label: 'Word Wall Randomizer',
templateUrl: `${patcherUrl}/partials/settings.html`,
controller: function ($scope) {
$scope.showRecentLog = () => {
if (!fh.jetpack.exists(rwwLogPath)) {
alert("Log file does not exist!");
return;
}
fh.openFile(rwwLogPath);
};
},
defaultSettings: {
isDynamic: false,
setEsl: true,
showLog: false,
patchFileName: 'RandomWordWallsPatch.esp'
}
},
execute: (patchFile, helpers, settings, locals) => ({
initialize: () => {
const settingsJson = new RwwSettingsJSON(settings.ignoredFiles, helpers);
// Stores output log strings
locals.outputArray = [];
locals.outputArray.push(`${new Date().toString()}\n`);
const settingsLog = `PATCHER SETTINGS:\nIgnored files: ${settings.ignoredFiles.join(", ")}\nisDynamic: ${settings.isDynamic}\nsetEsl: ${settings.setEsl}\npatchFileName: ${settings.patchFileName}`;
helpers.logMessage(settingsLog);
locals.outputArray.push(settingsLog);
if (!settings.isDynamic) {
helpers.logMessage("Loading hardcoded Word Walls...");
locals.hardcodedWalls = settingsJson.getHardcodedWalls();
const hardCodedWallIDs = locals.hardcodedWalls.map(handle => xelib.GetHexFormID(handle));
const hardcodeLog = `Loaded hardcoded Word Walls: ${hardCodedWallIDs.join(", ")}`;
helpers.logMessage(hardcodeLog);
locals.outputArray.push(hardcodeLog);
}
else {
helpers.logMessage("Loading Word Wall triggers...");
locals.wordWallTriggers = settingsJson.getWordWallTriggers();
const wordWallTriggerIDs = locals.wordWallTriggers.map(handle => xelib.GetHexFormID(handle));
const triggerLog = `Loaded triggers: ${wordWallTriggerIDs.join(", ")}`;
helpers.logMessage(triggerLog);
locals.outputArray.push(triggerLog);
helpers.logMessage("Loading blacklisted FormIDs...");
locals.refrIdBlacklist = settingsJson.getBlacklist();
const blacklistLog = `Loaded blacklist: ${locals.refrIdBlacklist.join(", ")}`;
helpers.logMessage(blacklistLog);
locals.outputArray.push(blacklistLog);
}
},
process: [{
records: (filesToPatch, helpers, settings, locals) => {
if (settings.isDynamic) {
filesToPatch.forEach(file => {
helpers.logMessage(`Building references for ${file}...`);
xelib.BuildReferences(file);
});
}
else {
helpers.logMessage(`Dynamic patching disabled. Skipped reference building.`);
}
let wallRefrs;
if (settings.isDynamic) {
helpers.logMessage("Dynamically getting references to Word Wall Triggers...");
wallRefrs = locals.wordWallTriggers.reduce((result, acti) => {
const references = xelib.GetReferencedBy(xelib.GetMasterRecord(acti));
return result.concat(
references.filter(refr =>
xelib.Signature(refr) === "REFR" && !locals.refrIdBlacklist.includes(xelib.GetHexFormID(refr))
)
);
}, []);
}
else {
helpers.logMessage("Using hardcoded Word Walls...");
wallRefrs = locals.hardcodedWalls;
}
const filteredID = [];
const processedWallRefrs = wallRefrs.reduce((result, currentRefr) => {
const formid = xelib.GetHexFormID(currentRefr);
if (filteredID.includes(formid)) {
return result;
}
filteredID.push(formid);
if (xelib.Signature(currentRefr) !== "REFR") {
return result;
}
const previousOverride = xelib.GetPreviousOverride(currentRefr, patchFile);
if (xelib.GetRecordFlag(previousOverride, "Initially Disabled") || xelib.GetRecordFlag(previousOverride, "Deleted")) {
return result;
}
result.push(previousOverride);
return result;
}, []);
locals.wordWallRefsShuffled = shuffleArray(processedWallRefrs);
locals.indexCount = 0;
return processedWallRefrs;
},
patch: (record, helpers, settings, locals) => {
const formid = xelib.GetHexFormID(record);
helpers.logMessage(`Patching ${formid}...`);
const wallCopyFrom = new WordWallRefr(locals.wordWallRefsShuffled[locals.indexCount]);
const wallCopyTo = new WordWallRefr(record);
locals.outputArray.push("\n==============================");
locals.outputArray.push(`REFR: ${formid}`);
locals.outputArray.push(`At cell: ${wallCopyTo.cellName}`);
locals.outputArray.push(`Original shout: ${xelib.GetValue(wallCopyTo.shoutGlobalValue)}`);
locals.outputArray.push(`Randomized shout: ${xelib.GetValue(wallCopyFrom.shoutGlobalValue)}`);
xelib.SetElement(wallCopyTo.myWord01Value, wallCopyFrom.myWord01Value);
xelib.SetElement(wallCopyTo.myWord02Value, wallCopyFrom.myWord02Value);
xelib.SetElement(wallCopyTo.myWord03Value, wallCopyFrom.myWord03Value);
xelib.SetElement(wallCopyTo.shoutGlobalValue, wallCopyFrom.shoutGlobalValue);
locals.indexCount += 1;
}
}],
finalize: () => {
helpers.logMessage(`Setting ESL flag to ${settings.setEsl}.`);
xelib.SetRecordFlag(xelib.GetFileHeader(patchFile), "ESL", settings.setEsl);
helpers.logMessage(`Saving log file to ${rwwLogPath}`);
fh.saveTextFile(rwwLogPath, locals.outputArray.join("\n"));
if (settings.showLog) {
helpers.logMessage("Opening log file...");
fh.openFile(rwwLogPath);
}
}
})
});