private static SaveFile DeserializeSaveFile(FileInfo file)
        {
            SaveFile saveFile = null;

            file.Refresh();

            if (!file.Exists)
            {
                CrashHandler.ShowException(new FileNotFoundException("The file " + file.Name + " was not found.", file.Name));

                return(saveFile);
            }

            //Open the save file.
            FileStream fs = new FileStream(file.FullName, FileMode.Open);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                saveFile = (SaveFile)formatter.Deserialize(fs);
            }
            catch (Exception e)
            {
                CrashHandler.ShowException("Failed to deserialize save file. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }

            return(saveFile);
        }
Exemple #2
0
        private void CreateSaveFile()
        {
            //Check if the file name is valid BEFORE trying to create a new save file:
            if (SaveFileController.IsValidFileName(saveFileNameInputField.Text))
            {
                SaveFile saveFile = new SaveFile(saveFileNameInputField.Text, playerNameInputField.Text);

                GameController.NewGame();
                SaveFileController.SaveGame(saveFile);
            }
            else
            {
                CrashHandler.ShowException("Save file name \"" + saveFileNameInputField.Text + "\" is not a valid name.");
            }
        }
        public static Texture2D GetSprite(string path)
        {
            Texture2D loaded = Missing_Sprite;

            try
            {
                loaded = MainGame.Singleton.Content.Load <Texture2D>(path);
            }
            catch (ContentLoadException e)
            {
                CrashHandler.ShowException(e);
            }

            return(loaded);
        }
        public static SoundEffect GetSoundEffect(string path)
        {
            SoundEffect loaded = null;

            try
            {
                loaded = MainGame.Singleton.Content.Load <SoundEffect>(path);
            }
            catch (ContentLoadException e)
            {
                CrashHandler.ShowException(e);
            }

            return(loaded);
        }
        public static SpriteFont GetFont(string path)
        {
            SpriteFont loaded = null;

            try
            {
                loaded = MainGame.Singleton.Content.Load <SpriteFont>(path);
            }
            catch (ContentLoadException e)
            {
                CrashHandler.ShowException(e);
            }

            return(loaded);
        }
        private static void SerializeSaveFile(SaveFile saveFile)
        {
            //Create the save file.
            FileStream fs = new FileStream(saveFile.SaveFilePath, FileMode.Create);

            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, saveFile);
            }
            catch (Exception e)
            {
                CrashHandler.ShowException("Failed to deserialize save file. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }
        }
        public static T[] LoadAllData <T>(DataFileType type, string nodeName) where T : Data
        {
            if (DataFiles.TryGetValue(type, out DataFile dataFile))
            {
                XmlNodeList nodeList = dataFile.GetNodes(nodeName);
                T[]         nodes    = new T[nodeList.Count];

                for (int i = 0; i < nodeList.Count; i++)
                {
                    nodes[i] = Deserialize <T>(nodeList[i]);
                }

                return(nodes);
            }
            else
            {
                CrashHandler.ShowException(string.Format("Data of type {0} in node {1} does not exist.", type, nodeName));

                return(null);
            }
        }
        /// <summary>
        /// Loads the specified data from specified file (if it exists).
        /// </summary>
        public static T LoadData <T>(string id, DataFileType type) where T : Data
        {
            if (DataFiles.TryGetValue(type, out DataFile dataFile))
            {
                XmlNode node = dataFile.GetNode(id);

                if (node != null)
                {
                    return(Deserialize <T>(node));
                }
                else
                {
                    CrashHandler.ShowException(string.Format("Data of type {0} with ID {1} does not exist.", id, type));

                    return(null);
                }
            }
            else
            {
                CrashHandler.ShowException(string.Format("Data of type {0} with ID {1} does not exist.", id, type));

                return(null);
            }
        }