mirror of
https://git.cavemanon.xyz/Cavemanon/SnootGame.git
synced 2025-01-22 17:26:20 +01:00
loadGallery SDK Fix & seen_image
This commit is contained in:
parent
f332a988c2
commit
8117fa1981
1 changed files with 8 additions and 59 deletions
|
@ -83,28 +83,23 @@ init python:
|
|||
# Appends extra spaces at the end
|
||||
# () -> None
|
||||
def loadGallery():
|
||||
from os import listdir, getcwd
|
||||
from os.path import isfile, join
|
||||
|
||||
cgPath = "images/cgs/"
|
||||
workingDirPath = getcwd().replace("\\", "/")
|
||||
cgDirPath = workingDirPath + "/game/" + cgPath
|
||||
|
||||
# Reset gallery
|
||||
galleryItems = []
|
||||
g = Gallery()
|
||||
g.transition = dissolve
|
||||
|
||||
list_img = renpy.list_images()
|
||||
|
||||
# Add each image to the gallery
|
||||
for cgFile in listdir(cgDirPath):
|
||||
filePath = join(cgDirPath, cgFile)
|
||||
if isfile(filePath):
|
||||
ext = cgFile[-3:].lower()
|
||||
if (ext in ACCEPTED_EXTENSIONS):
|
||||
attr = getImageFileAttributes(filePath)
|
||||
fname = str(cgFile[0:-4])
|
||||
unlocked = fname in persistent.cggallery
|
||||
addGalleryItem(fname, ext, attr["w"], attr["h"], unlocked)
|
||||
for str in list_img:
|
||||
_str = cgPath+str+"."+ACCEPTED_EXTENSIONS[0]
|
||||
if renpy.loadable(_str): #brute force
|
||||
unlocked = renpy.seen_image(str)
|
||||
image = renpy.image_size(Image(_str))
|
||||
addGalleryItem(str, ACCEPTED_EXTENSIONS[0], image[0], image[1], unlocked)
|
||||
|
||||
# Add empty items to fill grid after last cg button
|
||||
extraSpaces = GALLERY_COLS - (len(galleryItems) % GALLERY_COLS)
|
||||
|
@ -115,52 +110,6 @@ init python:
|
|||
"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
|
||||
# Basically the delta diff dimensions
|
||||
# (w: int; h: int) -> { x: float; y: float }
|
||||
|
|
Loading…
Reference in a new issue