-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathstart.ts
More file actions
310 lines (279 loc) · 9.03 KB
/
start.ts
File metadata and controls
310 lines (279 loc) · 9.03 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
import child_process from 'child_process';
import fs from 'fs';
import { ExitPromptError } from '@inquirer/core';
import { confirm, input, number, password, select } from '@inquirer/prompts';
// if you're forking this feel free to change these :) it does make some assumptions elsewhere (branch names)
const repoOrg = 'https://github.com/LostCityRS';
const engineRepo = 'Engine-TS';
const contentRepo = 'Content';
const webRepo = 'Client-TS';
const javaRepo = 'Client-Java';
function cloneRepo(repo: string, dir: string, branch: string) {
child_process.execSync(`git clone ${repoOrg}/${repo} --single-branch -b ${branch} ${dir}`, {
stdio: 'inherit'
});
}
function updateRepo(cwd: string) {
child_process.execSync('git pull', {
stdio: 'inherit',
cwd
});
}
function runOnOs(exec: string, cwd?: string) {
const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
child_process.execSync(`${start} ${exec}`, {
stdio: 'inherit',
cwd
});
}
let config = {
rev: 'unset'
};
type RevInfo = {
description: string;
webclient?: boolean;
wip?: boolean;
clientBranch?: string;
}
const revInfo: Record<string, RevInfo> = {
'225': {
description: 'May 18, 2004',
webclient: true
},
'244': {
description: 'June 28, 2004',
webclient: true
},
'245.2': {
description: 'July 13, 2004 (there were 3 "245" builds!)',
webclient: true
},
'254': {
description: 'September 7, 2004',
webclient: true
},
'274': {
description: 'November 23, 2004',
wip: true
},
'377-wip': {
description: 'May 5, 2006',
wip: true,
clientBranch: '377'
}
};
let running = true;
async function main() {
if (!fs.existsSync('server.json')) {
await promptConfig();
}
config = JSON.parse(fs.readFileSync('server.json', 'utf8'));
if (!fs.existsSync('engine')) {
cloneRepo(engineRepo, 'engine', config.rev);
}
if (!fs.existsSync('content')) {
cloneRepo(contentRepo, 'content', config.rev);
}
if (revInfo[config.rev]?.webclient && !fs.existsSync('webclient')) {
cloneRepo(webRepo, 'webclient', config.rev);
}
if (!fs.existsSync('javaclient')) {
cloneRepo(javaRepo, 'javaclient', revInfo[config.rev]?.clientBranch ?? config.rev);
}
if (!fs.existsSync('engine/.env') && !fs.existsSync('engine/data/config/world.json')) {
child_process.spawnSync('bun install', {
shell: true,
stdio: 'inherit',
cwd: 'engine'
});
child_process.spawnSync('bun run setup', {
shell: true,
stdio: 'inherit',
cwd: 'engine'
});
}
const choice = await select({
message: 'What would you like to do?',
choices: [{
name: 'Start Server',
description: 'Starts the server normally',
value: 'start'
}, {
name: 'Update Source',
description: 'Pull the latest commits for all subprojects',
value: 'update'
},
revInfo[config.rev]?.webclient ? {
name: 'Run Web Client',
description: 'Opens your browser to play using the modern web client (TypeScript)',
value: 'web'
} : {
name: 'Run Web Client (unavailable)',
description: 'Not available in this version.',
value: ''
},
{
name: 'Run Java Client',
description: 'Opens the legacy Java applet to play using the original client',
value: 'java'
}, {
name: 'Advanced Options',
description: 'View more options',
value: 'advanced'
}, {
name: 'Quit',
description: '',
value: 'quit'
}]
}, { clearPromptOnDone: true });
if (choice === 'start') {
child_process.execSync('bun start', {
stdio: 'inherit',
cwd: 'engine'
});
} else if (choice === 'update') {
updateRepo('engine');
updateRepo('content');
updateRepo('webclient');
updateRepo('javaclient');
} else if (choice === 'web') {
if (!revInfo[config.rev]?.webclient) {
console.log('This version does not have a webclient available (yet?), sorry.');
} else if (process.platform === 'win32' || process.platform === 'darwin') {
runOnOs('http://localhost/rs2.cgi');
} else {
runOnOs('http://localhost:8888/rs2.cgi');
}
} else if (choice === 'java') {
const command = process.platform === 'win32' ? 'gradlew' : './gradlew';
if (config.rev === '225') {
child_process.execSync(`${command} run --args="10 0 highmem members"`, {
stdio: 'inherit',
cwd: 'javaclient'
});
} else {
child_process.execSync(`${command} run --args="10 0 highmem members 32"`, {
stdio: 'inherit',
cwd: 'javaclient'
});
}
} else if (choice === 'advanced') {
await promptAdvanced();
} else if (choice === 'quit') {
running = false;
}
}
async function promptConfig() {
const orderedRevs = Object.entries(revInfo);
orderedRevs.sort((a, b) => parseInt(a[0]) - parseInt(b[0])); // descending revs
orderedRevs.sort((a, b) => a[1].wip ? 1 : -1); // wip last
let choices = [];
for (const [rev, info] of orderedRevs) {
choices.push({
name: info.wip ? `${rev} (DEVELOPERS ONLY)` : rev,
value: rev,
description: info.description
});
}
const rev = await select({
message: 'What version are you interested in?',
choices
}, { clearPromptOnDone: true });
config.rev = rev;
fs.writeFileSync('server.json', JSON.stringify(config, null, 2));
}
async function promptAdvanced() {
const choice = await select({
message: 'What would you like to do?',
choices: [{
name: 'Start Server (engine dev)',
description: 'Starts the server and watches for .ts file changes to reload',
value: 'start-dev'
}, {
// todo:
// name: 'Reconfigure Server',
// description: 'Edit the environment config for the server',
// value: 'configure'
// }, {
name: 'Clean-build Server',
description: '',
value: 'clean-build'
},
revInfo[config.rev]?.webclient ? {
name: 'Build Web Client',
description: '',
value: 'build-web'
} : {
name: 'Build Web Client (unavailable)',
description: 'Not available in this version.',
value: ''
},
{
name: 'Build Java Client',
description: '',
value: 'build-java'
}, {
name: 'Change Version',
description: 'THIS OPTION WILL DESTROY YOUR WORKING FOLDER AND CREATE A NEW ONE.',
value: 'change-version'
}, {
name: 'Back',
description: 'Go back',
value: 'back'
}]
}, { clearPromptOnDone: true });
if (choice === 'start-dev') {
child_process.execSync('bun run dev', {
stdio: 'inherit',
cwd: 'engine'
});
} else if (choice === 'configure') {
// todo: has issues with input appearing right now
child_process.spawnSync('bun run setup', {
shell: true,
stdio: 'inherit',
cwd: 'engine'
});
} else if (choice === 'clean-build') {
child_process.execSync('bun run clean', {
stdio: 'inherit',
cwd: 'engine'
});
child_process.execSync('bun run build', {
stdio: 'inherit',
cwd: 'engine'
});
} else if (choice === 'build-web') {
child_process.execSync('bun run build', {
stdio: 'inherit',
cwd: 'webclient'
});
fs.copyFileSync('webclient/out/client.js', 'engine/public/client/client.js');
} else if (choice === 'build-java') {
const command = process.platform === 'win32' ? 'gradlew' : './gradlew';
child_process.execSync(`${command} build`, {
stdio: 'inherit',
cwd: 'javaclient'
});
} else if (choice === 'change-version') {
await promptConfig();
fs.rmSync('engine', { recursive: true, force: true });
fs.rmSync('content', { recursive: true, force: true });
fs.rmSync('webclient', { recursive: true, force: true });
fs.rmSync('javaclient', { recursive: true, force: true });
}
}
try {
while (running) {
await main();
}
} catch (e) {
if (e instanceof ExitPromptError) {
process.exit(0);
} else if (e instanceof Error) {
if (e.message.startsWith('Command failed:')) {
process.exit(0);
}
console.log(e.message);
}
}