2019-07-09 21:50:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using Toolbox;
|
2019-07-09 21:50:50 +00:00
|
|
|
|
using System.Windows.Forms;
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using Toolbox.Library;
|
|
|
|
|
using Toolbox.Library.IO;
|
2019-07-09 21:50:50 +00:00
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
|
|
|
|
public class U8 : IArchiveFile, IFileFormat, IDirectoryContainer
|
|
|
|
|
{
|
|
|
|
|
public FileType FileType { get; set; } = FileType.Archive;
|
|
|
|
|
|
|
|
|
|
public bool CanSave { get; set; }
|
|
|
|
|
public string[] Description { get; set; } = new string[] { "U8" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.u8"};
|
|
|
|
|
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; }
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
private readonly uint BEMagic = 0x55AA382D;
|
|
|
|
|
private readonly uint LEMagic = 0x2D38AA55;
|
|
|
|
|
|
2019-07-09 21:50:50 +00:00
|
|
|
|
public bool Identify(System.IO.Stream stream)
|
|
|
|
|
{
|
2019-07-16 21:35:21 +00:00
|
|
|
|
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
|
2019-07-09 21:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
|
2019-08-06 00:46:20 +00:00
|
|
|
|
|
|
|
|
|
uint signature = reader.ReadUInt32();
|
|
|
|
|
reader.Position = 0;
|
2019-08-06 22:52:48 +00:00
|
|
|
|
return signature == BEMagic || signature == LEMagic;
|
2019-07-09 21:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type[] Types
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
return types.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<INode> nodes = new List<INode>();
|
|
|
|
|
|
2019-07-18 22:05:27 +00:00
|
|
|
|
public IEnumerable<ArchiveFileInfo> Files => null;
|
2019-07-09 21:50:50 +00:00
|
|
|
|
public IEnumerable<INode> Nodes => nodes;
|
|
|
|
|
|
2019-07-18 22:05:27 +00:00
|
|
|
|
public void ClearFiles() { nodes.Clear(); }
|
|
|
|
|
|
2019-07-09 21:50:50 +00:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return FileName; }
|
|
|
|
|
set { FileName = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
|
|
|
|
|
private bool IsBigEndian = false;
|
2019-07-09 21:50:50 +00:00
|
|
|
|
|
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new FileReader(stream))
|
|
|
|
|
{
|
|
|
|
|
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
|
2019-08-06 00:46:20 +00:00
|
|
|
|
|
2019-07-09 21:50:50 +00:00
|
|
|
|
uint Signature = reader.ReadUInt32();
|
2019-08-06 22:52:48 +00:00
|
|
|
|
IsBigEndian = Signature == BEMagic;
|
|
|
|
|
|
|
|
|
|
reader.SetByteOrder(IsBigEndian);
|
2019-07-09 21:50:50 +00:00
|
|
|
|
uint FirstNodeOffset = reader.ReadUInt32();
|
|
|
|
|
uint NodeSectionSize = reader.ReadUInt32();
|
|
|
|
|
uint FileDataOffset = reader.ReadUInt32();
|
|
|
|
|
byte[] Reserved = new byte[4];
|
|
|
|
|
|
2019-08-06 00:46:20 +00:00
|
|
|
|
Console.WriteLine("FirstNodeOffset " + FirstNodeOffset);
|
|
|
|
|
|
|
|
|
|
reader.SeekBegin(FirstNodeOffset);
|
2019-07-09 21:50:50 +00:00
|
|
|
|
var RootNode = new NodeEntry();
|
|
|
|
|
RootNode.Read(reader);
|
2019-08-06 00:46:20 +00:00
|
|
|
|
|
|
|
|
|
//Root has total number of nodes
|
|
|
|
|
uint TotalNodeCount = RootNode.Setting2;
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
//Read all our entries
|
2019-08-06 00:46:20 +00:00
|
|
|
|
List<NodeEntry> entries = new List<NodeEntry>();
|
|
|
|
|
entries.Add(RootNode);
|
|
|
|
|
for (int i = 0; i < TotalNodeCount - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
var node = new NodeEntry();
|
|
|
|
|
node.Read(reader);
|
|
|
|
|
entries.Add(node);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
//Read string pool
|
|
|
|
|
uint stringPoolPos = 0;
|
|
|
|
|
Dictionary<uint, string> StringTable = new Dictionary<uint, string>();
|
|
|
|
|
for (int i = 0; i < TotalNodeCount; i++)
|
|
|
|
|
{
|
|
|
|
|
string str = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated, Encoding.ASCII);
|
|
|
|
|
StringTable.Add(stringPoolPos, str);
|
|
|
|
|
stringPoolPos += (uint)str.Length + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Set the strings
|
|
|
|
|
for (int i = 0; i < TotalNodeCount; i++)
|
|
|
|
|
{
|
|
|
|
|
entries[i].Name = StringTable[entries[i].StringPoolOffset];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Setup our directory entries for loading to the tree
|
|
|
|
|
DirectoryEntry[] dirs = new DirectoryEntry[TotalNodeCount];
|
2019-08-06 00:46:20 +00:00
|
|
|
|
for (int i = 0; i < dirs.Length; i++)
|
|
|
|
|
dirs[i] = new DirectoryEntry();
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
DirectoryEntry currentDir = dirs[0];
|
2019-08-06 00:46:20 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < TotalNodeCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var node = entries[i];
|
2019-08-06 22:52:48 +00:00
|
|
|
|
Console.WriteLine($"node " + node.Name + " " + node.nodeType);
|
2019-08-06 00:46:20 +00:00
|
|
|
|
if (node.nodeType == NodeEntry.NodeType.Directory)
|
|
|
|
|
{
|
|
|
|
|
DirectoryEntry dir = new DirectoryEntry();
|
2019-08-06 22:52:48 +00:00
|
|
|
|
dir.Name = node.Name;
|
2019-08-06 00:46:20 +00:00
|
|
|
|
dir.nodeEntry = node;
|
|
|
|
|
dirs[node.Setting1].AddNode(dir);
|
|
|
|
|
currentDir = dir;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FileEntry entry = new FileEntry();
|
2019-08-06 22:52:48 +00:00
|
|
|
|
entry.FileName = node.Name;
|
|
|
|
|
entry.Name = node.Name;
|
2019-08-06 00:46:20 +00:00
|
|
|
|
entry.nodeEntry = node;
|
|
|
|
|
currentDir.nodes.Add(entry);
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
reader.SeekBegin(entry.nodeEntry.Setting1);
|
|
|
|
|
entry.FileData = reader.ReadBytes((int)entry.nodeEntry.Setting2);
|
2019-08-06 00:46:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
nodes.Add(currentDir);
|
2019-07-09 21:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveFile(FileWriter writer)
|
|
|
|
|
{
|
2019-08-06 22:52:48 +00:00
|
|
|
|
writer.SetByteOrder(IsBigEndian);
|
2019-07-09 21:50:50 +00:00
|
|
|
|
|
2019-08-06 22:52:48 +00:00
|
|
|
|
long pos = writer.Position;
|
|
|
|
|
writer.Write(BEMagic);
|
2019-07-09 21:50:50 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 00:46:20 +00:00
|
|
|
|
public class FileEntry : ArchiveFileInfo
|
|
|
|
|
{
|
|
|
|
|
public NodeEntry nodeEntry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DirectoryEntry : IDirectoryContainer
|
|
|
|
|
{
|
|
|
|
|
public NodeEntry nodeEntry;
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<INode> Nodes { get { return nodes; } }
|
|
|
|
|
public List<INode> nodes = new List<INode>();
|
|
|
|
|
|
|
|
|
|
public void AddNode(INode node)
|
|
|
|
|
{
|
|
|
|
|
nodes.Add(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 21:50:50 +00:00
|
|
|
|
public class NodeEntry : INode
|
|
|
|
|
{
|
|
|
|
|
public NodeType nodeType
|
|
|
|
|
{
|
|
|
|
|
get { return (NodeType)(flags >> 24); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum NodeType
|
|
|
|
|
{
|
|
|
|
|
File,
|
2019-08-06 00:46:20 +00:00
|
|
|
|
Directory,
|
2019-07-09 21:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 00:46:20 +00:00
|
|
|
|
public uint StringPoolOffset
|
2019-07-09 21:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
get { return flags & 0x00ffffff; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private uint flags;
|
|
|
|
|
|
|
|
|
|
public uint Setting1; //Offset (file) or parent index (directory)
|
|
|
|
|
public uint Setting2; //Size (file) or node count (directory)
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public void Read(FileReader reader)
|
|
|
|
|
{
|
|
|
|
|
flags = reader.ReadUInt32();
|
|
|
|
|
Setting1 = reader.ReadUInt32();
|
|
|
|
|
Setting2 = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 21:35:18 +00:00
|
|
|
|
public void Save(System.IO.Stream stream)
|
2019-07-09 21:50:50 +00:00
|
|
|
|
{
|
2019-08-06 21:35:18 +00:00
|
|
|
|
SaveFile(new FileWriter(stream));
|
2019-07-09 21:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|