예제 #1
0
        /// <summary>
        /// Generates and saves the code for the passed Gum ElementSave (both generated and custom code template),
        /// but does not add the resulting .cs files to the VisualStudio project.
        /// </summary>
        /// <param name="element">The element to generate.</param>
        /// <returns>Information about what was generated and saved.</returns>
        public GenerationResult GenerateCodeFor(Gum.DataTypes.ElementSave element,
                                                CodeGenerationSavingBehavior savingBehavior = CodeGenerationSavingBehavior.AlwaysSave)
        {
            GenerationResult resultToReturn = new GenerationResult();

            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            string directoryToSave = GumRuntimesFolder;

            string generatedCode = mGueDerivingClassCodeGenerator.GenerateCodeFor(element);

            FilePath generatedSaveLocation = directoryToSave + element.Name + "Runtime.Generated.cs";

            if (savingBehavior == CodeGenerationSavingBehavior.AlwaysSave)
            {
                resultToReturn.DidSaveGenerated = true;
            }
            else // if(savingBehavior == CodeGenerationSavingBehavior.SaveIfGeneratedDiffers)
            {
                // We only want to save this file if what we've just generated is different than what is already on disk:
                if (!generatedSaveLocation.Exists())
                {
                    resultToReturn.DidSaveGenerated = true;
                }
                else
                {
                    var existingText = File.ReadAllText(generatedSaveLocation.FullPath);

                    resultToReturn.DidSaveGenerated = existingText != generatedCode;
                }
            }

            string customCodeSaveLocation = directoryToSave + element.Name + "Runtime.cs";

            // If it doesn't exist, overwrite it. If it does exist, don't overwrite it - we might lose
            // custom code.
            if (!System.IO.File.Exists(customCodeSaveLocation) &&
                // Standard elements don't have CustomInit
                (element is StandardElementSave) == false)
            {
                resultToReturn.DidSaveCustom = true;
            }

            if (string.IsNullOrEmpty(generatedCode))
            {
                resultToReturn.DidSaveCustom    = false;
                resultToReturn.DidSaveGenerated = false;
            }

            if (resultToReturn.DidSaveGenerated)
            {
                // in case directory doesn't exist
                var directory = generatedSaveLocation.GetDirectoryContainingThis();

                System.IO.Directory.CreateDirectory(directory.FullPath);

                GlueCommands.Self.TryMultipleTimes(() =>
                                                   System.IO.File.WriteAllText(generatedSaveLocation.FullPath, generatedCode));
            }

            if (resultToReturn.DidSaveCustom)
            {
                var customCode = CustomCodeGenerator.Self.GetCustomCodeTemplateCode(element);

                GlueCommands.Self.TryMultipleTimes(() =>
                                                   System.IO.File.WriteAllText(customCodeSaveLocation, customCode));
            }

            return(resultToReturn);
        }
예제 #2
0
        public void GenerateDerivedGueRuntimes()
        {
            if (AppState.Self.GumProjectSave == null &&
                FlatRedBall.Glue.Elements.ObjectFinder.Self.GlueProject != null)
            {
                var rfs = FlatRedBall.Glue.Elements.ObjectFinder.Self.GlueProject.GetAllReferencedFiles()
                          .FirstOrDefault(item => FlatRedBall.IO.FileManager.GetExtension(item.Name) == "gumx");

                if (rfs != null)
                {
                    string fullFileName = FlatRedBall.Glue.ProjectManager.ContentDirectory + rfs.Name;

                    string gumXDirectory = FlatRedBall.IO.FileManager.GetDirectory(fullFileName);

                    FileReferenceTracker.Self.LoadGumxIfNecessaryFromDirectory(gumXDirectory);
                }
            }

            if (Gum.Managers.ObjectFinder.Self.GumProjectSave != null)
            {
                string directoryToSave = GumRuntimesFolder;

                System.IO.Directory.CreateDirectory(directoryToSave);

                bool wasAnythingAdded = false;

                wasAnythingAdded |= GenerateAndSaveRuntimeAssociations();

                var elements = AppState.Self.AllLoadedElements.ToList();

                // This can greatly improve speed, just don't put async calls in here or it won't block
                Parallel.ForEach(elements, (element) =>
                {
                    GenerationResult generationResult = new GenerationResult();
                    try
                    {
                        generationResult = GenerateCodeFor(element);
                    }
                    catch (Exception e)
                    {
                        GlueCommands.Self.PrintError(e.ToString());
                    }

                    if (generationResult.DidSaveGenerated)
                    {
                        string location   = directoryToSave + element.Name + "Runtime.Generated.cs";
                        wasAnythingAdded |=
                            FlatRedBall.Glue.ProjectManager.CodeProjectHelper.AddFileToCodeProjectIfNotAlreadyAdded(
                                FlatRedBall.Glue.ProjectManager.ProjectBase, location);
                    }
                    if (generationResult.DidSaveCustom)
                    {
                        string location   = directoryToSave + element.Name + "Runtime.cs";
                        wasAnythingAdded |=
                            FlatRedBall.Glue.ProjectManager.CodeProjectHelper.AddFileToCodeProjectIfNotAlreadyAdded(
                                FlatRedBall.Glue.ProjectManager.ProjectBase, location);
                    }
                });

                if (wasAnythingAdded)
                {
                    FlatRedBall.Glue.ProjectManager.SaveProjects();
                }
            }
        }