2019-05-01 19:38:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
using Switch_Toolbox;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Switch_Toolbox.Library;
|
|
|
|
|
using Switch_Toolbox.Library.IO;
|
2019-05-01 19:38:14 +00:00
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
2019-06-30 01:05:10 +00:00
|
|
|
|
public class RARC : IArchiveFile, IFileFormat, IDirectoryContainer
|
2019-05-01 19:38:14 +00:00
|
|
|
|
{
|
2019-06-17 23:19:27 +00:00
|
|
|
|
public FileType FileType { get; set; } = FileType.Archive;
|
|
|
|
|
|
|
|
|
|
public bool CanSave { get; set; }
|
|
|
|
|
public string[] Description { get; set; } = new string[] { "RARC" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.rarc", "*.arc", "*.yaz0" };
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
public IFileInfo IFileInfo { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool CanAddFiles { get; set; }
|
|
|
|
|
public bool CanRenameFiles { get; set; }
|
|
|
|
|
public bool CanReplaceFiles { get; set; }
|
|
|
|
|
public bool CanDeleteFiles { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool Identify(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new Switch_Toolbox.Library.IO.FileReader(stream, true))
|
|
|
|
|
{
|
|
|
|
|
return reader.CheckSignature(4, "RARC");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type[] Types
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
return types.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FileEntry> files = new List<FileEntry>();
|
2019-06-30 01:05:10 +00:00
|
|
|
|
public List<INode> nodes = new List<INode>();
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
|
|
|
|
public IEnumerable<ArchiveFileInfo> Files => files;
|
2019-06-30 01:05:10 +00:00
|
|
|
|
public IEnumerable<INode> Nodes => nodes;
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return FileName; }
|
|
|
|
|
set { FileName = value; }
|
|
|
|
|
}
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
|
|
|
|
private DirectoryEntry[] Directories;
|
|
|
|
|
|
2019-07-06 00:41:55 +00:00
|
|
|
|
private uint HeaderSize = 32;
|
|
|
|
|
private uint Unknown = 256;
|
|
|
|
|
|
2019-06-17 23:19:27 +00:00
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new FileReader(stream))
|
|
|
|
|
{
|
2019-06-30 01:05:10 +00:00
|
|
|
|
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
reader.ReadSignature(4, "RARC");
|
|
|
|
|
uint FileSize = reader.ReadUInt32();
|
2019-07-06 00:41:55 +00:00
|
|
|
|
HeaderSize = reader.ReadUInt32();
|
2019-06-17 23:19:27 +00:00
|
|
|
|
uint DataOffset = reader.ReadUInt32();
|
|
|
|
|
uint FileDataSize = reader.ReadUInt32();
|
|
|
|
|
uint EndOfFileOffset = reader.ReadUInt32();
|
|
|
|
|
byte[] Padding = reader.ReadBytes(8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Info Block
|
|
|
|
|
long pos = reader.Position;
|
|
|
|
|
|
|
|
|
|
uint DirectoryCount = reader.ReadUInt32();
|
|
|
|
|
uint DirectoryOffset = reader.ReadUInt32();
|
|
|
|
|
uint TotalNodeCount = reader.ReadUInt32();
|
2019-06-30 15:14:51 +00:00
|
|
|
|
uint NodeOffset = reader.ReadUInt32() + (uint)pos;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
uint StringTableSize = reader.ReadUInt32();
|
2019-06-30 15:14:51 +00:00
|
|
|
|
uint StringTablOffset = reader.ReadUInt32() + (uint)pos;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
ushort NodeCount = reader.ReadUInt16();
|
2019-07-06 00:41:55 +00:00
|
|
|
|
Unknown = reader.ReadUInt16();
|
2019-06-17 23:19:27 +00:00
|
|
|
|
byte[] Padding2 = reader.ReadBytes(4);
|
|
|
|
|
|
|
|
|
|
Directories = new DirectoryEntry[DirectoryCount];
|
2019-06-30 15:14:51 +00:00
|
|
|
|
for (int dir = 0; dir < DirectoryCount; dir++)
|
|
|
|
|
Directories[dir] = new DirectoryEntry(this);
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
2019-06-30 19:53:37 +00:00
|
|
|
|
Console.WriteLine($"DirectoryCount {DirectoryCount}");
|
|
|
|
|
|
2019-06-17 23:19:27 +00:00
|
|
|
|
reader.SeekBegin(DirectoryOffset + pos);
|
|
|
|
|
for (int dir = 0; dir < DirectoryCount; dir++)
|
|
|
|
|
{
|
|
|
|
|
Directories[dir].Read(reader);
|
2019-06-30 15:14:51 +00:00
|
|
|
|
uint NamePointer = StringTablOffset + Directories[dir].NameOffset;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
Directories[dir].Name = ReadStringAtTable(reader, NamePointer);
|
|
|
|
|
|
2019-06-30 19:53:37 +00:00
|
|
|
|
Console.WriteLine($"DirectoryCount {DirectoryCount}");
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
2019-06-30 15:14:51 +00:00
|
|
|
|
long EndDirectoryPos = reader.Position;
|
|
|
|
|
for (int n = 0; n < Directories[dir].NodeCount; n++)
|
2019-06-17 23:19:27 +00:00
|
|
|
|
{
|
2019-06-30 15:14:51 +00:00
|
|
|
|
reader.SeekBegin(NodeOffset + ((n + Directories[dir].FirstNodeIndex) * 0x14));
|
|
|
|
|
FileEntry entry = new FileEntry();
|
|
|
|
|
entry.Read(reader);
|
|
|
|
|
NamePointer = StringTablOffset + entry.NameOffset;
|
|
|
|
|
entry.Name = ReadStringAtTable(reader, NamePointer);
|
|
|
|
|
|
|
|
|
|
if (entry.FileId != 0xFFFF)
|
|
|
|
|
{
|
|
|
|
|
using (reader.TemporarySeek(pos + DataOffset + entry.Offset, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
entry.FileData = reader.ReadBytes((int)entry.Size);
|
|
|
|
|
}
|
2019-06-30 19:53:37 +00:00
|
|
|
|
entry.FileName = entry.Name;
|
|
|
|
|
|
2019-06-30 15:14:51 +00:00
|
|
|
|
Directories[dir].AddNode(entry);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Directories[dir].AddNode(Directories[entry.Offset]);
|
2019-06-17 23:19:27 +00:00
|
|
|
|
}
|
2019-06-30 15:14:51 +00:00
|
|
|
|
reader.SeekBegin(EndDirectoryPos);
|
2019-06-17 23:19:27 +00:00
|
|
|
|
}
|
2019-06-30 15:14:51 +00:00
|
|
|
|
|
|
|
|
|
this.Name = Directories[0].Name;
|
|
|
|
|
nodes.AddRange(Directories[0].Nodes);
|
2019-06-17 23:19:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 00:41:55 +00:00
|
|
|
|
public int GetTotalCount()
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
foreach (var dir in Directories)
|
|
|
|
|
GetTotalCount(dir, count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetFileCount()
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
foreach (var dir in Directories)
|
|
|
|
|
GetFileCount(dir, count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetDirectoryCount()
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
foreach (var dir in Directories)
|
|
|
|
|
GetDirectoryCount(dir, count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetTotalCount(INode node, int count)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
foreach (var c in ((DirectoryEntry)node).nodes)
|
|
|
|
|
return GetDirectoryCount(c, count);
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int GetFileCount(INode node, int count)
|
|
|
|
|
{
|
|
|
|
|
if (node is FileEntry)
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
foreach (var c in ((DirectoryEntry)node).nodes)
|
|
|
|
|
return GetDirectoryCount(c, count);
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetDirectoryCount(INode node, int count)
|
|
|
|
|
{
|
|
|
|
|
if (node is DirectoryEntry)
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
foreach (var c in ((DirectoryEntry)node).nodes)
|
|
|
|
|
return GetDirectoryCount(c, count);
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveFile(FileWriter writer)
|
|
|
|
|
{
|
|
|
|
|
long pos = writer.Position;
|
|
|
|
|
|
|
|
|
|
writer.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
|
|
|
|
|
writer.WriteSignature("RARC");
|
|
|
|
|
writer.Write(uint.MaxValue); //FileSize
|
|
|
|
|
writer.Write(HeaderSize);
|
|
|
|
|
writer.Write(uint.MaxValue); //DataOffset
|
|
|
|
|
writer.Write(uint.MaxValue); //End of file
|
|
|
|
|
writer.Seek(8); //padding
|
|
|
|
|
|
|
|
|
|
writer.SeekBegin(HeaderSize);
|
|
|
|
|
long InfoPos = writer.Position;
|
|
|
|
|
|
|
|
|
|
writer.Write(GetDirectoryCount());
|
|
|
|
|
writer.Write(uint.MaxValue); //DirectoryOffset
|
|
|
|
|
writer.Write(GetFileCount());
|
|
|
|
|
writer.Write(uint.MaxValue); //File Node Offset
|
|
|
|
|
writer.Write(uint.MaxValue); //String pool size
|
|
|
|
|
writer.Write(uint.MaxValue); //String pool offset
|
|
|
|
|
writer.Write((ushort)GetFileCount());
|
|
|
|
|
writer.Write((ushort)Unknown);
|
|
|
|
|
writer.Write(0); //padding
|
|
|
|
|
|
|
|
|
|
//Write directory Offset
|
|
|
|
|
WriteOffset(writer, 0x4, InfoPos);
|
|
|
|
|
for (int dir = 0; dir < Directories.Length; dir++)
|
|
|
|
|
{
|
|
|
|
|
Directories[dir].Write(writer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int dir = 0; dir < Directories.Length; dir++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Write file size
|
|
|
|
|
using (writer.TemporarySeek(pos + 0x4, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)writer.BaseStream.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteDirectories()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteOffset(FileWriter writer, long Target, long RelativePosition)
|
|
|
|
|
{
|
|
|
|
|
long Position = writer.Position;
|
|
|
|
|
using (writer.TemporarySeek(RelativePosition + Target, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)Position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 23:19:27 +00:00
|
|
|
|
private string ReadStringAtTable(FileReader reader, uint NameOffset)
|
|
|
|
|
{
|
|
|
|
|
using (reader.TemporarySeek(NameOffset, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
return reader.ReadZeroTerminatedString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ushort CalculateHash(string Name)
|
|
|
|
|
{
|
|
|
|
|
ushort Hash = 0;
|
|
|
|
|
for (int i = 0; i < Name.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Hash *= 3;
|
|
|
|
|
Hash += Name[i];
|
|
|
|
|
}
|
|
|
|
|
return Hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateDirectoryEntry()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 01:05:10 +00:00
|
|
|
|
public class DirectoryEntry : IDirectoryContainer
|
2019-06-17 23:19:27 +00:00
|
|
|
|
{
|
|
|
|
|
public RARC ParentArchive { get; }
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
private uint Identifier;
|
|
|
|
|
|
|
|
|
|
internal uint NameOffset; //Relative to string table
|
|
|
|
|
|
|
|
|
|
public ushort Hash { get; set; }
|
|
|
|
|
|
2019-06-30 15:14:51 +00:00
|
|
|
|
public ushort NodeCount;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
|
|
|
|
public uint FirstNodeIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
public DirectoryEntry(RARC rarc) { ParentArchive = rarc; }
|
|
|
|
|
|
2019-06-30 15:14:51 +00:00
|
|
|
|
public IEnumerable<INode> Nodes { get { return nodes; } }
|
|
|
|
|
public List<INode> nodes = new List<INode>();
|
|
|
|
|
|
|
|
|
|
public void AddNode(INode node)
|
|
|
|
|
{
|
|
|
|
|
nodes.Add(node);
|
|
|
|
|
}
|
2019-06-30 01:05:10 +00:00
|
|
|
|
|
2019-07-06 00:41:55 +00:00
|
|
|
|
|
|
|
|
|
internal long _positionPtr;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
public void Read(FileReader reader)
|
|
|
|
|
{
|
|
|
|
|
Identifier = reader.ReadUInt32();
|
|
|
|
|
NameOffset = reader.ReadUInt32();
|
|
|
|
|
Hash = reader.ReadUInt16();
|
|
|
|
|
NodeCount = reader.ReadUInt16();
|
|
|
|
|
FirstNodeIndex = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(FileWriter writer)
|
|
|
|
|
{
|
2019-07-06 00:41:55 +00:00
|
|
|
|
_positionPtr = writer.Position;
|
|
|
|
|
|
2019-06-17 23:19:27 +00:00
|
|
|
|
Hash = CalculateHash(Name);
|
|
|
|
|
|
|
|
|
|
writer.Write(Identifier);
|
|
|
|
|
writer.Write(NameOffset);
|
|
|
|
|
writer.Write(Hash);
|
|
|
|
|
writer.Write(NodeCount);
|
|
|
|
|
writer.Write(FirstNodeIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Save()
|
|
|
|
|
{
|
2019-07-06 00:41:55 +00:00
|
|
|
|
var mem = new System.IO.MemoryStream();
|
|
|
|
|
SaveFile(new FileWriter(mem));
|
|
|
|
|
return mem.ToArray();
|
2019-06-17 23:19:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FileEntry : ArchiveFileInfo
|
|
|
|
|
{
|
2019-06-30 15:14:51 +00:00
|
|
|
|
public ushort FileId { get; set; }
|
2019-06-17 23:19:27 +00:00
|
|
|
|
public uint Unknown { get; set; }
|
|
|
|
|
|
|
|
|
|
internal uint Size;
|
|
|
|
|
internal uint Offset;
|
2019-06-30 15:14:51 +00:00
|
|
|
|
internal ushort NameOffset;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
2019-07-06 00:41:55 +00:00
|
|
|
|
internal long _positionPtr;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
public void Read(FileReader reader)
|
|
|
|
|
{
|
2019-06-30 15:14:51 +00:00
|
|
|
|
FileId = reader.ReadUInt16();
|
2019-07-06 00:41:55 +00:00
|
|
|
|
Unknown = reader.ReadUInt32();
|
2019-06-30 15:14:51 +00:00
|
|
|
|
NameOffset = reader.ReadUInt16();
|
2019-06-17 23:19:27 +00:00
|
|
|
|
Offset = reader.ReadUInt32();
|
|
|
|
|
Size = reader.ReadUInt32();
|
|
|
|
|
}
|
2019-07-06 00:41:55 +00:00
|
|
|
|
|
|
|
|
|
public void Write(FileWriter writer)
|
|
|
|
|
{
|
|
|
|
|
_positionPtr = writer.Position;
|
|
|
|
|
|
|
|
|
|
SaveFileFormat();
|
|
|
|
|
|
|
|
|
|
writer.Write(FileId);
|
|
|
|
|
writer.Write(Unknown);
|
|
|
|
|
writer.Write(NameOffset);
|
|
|
|
|
writer.Write(uint.MaxValue);
|
|
|
|
|
writer.Write(FileData.Length);
|
|
|
|
|
}
|
2019-06-17 23:19:27 +00:00
|
|
|
|
}
|
2019-05-01 19:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|