Skip to content

Commit 2e0ae72

Browse files
committed
refactored asm aprser
1 parent 3939dd1 commit 2e0ae72

5 files changed

Lines changed: 86 additions & 82 deletions

File tree

flex2.idea

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ lil-g/gemini
3333

3434
====
3535

36-
when saving, if 0 appears anywhere in the header use its offset for blank sprites
36+
object collapse state persistance
3737
when saving asm, use the object name as the label name :D
38-
divide tile index by 2 for sonic 2 2 player mappings
3938
create new file | newFactory={(path)=>{}}
40-
41-
bug: try and load dplcs with none there

modules/formats/asm.js

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import chunk from 'lodash/chunk';
2-
import flatten from 'lodash/flatten';
3-
41
const sizeLookup = {
52
'b': 1,
63
'w': 2,
@@ -13,72 +10,72 @@ export function asmToBin(buffer) {
1310
.replace(/\$|even|(;(.*?)$)/gm, '') // remove comments / even / $ (assume no decimal)
1411
.replace(/(^\s*$)/gm, ''); // remove empty lines
1512

16-
const sections = asm.split(/^(.*?):/gm);
17-
// header contains a - symbol (assume no negatives in data sections)
18-
const headerIndex = sections.findIndex((f) => f.indexOf('-') != -1);
19-
const headers = sections[headerIndex];
13+
// split into labels/data
14+
// double comment char used to indicate start of line
15+
const sections = (asm
16+
.replace(/^\S/gm, (d) => `;;${d}`)
17+
.replace(/\n/gm, '') + ';')
18+
.match(/;.*?:.*?;/g)
19+
.map((d) => d.replace(/;/g, '').split(':'));
20+
21+
// calculate pointer for each label
22+
let dataIndex = 0;
23+
const pointerMap = {};
24+
sections.forEach(([label, data]) => {
25+
pointerMap[label] = dataIndex;
26+
// insert newlines to split on
27+
const lines = data.split('dc').join('\ndc').split('\n');
28+
lines.forEach((line) => {
29+
const sizeMatch = line.match(/dc\.(b|w|l)/);
30+
if (sizeMatch) {
31+
const size = sizeLookup[sizeMatch[1]];
32+
const fragments = line.split(',');
33+
dataIndex += size * fragments.length;
34+
}
35+
});
36+
});
2037

21-
const dataSections = chunk(sections.splice(headerIndex + 1), 2)
22-
.map(([label, data]) => {
23-
const lines = data.split('\n');
24-
let bytes = [];
25-
lines.forEach((line) => {
26-
const sizeMatch = line.match(/dc\.(b|w|l)/);
27-
if (sizeMatch) {
28-
const size = sizeLookup[sizeMatch[1]];
29-
const fragments = line.replace(/dc\.(b|w|l)|\s/g, '').split(',');
30-
// save each fragment into byte array based on size
31-
fragments.forEach((fragment) => {
38+
const bytes = [];
39+
40+
// now just convert the data sections
41+
asm.replace(/^(.*?):/gm, '')
42+
.split('\n')
43+
.forEach((line) => {
44+
const sizeMatch = line.match(/dc\.(b|w|l)/);
45+
if (sizeMatch) {
46+
const size = sizeLookup[sizeMatch[1]];
47+
const fragments = line.replace(/dc\.(b|w|l)|\s/g, '').split(',');
48+
49+
// save each fragment into byte array based on size
50+
fragments.forEach((fragment) => {
51+
if (~fragment.indexOf('-')) {
52+
// if data is calculated from labels
53+
const [lVal, rVal] = fragment.split('-');
54+
let pointer = (pointerMap[lVal] - pointerMap[rVal]);
55+
let pointerBytes = [];
56+
for (let i = 0; i < size; i++) {
57+
pointerBytes.unshift(pointer & 0xFF);
58+
pointer = pointer >> 8;
59+
}
60+
bytes.push(...pointerBytes);
61+
}
62+
else {
3263
let hex = parseInt(fragment, 16);
3364
let fragmentBytes = [];
3465
for (let i = 0; i < size; i++) {
3566
fragmentBytes.unshift(hex & 0xFF);
3667
hex = hex >> 8;
3768
}
3869
bytes.push(...fragmentBytes);
39-
});
40-
}
41-
});
70+
}
71+
});
4272

43-
return {
44-
label,
45-
bytes,
46-
};
47-
});
48-
49-
// assume word sized headers
50-
const headersList = headers
51-
.replace(/dc\.w/gm, '') // remove data annotation
52-
.replace(/\n/gm, ',') // change \n to comma
53-
.replace(/\s/gm, '') // strip whitespace
54-
.replace(/,$|^,/g, '') // strip trailing/beginning comma
55-
.split(','); // split by comma
56-
57-
const headerSize = headersList.length * 2; // bytes
58-
59-
const headerBytes = [];
60-
61-
headersList.forEach((header) => {
62-
const dashIndex = header.indexOf('-');
63-
if (dashIndex == -1) {
64-
const value = parseInt(header, 16);
65-
headerBytes.push(value >> 8, value & 0xFF);
66-
}
67-
else {
68-
const label = header.slice(0, dashIndex);
69-
let value = headerSize;
70-
for (let i = 0; i < dataSections.length; i++) {
71-
if (label == dataSections[i].label) {
72-
break;
73-
}
74-
value += dataSections[i].bytes.length;
7573
}
74+
});
7675

77-
headerBytes.push(value >> 8, value & 0xFF);
78-
}
79-
});
80-
81-
const dataBytes = flatten(dataSections.map((d) => d.bytes));
76+
return bytes;
77+
}
8278

83-
return [...headerBytes, ...dataBytes];
79+
export function stuffToAsm(headers, frames, name) {
80+
return '';
8481
}

modules/formats/dplc.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ export function DPLCsToBuffer(dplcs, format) {
103103
});
104104

105105
const headerWords = headers.map((num) => numberToByteArray(num, 2));
106+
const framesArray =frames.map(({header, pieces}) => [header, pieces]);
106107

107-
const bytes = flattenDeep([
108-
headerWords,
109-
frames.map(({header, pieces}) => [header, pieces]),
110-
]);
108+
const bytes = flattenDeep([headerWords, framesArray]);
111109

112-
return new Buffer(Uint8Array.from(bytes));
110+
return {
111+
chunk: new Buffer(Uint8Array.from(bytes)),
112+
headers: headerWords,
113+
frames: framesArray,
114+
};
113115

114116
}

