Exemple #1
0
        private static void GenerateInitializeAsync(ICodeBlock currentBlock)
        {
            currentBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");
            currentBlock.Line("NamedDelegate namedDelegate = new NamedDelegate();");

            foreach (ReferencedFileSave rfs in ProjectManager.GlueProjectSave.GlobalFiles)
            {
                if (!ReferencedFileSaveCodeGenerator.IsRfsHighPriority(rfs) && !rfs.LoadedOnlyWhenReferenced && rfs.LoadedAtRuntime)
                {
                    currentBlock.Line("namedDelegate.Name = \"" + rfs.Name + "\";");
                    currentBlock.Line("namedDelegate.LoadMethod = Load" + rfs.Name.Replace("/", "_").Replace(".", "_") + ";");
                    currentBlock.Line("LoadMethodList.Add( namedDelegate );");
                }
            }

            currentBlock._();

            currentBlock.Line("#if WINDOWS_8");
            currentBlock.Line("System.Threading.Tasks.Task.Run((System.Action)AsyncInitialize);");
            currentBlock.Line("#else");

            currentBlock.Line("ThreadStart threadStart = new ThreadStart(AsyncInitialize);");
            currentBlock.Line("Thread thread = new Thread(threadStart);");
            currentBlock.Line("thread.Name = \"GlobalContent Async load\";");
            currentBlock.Line("thread.Start();");
            currentBlock.Line("#endif");
            currentBlock.Line("#endif");
        }
Exemple #2
0
        public static void CreateEmptyCodeIfNecessary(IElement currentElement, string fullFileName, bool forceRegenerateContents)
        {
            bool doesFileExist = File.Exists(fullFileName);

            if (!doesFileExist)
            {
                PluginManager.ReceiveOutput("Forcing a regneration of " + fullFileName + " because Glue can't find it anywhere.");

                // There is no shared code file for this event, so we need to make one
                ProjectManager.CodeProjectHelper.CreateAndAddPartialCodeFile(fullFileName, true);
            }

            if (!doesFileExist || forceRegenerateContents)
            {
                string namespaceString = GlueCommands.Self.GenerateCodeCommands.GetNamespaceForElement(currentElement);


                ICodeBlock templateCodeBlock = new CodeDocument();

                EventCodeGenerator.AddUsingStatementsToBlock(templateCodeBlock);

                ICodeBlock namespaceCB = templateCodeBlock.Namespace(namespaceString);

                ICodeBlock classCB = namespaceCB.Class("public partial", currentElement.ClassName, null);

                classCB
                ._()
                .End()
                .End();

                string templateToSave = templateCodeBlock.ToString();
                FlatRedBall.Glue.IO.FileWatchManager.IgnoreNextChangeOnFile(fullFileName);
                FileManager.SaveText(templateToSave, fullFileName);
            }

            // Add if it isn't part of the project
            const bool saveFile = false; // don't save it - we just want to make sure it's part of the project

            ProjectManager.CodeProjectHelper.CreateAndAddPartialCodeFile(fullFileName, saveFile);
        }
        public string GetRuntimeRegistrationPartialClassContents(bool registerFormsAssociations)
        {
            CodeBlockBase codeBlock = new CodeBlockBase(null);

            ICodeBlock currentBlock = codeBlock.Namespace("FlatRedBall.Gum");

            currentBlock = currentBlock.Class("public ", "GumIdbExtensions", "");

            currentBlock = currentBlock.Function("public static void", "RegisterTypes", "");
            {
                AddAssignmentFunctionContents(currentBlock);

                if (registerFormsAssociations)
                {
                    currentBlock._();
                    AddFormsAssociations(currentBlock);
                }
            }


            return(codeBlock.ToString());
        }
