mirror of
https://github.com/TheCommsChannel/TC2-BBS-mesh.git
synced 2025-12-17 08:44:15 -05:00
Added a repair tool for incorrectly formatted line mapping.
This commit is contained in:
parent
775ab0c688
commit
9f2b60b9f2
2 changed files with 187 additions and 125 deletions
62
game_line_offset_fix.py
Normal file
62
game_line_offset_fix.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# This will fix a gamefile that was written with a title="Title" on line 1, but was not offset
|
||||
# to account for that. If title="Title" is used on line 1, that line should then be treated
|
||||
# as line 0. The gamefile processor assumes line 1 is the first line that doesn't start with title=
|
||||
# This script will subtract one from every line number mapping in the gamefile.
|
||||
|
||||
# Alternatively, you could add a "dummy line" to the second storyline shifting the rest of
|
||||
# the file down by one line.
|
||||
|
||||
import re
|
||||
import os
|
||||
|
||||
def list_game_files(directory):
|
||||
return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
||||
|
||||
def adjust_numbers_in_file(input_file, output_file):
|
||||
with open(input_file, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
updated_lines = []
|
||||
for line in lines:
|
||||
# Preserve title line
|
||||
if line.startswith("title="):
|
||||
updated_lines.append(line)
|
||||
continue
|
||||
|
||||
# Find numbers and subtract 1 from each
|
||||
def adjust_number(match):
|
||||
return f", {match.group(1)}, {int(match.group(2)) - 1}"
|
||||
|
||||
updated_line = re.sub(r",\s*([^,]+)\s*,\s*(\d+)", adjust_number, line)
|
||||
updated_lines.append(updated_line)
|
||||
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
f.writelines(updated_lines)
|
||||
|
||||
def main():
|
||||
games_dir = "./games"
|
||||
game_files = list_game_files(games_dir)
|
||||
|
||||
if not game_files:
|
||||
print("No files found in the ./games directory.")
|
||||
return
|
||||
|
||||
print("Select a file to process:")
|
||||
for idx, filename in enumerate(game_files, start=1):
|
||||
print(f"{idx}: {filename}")
|
||||
|
||||
choice = int(input("Enter the number of the file: ")) - 1
|
||||
if choice < 0 or choice >= len(game_files):
|
||||
print("Invalid selection.")
|
||||
return
|
||||
|
||||
input_filename = game_files[choice]
|
||||
input_filepath = os.path.join(games_dir, input_filename)
|
||||
output_filename = f"{os.path.splitext(input_filename)[0]}-linefix{os.path.splitext(input_filename)[1]}"
|
||||
output_filepath = os.path.join(games_dir, output_filename)
|
||||
|
||||
adjust_numbers_in_file(input_filepath, output_filepath)
|
||||
print(f"Processed file saved as: {output_filename}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue