Switch-Toolbox/File_Format_Library/FileFormats/Archives/ME01.cs

200 lines
6.2 KiB
C#
Raw Normal View History

2019-07-01 00:29:00 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Toolbox;
2019-07-01 00:29:00 +00:00
using System.Windows.Forms;
using Toolbox.Library;
using Toolbox.Library.IO;
2019-07-01 00:29:00 +00:00
namespace FirstPlugin
{
public class ME01 : IArchiveFile, IFileFormat
{
public FileType FileType { get; set; } = FileType.Archive;
public bool CanSave { get; set; }
2019-07-04 22:12:58 +00:00
public string[] Description { get; set; } = new string[] { "ME01", "SA01" };
public string[] Extension { get; set; } = new string[] { "*.bin", "*.sgarc" };
2019-07-01 00:29:00 +00:00
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; }
2019-07-01 01:27:05 +00:00
public bool CanReplaceFiles { get; set; } = true;
2019-07-01 00:29:00 +00:00
public bool CanDeleteFiles { get; set; }
2019-07-04 22:12:58 +00:00
private bool IsSA01 = false;
2019-07-01 00:29:00 +00:00
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
2019-07-01 00:29:00 +00:00
{
2019-07-04 22:12:58 +00:00
bool IsMEO1 = reader.CheckSignature(4, "ME01");
IsSA01 = reader.CheckSignature(4, "SA01");
return IsMEO1 || IsSA01;
2019-07-01 00:29:00 +00:00
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public List<FileEntry> files = new List<FileEntry>();
public IEnumerable<ArchiveFileInfo> Files => files;
2019-07-01 00:50:56 +00:00
private uint Alignment;
2019-07-01 00:29:00 +00:00
public void Load(System.IO.Stream stream)
{
2019-07-01 00:50:56 +00:00
CanSave = true;
2019-07-01 00:29:00 +00:00
using (var reader = new FileReader(stream))
{
2019-07-04 22:12:58 +00:00
if (IsSA01)
reader.SetByteOrder(true);
else
reader.SetByteOrder(false);
uint Signature = reader.ReadUInt32();
2019-07-01 00:29:00 +00:00
uint FileCount = reader.ReadUInt32();
2019-07-01 00:50:56 +00:00
Alignment = reader.ReadUInt32();
2019-07-01 00:29:00 +00:00
uint[] DataOffsets = reader.ReadUInt32s((int)FileCount);
uint[] Sizes = reader.ReadUInt32s((int)FileCount);
string[] FileNames = new string[FileCount];
for (int i = 0; i < FileCount; i++)
{
FileNames[i] = reader.ReadZeroTerminatedString();
while (true)
{
if (reader.ReadByte() != 0x30)
{
reader.Seek(-1);
break;
}
}
}
long DataPosition = reader.Position;
for (int i = 0; i < FileCount; i++)
{
//reader.SeekBegin(DataPosition + DataOffsets[i]);
var file = new FileEntry();
file.FileName = FileNames[i];
file.FileData = reader.ReadBytes((int)Sizes[i]);
files.Add(file);
while (true)
{
if (reader.EndOfStream)
break;
if (reader.ReadByte() != 0x30)
{
reader.Seek(-1);
break;
}
}
}
}
}
public void Unload()
{
}
public byte[] Save()
{
2019-07-01 00:50:56 +00:00
var mem = new System.IO.MemoryStream();
using (var writer = new FileWriter(mem))
{
2019-07-04 22:12:58 +00:00
if (IsSA01)
writer.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
else
writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
if (IsSA01)
writer.WriteSignature("SA01");
else
writer.WriteSignature("ME01");
2019-07-01 00:50:56 +00:00
writer.Write(files.Count);
writer.Write(Alignment);
writer.Write(new uint[files.Count]); //Save space for offsets
for (int i = 0; i < files.Count; i++)
{
files[i].SaveFileFormat();
2019-07-01 00:50:56 +00:00
writer.Write(files[i].FileData.Length);
}
2019-07-01 00:50:56 +00:00
for (int i = 0; i < files.Count; i++)
{
writer.WriteString(files[i].FileName);
//Fixed 128 string length
if (i != files.Count - 1)
{
2019-07-01 01:06:22 +00:00
int PaddingCount = 128 - files[i].FileName.Length - 1;
2019-07-01 00:50:56 +00:00
for (int p = 0; p < PaddingCount; p++)
writer.Write((byte)0x30);
}
else //Else align normally
{
Align(writer, (int)Alignment);
}
}
uint[] Offsets = new uint[files.Count];
long DataStart = writer.Position;
for (int i = 0; i < files.Count; i++)
{
Offsets[i] = (uint)(writer.Position - DataStart);
writer.Write(files[i].FileData);
Align(writer, (int)Alignment);
}
writer.Seek(12, System.IO.SeekOrigin.Begin);
writer.Write(Offsets);
}
return mem.ToArray();
}
private void Align(FileWriter writer, int alignment)
{
var startPos = writer.Position;
long position = writer.Seek((-writer.Position % alignment + alignment) % alignment, System.IO.SeekOrigin.Current);
writer.Seek(startPos, System.IO.SeekOrigin.Begin);
while (writer.Position != position)
{
writer.Write((byte)0x30);
}
2019-07-01 00:29:00 +00:00
}
public bool AddFile(ArchiveFileInfo archiveFileInfo)
{
return false;
}
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
{
return false;
}
public class FileEntry : ArchiveFileInfo
{
}
}
}