Exemple #4
0
        public void CodeGenerationStart(IElement element)
        {
            var stateChainCollection = GlueCommands.TreeNodeCommands.GetProperty <StateChainCollection>(element, PropertyName);

            if (stateChainCollection == null)
            {
                return;
            }

            var        elementNameWithoutPath = FileManager.RemovePath(element.Name);
            var        document  = new CodeDocument();
            ICodeBlock codeBlock = document;


            if (stateChainCollection.StateChains.Count <= 0)
            {
                return;
            }


            codeBlock = codeBlock
                        .Line("using FlatRedBall.Instructions;")
                        .Namespace(GlueCommands.GenerateCodeCommands.GetNamespaceForElement(element))
                        .Class("public partial ", element.ClassName, "");


            //Create Enum
            codeBlock = codeBlock
                        .Enum("public", "StateChains")
                        .Line("None = 0,");

            for (int i = 0; i < stateChainCollection.StateChains.Count; i++)
            {
                if (i == stateChainCollection.StateChains.Count - 1)
                {
                    codeBlock.Line(stateChainCollection.StateChains[i].Name);
                }
                else
                {
                    codeBlock.Line(stateChainCollection.StateChains[i].Name + ",");
                }
            }

            codeBlock = codeBlock.End();

            //Private members
            codeBlock
            ._()
            .Line("private StateChains _currentStateChain = StateChains.None;")
            .Line("private int _index;")
            .Line("private Instruction _instruction;")
            ._();

            //CurrentStateChain Property
            codeBlock = codeBlock
                        .Property("public StateChains", "CurrentStateChain")
                        .Get()
                        .Line("return _currentStateChain;")
                        .End()
                        .Set()
                        .Line("StopStateChain();")
                        ._()
                        .Line("_currentStateChain = value;")
                        .Line("_index = 0;")
                        ._()
                        .Switch("_currentStateChain");

            foreach (var stateChain in stateChainCollection.StateChains)
            {
                codeBlock
                .Case("StateChains." + stateChain.Name)
                .Line("StartNextState" + stateChain.Name + "();");
            }

            codeBlock = codeBlock
                        .End()
                        .End()
                        .End();

            codeBlock._();

            //ManageStateChains
            codeBlock = codeBlock
                        .Function("public void", "ManageStateChains", "")
                        .If("CurrentStateChain == StateChains.None")
                        .Line("return;")
                        .End()
                        ._()
                        .Switch("CurrentStateChain");

            foreach (var stateChain in stateChainCollection.StateChains)
            {
                var index = 0;

                codeBlock = codeBlock
                            .Case("StateChains." + stateChain.Name);

                foreach (var stateChainState in
                         stateChain.StateChainStates.Where(stateChainState => !string.IsNullOrEmpty(stateChainState.State)))
                {
                    if (index == 0)
                    {
                        codeBlock
                        .If("_index == 0 && CurrentState == VariableState." + stateChainState.State)
                        .Line("_index++;")
                        .Line("StartNextState" + stateChain.Name + "();");
                    }
                    else
                    {
                        codeBlock
                        .ElseIf("_index == " + index + " && CurrentState == VariableState." +
                                stateChainState.State)
                        .Line("_index++;")
                        .Line("StartNextState" + stateChain.Name + "();");
                    }

                    index++;
                }

                codeBlock = codeBlock
                            .End();
            }

            codeBlock = codeBlock
                        .End()
                        .End();

            codeBlock._();

            //StopStateChain
            codeBlock = codeBlock
                        .Function("public void", "StopStateChain", "")
                        .If("CurrentStateChain == StateChains.None")
                        .Line("return;")
                        .End()
                        ._()
                        .Switch("CurrentStateChain");

            foreach (var stateChain in stateChainCollection.StateChains)
            {
                var index = 0;

                codeBlock = codeBlock
                            .Case("StateChains." + stateChain.Name);

                foreach (var stateChainState in stateChain.StateChainStates)
                {
                    if (index == 0)
                    {
                        codeBlock
                        .If("_index == 0")
                        .Line("Instructions.Remove(_instruction);")
                        .Line("StopStateInterpolation(VariableState." + stateChainState.State + ");")
                        .End();
                    }
                    else
                    {
                        codeBlock
                        .ElseIf("_index == " + index)
                        .Line("Instructions.Remove(_instruction);")
                        .Line("StopStateInterpolation(VariableState." + stateChainState.State + ");")
                        .End();
                    }

                    index++;
                }

                codeBlock = codeBlock
                            .End();
            }

            codeBlock = codeBlock
                        .End()
                        .Line("_instruction = null;")
                        .End();

            codeBlock._();

            //StartNextState*****
            foreach (var stateChain in stateChainCollection.StateChains)
            {
                codeBlock = codeBlock
                            .Function("private void", "StartNextState" + stateChain.Name, "")
                            .If("_index < 0")
                            .Line("_index = 0;")
                            .End()
                            ._()
                            .If("_index >= " + stateChain.StateChainStates.Count)
                            .Line("_index = 0;")
                            .End()
                            ._()
                            .Switch("_index");

                var index = 0;

                foreach (var stateChainState in stateChain.StateChainStates)
                {
                    codeBlock
                    .Case(index.ToString())
                    .Line("_instruction = InterpolateToState(VariableState." + stateChainState.State + ", " + stateChainState.Time / 1000 + ");");

                    index++;
                }

                codeBlock = codeBlock
                            .End()
                            .End()
                            ._();
            }

            GlueCommands.ProjectCommands.CreateAndAddPartialFile(element, "StateChains", document.ToString());
        }
