-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCollectionDowngrader.cs
187 lines (151 loc) · 7.45 KB
/
CollectionDowngrader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System.Text.RegularExpressions;
using Realms;
using Realms.Exceptions;
using CollectionDowngrader.LazerSchema;
namespace CollectionDowngrader
{
class CollectionDowngrader
{
const int LazerSchemaVersion = 41;
private static int Main(string[] args)
{
String realmFile, outputFile;
FileStream outStream;
BinaryWriter binWriter;
Realm db;
DateTimeOffset lastMod;
BeatmapCollection? lastModCollection;
if (args.Length != 2)
{
Console.Error.WriteLine("Usage: CollectionDowngrader <path to osu! (lazer) client.realm> " +
"<output osu! (stable) collection.db path>");
return 1;
}
realmFile = args[0];
outputFile = args[1];
if (File.Exists(realmFile)) {
Console.WriteLine("Found realm file.");
} else {
Console.Error.WriteLine("Realm file does not exist, stop.");
return 1;
}
if (File.Exists(outputFile))
{
Console.Error.WriteLine("Output file already exists, aborting.");
return 1;
}
RealmConfiguration config = new(realmFile)
{
IsReadOnly = true,
SchemaVersion = LazerSchemaVersion
};
config.Schema = new[] {
typeof(Beatmap),
typeof(BeatmapCollection),
typeof(BeatmapDifficulty),
typeof(BeatmapMetadata),
typeof(BeatmapSet),
typeof(BeatmapUserSettings),
typeof(RealmFile),
typeof(RealmNamedFileUsage),
typeof(RealmUser),
typeof(Ruleset),
typeof(ModPreset)
};
try
{
db = Realm.GetInstance(config);
}
catch (RealmException re)
{
Console.Error.WriteLine($"Error opening database:\n\n{re.Message}");
// example msg: "Provided schema version A does not equal last set version B."
// example msg2: "Provided schema version A is less than last set version B."
if (re.Message.Contains("less than last set version"))
{
Console.Error.WriteLine("It seemed like the specified osu! (lazer) database is in a new format " +
"which is not compatible with this version of CollectionDowngrader.");
Console.Error.WriteLine("\nYou can go check the project page to see if there's a new release, " +
"or file an Issue on GitHub to let me know it needs updating.");
}
else
{
Regex regex = new Regex(@"Provided schema version (\d+) does not equal last set version (\d+).");
Match match = regex.Match(re.Message);
if (match.Success && match.Groups.Count == 3)
{
int providedVersion = int.Parse(match.Groups[1].Value);
int lastSetVersion = int.Parse(match.Groups[2].Value);
// if provided version is smaller than the last set version, it means CollectionDowngrader is too old
// ask the user to update in this case
if (providedVersion < lastSetVersion)
{
Console.Error.WriteLine("It seemed like the specified osu! (lazer) database is in a new format " +
"which is not compatible with this version of CollectionDowngrader.");
Console.Error.WriteLine("\nYou can go check the project page to see if there's a new release, " +
"or file an Issue on GitHub to let me know it needs updating.");
}
else
{
// otherwise, user installed CollectionDowngrader is too new for the database
// ask the user to update osu! (lazer) client or downgrade CollectionDowngrader in this case
Console.Error.WriteLine("It seemed like the specified osu! (lazer) database is in an old format " +
"which is not compatible with this version of CollectionDowngrader.");
Console.Error.WriteLine("\nYou can try to update your osu! (lazer) client, or downgrade " +
"CollectionDowngrader to an older version.");
}
}
}
return 1;
}
Console.WriteLine("The specified osu! (lazer) database is loaded successfully.");
List<BeatmapCollection> collections = db.All<BeatmapCollection>().ToList();
int collectionCount = collections.Count;
Console.WriteLine($"Found {collectionCount} collections in the database.");
try
{
outStream = File.Open(outputFile, FileMode.CreateNew, FileAccess.Write);
}
catch (IOException ioe)
{
Console.Error.WriteLine($"Cannot create output file for writing: {ioe.Message}");
db.Dispose();
return 1;
}
Console.WriteLine("Output file is created successfully, now start writing data.");
binWriter = new BinaryWriter(outStream);
// find the last modified collection and its modification date
lastModCollection = collections.MaxBy(i => i.LastModified.Ticks);
lastMod = lastModCollection is null ? DateTimeOffset.Now : lastModCollection.LastModified;
try
{
binWriter.Write((int)lastMod.Ticks); // last modification date
binWriter.Write(collectionCount); // collection count
foreach (BeatmapCollection collection in collections)
{
binWriter.Write((byte)0x0b);
binWriter.Write(collection.Name); // collection name
binWriter.Write(collection.BeatmapMD5Hashes.Count); // beatmap count
foreach (string hash in collection.BeatmapMD5Hashes)
{
binWriter.Write((byte)0x0b);
binWriter.Write(hash); // beatmap MD5 hash
}
}
}
catch (IOException ioe)
{
Console.Error.WriteLine($"Error writing data: {ioe.Message}");
binWriter.Close();
outStream.Close();
db.Dispose();
return 1;
}
binWriter.Close();
outStream.Close();
db.Dispose();
Console.WriteLine("Everything is OK.");
return 0;
}
}
}