@@ -2,8 +2,8 @@ import WebSocket from 'ws';
22import { v4 as uuidv4 } from 'uuid' ;
33import { Logger } from '../utils/logger.js' ;
44import { McpUnityError , ErrorType } from '../utils/errors.js' ;
5- import { execSync } from 'child_process ' ;
6- import { default as winreg } from 'winreg ' ;
5+ import { promises as fs } from 'fs ' ;
6+ import path from 'path ' ;
77
88interface PendingRequest {
99 resolve : ( value : any ) => void ;
@@ -30,28 +30,14 @@ interface UnityResponse {
3030
3131export class McpUnity {
3232 private logger : Logger ;
33- private port : number ;
33+ private port : number | null = null ;
3434 private ws : WebSocket | null = null ;
3535 private pendingRequests : Map < string , PendingRequest > = new Map < string , PendingRequest > ( ) ;
36- private readonly REQUEST_TIMEOUT : number ;
36+ private requestTimeout = 10000 ;
3737 private retryDelay = 1000 ;
3838
3939 constructor ( logger : Logger ) {
4040 this . logger = logger ;
41-
42- // Initialize port from environment variable or use default
43- const envRegistry = process . platform === 'win32'
44- ? this . getUnityPortFromWindowsRegistry ( )
45- : this . getUnityPortFromUnixRegistry ( ) ;
46-
47- const envPort = process . env . UNITY_PORT || envRegistry ;
48- this . port = envPort ? parseInt ( envPort , 10 ) : 8090 ;
49- this . logger . info ( `Using port: ${ this . port } for Unity WebSocket connection` ) ;
50-
51- // Initialize timeout from environment variable (in seconds; it is the same as Cline) or use default (10 seconds)
52- const envTimeout = process . env . UNITY_REQUEST_TIMEOUT ;
53- this . REQUEST_TIMEOUT = envTimeout ? parseInt ( envTimeout , 10 ) * 1000 : 10000 ;
54- this . logger . info ( `Using request timeout: ${ this . REQUEST_TIMEOUT / 1000 } seconds` ) ;
5541 }
5642
5743 /**
@@ -60,6 +46,9 @@ export class McpUnity {
6046 */
6147 public async start ( clientName ?: string ) : Promise < void > {
6248 try {
49+ this . logger . info ( 'Attempting to read startup parameters...' ) ;
50+ await this . parseAndSetConfig ( ) ;
51+
6352 this . logger . info ( 'Attempting to connect to Unity WebSocket...' ) ;
6453 await this . connect ( clientName ) ; // Pass client name to connect
6554 this . logger . info ( 'Successfully connected to Unity WebSocket' ) ;
@@ -77,6 +66,22 @@ export class McpUnity {
7766
7867 return Promise . resolve ( ) ;
7968 }
69+
70+ /**
71+ * Reads our configuration file and sets parameters of the server based on them.
72+ */
73+ private async parseAndSetConfig ( ) {
74+ const config = await this . readConfigFileAsJson ( ) ;
75+
76+ const configPort = config . Port ;
77+ this . port = configPort ? parseInt ( configPort , 10 ) : 8090 ;
78+ this . logger . info ( `Using port: ${ this . port } for Unity WebSocket connection` ) ;
79+
80+ // Initialize timeout from environment variable (in seconds; it is the same as Cline) or use default (10 seconds)
81+ const configTimeout = config . RequestTimeoutSeconds ;
82+ this . requestTimeout = configTimeout ? parseInt ( configTimeout , 10 ) * 1000 : 10000 ;
83+ this . logger . info ( `Using request timeout: ${ this . requestTimeout / 1000 } seconds` ) ;
84+ }
8085
8186 /**
8287 * Connect to the Unity WebSocket
@@ -112,7 +117,7 @@ export class McpUnity {
112117 this . disconnect ( ) ;
113118 reject ( new McpUnityError ( ErrorType . CONNECTION , 'Connection timeout' ) ) ;
114119 }
115- } , this . REQUEST_TIMEOUT ) ;
120+ } , this . requestTimeout ) ;
116121
117122 this . ws . onopen = ( ) => {
118123 clearTimeout ( connectionTimeout ) ;
@@ -249,12 +254,12 @@ export class McpUnity {
249254 // Create timeout for the request
250255 const timeout = setTimeout ( ( ) => {
251256 if ( this . pendingRequests . has ( requestId ) ) {
252- this . logger . error ( `Request ${ requestId } timed out after ${ this . REQUEST_TIMEOUT } ms` ) ;
257+ this . logger . error ( `Request ${ requestId } timed out after ${ this . requestTimeout } ms` ) ;
253258 this . pendingRequests . delete ( requestId ) ;
254259 reject ( new McpUnityError ( ErrorType . TIMEOUT , 'Request timed out' ) ) ;
255260 }
256261 this . reconnect ( ) ;
257- } , this . REQUEST_TIMEOUT ) ;
262+ } , this . requestTimeout ) ;
258263
259264 // Store pending request
260265 this . pendingRequests . set ( requestId , {
@@ -282,29 +287,21 @@ export class McpUnity {
282287 // Basic WebSocket connection check
283288 return this . ws !== null && this . ws . readyState === WebSocket . OPEN ;
284289 }
285-
286- /**
287- * Retrieves the UNITY_PORT value from the Windows registry (HKCU\Environment)
288- * @returns The port value as a string if found, otherwise an empty string
289- */
290- private getUnityPortFromWindowsRegistry ( ) : string {
291- const regKey = new winreg ( { hive : winreg . HKCU , key : '\\Environment' } ) ;
292- let result = '' ;
293- regKey . get ( 'UNITY_PORT' , ( err : Error | null , item : any ) => {
294- if ( err ) {
295- this . logger . error ( `Error getting registry value: ${ err . message } ` ) ;
296- } else {
297- result = item . value ;
298- }
299- } ) ;
300- return result ;
301- }
302290
303291 /**
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
292+ * Read the McpUnitySettings.json file and return its contents as a JSON object.
293+ * @returns a JSON object with the contents of the McpUnitySettings.json file.
306294 */
307- private getUnityPortFromUnixRegistry ( ) : string {
308- return execSync ( 'printenv UNITY_PORT' , { stdio : [ 'pipe' , 'pipe' , 'ignore' ] } ) . toString ( ) . trim ( ) ;
295+ private async readConfigFileAsJson ( ) : Promise < any > {
296+ const configPath = path . resolve ( process . cwd ( ) , '../ProjectSettings/McpUnitySettings.json' ) ;
297+ this . logger . debug ( `Reading McpUnitySettings.json from ${ configPath } ` ) ;
298+ try {
299+ const content = await fs . readFile ( configPath , 'utf-8' ) ;
300+ const json = JSON . parse ( content ) ;
301+ return json ;
302+ } catch ( err ) {
303+ this . logger . debug ( `McpUnitySettings.json not found or unreadable: ${ err instanceof Error ? err . message : String ( err ) } ` ) ;
304+ return { } ;
305+ }
309306 }
310307}
0 commit comments