private static string GenerateConfigurationCode()
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            var fileName = GlueState.Self.CurrentGlueProjectFileName;

            var projectName = FileManager.RemoveExtension(FileManager.RemovePath(fileName));

            ICodeBlock codeBlock = topBlock.Namespace(GlueState.Self.ProjectNamespace);

            codeBlock = codeBlock.Class("", "GameNetworkConfiguration : RedGrin.NetworkConfiguration");

            var constructor = codeBlock.Constructor("public", "GameNetworkConfiguration", "");


            var portNumber = projectName.GetHashCode() % (ushort.MaxValue - 1024) + 1024;


            constructor.Line($"ApplicationName = \"{projectName}\";");
            constructor.Line($"ApplicationPort = {portNumber};");
            constructor.Line($"DeadReckonSeconds = 1.0f;");
            constructor.Line($"EntityStateTypes = new System.Collections.Generic.List<System.Type>();");

            var netEntities = GlueState.Self.CurrentGlueProject.Entities
                              .Where(item => NetworkEntityViewModel.IsNetworked(item));

            foreach (var netEntity in netEntities)
            {
                var netStateFullName = CodeGeneratorCommonLogic.GetNetStateFullName(netEntity);
                constructor.Line(
                    $"EntityStateTypes.Add(typeof({netStateFullName}));");
            }
            return(topBlock.ToString());
        }
        public static void GenerateCodeFor(ScreenSave screenSave, bool save = false)
        {
            var isNetworkScreen = NetworkScreenViewModel.IsNetworked(screenSave);

            if (isNetworkScreen)
            {
                var screenGeneratedCode            = GetGeneratedScreenCode(screenSave);
                var generatedScreenNetworkFilePath = CodeGeneratorCommonLogic.GetGeneratedElementNetworkFilePathFor(screenSave);

                CodeGeneratorCommonLogic.SaveFile(screenGeneratedCode, generatedScreenNetworkFilePath);
                CodeGeneratorCommonLogic.AddCodeFileToProject(generatedScreenNetworkFilePath);

                var customScreenNetworkFilePath = CodeGeneratorCommonLogic.GetCustomElementNetworkFilePathFor(screenSave);
                if (customScreenNetworkFilePath.Exists() == false)
                {
                    var customScreenNetworkCode = GenerateEmptyCustomScreenNetworkCode(screenSave);
                    CodeGeneratorCommonLogic.SaveFile(customScreenNetworkCode, customScreenNetworkFilePath);
                }
                CodeGeneratorCommonLogic.AddCodeFileToProject(customScreenNetworkFilePath);

                GlueCommands.Self.ProjectCommands.MakeGeneratedCodeItemsNested();

                if (save)
                {
                    GlueCommands.Self.ProjectCommands.SaveProjects();
                }
            }
        }
        public static void GenerateConfiguration()
        {
            var configurationCode = GenerateConfigurationCode();

            var filePath = GetConfigurationFilePath();

            CodeGeneratorCommonLogic.SaveFile(configurationCode, filePath);
            CodeGeneratorCommonLogic.AddCodeFileToProject(filePath);
        }
Ejemplo n.º 4
0
        private static void GenerateClaimEntityMessage()
        {
            var code = GenerateClaimEntityMessageCode();

            var filePath = GetClaimEntityFilePath();

            CodeGeneratorCommonLogic.SaveFile(code, filePath);

            CodeGeneratorCommonLogic.AddCodeFileToProject(filePath);
        }
        public static IEnumerable <FilePath> GetAllNetworkFilesFor(EntitySave entitySave)
        {
            yield return(CodeGeneratorCommonLogic.GetGeneratedElementNetworkFilePathFor(entitySave));

            yield return(CodeGeneratorCommonLogic.GetCustomElementNetworkFilePathFor(entitySave));

            yield return(CodeGeneratorCommonLogic.GetGeneratedNetStateFilePathFor(entitySave));

            yield return(CodeGeneratorCommonLogic.GetCustomNetStateFilePathFor(entitySave));
        }
Ejemplo n.º 6
0
        private static string GenerateEmptyCustomNetStateCode(EntitySave entitySave)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string netStateNamespace = CodeGeneratorCommonLogic.GetNetStateNamespace(entitySave);

            ICodeBlock codeBlock = topBlock.Namespace(netStateNamespace);

            codeBlock = codeBlock.Class("public partial", entitySave.GetStrippedName() + "NetState");

            return(topBlock.ToString());
        }
