Skip to content

Instantly share code, notes, and snippets.

@esterTion
Created September 26, 2019 08:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esterTion/ff57aafd8dc950216041ac004fc25536 to your computer and use it in GitHub Desktop.
Save esterTion/ff57aafd8dc950216041ac004fc25536 to your computer and use it in GitHub Desktop.
<?php
// https://github.com/esterTion/unity-texture-toolkit/blob/master/UnityBundle.php
require_once 'UnityBundle.php';
// master data manifest
$f = new FileStream('masterdata_i_ja');
$f->littleEndian = true;
// head sha1? skipped in code
$f->position = 20;
function ReadByte(Stream $f) {
return hexdec(bin2hex($f->byte));
}
function ReadInt(Stream $f) {
$val = ReadByte($f);
if ($val >= 128) {
$b2 = ReadByte($f);
$b3 = ReadByte($f);
$b4 = ReadByte($f);
$val = $val + (($b2 + (($b3 + ($b4 << 8)) << 8)) << 7);
}
return $val;
}
function ReadString(Stream $f) {
$strlen = ReadInt($f);
return $f->readData($strlen);
}
function GetKeyData($k) {
$keys = [];
for ($i=0; $i<strlen($k); $i+=8) {
$keys[] = hexdec(substr($k, $i, 8));
}
return $keys;
}
// three header things
$hash = ReadString($f);
$lang = ReadString($f);
$rows = ReadInt($f);
$out = [];
for ($i=0; $i < $rows; $i++) {
$v15 = ReadString($f); // db name
$v16 = ReadString($f); // db keys
$out[] = [$v15, $v16, GetKeyData($v16)];
}
for ($i=0; $i < $rows; $i++) {
$out[$i][] = bin2hex($f->readData(20)); // download sha1 hash
$out[$i][] = ReadInt($f); // download file size
}
//print_r($out[0]);
// find at Namespace: LLAS.DM.Dbapi Constant, SqliteKey1 & 2 & 3
$constKeys = [
0x3039, // 12345
0x10932,// 67890
0x7AB7 // 31415
];
for ($i=0; $i<count($out); $i++) {
$keys = [
$constKeys[0] ^ $out[$i][2][0],
$constKeys[1] ^ $out[$i][2][1],
$constKeys[2] ^ $out[$i][2][2],
];
if (!file_exists($out[$i][0])) continue;
print_r($out[$i]);
$data = (file_get_contents($out[$i][0]));
$data = SIFAS_Decrypt_Stream($data, ...$keys);
file_put_contents($out[$i][0].'-dec.db.gz', $data);
file_put_contents($out[$i][0].'-dec.db', gzinflate($data));
}
function SIFAS_Decrypt_Stream(string $data, int $key0, int $key1, int $key2) {
for ($j=0; $j<strlen($data); $j++) {
$data[$j] = $data[$j] ^ chr((($key1 ^ $key0 ^ $key2) >> 24) & 0xff);
$key0 = (0x343fd * $key0 + 0x269ec3) & 0xFFFFFFFF;
$key1 = (0x343fd * $key1 + 0x269ec3) & 0xFFFFFFFF;
$key2 = (0x343fd * $key2 + 0x269ec3) & 0xFFFFFFFF;
}
return $data;
}
/*
Read assets: (not tested)
SIFAS_Decrypt_Stream($encrypted_asset_data, 12345, $key1, $key2)
$key1 & $key2 from asset_i_ja_0.db
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment