forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinach.ts
265 lines (215 loc) · 7.71 KB
/
coinach.ts
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
// Helper for automating SaintCoinach -- https://github.com/ufx/SaintCoinach
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import eslint from 'eslint';
const coinachExe = 'SaintCoinach.Cmd.exe';
const defaultCoinachPaths = ['C:\\SaintCoinach\\', 'D:\\SaintCoinach\\'];
if (process.env['CACTBOT_DEFAULT_COINACH_PATH'])
defaultCoinachPaths.push(process.env['CACTBOT_DEFAULT_COINACH_PATH']);
const ffxivExe = path.join('game', 'ffxiv_dx11.exe');
const defaultFfxivPaths = [
'C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn',
'D:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn',
];
if (process.env['CACTBOT_DEFAULT_FFXIV_PATH'])
defaultFfxivPaths.push(process.env['CACTBOT_DEFAULT_FFXIV_PATH']);
const isObject = (obj: unknown): obj is { [key: string]: unknown } =>
obj !== null && typeof obj === 'object' && !Array.isArray(obj);
class CoinachError extends Error {
constructor(message?: string, cmd?: string, output?: string) {
const errorMessage = `message: ${message ?? ''}
cmd: ${cmd ?? ''}
output: ${output ?? ''}
`;
super(errorMessage);
}
}
export class CoinachReader {
coinachPath: string;
ffxivPath: string;
verbose: boolean;
constructor(coinachPath: string | null, ffxivPath: string | null, verbose = false) {
this.verbose = verbose;
if (!coinachPath) {
for (const p of defaultCoinachPaths) {
if (fs.existsSync(path.join(p, coinachExe))) {
coinachPath = p;
break;
}
}
}
if (!ffxivPath) {
for (const p of defaultFfxivPaths) {
if (fs.existsSync(path.join(p, ffxivExe))) {
ffxivPath = p;
break;
}
}
}
if (!coinachPath)
throw new CoinachError('coinach path not found');
if (!ffxivPath)
throw new CoinachError('ffxiv path not found');
this.coinachPath = coinachPath;
this.ffxivPath = ffxivPath;
if (!fs.existsSync(path.join(this.coinachPath, coinachExe)))
throw new CoinachError('invalid coinach path: ' + this.coinachPath);
if (!fs.existsSync(path.join(this.ffxivPath, ffxivExe)))
throw new CoinachError('invalid ffxiv path: ' + this.ffxivPath);
}
async exd(table: string, lang: string): Promise<string[]> {
return await this._coinachCmd('exd', table, lang);
}
async rawexd(table: string, lang: string): Promise<string[]> {
return await this._coinachCmd('rawexd', table, lang);
}
async _coinachCmd(coinachCmd: string, table: string, lang: string): Promise<string[]> {
const cmdList = [path.join(this.coinachPath, coinachExe)];
const args = [this.ffxivPath, 'lang ' + lang, coinachCmd + ' ' + table];
const cmd = [...cmdList, ...args].map((x) => `"${x}"`).join(' ');
if (this.verbose) {
console.log(`coinach_path: ${this.coinachPath}`);
console.log('ffxiv_path: ' + this.ffxivPath);
console.log('cmd: ' + cmd);
}
// # This will throw an exception if stuff is VERY wrong
// # however, return code is still 0 even if all exports fail.
// # Also, it seems to need to be run from the SaintCoinach directory.
const output = String(promisify(exec)(cmd));
// # Manually check output for errors.
let m = /^([0-9])* files exported, ([0-9])* failed/m.exec(output);
if (!m)
throw new CoinachError('Unknown output', cmd, output);
if (m[1] === '0')
throw new CoinachError('Zero successes', cmd, output);
if (m[2] !== '0')
throw new CoinachError('Non-zero failures', cmd, output);
// # Find directory that this export was written to.
// # There's no way to control this.
m = /^Definition version: ([0-9.]*)/m.exec(output);
if (!m)
throw new CoinachError('Unknown output', cmd, output);
const csvFilename = path.join(
this.coinachPath,
m[1] ?? '',
coinachCmd,
`${table}.csv`,
);
if (this.verbose) {
console.log(`output: \noutput`);
console.log(`output csv: ${csvFilename}`);
}
// # Read the whole file immediately,
// # as future commands with different langs will overwrite.
const lines = String(
await promisify(fs.readFile)(csvFilename, {
flag: 'r',
encoding: 'utf-8',
}),
).split(/\r?\n/);
if (this.verbose)
console.log(`csv lines: ${lines.length}`);
return lines;
}
}
export class CoinachWriter {
cactbotPath: string;
verbose: boolean;
constructor(cactbotPath: string | null, verbose: boolean) {
this.verbose = verbose;
if (!cactbotPath)
cactbotPath = this._findCactbotPath();
this.cactbotPath = cactbotPath;
if (!fs.existsSync(this.cactbotPath))
throw new Error(`Invalid cactboth path: ${this.cactbotPath}`);
}
_findCactbotPath(): string {
return '.';
}
sortObjByKeys(obj: unknown): unknown {
if (!isObject(obj) || Array.isArray(obj))
return obj;
const out = Object
.keys((obj))
.sort()
.reduce((acc: typeof obj, key) => {
const nested = obj[key];
if (isObject(nested))
acc[key] = this.sortObjByKeys(nested);
else
acc[key] = nested;
return acc;
}, {});
return out;
}
async write(
filename: string,
scriptname: string,
_variable: string | null,
d: unknown[],
): Promise<void> {
const fullPath = path.join(this.cactbotPath, filename);
let str = JSON.stringify(this.sortObjByKeys(d), null, 2);
// # make keys integers, remove leading zeroes.
str = str.replace(/\'0*([0-9]+)\': {/, '$1: {');
const f = `// Auto-generated from ${scriptname}
// DO NOT EDIT THIS FILE DIRECTLY
export default ${str}`;
const linter = new eslint.ESLint({ fix: true });
const results = await linter.lintText(f, { filePath: fullPath });
// There's only one result from lintText, as per documentation.
const lintResult = results[0];
if (!lintResult || lintResult.errorCount > 0 || lintResult.warningCount > 0) {
console.error('Lint ran with errors, aborting.');
process.exit(2);
}
// Overwrite the file, if it already exists.
const flags = 'w';
const writer = fs.createWriteStream(fullPath, { flags: flags });
writer.on('error', (err) => {
console.error(err);
process.exit(-1);
});
writer.write(lintResult.output);
if (this.verbose)
console.log(`wrote: ${filename}`);
}
async writeTypeScript(
filename: string,
scriptname: string,
header: string | null,
type: string | null,
asConst: boolean | null,
data: { [s: string]: unknown },
): Promise<void> {
const fullPath = path.join(this.cactbotPath, filename);
let str = JSON.stringify(this.sortObjByKeys(data), null, 2);
// # make keys integers, remove leading zeroes.
str = str.replace(/\'0*([0-9]+)\': {/, '$1: {');
const f = `// Auto-generated from ${scriptname}
// DO NOT EDIT THIS FILE DIRECTLY
${header ? '\n' + header : ''}
const data${type ? ':' + type : ' '} = ${str}${asConst ? ' as const' : ''};
export default data;`;
const linter = new eslint.ESLint({ fix: true });
const results = await linter.lintText(f, { filePath: fullPath });
// There's only one result from lintText, as per documentation.
const lintResult = results[0];
if (!lintResult || lintResult.errorCount > 0 || lintResult.warningCount > 0) {
console.error('Lint ran with errors, aborting.');
process.exit(2);
}
// Overwrite the file, if it already exists.
const flags = 'w';
const writer = fs.createWriteStream(fullPath, { flags: flags });
writer.on('error', (err) => {
console.error(err);
process.exit(-1);
});
writer.write(lintResult.output);
if (this.verbose)
console.log(`wrote: ${filename}`);
}
}