1
0
mirror of synced 2025-03-01 16:10:52 +01:00

EFFN ptcl fixes and add saving back

This commit is contained in:
KillzXGaming 2019-08-17 09:58:17 -04:00
parent 31ec93411f
commit 3769cc12c2
10 changed files with 331 additions and 213 deletions

Binary file not shown.

View File

@ -6,12 +6,14 @@ using System.Threading.Tasks;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Toolbox.Library; using Toolbox.Library;
using Toolbox.Library.IO; using Toolbox.Library.IO;
using Toolbox.Library.Forms;
using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using FirstPlugin.Forms; using FirstPlugin.Forms;
namespace DKCTF namespace DKCTF
{ {
public class MSBT : TreeNodeFile, IFileFormat public class MSBT : TreeNodeFile, IFileFormat, ILeaveOpenOnLoad
{ {
public FileType FileType { get; set; } = FileType.Layout; public FileType FileType { get; set; } = FileType.Layout;
@ -42,13 +44,17 @@ namespace DKCTF
} }
} }
private Stream _stream;
public void Load(System.IO.Stream stream) public void Load(System.IO.Stream stream)
{ {
using (var reader = new Toolbox.Library.IO.FileReader(stream)) _stream = stream;
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
{ {
reader.SetByteOrder(true); reader.SetByteOrder(true);
var header = reader.ReadStruct<MSBTHeader>(); var header = reader.ReadStruct<MSBTHeader>();
Text = FileName;
//parse the data //parse the data
int index = 0; int index = 0;
while (!reader.EndOfStream) while (!reader.EndOfStream)
@ -57,8 +63,10 @@ namespace DKCTF
long startPos = reader.Position; long startPos = reader.Position;
reader.SeekBegin(startPos + (int)chunk.DataOffset); reader.SeekBegin(startPos + (int)chunk.DataOffset);
byte[] chunkData = reader.ReadBytes((int)chunk.DataSize); var subStream = new SubStream(_stream, reader.Position, (long)chunk.DataSize);
Nodes.Add(new MessageEntry(chunkData, index++, chunk.ChunkType)); Nodes.Add(new MessageEntry(subStream, index++, chunk.ChunkType));
reader.SeekBegin(startPos + (int)chunk.DataOffset + (int)chunk.DataSize);
} }
} }
} }
@ -67,19 +75,40 @@ namespace DKCTF
} }
public class MessageEntry : TreeNodeCustom public class MessageEntry : TreeNodeCustom, IContextMenuNode
{ {
FirstPlugin.MSBT msbt; FirstPlugin.MSBT msbt;
public MessageEntry(byte[] data, int index, string type) private Stream stream;
public MessageEntry(Stream data, int index, string type)
{ {
Text = $"Message Entry ({type}) {index}"; stream = data;
var chunkFile = STFileLoader.OpenFileFormat(Text, data); Text = $"{type}.msbt";
var chunkFile = STFileLoader.OpenFileFormat(data, Text);
if (chunkFile != null && chunkFile is FirstPlugin.MSBT) if (chunkFile != null && chunkFile is FirstPlugin.MSBT)
msbt = (FirstPlugin.MSBT)chunkFile; msbt = (FirstPlugin.MSBT)chunkFile;
} }
public ToolStripItem[] GetContextMenuItems()
{
List<ToolStripItem> Items = new List<ToolStripItem>();
Items.Add(new STToolStipMenuItem("Export", null, ExportAction, Keys.Control | Keys.E));
return Items.ToArray();
}
private void ExportAction(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = Utils.GetAllFilters(typeof(MSBT));
sfd.FileName = Text;
if (sfd.ShowDialog() == DialogResult.OK)
{
stream.ExportToFile(sfd.FileName);
}
}
public override void OnClick(TreeView treeview) public override void OnClick(TreeView treeview)
{ {
if (msbt != null) if (msbt != null)

View File

@ -5,10 +5,11 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Toolbox.Library.IO; using Toolbox.Library.IO;
using Toolbox.Library; using Toolbox.Library;
using System.Windows.Forms;
namespace FirstPlugin namespace FirstPlugin
{ {
public class EFF : TreeNodeFile,IFileFormat public class EFF : TreeNodeFile,IFileFormat, IContextMenuNode
{ {
public FileType FileType { get; set; } = FileType.Effect; public FileType FileType { get; set; } = FileType.Effect;
@ -36,19 +37,54 @@ namespace FirstPlugin
} }
} }
private PTCL ptcl;
private byte[] DataStart;
public void Load(System.IO.Stream stream) public void Load(System.IO.Stream stream)
{ {
Text = FileName; Text = FileName;
CanSave = true;
FileReader reader = new FileReader(stream); FileReader reader = new FileReader(stream);
reader.Seek(4096, System.IO.SeekOrigin.Begin); int SectionSize = 0;
while (true)
{
string magicCheck = reader.ReadString(4, Encoding.ASCII);
if (magicCheck == "VFXB")
{
reader.Seek(-4);
break;
}
PTCL pctl = new PTCL(); SectionSize += 4;
pctl.Text = "Output.pctl"; }
Nodes.Add(pctl);
PTCL.Header Header = new PTCL.Header(); DataStart = reader.getSection(0, SectionSize);
Header.Read(reader, pctl);
Text = FileName;
ptcl = new PTCL();
ptcl.IFileInfo = new IFileInfo();
ptcl.FileName = "Output.pctl";
Nodes.Add(ptcl);
ptcl.Load(new SubStream(stream, reader.Position));
}
public ToolStripItem[] GetContextMenuItems()
{
return new ToolStripItem[]
{
new ToolStripMenuItem("Save", null, SaveAction, Keys.Control | Keys.S),
};
}
private void SaveAction(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = Utils.GetAllFilters(typeof(EFF));
sfd.FileName = FileName;
if (sfd.ShowDialog() == DialogResult.OK)
STFileSaver.SaveFileFormat(this, sfd.FileName);
} }
public void Unload() public void Unload()
@ -58,6 +94,13 @@ namespace FirstPlugin
public void Save(System.IO.Stream stream) public void Save(System.IO.Stream stream)
{ {
using (var writer = new FileWriter(stream))
{
writer.Write(DataStart);
var mem = new System.IO.MemoryStream();
ptcl.Save(mem);
writer.Write(mem.ToArray());
}
} }
} }
} }

