Skip to content

Commit 01ca108

Browse files
committed
Custom palettes allowed
1 parent 387fd92 commit 01ca108

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Changelog
22

33
## [1.0.0]
4-
- New mapping/DPLC engine
5-
- Real custom mappings
4+
- New engine
5+
- Real custom mappings & DPLCs
6+
- Custom art formats (including compression)
67
- Sonic 2 Special Stage added [#21](/../../issues/21)
78
- Sonic 3&K Sonic/Supersonic partial support
89
- New ASM parser/writer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ As of version 1.0.0, Flex 2 supports a wider array of formats and allows you to
8888

8989
The base formats are provided in the `scripts/` directory. These can be modified to suit whatever format you decide to come up with.
9090

91-
You can provide custom mapping formats, DPLCs formats, art formats (including compression) and palette formats.
91+
You can provide custom mapping formats, DPLCs formats, art formats (including compression), and palette formats.
9292

9393
The definition file format is currently undocumented, and still being expanded on. If you have a request to add support for a new disassembly, or just want more information on the format - open an issue on github with your request.

app/components/file/file-object.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export const FileObject = observer(({ obj }) => {
3434

3535
const scriptDPLCs = scriptSafe && script.DPLCs;
3636
const scriptArt = scriptSafe && script.art;
37+
const scriptPalettes = scriptSafe && script.palettes;
3738
const toggleDPLCs = () => (obj.dplcs.enabled = !obj.dplcs.enabled);
3839

39-
4040
function ioWrap(filePath, setError, e, cb) {
4141
setError();
4242
if (scriptSafe && filePath) {
@@ -116,9 +116,16 @@ export const FileObject = observer(({ obj }) => {
116116
obj.dplcs.enabled &&
117117
environment.dplcs.length < mappings.sprites.length
118118
) {
119-
environment.dplcs.push(...Array.from({
120-
length: mappings.sprites.length - environment.dplcs.length,
121-
}, () => []));
119+
environment.dplcs.push(
120+
...Array.from(
121+
{
122+
length:
123+
mappings.sprites.length -
124+
environment.dplcs.length,
125+
},
126+
() => [],
127+
),
128+
);
122129
}
123130
});
124131
}
@@ -130,8 +137,7 @@ export const FileObject = observer(({ obj }) => {
130137
if (!mappingsASM) {
131138
await fs.writeFile(path, writeBIN(mappings));
132139
} else {
133-
const label =
134-
obj.mappings.label || 'Mappings';
140+
const label = obj.mappings.label || 'Mappings';
135141
await fs.writeFile(path, writeASM(label, mappings));
136142
}
137143
});
@@ -159,8 +165,7 @@ export const FileObject = observer(({ obj }) => {
159165
if (!dplcsASM) {
160166
await fs.writeFile(path, writeBIN(dplcs));
161167
} else {
162-
const label =
163-
obj.dplcs.label || 'DPLCS';
168+
const label = obj.dplcs.label || 'DPLCS';
164169
await fs.writeFile(path, writeASM(label, dplcs));
165170
}
166171
});
@@ -180,12 +185,11 @@ export const FileObject = observer(({ obj }) => {
180185
const path = isAbsolute
181186
? palPath
182187
: workspace.absolutePath(palPath);
183-
buffersToColors([
184-
{
185-
buffer: await fs.readFile(path),
186-
length,
187-
},
188-
]).forEach((line) => {
188+
189+
(scriptPalettes ? script.readPalettes : buffersToColors)({
190+
buffer: await fs.readFile(path),
191+
length,
192+
}).forEach((line) => {
189193
if (cursor < 4) {
190194
environment.palettes[cursor] = line;
191195
cursor++;
@@ -208,7 +212,9 @@ export const FileObject = observer(({ obj }) => {
208212
? palPath
209213
: workspace.absolutePath(palPath);
210214

211-
const chunk = colorsToBuffers(
215+
const chunk = (scriptPalettes
216+
? script.writePalettes
217+
: colorsToBuffers)(
212218
environment.palettes,
213219
cursor,
214220
cursor + length,

app/formats/palette.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ export function hexToMDHex(color) {
1515
.join``;
1616
}
1717

18-
export function buffersToColors(list) {
18+
export function buffersToColors({ buffer, length }) {
1919
let colors = [];
20-
list.forEach(({buffer, length}) => {
21-
const data = Uint8Array.from(buffer);
22-
for (let i = 0; i < length * 32; i+=2) {
23-
if (data.length <= i) {
24-
throw new Error('Trying to load more palettes than exist');
25-
}
26-
const [b, gr] = [
27-
data[i].toString(16),
28-
data[i+1].toString(16).padStart(2, '0'),
29-
];
30-
const [g, r] = [...gr];
31-
colors.push(`#${r}${g}${b}`);
3220

21+
const data = Uint8Array.from(buffer);
22+
for (let i = 0; i < length * 32; i+=2) {
23+
if (data.length <= i) {
24+
throw new Error('Trying to load more palettes than exist');
3325
}
34-
});
26+
const [b, gr] = [
27+
data[i].toString(16),
28+
data[i+1].toString(16).padStart(2, '0'),
29+
];
30+
const [g, r] = [...gr];
31+
colors.push(`#${r}${g}${b}`);
32+
33+
}
3534

3635
return chunk(colors, 16).splice(0, 4);
3736
}

app/formats/scripts/run-script.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export default catchFunc((file) => {
122122
const [artArgs, artFunc] = useDef();
123123
const [mappingArgs, mappingFunc] = useDef();
124124
const [dplcArgs, dplcFunc] = useDef();
125+
const [paletteArgs, paletteFunc] = useDef();
125126

126127
(new Function('Flex2', loadScript(file)))({
127128
...constants,
@@ -310,5 +311,15 @@ export default catchFunc((file) => {
310311
});
311312
}
312313

314+
if (paletteArgs[0]) {
315+
const [readPalettes, writePalettes] = paletteArgs[0];
316+
Object.assign(methods, {
317+
palettes: true,
318+
readPalettes,
319+
writePalettes,
320+
});
321+
}
322+
323+
313324
return methods;
314325
});

0 commit comments

Comments
 (0)