Skip to content

Instantly share code, notes, and snippets.

@Lemonszz
Created May 26, 2023 03:31
Show Gist options
  • Save Lemonszz/ae265aa88232ce246bbb293456211f8e to your computer and use it in GitHub Desktop.
Save Lemonszz/ae265aa88232ce246bbb293456211f8e to your computer and use it in GitHub Desktop.
#Make a backup before running this
#No liability etc etc
#xoxoxo
import os
import json
def add_relative_path_to_json(modid, directory, base_directory=None):
if base_directory is None:
base_directory = directory
for path in os.listdir(directory):
full_path = os.path.join(directory, path)
if os.path.isdir(full_path):
add_relative_path_to_json(modid, full_path, base_directory)
else:
if os.path.splitext(full_path)[1] == ".json":
with open(full_path, 'r') as f:
data = json.load(f)
if 'random_sequence' not in data:
# Set sequence to the relative file path without extension
sequence = modid + ":" + os.path.splitext(os.path.relpath(full_path, base_directory))[0].replace("\\", "/")
data['random_sequence'] = sequence
# Write the modified json back to the file
with open(full_path, 'w') as outfile:
json.dump(data, outfile, indent=1)
# Ask user to input the directory path
modid = input("Mod ID: ")
directory_path = input("data/modid/loot_tables directory path: ")
add_relative_path_to_json(modid, directory_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment