Skip to content

Commit

Permalink
Reworked language system, added option to export and import pal for m…
Browse files Browse the repository at this point in the history
…anual editing in the file menu
  • Loading branch information
EternalWraith committed Mar 6, 2024
1 parent fed1309 commit 354986a
Show file tree
Hide file tree
Showing 13 changed files with 1,706 additions and 2,321 deletions.
71 changes: 59 additions & 12 deletions palworld_pal_edit/PalEdit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, webbrowser, json, time, uuid, math
import os, webbrowser, json, time, uuid, math, zipfile

# pyperclip
# docs: https://pypi.org/project/pyperclip/#description
Expand Down Expand Up @@ -138,9 +138,9 @@ class PalEdit():
ranks = (0, 1, 2, 3, 4)

def load_i18n(self, lang=""):
path = f"{PalInfo.module_dir}/resources/data/ui.json"
if os.path.exists(f"{PalInfo.module_dir}/resources/data/ui_{lang}.json"):
path = f"{PalInfo.module_dir}/resources/data/ui_{lang}.json"
path = f"{PalInfo.module_dir}/resources/data/en-GB/ui.json"
if os.path.exists(f"{PalInfo.module_dir}/resources/data/{lang}/ui.json"):
path = f"{PalInfo.module_dir}/resources/data/{lang}/ui.json"
with open(path, "r", encoding="utf-8") as f:
keys = json.load(f)
for i18n_k in keys: # For multi lang didn't translate with original one
Expand All @@ -166,7 +166,7 @@ def load_i18n(self, lang=""):
PalInfo.LoadPals(lang)
PalInfo.LoadAttacks(lang)
PalInfo.LoadPassives(lang)
self.attackops.clear()
self.attackops = []
for e in PalInfo.PalAttacks:
self.attackops.append(PalInfo.PalAttacks[e])
self.attackops.remove("None")
Expand Down Expand Up @@ -733,6 +733,8 @@ def loadpal(self, paldata):

self.changetext(-1)
self.jump()
self.enable_menus()


def jump(self):
self.gui.attributes('-topmost', 1)
Expand Down Expand Up @@ -795,6 +797,8 @@ def savefile(self):
self.jump()
messagebox.showinfo("Done", "Done saving!")
self.resetTitle()
self.disable_menus()


def savepson(self, filename):
f = open(filename, "w", encoding="utf8")
Expand Down Expand Up @@ -984,7 +988,7 @@ def spawnpal(self):
if not os.path.exists(playersav):
print("Cannot Load Player Save!")
return
player = PalPlayerEntity(palworld_pal_edit.SaveConverter.convert_sav_to_obj(playersav))
player = PalInfo.PalPlayerEntity(palworld_pal_edit.SaveConverter.convert_sav_to_obj(playersav))
palworld_pal_edit.SaveConverter.convert_obj_to_sav(player.dump(), playersav + ".bak", True)

file = askopenfilename(filetypes=[("json files", "*.json")])
Expand All @@ -1008,6 +1012,7 @@ def spawnpal(self):
print("Player Pal Storage is full!")
return
pal.InitializationPal(newguid, playerguid, groupguid, slotguid)
pal.SetSoltIndex(i)
self.palguidmanager.AddGroupSaveData(groupguid, newguid)
self.palguidmanager.SetContainerSave(slotguid, i, newguid)
self.data['properties']['worldSaveData']['value']['CharacterSaveParameterMap']['value'].append(pal._data)
Expand Down Expand Up @@ -1169,8 +1174,21 @@ def createWindow(self):
def resetTitle(self):
self.gui.title(f"PalEdit v{PalEditConfig.version}")

def add_lang_menu(self, langmenu, languages, lang):
langmenu.add_command(label=languages[lang], command=lambda: self.load_i18n(lang))
def add_lang_menu(self, langmenu, lang):
with open(f"{PalInfo.module_dir}/resources/data/{lang}/ui.json", "r", encoding="utf-8") as f:
content = json.load(f)
l = content["language"]
langmenu.add_command(label=l, command=lambda: self.load_i18n(lang))

