import json
import os
import tkinter as tk
from tkinter import filedialog
import subprocess
import fnmatch
import time
def convert_to_asset_path(filepath):
assets_index = filepath.index('ASSETS')
new_filepath = filepath[assets_index:]
new_filepath = new_filepath.replace('\\', '/')
return new_filepath
def find_ogg_files(directory):
"""Recursively find all .ogg files in the given directory."""
ogg_files = []
for root, dirs, files in os.walk(directory):
for filename in fnmatch.filter(files, '*.ogg'):
ogg_files.append(os.path.join(root, filename))
return ogg_files
def get_filename(filepath):
_, filename = os.path.split(filepath)
return filename
def main():
GENERIC_ASSET_LIST_FILE = json_path_textbox.get("1.0", "end-1c")
ASSET_DIRECTORY = asset_textbox.get("1.0", "end-1c")
settings = {
"GENERIC_ASSET_LIST_PATH": GENERIC_ASSET_LIST_FILE,
"ASSET_PATH": ASSET_DIRECTORY
}
command = "p4 edit " + GENERIC_ASSET_LIST_FILE
subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
with open("settings.json", 'w') as f:
json.dump(settings, f, indent=4)
if not os.path.exists(GENERIC_ASSET_LIST_FILE):
button.configure(text="Failed, try again", bg='#f04747', fg='#ffffff')
return
if not os.path.exists(ASSET_DIRECTORY):
button.configure(text="Failed, try again", bg='#f04747', fg='#ffffff')
return
ogg_files = find_ogg_files(ASSET_DIRECTORY)
asset_id = [get_filename(ogg_files) for ogg_files in ogg_files]
asset_file_path = [convert_to_asset_path(fp) for fp in ogg_files]
assets = list(zip(asset_id, asset_file_path))
with open(GENERIC_ASSET_LIST_FILE, 'r') as f:
data = json.load(f)
new_data = []
for i in range(len(assets)):
new_data.append({
"GenericAssetListItem": {
"mId": assets[i][0],
"mPath": assets[i][1]
}
})
for section in data["streamDeltas"]:
if section["path"] == "mAssets":
section["current"] = new_data
break
os.remove(GENERIC_ASSET_LIST_FILE)
with open(GENERIC_ASSET_LIST_FILE, 'w') as f:
json.dump(data, f, indent=4)
button.configure(text="Done!", bg='#43b581', fg='#ffffff', state=tk.DISABLED)
root.after(2000, lambda: button.configure(text="Update", bg='#7289da', fg='#ffffff', state=tk.NORMAL))
def browse_file(text_box, title, default_dir=".", select_dir=False):
if select_dir == True:
filename = filedialog.askdirectory(initialdir=default_dir, title=title)
else:
filename = filedialog.askopenfilename(initialdir=default_dir, title=title, filetypes=(("JSON files", "*.json"),))
if filename == "":
return
text_box.delete("1.0", tk.END)
text_box.insert("1.0", filename)
if __name__ == '__main__':
if os.path.exists("settings.json"):
with open("settings.json", 'r') as f:
settings_json = json.load(f)
if "GENERIC_ASSET_LIST_PATH" in settings_json:
saved_json_path = settings_json["GENERIC_ASSET_LIST_PATH"]
saved_asset_path = settings_json["ASSET_PATH"]
else:
settings_json = {}
saved_json_path = ""
saved_asset_path = ""
saved_csv_path = ""
else:
settings_json = {}
saved_json_path = ""
saved_asset_path = ""
saved_csv_path = ""
root = tk.Tk()
root.title("Asset List Updater")
root.configure(background='#2f3136')
json_label = tk.Label(root, text="GenericAssetList JSON file path", bg='#2f3136', fg='#ffffff', font=('Arial', 12, 'bold'))
json_label.pack(side=tk.TOP, padx=10)
json_frame = tk.Frame(root, bg='#2f3136')
json_frame.pack(side=tk.TOP, padx=10, pady=5, fill=tk.X)
json_path_textbox = tk.Text(json_frame, height=2, width=45, bg='#36393f', fg='#ffffff', font=('Arial', 12), insertbackground="white")
json_path_textbox.pack(side=tk.LEFT)
json_path_textbox.insert("1.0", saved_json_path)
json_path_button = tk.Button(json_frame, text="Browse", bg='#7289da', fg='#ffffff',
borderwidth=0, relief='raised', highlightthickness=1,
highlightbackground='#7289da', height=2, width=10,
command=lambda: browse_file(json_path_textbox, "Select JSON file", default_dir="T:\\p4\\depot\\lol\\__MAIN__\\DevRoot\\PROPERTIES\\Data\\LCU\\EventAssets\\"))
json_path_button.pack(side=tk.RIGHT, padx=5)
asset_label = tk.Label(root, text="Audio asset directory", bg='#2f3136', fg='#ffffff', font=('Arial', 12, 'bold'))
asset_label.pack(side=tk.TOP, padx=10)
asset_frame = tk.Frame(root, bg='#2f3136')
asset_frame.pack(side=tk.TOP, padx=10, pady=5, fill=tk.X)
asset_textbox = tk.Text(asset_frame, height=2, width=45, bg='#36393f', fg='#ffffff', font=('Arial', 12), insertbackground="white")
asset_textbox.pack(side=tk.LEFT)
asset_textbox.insert("1.0", saved_asset_path)
asset_browse_button = tk.Button(asset_frame, text="Browse", bg='#7289da', fg='#ffffff',
borderwidth=0, relief='raised', highlightthickness=1,
highlightbackground='#7289da', height=2, width=10,
command=lambda: browse_file(asset_textbox, "Select asset directory",
default_dir="T:\\p4\\depot\\lol\\__MAIN__\\DevRoot\\ASSETS\\Events\\",
select_dir=True))
asset_browse_button.pack(side=tk.LEFT, padx=5)
button_frame = tk.Frame(root, bg='#2f3136')
button_frame.pack(side=tk.BOTTOM, pady=10)
button = tk.Button(button_frame, text="Update", bg='#7289da', fg='#ffffff',
borderwidth=0, relief='raised', highlightthickness=1,
highlightbackground='#7289da', height=2, width=20,
command=main)
button.pack(side=tk.BOTTOM, pady=10)
root.geometry("500x250")
root.resizable(width=False, height=False)
root.iconbitmap('myicon.ico')
root.mainloop()