1
0
mirror of synced 2024-12-11 23:36:01 +01:00
Switch-Toolbox/File_Format_Library/FileFormats/DKCTF/MSBT.cs

140 lines
4.3 KiB
C#
Raw Normal View History

2019-08-17 00:07:03 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Toolbox.Library;
using Toolbox.Library.IO;
2019-08-17 15:58:17 +02:00
using Toolbox.Library.Forms;
using System.IO;
2019-08-17 00:07:03 +02:00
using System.Windows.Forms;
using FirstPlugin.Forms;
namespace DKCTF
{
2019-08-17 15:58:17 +02:00
public class MSBT : TreeNodeFile, IFileFormat, ILeaveOpenOnLoad
2019-08-17 00:07:03 +02:00
{
public FileType FileType { get; set; } = FileType.Layout;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "MSBT (DKCTF)" };
public string[] Extension { get; set; } = new string[] { "*.MSBT" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
if (stream.Length <= 24)
return false;
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
{
return reader.CheckSignature(4, "MSBT", 20);
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
2019-08-17 15:58:17 +02:00
private Stream _stream;
2019-08-17 00:07:03 +02:00
public void Load(System.IO.Stream stream)
{
2019-08-17 15:58:17 +02:00
_stream = stream;
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
2019-08-17 00:07:03 +02:00
{
reader.SetByteOrder(true);
var header = reader.ReadStruct<MSBTHeader>();
2019-08-17 15:58:17 +02:00
Text = FileName;
2019-08-17 00:07:03 +02:00
//parse the data
int index = 0;
while (!reader.EndOfStream)
{
CChunkDescriptor chunk = reader.ReadStruct<CChunkDescriptor>();
long startPos = reader.Position;
reader.SeekBegin(startPos + (int)chunk.DataOffset);
2019-08-17 15:58:17 +02:00
var subStream = new SubStream(_stream, reader.Position, (long)chunk.DataSize);
Nodes.Add(new MessageEntry(subStream, index++, chunk.ChunkType));
reader.SeekBegin(startPos + (int)chunk.DataOffset + (int)chunk.DataSize);
2019-08-17 00:07:03 +02:00
}
}
}
public void Unload()
{
}
2019-08-17 15:58:17 +02:00
public class MessageEntry : TreeNodeCustom, IContextMenuNode
2019-08-17 00:07:03 +02:00
{
FirstPlugin.MSBT msbt;
2019-08-17 15:58:17 +02:00
private Stream stream;
public MessageEntry(Stream data, int index, string type)
2019-08-17 00:07:03 +02:00
{
2019-08-17 15:58:17 +02:00
stream = data;
2019-08-17 00:07:03 +02:00
2019-08-17 15:58:17 +02:00
Text = $"{type}.msbt";
var chunkFile = STFileLoader.OpenFileFormat(data, Text);
2019-08-17 00:07:03 +02:00
if (chunkFile != null && chunkFile is FirstPlugin.MSBT)
msbt = (FirstPlugin.MSBT)chunkFile;
}
2019-08-17 15:58:17 +02:00
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);
}
}
2019-08-17 00:07:03 +02:00
public override void OnClick(TreeView treeview)
{
if (msbt != null)
{
MSBTEditor editor = (MSBTEditor)LibraryGUI.GetActiveContent(typeof(MSBTEditor));
if (editor == null)
{
editor = new MSBTEditor();
editor.Dock = DockStyle.Fill;
LibraryGUI.LoadEditor(editor);
}
editor.LoadMSBT(msbt);
}
}
}
public void Save(System.IO.Stream stream)
{
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class MSBTHeader
{
CFormDescriptor PackForm;
}
}