11import React , { useState , useRef } from 'react' ;
22import { observer } from 'mobx-react' ;
33import { 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' ;
116
127import { decompress , compress , compressionFormats } from '#formats/compression' ;
138import { bufferToTiles , tilesToBuffer } from '#formats/art' ;
@@ -17,28 +12,26 @@ import { workspace } from '#store/workspace';
1712import ErrorMsg from './error' ;
1813import SaveLoad from './save-load' ;
1914import { promises } from 'fs' ;
20- import { extname } from 'path' ;
15+ import { extname , basename } from 'path' ;
2116import { uuid } from '#util/uuid' ;
2217
2318const fs = promises ;
2419const compressionList = Object . keys ( compressionFormats ) ;
2520
21+ const isASM = ( path ) => [ '.asm' , '.s' ] . includes ( extname ( path ) ) ;
22+
2623export const FileObject = observer ( ( { obj } ) => {
2724 scripts . length ; // react to script updates
2825 const script = obj . format && runScript ( obj . format ) ;
2926 const scriptSafe = script && ! script . error ;
3027
3128 const { isAbsolute } = obj ; // set in store/workspace
3229
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 ) ;
3532 const linesLeft = obj . palettes . reduce ( ( a , c ) => a - c . length , 4 ) ;
3633
37- const scriptDPLCs = scriptSafe && script . DPLCs ;
38- const scriptArt = scriptSafe && script . art ;
39- const scriptPalettes = scriptSafe && script . palettes ;
4034 const toggleDPLCs = ( ) => ( obj . dplcs . enabled = ! obj . dplcs . enabled ) ;
41- const parseASM = ( scriptSafe && script . parseASM ) || parseASMInternal ;
4235
4336 function ioWrap ( filePath , setError , e , cb ) {
4437 setError ( ) ;
@@ -59,9 +52,28 @@ export const FileObject = observer(({ obj }) => {
5952 }
6053 }
6154
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+
6273 const loadRef = useRef ( ) ;
6374
6475 function loadObject ( ) {
76+ loadRef . current . childNodes . forEach ( n => { n . textContent = '' ; } ) ;
6577 loadArt ( { target : loadRef . current . childNodes [ 0 ] } ) ;
6678 loadMappings ( { target : loadRef . current . childNodes [ 1 ] } ) ;
6779 if ( obj . dplcs . enabled ) {
@@ -71,6 +83,7 @@ export const FileObject = observer(({ obj }) => {
7183 }
7284
7385 function saveObject ( ) {
86+ loadRef . current . childNodes . forEach ( n => { n . textContent = '' ; } ) ;
7487 saveArt ( { target : loadRef . current . childNodes [ 0 ] } ) ;
7588 saveMappings ( { target : loadRef . current . childNodes [ 1 ] } ) ;
7689 if ( obj . dplcs . enabled ) {
@@ -85,7 +98,7 @@ export const FileObject = observer(({ obj }) => {
8598 ioWrap ( obj . art . path , setArtError , e , async ( path ) => {
8699 const buffer = ( await fs . readFile ( path ) ) . slice ( obj . art . offset || 0 ) ;
87100
88- if ( scriptArt ) {
101+ if ( script . art ) {
89102 environment . tiles . replace ( script . readArt ( buffer ) ) ;
90103 } else {
91104 const decompBuffer = await decompress (
@@ -102,12 +115,12 @@ export const FileObject = observer(({ obj }) => {
102115 if ( obj . art . offset ) {
103116 throw new Error ( 'Can only save art at offset 0' ) ;
104117 }
105- const tiles = scriptArt
118+ const tiles = script . art
106119 ? script . writeArt ( tiles )
107120 : tilesToBuffer ( environment . tiles , obj . art . compression ) ;
108121 await fs . writeFile ( path , tiles ) ;
109122
110- if ( scriptArt ) {
123+ if ( script . art ) {
111124 await fs . writeFile ( path , script . writeArt ( tiles ) ) ;
112125 } else {
113126 const buffer = tilesToBuffer ( environment . tiles ) ;
@@ -124,10 +137,7 @@ export const FileObject = observer(({ obj }) => {
124137 function loadMappings ( e ) {
125138 ioWrap ( obj . mappings . path , setMappingError , e , async ( path ) => {
126139 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 ) ;
131141
132142 const mappings = script . readMappings ( buffer ) ;
133143 if ( mappings . error ) throw mappings . error ;
@@ -168,9 +178,7 @@ export const FileObject = observer(({ obj }) => {
168178 function loadDPLCs ( e ) {
169179 ioWrap ( obj . dplcs . path , setDPLCError , e , async ( path ) => {
170180 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 ) ;
174182
175183 const dplcs = script . readDPLCs ( buffer ) ;
176184 if ( dplcs . error ) throw dplcs . error ;
@@ -206,7 +214,7 @@ export const FileObject = observer(({ obj }) => {
206214 ? palPath
207215 : workspace . absolutePath ( palPath ) ;
208216
209- ( scriptPalettes ? script . readPalettes : buffersToColors ) ( {
217+ ( script . palettes ? script . readPalettes : buffersToColors ) ( {
210218 buffer : await fs . readFile ( path ) ,
211219 length,
212220 } ) . forEach ( ( line ) => {
@@ -233,7 +241,7 @@ export const FileObject = observer(({ obj }) => {
233241 : workspace . absolutePath ( palPath ) ;
234242
235243 const chunk = (
236- scriptPalettes ? script . writePalettes : colorsToBuffers
244+ script . palettes ? script . writePalettes : colorsToBuffers
237245 ) ( environment . palettes , cursor , cursor + length ) ;
238246 await fs . writeFile ( path , chunk ) ;
239247 cursor += length ;
@@ -263,7 +271,7 @@ export const FileObject = observer(({ obj }) => {
263271 < Item color = "green" > Art</ Item >
264272 < SaveLoad load = { loadArt } save = { saveArt } />
265273 </ div >
266- { ! scriptArt && (
274+ { ! script . art && (
267275 < >
268276 < div className = "menu-item" >
269277 < Item > Compression</ Item >
@@ -305,7 +313,7 @@ export const FileObject = observer(({ obj }) => {
305313 </ div >
306314 ) }
307315
308- { scriptDPLCs && (
316+ { script . PLCs && (
309317 < >
310318 < div className = "menu-item" onClick = { toggleDPLCs } >
311319 < Item > DPLCs Enabled</ Item >
0 commit comments