Skip to content

Commit

Permalink
Misc: update osdb format to include starrating and playmode data in m…
Browse files Browse the repository at this point in the history
…inimal mode
  • Loading branch information
Piotrekol committed Dec 17, 2019
1 parent 25e89aa commit 8949798
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 61 deletions.
23 changes: 14 additions & 9 deletions App/SidePanelActionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions CollectionManagerDll/Exceptions/CorruptedFileException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace CollectionManager.Exceptions
{
public class CorruptedFileException : Exception
{
public CorruptedFileException(string message) : base(message)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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);
Expand All @@ -179,7 +169,6 @@ public IEnumerable<Collection> ReadOsdb(string fullFileDir, MapCacher mapCacher)
_binReader = new BinaryReader(_memStream);
}


_binReader.BaseStream.Seek(0, SeekOrigin.Begin);
var versionString = _binReader.ReadString();
//check header
Expand All @@ -190,7 +179,7 @@ public IEnumerable<Collection> ReadOsdb(string fullFileDir, MapCacher mapCacher)

if (fileVersion == -1)
{
Error("Unrecognized osdb file version");
throw new CorruptedFileException($"Unrecognized osdb file version (got: {versionString})");
}
else
{
Expand Down Expand Up @@ -249,17 +238,14 @@ public IEnumerable<Collection> 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);
Expand All @@ -281,29 +267,11 @@ public IEnumerable<Collection> 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;
}
}
}

0 comments on commit 8949798

Please sign in to comment.