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());
        }
Beispiel #2
0
        public void GenerateConstructor(ElementSave elementSave, ICodeBlock currentBlock, string runtimeClassName)
        {
            string baseCall = null;

            bool hasBase = !string.IsNullOrEmpty(elementSave.BaseType);

            if (hasBase)
            {
                baseCall = "base(fullInstantiation, false)";
            }
            var constructor = currentBlock.Constructor("public", runtimeClassName, "bool fullInstantiation = true, bool callAssignReferences = true", baseCall);

            bool hasEvents            = elementSave.DefaultState.GetValueOrDefault <bool>("HasEvents");
            bool exposeChildrenEvents = elementSave.DefaultState.GetValueOrDefault <bool>("ExposeChildrenEvents");

            constructor.Line($"this.HasEvents = {hasEvents.ToString().ToLower()};");
            constructor.Line($"this.ExposeChildrenEvents = {exposeChildrenEvents.ToString().ToLower()};");

            var ifStatement = constructor.If("fullInstantiation");

            string componentScreenOrStandard = null;

            if (elementSave is ComponentSave)
            {
                componentScreenOrStandard = "Components";
            }
            else if (elementSave is ScreenSave)
            {
                componentScreenOrStandard = "Screens";
            }
            else if (elementSave is StandardElementSave)
            {
                componentScreenOrStandard = "StandardElements";
            }

            ifStatement.Line("Gum.DataTypes.ElementSave elementSave = Gum.Managers.ObjectFinder.Self.GumProjectSave." +
                             componentScreenOrStandard + ".First(item => item.Name == \"" + elementSave.Name.Replace("\\", "\\\\") + "\");");
            ifStatement.Line("this.ElementSave = elementSave;");
            ifStatement.Line("string oldDirectory = FlatRedBall.IO.FileManager.RelativeDirectory;");

            ifStatement.Line("FlatRedBall.IO.FileManager.RelativeDirectory = FlatRedBall.IO.FileManager.GetDirectory(Gum.Managers.ObjectFinder.Self.GumProjectSave.FullFileName);");



            ifStatement.Line("GumRuntime.ElementSaveExtensions.SetGraphicalUiElement(elementSave, this, RenderingLibrary.SystemManagers.Default);");

            ifStatement.Line("FlatRedBall.IO.FileManager.RelativeDirectory = oldDirectory;");

            var innerIf = ifStatement.If("callAssignReferences");

            innerIf.Line("this.AssignReferences();");
            // This used to be called in AddtoManagers, but that
            // doesn't get called for all objects - specifically on
            // runtimes created in code and added to children.
        }
Beispiel #3
0
        public void GenerateConstructor(ElementSave elementSave, ICodeBlock currentBlock, string runtimeClassName)
        {
            string baseCall = null;

            bool hasBase = !string.IsNullOrEmpty(elementSave.BaseType);

            if (hasBase)
            {
                baseCall = "base(false, tryCreateFormsObject)";
            }
            var constructor = currentBlock.Constructor("public", runtimeClassName, "bool fullInstantiation = true, bool tryCreateFormsObject = true", baseCall);

            // This may not have a value, so if not, don't set it:
            var state = elementSave.DefaultState;

            string throwaway;

            if (GetIfShouldGenerateFormsCode(elementSave, out throwaway))
            {
                constructor.Line("this.tryCreateFormsObject = tryCreateFormsObject;");
            }

            // State can be null if the backing file for this element
            // doesn't exist. We can tolerate it because the error window
            // in Glue will report it as an error
            if (state?.GetVariableSave("HasEvents") != null)
            {
                bool hasEvents = state.GetValueOrDefault <bool>("HasEvents");
                constructor.Line($"this.HasEvents = {hasEvents.ToString().ToLower()};");
            }

            if (state?.GetVariableSave("ExposeChildrenEvents") != null)
            {
                bool exposeChildrenEvents = state.GetValueOrDefault <bool>("ExposeChildrenEvents");
                constructor.Line($"this.ExposeChildrenEvents = {exposeChildrenEvents.ToString().ToLower()};");
            }

            var ifStatement = constructor.If("fullInstantiation");

            string componentScreenOrStandard = null;

            if (elementSave is ComponentSave)
            {
                componentScreenOrStandard = "Components";
            }
            else if (elementSave is Gum.DataTypes.ScreenSave)
            {
                componentScreenOrStandard = "Screens";
            }
            else if (elementSave is StandardElementSave)
            {
                componentScreenOrStandard = "StandardElements";
            }

            ifStatement.Line("Gum.DataTypes.ElementSave elementSave = Gum.Managers.ObjectFinder.Self.GumProjectSave." +
                             componentScreenOrStandard + ".First(item => item.Name == \"" + elementSave.Name.Replace("\\", "\\\\") + "\");");
            ifStatement.Line("this.ElementSave = elementSave;");
            ifStatement.Line("string oldDirectory = FlatRedBall.IO.FileManager.RelativeDirectory;");

            ifStatement.Line("FlatRedBall.IO.FileManager.RelativeDirectory = FlatRedBall.IO.FileManager.GetDirectory(Gum.Managers.ObjectFinder.Self.GumProjectSave.FullFileName);");

            ifStatement.Line("GumRuntime.ElementSaveExtensions.SetGraphicalUiElement(elementSave, this, RenderingLibrary.SystemManagers.Default);");

            ifStatement.Line("FlatRedBall.IO.FileManager.RelativeDirectory = oldDirectory;");
        }