@@ -91,103 +91,118 @@ const WINDOWS = process.platform === 'win32'
9191const GRADLE_WRAPPER = WINDOWS ? 'gradlew.bat' : 'gradlew'
9292
9393ipcMain . handle ( 'electronAPI' , async ( event , operation , ...args ) => {
94- switch ( operation ) {
95- case 'openScaffoldDirectory' : {
96- const result = await dialog . showOpenDialog ( {
97- title : 'Please select your battlecode-scaffold directory.' ,
98- properties : [ 'openDirectory' ]
99- } )
100- return result . canceled ? undefined : result . filePaths [ 0 ]
101- }
102- case 'getRootPath' :
103- return app . getAppPath ( )
104- case 'getJavas' : {
105- const output = [ ]
106- const foundPaths = { }
107- try {
108- const javas = ( await javaFind . getJavas ( ) ) . filter ( ( j ) => j . version . major == 1 && j . version . minor == 8 )
109- for ( const j of javas ) {
110- const v = j . version
111- const displayStr = `${ v . major } .${ v . minor } .${ v . patch } _${ v . update } (${ j . arch } )`
112- const formattedPath = reformatJavaPath ( j . path )
113- if ( ! formattedPath || formattedPath in foundPaths ) continue
114- foundPaths [ formattedPath ] = true
115- output . push ( displayStr )
116- output . push ( formattedPath )
94+ try {
95+ switch ( operation ) {
96+ case 'openScaffoldDirectory' : {
97+ const result = await dialog . showOpenDialog ( {
98+ title : 'Please select your battlecode-scaffold directory.' ,
99+ properties : [ 'openDirectory' ]
100+ } )
101+ return result . canceled ? undefined : result . filePaths [ 0 ]
102+ }
103+ case 'getRootPath' :
104+ return app . getAppPath ( )
105+ case 'getJavas' : {
106+ const output = [ ]
107+ const foundPaths = { }
108+ try {
109+ const javas = ( await javaFind . getJavas ( ) ) . filter (
110+ ( j ) => j . version . major == 1 && j . version . minor == 8
111+ )
112+ for ( const j of javas ) {
113+ const v = j . version
114+ const displayStr = `${ v . major } .${ v . minor } .${ v . patch } _${ v . update } (${ j . arch } )`
115+ const formattedPath = reformatJavaPath ( j . path )
116+ if ( ! formattedPath || formattedPath in foundPaths ) continue
117+ foundPaths [ formattedPath ] = true
118+ output . push ( displayStr )
119+ output . push ( formattedPath )
120+ }
121+ } catch { }
122+ return output
123+ }
124+ case 'exportMap' : {
125+ const result = await dialog . showSaveDialog ( {
126+ title : 'Export map' ,
127+ defaultPath : args [ 1 ]
128+ } )
129+ if ( ! result . canceled ) {
130+ const path = result . filePath
131+ fs . writeFileSync ( path , new Uint8Array ( args [ 0 ] ) )
117132 }
118- } catch { }
119- return output
120- }
121- case 'exportMap' : {
122- const result = await dialog . showSaveDialog ( {
123- title : 'Export map' ,
124- defaultPath : args [ 1 ]
125- } )
126- if ( ! result . canceled ) {
127- const path = result . filePath
128- fs . writeFileSync ( path , new Uint8Array ( args [ 0 ] ) )
133+ return
129134 }
130- return
131- }
132- case 'getServerVersion' : {
133- let version = ''
134- try {
135- const response = await fetch ( `https://api.battlecode.org/api/episode/e/bc${ args [ 0 ] } /?format=json` )
136- version = ( await response . json ( ) ) . release_version_public
137- } catch { }
138- return version
139- }
140- case 'path.join' :
141- return path . join ( ...args )
142- case 'path.relative' :
143- return path . relative ( ...args )
144- case 'path.dirname' :
145- return path . dirname ( args [ 0 ] )
146- case 'path.sep' :
147- return path . sep
148- case 'fs.existsSync' :
149- return fs . existsSync ( args [ 0 ] )
150- case 'fs.mkdirSync' :
151- return fs . mkdirSync ( args [ 0 ] )
152- case 'fs.getFiles' :
153- return getFiles ( args [ 0 ] , args [ 1 ] === 'true' )
154- case 'child_process.spawn' : {
155- const scaffoldPath = args [ 0 ]
156- const javaPath = args [ 1 ]
157- const flags = args [ 2 ]
158- const wrapperPath = path . join ( scaffoldPath , GRADLE_WRAPPER )
159- const options = { cwd : scaffoldPath }
160- if ( javaPath ) options . env = { JAVA_HOME : javaPath }
161- const child = child_process . spawn ( wrapperPath , flags , options )
162- const pid = child . pid . toString ( )
163-
164- processes . set ( pid , child )
165-
166- child . stdout . on ( 'data' , ( data ) => {
167- event . sender . send ( 'child_process.stdout' , { pid, data : data . toString ( ) } )
168- } )
169- child . stderr . on ( 'data' , ( data ) => {
170- event . sender . send ( 'child_process.stderr' , { pid, data : data . toString ( ) } )
171- } )
172- child . on ( 'exit' , ( code , signal ) => {
173- processes . delete ( child . pid )
174- event . sender . send ( 'child_process.exit' , {
175- pid,
176- code : ( code ?? 0 ) . toString ( ) ,
177- signal : ( signal ?? 0 ) . toString ( )
135+ case 'getServerVersion' : {
136+ let version = ''
137+ try {
138+ const response = await fetch ( `https://api.battlecode.org/api/episode/e/bc${ args [ 0 ] } /?format=json` )
139+ version = ( await response . json ( ) ) . release_version_public
140+ } catch { }
141+ return version
142+ }
143+ case 'path.join' :
144+ return path . join ( ...args )
145+ case 'path.relative' :
146+ return path . relative ( ...args )
147+ case 'path.dirname' :
148+ return path . dirname ( args [ 0 ] )
149+ case 'path.sep' :
150+ return path . sep
151+ case 'fs.existsSync' :
152+ return fs . existsSync ( args [ 0 ] )
153+ case 'fs.mkdirSync' :
154+ return fs . mkdirSync ( args [ 0 ] )
155+ case 'fs.getFiles' :
156+ return getFiles ( args [ 0 ] , args [ 1 ] === 'true' )
157+ case 'child_process.spawn' : {
158+ const scaffoldPath = args [ 0 ]
159+ const javaPath = args [ 1 ]
160+ const flags = args [ 2 ]
161+ const wrapperPath = path . join ( scaffoldPath , GRADLE_WRAPPER )
162+ const options = { cwd : scaffoldPath }
163+ if ( javaPath ) options . env = { JAVA_HOME : javaPath }
164+
165+ const child = child_process . spawn ( wrapperPath , flags , options )
166+
167+ const pid = await new Promise ( ( resolve , reject ) => {
168+ child . on ( 'error' , reject )
169+ child . on ( 'spawn' , ( ) => {
170+ const pid = child . pid . toString ( )
171+
172+ processes . set ( pid , child )
173+
174+ child . stdout . on ( 'data' , ( data ) => {
175+ event . sender . send ( 'child_process.stdout' , { pid, data : data . toString ( ) } )
176+ } )
177+ child . stderr . on ( 'data' , ( data ) => {
178+ event . sender . send ( 'child_process.stderr' , { pid, data : data . toString ( ) } )
179+ } )
180+ child . on ( 'exit' , ( code , signal ) => {
181+ processes . delete ( child . pid )
182+ event . sender . send ( 'child_process.exit' , {
183+ pid,
184+ code : ( code ?? 0 ) . toString ( ) ,
185+ signal : ( signal ?? 0 ) . toString ( )
186+ } )
187+ } )
188+
189+ resolve ( pid )
190+ } )
178191 } )
179- } )
180192
181- return child . pid . toString ( )
182- }
183- case 'child_process.kill' : {
184- const pid = args [ 0 ]
185- if ( processes . has ( pid ) ) {
186- processes . get ( pid ) . kill ( )
193+ return pid
194+ }
195+ case 'child_process.kill' : {
196+ const pid = args [ 0 ]
197+ if ( processes . has ( pid ) ) {
198+ processes . get ( pid ) . kill ( )
199+ }
200+ return
187201 }
188- return
202+ default :
203+ throw new Error ( 'Invalid ipc API operation: ' + operation )
189204 }
190- default :
191- throw new Error ( 'Invalid ipc API operation: ' + operation )
205+ } catch ( e ) {
206+ return { ELECTRON_ERROR : e . toString ( ) }
192207 }
193208} )
0 commit comments