Пример #1
0
        private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            //Save the engine settings. This code is not pretty.... - JS

            //IGame
            //Check if we have changed the IGame script. If so, copy the current IGame property values
            //over to the new IGame Type.
            if (defaultGameType.SelectedIndex >= 0)
            {
                //Gets the Value from the collection based on the Key the combo box has selected.
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)defaultGameType.SelectedItem;

                //Only do this if the setting was changed
                if (EngineSettings.Default.GameScript != selectedValue.Value)
                {
                    //Save a reference to our current World data.
                    IGameObject currentGame = (IGameObject)Editor.Game;

                    //Create a instance of the new scripted Type
                    IGameObject newGame = (IGameObject)ScriptFactory.GetScript(selectedValue.Value);

                    //If we found the script, lets copy the properties of the current IGame
                    //object over to the new one. If we didn't copy the properites, all of the games
                    //items, rooms, players, characters would be lost.
                    if (newGame != null)
                    {
                        //Copy the properties from currentGame over to newGame
                        newGame.CopyState(ref currentGame);

                        Editor.Game = (IGame)newGame;
                    }

                    //Save the new IGame game object to the engine settings.
                    EngineSettings.Default.GameScript = selectedValue.Value;
                }
            }

            //IPlayer
            //Check if we have changed the IPlayer script. If so, copy the current IPlayerproperty values
            //over to the new IPlayer Type.
            if (defaultPlayerType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)defaultPlayerType.SelectedItem;

                //Only do this if the setting was changed
                if (EngineSettings.Default.PlayerScript != selectedValue.Value)
                {
                    EngineSettings.Default.PlayerScript = selectedValue.Value;
                }
            }

            //Login State
            //Check if we have changed the login script.
            //This is the script that gets executed immediately once the player
            //has established a network connection with the server.
            if (loginState.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)loginState.SelectedItem;

                //Only do this if the setting was changed
                if (EngineSettings.Default.LoginState != selectedValue.Value)
                {
                    EngineSettings.Default.LoginState = selectedValue.Value;
                }
            }

            //Login Completed State
            //Check if we have changed the initial state used when a player completes the login process.
            if (LoginCompleteState.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)LoginCompleteState.SelectedItem;

                //Only do this if the setting was changed
                if (EngineSettings.Default.LoginCompletedState != selectedValue.Value)
                {
                    EngineSettings.Default.LoginCompletedState = selectedValue.Value;
                }
            }

            //IWorld
            //Check if we have changed the IWorld script. If so, copy the current IWorld property values
            //over to the new IWorld Type.
            if (defaultWorldType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)defaultWorldType.SelectedItem;

                //Only do this if the setting was changed
                if (EngineSettings.Default.WorldScript != selectedValue.Value)
                {
                    //Save a reference to our current World data.
                    IGameObject currentWorld = (IGameObject)Editor.Game.World;

                    IGameObject newWorld = (IGameObject)ScriptFactory.GetScript(selectedValue.Value);

                    if (newWorld != null)
                    {
                        newWorld.CopyState(ref currentWorld);

                        Editor.Game.World = (IWorld)newWorld;
                    }

                    EngineSettings.Default.WorldScript = selectedValue.Value;
                }
            }

            //IRealm
            //Check if we have changed the IRealm script. If so, copy the current IRealm property values
            //over to the new IRealm Type.
            if (realmType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)realmType.SelectedItem;

                if (EngineSettings.Default.RealmScript != selectedValue.Value)
                {
                    List <IRealm> newRealmCollection = new List <IRealm>();

                    foreach (IRealm realm in Editor.Game.World.Realms)
                    {
                        IGameObject newRealm = (IGameObject)ScriptFactory.GetScript(selectedValue.Value);

                        if (newRealm != null)
                        {
                            IGameObject tmp = (IGameObject)realm;
                            newRealm.CopyState(ref tmp);
                            newRealmCollection.Add((IRealm)newRealm);
                        }
                    }

                    Editor.Game.World.Realms = newRealmCollection;
                    requiresReset            = true;
                }
                EngineSettings.Default.RealmScript = selectedValue.Value;
            }

            //IZone
            //Check if we have changed the IZone script. If so, copy the current IZone property values
            //over to the new IZone Type.
            if (zoneType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)zoneType.SelectedItem;

                if (EngineSettings.Default.ZoneScript != selectedValue.Value)
                {
                    List <IZone> newZoneCollection = new List <IZone>();

                    foreach (IRealm realm in Editor.Game.World.Realms)
                    {
                        foreach (IZone zone in realm.Zones)
                        {
                            IGameObject newZone = (IGameObject)ScriptFactory.GetScript(selectedValue.Value);

                            if (newZone != null)
                            {
                                IGameObject tmp = (IGameObject)zone;
                                newZone.CopyState(ref tmp);
                                newZoneCollection.Add((IZone)newZone);
                            }

                            realm.Zones       = newZoneCollection;
                            newZoneCollection = new List <IZone>();
                            requiresReset     = true;
                        }
                    }
                    EngineSettings.Default.ZoneScript = selectedValue.Value;
                }
            }

            //IRoom
            //Check if we have changed the IRoom script. If so, copy the current IRoom property values
            //over to the new IRoom Type.
            if (roomType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)roomType.SelectedItem;

                if (EngineSettings.Default.RoomScript != selectedValue.Value)
                {
                    List <IRoom> newRoomCollection = new List <IRoom>();

                    foreach (IRealm realm in Editor.Game.World.Realms)
                    {
                        foreach (IZone zone in realm.Zones)
                        {
                            foreach (IRoom room in zone.Rooms)
                            {
                                IGameObject newRoom = (IGameObject)ScriptFactory.GetScript(selectedValue.Value);

                                if (newRoom != null)
                                {
                                    IGameObject tmp = (IGameObject)room;
                                    newRoom.CopyState(ref tmp);
                                    newRoomCollection.Add((IRoom)newRoom);
                                }
                            }
                            zone.Rooms        = newRoomCollection;
                            newRoomCollection = new List <IRoom>();
                            requiresReset     = true;
                        }
                    }
                    EngineSettings.Default.RoomScript = selectedValue.Value;
                }
            }

            //IDoor
            //Check if we have changed the IDoor script. If so, copy the current IDoor property values
            //over to the new IDoor Type.
            if (doorType.SelectedIndex >= 0)
            {
                KeyValuePair <string, string> selectedValue = (KeyValuePair <string, string>)doorType.SelectedItem;

                if (EngineSettings.Default.DoorScript != selectedValue.Value)
                {
                    Dictionary <AvailableTravelDirections, IDoor> newDoorCollection = new Dictionary <AvailableTravelDirections, IDoor>();

                    foreach (IRealm realm in Editor.Game.World.Realms)
                    {
                        foreach (IZone zone in realm.Zones)
                        {
                            foreach (IRoom room in zone.Rooms)
                            {
                                foreach (IDoor door in room.Doorways.Values)
                                {
                                    IDoor newDoor = (IDoor)ScriptFactory.GetScript(selectedValue.Value);

                                    if (newDoor != null)
                                    {
                                        IGameObject tmp = (IGameObject)door;
                                        newDoor.CopyState(ref tmp);
                                        newDoorCollection.Add(newDoor.FacingDirection, newDoor);
                                    }
                                }
                                room.Doorways     = newDoorCollection;
                                newDoorCollection = new Dictionary <AvailableTravelDirections, IDoor>();
                            }
                        }
                    }
                    EngineSettings.Default.DoorScript = selectedValue.Value;
                }
            }

            //Script Folder Name
            //Check if we have changed the scripts path.
            if (!string.IsNullOrEmpty(scriptsPath.Text))
            {
                //Only do this if the setting was changed
                if (EngineSettings.Default.ScriptsPath != scriptsPath.Text)
                {
                    string oldPath = EngineSettings.Default.ScriptsPath;
                    string newPath = Path.Combine(Application.StartupPath, scriptsPath.Text);

                    //Get all of the plug-in script libraries that exists in the old path
                    string[] files = Directory.GetFiles(oldPath, "*.dll");

                    //Copy each file over to the new path.
                    //We only move the libraries, because the library collection will expect
                    //them to exist.
                    foreach (string file in files)
                    {
                        string filename = Path.GetFileName(file);
                        File.Copy(file, Path.Combine(newPath, filename), true);
                    }
                    EngineSettings.Default.ScriptsPath = scriptsPath.Text;
                }
            }

            //Script Libraries
            //Re-creates the list of script assembly libraries that the game is using
            if (scriptLibrary.Items.Count >= 0)
            {
                EngineSettings.Default.ScriptLibrary.Clear();

                foreach (string library in scriptLibrary.Items)
                {
                    EngineSettings.Default.ScriptLibrary.Add(library);
                }
            }

            //World Save Folder
            //Checks if we have changed the save file location for the World
            if (!string.IsNullOrEmpty(worldFile.Text))
            {
                //Only do this if the setting was changed
                if (EngineSettings.Default.WorldSaveFile != worldFile.Text)
                {
                    try
                    {
                        if (!Directory.Exists(Path.Combine(Environment.CurrentDirectory, worldFile.Text)))
                        {
                            Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, worldFile.Text));
                        }

                        //Save the World with the new location
                        FileIO file = new FileIO();
                        file.Save(Editor.Game.World, Path.Combine(Environment.CurrentDirectory, worldFile.Text));
                        //Delete the old file
                        File.Delete(Path.Combine(Environment.CurrentDirectory, EngineSettings.Default.WorldSaveFile));

                        //Save the Engine Setting.
                        EngineSettings.Default.WorldSaveFile = worldFile.Text;
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Failed to change the settings for the World file.");
                    }
                }
            }

            //Player Save Folder
            //Checks if we have changed the save file location for players.
            if (!string.IsNullOrEmpty(playerSavePath.Text))
            {
                //Only do this if the setting was changed
                if (EngineSettings.Default.PlayerSavePath != playerSavePath.Text)
                {
                    if (!Directory.Exists(Path.Combine(Environment.CurrentDirectory, playerSavePath.Text)))
                    {
                        Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, playerSavePath.Text));
                    }
                    foreach (string file in Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, EngineSettings.Default.PlayerSavePath), "*.char"))
                    {
                        File.Copy(file, Path.Combine(Environment.CurrentDirectory, playerSavePath.Text, Path.GetFileName(file)));
                        File.Delete(file);
                    }
                    EngineSettings.Default.PlayerSavePath = playerSavePath.Text;
                }
            }

            //Save the engine settings
            EngineSettings.Default.Save();

            //Save the game
            Editor.Game.SaveWorld();

            //Check if we require a reset. If so, clear out Game Objects that might have been edited.
            if (requiresReset)
            {
                Editor.CurrentRealm = null;
                Editor.CurrentZone  = null;
                Editor.CurrentRoom  = null;

                MessageBox.Show("Due to changing scripts that belong to Environments, currently loaded Environments have been unloaded.\n\nYou will need to re-select the environments you want to work with.", this.Text);
            }

            //Done.
            this.Close();
        }