@@ -2,11 +2,11 @@ import * as fs from 'fs';
22import * as path from 'path' ;
33import { Logger } from './logging' ;
44import {
5- getArgumentValueAsString ,
6- killChildProcesses ,
5+ GetArgumentValueAsString ,
6+ KillChildProcesses ,
77 ProcInfo ,
8- readPidFile ,
9- tryKillProcess
8+ ReadPidFile ,
9+ TryKillProcess
1010} from './utilities' ;
1111import {
1212 spawn ,
@@ -19,21 +19,25 @@ export interface EditorCommand {
1919}
2020
2121export class UnityEditor {
22- public editorRootPath : string ;
22+ public readonly editorRootPath : string ;
23+
24+ private readonly logger : Logger = Logger . instance ;
25+ private readonly autoAddNoGraphics : boolean ;
2326
2427 private procInfo : ProcInfo | undefined ;
25- private pidFile : string ;
26- private logger : Logger = Logger . instance ;
27- private autoAddNoGraphics : boolean ;
2828
29- constructor ( public editorPath : string ) {
29+ /**
30+ * Initializes a new instance of the UnityEditor class.
31+ * @param editorPath The path to the Unity Editor installation.
32+ * @throws Will throw an error if the editor path is invalid or not executable.
33+ */
34+ constructor ( public readonly editorPath : string ) {
3035 if ( ! fs . existsSync ( editorPath ) ) {
3136 throw new Error ( `The Unity Editor path does not exist: ${ editorPath } ` ) ;
3237 }
3338
3439 fs . accessSync ( editorPath , fs . constants . X_OK ) ;
3540 this . editorRootPath = UnityEditor . GetEditorRootPath ( editorPath ) ;
36- this . pidFile = path . join ( process . env . RUNNER_TEMP || process . env . USERPROFILE || '.' , '.unity' , 'unity-editor-process-id.txt' ) ;
3741
3842 const match = editorPath . match ( / (?< major > \d + ) \. (?< minor > \d + ) \. (?< patch > \d + ) / ) ;
3943
@@ -117,6 +121,7 @@ export class UnityEditor {
117121 isCancelled = true ;
118122 await this . tryKillEditorProcess ( ) ;
119123 } ;
124+
120125 process . once ( 'SIGINT' , onCancel ) ;
121126 process . once ( 'SIGTERM' , onCancel ) ;
122127 let exitCode : number | undefined ;
@@ -133,7 +138,10 @@ export class UnityEditor {
133138 exitCode = 1 ;
134139 }
135140 } finally {
141+ process . removeListener ( 'SIGINT' , onCancel ) ;
142+ process . removeListener ( 'SIGTERM' , onCancel ) ;
136143 this . logger . endGroup ( ) ;
144+
137145 if ( ! isCancelled ) {
138146 await this . tryKillEditorProcess ( ) ;
139147
@@ -188,7 +196,7 @@ export class UnityEditor {
188196 command . args . push ( '-logFile' , this . GenerateLogFilePath ( command . projectPath ) ) ;
189197 }
190198
191- const logPath : string = getArgumentValueAsString ( '-logFile' , command . args ) ;
199+ const logPath : string = GetArgumentValueAsString ( '-logFile' , command . args ) ;
192200
193201 let unityProcess : ChildProcessByStdio < null , null , null > ;
194202
@@ -225,26 +233,6 @@ export class UnityEditor {
225233
226234 onPid ( { pid : processId , ppid : process . pid , name : this . editorPath } ) ;
227235 this . logger . debug ( `Unity process started with pid: ${ processId } ` ) ;
228- // make sure the directory for the PID file exists
229- const pidDir = path . dirname ( this . pidFile ) ;
230-
231- if ( ! fs . existsSync ( pidDir ) ) {
232- fs . mkdirSync ( pidDir , { recursive : true } ) ;
233- } else {
234- try {
235- var existingProcInfo = await readPidFile ( this . pidFile ) ;
236- if ( existingProcInfo ) {
237- const killedPid = await tryKillProcess ( existingProcInfo ) ;
238- if ( killedPid ) {
239- this . logger . warn ( `Killed existing Unity process with pid: ${ killedPid } ` ) ;
240- }
241- }
242- } catch {
243- // PID file does not exist, continue
244- }
245- }
246- // Write the PID to the PID file
247- fs . writeFileSync ( this . pidFile , String ( processId ) ) ;
248236 const logPollingInterval = 100 ; // milliseconds
249237 // Wait for log file to appear
250238 while ( ! fs . existsSync ( logPath ) ) {
@@ -318,8 +306,8 @@ export class UnityEditor {
318306
319307 private async tryKillEditorProcess ( ) : Promise < void > {
320308 if ( this . procInfo ) {
321- await tryKillProcess ( this . procInfo ) ;
322- await killChildProcesses ( this . procInfo ) ;
309+ await TryKillProcess ( this . procInfo ) ;
310+ await KillChildProcesses ( this . procInfo ) ;
323311 } else {
324312 this . logger . debug ( 'No Unity process info available to kill.' ) ;
325313 }
0 commit comments