2019-05-01 21:38:14 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using Toolbox;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
using System.Windows.Forms;
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using Toolbox.Library;
|
|
|
|
|
using Toolbox.Library.IO;
|
2019-05-01 21:38:14 +02:00
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
2019-07-15 21:41:02 +02:00
|
|
|
|
public class RARC : IArchiveFile, IFileFormat, IDirectoryContainer, IContextMenuNode
|
2019-05-01 21:38:14 +02:00
|
|
|
|
{
|
2019-06-18 01:19:27 +02: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)
|
|
|
|
|
{
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
|
2019-06-18 01:19:27 +02:00
|
|
|
|
{
|
|
|
|
|
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 03:05:10 +02:00
|
|
|
|
public List<INode> nodes = new List<INode>();
|
2019-06-18 01:19:27 +02:00
|
|
|
|
|
2019-07-19 00:05:27 +02:00
|
|
|
|
public IEnumerable<ArchiveFileInfo> Files => files;
|
2019-06-30 03:05:10 +02:00
|
|
|
|
public IEnumerable<INode> Nodes => nodes;
|
|
|
|
|
|
2019-07-19 00:05:27 +02:00
|
|
|
|
public void ClearFiles() { files.Clear(); }
|
|
|
|
|
|
2019-06-30 03:05:10 +02:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return FileName; }
|
|
|
|
|
set { FileName = value; }
|
|
|
|
|
}
|
2019-06-18 01:19:27 +02:00
|
|
|
|
|
|
|
|
|
private DirectoryEntry[] Directories;
|
|
|
|
|
|
2019-07-06 02:41:55 +02:00
|
|
|
|
private uint HeaderSize = 32;
|
|
|
|
|
private uint Unknown = 256;
|
|
|
|
|
|
2019-07-15 21:41:02 +02:00
|
|
|
|
public ToolStripItem[] GetContextMenuItems()
|
|
|
|
|
{
|
|
|
|
|
List<ToolStripItem> Items = new List<ToolStripItem>();
|
|
|
|
|
Items.Add(new ToolStripMenuItem("Save", null, SaveAction, Keys.Control | Keys.S));
|
|
|
|
|
return Items.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveAction(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
|
|
|
sfd.Filter = Utils.GetAllFilters(this);
|
|
|
|
|
sfd.FileName = FileName;
|
|
|
|
|
|
|
|
|
|
if (sfd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
STFileSaver.SaveFileFormat(this, sfd.FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 01:19:27 +02:00
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
2019-07-15 21:41:02 +02:00
|
|
|
|
CanSave = true;
|
2019-07-26 21:35:15 +02:00
|
|
|
|
CanRenameFiles = true;
|
|
|
|
|
CanReplaceFiles = true;
|
2019-07-15 21:41:02 +02:00
|
|
|
|
|
2019-06-18 01:19:27 +02:00
|
|
|
|
using (var reader = new FileReader(stream))
|
|
|
|
|
{
|
2019-07-26 21:35:15 +02:00
|
|
|
|
_savedDirectories.Clear();
|
|
|
|
|
|
2019-06-30 03:05:10 +02:00
|
|
|
|
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
reader.ReadSignature(4, "RARC");
|
|
|
|
|
uint FileSize = reader.ReadUInt32();
|
2019-07-06 02:41:55 +02:00
|
|
|
|
HeaderSize = reader.ReadUInt32();
|
2019-06-18 01:19:27 +02: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();
|
2019-07-06 17:29:29 +02:00
|
|
|
|
uint DirectoryOffset = reader.ReadUInt32() + (uint)pos;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
uint TotalNodeCount = reader.ReadUInt32();
|
2019-06-30 17:14:51 +02:00
|
|
|
|
uint NodeOffset = reader.ReadUInt32() + (uint)pos;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
uint StringTableSize = reader.ReadUInt32();
|
2019-06-30 17:14:51 +02:00
|
|
|
|
uint StringTablOffset = reader.ReadUInt32() + (uint)pos;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
ushort NodeCount = reader.ReadUInt16();
|
2019-07-06 02:41:55 +02:00
|
|
|
|
Unknown = reader.ReadUInt16();
|
2019-06-18 01:19:27 +02:00
|
|
|
|
byte[] Padding2 = reader.ReadBytes(4);
|
|
|
|
|
|
|
|
|
|
Directories = new DirectoryEntry[DirectoryCount];
|
2019-06-30 17:14:51 +02:00
|
|
|
|
for (int dir = 0; dir < DirectoryCount; dir++)
|
|
|
|
|
Directories[dir] = new DirectoryEntry(this);
|
2019-06-18 01:19:27 +02:00
|
|
|
|
|
2019-06-30 21:53:37 +02:00
|
|
|
|
Console.WriteLine($"DirectoryCount {DirectoryCount}");
|
2019-07-06 17:29:29 +02:00
|
|
|
|
Console.WriteLine($"StringTablOffset {StringTablOffset}");
|
2019-06-30 21:53:37 +02:00
|
|
|
|
|
2019-07-06 17:29:29 +02:00
|
|
|
|
reader.SeekBegin(DirectoryOffset);
|
2019-06-18 01:19:27 +02:00
|
|
|
|
for (int dir = 0; dir < DirectoryCount; dir++)
|
|
|
|
|
{
|
|
|
|
|
Directories[dir].Read(reader);
|
2019-07-06 17:29:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int dir = 0; dir < DirectoryCount; dir++)
|
|
|
|
|
{
|
2019-06-30 17:14:51 +02:00
|
|
|
|
uint NamePointer = StringTablOffset + Directories[dir].NameOffset;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
Directories[dir].Name = ReadStringAtTable(reader, NamePointer);
|
|
|
|
|
|
2019-07-26 01:02:51 +02:00
|
|
|
|
Console.WriteLine($"{ Directories[dir].Name } {dir}");
|
|
|
|
|
|
2019-06-30 17:14:51 +02:00
|
|
|
|
for (int n = 0; n < Directories[dir].NodeCount; n++)
|
2019-06-18 01:19:27 +02:00
|
|
|
|
{
|
2019-06-30 17:14:51 +02: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);
|
|
|
|
|
|
2019-07-06 17:29:29 +02:00
|
|
|
|
//These make it crash so just skip them
|
|
|
|
|
//Unsure what purpose these have
|
|
|
|
|
if (entry.Name == "." || entry.Name == "..")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (entry.IsDirectory)
|
|
|
|
|
{
|
|
|
|
|
Directories[dir].AddNode(Directories[entry.Offset]);
|
2019-07-26 00:54:58 +02:00
|
|
|
|
_savedDirectories.Add(entry);
|
2019-07-06 17:29:29 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
2019-06-30 17:14:51 +02:00
|
|
|
|
{
|
|
|
|
|
using (reader.TemporarySeek(pos + DataOffset + entry.Offset, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
entry.FileData = reader.ReadBytes((int)entry.Size);
|
|
|
|
|
}
|
2019-06-30 21:53:37 +02:00
|
|
|
|
entry.FileName = entry.Name;
|
|
|
|
|
|
2019-06-30 17:14:51 +02:00
|
|
|
|
Directories[dir].AddNode(entry);
|
|
|
|
|
}
|
2019-06-18 01:19:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 17:14:51 +02:00
|
|
|
|
|
|
|
|
|
this.Name = Directories[0].Name;
|
|
|
|
|
nodes.AddRange(Directories[0].Nodes);
|
2019-06-18 01:19:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
private List<FileEntry> _savedFiles = new List<FileEntry>();
|
2019-07-26 00:54:58 +02:00
|
|
|
|
private List<FileEntry> _savedDirectories = new List<FileEntry>();
|
|
|
|
|
private List<DirectoryEntry> _savedNodes = new List<DirectoryEntry>();
|
2019-07-06 02:41:55 +02:00
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
private void LoadAllDirectoriesAndFiles(DirectoryEntry parentDir)
|
2019-07-06 02:41:55 +02:00
|
|
|
|
{
|
2019-07-25 22:49:04 +02:00
|
|
|
|
for (int i = 0; i < parentDir.nodes.Count; i++)
|
2019-07-15 21:41:02 +02:00
|
|
|
|
{
|
2019-07-25 22:49:04 +02:00
|
|
|
|
if (parentDir.nodes[i] is DirectoryEntry)
|
|
|
|
|
{
|
2019-07-26 00:54:58 +02:00
|
|
|
|
//_savedDirectories.Add((DirectoryEntry)parentDir.nodes[i]);
|
2019-07-25 22:49:04 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
_savedFiles.Add((FileEntry)parentDir.nodes[i]);
|
2019-07-15 21:41:02 +02:00
|
|
|
|
}
|
2019-07-06 02:41:55 +02:00
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
for (int i = 0; i < parentDir.nodes.Count; i++)
|
2019-07-15 21:41:02 +02:00
|
|
|
|
{
|
2019-07-25 22:49:04 +02:00
|
|
|
|
if (parentDir.nodes[i] is DirectoryEntry)
|
|
|
|
|
LoadAllDirectoriesAndFiles((DirectoryEntry)parentDir.nodes[i]);
|
2019-07-15 21:41:02 +02:00
|
|
|
|
}
|
2019-07-06 02:41:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
//Save a string table to a char array like WArchive
|
|
|
|
|
//It's a good idea, and i don't want to deal with offset linking
|
|
|
|
|
private List<char> _exportStringTable;
|
|
|
|
|
private List<byte[]> _savedFileData;
|
|
|
|
|
public void SaveFile(FileWriter writer)
|
2019-07-06 02:41:55 +02:00
|
|
|
|
{
|
2019-07-26 02:56:28 +02:00
|
|
|
|
_savedFiles.Clear();
|
2019-07-25 22:49:04 +02:00
|
|
|
|
_exportStringTable = new List<char>();
|
|
|
|
|
_savedFileData = new List<byte[]>();
|
2019-07-26 00:54:58 +02:00
|
|
|
|
_savedNodes = Directories.ToList();
|
2019-07-06 02:41:55 +02:00
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
LoadAllDirectoriesAndFiles(Directories[0]);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
_exportStringTable = new List<char>();
|
|
|
|
|
_exportStringTable.AddRange(".\0".ToCharArray());
|
|
|
|
|
_exportStringTable.AddRange("..\0".ToCharArray());
|
2019-07-06 02:41:55 +02:00
|
|
|
|
|
|
|
|
|
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
|
2019-07-15 21:41:02 +02:00
|
|
|
|
writer.Write(uint.MaxValue); //File Size
|
2019-07-06 02:41:55 +02:00
|
|
|
|
writer.Write(uint.MaxValue); //End of file
|
|
|
|
|
writer.Seek(8); //padding
|
|
|
|
|
|
|
|
|
|
writer.SeekBegin(HeaderSize);
|
|
|
|
|
long InfoPos = writer.Position;
|
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
writer.Write(_savedNodes.Count);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
writer.Write(uint.MaxValue); //DirectoryOffset
|
2019-07-26 02:14:33 +02:00
|
|
|
|
writer.Write(_savedNodes.Count + _savedDirectories.Count + _savedFiles.Count);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
writer.Write(uint.MaxValue); //File Node Offset
|
|
|
|
|
writer.Write(uint.MaxValue); //String pool size
|
|
|
|
|
writer.Write(uint.MaxValue); //String pool offset
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Write((ushort)0);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
writer.Write((ushort)Unknown);
|
|
|
|
|
writer.Write(0); //padding
|
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
List<FileEntry> TotalList = _savedDirectories.Concat(_savedFiles).ToList();
|
|
|
|
|
|
|
|
|
|
//Write nodes
|
2019-07-15 21:41:02 +02:00
|
|
|
|
WriteOffset(writer, 4, InfoPos);
|
2019-07-26 00:54:58 +02:00
|
|
|
|
for (int dir = 0; dir < _savedNodes.Count; dir++)
|
2019-07-06 02:41:55 +02:00
|
|
|
|
{
|
2019-07-26 00:54:58 +02:00
|
|
|
|
writer.Write(_savedNodes[dir].Identifier);
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Write((int)_exportStringTable.Count);
|
2019-07-26 00:54:58 +02:00
|
|
|
|
writer.Write(_savedNodes[dir].Hash);
|
|
|
|
|
writer.Write((ushort)(_savedNodes[dir].nodes.Count));
|
|
|
|
|
writer.Write(TotalList.FindIndex(i => i.Name == _savedNodes[dir].nodes[0].Name));
|
|
|
|
|
_exportStringTable.AddRange(_savedNodes[dir].Name.ToCharArray());
|
2019-07-25 22:49:04 +02:00
|
|
|
|
_exportStringTable.Add('\0'); //Null terminated
|
2019-07-06 02:41:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Align(32);
|
2019-07-15 21:41:02 +02:00
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
//Write the directories and files
|
2019-07-15 21:41:02 +02:00
|
|
|
|
WriteOffset(writer, 12, InfoPos);
|
2019-07-26 21:35:15 +02:00
|
|
|
|
int I = 0;
|
2019-07-26 00:54:58 +02:00
|
|
|
|
foreach (FileEntry entry in TotalList)
|
2019-07-06 02:41:55 +02:00
|
|
|
|
{
|
2019-07-26 03:02:15 +02:00
|
|
|
|
entry.SaveFileFormat();
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Write(entry.FileId);
|
|
|
|
|
writer.Write(entry.Hash);
|
|
|
|
|
writer.Write(entry.Flags);
|
|
|
|
|
writer.Seek(1); //Padding
|
|
|
|
|
|
|
|
|
|
if ((entry.Name == "."))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((ushort)0);
|
|
|
|
|
}
|
|
|
|
|
else if ((entry.Name == ".."))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((ushort)2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-26 00:54:58 +02:00
|
|
|
|
// Offset of name in the string table
|
|
|
|
|
writer.Write((ushort)_exportStringTable.Count);
|
|
|
|
|
|
|
|
|
|
// Add name to string table
|
|
|
|
|
_exportStringTable.AddRange(entry.Name.ToCharArray());
|
|
|
|
|
|
|
|
|
|
// Strings must be null terminated
|
|
|
|
|
_exportStringTable.Add('\0');
|
|
|
|
|
|
|
|
|
|
/* if (_savedFiles.Find(i => i.Name == entry.Name) != null)
|
2019-07-25 22:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
string test = new string(_exportStringTable.ToArray());
|
|
|
|
|
|
|
|
|
|
int testt = test.IndexOf(entry.Name);
|
|
|
|
|
|
|
|
|
|
writer.Write((ushort)test.IndexOf(entry.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-26 00:54:58 +02:00
|
|
|
|
|
|
|
|
|
}*/
|
2019-07-25 22:49:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry.Flags == 0x02)
|
2019-07-15 21:41:02 +02:00
|
|
|
|
{
|
2019-07-26 02:07:08 +02:00
|
|
|
|
writer.Write((int)TotalList.IndexOf(entry) + 1);
|
|
|
|
|
writer.Write((int)0);
|
2019-07-25 22:49:04 +02:00
|
|
|
|
}
|
2019-07-26 02:07:08 +02:00
|
|
|
|
else
|
2019-07-25 22:49:04 +02:00
|
|
|
|
{
|
2019-07-26 00:54:58 +02:00
|
|
|
|
uint dataOffset = 0;
|
|
|
|
|
foreach (var data in _savedFileData)
|
|
|
|
|
dataOffset += (uint)data.Length;
|
|
|
|
|
|
|
|
|
|
writer.Write(dataOffset);
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Write(entry.FileData.Length);
|
|
|
|
|
_savedFileData.Add(entry.FileData);
|
2019-07-15 21:41:02 +02:00
|
|
|
|
}
|
2019-07-25 22:49:04 +02:00
|
|
|
|
|
|
|
|
|
writer.Write(0);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Align(32);
|
|
|
|
|
|
|
|
|
|
WriteOffset(writer, 20, InfoPos);
|
|
|
|
|
writer.Write(_exportStringTable.ToArray());
|
|
|
|
|
|
|
|
|
|
writer.Align(32);
|
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
|
|
|
|
|
var dataPos = writer.Position;
|
|
|
|
|
using (writer.TemporarySeek(pos + 12, System.IO.SeekOrigin.Begin)) {
|
|
|
|
|
writer.Write((uint)(dataPos - InfoPos));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
foreach (var data in _savedFileData)
|
|
|
|
|
writer.Write(data);
|
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
uint DataSize = (uint)(writer.Position - dataPos);
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
writer.Align(32);
|
|
|
|
|
|
2019-07-26 00:54:58 +02:00
|
|
|
|
uint EndFileSize = (uint)(writer.Position - dataPos);
|
|
|
|
|
|
|
|
|
|
//Write data size
|
|
|
|
|
using (writer.TemporarySeek(pos + 16, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)DataSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Write end of file size
|
|
|
|
|
using (writer.TemporarySeek(pos + 20, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)EndFileSize);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
//Write string table size
|
2019-07-06 02:41:55 +02:00
|
|
|
|
using (writer.TemporarySeek(pos + 0x4, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)writer.BaseStream.Length);
|
|
|
|
|
}
|
2019-07-25 22:49:04 +02:00
|
|
|
|
|
|
|
|
|
//Write file size
|
|
|
|
|
using (writer.TemporarySeek(InfoPos + 16, System.IO.SeekOrigin.Begin))
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)_exportStringTable.Count);
|
|
|
|
|
}
|
2019-07-28 18:45:39 +02:00
|
|
|
|
|
|
|
|
|
writer.Dispose();
|
|
|
|
|
GC.SuppressFinalize(writer);
|
2019-07-06 02:41:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2019-07-15 21:41:02 +02:00
|
|
|
|
writer.Write((uint)(Position - RelativePosition));
|
2019-07-06 02:41:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 01:19:27 +02: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 03:05:10 +02:00
|
|
|
|
public class DirectoryEntry : IDirectoryContainer
|
2019-06-18 01:19:27 +02:00
|
|
|
|
{
|
|
|
|
|
public RARC ParentArchive { get; }
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
2019-07-25 22:49:04 +02:00
|
|
|
|
public uint Identifier;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
internal uint NameOffset; //Relative to string table
|
|
|
|
|
public ushort Hash { get; set; }
|
2019-06-30 17:14:51 +02:00
|
|
|
|
public ushort NodeCount;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
public uint FirstNodeIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
public DirectoryEntry(RARC rarc) { ParentArchive = rarc; }
|
|
|
|
|
|
2019-06-30 17:14:51 +02: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 03:05:10 +02:00
|
|
|
|
|
2019-06-18 01:19:27 +02:00
|
|
|
|
public void Read(FileReader reader)
|
|
|
|
|
{
|
|
|
|
|
Identifier = reader.ReadUInt32();
|
|
|
|
|
NameOffset = reader.ReadUInt32();
|
|
|
|
|
Hash = reader.ReadUInt16();
|
|
|
|
|
NodeCount = reader.ReadUInt16();
|
|
|
|
|
FirstNodeIndex = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 22:49:04 +02:00
|
|
|
|
public void UpdateHash() {
|
2019-06-18 01:19:27 +02:00
|
|
|
|
Hash = CalculateHash(Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Save()
|
|
|
|
|
{
|
2019-07-06 02:41:55 +02:00
|
|
|
|
var mem = new System.IO.MemoryStream();
|
|
|
|
|
SaveFile(new FileWriter(mem));
|
|
|
|
|
return mem.ToArray();
|
2019-06-18 01:19:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FileEntry : ArchiveFileInfo
|
|
|
|
|
{
|
2019-07-06 17:29:29 +02:00
|
|
|
|
//According to this to determine directory or not
|
|
|
|
|
//https://github.com/LordNed/WArchive-Tools/blob/3c7fdefe54b4c7634a042847b7455de61705033f/ArchiveToolsLib/Archive/Archive.cs#L44
|
|
|
|
|
public bool IsDirectory { get { return (Flags & 2) >> 1 == 1; } }
|
|
|
|
|
|
2019-06-30 17:14:51 +02:00
|
|
|
|
public ushort FileId { get; set; }
|
2019-07-06 17:29:29 +02:00
|
|
|
|
public ushort Hash { get; set; }
|
|
|
|
|
public byte Flags { get; set; }
|
2019-06-18 01:19:27 +02:00
|
|
|
|
|
|
|
|
|
internal uint Size;
|
|
|
|
|
internal uint Offset;
|
2019-06-30 17:14:51 +02:00
|
|
|
|
internal ushort NameOffset;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
|
2019-07-06 02:41:55 +02:00
|
|
|
|
internal long _positionPtr;
|
2019-06-18 01:19:27 +02:00
|
|
|
|
public void Read(FileReader reader)
|
|
|
|
|
{
|
2019-06-30 17:14:51 +02:00
|
|
|
|
FileId = reader.ReadUInt16();
|
2019-07-06 17:29:29 +02:00
|
|
|
|
Hash = reader.ReadUInt16();
|
|
|
|
|
Flags = reader.ReadByte();
|
|
|
|
|
reader.Seek(1); //Padding
|
2019-06-30 17:14:51 +02:00
|
|
|
|
NameOffset = reader.ReadUInt16();
|
2019-06-18 01:19:27 +02:00
|
|
|
|
Offset = reader.ReadUInt32();
|
|
|
|
|
Size = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-01 21:38:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|