Skip to content

Commit

Permalink
Glue communication now only enabled when game runs in Debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Dec 27, 2023
1 parent be59dc8 commit 9cb6826
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,40 @@ namespace GameCommunicationPlugin.CodeGeneration
{
public class Game1GlueCommunicationGenerator : Game1CodeGenerator
{

public Game1GlueCommunicationGenerator(bool isGameCommunicationEnabled, int portNumber)
{
IsGameCommunicationEnabled = isGameCommunicationEnabled;
PortNumber = portNumber;
}
}

public bool GenerateConnectionOnlyInDebug { get; set; } = true;
public bool IsGameCommunicationEnabled { get; set; }
public int PortNumber { get; set; }
public override void GenerateClassScope(ICodeBlock codeBlock)
{
if(IsGameCommunicationEnabled)
{
AddIfDebug(codeBlock);
codeBlock.Line("GlueCommunication.GameConnectionManager gameConnectionManager;");
EndIfDebug(codeBlock);
}
}

private void AddIfDebug(ICodeBlock codeBlock)
{
if (GenerateConnectionOnlyInDebug)
{
codeBlock.Line("#if DEBUG");
}
}


private void EndIfDebug(ICodeBlock codeBlock)
{
if (GenerateConnectionOnlyInDebug)
{
codeBlock.Line("#endif");
}
}

Expand Down Expand Up @@ -48,6 +69,7 @@ private void GenerateGameCommunicationInitialize(ICodeBlock codeBlock)
codeBlock.Line("};");
codeBlock.Line();

AddIfDebug(codeBlock);
codeBlock.Line($"gameConnectionManager = new GlueCommunication.GameConnectionManager({PortNumber});");
codeBlock.Line("gameConnectionManager.OnPacketReceived += async (packet) =>");
codeBlock.Line("{");
Expand All @@ -64,36 +86,7 @@ private void GenerateGameCommunicationInitialize(ICodeBlock codeBlock)
codeBlock.Line(" }");
codeBlock.Line("};");
codeBlock.Line("this.Exiting += (not, used) => gameConnectionManager.Dispose();");

//if(GameCommunicationHelper.IsFrbUsesJson())
//{
// codeBlock.Line("GlueCommunication.GameConnectionManager.CanUseJsonManager = true;");
// codeBlock.Line("var gjmInstance = GlueCommunication.Json.GlueJsonManager.Instance;");
// codeBlock.Line("gameConnectionManager.OnPacketReceived += async (packet) =>");
// codeBlock.Line("{");
// codeBlock.Line(" if (packet.Packet.PacketType == \"JsonUpdate\")");
// codeBlock.Line(" {");
// codeBlock.Line(" await gjmInstance.ProcessUpdatePacket(packet.Packet);");
// codeBlock.Line(" }");
// codeBlock.Line("};");
// //codeBlock.Line("gjmInstance.HandleUpdatedSelection += async (dto) => await glueControlManager.ProcessMessage(dto);");
// //codeBlock.Line("gjmInstance.SendPacket += (packet) => gameConnectionManager.SendItem(packet);");
// //codeBlock.Line("gjmInstance.SendPacketWithResponse += (packet) => { return gameConnectionManager.SendItemWithResponse(packet); };");
//}

//Test Block
//codeBlock.Line("System.Threading.Tasks.Task.Run(() =>");
//codeBlock.Line("{");
//codeBlock.Line(" while (true)");
//codeBlock.Line(" {");
//codeBlock.Line(" System.Threading.Thread.Sleep(500);");
//codeBlock.Line(" gameConnectionManager.SendItem(new GlueCommunication.GameConnectionManager.Packet");
//codeBlock.Line(" {");
//codeBlock.Line(" PacketType = \"Test\",");
//codeBlock.Line(" Payload = System.DateTime.Now.ToLongTimeString(),");
//codeBlock.Line(" });");
//codeBlock.Line(" }");
//codeBlock.Line("});");
EndIfDebug(codeBlock);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,36 @@ namespace GameCommunicationPlugin.GlueControl.CodeGeneration
{
public class Game1GlueControlGenerator : Game1CodeGenerator
{
public bool GenerateConnectionOnlyInDebug { get; set; } = true;
private void AddIfDebug(ICodeBlock codeBlock)
{
if (GenerateConnectionOnlyInDebug)
{
codeBlock.Line("#if DEBUG");
}
}


private void EndIfDebug(ICodeBlock codeBlock)
{
if (GenerateConnectionOnlyInDebug)
{
codeBlock.Line("#endif");
}
}

public bool IsGlueControlManagerGenerationEnabled { get; set; }
public int PortNumber { get; set; }
public override void GenerateClassScope(ICodeBlock codeBlock)
{
if(IsGlueControlManagerGenerationEnabled)
{
AddIfDebug(codeBlock);

codeBlock.Line("GlueControl.GlueControlManager glueControlManager;");

EndIfDebug(codeBlock);

}
}

Expand Down Expand Up @@ -122,6 +145,7 @@ private void GenerateGlueControlManagerInitialize(ICodeBlock codeBlock)
{
if (IsGlueControlManagerGenerationEnabled)
{
AddIfDebug(codeBlock);
codeBlock.Line($"glueControlManager = new GlueControl.GlueControlManager({PortNumber})");
codeBlock.Line("{");
codeBlock.Line(" GameConnectionManager = gameConnectionManager,");
Expand All @@ -134,7 +158,6 @@ private void GenerateGlueControlManagerInitialize(ICodeBlock codeBlock)
sizeChangedInnerBlockIf.Line("GlueControl.Editing.CameraLogic.UpdateCameraToZoomLevel(zoomAroundCursorPosition: false);");
sizeChangedInnerBlock.Line("GlueControl.Editing.CameraLogic.PushZoomLevelToEditor();");
codeBlock.Line(";");

// Vic says - We run all Glue commands before running custom initialize. The reason is - custom initialize
// may make modifications to objects that are created by glue commands (such as assigning acceleration to objects
// in a list), but it is unlikely that scripts will make modifications to objects created in CustomInitialize because
Expand Down Expand Up @@ -163,6 +186,7 @@ private void GenerateGlueControlManagerInitialize(ICodeBlock codeBlock)
}
}
codeBlock.Line(";");
EndIfDebug(codeBlock);

if(GlueState.Self.CurrentGlueProject.FileVersion >= (int)GluxVersions.HasScreenManagerAfterScreenDestroyed)
{
Expand Down

0 comments on commit 9cb6826

Please sign in to comment.