add warning and place the relevent var on top

(posted from gitea)
This commit is contained in:
whosit 2024-02-01 10:36:48 +00:00
parent 6221c44836
commit 4c116ee89e

View File

@ -1,258 +1,266 @@
WRITE_LUA = True # WARNING: running this file will directly modify .lua and .b3d files
# without creating backups. Running it again will _accumulate_ shift
# in b3d files (if there's any change in lua part).
import os.path
import re # where the origin should be put between top and bottom of the box
import copy RATIO = 2/3
# return new corners
def fix_box_coords(p1, p2): WRITE_LUA = True
assert(p1[1] < p2[1])
p1 = copy.copy(p1) import os.path
p2 = copy.copy(p2) import re
height = p2[1] - p1[1] import copy
ratio = 2/3
#shift = p1 - ()
shift = -height * ratio - p1[1] # return new corners
shift = round_to_base(shift, prec=2, base=0.05) def fix_box_coords(p1, p2):
p1[1] += shift assert(p1[1] < p2[1])
p2[1] += shift p1 = copy.copy(p1)
# assert(p2[1] - p1[1] == height) # lol, floats p2 = copy.copy(p2)
p1 = list(map(make_float_pretty, p1)) height = p2[1] - p1[1]
p2 = list(map(make_float_pretty, p2)) ratio = RATIO
#shift = p1 - ()
# print("diff:", height - (p2[1] - p1[1])) shift = -height * ratio - p1[1]
return p1, p2 shift = round_to_base(shift, prec=2, base=0.05)
p1[1] += shift
p2[1] += shift
# assert(p2[1] - p1[1] == height) # lol, floats
p1 = list(map(make_float_pretty, p1))
def vector_sub(a,b): p2 = list(map(make_float_pretty, p2))
return [
a[0]-b[0], # print("diff:", height - (p2[1] - p1[1]))
a[1]-b[1], return p1, p2
a[2]-b[2],
]
def round_to_base(x, prec=2, base=0.05):
return round(base * round(x/base), prec) def vector_sub(a,b):
return [
def make_float_pretty(n): a[0]-b[0],
n = float(f'{n:.4f}') a[1]-b[1],
return n a[2]-b[2],
]
def replace_func(p1, p2):
def replace(match): def round_to_base(x, prec=2, base=0.05):
if match.group("var") == b"p1": return round(base * round(x/base), prec)
r = f"p1 = {{x = {p1[0]}, y = {p1[1]}, z = {p1[2]}}}"
print(r) def make_float_pretty(n):
return match.group("spaces") + r.encode('utf-8') n = float(f'{n:.4f}')
elif match.group("var") == b"p2": return n
r = f"p2 = {{x = {p2[0]}, y = {p2[1]}, z = {p2[2]}}}"
print(r) def replace_func(p1, p2):
return match.group("spaces") + r.encode('utf-8') def replace(match):
else: if match.group("var") == b"p1":
raise Exception("this is wrong") r = f"p1 = {{x = {p1[0]}, y = {p1[1]}, z = {p1[2]}}}"
return replace print(r)
return match.group("spaces") + r.encode('utf-8')
elif match.group("var") == b"p2":
POINT_PAT = rb""" r = f"p2 = {{x = {p2[0]}, y = {p2[1]}, z = {p2[2]}}}"
(?P<spaces>\s+)(?P<var>p[12])\s*=\s*\{ print(r)
\s* x \s* = \s* (?P<x>[+-]?(\d+(\.\d*)?|\.\d+))\s*, return match.group("spaces") + r.encode('utf-8')
\s* y \s* = \s* (?P<y>[+-]?(\d+(\.\d*)?|\.\d+))\s*, else:
\s* z \s* = \s* (?P<z>[+-]?(\d+(\.\d*)?|\.\d+))\s* raise Exception("this is wrong")
\} return replace
"""
MESH_PAT = rb""" POINT_PAT = rb"""
\s+mesh\s*=\s* (?P<spaces>\s+)(?P<var>p[12])\s*=\s*\{
(?P<quote>['"])(?P<name>[^'"]+)(?P=quote) \s* x \s* = \s* (?P<x>[+-]?(\d+(\.\d*)?|\.\d+))\s*,
""" \s* y \s* = \s* (?P<y>[+-]?(\d+(\.\d*)?|\.\d+))\s*,
\s* z \s* = \s* (?P<z>[+-]?(\d+(\.\d*)?|\.\d+))\s*
def find_and_change_collisionbox(file): \}
print(f"******* {file} ********") """
with open(file, 'rb+') as f:
p1 = [0,0,0] MESH_PAT = rb"""
p2 = [0,0,0] \s+mesh\s*=\s*
(?P<quote>['"])(?P<name>[^'"]+)(?P=quote)
text = f.read() """
# first pass def find_and_change_collisionbox(file):
iter = re.finditer(POINT_PAT, text, re.VERBOSE) print(f"******* {file} ********")
match = next(iter) with open(file, 'rb+') as f:
print(match.groupdict()) p1 = [0,0,0]
assert(match.group("var") == b"p1") p2 = [0,0,0]
p1[0] = float(match.group("x"))
p1[1] = float(match.group("y")) text = f.read()
p1[2] = float(match.group("z"))
# first pass
match = next(iter) iter = re.finditer(POINT_PAT, text, re.VERBOSE)
print(match.groupdict()) match = next(iter)
assert(match.group("var") == b"p2") print(match.groupdict())
p2[0] = float(match.group("x")) assert(match.group("var") == b"p1")
p2[1] = float(match.group("y")) p1[0] = float(match.group("x"))
p2[2] = float(match.group("z")) p1[1] = float(match.group("y"))
p1[2] = float(match.group("z"))
try:
# make sure there are no more matches match = next(iter)
match = next(iter) print(match.groupdict())
assert(False) # could be selectionbox! assert(match.group("var") == b"p2")
except StopIteration: p2[0] = float(match.group("x"))
pass p2[1] = float(match.group("y"))
p2[2] = float(match.group("z"))
# do shifting calc
try:
print(p1, p2) # make sure there are no more matches
new_p1, new_p2 = fix_box_coords(p1, p2) match = next(iter)
print(new_p1, new_p2) assert(False) # could be selectionbox!
shift = vector_sub(new_p1, p1) except StopIteration:
print(shift) pass
# second pass # do shifting calc
fixed_text = re.sub(POINT_PAT, replace_func(new_p1, new_p2), text, flags=re.VERBOSE)
print(p1, p2)
# print(fixed_text) new_p1, new_p2 = fix_box_coords(p1, p2)
if WRITE_LUA: print(new_p1, new_p2)
f.seek(0) shift = vector_sub(new_p1, p1)
f.write(fixed_text) print(shift)
f.truncate()
# second pass
mesh_name = re.search(MESH_PAT, text, flags=re.VERBOSE) fixed_text = re.sub(POINT_PAT, replace_func(new_p1, new_p2), text, flags=re.VERBOSE)
if mesh_name:
mesh_name = mesh_name.group("name").decode('utf-8') # print(fixed_text)
else: if WRITE_LUA:
mesh_name = os.path.basename(file) f.seek(0)
m = re.match("(?P<name>[a-z]+)_mobkit.lua", mesh_name) f.write(fixed_text)
mesh_name = "petz_" + m.group("name") + ".b3d" f.truncate()
#raise mesh_name
assert(mesh_name) mesh_name = re.search(MESH_PAT, text, flags=re.VERBOSE)
print(mesh_name) if mesh_name:
mesh_name = mesh_name.group("name").decode('utf-8')
shift_b3d(os.path.join("petz", "models", mesh_name), shift) else:
mesh_name = os.path.basename(file)
############################################################## m = re.match("(?P<name>[a-z]+)_mobkit.lua", mesh_name)
mesh_name = "petz_" + m.group("name") + ".b3d"
import struct #raise mesh_name
assert(mesh_name)
def shift_b3d(filename, shift): print(mesh_name)
with open(filename, "rb+") as f:
data = f.read() shift_b3d(os.path.join("petz", "models", mesh_name), shift)
if not data.startswith(b"BB3D"):
raise Exception("Not a b3d file?") ##############################################################
offset = data.find(b"NODE")
print("%x" % offset) import struct
offset += 8 # skip NODE and 4 bytes after (id? len?)
name_offset = offset def shift_b3d(filename, shift):
offset = data.find(b'\0', name_offset) with open(filename, "rb+") as f:
print("node name:", data[name_offset:offset]) data = f.read()
offset += 1 # terminating \0 if not data.startswith(b"BB3D"):
tx, = struct.unpack_from("f", data, offset) raise Exception("Not a b3d file?")
ty, = struct.unpack_from("f", data, offset+4) offset = data.find(b"NODE")
tz, = struct.unpack_from("f", data, offset+8) print("%x" % offset)
print(tx, ty, tz) offset += 8 # skip NODE and 4 bytes after (id? len?)
out = bytearray(data) name_offset = offset
struct.pack_into("f", out, offset, tx + shift[0]) offset = data.find(b'\0', name_offset)
struct.pack_into("f", out, offset+4, ty + shift[1]) print("node name:", data[name_offset:offset])
struct.pack_into("f", out, offset+8, tz + shift[2]) offset += 1 # terminating \0
new_y = struct.unpack_from("f", out, offset+4) tx, = struct.unpack_from("f", data, offset)
print(f"{new_y}") ty, = struct.unpack_from("f", data, offset+4)
f.seek(0) tz, = struct.unpack_from("f", data, offset+8)
f.write(out) print(tx, ty, tz)
f.truncate() out = bytearray(data)
struct.pack_into("f", out, offset, tx + shift[0])
############################################################## struct.pack_into("f", out, offset+4, ty + shift[1])
struct.pack_into("f", out, offset+8, tz + shift[2])
new_y = struct.unpack_from("f", out, offset+4)
def check_if_petz_dir(): print(f"{new_y}")
def ensure_file(*names): f.seek(0)
fname = os.path.join(*names) f.write(out)
assert(os.path.isfile(fname)) f.truncate()
def ensure_dir(*names):
fname = os.path.join(*names) ##############################################################
assert(os.path.isdir(fname))
ensure_dir("petz") def check_if_petz_dir():
ensure_dir("petz", "petz") def ensure_file(*names):
ensure_dir("petz", "models") fname = os.path.join(*names)
ensure_file("petz", "petz.conf") assert(os.path.isfile(fname))
def ensure_dir(*names):
print("* Looks like petz dir") fname = os.path.join(*names)
assert(os.path.isdir(fname))
if __name__ == "__main__": ensure_dir("petz")
ensure_dir("petz", "petz")
check_if_petz_dir() ensure_dir("petz", "models")
ensure_file("petz", "petz.conf")
# TODO: do this for passed args only
print("* Looks like petz dir")
# filepath = os.path.join("petz", "petz", "grizzly_mobkit.lua")
# filepath = os.path.join("petz", "petz", "bunny_mobkit.lua") if __name__ == "__main__":
# filepath = os.path.join("petz", "petz", "foxy_mobkit.lua")
# import sys check_if_petz_dir()
# filepath = sys.arv[1]
# files = os.listdir(os.path.join("petz","petz")) # TODO: do this for passed args only
# for f in files:
# print(f'"{f}",')
# filepath = os.path.join("petz", "petz", "grizzly_mobkit.lua")
broken = [ # filepath = os.path.join("petz", "petz", "bunny_mobkit.lua")
# "ant_mobkit.lua", # does not work # filepath = os.path.join("petz", "petz", "foxy_mobkit.lua")
"bat_mobkit.lua", # import sys
"beaver_mobkit.lua", # filepath = sys.arv[1]
"bee_mobkit.lua", # files = os.listdir(os.path.join("petz","petz"))
"bunny_mobkit.lua", # for f in files:
"butterfly_mobkit.lua", # print(f'"{f}",')
"calf_mobkit.lua",
"camel_mobkit.lua", broken = [
# "chicken_mobkit.lua", # "ant_mobkit.lua", # does not work
# "chimp_mobkit.lua", "bat_mobkit.lua",
"clownfish_mobkit.lua", "beaver_mobkit.lua",
"dolphin_mobkit.lua", "bee_mobkit.lua",
"ducky_mobkit.lua", "bunny_mobkit.lua",
"elephant_mobkit.lua", "butterfly_mobkit.lua",
"flamingo_mobkit.lua", "calf_mobkit.lua",
"foxy_mobkit.lua", "camel_mobkit.lua",
"frog_mobkit.lua", # "chicken_mobkit.lua",
"gecko_mobkit.lua", # "chimp_mobkit.lua",
"goat_mobkit.lua", "clownfish_mobkit.lua",
"grizzly_mobkit.lua", "dolphin_mobkit.lua",
"hamster_mobkit.lua", "ducky_mobkit.lua",
"kitty_mobkit.lua", "elephant_mobkit.lua",
"lamb_mobkit.lua", "flamingo_mobkit.lua",
"leopard_mobkit.lua", "foxy_mobkit.lua",
"lion_mobkit.lua", "frog_mobkit.lua",
"moth_mobkit.lua", "gecko_mobkit.lua",
"mr_pumpkin_mobkit.lua", "goat_mobkit.lua",
"panda_mobkit.lua", "grizzly_mobkit.lua",
"parrot_mobkit.lua", "hamster_mobkit.lua",
"penguin_mobkit.lua", "kitty_mobkit.lua",
"pigeon_mobkit.lua", "lamb_mobkit.lua",
"piggy_mobkit.lua", "leopard_mobkit.lua",
"polar_bear_mobkit.lua", "lion_mobkit.lua",
"pony_mobkit.lua", "moth_mobkit.lua",
"puppy_mobkit.lua", "mr_pumpkin_mobkit.lua",
"rat_mobkit.lua", "panda_mobkit.lua",
"santa_killer_mobkit.lua", "parrot_mobkit.lua",
"silkworm_mobkit.lua", "penguin_mobkit.lua",
"squirrel_mobkit.lua", "pigeon_mobkit.lua",
"tarantula_mobkit.lua", "piggy_mobkit.lua",
"toucan_mobkit.lua", "polar_bear_mobkit.lua",
"tropicalfish_mobkit.lua", "pony_mobkit.lua",
"turtle_mobkit.lua", "puppy_mobkit.lua",
"wolf_mobkit.lua", "rat_mobkit.lua",
] "santa_killer_mobkit.lua",
"silkworm_mobkit.lua",
failed = [] "squirrel_mobkit.lua",
if True: "tarantula_mobkit.lua",
for name in broken: "toucan_mobkit.lua",
filepath = os.path.join("petz", "petz", name) "tropicalfish_mobkit.lua",
try: "turtle_mobkit.lua",
find_and_change_collisionbox(filepath) "wolf_mobkit.lua",
except Exception as e: ]
failed.append(name)
else: failed = []
name = "puppy_mobkit.lua" if True:
filepath = os.path.join("petz", "petz", name) for name in broken:
find_and_change_collisionbox(filepath) filepath = os.path.join("petz", "petz", name)
try:
find_and_change_collisionbox(filepath)
except Exception as e:
failed.append(name)
else:
name = "puppy_mobkit.lua"
filepath = os.path.join("petz", "petz", name)
find_and_change_collisionbox(filepath)
print(failed) print(failed)