loadGallery SDK Fix & seen_image

This commit is contained in:
nutbuster 2021-07-11 10:37:33 +10:00
parent f332a988c2
commit 8117fa1981

View file

@ -83,28 +83,23 @@ init python:
# Appends extra spaces at the end # Appends extra spaces at the end
# () -> None # () -> None
def loadGallery(): def loadGallery():
from os import listdir, getcwd
from os.path import isfile, join
cgPath = "images/cgs/" cgPath = "images/cgs/"
workingDirPath = getcwd().replace("\\", "/")
cgDirPath = workingDirPath + "/game/" + cgPath
# Reset gallery # Reset gallery
galleryItems = [] galleryItems = []
g = Gallery() g = Gallery()
g.transition = dissolve g.transition = dissolve
list_img = renpy.list_images()
# Add each image to the gallery # Add each image to the gallery
for cgFile in listdir(cgDirPath): for str in list_img:
filePath = join(cgDirPath, cgFile) _str = cgPath+str+"."+ACCEPTED_EXTENSIONS[0]
if isfile(filePath): if renpy.loadable(_str): #brute force
ext = cgFile[-3:].lower() unlocked = renpy.seen_image(str)
if (ext in ACCEPTED_EXTENSIONS): image = renpy.image_size(Image(_str))
attr = getImageFileAttributes(filePath) addGalleryItem(str, ACCEPTED_EXTENSIONS[0], image[0], image[1], unlocked)
fname = str(cgFile[0:-4])
unlocked = fname in persistent.cggallery
addGalleryItem(fname, ext, attr["w"], attr["h"], unlocked)
# Add empty items to fill grid after last cg button # Add empty items to fill grid after last cg button
extraSpaces = GALLERY_COLS - (len(galleryItems) % GALLERY_COLS) extraSpaces = GALLERY_COLS - (len(galleryItems) % GALLERY_COLS)
@ -115,52 +110,6 @@ init python:
"ext": None "ext": None
}) })
# Get width/height of image by absolute path
# based on https://stackoverflow.com/questions/8032642/how-to-obtain-image-size-using-standard-python-class-without-using-external-lib
# (filePath: string) -> { w: int; h: int } | None
def getImageFileAttributes(filePath):
try:
from struct import unpack
from imghdr import what
w, h = 0, 0
with open(filePath, "rb") as file:
ext = what(filePath)
header = file.read(24)
if (len(header) != 24):
raise Exception("File header invalid for " + filePath)
if (ext == 'png'):
checksum = unpack('>i', header[4:8])[0]
if (checksum != 0x0d0a1a0a):
raise Exception("File checksum invalid for " + filePath)
w, h = unpack('>ii', head[16:24])
if (ext == 'jpeg' or ext == 'jpg'):
file.seek(0)
size = 2
ftype = 0
while not 0xc0 <= ftype <= 0xcf:
file.seek(size, 1)
byte = file.read(1)
while ord(byte) == 0xff:
byte = file.read(1)
ftype = ord(byte)
size = unpack('>H', file.read(2))[0] - 2
file.seek(1, 1)
h, w = unpack('>HH', file.read(4))
return { "w": w, "h": h }
except Exception:
#TODO: log error
return None
# Returns what params to call im.FactorScale with for cg button size # Returns what params to call im.FactorScale with for cg button size
# Basically the delta diff dimensions # Basically the delta diff dimensions
# (w: int; h: int) -> { x: float; y: float } # (w: int; h: int) -> { x: float; y: float }