Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Cracko298 authored Aug 31, 2024
1 parent 071382d commit a0e8035
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
4 changes: 2 additions & 2 deletions convertBlangToJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
os.makedirs(od,exist_ok=True)

for filename in os.listdir(id):
if filename.endswith(".blang"):
if filename.endswith(".blang") and '-pocket' in filename:
print(filename)
blangFile = BlangFile().open(f"{id}\\{filename}")
blangFile.toJson(od)
blangFile.exportToJson(f"{od}\\{filename.replace('.blang','.json')}")

if os.name == 'nt':
os.system('pause')
9 changes: 6 additions & 3 deletions convertJsonToBlang.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
os.makedirs(od,exist_ok=True)

for filename in os.listdir(id):
if filename.endswith(".json"):
if filename.endswith(".json") and '-pocket' in filename:
print(filename)
inputFile = os.path.join(id, filename)
blangFile = BlangFile().fromJson(inputFile)
blangFile.export(od)
with open(inputFile, "r", encoding="utf-8") as f:
jsonData = f.read()

blangFile = BlangFile().importFromJson(jsonData)
blangFile.export(f"{od}\\{filename.replace('.json','.blang')}")

if os.name == "nt":
os.system("pause")
51 changes: 22 additions & 29 deletions mc3dsblang.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class BlangFile:
def __init__(self):
return

def open(self, path: str = None):
if path == None:
raise MC3DSBlangException("path is empty")
if type(path) != str:
raise MC3DSBlangException("path must be a 'str'")

self.filename = Path(path).stem

def open(self, path: str|Path):
if isinstance(path, str):
path = Path(path)
elif isinstance(path, Path):
pass
else:
raise TypeError("path must be a 'str' or 'Path'")
with open(path, "rb") as f:
file_content = list(f.read())

Expand Down Expand Up @@ -62,20 +62,20 @@ def open(self, path: str = None):
def getData(self):
return self.data

def getTexts(self):
def getTexts(self) -> list:
return self.texts

def replace(self, text: str, newtext: str):
if type(text) != str:
raise MC3DSBlangException("text must be a 'str'")
def replace(self, idx: int, newtext: str):
if type(idx) != int:
raise MC3DSBlangException("idx must be an 'int'")
if type(newtext) != str:
raise MC3DSBlangException("newtext must be a 'str'")

if text in self.texts:
if idx >= 0 and idx < len(self.texts):
if newtext != "" and newtext != '':
self.texts[self.texts.index(text)] = newtext
self.texts[idx] = newtext
else:
self.texts[self.texts.index(text)] = " "
self.texts[idx] = " "
return

def export(self, path: str):
Expand Down Expand Up @@ -111,42 +111,35 @@ def export(self, path: str):

self.exportData = bytearray(self.exportData)

with open(os.path.join(path, f"{self.filename}.blang"), "wb") as f:
with open(path, "wb") as f:
f.write(self.exportData)
return

def toJson(self, path: str):
def exportToJson(self, path: str):
long = len(self.data)
dataDictionary = {}
for i in range(0, long):
item = self.data[i]
identifier = []
for j in range(0, 4):
identifier.append(item[j])
identifier = bytearray(identifier)
identifier = bytearray(self.data[i])
identifier = int.from_bytes(identifier, "little")
identifier = str(identifier)

dataDictionary[identifier] = {}
dataDictionary[identifier]["order"] = i + 1
dataDictionary[identifier]["text"] = self.texts[i]

outFile = open(os.path.join(path, f"{self.filename}.json"), "w", encoding="utf-8")
outFile = open(path, "w", encoding="utf-8")
json.dump(dataDictionary, outFile, indent=4, ensure_ascii=False)
outFile.close()
return

def fromJson(self, path: str):
if type(path) != str:
def importFromJson(self, json_string: str):
if type(json_string) != str:
raise MC3DSBlangException("path must be a 'str'")

data = []
texts = []

with open(path, "r", encoding="utf-8") as jsonData:
dataDictionary = json.load(jsonData)

self.filename = Path(path).stem
dataDictionary = json.loads(json_string)

idx = 1
while idx <= len(dataDictionary):
Expand Down

0 comments on commit a0e8035

Please sign in to comment.