commit 2fbeb623f0e406088cdf54c2e3ce93a8b713cbc9 Author: Fierelier Date: Tue Oct 1 12:54:24 2024 +0200 Initial commit diff --git a/ipl2json.py b/ipl2json.py new file mode 100644 index 0000000..4322983 --- /dev/null +++ b/ipl2json.py @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 +# GTA SA IPL to JSON parser, Python 3.2 (Windows 2000) or up +# Also compatible with Linux and others + +# Arguments: +# Example: ipl2json.py nodes14.ipl nodes14.json + +# You can get the ipl files from GTA3.IMG + +# Thanks to: https://gtamods.com/wiki/Paths_(GTA_SA) + +# -------------------------------------------------------------------------- + +# MIT License + +# Copyright (c) 2024 + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import sys +import os +import json + +def read(fh,tp): + signed = True + if tp[0] == "u": + tp = tp[1:] + signed = False + + if tp.startswith("int"): + tp = tp[3:] + tp = int(int(tp) / 8) + return int.from_bytes(fh.read(tp),byteorder="little",signed=signed) + + if tp != "float": + raise Exception("Invalid format: " +tp) + + return float.from_bytes(fh.read(4),byteorder="little",signed=signed) + +ipl = open(sys.argv[1],"rb") +# HEADER +output = {} +output["node_count"] = read(ipl,"uint32") +output["veh_node_count"] = read(ipl,"uint32") +output["ped_node_count"] = read(ipl,"uint32") +output["navi_node_count"] = read(ipl,"uint32") +output["link_count"] = read(ipl,"uint32") + +# Some helpers for seeking file +offset_node_links = 20 + (28 * output["node_count"]) + (14 * output["navi_node_count"]) +offset_navi_links = offset_node_links + (4 * output["link_count"]) + 768 +offset_link_lengths = offset_navi_links + (2 * output["link_count"]) +offset_path_intersection = offset_link_lengths + output["link_count"] + +def access_bit(data, num): + base = int(num // 8) + shift = int(num % 8) + return (data[base] >> shift) & 0x1 + +# SECTION 1 - PATH NODES +output["path_nodes"] = {} +node_index = 0 +while node_index < output["node_count"]: + node = {} + node["mem_address"] = read(ipl,"uint32") # useless (unused) + node["unknown0"] = read(ipl,"uint32") # useless + node["pos"] = {} + node["pos"]["x"] = read(ipl,"int16") + node["pos"]["y"] = read(ipl,"int16") + node["pos"]["z"] = read(ipl,"int16") + node["unknown1"] = read(ipl,"int16") # useless + node["link_id"] = read(ipl,"uint16") # useless (links are stored in node) + node["area_id"] = read(ipl,"uint16") # useless-ish (in file name) + node["node_id"] = read(ipl,"uint16") # useless (nodes are indiced by their ID) + node["path_width"] = read(ipl,"uint8") + node["node_type"] = read(ipl,"uint8") + node["flags"] = [] + flags = ipl.read(4) + for i in range(32): node["flags"].append(access_bit(flags,i)) + node["link_count"] = node["flags"][0] + node["flags"][1] + node["flags"][2] + node["flags"][3] + + # Reading links (section 3) + node["links"] = [] + link_i = 0 + oldoffset = ipl.tell() + offset = offset_node_links + (4 * node["link_id"]) + ipl.seek(offset) + while link_i < node["link_count"]: + link = {} + link["area_id"] = read(ipl,"uint16") + link["node_id"] = read(ipl,"uint16") + link["navi_area_id"] = 0 + link["navi_node_id"] = 0 + link["length"] = 0 + link["flags"] = [] + node["links"].append(link) + link_i += 1 + ipl.seek(oldoffset) + + # Reading navi links (section 5) + link_i = 0 + oldoffset = ipl.tell() + offset = offset_navi_links + (2 * node["link_id"]) + ipl.seek(offset) + while link_i < node["link_count"]: + data = ipl.read(2) + node_id = "" + area_id = "" + for i in range(10): node_id = str(access_bit(data,i)) + node_id + for i in range(10,16): area_id = str(access_bit(data,i)) + area_id + node["links"][link_i]["navi_node_id"] = int(node_id,2) + node["links"][link_i]["navi_area_id"] = int(area_id,2) + link_i += 1 + ipl.seek(oldoffset) + + # Reading link lengths (section 6) + link_i = 0 + oldoffset = ipl.tell() + offset = offset_link_lengths + node["link_id"] + ipl.seek(offset) + while link_i < node["link_count"]: + node["links"][link_i]["length"] = read(ipl,"uint8") + link_i += 1 + ipl.seek(oldoffset) + + # Reading path intersection flags (section 7) + link_i = 0 + oldoffset = ipl.tell() + offset = offset_path_intersection + (node["link_id"] * 2) + ipl.seek(offset) + while link_i < node["link_count"]: + node["links"][link_i]["flags"].append(read(ipl,"uint8")) + node["links"][link_i]["flags"].append(read(ipl,"uint8")) + link_i += 1 + ipl.seek(oldoffset) + + output["path_nodes"][node["node_id"]] = node + node_index += 1 + +# SECTION 2 - NAVI NODES +output["navi_nodes"] = {} +node_index = 0 +while node_index < output["navi_node_count"]: + node = {} + node["pos"] = {} + node["pos"]["x"] = read(ipl,"int16") + node["pos"]["y"] = read(ipl,"int16") + node["area_id"] = read(ipl,"uint16") # useless-ish (in file name) + node["node_id"] = read(ipl,"uint16") # useless (nodes are indiced by their ID) + node["dir"] = {} + node["dir"]["x"] = read(ipl,"int8") + node["dir"]["y"] = read(ipl,"int8") + node["flags"] = [] + flags = ipl.read(4) + for i in range(32): node["flags"].append(access_bit(flags,i)) + node["lane_left_count"] = node["flags"][8] + node["flags"][9] + node["flags"][10] + node["lane_right_count"] = node["flags"][11] + node["flags"][12] + node["flags"][13] + output["navi_nodes"][node["node_id"]] = node + + node_index += 1 + +ipl.close() +outf = open(sys.argv[2],"w") +outf.write(json.dumps(output,sort_keys=True,indent=2)) # Put indent to None instead of 2 for smaller output (less readable) +outf.close() \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..070bd57 --- /dev/null +++ b/readme.txt @@ -0,0 +1,9 @@ +GTA SA IPL to JSON parser, Python 3.2 (Windows 2000) or up +Also compatible with Linux and others + +Arguments: +Example: ipl2json.py nodes14.ipl nodes14.json + +You can get the ipl files from GTA3.IMG + +Thanks to: https://gtamods.com/wiki/Paths_(GTA_SA)