Skip to content

Commit

Permalink
More removal of async event calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Apr 13, 2024
1 parent 542732c commit 192cadd
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 108 deletions.
18 changes: 9 additions & 9 deletions FRBDK/Glue/CompilerPlugin/Managers/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ internal async Task<GeneralResponse> Run(bool preventFocus, string runArguments
{
runArguments += " LaunchedByEditor";
}
startResponse = StartProcess(preventFocus, runArguments, exeLocation);
startResponse = await StartProcess(preventFocus, runArguments, exeLocation);

if (startResponse.Succeeded)
{
Expand All @@ -328,7 +328,7 @@ internal async Task<GeneralResponse> 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();

Expand All @@ -355,7 +355,7 @@ internal async Task<GeneralResponse> Run(bool preventFocus, string runArguments

if (id == null || id == IntPtr.Zero)
{
Thread.Sleep(millisecondsToWaitBeforeRetry);
await Task.Delay(millisecondsToWaitBeforeRetry);
continue;
}
else
Expand Down Expand Up @@ -397,7 +397,7 @@ internal async Task<GeneralResponse> Run(bool preventFocus, string runArguments
{
if (startResponse.Data.HasExited && startResponse.Data.ExitCode != 0)
{
var message = GetCrashMessage();
var message = await GetCrashMessage();

if (!string.IsNullOrEmpty(message))
{
Expand Down Expand Up @@ -469,7 +469,7 @@ private static string GetGameExeLocation(string configuration)
}
}

private ToolsUtilities.GeneralResponse<Process> StartProcess(bool preventFocus, string runArguments, string exeLocation)
private async Task<ToolsUtilities.GeneralResponse<Process>> StartProcess(bool preventFocus, string runArguments, string exeLocation)
{
if (preventFocus)
{
Expand Down Expand Up @@ -502,7 +502,7 @@ private ToolsUtilities.GeneralResponse<Process> StartProcess(bool preventFocus,
if (hasExited && process?.ExitCode != 0)
{

var message = GetCrashMessage();
var message = await GetCrashMessage();
var response = ToolsUtilities.GeneralResponse<Process>.UnsuccessfulWith(message);
response.Data = process;
return response;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -578,15 +578,15 @@ private async void HandleProcessExit(object sender, EventArgs e)
}
}

private string GetCrashMessage()
private async Task<string> GetCrashMessage()
{
string exeLocation = GetGameExeLocation(this._compilerViewModel.Configuration);

string message = string.Empty;
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ private IPEndPoint EndPoint
public double TimeoutInSeconds { get; set; } = 10;
#endregion

public static GameConnectionManager Self { get; set; }

public GameConnectionManager(Action<string, string> eventCaller)
{
_eventCaller = eventCaller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CommandSender
#region Fields/Properties

public Action<string> PrintOutput { get; set; }
public Func<string, Task<string>> SendPacket { get; set; }
public Func<string, Task<GeneralResponse<string>>> SendPacket { get; set; }
SemaphoreSlim sendCommandSemaphore = new SemaphoreSlim(1, 1);

public GlueViewSettingsViewModel GlueViewSettingsViewModel { get; set; }
Expand Down Expand Up @@ -176,9 +176,8 @@ private CommandSender() { }
};
}

var response = JsonConvert.DeserializeObject<ToolsUtilities.GeneralResponse<string>>(returnValue);

return response;
return returnValue;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<string>();
toReturn.SetFrom(response);
toReturn.Data = response?.Data?.Payload;

return toReturn;

};
//ReactToPluginEventWithReturn("GameCommunication_Send_OldDTO", value);

glueViewSettingsView = new Views.GlueViewSettings();
glueViewSettingsView.ViewModel = GlueViewSettingsViewModel;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,49 +444,15 @@ internal async Task HandleNewObjectList(List<NamedObjectSave> 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<AddObjectDtoResponse>(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);
}
}
}
Expand Down
53 changes: 14 additions & 39 deletions FRBDK/Glue/GameCommunicationPlugin/MainGameCommunicationPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -73,15 +78,6 @@ public override void HandleEvent(string eventName, string payload)
case "GameCommunication_SendPacket":
_gameCommunicationManager.SendItem(JsonConvert.DeserializeObject<GameConnectionManager.Packet>(payload));

break;

case "GameCommunication_Send_OldDTO":
_gameCommunicationManager.SendItem(new GameConnectionManager.Packet
{
PacketType = "OldDTO",
Payload = payload
});

break;
}
}
Expand All @@ -94,40 +90,19 @@ protected override async Task<string> HandleEventWithReturnImplementation(string
var returnValue = await _gameCommunicationManager.SendItemWithResponse(JsonConvert.DeserializeObject<GameConnectionManager.Packet>(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<string>
{
Succeeded = returnPacket != null,
Data = returnPacket?.Payload,
Message = response.Message
});

case "GameCommunication_SetPrimarySettings":
var sPayload = JObject.Parse(payload);

_gameCommunicationManager.Port = sPayload.ContainsKey("PortNumber") ? sPayload.Value<int>("PortNumber") : 8888;
_gameCommunicationManager.DoConnections = sPayload.ContainsKey("IsGlueControlManagerGenerationEnabled") ? sPayload.Value<bool>("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))
Expand Down
7 changes: 2 additions & 5 deletions FRBDK/Glue/Glue/Plugins/PluginBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,9 @@ public async Task<string> HandleEventWithReturn(string eventName, string payload
});
}

protected virtual async Task<string> HandleEventWithReturnImplementation(string eventName, string payload)
protected virtual Task<string> HandleEventWithReturnImplementation(string eventName, string payload)
{
return await Task.Run(() =>
{
return (string)null;
});
return Task.FromResult((string)null);
}

public void HandleEventResponseWithReturn(string payload)
Expand Down

0 comments on commit 192cadd

Please sign in to comment.