diff --git a/FRBDK/Glue/CompilerPlugin/Managers/Runner.cs b/FRBDK/Glue/CompilerPlugin/Managers/Runner.cs index e484e27b4..cd59cfd06 100644 --- a/FRBDK/Glue/CompilerPlugin/Managers/Runner.cs +++ b/FRBDK/Glue/CompilerPlugin/Managers/Runner.cs @@ -318,7 +318,7 @@ internal async Task Run(bool preventFocus, string runArguments { runArguments += " LaunchedByEditor"; } - startResponse = StartProcess(preventFocus, runArguments, exeLocation); + startResponse = await StartProcess(preventFocus, runArguments, exeLocation); if (startResponse.Succeeded) { @@ -328,7 +328,7 @@ internal async Task Run(bool preventFocus, string runArguments while (runningGameProcess == null) { // didn't find it, so let's wait a little and try again: - Thread.Sleep(millisecondsToWaitBeforeRetry); + await Task.Delay(millisecondsToWaitBeforeRetry); runningGameProcess = TryFindGameProcess(); @@ -355,7 +355,7 @@ internal async Task Run(bool preventFocus, string runArguments if (id == null || id == IntPtr.Zero) { - Thread.Sleep(millisecondsToWaitBeforeRetry); + await Task.Delay(millisecondsToWaitBeforeRetry); continue; } else @@ -397,7 +397,7 @@ internal async Task Run(bool preventFocus, string runArguments { if (startResponse.Data.HasExited && startResponse.Data.ExitCode != 0) { - var message = GetCrashMessage(); + var message = await GetCrashMessage(); if (!string.IsNullOrEmpty(message)) { @@ -469,7 +469,7 @@ private static string GetGameExeLocation(string configuration) } } - private ToolsUtilities.GeneralResponse StartProcess(bool preventFocus, string runArguments, string exeLocation) + private async Task> StartProcess(bool preventFocus, string runArguments, string exeLocation) { if (preventFocus) { @@ -502,7 +502,7 @@ private ToolsUtilities.GeneralResponse StartProcess(bool preventFocus, if (hasExited && process?.ExitCode != 0) { - var message = GetCrashMessage(); + var message = await GetCrashMessage(); var response = ToolsUtilities.GeneralResponse.UnsuccessfulWith(message); response.Data = process; return response; @@ -547,7 +547,7 @@ private async void HandleProcessExit(object sender, EventArgs e) { if (process.ExitCode != 0) { - string message = GetCrashMessage(); + string message = await GetCrashMessage(); if (!string.IsNullOrEmpty(message)) { System.Windows.MessageBox.Show(message); @@ -578,7 +578,7 @@ private async void HandleProcessExit(object sender, EventArgs e) } } - private string GetCrashMessage() + private async Task GetCrashMessage() { string exeLocation = GetGameExeLocation(this._compilerViewModel.Configuration); @@ -586,7 +586,7 @@ private string GetCrashMessage() if (!string.IsNullOrEmpty(exeLocation)) { // await a little to see if the crash.txt file gets written... - Thread.Sleep(3); + await Task.Delay(3); var directory = FileManager.GetDirectory(exeLocation); var logFile = directory + "CrashInfo.txt"; diff --git a/FRBDK/Glue/GameCommunicationPlugin/Common/GameConnectionManager.cs b/FRBDK/Glue/GameCommunicationPlugin/Common/GameConnectionManager.cs index 3c31a5d01..6e4d6804c 100644 --- a/FRBDK/Glue/GameCommunicationPlugin/Common/GameConnectionManager.cs +++ b/FRBDK/Glue/GameCommunicationPlugin/Common/GameConnectionManager.cs @@ -94,6 +94,8 @@ private IPEndPoint EndPoint public double TimeoutInSeconds { get; set; } = 10; #endregion + public static GameConnectionManager Self { get; set; } + public GameConnectionManager(Action eventCaller) { _eventCaller = eventCaller; diff --git a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/CommandSending/CommandSender.cs b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/CommandSending/CommandSender.cs index 115d7eb9f..05969b0e4 100644 --- a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/CommandSending/CommandSender.cs +++ b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/CommandSending/CommandSender.cs @@ -28,7 +28,7 @@ public class CommandSender #region Fields/Properties public Action PrintOutput { get; set; } - public Func> SendPacket { get; set; } + public Func>> SendPacket { get; set; } SemaphoreSlim sendCommandSemaphore = new SemaphoreSlim(1, 1); public GlueViewSettingsViewModel GlueViewSettingsViewModel { get; set; } @@ -176,9 +176,8 @@ private CommandSender() { } }; } - var response = JsonConvert.DeserializeObject>(returnValue); - return response; + return returnValue; } diff --git a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/MainCompilerPlugin.cs b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/MainCompilerPlugin.cs index ab1fed602..f621997b9 100644 --- a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/MainCompilerPlugin.cs +++ b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/MainCompilerPlugin.cs @@ -160,7 +160,6 @@ private void AssignEvents() this.ReactToScreenRemoved += ToolbarController.Self.HandleScreenRemoved; // todo - handle startup changed... - this.ReactToNewObjectHandler += _refreshManager.HandleNewObjectCreated; this.ReactToNewObjectListAsync += _refreshManager.HandleNewObjectList; this.ReactToObjectRemoved += async (owner, nos) => @@ -470,11 +469,10 @@ private void HandleGluxLoaded() game1GlueControlGenerator.PortNumber = model.PortNumber; game1GlueControlGenerator.IsGlueControlManagerGenerationEnabled = model.GenerateGlueControlManagerCode && IsFrbNewEnough(); - ReactToPluginEventWithReturn("GameCommunication_SetPrimarySettings", JsonConvert.SerializeObject(new - { - IsGlueControlManagerGenerationEnabled = game1GlueControlGenerator.IsGlueControlManagerGenerationEnabled, - PortNumber = game1GlueControlGenerator.PortNumber - })); + MainGameCommunicationPlugin.Self.SetPrimarySettings( + game1GlueControlGenerator.PortNumber, + game1GlueControlGenerator.IsGlueControlManagerGenerationEnabled + ); _refreshManager.PortNumber = model.PortNumber; @@ -634,7 +632,22 @@ private void CreateBuildControl() CommandSender.Self.GlueViewSettingsViewModel = GlueViewSettingsViewModel; CommandSender.Self.CompilerViewModel = CompilerViewModel; CommandSender.Self.PrintOutput = (value) => ReactToPluginEvent("Compiler_Output_Standard", value); - CommandSender.Self.SendPacket = (value) => ReactToPluginEventWithReturn("GameCommunication_Send_OldDTO", value); + CommandSender.Self.SendPacket = async (value) => + { + var response = await GameJsonCommunicationPlugin.Common.GameConnectionManager.Self.SendItemWithResponse(new GameJsonCommunicationPlugin.Common.GameConnectionManager.Packet + { + PacketType = "OldDTO", + Payload = value + }); + + var toReturn = new global::ToolsUtilities.GeneralResponse(); + toReturn.SetFrom(response); + toReturn.Data = response?.Data?.Payload; + + return toReturn; + + }; + //ReactToPluginEventWithReturn("GameCommunication_Send_OldDTO", value); glueViewSettingsView = new Views.GlueViewSettings(); glueViewSettingsView.ViewModel = GlueViewSettingsViewModel; @@ -853,11 +866,10 @@ private async Task HandlePortOrGenerateCheckedChanged(string propertyName) game1GlueControlGenerator.PortNumber = GlueViewSettingsViewModel.PortNumber; _refreshManager.PortNumber = GlueViewSettingsViewModel.PortNumber; - var returnValue = await ReactToPluginEventWithReturn("GameCommunication_SetPrimarySettings", JsonConvert.SerializeObject(new - { - IsGlueControlManagerGenerationEnabled = game1GlueControlGenerator.IsGlueControlManagerGenerationEnabled, - PortNumber = game1GlueControlGenerator.PortNumber - })); + MainGameCommunicationPlugin.Self.SetPrimarySettings( + game1GlueControlGenerator.PortNumber, + game1GlueControlGenerator.IsGlueControlManagerGenerationEnabled + ); GlueCommands.Self.GenerateCodeCommands.GenerateGame1(); if (IsFrbNewEnough() && GlueViewSettingsViewModel.EnableLiveEdit) diff --git a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/Managers/RefreshManager.cs b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/Managers/RefreshManager.cs index b9f5033f0..c03386f4f 100644 --- a/FRBDK/Glue/GameCommunicationPlugin/GlueControl/Managers/RefreshManager.cs +++ b/FRBDK/Glue/GameCommunicationPlugin/GlueControl/Managers/RefreshManager.cs @@ -444,49 +444,15 @@ internal async Task HandleNewObjectList(List newObjectList) { CreateStopAndRestartTask("Restarting because the add object group failed"); } - } - } - - internal async void HandleNewObjectCreated(NamedObjectSave newNamedObject) - { - if (ViewModel.IsRunning && ViewModel.IsEditChecked) - { - AddObjectDto addObjectDto = CreateAddObjectDtoFor(newNamedObject); - - var sendResponse = await CommandSender.Self.Send(addObjectDto); - string addResponseAsString = null; - if (sendResponse.Succeeded) - { - addResponseAsString = sendResponse.Data; - } - - AddObjectDtoResponse addResponse = null; - if (!string.IsNullOrEmpty(addResponseAsString)) - { - try - { - addResponse = JsonConvert.DeserializeObject(addResponseAsString); - } - catch (Exception) - { - printOutput($"Error parsing string:\n\n{addResponseAsString}"); - } - } - - if (addResponse?.CreationResponse.Succeeded == true) - { - var isPositionedObject = newNamedObject.SourceType == SourceType.Entity || - (newNamedObject.GetAssetTypeInfo()?.IsPositionedObject == true); - if (isPositionedObject) - { - await AdjustNewObjectToCameraPosition(newNamedObject); - } - } else { - if(GlueViewSettingsViewModel.RestartOnFailedCommands) + var firstPositionedObject = newObjectList.FirstOrDefault(item => + item.SourceType == SourceType.Entity || + item.GetAssetTypeInfo()?.IsPositionedObject == true); + + if(firstPositionedObject != null) { - CreateStopAndRestartTask($"Restarting because of added object {newNamedObject}"); + await AdjustNewObjectToCameraPosition(firstPositionedObject); } } } diff --git a/FRBDK/Glue/GameCommunicationPlugin/MainGameCommunicationPlugin.cs b/FRBDK/Glue/GameCommunicationPlugin/MainGameCommunicationPlugin.cs index c862f430f..37e6209e0 100644 --- a/FRBDK/Glue/GameCommunicationPlugin/MainGameCommunicationPlugin.cs +++ b/FRBDK/Glue/GameCommunicationPlugin/MainGameCommunicationPlugin.cs @@ -28,6 +28,8 @@ public class MainGameCommunicationPlugin : PluginBase public override Version Version => new Version(1, 0); + public static MainGameCommunicationPlugin Self { get; private set; } + #endregion public override bool ShutDown(PluginShutDownReason shutDownReason) @@ -36,15 +38,18 @@ public override bool ShutDown(PluginShutDownReason shutDownReason) _gameCommunicationManager.OnPacketReceived -= HandleOnPacketReceived; _gameCommunicationManager.Dispose(); _gameCommunicationManager = null; - + GameConnectionManager.Self = null; return true; } public override void StartUp() { + Self = this; _gameCommunicationManager = new GameConnectionManager(ReactToPluginEvent); _gameCommunicationManager.Port = 8888; _gameCommunicationManager.OnPacketReceived += HandleOnPacketReceived; + GameConnectionManager.Self = _gameCommunicationManager; + ReactToLoadedGlux += HandleGluxLoaded; game1GlueCommunicationGenerator = new Game1GlueCommunicationGenerator(true, 8888); @@ -73,15 +78,6 @@ public override void HandleEvent(string eventName, string payload) case "GameCommunication_SendPacket": _gameCommunicationManager.SendItem(JsonConvert.DeserializeObject(payload)); - break; - - case "GameCommunication_Send_OldDTO": - _gameCommunicationManager.SendItem(new GameConnectionManager.Packet - { - PacketType = "OldDTO", - Payload = payload - }); - break; } } @@ -94,40 +90,19 @@ protected override async Task HandleEventWithReturnImplementation(string var returnValue = await _gameCommunicationManager.SendItemWithResponse(JsonConvert.DeserializeObject(payload)); return returnValue.Data?.Payload; - case "GameCommunication_Send_OldDTO": - var response = await _gameCommunicationManager.SendItemWithResponse(new GameConnectionManager.Packet - { - PacketType = "OldDTO", - Payload = payload - }); - - var returnPacket = response.Data; - - if(returnPacket?.PacketType == "OldDTO" && returnPacket?.Payload != "{\"Commands\":[]}") - Debug.WriteLine($"{returnPacket.PacketType}, {returnPacket.Payload}"); - - return JsonConvert.SerializeObject(new GeneralResponse - { - Succeeded = returnPacket != null, - Data = returnPacket?.Payload, - Message = response.Message - }); - - case "GameCommunication_SetPrimarySettings": - var sPayload = JObject.Parse(payload); - - _gameCommunicationManager.Port = sPayload.ContainsKey("PortNumber") ? sPayload.Value("PortNumber") : 8888; - _gameCommunicationManager.DoConnections = sPayload.ContainsKey("IsGlueControlManagerGenerationEnabled") ? sPayload.Value("IsGlueControlManagerGenerationEnabled") : false; - game1GlueCommunicationGenerator.PortNumber = _gameCommunicationManager.Port; - game1GlueCommunicationGenerator.IsGameCommunicationEnabled = _gameCommunicationManager.DoConnections; - - return ""; - } return null; } + public void SetPrimarySettings(int portNumber, bool doConnections) + { + _gameCommunicationManager.Port = portNumber; + _gameCommunicationManager.DoConnections = doConnections; + game1GlueCommunicationGenerator.PortNumber = _gameCommunicationManager.Port; + game1GlueCommunicationGenerator.IsGameCommunicationEnabled = _gameCommunicationManager.DoConnections; + } + private void HandleOnPacketReceived(GameConnectionManager.PacketReceivedArgs packetReceivedArgs) { if(!string.IsNullOrEmpty(packetReceivedArgs.Packet.Payload)) diff --git a/FRBDK/Glue/Glue/Plugins/PluginBase.cs b/FRBDK/Glue/Glue/Plugins/PluginBase.cs index d8aeb987b..97107a106 100644 --- a/FRBDK/Glue/Glue/Plugins/PluginBase.cs +++ b/FRBDK/Glue/Glue/Plugins/PluginBase.cs @@ -919,12 +919,9 @@ public async Task HandleEventWithReturn(string eventName, string payload }); } - protected virtual async Task HandleEventWithReturnImplementation(string eventName, string payload) + protected virtual Task HandleEventWithReturnImplementation(string eventName, string payload) { - return await Task.Run(() => - { - return (string)null; - }); + return Task.FromResult((string)null); } public void HandleEventResponseWithReturn(string payload)