11import { InitFunctionEnum } from "@utils/main/enums" ;
2+ import { joinRelative } from "@utils/path" ;
23
34import { LIBS_FUNCTIONS_NAME } from "./const" ;
45import {
@@ -12,32 +13,52 @@ import {
1213export class MainGenerator {
1314 private buffer = "" ;
1415 private indentation = 0 ;
16+ private readonly editor : boolean ;
17+ private readonly pathToRoot : string ;
18+
19+ constructor ( editor : boolean = false , pathToRoot : string = "" ) {
20+ this . editor = editor ;
21+ this . pathToRoot = pathToRoot ;
22+ }
1523
1624 toString ( ) : string {
1725 return this . buffer ;
1826 }
1927
2028 generateBaseImports ( hasTypes : boolean ) : MainGenerator {
21- if ( hasTypes ) this . writeLine ( `import { type IRunOptions } from "@nanoforge-dev/common";` ) ;
22- this . writeLine ( `import { NanoforgeFactory } from "@nanoforge-dev/core";` ) ;
29+ if ( this . editor ) {
30+ if ( hasTypes )
31+ this . writeLine (
32+ `import { type IEditorRunOptions, NanoforgeFactory } from "@nanoforge-dev/core-editor";` ,
33+ ) ;
34+ else this . writeLine ( `import { NanoforgeFactory } from "@nanoforge-dev/core-editor";` ) ;
35+ } else {
36+ if ( hasTypes ) this . writeLine ( `import { type IRunOptions } from "@nanoforge-dev/common";` ) ;
37+ this . writeLine ( `import { NanoforgeFactory } from "@nanoforge-dev/core";` ) ;
38+ }
39+
2340 this . endSection ( ) ;
2441 return this ;
2542 }
2643
2744 generateLibsImports ( libs : SaveLibrary [ ] ) : MainGenerator {
28- return this . generateImports ( libs ) ;
45+ return this . generateImports ( libs , false ) ;
2946 }
3047
3148 generateComponentsImports ( libs : SaveComponent [ ] ) : MainGenerator {
32- return this . generateImports ( libs ) ;
49+ return this . generateImports ( libs , true ) ;
3350 }
3451
3552 generateSystemsImports ( libs : SaveSystem [ ] ) : MainGenerator {
36- return this . generateImports ( libs ) ;
53+ return this . generateImports ( libs , true ) ;
3754 }
3855
3956 generateMainFunction ( hasTypes : boolean , cb : ( generator : MainGenerator ) => void ) : MainGenerator {
40- this . writeLine ( `export async function main(options${ hasTypes ? ": IRunOptions" : "" } ) {` ) ;
57+ if ( this . editor )
58+ this . writeLine (
59+ `export async function main(options${ hasTypes ? ": IEditorRunOptions" : "" } ) {` ,
60+ ) ;
61+ else this . writeLine ( `export async function main(options${ hasTypes ? ": IRunOptions" : "" } ) {` ) ;
4162 this . indentation += 1 ;
4263 cb ( this ) ;
4364 this . indentation -= 1 ;
@@ -84,7 +105,7 @@ export class MainGenerator {
84105 }
85106
86107 generateEntities ( entities : SaveEntity [ ] ) : MainGenerator {
87- entities . forEach ( ( entity ) => this . generateEntity ( entity ) ) ;
108+ entities . forEach ( ( entity , index ) => this . generateEntity ( entity , index ) ) ;
88109 return this ;
89110 }
90111
@@ -101,14 +122,17 @@ export class MainGenerator {
101122
102123 generateInitFunctionsImportsIfNeeded ( needed : boolean ) : MainGenerator {
103124 if ( ! needed ) return this ;
104- return this . generateImports ( [
105- { name : "afterInit" , path : "./init/after-init" } ,
106- { name : "afterRegistryInit" , path : "./init/after-registry-init" } ,
107- { name : "afterRun" , path : "./init/after-run" } ,
108- { name : "beforeInit" , path : "./init/before-init" } ,
109- { name : "beforeRegistryInit" , path : "./init/before-registry-init" } ,
110- { name : "beforeRun" , path : "./init/before-run" } ,
111- ] ) ;
125+ return this . generateImports (
126+ [
127+ { name : "afterInit" , path : "./init/after-init" } ,
128+ { name : "afterRegistryInit" , path : "./init/after-registry-init" } ,
129+ { name : "afterRun" , path : "./init/after-run" } ,
130+ { name : "beforeInit" , path : "./init/before-init" } ,
131+ { name : "beforeRegistryInit" , path : "./init/before-registry-init" } ,
132+ { name : "beforeRun" , path : "./init/before-run" } ,
133+ ] ,
134+ true ,
135+ ) ;
112136 }
113137
114138 private generateInitFunction ( func : InitFunctionEnum ) : MainGenerator {
@@ -122,19 +146,29 @@ export class MainGenerator {
122146 return this ;
123147 }
124148
125- private generateImports ( els : { name : string ; path : string } [ ] ) : MainGenerator {
149+ private generateImports ( els : { name : string ; path : string } [ ] , relative : boolean ) : MainGenerator {
126150 els
127151 . sort ( ( a , b ) => a . path . localeCompare ( b . path ) )
128- . forEach ( ( { name, path } ) => this . writeLine ( `import { ${ name } } from "${ path } ";` ) ) ;
152+ . forEach ( ( { name, path } ) =>
153+ this . writeLine (
154+ `import { ${ name } } from "${ relative ? joinRelative ( this . pathToRoot , path ) : path } ";` ,
155+ ) ,
156+ ) ;
129157 this . endSection ( ) ;
130158 return this ;
131159 }
132160
133- private generateEntity ( entity : SaveEntity ) : void {
161+ private generateEntity ( entity : SaveEntity , entityIndex : number ) : void {
134162 this . writeLine ( `const ${ entity . id } = registry.spawnEntity();` ) ;
135- entity . components . forEach ( ( { name, params } ) =>
136- this . writeLine ( `registry.addComponent(${ entity . id } , new ${ name } (${ params . join ( ", " ) } ));` ) ,
137- ) ;
163+ entity . components . forEach ( ( { name, params : rawParams } , componentIndex ) => {
164+ const params = ! this . editor
165+ ? rawParams
166+ : rawParams . map (
167+ ( _param , index ) =>
168+ `options.editor.save.entities[${ entityIndex } ].components[${ componentIndex } ].params[${ index } ]` ,
169+ ) ;
170+ this . writeLine ( `registry.addComponent(${ entity . id } , new ${ name } (${ params . join ( ", " ) } ));` ) ;
171+ } ) ;
138172 this . endSection ( ) ;
139173 }
140174
0 commit comments