-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosuCollectionManager.py
More file actions
201 lines (145 loc) · 5.7 KB
/
Copy pathosuCollectionManager.py
File metadata and controls
201 lines (145 loc) · 5.7 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import argparse
# -------------------------------arguments--------------------------------
parser = argparse.ArgumentParser()
parser.add_argument(
"-f", "--file", type=str, required=True, help="path to your collection.db file"
)
parser.add_argument("-l", "--list", action="store_true")
parser.add_argument("-m", "--merge", type=str)
# ------------------------------------------------------------------------
# -------------------------------main-------------------------------------
def main():
args = parser.parse_args()
collectionManager = CollectionManager(args)
MainCollection = Collection()
MainCollection.read_collection(args.file)
if args.list:
collectionManager.list(MainCollection)
if args.merge:
MergeCollection = Collection()
MergeCollection.read_collection(args.merge)
collectionManager.merge_collections(MainCollection, MergeCollection)
# ------------------------------------------------------------------------
# ------------------------------db format---------------------------------
class osuDbReader:
def __init__(self, filepath):
self.file = open(filepath, "rb")
def read_byte(self):
return int.from_bytes(self.file.read(1), "little")
def read_short(self):
return int.from_bytes(self.file.read(2), "little")
def read_int(self):
return int.from_bytes(self.file.read(4), "little")
def read_long(self):
return int.from_bytes(self.file.read(8), "little")
def read_boolean(self):
if self.read_byte == 0:
return False
else:
return True
def read_uleb128(self):
result = 0
shift = 0
while True:
byte = int.from_bytes(self.file.read(1), byteorder="little")
result |= (byte & 0x7F) << shift
if byte & 0x80 == 0:
break
shift += 7
return result
def read_string(self):
if self.read_byte() == 0x0B:
lenght = self.read_uleb128()
return self.file.read(lenght).decode("utf-8")
class osuDbWriter:
def __init__(self, filepath):
self.file = open(filepath, "wb")
def write_int(self, integer):
int_b = integer.to_bytes(4, "little")
self.file.write(int_b)
def get_uleb128(self, integer):
result = 0
shift = 0
while True:
byte = integer
result |= (byte & 0x7F) << shift
# Detect last byte:
if byte & 0x80 == 0:
break
shift += 7
return result.to_bytes(1, "little")
def write_string(self, string):
if not string:
# If the string is empty, the string consists of just this byte
return bytes([0x00])
else:
# Else, it starts with 0x0b
result = bytes([0x0B])
# Followed by the length of the string as an ULEB128
result += self.get_uleb128(len(string))
# Followed by the string in UTF-8
result += string.encode("utf-8")
self.file.write(result)
# ------------------------------------------------------------------------
# --------------------------Collection class------------------------------
class Collection:
def __init__(self):
self.version = None
self.cols_count = None
self.collections = []
def check_collection(self):
if self.cols_count == 0:
print("Collection is empty!")
exit()
def read_collection(self, filepath):
db = osuDbReader(filepath)
self.version = db.read_int()
self.cols_count = db.read_int()
# checking if collection is empty
self.check_collection()
for i in range(self.cols_count):
collection_name = db.read_string()
maps_count = db.read_int()
self.md5hashes = []
for i in range(maps_count):
hash = db.read_string()
self.md5hashes.append(hash)
collection = {
"name": collection_name,
"maps_count": maps_count,
"hashes": self.md5hashes,
}
self.collections.append(collection)
def write_collection(self, filepath):
db = osuDbWriter(filepath)
db.write_int(self.version)
db.write_int(self.cols_count)
for collection in self.collections:
db.write_string(collection["name"])
db.write_int(collection["maps_count"])
for i in range(collection["maps_count"]):
db.write_string(collection["hashes"][i])
# ------------------------------------------------------------------------
# ------------------------Collection Manager------------------------------
class CollectionManager:
def __init__(self, args):
self.songs_folder = None
def list(self, collection):
print("Version:", collection.version)
print("Total collections:", collection.cols_count)
for c in collection.collections:
print(c["name"] + ":")
for hash in c["hashes"]:
print(" -{}".format(hash))
def merge_collections(self, collection_to, collection_from):
merged_collection = Collection()
merged_collection.version = collection_to.version
for c in collection_to.collections:
merged_collection.collections.append(c)
for c in collection_from.collections:
merged_collection.collections.append(c)
merged_collection.cols_count = len(merged_collection.collections)
merged_collection.write_collection("merged_collection.db")
# ------------------------------------------------------------------------
if __name__ == "__main__":
main()