public void DefaultSpawnRequestHandler(SpawnRequestPacket packet, IIncommingMessage message) { Logger.Debug("Default spawn handler started handling a request to spawn process"); var controller = Msf.Server.Spawners.GetController(packet.SpawnerId); if (controller == null) { message.Respond("Failed to spawn a process. Spawner controller not found", ResponseStatus.Failed); return; } var port = Msf.Server.Spawners.GetAvailablePort(); // Check if we're overriding an IP to master server var masterIp = string.IsNullOrEmpty(controller.DefaultSpawnerSettings.MasterIp) ? controller.Connection.ConnectionIp : controller.DefaultSpawnerSettings.MasterIp; // Check if we're overriding a port to master server var masterPort = controller.DefaultSpawnerSettings.MasterPort < 0 ? controller.Connection.ConnectionPort : controller.DefaultSpawnerSettings.MasterPort; // Machine Ip var machineIp = controller.DefaultSpawnerSettings.MachineIp; // Path to executable var path = controller.DefaultSpawnerSettings.ExecutablePath; if (string.IsNullOrEmpty(path)) { path = File.Exists(Environment.GetCommandLineArgs()[0]) ? Environment.GetCommandLineArgs()[0] : Process.GetCurrentProcess().MainModule.FileName; } // In case a path is provided with the request if (packet.Properties.ContainsKey(MsfDictKeys.ExecutablePath)) { path = packet.Properties[MsfDictKeys.ExecutablePath]; } // Get the scene name var sceneNameArgument = packet.Properties.ContainsKey(MsfDictKeys.SceneName) ? string.Format("{0} {1} ", Msf.Args.Names.LoadScene, packet.Properties[MsfDictKeys.SceneName]) : ""; if (!string.IsNullOrEmpty(packet.OverrideExePath)) { path = packet.OverrideExePath; } // If spawn in batchmode was set and `DontSpawnInBatchmode` arg is not provided var spawnInBatchmode = controller.DefaultSpawnerSettings.SpawnInBatchmode && !Msf.Args.DontSpawnInBatchmode; #region Custom Albot Data //Albot Edit! We add so that the gameserver knows if it is a realtime game and what type. //These values will be read in the Msf.Args and accesable after startup string realTimeValue = packet.Properties[MsfDictKeys.IsRealtime]; string gameTypeValue = packet.Properties[MsfDictKeys.GameType]; string spectatorsValue = packet.Properties[MsfDictKeys.Spectators]; string realtimeInfo = string.Format("{0} {1} ", Msf.Args.Names.Realtime, realTimeValue); string gameTypeInfo = string.Format("{0} {1} ", Msf.Args.Names.GameType, gameTypeValue); string SpectatorsInfo = string.Format("{0} {1} ", Msf.Args.Names.Spectators, spectatorsValue); #endregion var startProcessInfo = new ProcessStartInfo(path) { CreateNoWindow = false, UseShellExecute = false, Arguments = " " + (spawnInBatchmode ? "-batchmode -nographics " : "") + (controller.DefaultSpawnerSettings.AddWebGlFlag ? Msf.Args.Names.WebGl + " " : "") + sceneNameArgument + string.Format("{0} {1} ", Msf.Args.Names.MasterIp, masterIp) + string.Format("{0} {1} ", Msf.Args.Names.MasterPort, masterPort) + string.Format("{0} {1} ", Msf.Args.Names.SpawnId, packet.SpawnId) + string.Format("{0} {1} ", Msf.Args.Names.AssignedPort, port) + string.Format("{0} {1} ", Msf.Args.Names.MachineIp, machineIp) + (Msf.Args.DestroyUi ? Msf.Args.Names.DestroyUi + " " : "") + string.Format("{0} \"{1}\" ", Msf.Args.Names.SpawnCode, packet.SpawnCode) + packet.CustomArgs + realtimeInfo + gameTypeInfo + SpectatorsInfo }; Logger.Debug("Starting process with args: " + startProcessInfo.Arguments); var processStarted = false; try { new Thread(() => { try { Logger.Debug("New thread started"); using (var process = Process.Start(startProcessInfo)) { Logger.Debug("Process started. Spawn Id: " + packet.SpawnId + ", pid: " + process.Id); processStarted = true; Logger.Debug(_processLock); lock (_processLock) { // Save the process _processes[packet.SpawnId] = process; } var processId = process.Id; // Notify server that we've successfully handled the request BTimer.ExecuteOnMainThread(() => { Logger.Debug("Sending Success"); message.Respond(ResponseStatus.Success); controller.NotifyProcessStarted(packet.SpawnId, processId, startProcessInfo.Arguments); Logger.Debug("Post Sending Success"); }); process.WaitForExit(); } } catch { if (!processStarted) { BTimer.ExecuteOnMainThread(() => { message.Respond(ResponseStatus.Failed); }); } Logger.Debug("An exception was thrown while starting a process. Make sure that you have set a correct build path. " + "We've tried to start a process at: '" + path + "'. You can change it at 'SpawnerBehaviour' component"); } finally { lock (_processLock) { // Remove the process _processes.Remove(packet.SpawnId); } BTimer.ExecuteOnMainThread(() => { // Release the port number Msf.Server.Spawners.ReleasePort(port); Logger.Debug("Notifying about killed process with spawn id: " + packet.SpawnerId); controller.NotifyProcessKilled(packet.SpawnId); }); } }).Start(); } catch (Exception e) { message.Respond(e.Message, ResponseStatus.Error); Logs.Error(e); } }
public void RunOnMainThread(Action action) { BTimer.ExecuteOnMainThread(action); }