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());
        }
예제 #2
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());
        }
예제 #3
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;");
        }
예제 #4
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 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;");
        }