update_Astronauts

This commit is contained in:
rewjx 2022-02-05 20:58:08 +08:00
parent 8f35b00ced
commit f61126f5eb

View File

@ -41,7 +41,7 @@ namespace GameRes.Formats.Astronauts
public override bool IsHierarchic { get { return true; } } public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
private bool UseNameAsKey { get; set; } private bool UseNameAsKey = false;
static readonly byte[] KnownKey = { static readonly byte[] KnownKey = {
0x40, 0x21, 0x28, 0x38, 0xA6, 0x6E, 0x43, 0xA5, 0x40, 0x21, 0x28, 0x38, 0xA6, 0x43, 0xA5, 0x64, 0x40, 0x21, 0x28, 0x38, 0xA6, 0x6E, 0x43, 0xA5, 0x40, 0x21, 0x28, 0x38, 0xA6, 0x43, 0xA5, 0x64,
@ -51,36 +51,28 @@ namespace GameRes.Formats.Astronauts
public override ArcFile TryOpen(ArcView file) public override ArcFile TryOpen(ArcView file)
{ {
UseNameAsKey = false; UseNameAsKey = false;
ArcFile arc = TryOpenOrg(file); ArcFile arc = TryOpenGxpFile(file);
if(arc == null) if(null == arc)
{ {
//try use arc name as key UseNameAsKey = true; //try use arc name as key
UseNameAsKey = true; arc = TryOpenGxpFile(file);
arc = TryOpenOrg(file);
} }
return arc; return arc;
} }
public ArcFile TryOpenOrg (ArcView file) private ArcFile TryOpenGxpFile (ArcView file)
{ {
int count = file.View.ReadInt32 (0x18); int count = file.View.ReadInt32 (0x18);
if (!IsSaneCount (count)) if (!IsSaneCount (count))
return null; return null;
string arcName = Path.GetFileName(file.Name); string arcname = Path.GetFileName(file.Name);
byte[] arcNameBytes = Encoding.ASCII.GetBytes(arcName); byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname);
long base_offset = file.View.ReadInt64 (0x28); long base_offset = file.View.ReadInt64 (0x28);
uint entry_key = 0; uint entry_key = KnownKey[0] | (1u ^ KnownKey[1]) << 8 | (2u ^ KnownKey[2]) << 16 | (3u ^ KnownKey[3]) << 24;
if (UseNameAsKey) if (UseNameAsKey)
{ {
byte b1 = (byte)(KnownKey[0] ^ arcNameBytes[0]); uint arcname_key = (uint)(arcname_bytes[0] | (arcname_bytes[1 % arcname_bytes.Length]) << 8 | (arcname_bytes[2 % arcname_bytes.Length]) << 16 | (arcname_bytes[3 % arcname_bytes.Length]) << 24);
byte b2 = (byte)(1u ^ KnownKey[1] ^ arcNameBytes[1 % arcNameBytes.Length]); entry_key ^= arcname_key;
byte b3 = (byte)(2u ^ KnownKey[2] ^ arcNameBytes[2 % arcNameBytes.Length]);
byte b4 = (byte)(3u ^ KnownKey[3] ^ arcNameBytes[3 % arcNameBytes.Length]);
entry_key = (uint)(b1 | b2 << 8 | b3 << 16 | b4 << 24);
}
else
{
entry_key = KnownKey[0] | (1u ^ KnownKey[1]) << 8 | (2u ^ KnownKey[2]) << 16 | (3u ^ KnownKey[3]) << 24;
} }
uint index_offset = 0x30; uint index_offset = 0x30;
var entry_buffer = new byte[0x100]; var entry_buffer = new byte[0x100];
@ -95,14 +87,9 @@ namespace GameRes.Formats.Astronauts
if (entry_length != file.View.Read (index_offset, entry_buffer, 0, entry_length)) if (entry_length != file.View.Read (index_offset, entry_buffer, 0, entry_length))
return null; return null;
if (UseNameAsKey) if (UseNameAsKey)
{ Decrypt(entry_buffer, entry_length, arcname_bytes);
Decrypt(entry_buffer, entry_length, arcNameBytes);
}
else else
{
Decrypt(entry_buffer, entry_length); Decrypt(entry_buffer, entry_length);
}
int name_length = LittleEndian.ToInt32 (entry_buffer, 0xC) * 2; // length in characters int name_length = LittleEndian.ToInt32 (entry_buffer, 0xC) * 2; // length in characters
if (name_length >= entry_length) if (name_length >= entry_length)
return null; return null;
@ -123,27 +110,23 @@ namespace GameRes.Formats.Astronauts
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
if (UseNameAsKey) if (UseNameAsKey)
{ {
string name = Path.GetFileName(arc.File.Name); string arcname = Path.GetFileName(arc.File.Name);
byte[] nameBytes = Encoding.ASCII.GetBytes(name); byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname);
Decrypt(data, entry.Size, nameBytes); Decrypt(data, entry.Size, arcname_bytes);
} }
else else
{
Decrypt(data, entry.Size); Decrypt(data, entry.Size);
}
return new BinMemoryStream (data, entry.Name); return new BinMemoryStream (data, entry.Name);
} }
static void Decrypt (byte[] data, uint length, byte[] keyStrBytes=null) static void Decrypt (byte[] data, uint length, byte[] key=null)
{ {
for (uint i = 0; i < length; ++i) for (uint i = 0; i < length; ++i)
{ {
byte xorK = (byte)(i ^ KnownKey[i % KnownKey.Length]); byte xorkey = (byte)(i ^ KnownKey[i % KnownKey.Length]);
if(keyStrBytes != null) if(null != key)
{ xorkey ^= key[i % key.Length];
xorK ^= keyStrBytes[i % keyStrBytes.Length]; data[i] ^= xorkey;
}
data[i] ^= xorK;
} }
} }
} }