Exemple #5
0
        internal static ICodeBlock GenerateGeneralActivity(ICodeBlock codeBlock, IElement saveObject)
        {


            bool isEntity = saveObject is EntitySave;

            EntitySave entitySave = saveObject as EntitySave;

            // This code might seem a little weird.  The reason we do this
            // is because when an Entity is paused, it has a method that is
            // called.  However, when it is unpaused, there's just an instruction
            // that is executed - there is no event.  But if a Screen is paused, then
            // objects within that Screen don't get unpaused....so we're going to bet on
            // the Activity function only being called in unpaused Screens.  If this causes
            // probelsm we may have to make something a little more standard like an Unpause
            // method.
            if (isEntity &&
                (entitySave.ImplementsIClickable || entitySave.ImplementsIWindow)
                && !entitySave.GetInheritsFromIWindowOrIClickable()
                )
            {
                codeBlock.Line("mIsPaused = false;");
            }

            #region Call base.Activity if it has a derived object

            // We only need to do this for EntitySaves.  Screens inherit from the
            // Screen class so they ALWAYS call base.Activity.  It's in the generated
            // Screen template.  
            if ( saveObject.InheritsFromEntity())
            {
                codeBlock.Line("base.Activity();");
            }

            #endregion

            codeBlock._();

            // Eventually do we want to move this in the generate activity for custom variable code gen.
            CustomVariableCodeGenerator.WriteVelocityForCustomVariables(saveObject.CustomVariables, codeBlock);


            foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators)
            {

                codeGenerator.GenerateActivity(codeBlock, saveObject);
            }


            return codeBlock;
        }
