|
| 1 | +# Written by MediaMoots |
| 2 | + |
| 3 | +import sys |
| 4 | +import glob |
| 5 | +import PySimpleGUI as sg |
| 6 | +from totk_audio_classes import Bars |
| 7 | +from tkinter import filedialog as fd |
| 8 | + |
| 9 | +### pyinstaller bars_tool.py --onefile |
| 10 | + |
| 11 | +# Functions |
| 12 | +def show_options(): |
| 13 | + sg.theme('Black') |
| 14 | + event, values = sg.Window('TOTK BARS Tool', |
| 15 | + [[sg.Text('Options'), |
| 16 | + sg.Checkbox(default=False, text='Clear BARS?', size=(20, 3), key='ClrB')], |
| 17 | + [sg.Button('Start Tool'), sg.Button('Cancel')]]).read(close=True) |
| 18 | + |
| 19 | + if event == 'Start Tool': |
| 20 | + run_bars_tool(values['ClrB']) |
| 21 | + else: |
| 22 | + sys.exit() |
| 23 | + |
| 24 | +def run_bars_tool(is_clear_bars): |
| 25 | + bars_path, bwav_path = get_required_paths() |
| 26 | + |
| 27 | + bars = Bars(bars_path) |
| 28 | + |
| 29 | + if is_clear_bars: |
| 30 | + clear_bars(bars) |
| 31 | + |
| 32 | + add_or_replace_bars(bars, bwav_path) |
| 33 | + |
| 34 | + save_bars(bars) |
| 35 | + |
| 36 | +def get_required_paths(): |
| 37 | + # bars |
| 38 | + if len(sys.argv) > 1: |
| 39 | + bars_path = sys.argv[1] |
| 40 | + else: |
| 41 | + bars_path = fd.askopenfilename(title="Open BARS file to use...", filetypes=[ |
| 42 | + ('BARS Files', '*.bars')]) |
| 43 | + |
| 44 | + if bars_path == '': |
| 45 | + sys.exit() |
| 46 | + |
| 47 | + # bwav |
| 48 | + if len(sys.argv) > 2: |
| 49 | + bwav_path = sys.argv[2] |
| 50 | + else: |
| 51 | + bwav_path = fd.askdirectory( |
| 52 | + title="Open folder containing BWAV files...") |
| 53 | + |
| 54 | + if bwav_path == '': |
| 55 | + sys.exit() |
| 56 | + |
| 57 | + return bars_path, bwav_path |
| 58 | + |
| 59 | +def clear_bars(bars): |
| 60 | + bars.asset_count = 0 |
| 61 | + |
| 62 | + bars.metas.clear() |
| 63 | + bars.meta_offsets.clear() |
| 64 | + |
| 65 | + bars.assets.clear() |
| 66 | + bars.asset_offsets.clear() |
| 67 | + |
| 68 | + bars.crc_hashes.clear() |
| 69 | + |
| 70 | + bars.unknown = b'' |
| 71 | + |
| 72 | + bars.size = bars.get_size() |
| 73 | + |
| 74 | +def add_or_replace_bars(bars, bwav_path): |
| 75 | + for bwav in glob.glob(bwav_path + "/" + "*.bwav"): |
| 76 | + bars.add_or_replace_bwav(bwav, True) |
| 77 | + |
| 78 | +def save_bars(bars): |
| 79 | + if len(sys.argv) > 3: |
| 80 | + bars_out_path = sys.argv[3] |
| 81 | + else: |
| 82 | + bars_out_path = fd.asksaveasfilename( |
| 83 | + title="Select where to save the new BARS file...", filetypes=[('BARS File', '*.bars')]) |
| 84 | + |
| 85 | + if bars_out_path == '': |
| 86 | + sys.exit() |
| 87 | + |
| 88 | + if bars_out_path is not None: |
| 89 | + bars.write(bars_out_path) |
| 90 | + |
| 91 | +# Program Main |
| 92 | +if __name__ == '__main__': |
| 93 | + show_options() |
0 commit comments