Example #1
0
        public void Load(string path, SerializationFormat expectedFormat, bool createBackup)
        {
            FileManager.TryRegisterExternalFile(path);
            var file        = new AmfFile(path);
            var dataVersion = file.GetString("version");

            if (file.Error != null)
            {
                var box = new ExceptionBox();
                box.Title            = "Could not read file.";
                box.Message          = "TiTsEd could not read this file correctly. Maybe it was corrupted or generated by an old version of Flash. Continuing may make TiTsEd unstable or cause it to corrupt the file. It is strongly advised that you cancel this operation.";
                box.Path             = file.FilePath;
                box.ExceptionMessage = file.Error.Mesg;
                box.IsWarning        = true;
                var result = box.ShowDialog(ExceptionBoxButtons.Continue, ExceptionBoxButtons.Cancel);

                Logger.Error(file.Error.Mesg);
                if (result != ExceptionBoxResult.Continue)
                {
                    return;
                }
            }
            else if (String.IsNullOrEmpty(dataVersion))
            {
                var box = new ExceptionBox();
                box.Title     = "File version too old.";
                box.Message   = "TiTsEd may not be able to read this file correctly as it was generated by an older version of T**s. Continuing may make TiTsEd unstable or cause it to corrupt the file. It is strongly advised that you cancel this operation.";
                box.IsWarning = true;
                var result = box.ShowDialog(ExceptionBoxButtons.Continue, ExceptionBoxButtons.Cancel);

                Logger.Error(String.Format("{0} T**s data version: {1}.", box.Title, dataVersion));
                if (result != ExceptionBoxResult.Continue)
                {
                    return;
                }
            }

            // I would like to test dataVersion here some day to ensure that it's not too old of a version,
            // however, as long as Fen keeps occasionally pushing crappy version strings (e.g. 0.8.4.8d),
            // that can't happen.  Ideally, I'd like to see them switch to a segmented version system.

            // Sanity checks: see if the save can be re-saved as-is OR if any of the top-level property names are invalid as identifiers.
            if (!file.CanBeSaved(SerializationFormat.Slot) || HasBadPropertyNames(file))
            {
                var box = new ExceptionBox();
                box.Title     = "File could not be read correctly.";
                box.Message   = "TiTsEd could not read this file correctly, it is likely corrupted. Cancelling this operation.";
                box.IsWarning = true;
                box.ShowDialog(ExceptionBoxButtons.OK);

                Logger.Error(String.Format("{0} T**s data version: {1}.", box.Title, dataVersion));
                return;
            }

            // Sanity check: ensure the actual format matches the expected format (just a warning to the user about mixing up the formats).
            if (file.Format != expectedFormat)
            {
                var box = new ExceptionBox();
                box.Title     = "File format different from expected format.";
                box.Message   = "The file is actually a " + (file.Format == SerializationFormat.Slot ? "\"Slot\"" : "\"Save to File\"") + " format save, but it was loaded as though it were a " + (expectedFormat == SerializationFormat.Slot ? "\"Slot\"" : "\"Save to File\"") + " format save.\n\nThe two formats are not compatible, so care should be taken to ensure you do not confuse the two, since T**s can only load each format in a specific way.  Attempting to load a \"Slot\" save as though it were a \"Save to File\" save, or vice versa, will cause T**s to think the save is corrupt, at best, or see the save deleted, at worse. Do not mix them up.";
                box.IsWarning = true;
                box.ShowDialog(ExceptionBoxButtons.OK);
            }

            if (createBackup)
            {
                FileManager.CreateBackup(path);
            }
            _currentFile = file;

            XmlData.Select(XmlData.Files.T**s);
            Data = XmlData.Current;
            Game = new GameVM(_currentFile, Game);

            OnPropertyChanged("Data");
            OnPropertyChanged("Game");
            OnPropertyChanged("HasData");
            UpdateAppTitle();
            VM.Instance.NotifySaveRequiredChanged(false);
            if (FileOpened != null)
            {
                FileOpened(null, null);
            }
        }
