diff --git a/App/SidePanelActionsHandler.cs b/App/SidePanelActionsHandler.cs index 3ed3850..4e300ea 100644 --- a/App/SidePanelActionsHandler.cs +++ b/App/SidePanelActionsHandler.cs @@ -15,6 +15,7 @@ using App.Properties; using CollectionManager.DataTypes; using CollectionManager.Enums; +using CollectionManager.Exceptions; using CollectionManager.Modules.CollectionsManager; using CollectionManager.Modules.FileIO; using CollectionManagerExtensionsDll.DataTypes; @@ -457,23 +458,27 @@ private void FormOnClosing(object sender, EventArgs eventArgs) _downloadManagerForm.Close(); } } + private void LoadCollectionFile(object sender, object data = null) - { - string fileLocation = data?.ToString() ?? _userDialogs.SelectFile("", "Collection database (*.db/*.osdb)|*.db;*.osdb", - "collection.db"); - if (string.IsNullOrEmpty(fileLocation)) return; - var loadedCollections = _osuFileIo.CollectionLoader.LoadCollection(fileLocation); - _collectionEditor.EditCollection(CollectionEditArgs.AddCollections(loadedCollections)); - } + => LoadCollection(data?.ToString() ?? _userDialogs.SelectFile("", "Collection database (*.db/*.osdb)|*.db;*.osdb", "collection.db")); private void LoadDefaultCollection(object sender, object data = null) + => LoadCollection(Path.Combine(Initalizer.OsuDirectory, "collection.db")); + + private void LoadCollection(string fileLocation) { - var fileLocation = Path.Combine(Initalizer.OsuDirectory, "collection.db"); - if (File.Exists(fileLocation)) + if (string.IsNullOrEmpty(fileLocation) || !File.Exists(fileLocation)) + return; + + try { var loadedCollections = _osuFileIo.CollectionLoader.LoadCollection(fileLocation); _collectionEditor.EditCollection(CollectionEditArgs.AddCollections(loadedCollections)); } + catch (CorruptedFileException ex) + { + _userDialogs.OkMessageBox(ex.Message, "Error", MessageBoxType.Error); + } } private async void SaveDefaultCollection(object sender, object data = null) diff --git a/CollectionManagerDll/Exceptions/CorruptedFileException.cs b/CollectionManagerDll/Exceptions/CorruptedFileException.cs new file mode 100644 index 0000000..cbe56c0 --- /dev/null +++ b/CollectionManagerDll/Exceptions/CorruptedFileException.cs @@ -0,0 +1,12 @@ +using System; + +namespace CollectionManager.Exceptions +{ + public class CorruptedFileException : Exception + { + public CorruptedFileException(string message) : base(message) + { + + } + } +} \ No newline at end of file diff --git a/CollectionManagerDll/Modules/FileIO/FileCollections/OsdbCollectionHandler.cs b/CollectionManagerDll/Modules/FileIO/FileCollections/OsdbCollectionHandler.cs index 779433c..cdbfec8 100644 --- a/CollectionManagerDll/Modules/FileIO/FileCollections/OsdbCollectionHandler.cs +++ b/CollectionManagerDll/Modules/FileIO/FileCollections/OsdbCollectionHandler.cs @@ -5,6 +5,7 @@ using System.Text; using CollectionManager.DataTypes; using CollectionManager.Enums; +using CollectionManager.Exceptions; using CollectionManager.Interfaces; using CollectionManager.Modules.FileIO.OsuDb; using SharpCompress.Archives; @@ -32,7 +33,9 @@ public class OsdbCollectionHandler {"o!dm5", 5}, {"o!dm6", 6}, {"o!dm7", 7}, - {"o!dm7min", 1007} + {"o!dm8", 8}, + {"o!dm7min", 1007}, + {"o!dm8min", 1008}, }; public OsdbCollectionHandler(ILogger logger) @@ -42,24 +45,14 @@ public OsdbCollectionHandler(ILogger logger) public string CurrentVersion(bool minimalWrite = false) { - return "o!dm7" + (minimalWrite ? "min" : ""); + return "o!dm8" + (minimalWrite ? "min" : ""); } + private bool IsFullCollection(string versionString) + => !isMinimalCollection(versionString); private bool isMinimalCollection(string versionString) - { - return versionString.EndsWith("min"); - } - - protected virtual void Error(string message) - { - //Helpers.Error(message); - } - - protected virtual void Info(string message) - { - //Helpers.Info(message); - } - + => versionString.EndsWith("min"); + public void WriteOsdb(Collections collections, string fullFileDir, string editor, bool minimalWrite = false) { using (var fileStream = new FileStream(fullFileDir, FileMode.Create, FileAccess.ReadWrite)) @@ -149,11 +142,8 @@ private void WriteOsdb(Collections collections, BinaryWriter _binWriter, string _binWriter.Write(beatmap.Md5); _binWriter.Write(((BeatmapExtension)beatmap).UserComment); - if (!minimalWrite) - { - _binWriter.Write((byte)beatmap.PlayMode); - _binWriter.Write(beatmap.StarsNomod); - } + _binWriter.Write((byte)beatmap.PlayMode); + _binWriter.Write(beatmap.StarsNomod); } _binWriter.Write(beatmapWithHashOnly.Count); @@ -179,7 +169,6 @@ public IEnumerable ReadOsdb(string fullFileDir, MapCacher mapCacher) _binReader = new BinaryReader(_memStream); } - _binReader.BaseStream.Seek(0, SeekOrigin.Begin); var versionString = _binReader.ReadString(); //check header @@ -190,7 +179,7 @@ public IEnumerable ReadOsdb(string fullFileDir, MapCacher mapCacher) if (fileVersion == -1) { - Error("Unrecognized osdb file version"); + throw new CorruptedFileException($"Unrecognized osdb file version (got: {versionString})"); } else { @@ -249,17 +238,14 @@ public IEnumerable ReadOsdb(string fullFileDir, MapCacher mapCacher) map.UserComment = _binReader.ReadString(); } - if (!isMinimalCollection(versionString)) + if (fileVersion >= 8 || (fileVersion >= 5 && IsFullCollection(versionString))) { - if (fileVersion >= 5) - { - map.PlayMode = (PlayMode)_binReader.ReadByte(); - } + map.PlayMode = (PlayMode)_binReader.ReadByte(); + } - if (fileVersion >= 6) - { - map.ModPpStars.Add(map.PlayMode, new StarRating { { 0, _binReader.ReadDouble() } }); - } + if (fileVersion >= 8 || (fileVersion >= 6 && IsFullCollection(versionString))) + { + map.ModPpStars.Add(map.PlayMode, new StarRating { { 0, _binReader.ReadDouble() } }); } collection.AddBeatmap(map); @@ -281,29 +267,11 @@ public IEnumerable ReadOsdb(string fullFileDir, MapCacher mapCacher) if (_binReader.ReadString() != "By Piotrekol") { - Error("File footer is invalid, with could mean that this file is corrupted. CONTINUE AT YOUR OWN RISK"); + _binReader.Close(); + throw new CorruptedFileException("File footer is invalid, this collection might be corrupted."); } _binReader.Close(); - - collections = IssuseVersionRelevantProcedures(fileVersion, fileDate, collections); - } - - private Collections IssuseVersionRelevantProcedures(int fileVersion, DateTime fileDate, Collections collections) - { - if (fileVersion != -1) - { - if (fileVersion < 3) - { - Info("This collection was generated using an older version of Collection Manager." + - Environment.NewLine + - "All download links in this collection will not work." + Environment.NewLine + - "File version: " + fileVersion + Environment.NewLine + - "Date: " + fileDate); - } - } - - return collections; } } } \ No newline at end of file