View File

@ -64,8 +64,8 @@ namespace FirstPlugin
Text = FileName; Text = FileName;
CanSave = true; CanSave = true;
FileReader reader = new FileReader(new MemoryStream(data)); using (var reader = new FileReader(stream))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
string Signature = reader.ReadString(4, Encoding.ASCII); string Signature = reader.ReadString(4, Encoding.ASCII);
@ -96,6 +96,7 @@ namespace FirstPlugin
reader.Dispose(); reader.Dispose();
stream.Close(); stream.Close();
stream.Dispose(); stream.Dispose();
}
ContextMenuStrip = new STContextMenuStrip(); ContextMenuStrip = new STContextMenuStrip();
ContextMenuStrip.Items.Add(new ToolStripMenuItem("Save", null, Save, Keys.Control | Keys.S)); ContextMenuStrip.Items.Add(new ToolStripMenuItem("Save", null, Save, Keys.Control | Keys.S));
@ -116,11 +117,8 @@ namespace FirstPlugin
} }
private void Save(object sender, EventArgs args) private void Save(object sender, EventArgs args)
{ {
List<IFileFormat> formats = new List<IFileFormat>();
formats.Add(this);
SaveFileDialog sfd = new SaveFileDialog(); SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = Utils.GetAllFilters(formats); sfd.Filter = Utils.GetAllFilters(typeof(PTCL));
sfd.FileName = FileName; sfd.FileName = FileName;
if (sfd.ShowDialog() == DialogResult.OK) if (sfd.ShowDialog() == DialogResult.OK)

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Toolbox.Library.IO
{
public static class StreamExport
{
public static void ExportToFile(this Stream stream, string fileName)
{
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
{
stream.CopyTo(fileStream);
}
}
}
}

View File

@ -132,16 +132,35 @@ namespace Toolbox.Library.IO
/// <returns></returns> /// <returns></returns>
public static IFileFormat OpenFileFormat(string FileName, byte[] data = null, bool LeaveStreamOpen = false, bool InArchive = false, public static IFileFormat OpenFileFormat(string FileName, byte[] data = null, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0, uint DecompressedSize = 0, uint CompressedSize = 0) TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0, uint DecompressedSize = 0, uint CompressedSize = 0)
{
Stream stream;
if (data != null)
stream = new MemoryStream(data);
else
stream = File.OpenRead(FileName);
return OpenFileFormat(stream, FileName, data, LeaveStreamOpen, InArchive,
archiveNode, Compressed, CompType, DecompressedSize, CompressedSize);
}
/// <summary>
/// Gets the <see cref="IFileFormat"/> from a file or byte array.
/// </summary>
/// <param name="FileName">The name of the file</param>
/// <param name="data">The byte array of the data</param>
/// <param name="InArchive">If the file is in an archive so it can be saved back</param>
/// <param name="archiveNode">The node being replaced from an archive</param>
/// <param name="Compressed">If the file is being compressed or not</param>
/// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
/// <returns></returns>
public static IFileFormat OpenFileFormat(Stream stream, string FileName, byte[] data = null, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0, uint DecompressedSize = 0, uint CompressedSize = 0)
{ {
uint DecompressedFileSize = 0; uint DecompressedFileSize = 0;
uint CompressedFileSize = 0; uint CompressedFileSize = 0;
FileReader fileReader; using (var fileReader = new FileReader(stream, true))
if (data != null) {
fileReader = new FileReader(data);
else
fileReader = new FileReader(FileName);
if (CompType == CompressionType.None) if (CompType == CompressionType.None)
DecompressedFileSize = (uint)fileReader.BaseStream.Length; DecompressedFileSize = (uint)fileReader.BaseStream.Length;
if (CompType != CompressionType.None) if (CompType != CompressionType.None)
@ -167,12 +186,6 @@ namespace Toolbox.Library.IO
fileReader.Position = 4; fileReader.Position = 4;
ushort MagicHex3 = fileReader.ReadUInt16(); ushort MagicHex3 = fileReader.ReadUInt16();
Stream stream;
if (data != null)
stream = new MemoryStream(data);
else
stream = File.OpenRead(FileName);
//Note this method will soon be how all compression formats are handled rather than being checked here //Note this method will soon be how all compression formats are handled rather than being checked here
foreach (ICompressionFormat compressionFormat in FileManager.GetCompressionFormats()) foreach (ICompressionFormat compressionFormat in FileManager.GetCompressionFormats())
{ {
@ -314,9 +327,7 @@ namespace Toolbox.Library.IO
return OpenFileFormat(FileName, data, InArchive, LeaveStreamOpen, archiveNode, true, return OpenFileFormat(FileName, data, InArchive, LeaveStreamOpen, archiveNode, true,
CompressionType.Lz4f, DecompressedFileSize, CompressedFileSize); CompressionType.Lz4f, DecompressedFileSize, CompressedFileSize);
} }
}
fileReader.Close();
fileReader.Dispose();
foreach (IFileFormat fileFormat in FileManager.GetFileFormats()) foreach (IFileFormat fileFormat in FileManager.GetFileFormats())
{ {

View File

@ -27,6 +27,22 @@ namespace Toolbox.Library.IO
this.length = length; this.length = length;
baseOffset = offset; baseOffset = offset;
} }
public SubStream(Stream baseStream, long offset)
{
long length = baseStream.Length - offset;
if (baseStream == null) throw new ArgumentNullException("baseStream");
if (!baseStream.CanRead) throw new ArgumentException("baseStream.CanRead is false");
if (!baseStream.CanSeek) throw new ArgumentException("baseStream.CanSeek is false");
if (offset < 0) throw new ArgumentOutOfRangeException("offset");
if (offset + length > baseStream.Length) throw new ArgumentOutOfRangeException("length");
this.baseStream = baseStream;
this.length = length;
baseOffset = offset;
}
public override int Read(byte[] buffer, int offset, int count) public override int Read(byte[] buffer, int offset, int count)
{ {
baseStream.Position = baseOffset + offset + Position; baseStream.Position = baseOffset + offset + Position;

View File

@ -291,6 +291,7 @@
<Compile Include="Interfaces\ModelData\IMeshContainer.cs" /> <Compile Include="Interfaces\ModelData\IMeshContainer.cs" />
<Compile Include="Interfaces\Textures\ITextureContainer.cs" /> <Compile Include="Interfaces\Textures\ITextureContainer.cs" />
<Compile Include="Interfaces\Forms\IUpdateForm.cs" /> <Compile Include="Interfaces\Forms\IUpdateForm.cs" />
<Compile Include="IO\Extensions\StreamExport.cs" />
<Compile Include="IO\Extensions\UintExtension.cs" /> <Compile Include="IO\Extensions\UintExtension.cs" />
<Compile Include="IO\FileStreamStorage.cs" /> <Compile Include="IO\FileStreamStorage.cs" />
<Compile Include="IO\IOComoon.cs" /> <Compile Include="IO\IOComoon.cs" />