1
0
mirror of synced 2025-01-19 01:14:08 +01:00

Add SP2 archive stuff

This commit is contained in:
KillzXGaming 2019-05-28 16:11:40 -04:00
parent 394d0e8d90
commit 6999a6debf
15 changed files with 443 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,367 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO;
namespace FirstPlugin
{
public class SP2 : IFileFormat, IArchiveFile
{
public FileType FileType { get; set; } = FileType.Archive;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Sonic Team Racing Archive" };
public string[] Extension { get; set; } = new string[] { "*.sp2" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Switch_Toolbox.Library.IO.FileReader(stream, true))
{
bool IsMatch = reader.ReadUInt32() == 0xD6D1820C;
reader.Position = 0;
return IsMatch || Utils.HasExtension(FileName, ".sp2");
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public List<FileInfo> files = new List<FileInfo>();
public IEnumerable<ArchiveFileInfo> Files => files;
public bool CanAddFiles { get; set; }
public bool CanRenameFiles { get; set; }
public bool CanReplaceFiles { get; set; }
public bool CanDeleteFiles { get; set; }
private const uint ChunkTextureFile = 0xD6D1820C;
private const uint ChunkMetaInfo = 0xB111B40E;
private const uint ChunkAnimInfo = 0x22008309;
private const uint ChunkAnimData = 0x29318F0A;
private const uint ChunkSkeletonData = 0x115AB800;
private const uint ChunkModelData = 0xCA121903;
private const uint ChunkShaderData = 0x777A9B0E;
private const uint ChunkMaterialData = 0x79C90901;
private List<ChunkHeader> Chunks = new List<ChunkHeader>();
public void Load(System.IO.Stream stream)
{
CanSave = false;
using (var reader = new FileReader(stream))
{
while (!reader.EndOfStream)
{
ChunkHeader chunk = new ChunkHeader();
chunk.Position = reader.Position;
chunk.Identifier = reader.ReadUInt32();
uint unk = reader.ReadUInt32();
chunk.ChunkSize = reader.ReadUInt32();
chunk.ChunkId = reader.ReadUInt32();
chunk.NextFilePtr = reader.ReadUInt32();
chunk.FileSize = reader.ReadUInt32();
uint unk2 = reader.ReadUInt32();
uint unk3 = reader.ReadUInt32();
Chunks.Add(chunk);
var Identifer = chunk.Identifier.Reverse();
switch (Identifer)
{
case ChunkTextureFile:
if (chunk.ChunkSize > 0x88)
{
reader.Seek(chunk.Position + 0x88, System.IO.SeekOrigin.Begin);
chunk.FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
break;
case ChunkMetaInfo:
break;
case ChunkAnimInfo:
if (chunk.ChunkSize > 0xB0)
{
// reader.Seek(chunk.Position + 0xB0, System.IO.SeekOrigin.Begin);
// chunk.FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
break;
case ChunkAnimData:
AnimationFile animFile = new AnimationFile();
animFile.Read(reader);
chunk.ChunkData = animFile;
break;
case ChunkSkeletonData:
SkeletonFile skelFile = new SkeletonFile();
skelFile.Read(reader);
chunk.ChunkData = skelFile;
break;
case ChunkModelData:
ModelFile modelFile = new ModelFile();
modelFile.Read(reader);
chunk.ChunkData = modelFile;
break;
case ChunkMaterialData:
MaterialFile matFile = new MaterialFile();
matFile.Read(reader);
chunk.ChunkData = matFile;
break;
}
reader.Seek(chunk.Position + chunk.ChunkSize, System.IO.SeekOrigin.Begin);
}
ReadGPUFile(FilePath);
}
}
private void ReadGPUFile(string FileName)
{
string path = FileName.Replace("cpu", "gpu");
if (!System.IO.File.Exists(path))
return;
int offset = 0;
//Read the data based on CPU chunk info
using (var reader = new FileReader(path))
{
for (int i = 0; i < Chunks.Count; i++)
{
if (Chunks[i].FileSize != 0 || Chunks[i].FileName != string.Empty || Chunks[i].ChunkData != null)
{
long pos = reader.Position;
var identifer = Chunks[i].Identifier.Reverse();
var fileInfo = new FileInfo();
//Get CPU chunk data
if (Chunks[i].ChunkData != null)
{
if ( Chunks[i].ChunkData is AnimationFile)
{
AnimationFile animFile = (AnimationFile)Chunks[i].ChunkData;
fileInfo.FileName = animFile.FileName;
fileInfo.FileData = animFile.Data;
}
if (Chunks[i].ChunkData is SkeletonFile)
{
SkeletonFile animFile = (SkeletonFile)Chunks[i].ChunkData;
fileInfo.FileName = animFile.FileName;
fileInfo.FileData = animFile.Data;
}
if (Chunks[i].ChunkData is MaterialFile)
{
MaterialFile animFile = (MaterialFile)Chunks[i].ChunkData;
fileInfo.FileName = animFile.FileName;
fileInfo.FileData = animFile.Data;
}
if (Chunks[i].ChunkData is MaterialFile)
{
MaterialFile animFile = (MaterialFile)Chunks[i].ChunkData;
fileInfo.FileName = animFile.FileName;
fileInfo.FileData = animFile.Data;
}
if (Chunks[i].ChunkData is ModelFile)
{
ModelFile modelFile = (ModelFile)Chunks[i].ChunkData;
fileInfo.FileName = modelFile.FileName;
byte[] BufferData = new byte[0];
if (Chunks[i].FileSize != 0)
BufferData = reader.ReadBytes((int)Chunks[i].FileSize);
fileInfo.FileData = Utils.CombineByteArray(modelFile.Data, modelFile.Data2, BufferData);
//Don't advance the stream unless the chunk has a pointer
if (Chunks[i].NextFilePtr != 0)
reader.Seek(pos + Chunks[i].NextFilePtr, System.IO.SeekOrigin.Begin);
}
}
else //Else get the data from GPU
{
if (Chunks[i].FileName != string.Empty)
fileInfo.FileName = $"{Chunks[i].FileName}";
else
fileInfo.FileName = $"{i} {Chunks[i].ChunkId} {identifer.ToString("X")}";
if (Chunks[i].FileSize != 0)
fileInfo.FileData = reader.ReadBytes((int)Chunks[i].FileSize);
else
fileInfo.FileData = new byte[0];
}
files.Add(fileInfo);
//Don't advance the stream unless the chunk has a pointer
if (Chunks[i].NextFilePtr != 0)
reader.Seek(pos + Chunks[i].NextFilePtr, System.IO.SeekOrigin.Begin);
}
}
}
}
public void Unload()
{
}
public interface IChunkData { }
public class ChunkHeader
{
public IChunkData ChunkData;
public uint Identifier;
public long Position;
public uint ChunkSize;
public uint ChunkId;
public uint NextFilePtr;
public uint FileSize;
public string FileName = "";
}
//Info in CPU file about the model
//Note the GPU file chunk linked from this contains the buffers
public class ModelFile : IChunkData
{
public string FileName = "";
public string FileName2 = ""; //Yeah there's another file for some reason
public byte[] Data;
public byte[] Data2;
public void Read(FileReader reader)
{
long pos = reader.Position;
uint unk3 = reader.ReadUInt32();
uint unk4 = reader.ReadUInt32(); //Set to 1
uint SectionSize = reader.ReadUInt32(); //At the end, the file name
uint Padding = reader.ReadUInt32();
uint NextSectionOffset = reader.ReadUInt32();
reader.Seek(pos, System.IO.SeekOrigin.Begin);
//Model FILE
Data = reader.ReadBytes((int)SectionSize);
FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
//Todo
Data2 = new byte[0];
// reader.Seek(NextSectionOffset, System.IO.SeekOrigin.Begin);
// Data2 = reader.ReadBytes();
}
}
public class MaterialFile : IChunkData
{
public string FileName = "";
public byte[] Data;
public void Read(FileReader reader)
{
long pos = reader.Position;
uint unk3 = reader.ReadUInt32();
uint unk4 = reader.ReadUInt32(); //Set to 1
uint SectionSize = reader.ReadUInt32(); //At the end, the file name
uint Padding = reader.ReadUInt32();
reader.Seek(pos, System.IO.SeekOrigin.Begin);
//Material FILE
Data = reader.ReadBytes((int)SectionSize);
FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
}
public class SkeletonFile : IChunkData
{
public string FileName = "";
public byte[] Data;
public void Read(FileReader reader)
{
long pos = reader.Position;
uint unk3 = reader.ReadUInt32();
uint unk4 = reader.ReadUInt32(); //Set to 1
uint SectionSize = reader.ReadUInt32(); //At the end, the file name
uint Padding = reader.ReadUInt32();
reader.Seek(pos, System.IO.SeekOrigin.Begin);
//SKEL FILE
Data = reader.ReadBytes((int)SectionSize);
FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
}
public class AnimationFile : IChunkData
{
public string FileName = "";
public byte[] Data;
public void Read(FileReader reader)
{
long pos = reader.Position;
uint Hash = reader.ReadUInt32(); //Maybe a hash? Idk
uint unk4 = reader.ReadUInt32(); //Set to 1
uint SectionSize = reader.ReadUInt32(); //At the end, the file name
uint Padding = reader.ReadUInt32();
reader.Seek(pos, System.IO.SeekOrigin.Begin);
//ANIM FILE
Data = reader.ReadBytes((int)SectionSize);
FileName = reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
}
public class TextureFile : IChunkData
{
}
public class TextureInfo : IChunkData
{
}
public byte[] Save()
{
var mem = new System.IO.MemoryStream();
return mem.ToArray();
}
public bool AddFile(ArchiveFileInfo archiveFileInfo)
{
return false;
}
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
{
return false;
}
public class FileInfo : ArchiveFileInfo
{
}
}
}

View File

@ -294,6 +294,7 @@ namespace FirstPlugin
Formats.Add(typeof(TMPK));
Formats.Add(typeof(TEX3DS));
Formats.Add(typeof(NXARC));
Formats.Add(typeof(SP2));
Formats.Add(typeof(Turbo.Course_MapCamera_bin));
Formats.Add(typeof(Turbo.PartsBIN));

View File

@ -193,6 +193,7 @@
<Compile Include="FileFormats\Archives\APAK.cs" />
<Compile Include="FileFormats\Archives\NXARC.cs" />
<Compile Include="FileFormats\Archives\RARC.cs" />
<Compile Include="FileFormats\Archives\SP2.cs" />
<Compile Include="FileFormats\Archives\TMPK.cs" />
<Compile Include="FileFormats\Audio\Archives\BARS.cs" />
<Compile Include="FileFormats\Audio\BARSLIST.cs" />

View File

@ -1 +1 @@
b1cc2c6fe1714d994e4d77917f9fc78ba61787cc
fdc3ae3441a26fcd02916e04a1344635998d8023

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Switch_Toolbox.Library.IO
{
public static class IOExtensions
{
public static uint Reverse(this uint x)
{
// swap adjacent 16-bit blocks
x = (x >> 16) | (x << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
}
}
}

View File

@ -357,13 +357,61 @@ namespace Switch_Toolbox.Library
public override void OnDoubleMouseClick(TreeView treeview)
{
TreeNode node = STFileLoader.GetNodeFileFormat(Text, ArchiveFileInfo.FileData, true, this);
ArchiveFileInfo.FileFormat = (IFileFormat)node;
if (node != null)
ReplaceNode(this.Parent, this, node);
IFileFormat file = STFileLoader.OpenFileFormat(Text, ArchiveFileInfo.FileData, true);
if (file == null) //Format not supported so return
return;
ArchiveFileInfo.FileFormat = file;
if (Utils.HasInterface(file.GetType(), typeof(IEditor<>)))
{
OpenFormDialog(file);
}
else if (file != null)
ReplaceNode(this.Parent, this, (TreeNode)file);
}
public override void OnClick(TreeView treeView)
private void OpenFormDialog(IFileFormat fileFormat)
{
STForm form = GetEditorForm(fileFormat);
var parentForm = LibraryGUI.Instance.GetActiveForm();
form.Text = (((IFileFormat)fileFormat).FileName);
form.FormClosing += (sender, e) => FormClosing(sender, e, fileFormat);
form.Show(parentForm);
}
private void FormClosing(object sender, EventArgs args, IFileFormat fileFormat)
{
if (((Form)sender).DialogResult != DialogResult.OK)
return;
if (fileFormat.CanSave)
{
ArchiveFileInfo.FileData = fileFormat.Save();
UpdateHexView();
}
}
private STForm GetEditorForm(IFileFormat fileFormat)
{
Type objectType = fileFormat.GetType();
foreach (var inter in objectType.GetInterfaces())
{
if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IEditor<>))
{
System.Reflection.MethodInfo method = objectType.GetMethod("OpenForm");
return (STForm)method.Invoke(fileFormat, new object[0]);
}
}
return null;
}
public override void OnClick(TreeView treeView) {
UpdateHexView();
}
private void UpdateHexView()
{
HexEditor editor = (HexEditor)LibraryGUI.Instance.GetActiveContent(typeof(HexEditor));
if (editor == null)

View File

@ -557,6 +557,7 @@
<Compile Include="IO\DWord.cs" />
<Compile Include="IO\FileReader.cs" />
<Compile Include="IO\FileWriter.cs" />
<Compile Include="IO\IOExtensions.cs" />
<Compile Include="IO\Matrix4x4.cs" />
<Compile Include="IO\STFileLoader.cs" />
<Compile Include="IO\STFileSaver.cs" />