-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathA3sMods.ts
282 lines (246 loc) · 9.64 KB
/
A3sMods.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { parse } from 'arma-class-parser';
import got from 'got';
import he from 'he';
import { Low, JSONFile } from '@commonify/lowdb';
import { A3sEventsDto } from './A3sRemoteServer';
export interface Mod {
id: string;
name: string;
publishedid?: string;
download?: string;
dlcid?: string;
}
export interface Modlist {
[key: string]: Mod;
}
interface Db {
mods: Modlist;
}
const OFCRA_MOD_INDEX = 'https://ofcrav2.org/index.php?page=repository-en';
const OFCRA_CALENDAR = 'https://ofcrav2.org/index.php?page=donate';
export default class A3sMods {
private a3s_repo_url: string;
private db_file: string;
private overrides?: Modlist;
private db: Low<Db> | null = null;
/**
*
* @param a3s_repo_url the ArmA3Sync repo root url
* @param db_file db storage file
* @param overrides mod item overrides
*/
constructor(a3s_repo_url: string, db_file: string, overrides?: Modlist) {
this.a3s_repo_url = a3s_repo_url;
this.db_file = db_file;
this.overrides = overrides;
}
async init(): Promise<void> {
const adapter = new JSONFile<Db>(this.db_file);
this.db = new Low<Db>(adapter);
await this.db.read();
this.db.data = this.db.data || { mods: {} };
}
async save(): Promise<void> {
if (this.db && this.db.data) {
return this.db.write();
}
}
async parseA3sEvents(events: A3sEventsDto): Promise<void> {
if (this.db && this.db.data) {
const new_mods: Mod[] = [];
let updated = false;
for (const e of events.list) {
for (const a in e.addonNames) {
if (this.db.data.mods[a] === undefined && new_mods.find(m => m.id === a) === undefined) {
updated = true;
if (this.overrides && this.overrides[a] !== undefined) {
this.db.data.mods[a] = this.overrides[a];
} else {
new_mods.push({
id: a,
name: a
});
}
}
}
}
if (new_mods.length > 0) {
const added_mods = await this.fetchRepoIndexData(new_mods);
const promises = [];
for (const m of added_mods) {
promises.push(this.fetchSourceData(m.id).catch(e => { }));
}
await Promise.all(promises);
}
if (updated) {
this.save().catch(e => {
console.error(e.message);
});
}
} else {
throw new Error('Db not initialized');
}
}
// Note: this function is OFCRA specific
private async fetchRepoIndexData(new_mods: Mod[]) {
if (this.db && this.db.data) {
let rows: string[] = [];
try {
const src = await got(OFCRA_MOD_INDEX, { timeout: 3000 }).text();
rows = src.split('<tr>').slice(2);
} catch (err: any) {
console.error(err?.message);
}
if (rows.length > 0) {
for (const r of rows) {
const link_id = r.match(/<td>(.*?)<\/td><td>(.*?)<\/td>/);
if (link_id) {
const rm: Mod = {
id: link_id[2],
name: link_id[2]
};
const url = link_id[1].match(/href="(.*?)"/);
if (url) {
if (url[1].indexOf('/filedetails/?id=') !== -1) {
rm.publishedid = url[1].split('?id=')[1];
} else {
rm.download = url[1];
}
}
const nm = new_mods.find(m => m.id === rm.id);
if (nm) {
if (rm.publishedid) {
nm.publishedid = rm.publishedid;
}
if (rm.download) {
nm.download = rm.download;
}
this.db.data.mods[nm.id] = nm;
} else {
if (!this.db.data.mods[rm.id]) {
const nm: Mod = {
id: rm.id,
name: rm.id,
} as Mod;
this.db.data.mods[rm.id] = nm;
}
if (rm.publishedid) {
this.db.data.mods[rm.id].publishedid = rm.publishedid;
}
if (rm.download) {
this.db.data.mods[rm.id].download = rm.download;
}
new_mods.push(this.db.data.mods[rm.id]);
}
}
}
} else {
console.warn('repo index empty');
for (const nm of new_mods) {
if (!this.db.data.mods[nm.id]) {
this.db.data.mods[nm.id] = nm;
}
}
}
}
return new_mods;
}
// Note: this function is OFCRA specific
async fetchEventCalendar() {
const events: any[] = [];
let rows: string[] = [];
try {
const src = await got(OFCRA_CALENDAR, { timeout: 3000 }).text();
rows = src.split('<h2>Agenda</h2>')[1].split('<ul class="calendar">')[1].split('</ul>')[0].split('<li>');
} catch (err: any) {
console.error(err?.message);
}
if (rows.length > 0) {
for (const r of rows) {
const items = r.match(/<a href="(.*?)"><span class="date">(.*?)<\/span><span class="title">(.*?)<\/span><\/a>/);
if (items) {
const dp = items[2].split('.');
events.push({
url: items[1],
date: dp[2] + '-' + dp[1] + '-' + dp[0],
name: he.decode(items[3])
});
}
}
}
return events;
}
private async fetchSourceData(id: string) {
if (this.db && this.db.data && this.db.data.mods[id]) {
let meta_cpp = await this.parseModSource(id, 'meta');
let mod_cpp = await this.parseModSource(id, 'mod');
if (meta_cpp) {
if (meta_cpp.name) {
this.db.data.mods[id].name = meta_cpp.name;
}
if (meta_cpp.publishedid) {
this.db.data.mods[id].publishedid = String(meta_cpp.publishedid);
}
}
if (mod_cpp) {
if (mod_cpp.name && this.db.data.mods[id].name === id) {
const name_semver = mod_cpp.name.match(/(.*) \d\.\d\.\d$/);
if (name_semver) {
this.db.data.mods[id].name = name_semver[1];
} else {
this.db.data.mods[id].name = mod_cpp.name;
}
}
if (mod_cpp.action && this.db.data.mods[id].publishedid === undefined) {
if (mod_cpp.action.indexOf('/filedetails/?id=') !== -1) {
this.db.data.mods[id].publishedid = mod_cpp.action.split('?id=')[1];
} else if (this.db.data.mods[id].download === undefined) {
this.db.data.mods[id].download = mod_cpp.action;
}
}
}
if (this.db.data.mods[id].publishedid && this.db.data.mods[id].name === id) {
const stream = got.stream('https://steamcommunity.com/sharedfiles/filedetails/?id=' + this.db.data.mods[id].publishedid);
const head: string = await new Promise((resolve, reject) => {
let chunks: string = '';
stream.on('error', reject);
stream.on('data', data => {
chunks += data;
if (chunks.indexOf('</title>') !== -1) {
stream.destroy();
resolve(chunks);
}
});
stream.on('end', () => reject());
});
const name = head.match(/<title>Steam Workshop::(.*?)<\/title>/);
if (name && name[1]) {
this.db.data.mods[id].name = name[1];
}
}
}
}
private async parseModSource(id: string, type: string = 'meta') {
const file_url = this.a3s_repo_url + id + '/' + type + '.cpp';
let cpp = null;
let raw: Buffer | null = null;
try {
raw = await got(file_url, { encoding: 'binary', responseType: 'buffer', resolveBodyOnly: true, timeout: 3000 });
} catch (err) { }
if (raw) {
try {
cpp = parse(raw.toString('utf8'));
} catch (err_utf8) {
try {
cpp = parse(raw.toString('utf16le'));
} catch (err_utf16) {
console.error('Parsing error', file_url);
}
}
}
return cpp;
}
getMods() {
return this.db?.data?.mods;
}
}