Exemple #6
0
        private static bool GenerateLoadAsyncCode(ICodeBlock classLevelBlock, ICodeBlock initializeBlock)
        {
            bool loadAsync = ProjectManager.GlueProjectSave.GlobalContentSettingsSave.LoadAsynchronously;

            if (loadAsync)
            {
                GenerateInitializeAsync(initializeBlock);
            }

            loadAsync = ProjectManager.GlueProjectSave.GlobalContentSettingsSave.LoadAsynchronously;
            if (loadAsync)
            {
                classLevelBlock._();


                classLevelBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");

                classLevelBlock
                .Function("static void", "RequestContentLoad", "string contentName")
                .Lock("LoadMethodList")
                .Line("int index = -1;")
                .For("int i = 0; i < LoadMethodList.Count; i++")
                .If("LoadMethodList[i].Name == contentName")
                .Line("index = i;")
                .Line("break;")
                .End()
                .End()
                .If("index != -1")
                .Line("NamedDelegate delegateToShuffle = LoadMethodList[index];")
                .Line("LoadMethodList.RemoveAt(index);")
                .Line("LoadMethodList.Insert(0, delegateToShuffle);")
                .End()

                .End()
                .End();
                classLevelBlock.Line("#endif");

                classLevelBlock._();

                classLevelBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");
                classLevelBlock
                .Function("static void", "AsyncInitialize", "")

                .Line("#if XBOX360")
                .Line("// We can not use threads 0 or 2")
                .Line("// Async screen loading uses thread 4, so we'll use 3 here")
                .Line("Thread.CurrentThread.SetProcessorAffinity(3);")
                .Line("#endif")

                .Line("bool shouldLoop = LoadMethodList.Count != 0;")

                .While("shouldLoop")
                .Line("System.Action action = null;")
                .Lock("LoadMethodList")

                .Line("action = LoadMethodList[0].LoadMethod;")
                .Line("LoadMethodList.RemoveAt(0);")
                .Line("shouldLoop = LoadMethodList.Count != 0 && !ShouldStopLoading;")

                .End()
                .Line("action();")
                .End()
                .Line("IsInitialized = true;")
                ._()
                .End();
                classLevelBlock.Line("#endif");

                //stringBuilder.AppendLine("\t\t\tstring ContentManagerName = \"Global\";");
            }

            return(loadAsync);
        }
        private static void GenerateInitializeAsync(ICodeBlock currentBlock)
        {
            currentBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");
            currentBlock.Line("NamedDelegate namedDelegate = new NamedDelegate();");

            foreach (ReferencedFileSave rfs in ProjectManager.GlueProjectSave.GlobalFiles)
            {
                if (!IsRfsHighPriority(rfs) && !rfs.LoadedOnlyWhenReferenced && rfs.LoadedAtRuntime)
                {
                    currentBlock.Line("namedDelegate.Name = \"" + rfs.Name + "\";");
                    currentBlock.Line("namedDelegate.LoadMethod = Load" + rfs.Name.Replace("/", "_").Replace(".", "_") + ";");
                    currentBlock.Line("LoadMethodList.Add( namedDelegate );");
                }
            }

            currentBlock._();

            currentBlock.Line("#if WINDOWS_8");
            currentBlock.Line("System.Threading.Tasks.Task.Run((System.Action)AsyncInitialize);");
            currentBlock.Line("#else");

            currentBlock.Line("ThreadStart threadStart = new ThreadStart(AsyncInitialize);");
            currentBlock.Line("Thread thread = new Thread(threadStart);");
            currentBlock.Line("thread.Name = \"GlobalContent Async load\";");
            currentBlock.Line("thread.Start();");
            currentBlock.Line("#endif");
            currentBlock.Line("#endif");
        }
        private static bool GenerateLoadAsyncCode(ICodeBlock classLevelBlock, ICodeBlock initializeBlock)
        {
            bool loadAsync = ProjectManager.GlueProjectSave.GlobalContentSettingsSave.LoadAsynchronously;

            if (loadAsync)
            {
                GenerateInitializeAsync(initializeBlock);
            }

            loadAsync = ProjectManager.GlueProjectSave.GlobalContentSettingsSave.LoadAsynchronously;
            if (loadAsync)
            {

                classLevelBlock._();


                classLevelBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");

                classLevelBlock
                    .Function("static void", "RequestContentLoad", "string contentName")
                        .Lock("LoadMethodList")
                            .Line("int index = -1;")
                            .For("int i = 0; i < LoadMethodList.Count; i++")
                                .If("LoadMethodList[i].Name == contentName")
                                    .Line("index = i;")
                                    .Line("break;")
                                .End()
                            .End()
                            .If("index != -1")
                                .Line("NamedDelegate delegateToShuffle = LoadMethodList[index];")
                                .Line("LoadMethodList.RemoveAt(index);")
                                .Line("LoadMethodList.Insert(0, delegateToShuffle);")
                            .End()
                        .End()
                    .End();
                classLevelBlock.Line("#endif");

                classLevelBlock._();

                classLevelBlock.Line("#if !REQUIRES_PRIMARY_THREAD_LOADING");
                classLevelBlock
                    .Function("static void", "AsyncInitialize", "")

                        .Line("#if XBOX360")
                        .Line("// We can not use threads 0 or 2")
                        .Line("// Async screen loading uses thread 4, so we'll use 3 here")
                        .Line("Thread.CurrentThread.SetProcessorAffinity(3);")
                        .Line("#endif")

                        .Line("bool shouldLoop = LoadMethodList.Count != 0;")

                        .While("shouldLoop")
                            .Line("System.Action action = null;")
                            .Lock("LoadMethodList")

                                .Line("action = LoadMethodList[0].LoadMethod;")
                                .Line("LoadMethodList.RemoveAt(0);")
                                .Line("shouldLoop = LoadMethodList.Count != 0 && !ShouldStopLoading;")

                            .End()
                            .Line("action();")
                        .End()
                        .Line("IsInitialized = true;")
                        ._()
                    .End();
                classLevelBlock.Line("#endif");

                //stringBuilder.AppendLine("\t\t\tstring ContentManagerName = \"Global\";");

            }

            return loadAsync;
        }
        public override ICodeBlock GenerateDestroy(ICodeBlock codeBlock,  SaveClasses.IElement element)
        {
            for (int i = 0; i < element.ReferencedFiles.Count; i++)
            {
                codeBlock.InsertBlock(GetDestroyForReferencedFile(element, element.ReferencedFiles[i]));
            }
            codeBlock._();

            return codeBlock;
        }