modules/formats/mapping.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,21 @@ export function mappingsToBuffer(mappings, format) {
159159

160160
const headerWords = headers.map((num) => numberToByteArray(num, 2));
161161

162+
const framesArray = frames.map(({header, pieces}) => {
163+
return (zeroHeader && header == 0)
164+
? []
165+
: [numberToByteArray(header, headerSize), pieces];
166+
});
167+
162168
const bytes = flattenDeep([
163169
headerWords,
164-
frames.map(({header, pieces}) => {
165-
return (zeroHeader && header == 0)
166-
? []
167-
: [numberToByteArray(header, headerSize), pieces];
168-
}),
170+
framesArray,
169171
]);
170172

171-
return new Buffer(Uint8Array.from(bytes));
173+
return {
174+
chunk: new Buffer(Uint8Array.from(bytes)),
175+
headers: headerWords,
176+
frames: framesArray,
177+
};
172178

173179
}

modules/store/environment.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { bufferToTiles, tilesToBuffer } from '#formats/art';
1111
import { bufferToMappings, mappingsToBuffer } from '#formats/mapping';
1212
import { bufferToDPLCs, DPLCsToBuffer } from '#formats/dplc';
1313
import { buffersToColors, colorsToBuffers, defaultPalettes } from '#formats/palette';
14-
import { asmToBin } from '#formats/asm';
14+
import { asmToBin, stuffToAsm } from '#formats/asm';
1515
import { arrayMove } from 'react-sortable-hoc';
1616

1717
class Environment {
@@ -126,7 +126,7 @@ class Environment {
126126
this.mappings.replace([]);
127127
}
128128
// load DPLCs
129-
this.config.dplcsEnabled = obj.dplcs.enabled == true;
129+
this.config.dplcsEnabled = obj.dplcs.enabled == true && obj.dplcs.path;
130130
if (this.config.dplcsEnabled && obj.dplcs.path) {
131131
const dplcPath = workspace.absolutePath(obj.dplcs.path);
132132
const isAsm = extname(obj.dplcs.path) == '.asm';
@@ -196,21 +196,23 @@ class Environment {
196196
// mappings
197197
if (obj.mappings.path) {
198198
const mappingPath = workspace.absolutePath(obj.mappings.path);
199-
// const isAsm = extname(obj.mappings.path) == '.asm';
199+
const isAsm = extname(obj.mappings.path) == '.asm';
200200

201-
const chunk = mappingsToBuffer(this.mappings, obj.mappingDefinition);
202-
writeFile(mappingPath, chunk, (err, success) => {
201+
const { chunk, headers, frames } = mappingsToBuffer(this.mappings, obj.mappingDefinition);
202+
const out = isAsm ? stuffToAsm(headers, frames, obj.name) : chunk;
203+
writeFile(mappingPath, out, (err, success) => {
203204
err && errorMsg('Error Saving Mappings', err.message);
204205
});
205206
}
206207

207208
// dplcs
208209
if (obj.dplcs.enabled && obj.dplcs.path) {
209210
const dplcPath = workspace.absolutePath(obj.dplcs.path);
210-
// const isAsm = extname(obj.mappings.path) == '.asm';
211+
const isAsm = extname(obj.dplcs.path) == '.asm';
211212

212-
const chunk = DPLCsToBuffer(this.dplcs, obj.dplcDefinition);
213-
writeFile(dplcPath, chunk, (err, success) => {
213+
const { chunk, headers, frames } = DPLCsToBuffer(this.dplcs, obj.dplcDefinition);
214+
const out = isAsm ? stuffToAsm(headers, frames, obj.name) : chunk;
215+
writeFile(dplcPath, out, (err, success) => {
214216
err && errorMsg('Error Saving DPLCs', err.message);
215217
});
216218
}

0 commit comments

Comments
 (0)