def disable_menus(self):
filemenu.entryconfig(self.i18n['menu_save'], state="disabled")
filemenu.entryconfig(self.i18n['menu_export'], state="disabled")
filemenu.entryconfig(self.i18n['menu_import'], state="disabled")

def enable_menus(self):
filemenu.entryconfig(self.i18n['menu_save'], state="normal")
filemenu.entryconfig(self.i18n['menu_export'], state="normal")
filemenu.entryconfig(self.i18n['menu_import'], state="normal")

def build_menu(self):
self.menu = tk.Menu(self.gui)
Expand All @@ -1184,6 +1202,14 @@ def build_menu(self):
filemenu.add_command(label=self.i18n['menu_save'], command=self.savefile)
self.i18n_el['menu_save'] = (filemenu, 1)

filemenu.add_separator()
filemenu.add_command(label=self.i18n['menu_export'], command=self.dumppals)
self.i18n_el['menu_export'] = (filemenu, 3)
filemenu.add_command(label=self.i18n['menu_import'], command=self.spawnpal)
self.i18n_el['menu_import'] = (filemenu, 4)

self.disable_menus()

tools.add_cascade(label="File", menu=filemenu, underline=0)

toolmenu = tk.Menu(tools, tearoff=0)
Expand All @@ -1193,10 +1219,31 @@ def build_menu(self):
tools.add_cascade(label="Tools", menu=toolmenu, underline=0)

langmenu = tk.Menu(tools, tearoff=0)
with open(f"{PalInfo.module_dir}/resources/data/ui-lang.json", "r", encoding="utf-8") as f:
languages = json.load(f)
for lang in languages:
self.add_lang_menu(langmenu, languages, lang)
langops = next(os.walk(f"{PalInfo.module_dir}/resources/data"))
langops = [x for x in langops[1]]
#with open(f"{PalInfo.module_dir}/resources/data/ui-lang.json", "r", encoding="utf-8") as f:
#languages = json.load(f)
#for lang in languages:
#self.add_lang_menu(langmenu, languages, lang)

for lang in langops:
self.add_lang_menu(langmenu, lang)


def installlang():
file = askopenfilename(filetypes=[("Zip files", "*.zip")])
if file:
with zipfile.ZipFile(file, "r") as z:
name = file.split("/")[-1].replace(".zip","")
z.extractall(f"{PalInfo.module_dir}/resources/data/{name}")
langmenu.delete(langmenu.index("end")-1, langmenu.index("end"))
self.add_lang_menu(langmenu, name)
dlclangs()

def dlclangs():
langmenu.add_separator()
langmenu.add_command(label="Install language...", command=installlang)
dlclangs()

tools.add_cascade(label="Language", menu=langmenu, underline=0)

Expand Down
99 changes: 64 additions & 35 deletions palworld_pal_edit/PalInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,23 +874,33 @@ def LoadPals(lang=None):
PassiveRating = {}


def LoadPassives(lang=None):
def LoadPassives(lang="en-GB"):
global PalPassives, PassiveDescriptions, PassiveRating

PalPassives = {}
PassiveDescriptions = {}
PassiveRating = {}

if lang is not None and not os.path.exists("%s/resources/data/passives%s.json" % (module_dir, "_" + lang)):
lang = None
if lang == "":
lang = "en-GB"

if lang is not None and not os.path.exists(f"%s/resources/data/{lang}/passives.json" % (module_dir)):
lang = "en-GB"

with open("%s/resources/data/passives.json" % (module_dir), "r",
encoding="utf8") as datafile:
with open(f"%s/resources/data/{lang}/passives.json" % (module_dir), "r",
encoding="utf8") as passivefile:

with open("%s/resources/data/passives%s.json" % (module_dir, "_" + lang if lang is not None else ""), "r",
encoding="utf8") as passivefile:
for i in json.loads(passivefile.read())["values"]:
PalPassives[i["CodeName"]] = i["Name"]
PassiveDescriptions[i["CodeName"]] = i["Description"]
PassiveRating[i["CodeName"]] = i["Rating"]
PalPassives = dict(sorted(PalPassives.items()))
d = json.loads(datafile.read())
l = json.loads(passivefile.read())

for i in d:
code = i
PalPassives[code] = l[code]["Name"]
PassiveDescriptions[code] = l[code]["Description"]
PassiveRating[code] = d[i]["Rating"]
PalPassives = dict(sorted(PalPassives.items()))


LoadPassives()
Expand All @@ -902,33 +912,40 @@ def LoadPassives(lang=None):
SkillExclusivity = {}


def LoadAttacks(lang=None):
def LoadAttacks(lang="en-GB"):
global PalAttacks, AttackPower, AttackTypes, SkillExclusivity

if lang is not None and not os.path.exists("%s/resources/data/attacks%s.json" % (module_dir, "_" + lang)):
lang = None

with open("%s/resources/data/attacks%s.json" % (module_dir, "_" + lang if lang is not None else ""), "r",
encoding="utf8") as attackfile:
PalAttacks = {}
AttackPower = {}
AttackTypes = {}
SkillExclusivity = {}

l = json.loads(attackfile.read())

debugOutput = l["values"]

for i in l["values"]:
PalAttacks[i["CodeName"]] = i["Name"]
AttackPower[i["CodeName"]] = i["Power"]
AttackTypes[i["CodeName"]] = i["Type"]
if "Exclusive" in i:
SkillExclusivity[i["CodeName"]] = i["Exclusive"]
else:
SkillExclusivity[i["CodeName"]] = None
if lang == "":
lang = "en-GB"

if lang is not None and not os.path.exists(f"%s/resources/data/{lang}/attacks.json" % (module_dir)):
lang = "en-GB"

with open("%s/resources/data/attacks.json" % (module_dir), "r",
encoding="utf8") as datafile:
with open(f"%s/resources/data/{lang}/attacks.json" % (module_dir), "r",
encoding="utf8") as attackfile:
PalAttacks = {}
AttackPower = {}
AttackTypes = {}
SkillExclusivity = {}

d = json.loads(datafile.read())
l = json.loads(attackfile.read())

#debugOutput = d["values"]

for i in d:
code = i
PalAttacks[code] = l[code]
AttackPower[code] = d[i]["Power"]
AttackTypes[code] = d[i]["Type"]
if "Exclusive" in d[i]:
SkillExclusivity[code] = d[i]["Exclusive"]
else:
SkillExclusivity[code] = None

PalAttacks = dict(sorted(PalAttacks.items()))
PalAttacks = dict(sorted(PalAttacks.items()))

LoadAttacks()

Expand All @@ -947,7 +964,7 @@ def find(name):

if __name__ == "__main__":
# Convert Pals -> Moveset from Name to CodeName for i18n
with open("%s/resources/data/pals.json" % (module_dir), "r", encoding="utf-8") as f:
"""with open("%s/resources/data/pals.json" % (module_dir), "r", encoding="utf-8") as f:
pals = json.load(f)
for pal in pals['values']:
if 'Moveset' in pal:
Expand All @@ -964,6 +981,18 @@ def find(name):
with open("%s/resources/data/pals.json" % (module_dir), "w", encoding="utf-8") as f:
json.dump(pals, f, indent=4)
"""

with open(f"%s/resources/data/passives.json" % (module_dir), "r", encoding="utf-8") as f:
ps = json.load(f)
o = {}
for p in ps['values']:
o[p["CodeName"]] = {
"Rating": p["Rating"],
}
with open(f"%s/resources/data/passives.json" % (module_dir), "w", encoding="utf-8") as f:
json.dump(o, f, indent=4, ensure_ascii=False)


##
##
Expand Down
Loading

0 comments on commit 354986a

Please sign in to comment.