Example #2
0
File: VM.cs Project: klorpa/CoCEd
        public void Load(string path, SerializationFormat expectedFormat, bool createBackup)
        {
            FileManager.TryRegisterExternalFile(path);
            var file        = new AmfFile(path);
            var dataVersion = file.GetString("version");

            if (file.Error != null)
            {
                var box = new ExceptionBox()
                {
                    Title            = "Could not read file.",
                    Message          = "CoCEd could not read this file correctly. Maybe it was corrupted or generated by an old version of Flash. Continuing may make CoCEd unstable or cause it to corrupt the file. It is strongly advised that you cancel this operation.",
                    Path             = file.FilePath,
                    ExceptionMessage = file.Error.Mesg,
                    IsWarning        = true,
                };
                var result = box.ShowDialog(ExceptionBoxButtons.Continue, ExceptionBoxButtons.Cancel);

                Logger.Error(file.Error.Mesg);
                if (result != ExceptionBoxResult.Continue)
                {
                    return;
                }
            }
            else if (String.IsNullOrEmpty(dataVersion))
            {
                var box = new ExceptionBox()
                {
                    Title     = "File version too old.",
                    Message   = "CoCEd may not be able to read this file correctly as it was generated by an older version of CoC. Continuing may make CoCEd unstable or cause it to corrupt the file. It is strongly advised that you cancel this operation.",
                    IsWarning = true,
                };
                var result = box.ShowDialog(ExceptionBoxButtons.Continue, ExceptionBoxButtons.Cancel);

                Logger.Error(String.Format("{0} CoC data version: {1}.", box.Title, dataVersion));
                if (result != ExceptionBoxResult.Continue)
                {
                    return;
                }
            }

            // Some day I would like to be able to test `dataVersion` to ensure that it's not too old
            // of a version, however, as long as CoC developers keep pushing crappy version strings
            // (e.g. 0.8.4.8d), that can't happen.  Ideally, I'd like to see them switch to SemVer.
            //
            // This probably doesn't really apply to Vanilla anymore, since its finished and unlikely
            // to even receive patches at this point.

            // Sanity checks: see if the save can be re-saved as-is OR if any of the top-level property names are invalid as identifiers.
            if (!file.CanBeSaved(SerializationFormat.Slot) || HasBadPropertyNames(file))
            {
                var box = new ExceptionBox()
                {
                    Title     = "File could not be read correctly.",
                    Message   = "CoCEd could not read this file correctly, it is likely corrupted. Cancelling this operation.",
                    IsWarning = true,
                };
                box.ShowDialog(ExceptionBoxButtons.OK);

                Logger.Error(String.Format("{0} CoC data version: {1}.", box.Title, dataVersion));
                return;
            }

            // Sanity check: ensure the actual format matches the expected format (just a warning to the user about mixing up the formats).
            if (file.Format != expectedFormat)
            {
                var box = new ExceptionBox()
                {
                    Title     = "File format different from expected format.",
                    Message   = "The file is actually a " + (file.Format == SerializationFormat.Slot ? "\"Slot\"" : "\"Save to File\"") + " format save, but it was loaded as though it were a " + (expectedFormat == SerializationFormat.Slot ? "\"Slot\"" : "\"Save to File\"") + " format save.\n\nThe two formats are not compatible, so care should be taken to ensure you do not confuse the two, since CoC can only load each format in a specific way.  Attempting to load a \"Slot\" save as though it were a \"Save to File\" save, or vice versa, will cause CoC to think the save is corrupt, at best, or see the save deleted, at worse. Do not mix them up.",
                    IsWarning = true,
                };
                box.ShowDialog(ExceptionBoxButtons.OK);
            }

            if (createBackup)
            {
                FileManager.CreateBackup(path);
            }
            _currentFile = file;

            // Is this save from Vanilla, Revamp, or Xianxia?
            ModType modType;

            //if (_currentFile.Contains("soulforce"))
            //{
            //    modType = ModType.Xianxia;
            //    XmlData.Select(XmlData.Files.Xianxia);
            //}
            //else
            if (_currentFile.Contains("hunger"))
            {
                modType = ModType.Revamp;
                XmlData.Select(XmlData.Files.Revamp);
            }
            else
            {
                modType = ModType.Vanilla;
                XmlData.Select(XmlData.Files.Vanilla);
            }

            Data = XmlData.Current;
            Game = new GameVM(_currentFile, Game, modType);

            OnPropertyChanged("Data");
            OnPropertyChanged("Game");
            OnPropertyChanged("HasData");
            UpdateAppTitle();
            VM.Instance.NotifySaveRequiredChanged(false);
            if (FileOpened != null)
            {
                FileOpened(null, null);
            }
        }