2021-10-08 22:35:36 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2021-10-11 23:00:33 +02:00
|
|
|
|
using System.Windows.Forms;
|
2021-10-08 22:35:36 +02:00
|
|
|
|
using Toolbox.Library;
|
|
|
|
|
using Toolbox.Library.IO;
|
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
2021-10-11 23:00:33 +02:00
|
|
|
|
public class MTXT : TreeNodeFile, IFileFormat, ITextureContainer, IContextMenuNode
|
2021-10-08 22:35:36 +02:00
|
|
|
|
{
|
|
|
|
|
public FileType FileType { get; set; } = FileType.Image;
|
|
|
|
|
|
2021-10-10 20:06:08 +02:00
|
|
|
|
public bool CanSave { get; set; } = true;
|
2021-10-08 22:35:36 +02:00
|
|
|
|
public string[] Description { get; set; } = new string[] { "MTXT" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.bctex" };
|
|
|
|
|
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 FileReader(stream, true)) {
|
|
|
|
|
return reader.CheckSignature(4, "MTXT");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type[] Types
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
return types.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnAfterAdded()
|
|
|
|
|
{
|
|
|
|
|
if (Nodes.Count > 0 && this.TreeView != null)
|
|
|
|
|
this.TreeView.SelectedNode = Nodes[0];
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 23:00:33 +02:00
|
|
|
|
public ToolStripItem[] GetContextMenuItems()
|
|
|
|
|
{
|
|
|
|
|
return new ToolStripItem[]
|
|
|
|
|
{
|
|
|
|
|
new ToolStripMenuItem("Save", null, Save, Keys.Control | Keys.S),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Save(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
|
|
|
sfd.DefaultExt = "bctex";
|
|
|
|
|
sfd.Filter = "Supported Formats|*.bctex;";
|
|
|
|
|
sfd.FileName = FileName;
|
|
|
|
|
|
|
|
|
|
if (sfd.ShowDialog() == DialogResult.OK) {
|
|
|
|
|
STFileSaver.SaveFileFormat(this, sfd.FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-09 22:46:21 +02:00
|
|
|
|
public List<STGenericTexture> TextureList { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool DisplayIcons => false;
|
|
|
|
|
|
2021-10-10 20:06:08 +02:00
|
|
|
|
XTX TextureFile;
|
|
|
|
|
|
|
|
|
|
uint HeaderFlags;
|
|
|
|
|
ulong UnkData;
|
|
|
|
|
int TextureFlag;
|
|
|
|
|
|
2021-10-08 22:35:36 +02:00
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
Text = this.FileName;
|
2021-10-11 23:00:33 +02:00
|
|
|
|
Tag = this;
|
2021-10-08 22:35:36 +02:00
|
|
|
|
|
|
|
|
|
using (var reader = new FileReader(stream))
|
|
|
|
|
{
|
|
|
|
|
reader.ReadSignature(4, "MTXT");
|
2021-10-10 20:06:08 +02:00
|
|
|
|
HeaderFlags = reader.ReadUInt32();
|
|
|
|
|
var comp = reader.ReadBytes((int)reader.BaseStream.Length - 8);
|
2021-10-08 22:35:36 +02:00
|
|
|
|
var decomp = STLibraryCompression.GZIP.Decompress(comp);
|
|
|
|
|
ReadTextureBinary(decomp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-10 20:06:08 +02:00
|
|
|
|
|
2021-10-08 22:35:36 +02:00
|
|
|
|
private void ReadTextureBinary(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new FileReader(data))
|
|
|
|
|
{
|
|
|
|
|
//Note offsets are -8 relative due to decomp data and header being 8 bytes long
|
2021-10-10 20:06:08 +02:00
|
|
|
|
UnkData = reader.ReadUInt64();
|
2021-10-08 22:35:36 +02:00
|
|
|
|
uint w = reader.ReadUInt32(); //width
|
|
|
|
|
uint h = reader.ReadUInt32(); //height
|
|
|
|
|
reader.ReadUInt32(); //num mips
|
2021-10-10 20:06:08 +02:00
|
|
|
|
TextureFlag = reader.ReadInt32(); //unk (-1)
|
2021-10-08 22:35:36 +02:00
|
|
|
|
uint nameOffset = reader.ReadUInt32(); //name offset
|
|
|
|
|
reader.ReadUInt32();
|
|
|
|
|
var textureOffset = reader.ReadUInt32(); //texture xtx offset
|
|
|
|
|
reader.ReadUInt32();
|
|
|
|
|
var textureSize = reader.ReadUInt32(); //texture xtx size
|
|
|
|
|
|
|
|
|
|
string textureName = "";
|
|
|
|
|
using (reader.TemporarySeek(nameOffset - 8, System.IO.SeekOrigin.Begin)) {
|
|
|
|
|
textureName = reader.ReadZeroTerminatedString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stream = new SubStream(reader.BaseStream, (long)textureOffset - 8, (long)textureSize);
|
2021-10-10 20:06:08 +02:00
|
|
|
|
TextureFile = new XTX();
|
|
|
|
|
TextureFile.FileName = textureName;
|
|
|
|
|
TextureFile.Load(stream);
|
|
|
|
|
foreach (STGenericTexture node in TextureFile.Nodes)
|
2021-10-08 22:35:36 +02:00
|
|
|
|
Nodes.Add(node);
|
2021-10-09 22:46:21 +02:00
|
|
|
|
|
2021-10-10 20:06:08 +02:00
|
|
|
|
TextureList = TextureFile.TextureList;
|
2021-10-08 22:35:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(System.IO.Stream stream)
|
|
|
|
|
{
|
2021-10-10 20:06:08 +02:00
|
|
|
|
using (var writer = new FileWriter(stream))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteSignature("MTXT");
|
|
|
|
|
writer.Write(HeaderFlags);
|
|
|
|
|
writer.Write(STLibraryCompression.GZIP.Compress(WriteTextureBinary()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] WriteTextureBinary()
|
|
|
|
|
{
|
|
|
|
|
//Save XTX file binary
|
|
|
|
|
var binaryMem = new System.IO.MemoryStream();
|
|
|
|
|
TextureFile.Save(binaryMem);
|
|
|
|
|
var rawTextureFile = binaryMem.ToArray();
|
2021-10-08 22:35:36 +02:00
|
|
|
|
|
2021-10-10 20:06:08 +02:00
|
|
|
|
var mem = new System.IO.MemoryStream();
|
|
|
|
|
using (var writer = new FileWriter(mem))
|
|
|
|
|
{
|
|
|
|
|
//Write file header
|
|
|
|
|
writer.Write(UnkData);
|
|
|
|
|
writer.Write(TextureFile.TextureList[0].Width);
|
|
|
|
|
writer.Write(TextureFile.TextureList[0].Height);
|
|
|
|
|
writer.Write(TextureFile.TextureList[0].MipCount);
|
|
|
|
|
writer.Write(TextureFlag);
|
|
|
|
|
writer.Write(uint.MaxValue); //reserve name offset
|
|
|
|
|
writer.Write(0);
|
|
|
|
|
writer.Write(uint.MaxValue); //data offset
|
|
|
|
|
writer.Write(0);
|
|
|
|
|
writer.Write(rawTextureFile.Length); //data size
|
|
|
|
|
for (int i = 0; i < 19; i++)
|
|
|
|
|
writer.Write(0xFFFFFFFF);
|
|
|
|
|
|
|
|
|
|
//Write xtx file data
|
|
|
|
|
writer.WriteUint32Offset(32, -8);
|
|
|
|
|
writer.Write(rawTextureFile);
|
|
|
|
|
|
|
|
|
|
writer.WriteUint32Offset(24, -8);
|
|
|
|
|
writer.WriteString(Text);
|
|
|
|
|
}
|
|
|
|
|
return mem.ToArray();
|
2021-10-08 22:35:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|