Ejemplo n.º 7
0
        private static string GenerateEmptyCustomEntityNetworkCode(EntitySave entitySave)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string entityNamespace = CodeGeneratorCommonLogic.GetElementNamespace(entitySave);

            ICodeBlock codeBlock = topBlock.Namespace(entityNamespace);

            codeBlock = codeBlock.Class("public partial", entitySave.GetStrippedName());

            codeBlock.Function("void", "CustomUpdateFromState", $"{CodeGeneratorCommonLogic.GetNetStateFullName(entitySave)} state");
            codeBlock.Function("void", "CustomGetState", $"{CodeGeneratorCommonLogic.GetNetStateFullName(entitySave)} state");



            return(topBlock.ToString());
        }
        private static string GetGeneratedScreenCode(ScreenSave screenSave)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string screenNamespace = CodeGeneratorCommonLogic.GetElementNamespace(screenSave);

            ICodeBlock codeBlock = topBlock.Namespace(screenNamespace);

            codeBlock = codeBlock.Class("public partial", screenSave.GetStrippedName(), " : RedGrin.INetworkArena");

            GenerateRequestCreateEntity(codeBlock);

            GenerateRequestDestroy(codeBlock);



            return(topBlock.ToString());
        }
Ejemplo n.º 9
0
        private static void GenerateGetStateMethod(EntitySave entitySave, ICodeBlock codeBlock)
        {
            var netStateFullName = CodeGeneratorCommonLogic.GetNetStateFullName(entitySave);

            var getStateFunc = codeBlock.Function("public object", "GetState");

            getStateFunc.Line($"var state = new {netStateFullName}();");
            var variables = GetNetworkVariables(entitySave);

            foreach (var variable in variables)
            {
                getStateFunc.Line($"state.{variable.Name} = this.{variable.Name};");
            }

            getStateFunc.Line("CustomGetState(state);");

            getStateFunc.Line("return state;");
        }
Ejemplo n.º 10
0
        private static string GetGeneratedEntityNetworkCode(EntitySave entitySave)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string entityNamespace = CodeGeneratorCommonLogic.GetElementNamespace(entitySave);

            ICodeBlock codeBlock = topBlock.Namespace(entityNamespace);

            codeBlock = codeBlock.Class("public partial", entitySave.GetStrippedName(), " : RedGrin.INetworkEntity");

            codeBlock.AutoProperty("public long", "OwnerId");
            codeBlock.AutoProperty("public long", "EntityId");

            GenerateGetStateMethod(entitySave, codeBlock);

            GenerateUpdateFromStateMethod(entitySave, codeBlock);

            return(topBlock.ToString());
        }
Ejemplo n.º 11
0
        private static string GenerateNetStateGeneratedCode(EntitySave entitySave)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string netStateNamespace = CodeGeneratorCommonLogic.GetNetStateNamespace(entitySave);

            ICodeBlock codeBlock = topBlock.Namespace(netStateNamespace);

            codeBlock = codeBlock.Class("public partial", entitySave.GetStrippedName() + "NetState");

            var variables = GetNetworkVariables(entitySave);

            foreach (var variable in variables)
            {
                codeBlock.AutoProperty($"public {variable.Type}", variable.Name);
            }

            return(topBlock.ToString());
        }
Ejemplo n.º 12
0
        public static void GenerateCodeFor(EntitySave entitySave, bool save = true)
        {
            var isNetworkEntity = NetworkEntityViewModel.IsNetworked(entitySave);

            if (isNetworkEntity)
            {
                var entityGeneratedCode            = GetGeneratedEntityNetworkCode(entitySave);
                var generatedEntityNetworkFilePath = CodeGeneratorCommonLogic.GetGeneratedElementNetworkFilePathFor(entitySave);

                CodeGeneratorCommonLogic.SaveFile(entityGeneratedCode, generatedEntityNetworkFilePath);
                CodeGeneratorCommonLogic.AddCodeFileToProject(generatedEntityNetworkFilePath);

                var netStateGeneratedCode     = GenerateNetStateGeneratedCode(entitySave);
                var generatedNetStateFilePath = CodeGeneratorCommonLogic.GetGeneratedNetStateFilePathFor(entitySave);

                CodeGeneratorCommonLogic.SaveFile(netStateGeneratedCode, generatedNetStateFilePath);
                CodeGeneratorCommonLogic.AddCodeFileToProject(generatedNetStateFilePath);

                var customNetStateFilePath = CodeGeneratorCommonLogic.GetCustomNetStateFilePathFor(entitySave);
                if (customNetStateFilePath.Exists() == false)
                {
                    var customNetStateCode = GenerateEmptyCustomNetStateCode(entitySave);
                    CodeGeneratorCommonLogic.SaveFile(customNetStateCode, customNetStateFilePath);
                }
                CodeGeneratorCommonLogic.AddCodeFileToProject(customNetStateFilePath);

                var customEntityNetworkFilePath = CodeGeneratorCommonLogic.GetCustomElementNetworkFilePathFor(entitySave);
                if (customEntityNetworkFilePath.Exists() == false)
                {
                    var customEntityNetworkCode = GenerateEmptyCustomEntityNetworkCode(entitySave);
                    CodeGeneratorCommonLogic.SaveFile(customEntityNetworkCode, customEntityNetworkFilePath);
                }
                CodeGeneratorCommonLogic.AddCodeFileToProject(customEntityNetworkFilePath);

                GlueCommands.Self.ProjectCommands.MakeGeneratedCodeItemsNested();

                if (save)
                {
                    GlueCommands.Self.ProjectCommands.SaveProjects();
                }
            }
        }
