Exemplo n.º 1
0
        public static void GenerateCallInGame1(string gameFileName, bool whetherToCall)
        {
            string contents = null;

            if (!string.IsNullOrEmpty(gameFileName))
            {
                GlueCommands.Self.TryMultipleTimes(() =>
                                                   contents = FileManager.FromFileText(GlueState.Self.CurrentGlueProjectDirectory + gameFileName));
            }

            if (!string.IsNullOrEmpty(contents))
            {
                string whatToLookFor = "CameraSetup.SetupCamera(SpriteManager.Camera, graphics";

                string lineToReplaceWith = "CameraSetup.SetupCamera(SpriteManager.Camera, graphics);";

                if (whetherToCall)
                {
                    lineToReplaceWith = "\t\t\t" + lineToReplaceWith;
                }
                else
                {
                    lineToReplaceWith = "\t\t\t//" + lineToReplaceWith;
                }

                if (contents.Contains(whatToLookFor))
                {
                    // Only replace this if it's commented out:
                    int startOfLine;
                    int endOfLine;
                    StringFunctions.GetStartAndEndOfLineContaining(contents, "CameraSetup.SetupCamera", out startOfLine, out endOfLine);

                    string line = contents.Substring(startOfLine, endOfLine - startOfLine);

                    bool shouldReplace = line.Trim().StartsWith("//");
                    if (shouldReplace)
                    {
                        StringFunctions.ReplaceLine(ref contents, "CameraSetup.SetupCamera", lineToReplaceWith);
                    }
                }
                else
                {
                    // We gotta find where to put the start call.  This should be after
                    // FlatRedBallServices.InitializeFlatRedBall

                    int index = CodeParser.GetIndexAfterBaseInitialize(contents);

                    if (index == -1)
                    {
                        GlueCommands.Self.PrintError("Could not find code in Game1.cs to add camera setup");
                    }
                    else
                    {
                        contents = contents.Insert(index, lineToReplaceWith + Environment.NewLine);
                    }
                }

                // load to see if it's changed, and only change it if so:
                var absoluteFile = new FilePath(FileManager.RelativeDirectory + gameFileName);

                var shouldSave = absoluteFile.Exists() == false;

                if (!shouldSave)
                {
                    var existingText = FileManager.FromFileText(absoluteFile.FullPath);
                    shouldSave = existingText != contents;
                }

                if (shouldSave)
                {
                    GlueCommands.Self.TryMultipleTimes(() =>
                    {
                        FileManager.SaveText(contents, absoluteFile.FullPath);
                    }, 5);
                }
            }
        }