Skip to content

Commit

Permalink
adding logic to player stats window
Browse files Browse the repository at this point in the history
  • Loading branch information
moth1995 committed Dec 27, 2022
1 parent b5442e5 commit 9f96b01
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
9 changes: 8 additions & 1 deletion editor/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ def to_png(self, disable_alpha=False):
png = PNG(self, disable_alpha)
return png.png

@property
def tk_img(self):
return ImageTk.PhotoImage(Image.open(io.BytesIO(self.to_png())).convert("RGBA"))

class DDS:
data = bytearray()
Expand All @@ -216,7 +219,7 @@ class DDS:
def __init__(self):
pass

def from_bytes(self, pes_image_bytes:bytearray, decrypt:False):
def from_bytes(self, pes_image_bytes:bytearray, decrypt:bool=False):
magic_number = pes_image_bytes[:4]
if not self.__valid_PESImage(magic_number):
raise Exception("not valid PES IMAGE")
Expand Down Expand Up @@ -247,6 +250,10 @@ def dds(self):
self.__setHeader()
return self.header + self.data

@property
def tk_img(self):
return ImageTk.PhotoImage(Image.open(io.BytesIO(self.dds)).convert("RGBA"))


if __name__=="__main__":
file = open(r"C:\Users\marco\Documents\Visual Studio Code\PES_WE_11_14_OF_Editor\test\psp_face", "rb")
Expand Down
44 changes: 29 additions & 15 deletions editor/utils/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import traceback
import zlib
from .constants import *
from editor.images import PNG, PESImg
from editor.images import DDS, PNG, PESImg

def bytes_to_int(ba:bytearray, a:int):
ia = [ba[a + i] for i in range(4)]
Expand Down Expand Up @@ -98,7 +98,7 @@ def write_file_from_mem(location:str, new_file_contents:"bytes|bytearray"):
except:
return False

def get_face_texture(face_bin_location:str):
def get_face_texture(face_bin_location:str, platform:int):
#print(face_bin_location)
face_file_contents = read_file_to_mem(face_bin_location)
unzlib_face_file = bytearray(zlib.decompress(face_file_contents[32:]))
Expand All @@ -109,13 +109,20 @@ def get_face_texture(face_bin_location:str):
texture_offset = struct.unpack("<I", unzlib_face_file[12:16])[0]
else:
raise Exception("Unsupported face file")
pes_img = PESImg()
pes_img.from_bytes(unzlib_face_file[texture_offset:])
pes_img.bgr_to_bgri()
png_obj = PNG(pes_img, True)
return png_obj.png_bytes_to_tk_img()

def get_hair_texture(hair_bin_location:str):
if platform == 0: # ps2
pes_img = PESImg()
pes_img.from_bytes(unzlib_face_file[texture_offset:])
pes_img.bgr_to_bgri()
png_obj = PNG(pes_img, True)
return png_obj.png_bytes_to_tk_img()
elif platform == 1: # psp
dds = DDS()
dds.from_bytes(unzlib_face_file[texture_offset:], True)
return dds.tk_img
else:
raise Exception("Unsupported game version")

def get_hair_texture(hair_bin_location:str, platform:int=-1):
#print(hair_bin_location)
hair_file_contents = read_file_to_mem(hair_bin_location)
unzlib_hair_file = bytearray(zlib.decompress(hair_file_contents[32:]))
Expand All @@ -124,9 +131,16 @@ def get_hair_texture(hair_bin_location:str):
texture_offset = struct.unpack("<I", unzlib_hair_file[12:16])[0]
else:
raise Exception("Unsupported face file")
pes_img = PESImg()
pes_img.from_bytes(unzlib_hair_file[texture_offset:])
pes_img.bgr_to_bgri()
png_obj = PNG(pes_img, True)
return png_obj.png_bytes_to_tk_img()

if platform == 0: # ps2
pes_img = PESImg()
pes_img.from_bytes(unzlib_hair_file[texture_offset:])
pes_img.bgr_to_bgri()
png_obj = PNG(pes_img, True)
return png_obj.png_bytes_to_tk_img()
elif platform == 1: # psp
pes_img = PESImg()
pes_img.from_bytes(unzlib_hair_file[texture_offset:])
pes_img.psp_swizzle()
return pes_img.tk_img
else:
raise Exception("Unsupported game version")
22 changes: 11 additions & 11 deletions gui/player_stats_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,11 @@ def face_idx_var_tracer(self, var:str, index:int, mode:str):
self.has_face_hair_folder and
self.face_hair_folder !="" and
self.face_hair_version !=-1 and
self.player_face_idx_int_var.get() - 1<= len(self.faces_location) and
self.player_face_idx_int_var.get() - 1<= len(self.hairs_location)
self.player_face_idx_int_var.get() - 1< len(self.faces_location) and
self.player_face_idx_int_var.get() - 1< len(self.hairs_location)
):
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1])
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1])
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)
self.face_preview_lbl.config(
image=self.face_img,
relief="solid",
Expand Down Expand Up @@ -657,11 +657,11 @@ def toggle_appearance_menu(self, case:int):
self.has_face_hair_folder and
self.face_hair_folder !="" and
self.face_hair_version !=-1 and
self.player_face_idx_int_var.get() - 1<= len(self.faces_location) and
self.player_face_idx_int_var.get() - 1<= len(self.faces_location)
self.player_face_idx_int_var.get() - 1< len(self.faces_location) and
self.player_face_idx_int_var.get() - 1< len(self.hairs_location)
):
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1])
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1])
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)

self.face_preview_lbl.config(
image=self.face_img,
Expand Down Expand Up @@ -717,10 +717,10 @@ def toggle_appearance_menu(self, case:int):
self.face_hair_folder !="" and
self.face_hair_version !=-1 and
self.player_face_idx_int_var.get() - 1<= len(self.faces_location) and
self.player_face_idx_int_var.get() - 1<= len(self.faces_location)
self.player_face_idx_int_var.get() - 1<= len(self.hairs_location)
):
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1])
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1])
self.face_img = common_functions.get_face_texture(self.faces_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)
self.hair_img = common_functions.get_hair_texture(self.hairs_location[self.player_face_idx_int_var.get() - 1], self.face_hair_version)

self.face_preview_lbl.config(
image=self.face_img,
Expand Down

0 comments on commit 9f96b01

Please sign in to comment.