Ejemplo n.º 13
0
        private static void GenerateUpdateFromStateMethod(EntitySave entitySave, ICodeBlock codeBlock)
        {
            var functionBlock = codeBlock.Function("public void", "UpdateFromState", "object entityState, double stateTime");

            var variables = GetNetworkVariables(entitySave);

            var netStateFullName = CodeGeneratorCommonLogic.GetNetStateFullName(entitySave);

            functionBlock.Line($"var state = entityState as {netStateFullName};");

            if (variables.Any())
            {
                foreach (var variable in variables)
                {
                    functionBlock.Line($"this.{variable.Name} = state.{variable.Name};");
                }
            }

            functionBlock.Line("CustomUpdateFromState(state);");
        }
        private static string GenerateEmptyCustomScreenNetworkCode(ScreenSave screen)
        {
            ICodeBlock topBlock = new CodeBlockBaseNoIndent(null);

            string screenNamespace = CodeGeneratorCommonLogic.GetElementNamespace(screen);

            ICodeBlock codeBlock = topBlock.Namespace(screenNamespace);

            codeBlock = codeBlock.Class("public partial", screen.GetStrippedName());

            codeBlock.Function("void",
                               "CustomRequestCreateNetworkEntity",
                               "ref RedGrin.INetworkEntity entity, object entityData")
            .Line();

            codeBlock.Line();

            codeBlock.Function("void",
                               "CustomRequestDestroyNetworkEntity",
                               "RedGrin.INetworkEntity entity")
            .Line();

            return(topBlock.ToString());
        }
        private static void GenerateRequestCreateEntity(ICodeBlock codeBlock)
        {
            var requestCreateMethod = codeBlock.Function(
                "public RedGrin.INetworkEntity", "RequestCreateEntity", "long ownerId, object entityData");

            requestCreateMethod.Line("RedGrin.INetworkEntity entity = null;");

            bool needsElseIf = false;

            var netEntities = GlueState.Self.CurrentGlueProject.Entities
                              .Where(item => NetworkEntityViewModel.IsNetworked(item));

            foreach (var entitySave in netEntities)
            {
                ICodeBlock ifBlock;

                var    fullNetStateType = CodeGeneratorCommonLogic.GetNetStateFullName(entitySave);
                var    fullEntityType   = CodeGeneratorCommonLogic.GetElementFullName(entitySave);
                string ifcontents       = $"entityData is {fullNetStateType}";

                if (needsElseIf == false)
                {
                    ifBlock = requestCreateMethod.If(ifcontents);
                }
                else
                {
                    ifBlock = requestCreateMethod.ElseIf(ifcontents);
                }

                var hasFactory = entitySave.CreatedByOtherEntities;

                if (hasFactory)
                {
                    var factoryName = $"{GlueState.Self.ProjectNamespace}.Factories.{entitySave.GetStrippedName()}Factory";
                    ifBlock.Line($"entity = {factoryName}.CreateNew();");
                }
                else
                {
                    ifBlock.Line($"entity = new {fullEntityType}();");
                }

                // Even though the NetworkManager assigns the owner ID, we're going to do it here
                // to before calling UpdateFromState, so that any custom code that gets triggered from
                // UpdateFromState are guaranteed to have the right ID:

                ifBlock.Line("entity.OwnerId = ownerId;");

                ifBlock.Line("entity.UpdateFromState(entityData, 0);");

                needsElseIf = true;
            }

            // At first I thought to have the CustomRequestCreateNetworkEntity
            // inside the if/else if so that the created entity could be modified
            // but it's possible the user may want to have their own totally custom
            // network entities and network entity states, in which case they will need
            // to instantiate the object fully in custom code. Therefore, we'll call the
            // method no matter what, even if the entity is null
            requestCreateMethod.Line("CustomRequestCreateNetworkEntity(ref entity, entityData);");

            requestCreateMethod.Line("return entity;");
        }