Last active
February 21, 2023 20:53
-
-
Save DouglasAntunes/41ae9e0b8a50e17ed2dcbed29b5bef71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""############################################################################## | |
BATCH REMOVE COL3 FROM MODEL & APPEND COL1 FROM FOLDER LICENSE: MIT | |
################################################################################# | |
Folder Structure: | |
rootFolder/ | |
├── colpatch/ | |
│ ├── ... (input for .col files (on model.col or model_col.col) | |
├── batchRemoveCol3FromModel&AppendCol1FromFolder.py | |
└── ... (input for models in .dff format) | |
WARNING: This script OVERWRITES the model.dff, removing original | |
COL3 collision. | |
################################################################################# | |
Created by DouglasAntunes(BlackBurnBR007) https://github.com/DouglasAntunes | |
Find Updates of this script on: | |
https://gist.github.com/DouglasAntunes/41ae9e0b8a50e17ed2dcbed29b5bef71 | |
##############################################################################""" | |
from os import listdir | |
from os.path import isfile, join, dirname, splitext | |
from re import match, IGNORECASE | |
def generate_file_list(path): | |
return [f for f in listdir(path) if isfile(join(path, f))] | |
def main(): | |
for file in filter(lambda f: match(r".dff", splitext(f)[1], IGNORECASE), generate_file_list(dirname(__file__))): | |
with open(file, mode="rb") as dffFile: | |
found_header = False | |
#print("Opened file", file) | |
dffFile.seek(0, 2) | |
num_of_bytes = dffFile.tell() | |
# Reversed to optimize search on the end of the file | |
for i in reversed(range(num_of_bytes)): | |
dffFile.seek(i) | |
sample = dffFile.read(4) | |
# Search for 'COL3' | |
if sample == b"\x43\x4F\x4C\x33": | |
#print(">Header found at", str(i)) | |
found_header = True | |
# Seek back to start | |
dffFile.seek(0) | |
# Read to variable | |
data = dffFile.read(i) | |
# Try to find the col file | |
col_file_path = [ | |
f for f in generate_file_list(join(dirname(__file__), "colpatch")) | |
if match(r"{}(?:_col)?.col".format(splitext(file)[0]), f, IGNORECASE) | |
] | |
if len(col_file_path) > 1: | |
print(">Error: More than 1 match of col was found to", file) | |
break | |
elif len(col_file_path) == 0: | |
print(">Error: No .col file was found to", file) | |
break | |
# If file was found | |
with open(join("colpatch", col_file_path[0]), mode="rb") as col_file: | |
col_file.seek(0, 2) | |
num_of_bytes2 = col_file.tell() | |
data2 = col_file.read(num_of_bytes2) | |
# Overwrites the file removing the COL3 part | |
with open(file, mode="wb") as modded_dff_file: | |
# Writes dff without COL3 | |
modded_dff_file.write(data) | |
# Append col1 to the end of file | |
modded_dff_file.write(data2) | |
print(">Fixed & Appended COL1 to \"{}\"".format(file)) | |
# Stop Search on the current file | |
break | |
# END of if | |
# END of for | |
# If the search was not successful on the current file | |
if not found_header: | |
print(">Scanned \"{}\" and doesn't have a COL3 header".format(file)) | |
# Wait for a key press to close window. | |
input("Done. Press any key to continue...") | |
# END main | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment