From b1d7a83ee26d8b8df0e98d7215745560ebb123ff Mon Sep 17 00:00:00 2001 From: HoLLy Date: Wed, 9 Aug 2017 21:54:19 +0200 Subject: [PATCH 01/19] Refactoring, move classes to namespaces --- UnitTestProject/UnitTest1.cs | 2 ++ .../{ => Databases}/CollectionDb.cs | 12 ++++----- osu-database-reader/{ => Databases}/OsuDb.cs | 6 +++-- .../{ => Databases/Parts}/BeatmapEntry.cs | 8 +++--- .../{ => Databases/Parts}/Player.cs | 11 +++----- .../{ => Databases/Parts}/Replay.cs | 13 ++++------ .../{ => Databases/Parts}/Structs.cs | 8 ++---- .../{ => Databases}/PresenceDb.cs | 12 ++++----- .../{ => Databases}/ScoresDb.cs | 8 +++--- .../CustomBinaryReader.cs} | 26 +++++++++---------- .../osu-database-reader.csproj | 18 ++++++------- 11 files changed, 56 insertions(+), 68 deletions(-) rename osu-database-reader/{ => Databases}/CollectionDb.cs (78%) rename osu-database-reader/{ => Databases}/OsuDb.cs (88%) rename osu-database-reader/{ => Databases/Parts}/BeatmapEntry.cs (96%) rename osu-database-reader/{ => Databases/Parts}/Player.cs (84%) rename osu-database-reader/{ => Databases/Parts}/Replay.cs (85%) rename osu-database-reader/{ => Databases/Parts}/Structs.cs (60%) rename osu-database-reader/{ => Databases}/PresenceDb.cs (70%) rename osu-database-reader/{ => Databases}/ScoresDb.cs (84%) rename osu-database-reader/{CustomReader.cs => IO/CustomBinaryReader.cs} (66%) diff --git a/UnitTestProject/UnitTest1.cs b/UnitTestProject/UnitTest1.cs index d92b4cb..a953349 100644 --- a/UnitTestProject/UnitTest1.cs +++ b/UnitTestProject/UnitTest1.cs @@ -5,6 +5,8 @@ using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using osu_database_reader; +using osu_database_reader.Databases; +using osu_database_reader.Databases.Parts; namespace UnitTestProject { diff --git a/osu-database-reader/CollectionDb.cs b/osu-database-reader/Databases/CollectionDb.cs similarity index 78% rename from osu-database-reader/CollectionDb.cs rename to osu-database-reader/Databases/CollectionDb.cs index 899dd2f..53f402e 100644 --- a/osu-database-reader/CollectionDb.cs +++ b/osu-database-reader/Databases/CollectionDb.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using osu_database_reader.Databases.Parts; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases { public class CollectionDb { @@ -15,7 +13,7 @@ public class CollectionDb public static CollectionDb Read(string path) { var db = new CollectionDb(); - using (CustomReader r = new CustomReader(File.OpenRead(path))) { + using (CustomBinaryReader r = new CustomBinaryReader(File.OpenRead(path))) { db.OsuVersion = r.ReadInt32(); int amount = r.ReadInt32(); diff --git a/osu-database-reader/OsuDb.cs b/osu-database-reader/Databases/OsuDb.cs similarity index 88% rename from osu-database-reader/OsuDb.cs rename to osu-database-reader/Databases/OsuDb.cs index 3ce0779..96b5b78 100644 --- a/osu-database-reader/OsuDb.cs +++ b/osu-database-reader/Databases/OsuDb.cs @@ -2,8 +2,10 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using osu_database_reader.Databases.Parts; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases { public class OsuDb { @@ -18,7 +20,7 @@ public class OsuDb public static OsuDb Read(string path) { OsuDb db = new OsuDb(); - using (CustomReader r = new CustomReader(File.OpenRead(path))) { + using (CustomBinaryReader r = new CustomBinaryReader(File.OpenRead(path))) { db.OsuVersion = r.ReadInt32(); db.FolderCount = r.ReadInt32(); db.AccountUnlocked = r.ReadBoolean(); diff --git a/osu-database-reader/BeatmapEntry.cs b/osu-database-reader/Databases/Parts/BeatmapEntry.cs similarity index 96% rename from osu-database-reader/BeatmapEntry.cs rename to osu-database-reader/Databases/Parts/BeatmapEntry.cs index 28629ee..d8d9680 100644 --- a/osu-database-reader/BeatmapEntry.cs +++ b/osu-database-reader/Databases/Parts/BeatmapEntry.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases.Parts { public class BeatmapEntry { @@ -42,7 +42,7 @@ public class BeatmapEntry public int Unknown2; public byte ManiaScrollSpeed; - public static BeatmapEntry ReadFromReader(CustomReader r, bool readLength = true, int version = 20160729) { + public static BeatmapEntry ReadFromReader(CustomBinaryReader r, bool readLength = true, int version = 20160729) { BeatmapEntry e = new BeatmapEntry(); int length = 0; diff --git a/osu-database-reader/Player.cs b/osu-database-reader/Databases/Parts/Player.cs similarity index 84% rename from osu-database-reader/Player.cs rename to osu-database-reader/Databases/Parts/Player.cs index 80c3960..89400e2 100644 --- a/osu-database-reader/Player.cs +++ b/osu-database-reader/Databases/Parts/Player.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; +using System; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases.Parts { public class Player { @@ -19,7 +16,7 @@ public class Player public int GlobalRank; public DateTime Unknown1; //TODO: name this. Last update time? - public static Player ReadFromReader(CustomReader r) { + public static Player ReadFromReader(CustomBinaryReader r) { Player p = new Player(); p.PlayerId = r.ReadInt32(); diff --git a/osu-database-reader/Replay.cs b/osu-database-reader/Databases/Parts/Replay.cs similarity index 85% rename from osu-database-reader/Replay.cs rename to osu-database-reader/Databases/Parts/Replay.cs index 00a9ada..dda7c80 100644 --- a/osu-database-reader/Replay.cs +++ b/osu-database-reader/Databases/Parts/Replay.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; +using System; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases.Parts { public class Replay //used for both scores.db and .osr files { @@ -24,13 +21,13 @@ namespace osu_database_reader public static Replay Read(string path) { Replay replay; - using (CustomReader r = new CustomReader(File.OpenRead(path))) { + using (CustomBinaryReader r = new CustomBinaryReader(File.OpenRead(path))) { replay = ReadFromReader(r); //scoreid should not be needed } return replay; } - public static Replay ReadFromReader(CustomReader r, bool readScoreId = false) { + public static Replay ReadFromReader(CustomBinaryReader r, bool readScoreId = false) { Replay replay = new Replay { GameMode = (GameMode) r.ReadByte(), OsuVersion = r.ReadInt32(), diff --git a/osu-database-reader/Structs.cs b/osu-database-reader/Databases/Parts/Structs.cs similarity index 60% rename from osu-database-reader/Structs.cs rename to osu-database-reader/Databases/Parts/Structs.cs index 709c156..e20d395 100644 --- a/osu-database-reader/Structs.cs +++ b/osu-database-reader/Databases/Parts/Structs.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; -namespace osu_database_reader +namespace osu_database_reader.Databases.Parts { public struct TimingPoint { diff --git a/osu-database-reader/PresenceDb.cs b/osu-database-reader/Databases/PresenceDb.cs similarity index 70% rename from osu-database-reader/PresenceDb.cs rename to osu-database-reader/Databases/PresenceDb.cs index 3a3cbc4..d2ade50 100644 --- a/osu-database-reader/PresenceDb.cs +++ b/osu-database-reader/Databases/PresenceDb.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using osu_database_reader.Databases.Parts; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases { public class PresenceDb { @@ -15,7 +13,7 @@ public class PresenceDb public static PresenceDb Read(string path) { var db = new PresenceDb(); - using (CustomReader r = new CustomReader(File.OpenRead(path))) { + using (CustomBinaryReader r = new CustomBinaryReader(File.OpenRead(path))) { db.OsuVersion = r.ReadInt32(); int amount = r.ReadInt32(); diff --git a/osu-database-reader/ScoresDb.cs b/osu-database-reader/Databases/ScoresDb.cs similarity index 84% rename from osu-database-reader/ScoresDb.cs rename to osu-database-reader/Databases/ScoresDb.cs index d1b533e..5747959 100644 --- a/osu-database-reader/ScoresDb.cs +++ b/osu-database-reader/Databases/ScoresDb.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; +using osu_database_reader.Databases.Parts; +using osu_database_reader.IO; -namespace osu_database_reader +namespace osu_database_reader.Databases { public class ScoresDb { @@ -16,7 +16,7 @@ public class ScoresDb public static ScoresDb Read(string path) { var db = new ScoresDb(); - using (CustomReader r = new CustomReader(File.OpenRead(path))) + using (CustomBinaryReader r = new CustomBinaryReader(File.OpenRead(path))) { db.OsuVersion = r.ReadInt32(); int amount = r.ReadInt32(); diff --git a/osu-database-reader/CustomReader.cs b/osu-database-reader/IO/CustomBinaryReader.cs similarity index 66% rename from osu-database-reader/CustomReader.cs rename to osu-database-reader/IO/CustomBinaryReader.cs index 3e0c4e7..c69910b 100644 --- a/osu-database-reader/CustomReader.cs +++ b/osu-database-reader/IO/CustomBinaryReader.cs @@ -1,18 +1,16 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; -using System.Linq; using System.Text; -using System.Threading.Tasks; +using osu_database_reader.Databases.Parts; -namespace osu_database_reader +namespace osu_database_reader.IO { - public class CustomReader : BinaryReader + public class CustomBinaryReader : BinaryReader { - public CustomReader(Stream input) : base(input) {} - public CustomReader(Stream input, Encoding encoding) : base(input, encoding) {} - public CustomReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) {} + public CustomBinaryReader(Stream input) : base(input) {} + public CustomBinaryReader(Stream input, Encoding encoding) : base(input, encoding) {} + public CustomBinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) {} public byte[] ReadBytes() { // an overload to ReadBytes(int count) int length = ReadInt32(); @@ -26,23 +24,23 @@ public override string ReadString() { if (b == 0x0B) return base.ReadString(); else if (b == 0x00) - return string.Empty; + return null; else - throw new Exception($"Continuation byte is not 0x00 or 0x11, but is 0x{b.ToString("X2")} (position: {BaseStream.Position})"); + throw new Exception($"Type byte is not 0x00 or 0x11, but is 0x{b:X2} (position: {BaseStream.Position})"); } public DateTime ReadDateTime() { - long idk = ReadInt64(); - return new DateTime(idk, DateTimeKind.Utc); + long ticks = ReadInt64(); + return new DateTime(ticks, DateTimeKind.Utc); } public Dictionary ReadModsDoubleDictionary() { int length = ReadInt32(); Dictionary dicks = new Dictionary(); for (int i = 0; i < length; i++) { - ReadByte(); //type (0x08) + ReadByte(); //type (0x08, Int32) int key = ReadInt32(); - ReadByte(); //type (0x0D) + ReadByte(); //type (0x0D, Double) double value = ReadDouble(); dicks.Add((Mods)key, value); } diff --git a/osu-database-reader/osu-database-reader.csproj b/osu-database-reader/osu-database-reader.csproj index 2166c2f..df39508 100644 --- a/osu-database-reader/osu-database-reader.csproj +++ b/osu-database-reader/osu-database-reader.csproj @@ -40,17 +40,17 @@ - - - + + + - - - + + + - - - + + +