Fix line breaks.

This commit is contained in:
Crsky 2024-09-15 04:01:02 +08:00
parent 4a16bb3e9e
commit e642644845

View File

@ -1,53 +1,53 @@
//! \file ArcGXP.cs //! \file ArcGXP.cs
//! \date Thu Jun 30 08:17:12 2016 //! \date Thu Jun 30 08:17:12 2016
//! \brief Astronauts resource archive. //! \brief Astronauts resource archive.
// //
// Copyright (C) 2016 by morkt // Copyright (C) 2016 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the // deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is // sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE. // IN THE SOFTWARE.
// //
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text; using System.Text;
using GameRes.Utility; using GameRes.Utility;
namespace GameRes.Formats.Astronauts namespace GameRes.Formats.Astronauts
{ {
[Export(typeof(ArchiveFormat))] [Export(typeof(ArchiveFormat))]
public class PakOpener : ArchiveFormat public class PakOpener : ArchiveFormat
{ {
public override string Tag { get { return "GXP"; } } public override string Tag { get { return "GXP"; } }
public override string Description { get { return "Astronauts resource archive"; } } public override string Description { get { return "Astronauts resource archive"; } }
public override uint Signature { get { return 0x505847; } } // 'GXP' public override uint Signature { get { return 0x505847; } } // 'GXP'
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 = false; 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,
0x3E, 0x65, 0x24, 0x20, 0x46, 0x6E, 0x74, 0x3E, 0x65, 0x24, 0x20, 0x46, 0x6E, 0x74,
}; };
public override ArcFile TryOpen(ArcView file) public override ArcFile TryOpen(ArcView file)
{ {
UseNameAsKey = false; UseNameAsKey = false;
@ -58,76 +58,76 @@ namespace GameRes.Formats.Astronauts
arc = TryOpenGxpFile(file); arc = TryOpenGxpFile(file);
} }
return arc; return arc;
} }
private ArcFile TryOpenGxpFile (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[] arcname_bytes = 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 = KnownKey[0] | (1u ^ KnownKey[1]) << 8 | (2u ^ KnownKey[2]) << 16 | (3u ^ KnownKey[3]) << 24; uint entry_key = KnownKey[0] | (1u ^ KnownKey[1]) << 8 | (2u ^ KnownKey[2]) << 16 | (3u ^ KnownKey[3]) << 24;
if (UseNameAsKey) if (UseNameAsKey)
{ {
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); 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);
entry_key ^= arcname_key; entry_key ^= arcname_key;
} }
uint index_offset = 0x30; uint index_offset = 0x30;
var entry_buffer = new byte[0x100]; var entry_buffer = new byte[0x100];
var dir = new List<Entry> (count); var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {
var entry_length = file.View.ReadUInt32 (index_offset) ^ entry_key; var entry_length = file.View.ReadUInt32 (index_offset) ^ entry_key;
if (entry_length < 0x20 || entry_length > 0x1000) if (entry_length < 0x20 || entry_length > 0x1000)
return null; return null;
if (entry_length > entry_buffer.Length) if (entry_length > entry_buffer.Length)
entry_buffer = new byte[entry_length]; entry_buffer = new byte[entry_length];
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, arcname_bytes);
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;
var name = Encoding.Unicode.GetString (entry_buffer, 0x20, name_length); var name = Encoding.Unicode.GetString (entry_buffer, 0x20, name_length);
var entry = FormatCatalog.Instance.Create<Entry> (name); var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = base_offset + LittleEndian.ToInt64 (entry_buffer, 0x18); entry.Offset = base_offset + LittleEndian.ToInt64 (entry_buffer, 0x18);
entry.Size = LittleEndian.ToUInt32 (entry_buffer, 4); // length is 64-bit actually entry.Size = LittleEndian.ToUInt32 (entry_buffer, 4); // length is 64-bit actually
if (!entry.CheckPlacement (file.MaxOffset)) if (!entry.CheckPlacement (file.MaxOffset))
return null; return null;
dir.Add (entry); dir.Add (entry);
index_offset += entry_length; index_offset += entry_length;
} }
return new ArcFile (file, this, dir); return new ArcFile (file, this, dir);
} }
public override Stream OpenEntry (ArcFile arc, Entry entry) public override Stream OpenEntry (ArcFile arc, Entry entry)
{ {
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 arcname = Path.GetFileName(arc.File.Name); string arcname = Path.GetFileName(arc.File.Name);
byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname); byte[] arcname_bytes = Encoding.ASCII.GetBytes(arcname);
Decrypt(data, entry.Size, arcname_bytes); 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[] key=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 xorkey = (byte)(i ^ KnownKey[i % KnownKey.Length]); byte xorkey = (byte)(i ^ KnownKey[i % KnownKey.Length]);
if(null != key) if(null != key)
xorkey ^= key[i % key.Length]; xorkey ^= key[i % key.Length];
data[i] ^= xorkey; data[i] ^= xorkey;
} }
} }
} }
} }