From 94603bbdf923e77af646dccea2d75f4f3382b390 Mon Sep 17 00:00:00 2001 From: Farewell_ Date: Sat, 18 Nov 2023 08:59:55 +0100 Subject: [PATCH] Testing alphabetical sort --- checkDatatables.py | 46 +------------- helpers.py | 49 +++++++++++++++ sortAlphabetically.py | 135 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 43 deletions(-) create mode 100644 helpers.py create mode 100644 sortAlphabetically.py diff --git a/checkDatatables.py b/checkDatatables.py index 344ab1c..d0f127c 100644 --- a/checkDatatables.py +++ b/checkDatatables.py @@ -4,12 +4,14 @@ from encryption import decrypt_file import json import os +from helpers import doesPathExist, findAllObjects, findDoubloninList, findKeyInList + # "japaneseText" # "englishUsText" # "chineseTText" # "koreanText" # "chineseSText" -language = "englishUsText" +language = "japaneseText" isCHN = False @@ -80,10 +82,6 @@ except: words = None # endregion -# Forcing japanese language on 08.18 as this is what is usually used for omnimix. -if isCHN: - language = "japaneseText" - # region Classes And Methods class Genres(Enum): @@ -104,44 +102,6 @@ class Genres(Enum): return cls.Unknown -def findKeyInList(list: list, key: str, keyValue, value=None): - for object in list: - if object[key] == keyValue: - if value is not None: - return object[value] - else: - return object - - if value is not None: - return "" - else: - return None - - -def findAllObjects(list: list, key: str, keyValue): - templist = [] - templist.append(list) - objects = [] - - for element in templist[0]: - if element[key] == keyValue: - objects.append(element) - - return objects - - -def findDoubloninList(list: list, key: str, keyValue): - if len(findAllObjects(list=list, key=key, keyValue=keyValue)) > 1: - return True - return False - - -def doesPathExist(path: str): - if os.path.exists(path): - return True - return False - - def initCheckFile(): global checkFile checkFile = { diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..6b6e856 --- /dev/null +++ b/helpers.py @@ -0,0 +1,49 @@ +import os + + +def findKeyInList(list: list, key: str, keyValue, value=None): + for object in list: + try: + if object[key] == keyValue: + if value is not None: + return object[value] + else: + return object + except: + if value is not None: + print( + value + + " doesn't exist in " + + str(object) + + ", are you using the right language ?" + ) + exit(0) + + if value is not None: + return "" + else: + return None + + +def findAllObjects(list: list, key: str, keyValue): + templist = [] + templist.append(list) + objects = [] + + for element in templist[0]: + if element[key] == keyValue: + objects.append(element) + + return objects + + +def findDoubloninList(list: list, key: str, keyValue): + if len(findAllObjects(list=list, key=key, keyValue=keyValue)) > 1: + return True + return False + + +def doesPathExist(path: str): + if os.path.exists(path): + return True + return False diff --git a/sortAlphabetically.py b/sortAlphabetically.py new file mode 100644 index 0000000..cbf28ae --- /dev/null +++ b/sortAlphabetically.py @@ -0,0 +1,135 @@ +import gzip +import json +import shutil + +from helpers import doesPathExist, findAllObjects, findKeyInList + +from encryption import decrypt_file + +# "japaneseText" +# "englishUsText" +# "chineseTText" +# "koreanText" +# "chineseSText" +language = "japaneseText" +isChn = False + +# Loading musicinfo.bin, music_order.bin and wordlist.bin +try: + info = json.load(gzip.open("./Data/x64/datatable/musicinfo.bin", "rb"))["items"] + order = json.load(gzip.open("./Data/x64/datatable/music_order.bin", "rb"))["items"] + words = json.load(gzip.open("./Data/x64/datatable/wordlist.bin", "rb"))["items"] +except: + try: + info = json.loads( + decrypt_file(input_file="./Data/x64/datatable/musicinfo.bin") + )["items"] + order = json.loads( + decrypt_file(input_file="./Data/x64/datatable/music_order.bin") + )["items"] + words = json.load(gzip.open("./Data/x64/datatable/wordlist.bin", "rb"))["items"] + isChn = True + except: + print("Couldn't load files, exiting.") + exit(0) + + +class Entry: + name = "" + genreNo = -1 + id = "" + uniqueId = -1 + closeDispType = -1 + + def __init__(self, name, genreNo, id, uniqueId, closeDispType): + self.name = name + self.genreNo = genreNo + self.id = id + self.uniqueId = uniqueId + self.closeDispType = closeDispType + + def toJson(self): + return { + # "name": self.name, + "genreNo": self.genreNo, + "id": self.id, + "uniqueId": self.uniqueId, + "closeDispType": self.closeDispType, + } + + +# Adding all the existing songs in song_order in an array +entries = [] +for entry in order: + name = findKeyInList( + list=words, + key="key", + keyValue="song_" + entry["id"], + value=language, + ) + + if name == "": + print(entry["id"] + " is missing a name") + + entries.append( + Entry( + name=name, + genreNo=entry["genreNo"], + id=entry["id"], + uniqueId=entry["uniqueId"], + closeDispType=entry["closeDispType"], + ) + ) + +# # Adding all the missing songs in song_order in an array +# for entry in info: +# alreadyIn = False +# occurences = findAllObjects(list=order, key="id", keyValue=entry["id"]) +# if len(occurences) > 0: +# for occurence in occurences: +# if entry["genreNo"] == occurence["genreNo"]: +# alreadyIn = True +# break + +# if not alreadyIn: +# name = findKeyInList( +# list=words, +# key="key", +# keyValue="song_" + entry["id"], +# value=language, +# ) + +# if name == "": +# print(entry["id"] + " is missing a name") +# continue + +# print("Adding " + entry["id"]) +# entries.append( +# Entry( +# name=name, +# genreNo=entry["genreNo"], +# id=entry["id"], +# uniqueId=entry["uniqueId"], +# closeDispType=0, +# ) +# ) + +# Sorting names alphabetically. +entries.sort(key=lambda x: x.name, reverse=False) + +# Backing up the original order file +if not doesPathExist(path="./Data/x64/datatable/music_order.bin.bak"): + print("Backed up music_order") + dest = shutil.move( + "./Data/x64/datatable/music_order.bin", + "./Data/x64/datatable/music_order.bin.bak", + ) + +file = [] +for entry in entries: + file.append(entry.toJson()) +# Writing song_order.bin +json_object = json.dumps(file, ensure_ascii=False, indent="\t") +if not isChn: + with open("./Data/x64/datatable/music_order.bin", "w", encoding="utf8") as outfile: + outfile.write(gzip.compress(json_object))