-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
449 lines (399 loc) · 12 KB
/
index.html
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Otu! Map Importer</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
body {
background: rgb(43, 41, 41)
}
.centered {
position: fixed;
flex-wrap: wrap;
font-family: "Roboto", sans-serif;
color: white;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
transition: transform 0.5s ease;
/* Smooth transition on transform */
}
.select {
position: relative;
align-items: center;
background: #FFFFFF;
border: 0 solid #E2E8F0;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
box-sizing: border-box;
color: #1A202C;
display: inline-flex;
font-family: Inter, sans-serif;
font-size: 1rem;
font-weight: 700;
height: 56px;
justify-content: center;
line-height: 24px;
overflow-wrap: break-word;
padding: 24px;
text-decoration: none;
width: auto;
border-radius: 8px;
cursor: pointer;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
flex-basis: 65%;
}
#select-song {
flex-basis: 74%;
}
#fileInput {
display: none;
}
body {
margin: 0;
padding: 0;
}
.bar {
width: 33.33%;
height: 5px;
background-color: lightgrey;
position: fixed;
top: 0;
overflow: hidden;
/* Hides overflow so the animation only shows within the bar */
}
.bar+.bar {
margin-left: 2px;
}
#bar1 {
left: 0;
position: relative;
/* Use relative position for absolute positioning of pseudo-element */
}
#bar2 {
left: calc(33.33% + 2px);
}
#bar3 {
left: calc(66.66% + 6px);
}
@keyframes expandLine {
0% {
width: 0;
}
100% {
width: 100%;
}
}
.page2 {
transform: translateX(calc(100vw));
}
.page3 {
transform: translateX(calc(200vw));
}
.input {
max-width: 190px;
background-color: #f5f5f5;
color: #242424;
padding: .15rem .5rem;
min-height: 40px;
border-radius: 4px;
outline: none;
border: none;
line-height: 1.15;
box-shadow: 0px 10px 20px -18px;
}
.input:focus {
border-bottom: 2px solid #5b5fc7;
border-radius: 4px 4px 2px 2px;
}
.input:hover {
outline: 1px solid lightgrey;
}
.page-text {
flex-basis: 65%;
}
.center-header {
flex-basis: 100%;
transform: translateX(32vw);
}
</style>
</head>
<body>
<!-- progress bar -->
<div id="bar1" class="bar"></div>
<div id="bar2" class="bar"></div>
<div id="bar3" class="bar"></div>
<!-- file input -->
<input id="fileInput" type="file" />
<div class="centered page1">
<img src="otulogo.png" width="200px" height="200px" />
<h3> Map Importer</h3>
<button id="select-osu" class="select" role="button">Select .osu File</button>
</div>
<div class="centered page2">
<h1> Song Selection </h1>
<button id="select-song" class="select" role="button">Select .mp3/.ogg File</button>
</div>
<div class="centered page3">
<h1 class="center-header"> Advanced Settings </h1>
<p class="page-text"> Level Name (if not filled, most recent level will be written to) </p>
<input class="input" id="level-name" />
<p class="page-text"> Custom Song ID (if not filled, an unused song ID will be chosen automatically) </p>
<input class="input" type="number" id="song-id" onkeyup="enforceMinMax(this)" max="9999999" />
<button id="finish" class="select" role="button"> Import map into GD </button>
</div>
<script>
const { ipcRenderer } = require('electron');
const { exec } = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
let cmdOutput = '';
ipcRenderer.on('output-child', (event, code) => {
code = Array.from(code).map(x => String.fromCharCode(x)).join('');
cmdOutput += code;
});
function enforceMinMax(el) {
if (el.value != "") {
if (parseInt(el.value) < parseInt(el.min)) {
el.value = el.min;
}
if (parseInt(el.value) > parseInt(el.max)) {
el.value = el.max;
}
}
}
function hasValidExtension(filePath, accepts) {
// Extract extensions from the `accepts` attribute string
const extensions = accepts.split(',').map(ext => ext.trim().replace('.', ''));
// Create a regex pattern from the extensions list
const pattern = new RegExp(`\\.(${extensions.join('|')})$`, 'i');
// Test if the file path matches the pattern
return pattern.test(filePath);
}
let readFilePath;
function readFile(buffer = false, accept = '*', filePath = false) {
return new Promise((resolve, reject) => {
// Create an input element
const fileInput = document.getElementById('fileInput');
fileInput.setAttribute('accept', accept);
// Trigger the file explorer
fileInput.click();
// Event listener for when a file is selected
fileInput.onchange = event => {
const file = event.target.files[0];
if (accept !== '*' && !hasValidExtension(file.path, accept)) {
alert(`Only files ending with ${accept} can be used! Try using a file ending with ${accept} instead.`);
reject(false);
return;
}
readFilePath = file.path;
if (file && !filePath) {
const reader = new FileReader();
reader.onload = function (e) {
resolve(buffer ? Buffer.from(e.target.result) : e.target.result);
};
reader.onerror = function (e) {
reject(e);
};
// Read the file as a string
buffer ? reader.readAsArrayBuffer(file) : reader.readAsText(file);
return;
} else {
resolve(file.path);
}
};
});
}
let bars = ['#bar1', '#bar2', '#bar3'];
let pages = ['.page1', '.page2', '.page3'];
let index = 0;
let step = (movePage = true) => {
if (index == 3) return;
let element = document.querySelector(bars[index]);
let page = document.querySelector(pages[index]),
nextpage = document.querySelector(pages[index + 1]);
// Create a pseudo-element dynamically
let pseudoElement = document.createElement('div');
// Apply styles to the pseudo-element
pseudoElement.style.content = "''";
pseudoElement.style.position = 'absolute';
pseudoElement.style.top = '0';
pseudoElement.style.left = '0';
pseudoElement.style.width = '0';
pseudoElement.style.height = '100%';
pseudoElement.style.backgroundColor = 'rgb(128, 55, 191)';
pseudoElement.style.animation = 'expandLine 0.5s ease forwards';
// Add the pseudo-element to the specified element
element.appendChild(pseudoElement);
if (movePage) {
page.style.transform = 'translateX(calc(-100vw))';
nextpage.style.transform = 'translateX(calc(0vw))';
}
index++
}
const checkIfProcessIsRunning = (exeName) => {
return new Promise((resolve, reject) => {
exec('tasklist', (err, stdout, stderr) => {
if (err) {
reject(`Error executing tasklist: ${err}`);
return;
}
if (stderr) {
reject(`Error: ${stderr}`);
return;
}
const processes = stdout.split('\n');
const isRunning = processes.some(process => process.toLowerCase().includes(exeName.toLowerCase()));
resolve(isRunning);
});
});
};
/**
* Compare buffer with files in the directory using SHA256 hash sums.
*
* @param {Buffer} buffer - The input buffer to compare.
* @param {string} directory - The directory containing the files.
* @returns {Promise<boolean>} - Returns a promise that resolves to true if at least one file has the same contents, otherwise false.
*/
async function hasMatchingFile(buffer, directory) {
// Generate SHA256 hash for the input buffer
const bufferHash = crypto.createHash('sha256').update(buffer).digest('hex');
// Read all files in the directory
let files = await fs.promises.readdir(directory);
files = files.filter(x => hasValidExtension(x, '.mp3, .ogg') && x.startsWith('999'));
for (const file of files) {
const filePath = path.join(directory, file);
// Ensure the path is a file
if ((await fs.promises.lstat(filePath)).isFile()) {
const fileHash = await hashFile(filePath);
// Compare hashes
if (fileHash === bufferHash) {
return file;
}
}
}
return false;
}
/**
* Hashes a file using SHA256.
*
* @param {string} filePath - The path of the file to hash.
* @returns {Promise<string>} - The SHA256 hash of the file.
*/
function hashFile(filePath) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const stream = fs.createReadStream(filePath);
stream.on('data', chunk => {
hash.update(chunk);
});
stream.on('end', () => {
resolve(hash.digest('hex'));
});
stream.on('error', err => {
reject(err);
});
});
}
let osuFile;
let mp3buf;
document.querySelector('#select-osu').addEventListener('click', async () => {
osuFile = await readFile(false, ".osu", true);
step();
});
document.querySelector('#select-song').addEventListener('click', async () => {
mp3buf = await readFile(true, '.mp3, .ogg');
step();
});
function getRandomFourDigitInteger() {
return Math.floor(1000 + Math.random() * 9000).toString();
}
let gd_path = `${process.env.localappdata}\\GeometryDash`;
let songs = fs.readdirSync(gd_path).filter(x => hasValidExtension(x, '.mp3, .ogg'));
let genRandID = () => {
let randDigit = '999' + getRandomFourDigitInteger();
while (songs.includes(randDigit)) {
randDigit = getRandomFourDigitInteger();
};
return parseInt(randDigit);
};
let finish = document.querySelector('#finish');
let interval;
let import_loop = () => {
let arrs = Array(3).fill(0).map((_, i) => 'Importing file (this may take a bit)' + '.'.repeat(i + 1));
let arr_ind = 0;
interval = setInterval(() => {
if (arr_ind == 3) arr_ind = 0;
finish.innerText = arrs[arr_ind];
arr_ind++;
}, 250);
};
let stop_anim = () => {
finish.style.background = '#FFFFFF';
finish.innerText = "Import map into GD";
finish.style.cursor = "pointer";
clearInterval(interval);
};
let importOsu = (...args) => {
return new Promise((resolve, reject) => {
console.log(args)
ipcRenderer.send('spawn-process', args.filter(x => x !== undefined));
ipcRenderer.on('finished-write', (event, code) => {
if (code) {
alert("Import process has failed. Output from child process (only useful for debugging): " + cmdOutput);
stop_anim();
reject(false);
}
resolve(true);
});
});
}
let complete = async () => {
import_loop();
finish.style.background = 'lightgrey';
let isGDOpen = await checkIfProcessIsRunning('GeometryDash.exe');
if (isGDOpen) {
alert("Geometry Dash is currently open! Geometry Dash must be closed during the importing process, so please close GeometryDash.exe and try again.");
stop_anim();
return;
};
let levelName = document.querySelector('#level-name').value || undefined;
let songID = document.querySelector('#song-id').value || genRandID();
finish.style.cursor = "wait";
finish.disabled = true;
let fileExt = readFilePath.slice(readFilePath.length - 4, readFilePath.length);
console.log('gay', fileExt)
let full_song_path = path.join(gd_path, songID + fileExt);
let existsAlready = await hasMatchingFile(mp3buf, gd_path);
let replaceMap = true;
if (!existsAlready) fs.writeFileSync(full_song_path, mp3buf);
else {
console.log('file path set to an existing file', existsAlready);
songID = existsAlready.slice(0, -4);
full_song_path = path.join(gd_path, existsAlready);
}
try {
await importOsu(osuFile, songID, replaceMap, levelName);
} catch (err) {
console.log('err', err);
finish.disabled = false;
return;
};
clearInterval(interval);
stop_anim();
finish.disabled = false;
step(false);
setTimeout(() => {
alert('Osu! map has been imported into GD succesfully. This application will now close.');
window.close();
}, 500);
};
finish.onclick = complete;
</script>
</body>
</html>