-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
134 lines (104 loc) · 5.31 KB
/
script.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
async function checkFileExtension() {
const fileStatus = document.getElementById('fileStatus');
fileStatus.innerHTML = 'Checking file extension...<br>';
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const fileName = file.name;
const fileExtension = fileName.split('.').pop();
const folderName = fileName.split('.').slice(0, -1).join('.');
if (fileExtension === 'zip') {
var zip = new JSZip();
try {
fileStatus.innerHTML += 'Unzipping file...<br>';
await zip.loadAsync(file);
// console.log(zip)
// check if zip file contains a file with the name "${folderName}/level.dat0"
const bLevelDatExists = zip.file(`${folderName}/level.dat0`) !== null;
if(!bLevelDatExists){
console.log("The .zip file does not contain a world save")
fileStatus.innerHTML += 'Error: The .zip file does not contain a world save<br>';
return "The .zip file does not contain a world save";
}
// list all level.dat files in the zip file
var levelDatFiles = zip.file(/level\.dat\d+$/);
// console.log(levelDatFiles)
// console.log(`Found ${levelDatFiles.length} level.dat files in the .zip file`);
fileStatus.innerHTML += `Found ${levelDatFiles.length} level.dat files in the .zip file<br>`;
if(levelDatFiles.length < 1){
console.log("The .zip doesn't contain a level.datX file")
return "The .zip doesn't contain a level.datX file";
}
// read all level.dat files to byte array (like it would be read using fs.readFileSync)
var levelDatFilesByteArray = [];
for(let levelDatFile of levelDatFiles){
// content should be hex array
const content = await levelDatFile.async("uint8array");
// const HexArray = Array.from(content, byte => byte.toString(16).padStart(2, '0'));
// const HexBuffer = HexArray.join('');
var inflatedLevelDat
try{
console.log(`Inflating ${levelDatFile.name}`)
fileStatus.innerHTML += `Inflating ${levelDatFile.name}<br>`;
inflatedLevelDat = pako.inflate(content);
// console.log(inflatedLevelDat)
// convert inflatedLevelDat to hex string without Buffer becuase this is a browser
const hexBuffer = new Uint8Array(inflatedLevelDat);
var hex = Array.from(hexBuffer, byte => byte.toString(16).padStart(2, '0')).join('');
// convert hex to ansii string
const outputString = hex.match(/.{1,2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
if(!outputString.includes("command-ran"))
continue;
// search all hex of: "FF FF 00 01 00"
// search hex
// while hexBuffer has 0xFF 0xFF 0x00 0x01 0x00\
console.log(hex.indexOf('ffff000100'));
while(hex.indexOf('ffff000100') !== -1) {
hex = hex.replace(/ffff000100/g, 'ffff000000')
console.log(`[+] Removed cheat flag from ${levelDatFile.name}`)
fileStatus.innerHTML += `Removed cheat flag from ${levelDatFile.name}<br>`;
}
console.log(hex.indexOf('ffff000100'));
// convert hex back to Uint8Array
inflatedLevelDat = new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
// pako deflate
const deflatedLevelDat = pako.deflate(inflatedLevelDat);
console.log(deflatedLevelDat)
// write deflatedLevelDat to zip file
zip.file(`${levelDatFile.name}`, deflatedLevelDat);
console.log(zip.file(levelDatFile.name))
zip = await zip.generateAsync({type:"blob"});
// create download link and click it
}catch(e) {
console.log(e)
fileStatus.innerHTML += `Error inflating ${levelDatFile.name}<br>`;
return "Error inflating level.dat file";
}
levelDatFilesByteArray.push({name: levelDatFile.name, content: content, inflated: inflatedLevelDat});
}
// console
console.log(`Found and changed ${levelDatFilesByteArray.length} level.dat files in the .zip file`);
console.log(`The first level.dat file is:`)
// console.log(levelDatFilesByteArray[0].inflated); // Sometimes it can't find inflated
// html
fileStatus.innerHTML += `Found and changed ${levelDatFilesByteArray.length} level.dat files in the .zip file<br>`;
} catch (error) {
fileStatus.innerHTML += 'Error reading the .zip file<br>';
console.error('Error reading the .zip file:');
console.log(error);
return "Error reading the .zip file";
}
} else {
console.log('The file is not a .zip file');
return "The file is not a .zip file";
}
// create new blob and log url
const blob = new Blob([zip], {type: "application/zip"});
const url = URL.createObjectURL(blob);
// create download link and click it
var downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = `${folderName}.zip`;
fileStatus.innerHTML += `<a id="download" href="${url}">Download new Savegame</a><br>`;
document.body.appendChild(downloadLink);
console.log(`Download: ${url}`);
}