示例#1
0
        /// <summary>
        /// Initialize the data needed for the GUI to run.
        /// </summary>
        /// <param name="argArray">The command line arguments.</param>
        public void Initialize(string[] argArray)
        {
            // Restore the component definitions. Must be done first so that components used
            // in designs can be linked to when loading the .intel file.
            try
            {
                AllComponents allComponents = new AllComponents();
            }
            catch
            {
                Report.FatalError("Could not restore component definition file.");
            }

            // Need to identify the RaceName so we can load the correct race's intel.
            // We also want to identify the ClientState data store, if any, so we
            // can load it and use any historical information in there.
            // Last resort we will ask the user what to open.

            // There are a number of starting scenarios:
            // 1. the Nova GUI was started directly (e.g. in the debugger).
            //    There will be zero options/arguments in the argArray.
            //    We will continue an existing game, if any.
            //     - get GameFolder from the config file
            //     - look for races and ask the user to pick one. If none need to
            //       ask the user to open a game (.intel), then treat as per option 2.
            //     - Build the stateFileName from the GameFolder and Race name and
            //       load it, if it exists. If not don't care.
            // 2. the Nova GUI was started from Nova Launcher's "open a game option".
            //    or by double clicking a race in the Nova Console
            //    There will be a .intel file listed in the argArray.
            //    Evenything we need should be found in there.
            // 3. the Nova GUI was started from the launcher to continue a game.
            //    There will be a StateFileName in the argArray.
            //    Directly load the state file. If it is missing - FatalError.
            //    (The race name and game folder will be loaded from the state file)
            StatePathName = null;
            string intelFileName = null;

            // process the arguments
            CommandArguments commandArguments = new CommandArguments(argArray);

            if (commandArguments.Contains(CommandArguments.Option.RaceName))
            {
                EmpireState.Race.Name = commandArguments[CommandArguments.Option.RaceName];
            }
            if (commandArguments.Contains(CommandArguments.Option.StateFileName))
            {
                StatePathName = commandArguments[CommandArguments.Option.StateFileName];
            }
            if (commandArguments.Contains(CommandArguments.Option.IntelFileName))
            {
                intelFileName = commandArguments[CommandArguments.Option.IntelFileName];
            }

            // Get the name of the folder where all the game files will be stored.
            // Normally this would be placed in the config file by the NewGame wizard.
            // We also cache a copy in the ClientState.Data.GameFolder
            GameFolder = FileSearcher.GetFolder(Global.ServerFolderKey, Global.ServerFolderName);

            if (GameFolder == null)
            {
                Report.FatalError("ClientState.cs Initialize() - An expected config file entry is missing\n" +
                                  "Have you ran the Race Designer and \n" +
                                  "Nova Console?");
            }

            // Sort out what we need to initialize the ClientState
            bool isLoaded = false;

            // 1. the Nova GUI was started directly (e.g. in the debugger).
            //    There will be zero options/arguments in the argArray.
            //    We will continue an existing game, if any.
            if (argArray.Length == 0)
            {
                // - get GameFolder from the conf file - already done.

                // - look for races and ask the user to pick one.
                EmpireState.Race.Name = SelectRace(GameFolder);
                if (!string.IsNullOrEmpty(EmpireState.Race.Name))
                {
                    isLoaded = true;
                }
                else
                {
                    // If none need to ask the user to open a game (.intel),
                    // then treat as per option 2.
                    try
                    {
                        OpenFileDialog fd = new OpenFileDialog();
                        fd.Title    = "Open Game";
                        fd.FileName = "*." + Global.IntelExtension;
                        DialogResult result = fd.ShowDialog();
                        if (result != DialogResult.OK)
                        {
                            Report.FatalError("ClientState.cs Initialize() - Open Game dialog canceled. Exiting. Try running the NovaLauncher.");
                        }
                        intelFileName = fd.FileName;
                    }
                    catch
                    {
                        Report.FatalError("ClientState.cs Initialize() - Unable to open a game. Try running the NovaLauncher.");
                    }
                }
            }

            // 2. the Nova GUI was started from the launcher open a game option.
            //    There will be a .intel file listed in the argArray.
            if (!isLoaded && intelFileName != null)
            {
                if (File.Exists(intelFileName))
                {
                    // Evenything we need should be found in there.
                    IntelReader intelReader = new IntelReader(this);
                    intelReader.ReadIntel(intelFileName);
                    isLoaded = true;
                }
                else
                {
                    Report.FatalError("ClientState.cs Initialize() - Could not locate .intel file \"" + intelFileName + "\".");
                }
            }

            // 3. the Nova GUI was started from the launcher to continue a game.
            //    There will be a StateFileName in the argArray.
            // NB: we already copied it to ClientState.Data.StateFileName, but other
            // code sets that too, so check the arguments to see if it was there.
            if (!isLoaded && commandArguments.Contains(CommandArguments.Option.StateFileName))
            {
                // The state file is not sufficient to load a turn. We need the .intel
                // for this race. What race? The state file can tell us.
                // (i.e. The race name and game folder will be loaded from the state file)
                // If it is missing - FatalError.
                if (File.Exists(StatePathName))
                {
                    Restore();
                    IntelReader intelReader = new IntelReader(this);
                    intelReader.ReadIntel(intelFileName);
                    isLoaded = true;
                }
                else
                {
                    Report.FatalError("ClientState.cs Initialize() - File not found. Could not continue game \"" + StatePathName + "\".");
                }
            }


            if (!isLoaded)
            {
                Report.FatalError("ClientState.cs initialize() - Failed to find any .intel when initializing turn");
            }

            FirstTurn = false;
        }