Exemple #1
0
        internal static ICodeBlock GenerateFields(IElement saveObject)
        {
            ICodeBlock codeBlock = new CodeDocument(2);

            foreach (ElementComponentCodeGenerator codeGenerator in CodeWriter.CodeGenerators)
            {
                if (codeGenerator == null)
                {
                    throw new Exception("The CodeWriter contains a null code generator.  A plugin must have added this");
                }

                try
                {
                    codeGenerator.GenerateFields(codeBlock, saveObject);

                }
                catch (Exception e)
                {
                    throw new Exception("Error generating fields in generator " + codeGenerator.GetType().Name + 
                        "\n\n" + e.ToString());

                }
            }

            PerformancePluginCodeGenerator.GenerateFields(saveObject, codeBlock);

            EntitySave asEntitySave = saveObject as EntitySave;

            // No need to create LayerProvidedByContainer if this inherits from another object.
            if (asEntitySave != null && !saveObject.InheritsFromEntity())
            {
                // Add the layer that is going to get assigned in generated code
                codeBlock.Line("protected FlatRedBall.Graphics.Layer LayerProvidedByContainer = null;");
            }
            

            return codeBlock;


        }
Exemple #2
0
        internal static ICodeBlock GenerateInitialize(IElement saveObject)
        {

            ICodeBlock codeBlock = new CodeDocument(3);
            ICodeBlock currentBlock = codeBlock;

            // Start measuring performance before anything else
            PerformancePluginCodeGenerator.GenerateStartTimingInitialize(saveObject, codeBlock);

            PerformancePluginCodeGenerator.SaveObject = saveObject;
            PerformancePluginCodeGenerator.CodeBlock = codeBlock;

            PerformancePluginCodeGenerator.GenerateStart("CustomLoadStaticContent from Initialize");

            // Load static content before looping through the CodeGenerators
            // The reason for this is there is a ReferencedFileSaveCodeGenerator
            // which needs to work with static RFS's which are instantiated here
            currentBlock.Line("LoadStaticContent(ContentManagerName);");

            PerformancePluginCodeGenerator.GenerateEnd();


            PerformancePluginCodeGenerator.GenerateStart("General Initialize internals");

            foreach (ElementComponentCodeGenerator codeGenerator in CodeGenerators)
            {
                codeGenerator.GenerateInitialize(codeBlock, saveObject);
            }




            foreach (ElementComponentCodeGenerator codeGenerator in CodeGenerators)
            {
                codeGenerator.GenerateInitializeLate(codeBlock, saveObject);
            }

            if (saveObject is ScreenSave)
            {
                ScreenSave asScreenSave = saveObject as ScreenSave;
                codeBlock._();

                if (asScreenSave.IsRequiredAtStartup)
                {
                    string startupScreen = ProjectManager.StartUpScreen;

                    string qualifiedName = ProjectManager.ProjectNamespace + "." + startupScreen.Replace("\\", ".");

                    codeBlock.Line(string.Format("this.NextScreen = typeof({0}).FullName;", qualifiedName));
                }

                if (asScreenSave.UseGlobalContent)
                {
                    // no need to do anything here because Screens are smart enough to know to not load if they
                    // are using global content
                }

                if (!string.IsNullOrEmpty(asScreenSave.NextScreen))
                {
                    string nameToUse = ProjectManager.ProjectNamespace + "." + asScreenSave.NextScreen.Replace("\\", ".");

                    codeBlock.Line(string.Format("this.NextScreen = typeof({0}).FullName;", nameToUse));
                }

            }











            codeBlock._();
            PerformancePluginCodeGenerator.GenerateEnd();

            #region PostInitializeCode

            PerformancePluginCodeGenerator.GenerateStart("Post Initialize");


            if (saveObject.InheritsFromElement() == false)
            {
                codeBlock.Line("PostInitialize();");
            }
            PerformancePluginCodeGenerator.GenerateEnd();

            #endregion

            PerformancePluginCodeGenerator.GenerateStart("Base.Initialize");


            InheritanceCodeWriter.Self.WriteBaseInitialize(saveObject, codeBlock);

            PerformancePluginCodeGenerator.GenerateEnd();

            // I think we want to set this after calling base.Initialize so that the base
            // has a chance to set values on derived objects
            PerformancePluginCodeGenerator.GenerateStart("Reset Variables");
            // Now that variables are set, we can record reset variables
            NamedObjectSaveCodeGenerator.AssignResetVariables(codeBlock, saveObject);
            PerformancePluginCodeGenerator.GenerateEnd();

            PerformancePluginCodeGenerator.GenerateStart("AddToManagers");


            #region If shouldCallAddToManagers, call AddToManagers
            bool shouldCallAddToManagers = !saveObject.InheritsFromElement();
            if (shouldCallAddToManagers)
            {
                currentBlock = currentBlock
                    .If("addToManagers");
                if (saveObject is ScreenSave)
                {
                    currentBlock.Line("AddToManagers();");
                }
                else
                {
                    currentBlock.Line("AddToManagers(null);");
                }
            }

            #endregion
            PerformancePluginCodeGenerator.GenerateEnd();

            PerformancePluginCodeGenerator.GenerateEndTimingInitialize(saveObject, codeBlock);

            return codeBlock;
        }
		public static ICodeBlock GetMethodCallForBehavior(string behaviorName, IBehaviorContainer container)
		{
		    ICodeBlock codeBlock = new CodeDocument();

            string returnString = GetRawBehaviorMethodHeader(behaviorName);

            if (returnString.StartsWith("//"))
            {
                codeBlock.Line(returnString);
                return codeBlock;
            }
            else
            {

                returnString = returnString.Trim();
                returnString = returnString.Replace("public ", "");
                returnString = returnString.Replace("private ", "");
                returnString = returnString.Replace("protected ", "");
                returnString = returnString.Replace("internal ", "");
                returnString = returnString.Replace("override ", "");
                returnString = returnString.Replace("virtual ", "");
                returnString = returnString.Replace("static ", "");

                // The first word is going to be the return value
                returnString = returnString.Substring(returnString.IndexOf(' ') + 1);


				List<BehaviorRequirement> requirements = 
					BehaviorManager.GetBehaviorRequirementsForBehavior(behaviorName);

				if (requirements.Count != 0)
				{
					// Let's clear out the argments and fill them with the objects that fulfill the requirements
					int start = returnString.IndexOf('(') + 1;
					int indexOfClose = returnString.IndexOf(')');

					string argumentList = returnString.Substring(start, indexOfClose - start) ;

					returnString = returnString.Replace(argumentList, "");

					argumentList = "";

					for (int i = 0; i < requirements.Count; i++)
					{
						//string requirementFulfiller = 
						string requirementFulfiller = container.GetFulfillerName(requirements[i]);

						argumentList += requirementFulfiller;

						if (i != requirements.Count - 1)
						{
							argumentList += ", ";
						}

					}
					returnString = returnString.Insert(start, argumentList);


				}

                codeBlock.Line(returnString + ";");
                return codeBlock;
            }
		}
