Last active
February 21, 2023 20:53
-
-
Save DouglasAntunes/f44dc1298444a2f4f79bad1aa295507e 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 CHANGE NAME INSIDE MODEL FILE LICENSE: MIT | |
################################################################################# | |
Folder Structure: | |
rootFolder/ | |
├── batchChangeNameInsideModelFile.py | |
└── ... (input for models in .dff format) | |
WARNING: This script OVERWRITES the model.dff, changing original model name | |
inside the file to the current filename of the file. | |
################################################################################# | |
Created by DouglasAntunes(BlackBurnBR007) https://github.com/DouglasAntunes | |
Find Updates of this script on: | |
https://gist.github.com/DouglasAntunes/f44dc1298444a2f4f79bad1aa295507e | |
##############################################################################""" | |
from os import listdir | |
from os.path import isfile, join, dirname, splitext | |
from re import match, IGNORECASE | |
fileList = [f for f in listdir(join(dirname(__file__))) if isfile(join(dirname(__file__), f))] | |
for file in filter(lambda f: match(r".dff", splitext(f)[1], IGNORECASE), fileList): | |
with open(file, mode="rb") as dffFile: | |
# print("Opened file", file) | |
dffFile.seek(0, 2) | |
numOfBytes = dffFile.tell() | |
# Reversed to optimize search on the end of the file | |
for i in reversed(range(numOfBytes)): | |
dffFile.seek(i) | |
sample = dffFile.read(4) | |
# Search for 'COL3' | |
if sample == b"\x43\x4F\x4C\x33": | |
# print(">Header found at", str(i)) | |
startStringPos = i + 8 # current pos + 4 of the header + 4 bytes to start on string | |
endStringPos = startStringPos + 20 # string start pos + 20 bytes string size | |
# name to check/change | |
name = splitext(file)[0].lower() | |
# Max string size = 20 bytes | |
if len(name) > 20: | |
# print("The filename is greater than 20 characters, truncating...") | |
name = name[:20] | |
# Seek to start string pos to read the current name | |
dffFile.seek(startStringPos) | |
currentNameOnFile = dffFile.read(20).decode('ascii') | |
# if isn't the same name | |
if currentNameOnFile.rstrip() != name: | |
print("Processing {}...".format(file)) | |
# Seek back to the start | |
dffFile.seek(0) | |
# Read to variable the start until the init of string pos | |
data = dffFile.read(startStringPos) | |
# Append the dff name in place | |
data += name.encode('ascii') | |
# Append 00 to end of string if len < 20 | |
if len(name) < 20: | |
for j in range(20 - len(name)): | |
data += b"\x00" | |
# Seek pass the original name | |
dffFile.seek(endStringPos) | |
# Append the end of the original file | |
data += dffFile.read(numOfBytes - endStringPos) | |
# Overwrite the file | |
with open(file, mode="wb") as moddedDffFile: | |
moddedDffFile.write(data) | |
else: | |
print("Ignoring {}, same name of file inside the COL part".format(file)) | |
# Stop Search on the current file | |
break | |
# END of if | |
# END of for | |
# END dffFile | |
# END For | |
# Wait for a key press to close window. | |
input("Done. Press any key to continue...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment