-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtune.js
More file actions
233 lines (205 loc) · 5.42 KB
/
tune.js
File metadata and controls
233 lines (205 loc) · 5.42 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
"use strict";
const tune =
{
viewer: undefined,
hex: [],
hexStr: "",
index: undefined,
findID: undefined,
saved: true, // did user download data after modifying it?
// copy file hex and convert it to string
// file [fileList]: file that
loadFile: async function(file)
{
// create and save hex viewer
this.viewer = new Uint8Array(await simFile.read(file[0], "readAsArrayBuffer"));
// check magic bytes at start of file
let success = false;
if (file[0].name.includes("TableTune") && this.viewer[0] === 65 && this.viewer[1] === 76)
{
// if magic bytes spell "ALAR"
if (this.viewer[2] === 65 && this.viewer[3] === 82)
{
// hooray! it passed the check!
success = true;
}
// else, if magic bytes spell "ALLZ"
else if (this.viewer[2] === 76 && this.viewer[3] === 90)
{
alert("Hey, so... It appears like the TableTune file is currently compressed with ALLZ. Run \"Aqualead_LZSS\" on the file and input it again.");
}
else { alert("Hey! This file doesn't seem to be \"TableTune.aar\" like I asked for!"); }
}
else { alert("Hey! This file doesn't seem to be \"TableTune.aar\" like I asked for!"); }
if (!success)
{
this.viewer = undefined;
this.hex = [];
this.hexStr = "";
tuneState.checked = false;
tuneHide.classList.add("active");
return;
}
// for each byte in hex, check if they are 2 characters long and add a zero if not
this.hex = [];
this.viewer.forEach(currentValue =>
{
const str = currentValue.toString(16);
if (str.length === 1)
{
this.hex.push("0" + str);
}
else
{
this.hex.push(str);
}
});
this.hexStr = this.hex.join(" ");
// set file as saved
tuneState.checked = true;
tuneHide.classList.remove("active");
},
// find song ID in hex and set its offset to this.index
// diffSet [bool]: should the value of <input> elements be changed?
find: function(diffSet)
{
if (this.findID !== tuneID.value)
{
//let hex;
let index2 = 0;
let str;
// if the song ID was converted to hex, would it be 2 bytes long?
if (tuneID.value > 255)
{
// take the first digit in hex and place it after the other 2
// E.g. if ID is 291, it would be 123 in hex
// 23 01 00 00
const hex = Number(tuneID.value).toString(16);
str = hex.slice(1) + " 0" + hex.charAt(0) + " 00 00";
}
else
{
// just add 3 empty bytes after
str = Number(tuneID.value).toString(16) + " 00 00 00";
}
// loop until this.index is out of bounds
this.index = 0;
while (this.index !== -1)
{
// find song ID string in hex
this.index = this.hexStr.indexOf(str, index2);
// if it could find a match...
if (this.index !== -1)
{
this.index = this.index / 3;
// for each of the 4 difficulties...
let success = true;
for (let i = 0; i < 16; i += 4)
{
// if number is between 0 and 15 and is followed by 3 empty bytes
if (this.viewer[this.index + 32 + i] > -1 &&
this.viewer[this.index + 32 + i] < 16 &&
this.viewer[this.index + 33 + i] === 0 &&
this.viewer[this.index + 34 + i] === 0 &&
this.viewer[this.index + 35 + i] === 0)
{
// hooray! it passed the check!
// no i'm not going to re-write this code
}
// if it did not pass the above checks...
else
{
// exit checks, set as failed
success = false;
i = 99;
}
}
// if successful, exit loop
if (success)
{
break;
}
// else, repeat and find next match
else
{
this.index = this.index * 3;
index2 = this.index + 1;
}
}
}
// if match was able to be found
if (this.index !== -1)
{
if (diffSet)
{
// set <input> values to hex data
tuneE.value = this.viewer[this.index + 32];
tuneN.value = this.viewer[this.index + 36];
tuneH.value = this.viewer[this.index + 40];
tuneX.value = this.viewer[this.index + 44];
}
}
// else, if no match was found
else
{
tuneE.value = -1;
tuneN.value = -1;
tuneH.value = -1;
tuneX.value = -1;
alert("ID not found :(");
}
this.findID = tuneID.value;
}
},
// replace difficulty bytes with ones specified in <input>s
replace: function()
{
// if the ID was found already
if (this.index !== -1)
{
// throw error if song ID isn't tied to any valid ST ID.
if (!STList.includes(Number(tuneID.value)))
{
if (!confirm("Hmm... The song ID doesn't seem to be tied to any song in ST.\n\nContinue anyways?"))
{
return;
}
}
let count = 0;
if (this.viewer[this.index + 32] !== Number(tuneE.value))
{
this.viewer[this.index + 32] = Number(tuneE.value);
count++;
}
if (this.viewer[this.index + 36] !== Number(tuneN.value))
{
this.viewer[this.index + 36] = Number(tuneN.value);
count++;
}
if (this.viewer[this.index + 40] !== Number(tuneH.value))
{
this.viewer[this.index + 40] = Number(tuneH.value);
count++;
}
if (this.viewer[this.index + 44] !== Number(tuneX.value))
{
this.viewer[this.index + 44] = Number(tuneX.value);
count++;
}
if (count !== 0)
{
this.saved = false;
tuneSaved.style.display = "block";
}
}
},
download: function()
{
// download file
simFile.download(tune.viewer, "TableTune.aar", "application/octet-stream");
// set file as saved
this.saved = true;
tuneSaved.style.display = "none";
}
};
// lemocha - lemocha7.github.io