-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.js
180 lines (163 loc) · 8.37 KB
/
resolver.js
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
/** d2rmp, a patcher script toolset for D2R mods.
* Copyright (C) 2024 myocytebd
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { nativePath, normalizePath, scriptPath, readFileSyncNoThrow, writeFileSync, mkdirSync } = require('./utils');
const kNextStringIDPath = 'local/lng/next_string_id.txt';
function parseNextStringID(content) {
let ma = content.match(/[0-9]+/);
if (!ma) throw new Error(`Invalid NextStringID File`);
return parseInt(ma[0]);
}
function updateNextStringID(content, newID) { return content.replace(/[0-9]+/, '' + newID); }
/** @typedef {{ relPath: string, realPath: string, content, type: ?string }} FileResolverInputInfo */
/** @typedef { FileResolverInputInfo & { dirty: boolean, evicted : boolean } } FileResolverOutputInfo */
class FileResolver {
constructor({ outputPath, baseInputPath, userInputPath }) {
this.outputPath = outputPath || null;
this.baseInputPath = baseInputPath;
this.userInputPath = userInputPath || null;
/** @type {string[]} */ this.inputPaths = [];
/** @type {Object.<string, FileResolverOutputInfo>} */
this.implicitOutputMap = {}; // rel-path : { realPath, dirty, content }
/** @type {Object.<string, FileResolverInputInfo>} */
this.implicitInputMap = {}; // rel-path : { realPath, content }
this.nextStringID = -1;
this.init();
}
init() {
this.inputPaths = Array.from(new Set([ this.userInputPath, this.baseInputPath ]).values()).filter(s => s);
}
getPath(...mixedPaths) { return normalizePath(path.join(...mixedPaths)); }
getNativePath(...mixedPaths) { return nativePath(this.getPath(...mixedPaths)); }
resolveImplicitOutput(mixeModResPath) {
let outputRelPath = normalizePath(mixeModResPath);
let outputInfo = this.implicitOutputMap[outputRelPath];
if (!outputInfo) {
this.implicitOutputMap[outputRelPath] = outputInfo = { relPath: outputRelPath, realPath: null, dirty: false, evicted: false, content: null };
outputInfo.realPath = this.getNativePath(this.outputPath, outputRelPath);
outputInfo.evicted = fs.existsSync(outputInfo.realPath);
console.debug(`FileResolver: init output mapping: ${outputRelPath} => ${outputInfo.realPath} (evicted=${outputInfo.evicted})`);
}
return outputInfo;
}
resolveImplicitInput(mixedAnyRelPath) {
let inputRelPath = normalizePath(mixedAnyRelPath);
let inputInfo = this.implicitInputMap[inputRelPath];
if (!inputInfo) {
this.implicitInputMap[inputRelPath] = inputInfo = { relPath: inputRelPath, realPath : null, content: null };
let resPathCands = this.inputPaths.map(inputPath => this.getNativePath(inputPath, inputRelPath));
for (let resPathCand of resPathCands) {
if (fs.existsSync(resPathCand)) { inputInfo.realPath = resPathCand; break; }
}
console.debug(`FileResolver: init input mapping: ${inputRelPath} => ${inputInfo.realPath}`)
}
return inputInfo;
}
flush() {
for (let [ outputRelPath, outputInfo ] of Object.entries(this.implicitOutputMap)) {
if (outputInfo.dirty) this.flush1Ex(outputInfo);
}
this.flushNextStringID();
}
flush1Ex(outputInfo) {
if (!outputInfo.dirty) return [ true, null ];
outputInfo.dirty = false;
if (!outputInfo.realPath.startsWith(path.resolve(this.outputPath, '..'))) throw new Error;
console.log(`FileResolver: write back: ${outputInfo.relPath}`);
mkdirSync(path.dirname(outputInfo.realPath));
writeFileSync(outputInfo.realPath, outputInfo.content, { addBOM: false });
return [ true, null ];
}
flush1(mixedModResPath) { return this.flush1Ex(this.resolveImplicitOutput(mixedModResPath)); }
/** @returns {[ ?string, ?Error, ?FileResolverInputInfo ]} */
readAutoInputFileSync(mixedAnyRelPath, options) {
let outputInfo = this.resolveImplicitOutput(mixedAnyRelPath);
if (outputInfo.evicted) {
let [ content, err ] = readFileSyncNoThrow(outputInfo.realPath, options);
if (!err) {
console.info(`FileResolver: reload output content: ${outputInfo.relPath}`);
outputInfo.content = content;
} else { // Failure to read-back output file is a critical error
console.error(`FileResolver: failed to reload output content: ${outputInfo.relPath}`);
throw err;
}
}
if (outputInfo.content) {
console.debug(`FileResolver: readAutoInputFileSync: forward: ${outputInfo.relPath}`);
return [ outputInfo.content, null, outputInfo ];
}
let inputInfo = this.resolveImplicitInput(mixedAnyRelPath);
if (inputInfo.content) {
console.debug(`FileResolver: readAutoInputFileSync: cached: ${inputInfo.relPath}`);
return [ inputInfo.content, null, inputInfo ];
}
if (!inputInfo.realPath) {
console.debug(`FileResolver: readAutoInputFileSync: not found: ${inputInfo.relPath}`);
return [ null, null, null ];
}
let [ content, err ] = readFileSyncNoThrow(inputInfo.realPath, options);
if (!err) {
console.debug(`FileResolver: cache content: ${inputInfo.relPath}`);
inputInfo.content = content;
}
return [ content, err, inputInfo ];
}
/** @returns {?FileResolverInputInfo} */
resolveAutoInputFile(mixedAnyRelPath) {
let outputInfo = this.outputPath ? this.resolveImplicitOutput(mixedAnyRelPath) : null;
if (outputInfo?.evicted) {
console.debug(`FileResolver: resolveAutoInputFile: found evicted output: ${outputInfo.relPath}`);
return outputInfo;
}
let inputInfo = this.resolveImplicitInput(mixedAnyRelPath);
if (inputInfo.realPath) {
console.debug(`FileResolver: resolveAutoInputFile: found input: ${inputInfo.relPath}`);
return inputInfo;
} else {
console.debug(`FileResolver: resolveAutoInputFile: not found: ${inputInfo.relPath}`);
return null;
}
}
/** @returns {[ boolean, ?Error, ?FileResolverOutputInfo ]} */
updateOutputFile(mixedModResPath, content, options) {
let outputInfo = this.resolveImplicitOutput(mixedModResPath);
if (content !== outputInfo.content) {
outputInfo.dirty = true;
outputInfo.content = content;
console.log(`FileResolver: output updated: ${outputInfo.relPath}`);
} else {
console.debug(`FileResolver: output unchanged: ${outputInfo.relPath}`);
}
return [ true, null, outputInfo ];
}
/** @returns {[ boolean, ?Error ]} */
writeOutputFile(mixedModResPath, content, options) {
this.updateOutputFile(mixedModResPath, content, options || {});
return this.flush1(mixedModResPath);
}
acquireNextStringID() {
if (this.nextStringID < 0) {
let [ content, error ] = this.readAutoInputFileSync(kNextStringIDPath);
if (error) { error.extra = `Input File not Found${kNextStringIDPath}`; throw error; }
this.nextStringID = parseNextStringID(content);
console.log(`FileResolver: initial NextStringID: ${this.nextStringID}`);
}
return this.nextStringID++;
}
flushNextStringID() {
if (this.nextStringID < 0) return;
let [ content, error ] = this.readAutoInputFileSync(kNextStringIDPath);
if (error) { error.extra = `Input File not Found: ${kNextStringIDPath}`; throw error; }
this.writeOutputFile(kNextStringIDPath, updateNextStringID(content, this.nextStringID));
}
}
module.exports = { FileResolver };