import json;
import sys;
#open input file, read json
f = open(sys.argv[1], 'r', encoding='utf-8');
dict = json.load(f);
f.close();

#open output file, write magic and version
foo = sys.argv[1];
foo = foo[:foo.find('.')] + '.ysd';
out = open(foo, 'wb');
out.write(b'yrSD\0\0\0\0');

#create and write blob
blob = b'';
for k in dict:
	pos = len(blob);
	blob += bytes(dict[k], 'utf-8') + b'\0';
	dict[k] = pos;
out.write(len(blob).to_bytes(4, 'little'));
out.write(blob);

#write keys and offsets
out.write(len(dict).to_bytes(4, 'little'));
for k,v in dict.items():
	out.write((len(k)+1).to_bytes(1, 'little'));
	out.write(bytes(k, 'utf-8') + b'\0');
	out.write(v.to_bytes(4, 'little'));
	
#done
out.close();