From 0279e6f152d4832d7ab582571b854555fdc0c868 Mon Sep 17 00:00:00 2001 From: Ashley Stanton-Nurse Date: Wed, 13 Dec 2023 14:37:10 -0800 Subject: [PATCH] more refmts missed before --- .../CodeGenerators/CodeGeneratorUtils.cs | 2 +- .../CodeGenerators/GenerateRGActionClasses.cs | 40 +++++++++---------- .../GenerateRGActionMapClass.cs | 26 ++++++------ .../GenerateRGSerializationClass.cs | 4 +- .../CodeGenerators/GenerateRGStateClasses.cs | 16 ++++---- .../Runtime/Scripts/RGBotConfigs/RGState.cs | 14 +++---- .../Runtime/Scripts/RGBotServerListener.cs | 30 +++++++------- 7 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/CodeGeneratorUtils.cs b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/CodeGeneratorUtils.cs index fa2426786..d195590d0 100644 --- a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/CodeGeneratorUtils.cs +++ b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/CodeGeneratorUtils.cs @@ -9,7 +9,7 @@ namespace RegressionGames public class CodeGeneratorUtils { public static readonly string HeaderComment = $"/*\r\n* This file has been automatically generated. Do not modify.\r\n*/\r\n\r\n"; - + public static string SanitizeActionName(string name) { return Regex.Replace(name.Replace(" ", "_"), "[^0-9a-zA-Z_]", "_"); diff --git a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionClasses.cs b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionClasses.cs index 2c977e28b..e85d04ecb 100644 --- a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionClasses.cs +++ b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionClasses.cs @@ -19,7 +19,7 @@ public static class GenerateRGActionClasses { public static void Generate(List actionInfos) { - Dictionary fileWriteTasks = new(); + Dictionary fileWriteTasks = new(); // Iterate through BotActions foreach (var botAction in actionInfos) { @@ -41,7 +41,7 @@ public static void Generate(List actionInfos) } var projectNamespace = CodeGeneratorUtils.GetNamespaceForProject(); - + botAction.GeneratedClassName = $"{projectNamespace}.RGAction_{CodeGeneratorUtils.SanitizeActionName(botAction.ActionName)}"; @@ -167,7 +167,7 @@ private static MemberDeclarationSyntax GenerateGetActionNameMethod(RGActionAttri return getActionNameMethod; } - + private static ArgumentSyntax GenerateActionDelegate(RGActionAttributeInfo action) { // Generate the GetComponent().MethodName piece for both cases (0 and non-0 parameters) @@ -236,14 +236,14 @@ private static MemberDeclarationSyntax GenerateStartActionMethod(RGActionInfo ac foreach (var parameter in action.Parameters) { string paramName = parameter.Name; - + methodInvocationArguments.Add(paramName); parameterParsingStatements.Add(SyntaxFactory.ParseStatement($"{parameter.Type} {paramName} = default;")); parameterParsingStatements.Add(SyntaxFactory.IfStatement(IfCondition(parameter), IfBody(parameter), ElseBody(parameter))); } - string methodInvocationArgumentsString = methodInvocationArguments.Count > 0 ? - ", " + string.Join(", ", methodInvocationArguments) : + string methodInvocationArgumentsString = methodInvocationArguments.Count > 0 ? + ", " + string.Join(", ", methodInvocationArguments) : string.Empty; parameterParsingStatements.Add(SyntaxFactory.ParseStatement($"Invoke(\"{action.ActionName}\"{methodInvocationArgumentsString});")); @@ -264,16 +264,16 @@ private static InvocationExpressionSyntax IfCondition(RGParameterInfo param) { return SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - SyntaxFactory.IdentifierName("input"), + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.IdentifierName("input"), SyntaxFactory.IdentifierName("TryGetValue") )).WithArgumentList( SyntaxFactory.ArgumentList( - SyntaxFactory.SeparatedList(new List - { + SyntaxFactory.SeparatedList(new List + { SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( - SyntaxKind.StringLiteralExpression, + SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(param.Name) ) ), @@ -309,7 +309,7 @@ private static StatementSyntax IfBody(RGParameterInfo param) { var paramType = param.Type; var paramName = param.Name; - + string tryParseStatement; if (paramType.ToLower() == "string" || paramType.ToLower() == "system.string") { @@ -368,15 +368,15 @@ private static ElseClauseSyntax ElseBody(RGParameterInfo param) { return default(ElseClauseSyntax); } - + // Validation check for key existence if param must be non-null - return SyntaxFactory.ElseClause(SyntaxFactory.Block(new StatementSyntax[] + return SyntaxFactory.ElseClause(SyntaxFactory.Block(new StatementSyntax[] { SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - SyntaxFactory.IdentifierName("RGDebug"), + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.IdentifierName("RGDebug"), SyntaxFactory.IdentifierName("LogError") ) ).WithArgumentList( @@ -384,14 +384,14 @@ private static ElseClauseSyntax ElseBody(RGParameterInfo param) SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( - SyntaxKind.StringLiteralExpression, + SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal($"No parameter '{param.Name}' found") ) ) ) ) ) - ), SyntaxFactory.ReturnStatement() + ), SyntaxFactory.ReturnStatement() } )); } @@ -400,7 +400,7 @@ private static MemberDeclarationSyntax GenerateActionRequestConstructor(RGAction { var methodParameters = new List(); var parameterParsingStatements = new List(); - + foreach (var rgParameterInfo in action.Parameters) { methodParameters.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(rgParameterInfo.Name)) @@ -416,7 +416,7 @@ private static MemberDeclarationSyntax GenerateActionRequestConstructor(RGAction } inputString += "\r\n};"; - + parameterParsingStatements.Add( SyntaxFactory.ParseStatement(inputString) ); diff --git a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionMapClass.cs b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionMapClass.cs index 4694be0c9..834281d62 100644 --- a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionMapClass.cs +++ b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGActionMapClass.cs @@ -37,8 +37,8 @@ public static void Generate(List botActions) usings.Select(v => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(v))).ToArray() ) .AddMembers(GenerateClass(botActions)); - - + + // Create a compilation unit and add the namespace declaration CompilationUnitSyntax compilationUnit = SyntaxFactory.CompilationUnit() .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System"))) @@ -52,7 +52,7 @@ public static void Generate(List botActions) string filePath = Path.Combine(Application.dataPath, "RegressionGames", "Runtime", "GeneratedScripts", fileName); string fileContents = CodeGeneratorUtils.HeaderComment + formattedCode; Directory.CreateDirectory(Path.GetDirectoryName(filePath)); - File.WriteAllText(filePath, fileContents); + File.WriteAllText(filePath, fileContents); RGDebug.Log($"Successfully Generated {filePath}"); } @@ -66,27 +66,27 @@ private static ClassDeclarationSyntax GenerateClass(List .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)) .WithBody(SyntaxFactory.Block(filteredActions .GroupBy(b => b.Object) - .SelectMany(g => + .SelectMany(g => { - var innerIfStatements = g.Select(b => + var innerIfStatements = g.Select(b => SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - SyntaxFactory.IdentifierName("gameObject"), + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.IdentifierName("gameObject"), SyntaxFactory.IdentifierName($"AddComponent") ) ) ) ).ToArray(); - - return new StatementSyntax[] + + return new StatementSyntax[] { SyntaxFactory.IfStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - SyntaxFactory.ThisExpression(), + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.ThisExpression(), SyntaxFactory.GenericName("TryGetComponent") .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( @@ -98,8 +98,8 @@ private static ClassDeclarationSyntax GenerateClass(List ), SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Argument( - null, - SyntaxFactory.Token(SyntaxKind.OutKeyword), + null, + SyntaxFactory.Token(SyntaxKind.OutKeyword), SyntaxFactory.DeclarationExpression( SyntaxFactory.IdentifierName("var"), SyntaxFactory.DiscardDesignation(SyntaxFactory.Token(SyntaxKind.UnderscoreToken)) diff --git a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGSerializationClass.cs b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGSerializationClass.cs index 8f9c118a1..a6caf4a62 100644 --- a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGSerializationClass.cs +++ b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGSerializationClass.cs @@ -52,14 +52,14 @@ private static ClassDeclarationSyntax GenerateClass(List { continue; } - + foreach (RGParameterInfo parameter in botAction.Parameters) { if (RGUtils.IsCSharpPrimitive(parameter.Type)) { continue; } - + if (!processedTypes.Contains(parameter.Type)) { processedTypes.Add(parameter.Type); diff --git a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGStateClasses.cs b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGStateClasses.cs index 5f4f03403..ba1235ff9 100644 --- a/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGStateClasses.cs +++ b/src/gg.regression.unity.bots/Editor/Scripts/CodeGenerators/GenerateRGStateClasses.cs @@ -20,7 +20,7 @@ public static class GenerateRGStateClasses { public static void Generate(List rgStateAttributesInfos) { - Dictionary fileWriteTasks = new(); + Dictionary fileWriteTasks = new(); foreach (var rgStateAttributeInfo in rgStateAttributesInfos) { if (rgStateAttributeInfo.ShouldGenerateCSFile) @@ -62,7 +62,7 @@ public static void Generate(List rgStateAttributesInfos) // Create the Start method var startMethod = GenerateStartMethod(componentType, rgStateAttributeInfo.State); - + // Create the SEInstance method var seInstanceMethod = GenerateGetTypeForStateEntityMethod(componentType); @@ -229,7 +229,7 @@ private static MethodDeclarationSyntax GenerateGetStateMethod(string componentTy // Add statements to add each state variable to the dictionary statements.AddRange(memberInfos.Select(mi => { - ExpressionSyntax valueExpression = mi.FieldType == "method" + ExpressionSyntax valueExpression = mi.FieldType == "method" ? InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -276,9 +276,9 @@ private static MethodDeclarationSyntax GenerateGetStateMethod(string componentTy return getStateMethod; } - - - + + + private static MemberDeclarationSyntax[] GenerateStateEntityFields(List memberInfos) { var fields = new List(); @@ -286,7 +286,7 @@ private static MemberDeclarationSyntax[] GenerateStateEntityFields(List GetComponent(); - + /** * A function that is overriden to provide the custom state of this specific GameObject. * For example, you may want to retrieve and set the health of a player on the returned @@ -48,7 +48,7 @@ public IRGStateEntity GetGameObjectState() return state; } - + protected virtual Type GetTypeForStateEntity() { return typeof(RGStateEntity); @@ -69,15 +69,15 @@ private IRGStateEntity CreateStateEntityClassInstance() } return (IRGStateEntity)Activator.CreateInstance(type); } - - + + // Used to fill in the core state for any RGEntity that does NOT have an // RGState implementation on its game object public static IRGStateEntity GenerateCoreStateForRGEntity(RGEntity rgEntity) { IRGStateEntity state; - + // if button.. include whether it is interactable var button = rgEntity.Button; if (button != null) @@ -91,7 +91,7 @@ public static IRGStateEntity GenerateCoreStateForRGEntity(RGEntity rgEntity) state = new RGStateEntity(); } var theTransform = rgEntity.transform; - + state["id"] = theTransform.GetInstanceID(); // default to the gameObject name without uniqueness numbers var otName = rgEntity.objectType.Trim(); @@ -119,7 +119,7 @@ public static IRGStateEntity GenerateCoreStateForRGEntity(RGEntity rgEntity) } return state; } - + private static void PopulateEverythingStateForEntity(IRGStateEntity state, RGEntity entity) { var obsoleteAttributeType = typeof(ObsoleteAttribute); diff --git a/src/gg.regression.unity.bots/Runtime/Scripts/RGBotServerListener.cs b/src/gg.regression.unity.bots/Runtime/Scripts/RGBotServerListener.cs index 27310157b..376ea1bc8 100644 --- a/src/gg.regression.unity.bots/Runtime/Scripts/RGBotServerListener.cs +++ b/src/gg.regression.unity.bots/Runtime/Scripts/RGBotServerListener.cs @@ -689,8 +689,8 @@ public void HandleClientHandshakeMessage(long clientId, RGClientHandshake handsh return; } - //Handle spawning player and recording lifecycle for de-spawn - bool spawnable = handshakeMessage.spawnable; + //Handle spawning player and recording lifecycle for de-spawn + bool spawnable = handshakeMessage.spawnable; string lifecycle = string.IsNullOrEmpty(handshakeMessage.lifecycle) ? "MANAGED" @@ -698,9 +698,9 @@ public void HandleClientHandshakeMessage(long clientId, RGClientHandshake handsh clientConnectionMap[clientId].Lifecycle = lifecycle; - // if the bot coming in already put its unique client Id on the end.. ignore - // else make sure the name is unique by appending the id - string botName = $"{handshakeMessage.botName}"; + // if the bot coming in already put its unique client Id on the end.. ignore + // else make sure the name is unique by appending the id + string botName = $"{handshakeMessage.botName}"; string clientIdStringSuffix = $"-{clientId}"; if (!botName.EndsWith(clientIdStringSuffix)) { @@ -708,13 +708,13 @@ public void HandleClientHandshakeMessage(long clientId, RGClientHandshake handsh } Dictionary characterConfig = handshakeMessage.characterConfig; - // save the token the client gave us for talking to them - clientConnectionMap[clientId].Token = handshakeMessage.rgToken; + // save the token the client gave us for talking to them + clientConnectionMap[clientId].Token = handshakeMessage.rgToken; if (!spawnable && "PERSISTENT".Equals(lifecycle)) { - // should be a menu / human simulator bot, give them the default agent... thus allowing button clicks - RGEntity theAgent = this.gameObject.GetComponent(); + // should be a menu / human simulator bot, give them the default agent... thus allowing button clicks + RGEntity theAgent = this.gameObject.GetComponent(); agentMap[clientId] = new HashSet { theAgent }; } else @@ -722,8 +722,8 @@ public void HandleClientHandshakeMessage(long clientId, RGClientHandshake handsh agentMap[clientId] = new HashSet(); } - // set this BEFORE sending the response of handshake to the client so it actually sends - SetUnityBotState(clientId, RGUnityBotState.CONNECTED); + // set this BEFORE sending the response of handshake to the client so it actually sends + SetUnityBotState(clientId, RGUnityBotState.CONNECTED); if (spawnable) { try @@ -738,12 +738,12 @@ public void HandleClientHandshakeMessage(long clientId, RGClientHandshake handsh else { RGDebug.LogDebug($"Sending socket handshake response to client id: {clientId}"); - //send the client a handshake response so they can start processing - clientConnectionMap[clientId].SendHandshakeResponse(new RGServerHandshake(UnitySideToken, characterConfig, null)); + //send the client a handshake response so they can start processing + clientConnectionMap[clientId].SendHandshakeResponse(new RGServerHandshake(UnitySideToken, characterConfig, null)); } - // we used to do this in the connection on each send, but that was too many updates.. now we do it after sending handshake response - SetUnityBotState(clientId, RGUnityBotState.RUNNING); + // we used to do this in the connection on each send, but that was too many updates.. now we do it after sending handshake response + SetUnityBotState(clientId, RGUnityBotState.RUNNING); } catch (Exception ex) {