@@ -4,6 +4,8 @@ import { Logger } from '../utils/logger.js';
44import { McpUnityError , ErrorType } from '../utils/errors.js' ;
55import { execSync } from 'child_process' ;
66import { default as winreg } from 'winreg' ;
7+ import { promises as fs } from 'fs' ;
8+ import path from 'path' ;
79
810interface PendingRequest {
911 resolve : ( value : any ) => void ;
@@ -42,11 +44,11 @@ export class McpUnity {
4244 // Initialize port from environment variable or use default
4345 const envRegistry = process . platform === 'win32'
4446 ? this . getUnityPortFromWindowsRegistry ( )
45- : this . getUnityPortFromUnixRegistry ( ) ;
47+ : this . getUnityPortFromConfig ( ) ;
4648
4749 const envPort = process . env . UNITY_PORT || envRegistry ;
4850 this . port = envPort ? parseInt ( envPort , 10 ) : 8090 ;
49- this . logger . info ( `Using port: ${ this . port } for Unity WebSocket connection` ) ;
51+ this . logger . warn ( `Using port: ${ this . port } for Unity WebSocket connection` ) ;
5052
5153 // Initialize timeout from environment variable (in seconds; it is the same as Cline) or use default (10 seconds)
5254 const envTimeout = process . env . UNITY_REQUEST_TIMEOUT ;
@@ -300,11 +302,16 @@ export class McpUnity {
300302 return result ;
301303 }
302304
303- /**
304- * Retrieves the UNITY_PORT value from Unix-like system environment variables
305- * @returns The port value as a string if found, otherwise an empty string
306- */
307- private getUnityPortFromUnixRegistry ( ) : string {
308- return execSync ( 'printenv UNITY_PORT' , { stdio : [ 'pipe' , 'pipe' , 'ignore' ] } ) . toString ( ) . trim ( ) ;
305+ private getUnityPortFromConfig ( ) : string {
306+ // Read UNITY_PORT value from env.config synchronously using execSync
307+ const configPath = path . resolve ( process . cwd ( ) , 'env.config' ) ;
308+ try {
309+ const content = execSync ( `cat "${ configPath } "` , { stdio : [ 'pipe' , 'pipe' , 'ignore' ] } ) . toString ( ) ;
310+ const match = content . match ( / ^ U N I T Y _ P O R T \s * = \s * ( \d + ) / m) ;
311+ return match ? match [ 1 ] : '' ;
312+ } catch ( err ) {
313+ this . logger . debug ( `env.config not found or unreadable: ${ err instanceof Error ? err . message : String ( err ) } ` ) ;
314+ return '' ;
315+ }
309316 }
310317}
0 commit comments