1
1
import React , { useState , useRef } from 'react' ;
2
2
import { observer } from 'mobx-react' ;
3
3
import { Item , Input , File as FileInput , Select , Checkbox , Button } from '#ui' ;
4
- import {
5
- scripts ,
6
- runScript ,
7
- parseASM as parseASMInternal ,
8
- writeBIN ,
9
- writeASM ,
10
- } from '#formats/scripts' ;
4
+ import { scripts , runScript , writeBIN , writeASM , parseASMBasic } from '#formats/scripts' ;
5
+ import { assemble } from '#formats/asm' ;
11
6
12
7
import { decompress , compress , compressionFormats } from '#formats/compression' ;
13
8
import { bufferToTiles , tilesToBuffer } from '#formats/art' ;
@@ -17,28 +12,26 @@ import { workspace } from '#store/workspace';
17
12
import ErrorMsg from './error' ;
18
13
import SaveLoad from './save-load' ;
19
14
import { promises } from 'fs' ;
20
- import { extname } from 'path' ;
15
+ import { extname , basename } from 'path' ;
21
16
import { uuid } from '#util/uuid' ;
22
17
23
18
const fs = promises ;
24
19
const compressionList = Object . keys ( compressionFormats ) ;
25
20
21
+ const isASM = ( path ) => [ '.asm' , '.s' ] . includes ( extname ( path ) ) ;
22
+
26
23
export const FileObject = observer ( ( { obj } ) => {
27
24
scripts . length ; // react to script updates
28
25
const script = obj . format && runScript ( obj . format ) ;
29
26
const scriptSafe = script && ! script . error ;
30
27
31
28
const { isAbsolute } = obj ; // set in store/workspace
32
29
33
- const mappingsASM = extname ( obj . mappings . path ) === '.asm' ;
34
- const dplcsASM = extname ( obj . dplcs . path ) === '.asm' ;
30
+ const mappingsASM = isASM ( obj . mappings . path ) ;
31
+ const dplcsASM = isASM ( obj . dplcs . path ) ;
35
32
const linesLeft = obj . palettes . reduce ( ( a , c ) => a - c . length , 4 ) ;
36
33
37
- const scriptDPLCs = scriptSafe && script . DPLCs ;
38
- const scriptArt = scriptSafe && script . art ;
39
- const scriptPalettes = scriptSafe && script . palettes ;
40
34
const toggleDPLCs = ( ) => ( obj . dplcs . enabled = ! obj . dplcs . enabled ) ;
41
- const parseASM = ( scriptSafe && script . parseASM ) || parseASMInternal ;
42
35
43
36
function ioWrap ( filePath , setError , e , cb ) {
44
37
setError ( ) ;
@@ -59,9 +52,28 @@ export const FileObject = observer(({ obj }) => {
59
52
}
60
53
}
61
54
55
+ async function getBuffer ( path , isASM ) {
56
+ if ( isASM ) {
57
+ const contents = await fs . readFile ( path , 'utf8' ) ;
58
+
59
+ console . time ( path ) ;
60
+ if ( script . asm . basic ) return await parseASMBasic ( contents ) ;
61
+
62
+ const buffer = await assemble ( script . asm . prelude + contents , {
63
+ filename : basename ( path ) ,
64
+ } ) ;
65
+ console . timeEnd ( path ) ;
66
+
67
+ return buffer ;
68
+ }
69
+
70
+ return await fs . readFile ( path ) ;
71
+ }
72
+
62
73
const loadRef = useRef ( ) ;
63
74
64
75
function loadObject ( ) {
76
+ loadRef . current . childNodes . forEach ( n => { n . textContent = '' ; } ) ;
65
77
loadArt ( { target : loadRef . current . childNodes [ 0 ] } ) ;
66
78
loadMappings ( { target : loadRef . current . childNodes [ 1 ] } ) ;
67
79
if ( obj . dplcs . enabled ) {
@@ -71,6 +83,7 @@ export const FileObject = observer(({ obj }) => {
71
83
}
72
84
73
85
function saveObject ( ) {
86
+ loadRef . current . childNodes . forEach ( n => { n . textContent = '' ; } ) ;
74
87
saveArt ( { target : loadRef . current . childNodes [ 0 ] } ) ;
75
88
saveMappings ( { target : loadRef . current . childNodes [ 1 ] } ) ;
76
89
if ( obj . dplcs . enabled ) {
@@ -85,7 +98,7 @@ export const FileObject = observer(({ obj }) => {
85
98
ioWrap ( obj . art . path , setArtError , e , async ( path ) => {
86
99
const buffer = ( await fs . readFile ( path ) ) . slice ( obj . art . offset || 0 ) ;
87
100
88
- if ( scriptArt ) {
101
+ if ( script . art ) {
89
102
environment . tiles . replace ( script . readArt ( buffer ) ) ;
90
103
} else {
91
104
const decompBuffer = await decompress (
@@ -102,12 +115,12 @@ export const FileObject = observer(({ obj }) => {
102
115
if ( obj . art . offset ) {
103
116
throw new Error ( 'Can only save art at offset 0' ) ;
104
117
}
105
- const tiles = scriptArt
118
+ const tiles = script . art
106
119
? script . writeArt ( tiles )
107
120
: tilesToBuffer ( environment . tiles , obj . art . compression ) ;
108
121
await fs . writeFile ( path , tiles ) ;
109
122
110
- if ( scriptArt ) {
123
+ if ( script . art ) {
111
124
await fs . writeFile ( path , script . writeArt ( tiles ) ) ;
112
125
} else {
113
126
const buffer = tilesToBuffer ( environment . tiles ) ;
@@ -124,10 +137,7 @@ export const FileObject = observer(({ obj }) => {
124
137
function loadMappings ( e ) {
125
138
ioWrap ( obj . mappings . path , setMappingError , e , async ( path ) => {
126
139
if ( ! obj . dplcs . enabled ) environment . config . dplcsEnabled = false ;
127
-
128
- const buffer = mappingsASM
129
- ? parseASM ( await fs . readFile ( path , 'utf8' ) )
130
- : await fs . readFile ( path ) ;
140
+ const buffer = await getBuffer ( path , mappingsASM ) ;
131
141
132
142
const mappings = script . readMappings ( buffer ) ;
133
143
if ( mappings . error ) throw mappings . error ;
@@ -168,9 +178,7 @@ export const FileObject = observer(({ obj }) => {
168
178
function loadDPLCs ( e ) {
169
179
ioWrap ( obj . dplcs . path , setDPLCError , e , async ( path ) => {
170
180
environment . config . dplcsEnabled = true ;
171
- const buffer = dplcsASM
172
- ? parseASM ( await fs . readFile ( path , 'utf8' ) )
173
- : await fs . readFile ( path ) ;
181
+ const buffer = await getBuffer ( path , dplcsASM ) ;
174
182
175
183
const dplcs = script . readDPLCs ( buffer ) ;
176
184
if ( dplcs . error ) throw dplcs . error ;
@@ -206,7 +214,7 @@ export const FileObject = observer(({ obj }) => {
206
214
? palPath
207
215
: workspace . absolutePath ( palPath ) ;
208
216
209
- ( scriptPalettes ? script . readPalettes : buffersToColors ) ( {
217
+ ( script . palettes ? script . readPalettes : buffersToColors ) ( {
210
218
buffer : await fs . readFile ( path ) ,
211
219
length,
212
220
} ) . forEach ( ( line ) => {
@@ -233,7 +241,7 @@ export const FileObject = observer(({ obj }) => {
233
241
: workspace . absolutePath ( palPath ) ;
234
242
235
243
const chunk = (
236
- scriptPalettes ? script . writePalettes : colorsToBuffers
244
+ script . palettes ? script . writePalettes : colorsToBuffers
237
245
) ( environment . palettes , cursor , cursor + length ) ;
238
246
await fs . writeFile ( path , chunk ) ;
239
247
cursor += length ;
@@ -263,7 +271,7 @@ export const FileObject = observer(({ obj }) => {
263
271
< Item color = "green" > Art</ Item >
264
272
< SaveLoad load = { loadArt } save = { saveArt } />
265
273
</ div >
266
- { ! scriptArt && (
274
+ { ! script . art && (
267
275
< >
268
276
< div className = "menu-item" >
269
277
< Item > Compression</ Item >
@@ -305,7 +313,7 @@ export const FileObject = observer(({ obj }) => {
305
313
</ div >
306
314
) }
307
315
308
- { scriptDPLCs && (
316
+ { script . PLCs && (
309
317
< >
310
318
< div className = "menu-item" onClick = { toggleDPLCs } >
311
319
< Item > DPLCs Enabled</ Item >
0 commit comments