Exemple #4
0
        public static ICodeBlock CreateClass(string namespaceName, string className, bool isPartial, List<TypedMemberBase> members,
            bool isStatic, List<string> usingStatements, Dictionary<string, string> untypedMembers, ICodeBlock methodContent)
        {
            var codeBlock = new CodeDocument();

            #region Append Using Statements
            foreach(var usingStatement in usingStatements.Distinct())
            {
                codeBlock.Line("using " + usingStatement + ";");
            }
            #endregion

            #region Append Namespace

            codeBlock._();

            ICodeBlock currentBlock = codeBlock;

            currentBlock = currentBlock.Namespace(namespaceName);

            #endregion

            #region Append class header

            currentBlock = currentBlock.Class(className, Public: true, Static: isStatic, Partial: isPartial);

            #endregion

            for (int i = 0; i < members.Count; i++)
            {
                TypedMemberBase member = members[i];


                bool isPublic = member.Modifier == Modifier.Public;
                bool isPrivate = member.Modifier == Modifier.Private;
                bool isInternal = member.Modifier == Modifier.Internal;

                string memberType = member.MemberType.ToString();

                memberType = PrepareTypeToBeWritten(member, memberType);

                // We used to remove whitespace here,
                // but the member name may contain an assignment.
                // In that case we want spaces preserved.  Whatever
                // calls this method is in charge of removing whitespace.
                string memberName = member.MemberName;

                currentBlock.Line(StringHelper.Modifiers(
                    Public: isPublic, 
                    Private: isPrivate,
                    Internal: isInternal,
                    Static: isStatic, 
                    Type: memberType, 
                    Name: memberName) + ";");
            }

            foreach (KeyValuePair<string, string> kvp in untypedMembers)
            {
                string memberName = kvp.Key;
                string type = kvp.Value;


                bool isPublic = !memberName.StartsWith("m");

                currentBlock.Line(StringHelper.Modifiers(Public: isPublic, Static: isStatic, Type: type, Name: memberName) + ";");
            }


            if (methodContent == null)
            {
                currentBlock.Tag("Methods");
            }
            else
            {
                currentBlock.InsertBlock(methodContent);
            }

            currentBlock._()._();

            currentBlock.Replace(" System.Single ", " float ");
            currentBlock.Replace(" System.Boolean ", " bool ");
            currentBlock.Replace(" System.Int32 ", " int ");
            currentBlock.Replace(" System.String ", " string ");

            return codeBlock;
        }
		private static string GetBehaviorContents(string behaviorName)
		{
		    ICodeBlock codeBlock = new CodeDocument();
            string fileName = BehaviorFolder + behaviorName + ".cs";

            if (File.Exists(fileName))
            {

                string behaviorContents = FileManager.FromFileText(fileName
                    );

                codeBlock.Line(behaviorContents);
            }
            else
            {
                codeBlock.Line("// There is an invalid behavior reference to behavior " + behaviorName);
            }

		    return codeBlock.ToString();
		}
        protected ICodeBlock GetDestroyForReferencedFile(IElement element, ReferencedFileSave referencedFile)
        {
            ICodeBlock codeBlock = new CodeDocument(3);

            ///////////////////////////////EARLY OUT///////////////////////
            if (!referencedFile.LoadedAtRuntime || !referencedFile.DestroyOnUnload)
            {
                return codeBlock;
            }

            if (referencedFile.GetGeneratesMember() == false)
            {
                return codeBlock;
            }

            /////////////////////////////END EARLY OUT/////////////////////

            AddIfConditionalSymbolIfNecesssary(codeBlock, referencedFile);

            string fileName = referencedFile.Name;
            AssetTypeInfo ati = referencedFile.GetAssetTypeInfo();
            string variableName = referencedFile.GetInstanceName();

            bool isScreenSave = element is ScreenSave;
            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                variableName = "m" + variableName;
                codeBlock = codeBlock.If(variableName + " != null");
            }
            if (ati != null && (!referencedFile.IsSharedStatic || isScreenSave))
            {
                string typeName = ati.RuntimeTypeName;
                string destroyMethod = ati.DestroyMethod;
                string recycleMethod = ati.RecycledDestroyMethod;
                if (string.IsNullOrEmpty(recycleMethod))
                {
                    recycleMethod = destroyMethod;
                }



                if (!string.IsNullOrEmpty(ati.DestroyMethod))
                {
                    if (isScreenSave && recycleMethod != destroyMethod)
                    {
                        codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                        codeBlock.Line(destroyMethod.Replace("this", variableName) + ";");
                        if (referencedFile.LoadedOnlyWhenReferenced)
                        {
                            codeBlock = codeBlock.End().ElseIf(variableName + " != null");
                        }
                        else
                        {
                            codeBlock = codeBlock.End().Else();
                        }
                        codeBlock.Line(recycleMethod.Replace("this", variableName) + ";");
                        codeBlock = codeBlock.End();

                    }
                    else
                    {
                        codeBlock.Line(destroyMethod.Replace("this", variableName) + ";");
                    }
                }

                if (ati.ShouldBeDisposed && element.UseGlobalContent == false)
                {
                    codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                    codeBlock.Line(string.Format("{0}.Dispose();", variableName));
                    codeBlock = codeBlock.End();
                }

                
            }

            if (element is ScreenSave && referencedFile.IsSharedStatic)
            {
                if (referencedFile.LoadedOnlyWhenReferenced)
                {
                    variableName = "m" + referencedFile.GetInstanceName();
                }
                // We used to do this here, but we want to do it after all Objects have been destroyed
                // because we may need to make the file one way before the destruction of the objects.

                if (ati != null && ati.SupportsMakeOneWay)
                {
                    codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                    codeBlock.Line(string.Format("{0} = null;", variableName));
                    codeBlock = codeBlock.End().Else();
                    codeBlock.Line(string.Format("{0}.MakeOneWay();", variableName));
                    codeBlock = codeBlock.End();

                }
                else
                {
                    codeBlock.Line(string.Format("{0} = null;", variableName));

                }
            }

            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                codeBlock = codeBlock.End();
            }
            AddEndIfIfNecessary(codeBlock, referencedFile);
            return codeBlock;
        }
        public static ICodeBlock GetPostCustomActivityForReferencedFile(ReferencedFileSave referencedFile)
        {
            ICodeBlock codeBlock = new CodeDocument();

            if (!referencedFile.LoadedAtRuntime)
            {
                return codeBlock;
            }

            AssetTypeInfo ati = referencedFile.GetAssetTypeInfo();

            // January 8, 2012
            // This used to not
            // check LoadedOnlyWhenReferenced
            // but I think it should otherwise
            // this code will always load Scenes
            // that are LoadedOnlyWhenReferenced by
            // calling their ManageAll method - so I
            // added a check for LoadedOnlyWhenReferenced.
            if (ati != null && !referencedFile.IsSharedStatic && !referencedFile.LoadedOnlyWhenReferenced && ati.AfterCustomActivityMethod != null)
            {
                AddIfConditionalSymbolIfNecesssary(codeBlock, referencedFile);
                string variableName = referencedFile.GetInstanceName();

                codeBlock.Line(ati.AfterCustomActivityMethod.Replace("this", variableName) + ";");
                AddEndIfIfNecessary(codeBlock, referencedFile);
                return codeBlock;
            }
            else
            {
                return codeBlock;
            }
        }
        public static ICodeBlock GetPostInitializeForReferencedFile(ReferencedFileSave referencedFile)
        {
            AssetTypeInfo ati = referencedFile.GetAssetTypeInfo();
            ICodeBlock codeBlock = new CodeDocument();

            if (ati != null && !referencedFile.IsSharedStatic)
            {
                string variableName = referencedFile.GetInstanceName();
                if (!string.IsNullOrEmpty(ati.PostInitializeCode))
                {
                    codeBlock.Line(ati.PostInitializeCode.Replace("this", variableName) + ";");
                }
            }


            return codeBlock;
        }
        static ICodeBlock GetActivityForReferencedFile(ReferencedFileSave referencedFile, IElement element)
        {
            ICodeBlock codeBlock = new CodeDocument();
            /////////////////////EARLY OUT/////////////////////////
            if (!referencedFile.LoadedAtRuntime)
            {
                return codeBlock;
            }
            //////////////////END EARLY OUT//////////////////////

            AssetTypeInfo ati = referencedFile.GetAssetTypeInfo();


            // If it's an emitter, call TimedEmit:
            ParticleCodeGenerator.GenerateTimedEmit(codeBlock, referencedFile, element);

            if (ati != null && (!referencedFile.IsSharedStatic || element is ScreenSave )&& !referencedFile.LoadedOnlyWhenReferenced && ati.ActivityMethod != null)
            {
                AddIfConditionalSymbolIfNecesssary(codeBlock, referencedFile);
                string variableName = referencedFile.GetInstanceName();

                codeBlock.Line(ati.ActivityMethod.Replace("this", variableName) + ";");

                AddEndIfIfNecessary(codeBlock, referencedFile);
                return codeBlock;
            }
            else
            {
                return codeBlock;
            }
        }