Added some comments.

This commit is contained in:
mibi88 2024-07-31 18:53:46 +02:00
parent e801bb5632
commit a1811da9f1

View file

@ -47,7 +47,10 @@ def convert(input: str, output: str, params: dict, target):
convert_dialog(input, output, params, target)
return 0
def convert_map(input, output, params, target):
def convert_map(input: str, output: str, params: dict, target):
"""
Convert a map.
"""
if VERBOSE: print(f"INFO: Converting map {input} -> {output}")
input_map = Map(input)
dialog_file = ""
@ -75,6 +78,7 @@ def convert_map(input, output, params, target):
dialog_file = input_map.get_property("dialogFile")
if VERBOSE: print(f"INFO: Dialog file: {dialog_file}.")
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the dialog file.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -86,6 +90,7 @@ def convert_map(input, output, params, target):
map_y = int(input_map.get_property("mapY"))
if VERBOSE: print(f"INFO: Map position: ({map_x}, {map_y}).")
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the map position.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -99,6 +104,7 @@ def convert_map(input, output, params, target):
for i in dialog_data["dialogs"]:
dialog_ids.append(i["ID"])
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get informations about dialogs.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -108,6 +114,7 @@ def convert_map(input, output, params, target):
if VERBOSE: print("INFO: Getting the outdoor tileset")
outdoor_tileset = input_map.get_tileset_by_firstgid(1)
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the outdoor tileset.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -117,6 +124,7 @@ def convert_map(input, output, params, target):
if VERBOSE: print("INFO: Getting the walkable tileset")
walkable_tileset = input_map.get_tileset_by_firstgid(409)
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the walkable tileset.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -136,6 +144,7 @@ def convert_map(input, output, params, target):
raise Exception("Bad layer size!")
if VERBOSE: print("INFO: Layer data has the right size.")
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the background layer.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -151,6 +160,7 @@ def convert_map(input, output, params, target):
raise Exception("Bad layer size!")
if VERBOSE: print("INFO: Layer data has the right size.")
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the foreground layer.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -166,6 +176,7 @@ def convert_map(input, output, params, target):
raise Exception("Bad layer size!")
if VERBOSE: print("INFO: Layer data has the right size.")
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the walkable layer.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -216,6 +227,7 @@ def convert_map(input, output, params, target):
}
signs[object.id] = data
except Exception as e:
# Show a simple error message on failure.
sys.stderr.write(f"ERROR: Failed to get the extra data.\n"
+ f" Error message: {e}\n")
sys.exit(1)
@ -230,6 +242,7 @@ def convert_map(input, output, params, target):
tileset_name = os.path.splitext(os.path.basename(outdoor_tileset.source))[0]
map_struct += fxconv.ref(f"img_{tileset_name}")
# Store the walkable layer
walkable_data = bytes()
for i in walkable_layer:
if i < 0: i = 0
@ -240,6 +253,7 @@ def convert_map(input, output, params, target):
map_struct += fxconv.u32(len(npcs))
npc_struct = fxconv.Structure()
for i in npcs.values():
# Convert currentpos to a fixed point value.
npc_struct += fxconv.u32((i["position"][0]+i["path"][0])<<PRECISION)
npc_struct += fxconv.u32((i["position"][1]+i["path"][1])<<PRECISION)
npc_struct += fxconv.u32(i["position"][0])
@ -274,6 +288,7 @@ def convert_map(input, output, params, target):
map_struct += fxconv.u32(len(signs))
sign_struct = fxconv.Structure()
for i in signs.values():
# Create a sign struct for each sign.
sign_struct += fxconv.u32(i["position"][0])
sign_struct += fxconv.u32(i["position"][1])
sign_struct += fxconv.u32(i["icon"])
@ -286,14 +301,18 @@ def convert_map(input, output, params, target):
map_struct += fxconv.ptr(bytes())
map_struct += fxconv.u32(dialog_num)
# Get the name of the dialog file and create a reference to it: it is built
# separately.
dialog_name = os.path.splitext(os.path.basename(dialog_file))[0]
map_struct += fxconv.ref(f"_{dialog_name}")
# Store the background layer
background_data = bytes()
for i in background_layer:
background_data += fxconv.u16(i)
map_struct += fxconv.ptr(background_data)
# Store the foreground layer
foreground_data = bytes()
for i in foreground_layer:
foreground_data += fxconv.u16(i)
@ -303,8 +322,13 @@ def convert_map(input, output, params, target):
name = os.path.splitext(os.path.basename(input))[0]
fxconv.elf(map_struct, output, f"_{name}", **target)
def convert_dialog(input, output, params, target):
def convert_dialog(input: str, output: str, params: dict, target):
"""
Convert a JSON dialog file.
"""
if VERBOSE: print(f"INFO: Converting dialog file {input} -> {output}")
# Load the JSON dialog file.
dialog_data = None
try:
with open(input, "r") as file:
@ -313,9 +337,12 @@ def convert_dialog(input, output, params, target):
sys.stderr.write(f"ERROR: Failed parse json.\n"
+ f" Error message: {e}\n")
sys.exit(1)
# Create the dialog struct
dialog_struct = fxconv.Structure()
try:
for i in dialog_data["dialogs"]:
# Create a dialog structure for each dialog.
dialog_id = i["ID"]
dialog_struct += fxconv.u32(dialog_id)
dialog_struct += fxconv.string(i["dialog"])
@ -330,6 +357,7 @@ def convert_dialog(input, output, params, target):
name = os.path.splitext(os.path.basename(input))[0]
fxconv.elf(dialog_struct, output, f"__{name}", **target)
except Exception as e:
# Show an error message if the conversion fails.
sys.stderr.write(f"ERROR: Failed convert dialogs.\n"
+ f" Error message: {e}\n")
sys.exit(1)