44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import glob
|
|
import json
|
|
import os
|
|
import traceback
|
|
from .adx_parser import adx_file
|
|
|
|
def extract_loop_data_from_dir(dir_in, fout):
|
|
if fout is None:
|
|
fout = "BgmLoop.json"
|
|
files = glob.glob(os.path.join(dir_in, "**/*.adx"), recursive=True)
|
|
extract_loop_data_from_files(files, fout)
|
|
|
|
def extract_loop_data_from_files(files_in, fout):
|
|
if not files_in:
|
|
raise FileNotFoundError("No input files found")
|
|
collect = {}
|
|
for x in files_in:
|
|
res = extract_loop_data_from_file(x)
|
|
if res is not None:
|
|
collect[os.path.splitext(os.path.basename(x))[0]] = res
|
|
with open(fout, 'w') as out:
|
|
json.dump(collect, out, ensure_ascii=False, indent="\t", sort_keys=True)
|
|
|
|
def extract_loop_data_from_file(file_in, fout=None):
|
|
if not file_in.endswith(".adx"):
|
|
raise ValueError("Input file not .adx")
|
|
|
|
try:
|
|
with open(file_in, 'rb') as x:
|
|
data = x.read(64)
|
|
ADX = adx_file(data)
|
|
if ADX.l_exists and ADX.is_valid:
|
|
res = ADX.parse_loop_data()
|
|
else:
|
|
res = None
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
print("Error processing file: " + file_in)
|
|
return None
|
|
|
|
if fout is not None and res is not None:
|
|
with open(fout, 'w') as out:
|
|
json.dump(res, out, ensure_ascii=False, indent="\t", sort_keys=True)
|
|
return res
|