#!/usr/bin/env python3
"""
Only lua.u and setting.u are encrypted.
The key was found by attaching Frida, then pseudocode was generated for 
Game.BundleEncrypt.Decrypt(byte[] data) in a disassembler, then
translated into python code by Razmoth.

v15 = (QWORD)(a1+24);
a1 is a pointer to byte[] passed into Decrypt(byte[] data). byte[]
is actually a struct type, System_byte_array.
if you open il2cpp.h (you should have this in your il2cppdumper),
+24 would be il2cpp_array_size_t max_length.
"""
import sys
SEED = 0x166920ED

if __name__ == '__main__':
	if len(sys.argv) < 3:
		print("Usage: "+sys.argv[0]+" <input> <output>")
		sys.exit(2)
		
	with open(sys.argv[1], 'rb') as file:
		data = bytearray(file.read())

	size = len(data) - len(data) % 2
	for i in range(2, size, 4):
		j = i - 1
		# // means "floor division" in python
		if SEED % j < j // 3:
			data[j], data[i] = data[i], data[j]
	
	with open(sys.argv[2], 'wb') as file:
		file.write(bytes(data))
