示例#1
0
        /// <summary>
        ///		Called by the base engine class when its safe to begin deinitialization.
        /// </summary>
        protected override void Finish()
        {
            // Remember me!!! Remember me!!
            if (_rememberUser == true)
            {
                _engineConfigFile.SetSetting("account:username", _currentUsername);
                _engineConfigFile.SetSetting("account:password", _currentPassword);
            }
            else
            {
                _engineConfigFile.SetSetting("account:username", "");
                _engineConfigFile.SetSetting("account:password", "");
            }

            // Ignore everything else if we are not running a game.
            if (_gameName == "")
            {
                _explorerWindow.Dispose();
                _explorerWindow = null;
                GC.Collect();
                return;
            }

            // Close network.
            if (_serverWindow != null)
                NetworkManager.Finish();

            // Pump out a GameFinish event.
            EventManager.FireEvent(new Event("game_finish", this, null));

            // Allow the processes some time to be notified of closing events.
            EventManager.ProcessEvents();
            ProcessManager.RunProcesses(-1);

            // Close down the window.
            if (_window != null)
            {
                _window.Dispose();
                _window = null;
                GC.Collect();
            }

            // Close down the server window.
            if (_serverWindow != null)
            {
                _serverWindow.Dispose();
                _serverWindow = null;
                GC.Collect();
            }

            // Close the network connection.
            NetworkManager.Close();
        }
示例#2
0
        /// <summary>
        ///		Called by the base engine class when its safe to begin initialization.
        /// </summary>
        protected override bool Begin()
        {
            Runtime.Debug.DebugLogger.WriteLog("Entered begin function", LogAlertLevel.Warning);
            // Create the fusion fuction set.
            new FusionFunctionSet();
            new NetworkFunctionSet();

            // Bind all function sets to the global virtual machine.
            NativeFunctionSet.RegisterCommandSetsToVirtualMachine(VirtualMachine.GlobalInstance);

            // Grab some config out of the games config file.
            ASCIIEncoding encoding = new ASCIIEncoding();
            _currentPassword = _engineConfigFile["account:password", ""];
            _currentUsername = _engineConfigFile["account:username", ""];

            // Make sure we have a game we can run.
            if (_gameName != "")
            {
                // Create the loading window thread.
                _loading = true;
                new Thread(LoadingWindowThread).Start();

                NetworkManager.IsServer = _isServer;

                if (_isServer == false)
                {
                    // Setup graphics window.
                    SetupGameWindow();
                }
                else
                {
                    // Setup graphics window. We are only actually doing this so the graphics and audio drivers have something to bind to.
                    // Notice that we are hiding it as soon as its made.
                    SetupGameWindow();
                    _window.Hide();

                    // Setup server window.
                    _serverWindow = new ServerWindow();
                    _serverWindow.FormClosing += new FormClosingEventHandler(OnClosing);
                    _serverWindow.Show();

                    // Setup server.
                    if (NetworkManager.Start() == false)
                    {
                        throw new Exception("An error occured while attempting to setup network.");
                    }
                }

                // Create some hooks into network events.
                NetworkManager.ClientConnected += new ClientConnectedEventHandler(ClientConnected);
                NetworkManager.ClientDisconnected += new ClientDisconnectedEventHandler(ClientDisconnected);
                NetworkManager.Disconnected += new DisconnectedEventHandler(Disconnected);
                NetworkManager.PacketRecieved += new PacketRecievedEventHandler(PacketRecieved);

                // If pak files are being used then load all of them in and register
                // them with the resource manager
                if (_usePakFiles == true)
                {
                    ResourceManager.UsePakFiles = true;
                    DebugLogger.WriteLog("Looking for resources in pak files...");
                    foreach (string file in Directory.GetFiles(Environment.CurrentDirectory))
                        if (file.ToLower().EndsWith(".pk") == true)
                        {
                            DebugLogger.WriteLog("Looking for resources in \"" + Path.GetFileName(file) + "\"...");
                            PakFile pakFile = new PakFile(file);
                            ResourceManager.RegisterPakFile(pakFile);
                        }
                }

                // Load in the default tileset if it exists and add it to the tileset list.
                if (ResourceManager.ResourceExists(_tilesetPath + "\\default.xml") == true)
                {
                    DebugLogger.WriteLog("Found default tileset, loading...");
                    Tileset.AddToTilesetPool(new Tileset(_tilesetPath + "\\default.xml"));
                }

                // Load in the default font if it exists.
                if (ResourceManager.ResourceExists(_fontPath + "\\default.xml") == true)
                {
                    DebugLogger.WriteLog("Found default bitmap font, loading...");
                    GraphicsManager.DefaultBitmapFont = new BitmapFont(_fontPath + "\\default.xml");
                }

                // Load in the required language pack.
                string languageFile = _languagePath + "\\" + _language + ".xml";
                if (ResourceManager.ResourceExists(languageFile) == true)
                {
                    DebugLogger.WriteLog("Loading language pack for language " + _language + ".");
                    LanguageManager.LoadLanguagePack(_languagePath + "\\" + _language + ".xml", _language);
                }
                else
                   DebugLogger.WriteLog("Unable to find language pack for language " + _language + ".", LogAlertLevel.Error);

                // Register the console commands, incase we turn the console on later.
                new StatisticalConsoleCommands();
                new FusionConsoleCommands();
                new MapConsoleCommands();
                new SystemConsoleCommands();

                // Register all the command sets with the console.
                ConsoleCommandSet.RegisterCommandSets();

                // Shall we setup the graphical console?
                if (_showConsole == true && _isServer == false)
                {
                    // Enable the graphical console.
                    GraphicalConsole.Enabled = true;
                }

                // Check if the start script exists.
                if (ResourceManager.ResourceExists(_startScript) == true)
                {
                    // It does, w00t, lets create an execution process for it then.
                    _gameScriptProcess = new ScriptExecutionProcess(_startScript);
                    if (_gameScriptProcess.Process != null)
                    {
                        _gameScriptProcess.Priority = 1000000; // Always runs first, otherwise all hell breaks loose with the OnCreate events.
                        _gameScriptProcess.IsPersistent = true;
                        _gameScriptProcess.Process.OnStateChange += this.OnStateChange;
                        ProcessManager.AttachProcess(_gameScriptProcess);

                        OnStateChange(_gameScriptProcess.Process, _gameScriptProcess.Process.State);
                    }
                }

                // Pump out a GameBegin event.
                EventManager.FireEvent(new Event("game_begin", this, null));

                // Close the loading window.
                _loading = false;

                // Show the game window.
                if (_isServer == false)
                {
                    _window.Show();
                    _window.BringToFront();
                    _window.Activate();
                }
                else
                {
                    _serverWindow.Show();
                    _serverWindow.BringToFront();
                    _serverWindow.Activate();
                }
            }

            // Nope? Ok show the games explorer.
            else
            {
                _explorerWindow = new GamesExplorerWindow();
                _explorerWindow.FormClosing += new FormClosingEventHandler(OnClosing);
                _explorerWindow.Show();
            }

            return false;
        }