1
0
mirror of synced 2024-11-12 02:00:50 +01:00

Simpify BNTX deswizzling and start to do XTX saving.

This commit is contained in:
KillzXGaming 2019-04-30 15:25:42 -04:00
parent 3e9802fd05
commit f2c50f7dd7
27 changed files with 1047 additions and 400 deletions

Binary file not shown.

View File

@ -71,19 +71,13 @@ namespace FirstPlugin
public ushort ByteOrderMark;
public ushort Padding;
public ushort Unknown;
public Encoding StringEncoding = Encoding.Unicode;
public EncodingType StringEncoding;
public byte Version;
public List<MSBTEntry> entries = new List<MSBTEntry>();
byte[] Reserved = new byte[10];
public enum EncodingType : byte
{
UTF8 = 0,
UTF16 = 1,
}
public LBL1 Label1;
public NLI1 NLI1;
public TXT2 Text2;
@ -95,13 +89,15 @@ namespace FirstPlugin
ByteOrderMark = reader.ReadUInt16();
reader.CheckByteOrderMark(ByteOrderMark);
Padding = reader.ReadUInt16();
StringEncoding = reader.ReadEnum<EncodingType>(true);
byte encoding = reader.ReadByte();
Version = reader.ReadByte();
ushort SectionCount = reader.ReadUInt16();
ushort Unknown = reader.ReadUInt16();
uint FileSize = reader.ReadUInt32();
Reserved = reader.ReadBytes(10);
StringEncoding = (encoding == 0x01 ? Encoding.BigEndianUnicode : Encoding.UTF8);
for (int i = 0; i < SectionCount; i++)
{
long pos = reader.Position;
@ -113,10 +109,6 @@ namespace FirstPlugin
switch (Signature)
{
// Label1 = new LBL1();
// Label1.Read(reader, this);
// entries.Add(Label1);
case "NLI1":
NLI1 = new NLI1();
NLI1.Read(reader, this);
@ -127,10 +119,14 @@ namespace FirstPlugin
Text2.Read(reader, this);
entries.Add(Text2);
break;
case "LBL1":
Label1 = new LBL1();
Label1.Read(reader, this);
entries.Add(Label1);
break;
case "ATR1":
case "ATO1":
case "TSY1":
case "LBL1":
default:
MSBTEntry entry = new MSBTEntry();
entry.Signature = Signature;
@ -158,7 +154,7 @@ namespace FirstPlugin
writer.WriteSignature("MsgStdBn");
writer.Write(ByteOrderMark);
writer.Write(Padding);
writer.Write(StringEncoding, true);
writer.Write(StringEncoding == Encoding.UTF8 ? (byte)0 : (byte)1);
writer.Write(Version);
writer.Write((ushort)entries.Count);
writer.Write(Unknown);
@ -178,10 +174,68 @@ namespace FirstPlugin
}
}
public class LabelGroup
{
public uint NumberOfLabels;
public uint Offset;
}
public class LabelEntry : MSBTEntry
{
private uint _index;
public uint Length;
public string Name;
public uint Checksum;
public StringEntry String;
public uint Index
{
get { return _index; }
set { _index = value; }
}
public byte[] Value
{
get { return String.Data; }
set { String.Data = value; }
}
}
public class StringEntry : MSBTEntry
{
private uint _index;
public StringEntry(byte[] data)
{
Data = data;
}
public uint Index
{
get { return _index; }
set { _index = value; }
}
public string GetTextLabel(bool ShowText, Encoding encoding)
{
if (ShowText)
return $"{_index + 1} {GetText(encoding)}";
else
return $"{_index + 1}";
}
public string GetText(Encoding encoding)
{
return encoding.GetString(Data);
}
}
public class TXT2 : MSBTEntry
{
public uint[] Offsets;
public List<string> TextData = new List<string>();
public List<StringEntry> TextData = new List<StringEntry>();
public List<StringEntry> OriginalTextData = new List<StringEntry>();
public override void Read(FileReader reader, Header header)
{
@ -193,31 +247,26 @@ namespace FirstPlugin
for (int i = 0; i < EntryCount; i++)
{
if (i == 0)
reader.Position = Offsets[i] + Position;
ReadMessageString(reader);
ReadMessageString(reader, (uint)i);
}
}
private void ReadMessageString(FileReader reader)
private void ReadMessageString(FileReader reader, uint index)
{
List<char> chars = new List<char>();
List<byte> chars = new List<byte>();
short charCheck = reader.ReadInt16();
short charCheck = reader.ReadInt16();
while (charCheck != 0)
{
if (charCheck == 0x0E) //Control code
{
chars.AddRange(GetControlCode(reader));
}
else
chars.Add((char)charCheck);
chars.Add((byte)(charCheck >> 8));
chars.Add((byte)(charCheck & 255));
charCheck = reader.ReadInt16();
}
TextData.Add(new string(chars.ToArray()));
TextData.Add(new StringEntry(chars.ToArray()) { Index = index, });
OriginalTextData.Add(new StringEntry(chars.ToArray()) { Index = index, });
}
private char[] GetControlCode(FileReader reader)
@ -403,31 +452,38 @@ namespace FirstPlugin
public class LBL1 : MSBTEntry
{
public List<Tuple<string, int>> Labels = null;
public List<LabelGroup> Groups = new List<LabelGroup>();
public List<LabelEntry> Labels = new List<LabelEntry>();
public override void Read(FileReader reader, Header header)
{
Labels = new List<Tuple<string, int>>();
Padding = reader.ReadBytes(8);
long pos = reader.Position;
EntryCount = reader.ReadUInt32();
for (int i = 0; i < EntryCount; i++)
{
uint LabelCount = reader.ReadUInt32();
uint Offset = reader.ReadUInt32();
LabelGroup group = new LabelGroup();
group.NumberOfLabels = reader.ReadUInt32();
group.Offset = reader.ReadUInt32();
Groups.Add(group);
}
using (reader.TemporarySeek(Offset))
foreach (LabelGroup group in Groups)
{
reader.Seek(pos + group.Offset, SeekOrigin.Begin);
for (int i = 0; i < group.NumberOfLabels; i++)
{
for (int l = 0; l < LabelCount; l++)
{
byte stringSize = reader.ReadByte();
string Text = reader.ReadString(stringSize, Encoding.ASCII);
int index = reader.ReadInt32();
Labels.Add(Tuple.Create(Text, index));
}
LabelEntry entry = new LabelEntry();
entry.Length = reader.ReadByte();
entry.Name = reader.ReadString((int)entry.Length);
entry.Index = reader.ReadUInt32();
entry.Checksum = (uint)Groups.IndexOf(group);
Labels.Add(entry);
}
}
reader.Align(8);
}
public override void Write(FileWriter writer, Header header)
@ -435,14 +491,14 @@ namespace FirstPlugin
writer.WriteSignature(Signature);
writer.Write(Data.Length);
writer.Write(Padding);
for (int i = 0; i < Groups.Count; i++)
{
}
}
}
public class LabelEntry
{
}
public class MSBTEntry
{
public byte[] Data;

View File

@ -354,7 +354,7 @@ namespace FirstPlugin
Format = tex.Format;
NutFormat = ConvertGenericToNutFormat(tex.Format);
mipSizes = GenerateMipSizes();
mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);
}
surfacesNew.Clear();
@ -364,65 +364,7 @@ namespace FirstPlugin
}
}
private List<uint[]> GenerateMipSizes(uint SurfaceCount = 1)
{
List<uint[]> MipMapSizes = new List<uint[]>();
uint bpp = GetBytesPerPixel(Format);
uint blkWidth = GetBlockWidth(Format);
uint blkHeight = GetBlockHeight(Format);
uint blkDepth = GetBlockDepth(Format);
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
uint Pitch = 0;
uint DataAlignment = 512;
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
uint ArrayCount = (uint)mipSizes.Count;
uint ArrayOffset = 0;
for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++)
{
uint SurfaceSize = 0;
int blockHeightShift = 0;
uint[] MipOffsets = new uint[MipCount];
for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
{
uint width = (uint)Math.Max(1, Width >> mipLevel);
uint height = (uint)Math.Max(1, Height >> mipLevel);
uint depth = (uint)Math.Max(1, Depth >> mipLevel);
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
blockHeightShift += 1;
uint width__ = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth);
uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight);
//Calculate the mip size instead
byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)];
SurfaceSize += (uint)AlignedData.Length;
MipOffsets[mipLevel] = (SurfaceSize);
//Get the first mip offset and current one and the total image size
int msize = (int)((MipOffsets[0] + ImageData.Length - MipOffsets[mipLevel]) / ArrayCount);
Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64);
SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);
}
ArrayOffset += (uint)(ImageData.Length / ArrayCount);
MipMapSizes.Add(MipOffsets);
}
return MipMapSizes;
}
private void Export(object sender, EventArgs args)
{
@ -643,83 +585,7 @@ namespace FirstPlugin
if (!IsSwizzled)
return DDS.GetArrayFaces(this, ImageData,1)[ArrayLevel].mipmaps[0];
int target = 1;
uint bpp = GetBytesPerPixel(Format);
uint blkWidth = GetBlockWidth(Format);
uint blkHeight = GetBlockHeight(Format);
uint blkDepth = GetBlockDepth(Format);
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
uint Pitch = 0;
uint DataAlignment = 512;
uint TileMode = 0;
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
uint ArrayCount = (uint)mipSizes.Count;
uint ArrayOffset = 0;
for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++)
{
uint SurfaceSize = 0;
int blockHeightShift = 0;
List<uint> MipOffsets = new List<uint>();
for (int mipLevel = 0; mipLevel < mipSizes[arrayLevel].Length; mipLevel++)
{
uint width = (uint)Math.Max(1, Width >> mipLevel);
uint height = (uint)Math.Max(1, Height >> mipLevel);
uint depth = (uint)Math.Max(1, Depth >> mipLevel);
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
blockHeightShift += 1;
uint width__ = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth);
uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight);
//Calculate the mip size instead
byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)];
SurfaceSize += (uint)AlignedData.Length;
MipOffsets.Add(SurfaceSize);
//Get the first mip offset and current one and the total image size
int msize = (int)((MipOffsets[0] + ImageData.Length - MipOffsets[mipLevel]) / ArrayCount);
byte[] data_ = Utils.SubArray(ImageData, ArrayOffset + MipOffsets[mipLevel], (uint)msize);
try
{
Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64);
SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);
Console.WriteLine($"{width} {height} {blkWidth} {blkHeight} {target} {bpp} {TileMode} {(int)Math.Max(0, BlockHeightLog2 - blockHeightShift)} {data_.Length}");
byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, target, bpp, TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), data_);
//Create a copy and use that to remove uneeded data
byte[] result_ = new byte[size];
Array.Copy(result, 0, result_, 0, size);
result = null;
if (ArrayLevel == arrayLevel && MipLevel == mipLevel)
return result_;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show($"Failed to swizzle texture {Text}!");
Console.WriteLine(e);
return new byte[0];
}
}
ArrayOffset += (uint)(ImageData.Length / ArrayCount);
}
return new byte[0];
return TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, 1);
}
MenuItem save = new MenuItem("Save");
MenuItem export = new MenuItem("Export");

View File

@ -40,16 +40,37 @@ namespace FirstPlugin
public void Load(System.IO.Stream stream)
{
CanSave = true;
Text = FileName;
LoadFile(stream);
ContextMenuStrip = new STContextMenuStrip();
ContextMenuStrip.Items.Add(new ToolStripMenuItem("Save", null, SaveAction, Keys.Control | Keys.S));
}
private void SaveAction(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xtx";
sfd.Filter = "Supported Formats|*.xtx;";
sfd.FileName = FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
STFileSaver.SaveFileFormat(this, sfd.FileName);
}
}
public void Unload()
{
}
public byte[] Save()
{
return null;
MemoryStream mem = new MemoryStream();
SaveFile(new FileWriter(mem));
return mem.ToArray();
}
public class XTXFormats
@ -127,12 +148,19 @@ namespace FirstPlugin
public uint HeaderSize { get; set; }
public uint MajorVersion { get; set; }
public uint MinorVersion { get; set; }
public BlockHeader blockHeader { get; set; }
public List<BlockHeader> Blocks { get; set; }
public List<TextureInfo> TextureInfos { get; set; }
public List<byte[]> TextureBlocks { get; set; }
private const int texHeadBlkType = 2;
private const int dataBlkType = 3;
public void LoadFile(Stream data)
{
Blocks = new List<BlockHeader>();
TextureInfos = new List<TextureInfo>();
TextureBlocks = new List<byte[]>();
FileReader reader = new FileReader(data);
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "DFvN")
@ -142,12 +170,104 @@ namespace FirstPlugin
MajorVersion = reader.ReadUInt32();
MinorVersion = reader.ReadUInt32();
blockHeader = new BlockHeader();
blockHeader.Read(reader);
Nodes.Add(blockHeader.textureInfo);
uint TextureBlockType = 2;
uint DataBlockType = 3;
reader.Seek(HeaderSize, SeekOrigin.Begin);
bool blockB = false;
bool blockC = false;
uint ImageInfo = 0;
uint images = 0;
while (reader.Position < reader.BaseStream.Length)
{
Console.WriteLine("BLOCK POS " + reader.Position);
BlockHeader blockHeader = new BlockHeader();
blockHeader.Read(reader);
Blocks.Add(blockHeader);
if ((uint)blockHeader.BlockType == TextureBlockType)
{
ImageInfo += 1;
blockB = true;
TextureInfo textureInfo = new TextureInfo();
textureInfo.Read(new FileReader(blockHeader.Data));
textureInfo.Text = "Texture " + ImageInfo;
TextureInfos.Add(textureInfo);
}
if ((uint)blockHeader.BlockType == DataBlockType)
{
images += 1;
blockC = true;
TextureBlocks.Add(blockHeader.Data);
}
}
reader.Close();
reader.Dispose();
int curTex = 0;
foreach (var tex in TextureInfos) {
tex.ImageData = TextureBlocks[curTex++];
Nodes.Add(tex);
}
}
public void SaveFile(FileWriter writer)
{
SetupBlockSaving();
int Alignment = 512;
uint TextureInfoType = 2;
uint TextureBlockType = 3;
writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
writer.WriteSignature("DFvN");
writer.Write(HeaderSize);
writer.Write(MajorVersion);
writer.Write(MinorVersion);
writer.Seek(HeaderSize, SeekOrigin.Begin);
int curTexBlock = 0;
int curTexImage = 0;
foreach (var block in Blocks)
{
if (block.BlockType == TextureBlockType)
{
block.Data = TextureBlocks[curTexBlock++];
block.WriteHeader(writer);
block.WriteBlock(writer, Alignment);
}
else if (block.BlockType == TextureInfoType)
{
block.Data = TextureInfos[curTexImage++].Write();
block.WriteHeader(writer);
block.WriteBlock(writer, Alignment);
}
else
{
block.WriteHeader(writer);
block.WriteBlock(writer, 0x24);
}
}
writer.Close();
writer.Dispose();
}
private void SetupBlockSaving()
{
foreach (var textureInfo in TextureInfos)
{
}
}
public class BlockHeader
@ -157,11 +277,13 @@ namespace FirstPlugin
public uint BlockType { get; set; }
public uint GlobalBlockIndex { get; set; }
public uint IncBlockTypeIndex { get; set; }
public TextureInfo textureInfo { get; set; }
public long DataOffset;
public byte[] Data;
public void Read(FileReader reader)
{
var pos = reader.Position;
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "HBvN")
throw new Exception($"Invalid signature {Signature}! Expected HBvN.");
@ -173,16 +295,40 @@ namespace FirstPlugin
GlobalBlockIndex = reader.ReadUInt32();
IncBlockTypeIndex = reader.ReadUInt32();
int indx = 0;
if (BlockType == texHeadBlkType)
{
textureInfo = new TextureInfo();
textureInfo.Read(reader);
textureInfo.Text = "Texture " + indx++;
}
if (BlockType == dataBlkType)
{
reader.Seek((int)pos + DataOffset, SeekOrigin.Begin);
Data = reader.ReadBytes((int)DataSize);
}
private long headerPos = 0;
public void WriteHeader(FileWriter writer)
{
headerPos = writer.Position;
writer.WriteSignature("HBvN");
writer.Write(BlockSize);
writer.Write(Data.Length);
writer.Write(DataOffset);
writer.Write(BlockType);
writer.Write(BlockType);
writer.Write(GlobalBlockIndex);
writer.Write(IncBlockTypeIndex);
}
public void WriteBlock(FileWriter writer, int Alignment)
{
DataOffset = writer.Position;
using (writer.TemporarySeek(headerPos + 12, SeekOrigin.Begin)) {
writer.Write(DataOffset);
}
writer.Seek((int)(BlockSize + headerPos), SeekOrigin.Begin);
writer.Write(Data);
writer.Align(Alignment);
uint TotalDataSize =(uint)(writer.Position - (BlockSize + headerPos));
using (writer.TemporarySeek(headerPos + 8, SeekOrigin.Begin)) {
writer.Write(TotalDataSize);
}
}
}
@ -212,19 +358,19 @@ namespace FirstPlugin
}
}
public override bool CanEdit { get; set; } = false;
public override bool CanEdit { get; set; } = true;
public UInt64 DataSize { get; set; }
public uint Alignment { get; set; }
public uint Depth { get; set; }
public uint Target { get; set; }
public XTXFormats.XTXImageFormat XTXFormat { get; set; }
public uint MipCount { get; set; }
public uint SliceSize { get; set; }
public uint[] MipOffsets { get; set; }
public BlockHeader DataBlockHeader { get; set; }
public byte[] ImageData;
public override string ExportFilter => FileFilters.XTX;
public override string ReplaceFilter => FileFilters.XTX;
public void Read(FileReader reader)
{
DataSize = reader.ReadUInt64();
@ -234,34 +380,71 @@ namespace FirstPlugin
Depth = reader.ReadUInt32();
Target = reader.ReadUInt32();
XTXFormat = reader.ReadEnum<XTXFormats.XTXImageFormat>(true);
Format = ConvertFormat(XTXFormat);
MipCount = reader.ReadUInt32();
SliceSize = reader.ReadUInt32();
Format = ConvertFormat(XTXFormat);
ArrayCount = 1;
SliceSize = reader.ReadUInt32();
long offPos = reader.Position;
MipOffsets = reader.ReadUInt32s((int)MipCount);
ContextMenuStrip = new STContextMenuStrip();
ContextMenuStrip.Items.Add(new ToolStripMenuItem("Export", null, ExportAction, Keys.Control | Keys.E));
ContextMenuStrip.Items.Add(new ToolStripMenuItem("Replace", null, ReplaceAction, Keys.Control | Keys.R));
}
reader.Seek(offPos + 68, SeekOrigin.Begin);
byte[] Layout = reader.ReadBytes(8);
byte Sparse = reader.ReadByte();
reader.Seek(3);
long DataBlockOff = reader.Position;
public byte[] Write()
{
MemoryStream mem = new MemoryStream();
DataBlockHeader = new BlockHeader();
DataBlockHeader.Read(reader);
FileWriter writer = new FileWriter(mem);
writer.Write(DataSize);
writer.Write(Alignment);
writer.Write(Width);
writer.Write(Height);
writer.Write(Depth);
writer.Write(Target);
writer.Write(XTXFormat, true);
writer.Write(MipCount);
writer.Write(SliceSize);
writer.Close();
writer.Dispose();
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset, SeekOrigin.Begin);
long datastart = reader.Position;
ImageData = reader.ReadBytes((int)DataSize);
return mem.ToArray();
}
if (ImageData.Length == 0)
throw new System.Exception("Empty data size!");
public override void Export(string FileName)
{
Export(FileName);
}
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset + (long)DataBlockHeader.DataSize, SeekOrigin.Begin);
BlockHeader EndBlockHeader = new BlockHeader();
EndBlockHeader.Read(reader); }
public override void Replace(string FileName)
{
var bntxFile = new BNTX();
var tex = new TextureData();
tex.Replace(FileName, MipCount, Format);
//If it's null, the operation is cancelled
if (tex.Texture == null)
return;
var surfacesNew = tex.GetSurfaces();
var surfaces = GetSurfaces();
ImageData = tex.Texture.TextureData[0][0];
Width = tex.Texture.Width;
Height = tex.Texture.Height;
MipCount = tex.Texture.MipCount;
Format = tex.Format;
XTXFormat = ConvertFromGenericFormat(tex.Format);
MipOffsets = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length)[0];
surfacesNew.Clear();
surfaces.Clear();
UpdateEditor();
}
public override void OnClick(TreeView treeview)
{
@ -281,6 +464,29 @@ namespace FirstPlugin
editor.LoadImage(this);
}
private static XTXFormats.XTXImageFormat ConvertFromGenericFormat(TEX_FORMAT Format)
{
switch (Format)
{
case TEX_FORMAT.BC1_UNORM: return XTXFormats.XTXImageFormat.DXT1;
case TEX_FORMAT.BC2_UNORM: return XTXFormats.XTXImageFormat.DXT3;
case TEX_FORMAT.BC3_UNORM: return XTXFormats.XTXImageFormat.DXT5;
case TEX_FORMAT.BC4_UNORM: return XTXFormats.XTXImageFormat.BC4U;
case TEX_FORMAT.BC4_SNORM: return XTXFormats.XTXImageFormat.BC4S;
case TEX_FORMAT.BC5_UNORM: return XTXFormats.XTXImageFormat.BC5U;
case TEX_FORMAT.R8_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_R8;
case TEX_FORMAT.R8G8_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RG8;
case TEX_FORMAT.R10G10B10A2_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGB10A2;
case TEX_FORMAT.B5G6R5_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGB565;
case TEX_FORMAT.B5G5R5A1_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGB5A1;
case TEX_FORMAT.B4G4R4A4_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA4;
case TEX_FORMAT.R8G8B8A8_UNORM: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA8;
case TEX_FORMAT.R8G8B8A8_UNORM_SRGB: return XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA8_SRGB;
default:
throw new Exception($"Cannot convert format {Format}");
}
}
private static TEX_FORMAT ConvertFormat(XTXFormats.XTXImageFormat Format)
{
switch (Format)
@ -307,75 +513,50 @@ namespace FirstPlugin
public override void SetImageData(Bitmap bitmap, int ArrayLevel)
{
throw new NotImplementedException("Cannot set image data! Operation not implemented!");
if (bitmap == null)
return; //Image is likely disposed and not needed to be applied
MipCount = GenerateMipCount(bitmap.Width, bitmap.Height);
XTXFormat = ConvertFromGenericFormat(Format);
var tex = new Syroot.NintenTools.NSW.Bntx.Texture();
tex.Height = (uint)bitmap.Height;
tex.Width = (uint)bitmap.Width;
tex.Format = TextureData.GenericToBntxSurfaceFormat(Format);
tex.Name = Text;
tex.Path = "";
tex.TextureData = new List<List<byte[]>>();
STChannelType[] channels = SetChannelsByFormat(Format);
tex.sparseBinding = 0; //false
tex.sparseResidency = 0; //false
tex.Flags = 0;
tex.Swizzle = 0;
tex.textureLayout = 0;
tex.Regs = new uint[0];
tex.AccessFlags = 0x20;
tex.ArrayLength = (uint)ArrayLevel;
tex.MipCount = MipCount;
tex.Depth = Depth;
tex.Dim = Syroot.NintenTools.NSW.Bntx.GFX.Dim.Dim2D;
tex.TileMode = Syroot.NintenTools.NSW.Bntx.GFX.TileMode.Default;
tex.textureLayout2 = 0x010007;
tex.SurfaceDim = Syroot.NintenTools.NSW.Bntx.GFX.SurfaceDim.Dim2D;
tex.SampleCount = 1;
tex.Pitch = 32;
tex.MipOffsets = new long[tex.MipCount];
var mipmaps = TextureImporterSettings.SwizzleSurfaceMipMaps(tex,
GenerateMipsAndCompress(bitmap, Format), MipCount);
ImageData = Utils.CombineByteArray(mipmaps.ToArray());
}
public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
{
uint bpp = GetBytesPerPixel(Format);
uint blkWidth = GetBlockHeight(Format);
uint blkHeight = GetBlockWidth(Format);
uint blkDepth = GetBlockDepth(Format);
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
int TileMode = 0;
List<byte[]> mips = new List<byte[]>();
int blockHeightShift = 0;
for (int mipLevel = 0; mipLevel < MipOffsets.Length; mipLevel++)
{
uint width = (uint)Math.Max(1, Width >> mipLevel);
uint height = (uint)Math.Max(1, Height >> mipLevel);
uint depth = (uint)Math.Max(1, Depth >> mipLevel);
// uint size = width * height * bpp;
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
byte[] Output = new byte[size];
uint mipOffset;
if (mipLevel != 0)
{
mipOffset = (MipOffsets[mipLevel - 1]);
if (mipLevel == 1)
mipOffset -= (uint)size;
Array.Copy(ImageData, mipOffset, Output, 0, size);
}
else
Output = ImageData;
byte[] output = new byte[size];
Console.WriteLine(mipLevel + " " + size);
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
blockHeightShift += 1;
byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), Output);
//Create a copy and use that to remove uneeded data
byte[] result_ = new byte[size];
Array.Copy(result, 0, result_, 0, size);
result = null;
if (MipLevel == mipLevel)
return result_;
Console.WriteLine("bpp " + bpp);
Console.WriteLine("result_ " + size);
Console.WriteLine("width " + width);
Console.WriteLine("height " + height);
break;
}
return new byte[0];
return TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, (int)Target);
}
}
}

View File

@ -29,19 +29,35 @@
private void InitializeComponent()
{
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.textEditor1 = new Switch_Toolbox.Library.Forms.TextEditor();
this.listViewCustom1 = new Switch_Toolbox.Library.Forms.ListViewCustom();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.editTextTB = new Switch_Toolbox.Library.Forms.STTextBox();
this.stLabel1 = new Switch_Toolbox.Library.Forms.STLabel();
this.stLabel2 = new Switch_Toolbox.Library.Forms.STLabel();
this.originalTextTB = new Switch_Toolbox.Library.Forms.STTextBox();
this.stLabel3 = new Switch_Toolbox.Library.Forms.STLabel();
this.hexEditor1 = new Switch_Toolbox.Library.Forms.HexEditor();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.splitContainer3 = new System.Windows.Forms.SplitContainer();
this.contentContainer.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer3)).BeginInit();
this.splitContainer3.Panel1.SuspendLayout();
this.splitContainer3.Panel2.SuspendLayout();
this.splitContainer3.SuspendLayout();
this.SuspendLayout();
//
// contentContainer
//
this.contentContainer.Controls.Add(this.splitContainer1);
this.contentContainer.Size = new System.Drawing.Size(916, 520);
this.contentContainer.Controls.SetChildIndex(this.splitContainer1, 0);
//
// splitContainer1
@ -56,20 +72,11 @@
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textEditor1);
this.splitContainer1.Size = new System.Drawing.Size(543, 368);
this.splitContainer1.SplitterDistance = 181;
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(916, 495);
this.splitContainer1.SplitterDistance = 305;
this.splitContainer1.TabIndex = 11;
//
// textEditor1
//
this.textEditor1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textEditor1.IsXML = false;
this.textEditor1.Location = new System.Drawing.Point(0, 0);
this.textEditor1.Name = "textEditor1";
this.textEditor1.Size = new System.Drawing.Size(358, 368);
this.textEditor1.TabIndex = 0;
//
// listViewCustom1
//
this.listViewCustom1.BorderStyle = System.Windows.Forms.BorderStyle.None;
@ -79,7 +86,7 @@
this.listViewCustom1.Location = new System.Drawing.Point(0, 0);
this.listViewCustom1.Name = "listViewCustom1";
this.listViewCustom1.OwnerDraw = true;
this.listViewCustom1.Size = new System.Drawing.Size(181, 368);
this.listViewCustom1.Size = new System.Drawing.Size(305, 495);
this.listViewCustom1.TabIndex = 0;
this.listViewCustom1.UseCompatibleStateImageBehavior = false;
this.listViewCustom1.View = System.Windows.Forms.View.Details;
@ -89,11 +96,111 @@
//
this.columnHeader1.Width = 181;
//
// editTextTB
//
this.editTextTB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.editTextTB.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.editTextTB.Location = new System.Drawing.Point(3, 16);
this.editTextTB.Multiline = true;
this.editTextTB.Name = "editTextTB";
this.editTextTB.Size = new System.Drawing.Size(200, 286);
this.editTextTB.TabIndex = 0;
//
// stLabel1
//
this.stLabel1.AutoSize = true;
this.stLabel1.Location = new System.Drawing.Point(0, 0);
this.stLabel1.Name = "stLabel1";
this.stLabel1.Size = new System.Drawing.Size(28, 13);
this.stLabel1.TabIndex = 1;
this.stLabel1.Text = "Edit:";
//
// stLabel2
//
this.stLabel2.AutoSize = true;
this.stLabel2.Location = new System.Drawing.Point(3, 0);
this.stLabel2.Name = "stLabel2";
this.stLabel2.Size = new System.Drawing.Size(45, 13);
this.stLabel2.TabIndex = 2;
this.stLabel2.Text = "Original:";
//
// originalTextTB
//
this.originalTextTB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.originalTextTB.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.originalTextTB.Location = new System.Drawing.Point(3, 16);
this.originalTextTB.Multiline = true;
this.originalTextTB.Name = "originalTextTB";
this.originalTextTB.ReadOnly = true;
this.originalTextTB.Size = new System.Drawing.Size(385, 286);
this.originalTextTB.TabIndex = 3;
//
// stLabel3
//
this.stLabel3.AutoSize = true;
this.stLabel3.Location = new System.Drawing.Point(13, 4);
this.stLabel3.Name = "stLabel3";
this.stLabel3.Size = new System.Drawing.Size(55, 13);
this.stLabel3.TabIndex = 4;
this.stLabel3.Text = "Hex View:";
//
// hexEditor1
//
this.hexEditor1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.hexEditor1.Location = new System.Drawing.Point(13, 29);
this.hexEditor1.Name = "hexEditor1";
this.hexEditor1.Size = new System.Drawing.Size(591, 154);
this.hexEditor1.TabIndex = 5;
//
// splitContainer2
//
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.splitContainer3);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.hexEditor1);
this.splitContainer2.Panel2.Controls.Add(this.stLabel3);
this.splitContainer2.Size = new System.Drawing.Size(607, 495);
this.splitContainer2.SplitterDistance = 305;
this.splitContainer2.TabIndex = 6;
//
// splitContainer3
//
this.splitContainer3.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer3.Location = new System.Drawing.Point(0, 0);
this.splitContainer3.Name = "splitContainer3";
//
// splitContainer3.Panel1
//
this.splitContainer3.Panel1.Controls.Add(this.editTextTB);
this.splitContainer3.Panel1.Controls.Add(this.stLabel1);
//
// splitContainer3.Panel2
//
this.splitContainer3.Panel2.Controls.Add(this.originalTextTB);
this.splitContainer3.Panel2.Controls.Add(this.stLabel2);
this.splitContainer3.Size = new System.Drawing.Size(607, 305);
this.splitContainer3.SplitterDistance = 202;
this.splitContainer3.TabIndex = 0;
//
// MSBTEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(549, 398);
this.ClientSize = new System.Drawing.Size(922, 525);
this.Name = "MSBTEditor";
this.Text = "MSBTEditor";
this.contentContainer.ResumeLayout(false);
@ -101,6 +208,17 @@
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.splitContainer3.Panel1.ResumeLayout(false);
this.splitContainer3.Panel1.PerformLayout();
this.splitContainer3.Panel2.ResumeLayout(false);
this.splitContainer3.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer3)).EndInit();
this.splitContainer3.ResumeLayout(false);
this.ResumeLayout(false);
}
@ -110,6 +228,13 @@
private System.Windows.Forms.SplitContainer splitContainer1;
private Switch_Toolbox.Library.Forms.ListViewCustom listViewCustom1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private Switch_Toolbox.Library.Forms.TextEditor textEditor1;
private Switch_Toolbox.Library.Forms.HexEditor hexEditor1;
private Switch_Toolbox.Library.Forms.STLabel stLabel3;
private Switch_Toolbox.Library.Forms.STTextBox originalTextTB;
private Switch_Toolbox.Library.Forms.STLabel stLabel2;
private Switch_Toolbox.Library.Forms.STLabel stLabel1;
private Switch_Toolbox.Library.Forms.STTextBox editTextTB;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.SplitContainer splitContainer3;
}
}

View File

@ -28,6 +28,8 @@ namespace FirstPlugin.Forms
listViewCustom1.HeaderStyle = ColumnHeaderStyle.None;
listViewCustom1.FullRowSelect = true;
listViewCustom1.CanResizeList = false;
hexEditor1.EnableMenuBar = false;
}
MSBT activeMessageFile;
@ -40,7 +42,7 @@ namespace FirstPlugin.Forms
{
foreach (var text in msbt.header.Text2.TextData)
{
string listText = text;
string listText = text.GetTextLabel(ShowPreviewText, msbt.header.StringEncoding);
if (listText.Length > 25)
listText = $"{listText.Substring(0, 25)}......";
@ -62,7 +64,9 @@ namespace FirstPlugin.Forms
var textSection = activeMessageFile.header.Text2;
if (textSection != null)
{
textEditor1.FillEditor(textSection.TextData[index]);
editTextTB.Text = textSection.TextData[index].GetText(activeMessageFile.header.StringEncoding);
originalTextTB.Text = textSection.OriginalTextData[index].GetText(activeMessageFile.header.StringEncoding);
hexEditor1.LoadData(textSection.TextData[index].Data);
}
}
}

View File

@ -34,6 +34,7 @@ namespace FirstPlugin
public static string FSHA = GetFilter(".bfspa");
public static string NUTEXB = GetFilter(".dds",".png", ".tga", ".jpg", ".tiff", ".tif", ".gif");
public static string XTX = GetFilter(".dds", ".png", ".tga", ".jpg", ".tiff", ".tif", ".gif");
public static string GetFilter(Type type, object CheckAnimEffect = null, bool IsExporting = false)
{

View File

@ -13,6 +13,23 @@ namespace Switch_Toolbox.Library.Forms
{
public partial class HexEditor : UserControl
{
FindOptions _findOptions = new FindOptions();
public bool EnableMenuBar
{
set
{
if (value)
stContextMenuStrip1.Show();
else
stContextMenuStrip1.Hide();
}
get
{
return stContextMenuStrip1.Visible;
}
}
public HexEditor()
{
InitializeComponent();
@ -30,6 +47,9 @@ namespace Switch_Toolbox.Library.Forms
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
SearchHex searchHex = new SearchHex();
searchHex.HexBox = hexBox1;
searchHex.FindOptions = _findOptions;
if (searchHex.ShowDialog() == DialogResult.OK)
{
FindOptions options = new FindOptions();

View File

@ -28,46 +28,68 @@
/// </summary>
private void InitializeComponent()
{
this.stTextBox1 = new Switch_Toolbox.Library.Forms.STTextBox();
this.chkBoxMatchCase = new Switch_Toolbox.Library.Forms.STCheckBox();
this.stButton1 = new Switch_Toolbox.Library.Forms.STButton();
this.components = new System.ComponentModel.Container();
this.txtFind = new Switch_Toolbox.Library.Forms.STTextBox();
this.chkMatchCase = new Switch_Toolbox.Library.Forms.STCheckBox();
this.btnOK = new Switch_Toolbox.Library.Forms.STButton();
this.hexFind = new Be.Windows.Forms.HexBox();
this.radioBtnText = new System.Windows.Forms.RadioButton();
this.raditnHex = new System.Windows.Forms.RadioButton();
this.timer = new System.Windows.Forms.Timer(this.components);
this.timerPercent = new System.Windows.Forms.Timer(this.components);
this.contentContainer.SuspendLayout();
this.SuspendLayout();
//
// stTextBox1
// contentContainer
//
this.stTextBox1.Location = new System.Drawing.Point(27, 63);
this.stTextBox1.Name = "stTextBox1";
this.stTextBox1.Size = new System.Drawing.Size(381, 20);
this.stTextBox1.TabIndex = 1;
this.contentContainer.Controls.Add(this.raditnHex);
this.contentContainer.Controls.Add(this.radioBtnText);
this.contentContainer.Controls.Add(this.hexFind);
this.contentContainer.Controls.Add(this.btnOK);
this.contentContainer.Controls.Add(this.chkMatchCase);
this.contentContainer.Controls.Add(this.txtFind);
this.contentContainer.Size = new System.Drawing.Size(392, 267);
this.contentContainer.Controls.SetChildIndex(this.txtFind, 0);
this.contentContainer.Controls.SetChildIndex(this.chkMatchCase, 0);
this.contentContainer.Controls.SetChildIndex(this.btnOK, 0);
this.contentContainer.Controls.SetChildIndex(this.hexFind, 0);
this.contentContainer.Controls.SetChildIndex(this.radioBtnText, 0);
this.contentContainer.Controls.SetChildIndex(this.raditnHex, 0);
//
// chkBoxMatchCase
// txtFind
//
this.chkBoxMatchCase.AutoSize = true;
this.chkBoxMatchCase.Location = new System.Drawing.Point(324, 40);
this.chkBoxMatchCase.Name = "chkBoxMatchCase";
this.chkBoxMatchCase.Size = new System.Drawing.Size(84, 17);
this.chkBoxMatchCase.TabIndex = 2;
this.chkBoxMatchCase.Text = "Check Case";
this.chkBoxMatchCase.UseVisualStyleBackColor = true;
this.txtFind.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtFind.Location = new System.Drawing.Point(4, 54);
this.txtFind.Name = "txtFind";
this.txtFind.Size = new System.Drawing.Size(381, 20);
this.txtFind.TabIndex = 1;
//
// stButton1
// chkMatchCase
//
this.stButton1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.stButton1.Location = new System.Drawing.Point(333, 247);
this.stButton1.Name = "stButton1";
this.stButton1.Size = new System.Drawing.Size(75, 23);
this.stButton1.TabIndex = 3;
this.stButton1.Text = "Ok";
this.stButton1.UseVisualStyleBackColor = false;
this.stButton1.Click += new System.EventHandler(this.stButton1_Click);
this.chkMatchCase.AutoSize = true;
this.chkMatchCase.Location = new System.Drawing.Point(301, 31);
this.chkMatchCase.Name = "chkMatchCase";
this.chkMatchCase.Size = new System.Drawing.Size(84, 17);
this.chkMatchCase.TabIndex = 2;
this.chkMatchCase.Text = "Check Case";
this.chkMatchCase.UseVisualStyleBackColor = true;
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnOK.Location = new System.Drawing.Point(310, 238);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 3;
this.btnOK.Text = "Ok";
this.btnOK.UseVisualStyleBackColor = false;
this.btnOK.Click += new System.EventHandler(this.stButton1_Click);
//
// hexFind
//
this.hexFind.Font = new System.Drawing.Font("Segoe UI", 9F);
this.hexFind.Location = new System.Drawing.Point(27, 112);
this.hexFind.Location = new System.Drawing.Point(4, 103);
this.hexFind.Name = "hexFind";
this.hexFind.ShadowSelectionColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(60)))), ((int)(((byte)(188)))), ((int)(((byte)(255)))));
this.hexFind.Size = new System.Drawing.Size(371, 129);
@ -76,50 +98,56 @@
// radioBtnText
//
this.radioBtnText.AutoSize = true;
this.radioBtnText.Location = new System.Drawing.Point(27, 40);
this.radioBtnText.Location = new System.Drawing.Point(4, 31);
this.radioBtnText.Name = "radioBtnText";
this.radioBtnText.Size = new System.Drawing.Size(46, 17);
this.radioBtnText.TabIndex = 5;
this.radioBtnText.TabStop = true;
this.radioBtnText.Text = "Text";
this.radioBtnText.UseVisualStyleBackColor = true;
this.radioBtnText.CheckedChanged += new System.EventHandler(this.radioBtn_CheckedChanged);
this.radioBtnText.Enter += new System.EventHandler(this.radioBtnText_Enter);
//
// raditnHex
//
this.raditnHex.AutoSize = true;
this.raditnHex.Location = new System.Drawing.Point(27, 89);
this.raditnHex.Location = new System.Drawing.Point(4, 80);
this.raditnHex.Name = "raditnHex";
this.raditnHex.Size = new System.Drawing.Size(44, 17);
this.raditnHex.TabIndex = 6;
this.raditnHex.TabStop = true;
this.raditnHex.Text = "Hex";
this.raditnHex.UseVisualStyleBackColor = true;
this.raditnHex.CheckedChanged += new System.EventHandler(this.radioBtn_CheckedChanged);
this.raditnHex.Enter += new System.EventHandler(this.raditnHex_Enter);
//
// timerPercent
//
this.timerPercent.Tick += new System.EventHandler(this.timerPercent_Tick);
//
// SearchHex
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(420, 282);
this.Controls.Add(this.raditnHex);
this.Controls.Add(this.radioBtnText);
this.Controls.Add(this.hexFind);
this.Controls.Add(this.stButton1);
this.Controls.Add(this.chkBoxMatchCase);
this.Controls.Add(this.stTextBox1);
this.ClientSize = new System.Drawing.Size(398, 272);
this.Name = "SearchHex";
this.Text = "Find";
this.Activated += new System.EventHandler(this.SearchHex_Activated);
this.contentContainer.ResumeLayout(false);
this.contentContainer.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private STTextBox stTextBox1;
private Switch_Toolbox.Library.Forms.STCheckBox chkBoxMatchCase;
private STButton stButton1;
private STTextBox txtFind;
private Switch_Toolbox.Library.Forms.STCheckBox chkMatchCase;
private STButton btnOK;
private Be.Windows.Forms.HexBox hexFind;
private System.Windows.Forms.RadioButton radioBtnText;
private System.Windows.Forms.RadioButton raditnHex;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.Timer timerPercent;
}
}

View File

@ -11,8 +11,37 @@ using Be.Windows.Forms;
namespace Switch_Toolbox.Library.Forms
{
public partial class SearchHex : Form
public partial class SearchHex : STForm
{
public HexBox HexBox { get; set; }
private bool _finding;
private FindOptions _findOptions;
public FindOptions FindOptions
{
get
{
return _findOptions;
}
set
{
_findOptions = value;
Reinitialize();
}
}
private void Reinitialize()
{
if (hexFind.ByteProvider != null)
hexFind.ByteProvider.Changed -= new EventHandler(ByteProvider_Changed);
var hex = this._findOptions.Hex != null ? _findOptions.Hex : new byte[0];
hexFind.ByteProvider = new DynamicByteProvider(hex);
hexFind.ByteProvider.Changed += new EventHandler(ByteProvider_Changed);
}
public FindType findType;
public string findString;
public bool matchCase;
@ -21,6 +50,10 @@ namespace Switch_Toolbox.Library.Forms
public SearchHex()
{
InitializeComponent();
hexFind.BackColor = FormThemes.BaseTheme.FormBackColor;
hexFind.ForeColor = FormThemes.BaseTheme.FormBackColor;
}
public void SearchItem()
{
@ -30,9 +63,111 @@ namespace Switch_Toolbox.Library.Forms
private void stButton1_Click(object sender, EventArgs e)
{
var provider = this.hexFind.ByteProvider as DynamicByteProvider;
findHex = provider.Bytes.ToArray();
matchCase = chkBoxMatchCase.Checked;
findString = stTextBox1.Text;
_findOptions.Hex = provider.Bytes.ToArray();
_findOptions.Text = txtFind.Text;
_findOptions.Type = radioBtnText.Checked ? FindType.Hex : FindType.Text;
_findOptions.MatchCase = chkMatchCase.Checked;
_findOptions.IsValid = true;
FindNext();
}
public void FindNext()
{
if (!_findOptions.IsValid)
return;
UpdateUIToFindingState();
// start find process
long res = HexBox.Find(_findOptions);
UpdateUIToNormalState();
Application.DoEvents();
if (res == -1) // -1 = no match
{
// MessageBox.Show(strings.FindOperationEndOfFile, Application.ProductName,
// MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (res == -2) // -2 = find was aborted
{
return;
}
else // something was found
{
this.Close();
Application.DoEvents();
if (!HexBox.Focused)
HexBox.Focus();
}
}
private void UpdateUIToNormalState()
{
timer.Stop();
timerPercent.Stop();
_finding = false;
txtFind.Enabled = chkMatchCase.Enabled = raditnHex.Enabled = radioBtnText.Enabled
= hexFind.Enabled = btnOK.Enabled = true;
}
private void UpdateUIToFindingState()
{
_finding = true;
timer.Start();
timerPercent.Start();
txtFind.Enabled = chkMatchCase.Enabled = raditnHex.Enabled = radioBtnText.Enabled
= hexFind.Enabled = btnOK.Enabled = false;
}
void ByteProvider_Changed(object sender, EventArgs e)
{
ValidateFind();
}
private void ValidateFind()
{
var isValid = false;
if (radioBtnText.Checked && txtFind.Text.Length > 0)
isValid = true;
if (raditnHex.Checked && hexFind.ByteProvider.Length > 0)
isValid = true;
this.btnOK.Enabled = isValid;
}
private void radioBtn_CheckedChanged(object sender, EventArgs e)
{
txtFind.Enabled = radioBtnText.Checked;
hexFind.Enabled = !txtFind.Enabled;
if (txtFind.Enabled)
txtFind.Focus();
else
hexFind.Focus();
}
private void radioBtnText_Enter(object sender, EventArgs e) {
txtFind.Focus();
}
private void raditnHex_Enter(object sender, EventArgs e) {
hexFind.Focus();
}
private void SearchHex_Activated(object sender, EventArgs e)
{
if (radioBtnText.Checked)
txtFind.Focus();
else
hexFind.Focus();
}
private void timerPercent_Tick(object sender, EventArgs e)
{
}
}

View File

@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="timer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="timerPercent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>104, 17</value>
</metadata>
</root>

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ImageEditorBase));
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.stPanel2 = new Switch_Toolbox.Library.Forms.STPanel();
@ -35,6 +36,8 @@
this.alphaChannelBtn = new Switch_Toolbox.Library.Forms.STButton();
this.stPanel4 = new Switch_Toolbox.Library.Forms.STPanel();
this.pictureBoxCustom1 = new Switch_Toolbox.Library.Forms.PictureBoxCustom();
this.stContextMenuStrip2 = new Switch_Toolbox.Library.Forms.STContextMenuStrip(this.components);
this.copyImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.blueChannelBtn = new Switch_Toolbox.Library.Forms.STButton();
this.stPanel3 = new Switch_Toolbox.Library.Forms.STPanel();
this.editBtn = new Switch_Toolbox.Library.Forms.STButton();
@ -52,11 +55,16 @@
this.stContextMenuStrip1 = new Switch_Toolbox.Library.Forms.STMenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.undoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.redoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.editInExternalProgramToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.copyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.propertyGridToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.displayVerticalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.imageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.editInExternalProgramToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.generateMipmapsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.resizeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.reEncodeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.flipHorizontalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -67,13 +75,13 @@
this.adjustmentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.hueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.brightnessContrastToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.generateMipmapsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.stPanel1.SuspendLayout();
this.stPanel4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCustom1)).BeginInit();
this.stContextMenuStrip2.SuspendLayout();
this.stPanel3.SuspendLayout();
this.stContextMenuStrip1.SuspendLayout();
this.SuspendLayout();
@ -128,7 +136,7 @@
//
this.alphaChannelBtn.BackColor = System.Drawing.Color.Silver;
this.alphaChannelBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.alphaChannelBtn.Location = new System.Drawing.Point(448, 1);
this.alphaChannelBtn.Location = new System.Drawing.Point(464, 1);
this.alphaChannelBtn.Name = "alphaChannelBtn";
this.alphaChannelBtn.Size = new System.Drawing.Size(21, 21);
this.alphaChannelBtn.TabIndex = 17;
@ -151,6 +159,7 @@
//
this.pictureBoxCustom1.BackColor = System.Drawing.Color.Transparent;
this.pictureBoxCustom1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBoxCustom1.BackgroundImage")));
this.pictureBoxCustom1.ContextMenuStrip = this.stContextMenuStrip2;
this.pictureBoxCustom1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCustom1.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCustom1.Name = "pictureBoxCustom1";
@ -159,11 +168,25 @@
this.pictureBoxCustom1.TabIndex = 1;
this.pictureBoxCustom1.TabStop = false;
//
// stContextMenuStrip2
//
this.stContextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.copyImageToolStripMenuItem});
this.stContextMenuStrip2.Name = "stContextMenuStrip2";
this.stContextMenuStrip2.Size = new System.Drawing.Size(139, 26);
//
// copyImageToolStripMenuItem
//
this.copyImageToolStripMenuItem.Name = "copyImageToolStripMenuItem";
this.copyImageToolStripMenuItem.Size = new System.Drawing.Size(138, 22);
this.copyImageToolStripMenuItem.Text = "Copy Image";
this.copyImageToolStripMenuItem.Click += new System.EventHandler(this.copyImageToolStripMenuItem_Click);
//
// blueChannelBtn
//
this.blueChannelBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.blueChannelBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.blueChannelBtn.Location = new System.Drawing.Point(421, 1);
this.blueChannelBtn.Location = new System.Drawing.Point(437, 1);
this.blueChannelBtn.Name = "blueChannelBtn";
this.blueChannelBtn.Size = new System.Drawing.Size(21, 21);
this.blueChannelBtn.TabIndex = 16;
@ -184,7 +207,7 @@
this.stPanel3.Controls.Add(this.btnLeftArray);
this.stPanel3.Location = new System.Drawing.Point(0, 24);
this.stPanel3.Name = "stPanel3";
this.stPanel3.Size = new System.Drawing.Size(715, 51);
this.stPanel3.Size = new System.Drawing.Size(520, 51);
this.stPanel3.TabIndex = 3;
//
// editBtn
@ -297,7 +320,7 @@
//
this.greenChannelBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
this.greenChannelBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.greenChannelBtn.Location = new System.Drawing.Point(394, 1);
this.greenChannelBtn.Location = new System.Drawing.Point(410, 1);
this.greenChannelBtn.Name = "greenChannelBtn";
this.greenChannelBtn.Size = new System.Drawing.Size(21, 21);
this.greenChannelBtn.TabIndex = 15;
@ -311,10 +334,10 @@
this.imageBGComboBox.BorderStyle = System.Windows.Forms.ButtonBorderStyle.Solid;
this.imageBGComboBox.ButtonColor = System.Drawing.Color.Empty;
this.imageBGComboBox.FormattingEnabled = true;
this.imageBGComboBox.Location = new System.Drawing.Point(233, 1);
this.imageBGComboBox.Location = new System.Drawing.Point(262, 2);
this.imageBGComboBox.Name = "imageBGComboBox";
this.imageBGComboBox.ReadOnly = true;
this.imageBGComboBox.Size = new System.Drawing.Size(130, 21);
this.imageBGComboBox.Size = new System.Drawing.Size(115, 21);
this.imageBGComboBox.TabIndex = 2;
this.imageBGComboBox.SelectedIndexChanged += new System.EventHandler(this.imageBGComboBox_SelectedIndexChanged);
//
@ -322,7 +345,7 @@
//
this.redChannelBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.redChannelBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.redChannelBtn.Location = new System.Drawing.Point(367, 1);
this.redChannelBtn.Location = new System.Drawing.Point(383, 1);
this.redChannelBtn.Name = "redChannelBtn";
this.redChannelBtn.Size = new System.Drawing.Size(21, 21);
this.redChannelBtn.TabIndex = 14;
@ -334,6 +357,7 @@
//
this.stContextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.editToolStripMenuItem,
this.viewToolStripMenuItem,
this.imageToolStripMenuItem,
this.adjustmentsToolStripMenuItem});
@ -358,6 +382,45 @@
this.exportToolStripMenuItem.Text = "Export";
this.exportToolStripMenuItem.Click += new System.EventHandler(this.exportToolStripMenuItem_Click);
//
// editToolStripMenuItem
//
this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.undoToolStripMenuItem,
this.redoToolStripMenuItem,
this.editInExternalProgramToolStripMenuItem,
this.copyToolStripMenuItem});
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
this.editToolStripMenuItem.Text = "Edit";
//
// undoToolStripMenuItem
//
this.undoToolStripMenuItem.Name = "undoToolStripMenuItem";
this.undoToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
this.undoToolStripMenuItem.Text = "Undo";
this.undoToolStripMenuItem.Click += new System.EventHandler(this.undoToolStripMenuItem_Click);
//
// redoToolStripMenuItem
//
this.redoToolStripMenuItem.Name = "redoToolStripMenuItem";
this.redoToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
this.redoToolStripMenuItem.Text = "Redo";
this.redoToolStripMenuItem.Click += new System.EventHandler(this.redoToolStripMenuItem_Click);
//
// editInExternalProgramToolStripMenuItem
//
this.editInExternalProgramToolStripMenuItem.Name = "editInExternalProgramToolStripMenuItem";
this.editInExternalProgramToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
this.editInExternalProgramToolStripMenuItem.Text = "With External Program";
this.editInExternalProgramToolStripMenuItem.Click += new System.EventHandler(this.editInExternalProgramToolStripMenuItem_Click);
//
// copyToolStripMenuItem
//
this.copyToolStripMenuItem.Name = "copyToolStripMenuItem";
this.copyToolStripMenuItem.Size = new System.Drawing.Size(192, 22);
this.copyToolStripMenuItem.Text = "Copy";
this.copyToolStripMenuItem.Click += new System.EventHandler(this.copyToolStripMenuItem_Click);
//
// viewToolStripMenuItem
//
this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
@ -373,7 +436,7 @@
this.propertyGridToolStripMenuItem.CheckOnClick = true;
this.propertyGridToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
this.propertyGridToolStripMenuItem.Name = "propertyGridToolStripMenuItem";
this.propertyGridToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.propertyGridToolStripMenuItem.Size = new System.Drawing.Size(153, 22);
this.propertyGridToolStripMenuItem.Text = "Property Grid";
this.propertyGridToolStripMenuItem.CheckedChanged += new System.EventHandler(this.propertyGridToolStripMenuItem_CheckedChanged);
this.propertyGridToolStripMenuItem.Click += new System.EventHandler(this.propertyGridToolStripMenuItem_Click);
@ -381,7 +444,7 @@
// displayVerticalToolStripMenuItem
//
this.displayVerticalToolStripMenuItem.Name = "displayVerticalToolStripMenuItem";
this.displayVerticalToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.displayVerticalToolStripMenuItem.Size = new System.Drawing.Size(153, 22);
this.displayVerticalToolStripMenuItem.Text = "Display Vertical";
this.displayVerticalToolStripMenuItem.CheckedChanged += new System.EventHandler(this.displayVerticalToolStripMenuItem_CheckedChanged);
this.displayVerticalToolStripMenuItem.Click += new System.EventHandler(this.displayVerticalToolStripMenuItem_Click);
@ -389,7 +452,6 @@
// imageToolStripMenuItem
//
this.imageToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.editInExternalProgramToolStripMenuItem,
this.generateMipmapsToolStripMenuItem,
this.resizeToolStripMenuItem,
this.reEncodeToolStripMenuItem,
@ -402,12 +464,12 @@
this.imageToolStripMenuItem.Size = new System.Drawing.Size(52, 20);
this.imageToolStripMenuItem.Text = "Image";
//
// editInExternalProgramToolStripMenuItem
// generateMipmapsToolStripMenuItem
//
this.editInExternalProgramToolStripMenuItem.Name = "editInExternalProgramToolStripMenuItem";
this.editInExternalProgramToolStripMenuItem.Size = new System.Drawing.Size(230, 22);
this.editInExternalProgramToolStripMenuItem.Text = "Edit In External Program";
this.editInExternalProgramToolStripMenuItem.Click += new System.EventHandler(this.editInExternalProgramToolStripMenuItem_Click);
this.generateMipmapsToolStripMenuItem.Name = "generateMipmapsToolStripMenuItem";
this.generateMipmapsToolStripMenuItem.Size = new System.Drawing.Size(230, 22);
this.generateMipmapsToolStripMenuItem.Text = "Generate Mipmaps";
this.generateMipmapsToolStripMenuItem.Click += new System.EventHandler(this.generateMipmapsToolStripMenuItem_Click);
//
// resizeToolStripMenuItem
//
@ -482,13 +544,6 @@
this.brightnessContrastToolStripMenuItem.Size = new System.Drawing.Size(185, 22);
this.brightnessContrastToolStripMenuItem.Text = "Brightness / Contrast";
//
// generateMipmapsToolStripMenuItem
//
this.generateMipmapsToolStripMenuItem.Name = "generateMipmapsToolStripMenuItem";
this.generateMipmapsToolStripMenuItem.Size = new System.Drawing.Size(230, 22);
this.generateMipmapsToolStripMenuItem.Text = "Generate Mipmaps";
this.generateMipmapsToolStripMenuItem.Click += new System.EventHandler(this.generateMipmapsToolStripMenuItem_Click);
//
// ImageEditorBase
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -503,6 +558,7 @@
this.stPanel1.PerformLayout();
this.stPanel4.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCustom1)).EndInit();
this.stContextMenuStrip2.ResumeLayout(false);
this.stPanel3.ResumeLayout(false);
this.stPanel3.PerformLayout();
this.stContextMenuStrip1.ResumeLayout(false);
@ -549,7 +605,13 @@
private STCheckBox toggleAlphaChk;
private System.Windows.Forms.ToolStripMenuItem displayVerticalToolStripMenuItem;
private STButton editBtn;
private System.Windows.Forms.ToolStripMenuItem editInExternalProgramToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem generateMipmapsToolStripMenuItem;
private STContextMenuStrip stContextMenuStrip2;
private System.Windows.Forms.ToolStripMenuItem copyImageToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem copyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem editInExternalProgramToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem undoToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem redoToolStripMenuItem;
}
}

View File

@ -15,6 +15,61 @@ namespace Switch_Toolbox.Library.Forms
{
public partial class ImageEditorBase : UserControl
{
private int _currentCacheIndex = -1;
private int currentCacheIndex
{
get
{
return _currentCacheIndex;
}
set
{
SetRedoUndoMenuStrips(value);
_currentCacheIndex = value;
}
}
private void SetRedoUndoMenuStrips(int index)
{
if (index < ImageCache.Count && ImageCache.Count > 0 && index != -1)
redoToolStripMenuItem.Enabled = true;
else
redoToolStripMenuItem.Enabled = false;
if (index > 0 && ImageCache.Count > 0)
undoToolStripMenuItem.Enabled = true;
else
undoToolStripMenuItem.Enabled = false;
}
private List<Image> _imageCache = new List<Image>();
private List<Image> ImageCache
{
get
{
return _imageCache;
}
set
{
_imageCache = value;
SetRedoUndoMenuStrips(currentCacheIndex);
if (_imageCache.Count > 0)
{
undoToolStripMenuItem.Enabled = true;
}
else
{
undoToolStripMenuItem.Enabled = false;
}
}
}
public FileSystemWatcher FileWatcher;
private Thread Thread;
@ -137,6 +192,9 @@ namespace Switch_Toolbox.Library.Forms
OnDataAcquiredEvent += new DataAcquired(ThreadReportsDataAquiredEvent);
SetUpFileSystemWatcher();
undoToolStripMenuItem.Enabled = false;
redoToolStripMenuItem.Enabled = false;
}
private void SetUpFileSystemWatcher()
@ -547,10 +605,16 @@ namespace Switch_Toolbox.Library.Forms
if (Image != null)
{
Image.RotateFlip(rotation);
UpdateEdit(Image);
UpdateEditCached(Image);
}
}
private void UpdateEditCached(Image image)
{
// ImageCache.Add(new Bitmap(image));
UpdateEdit(image);
}
private void UpdateEdit(Image image)
{
ActiveTexture.EditedImages = new EditedBitmap[ActiveTexture.ArrayCount];
@ -670,7 +734,7 @@ namespace Switch_Toolbox.Library.Forms
resizeEditor.LoadImage(pictureBoxCustom1.Image);
if (resizeEditor.ShowDialog() == DialogResult.OK)
{
UpdateEdit(resizeEditor.newImage);
UpdateEditCached(resizeEditor.newImage);
}
}
@ -684,7 +748,7 @@ namespace Switch_Toolbox.Library.Forms
ActiveTexture.Format = encodingEditor.Format;
ActiveTexture.MipCount = (uint)encodingEditor.MipCount;
UpdateEdit(encodingEditor.newImage);
UpdateEditCached(encodingEditor.newImage);
}
}
@ -814,10 +878,6 @@ namespace Switch_Toolbox.Library.Forms
EditInExternalProgram();
}
private void editInExternalProgramToolStripMenuItem_Click(object sender, EventArgs e) {
EditInExternalProgram();
}
private TEX_FORMAT FormatToChange = TEX_FORMAT.UNKNOWN;
private void EditInExternalProgram(bool UseDefaultEditor = true)
{
@ -915,7 +975,7 @@ namespace Switch_Toolbox.Library.Forms
saveBtn.Invoke(new MethodInvoker(
delegate ()
{
UpdateEdit(image);
UpdateEditCached(image);
ApplyEdit(image);
//Update the image with decoding as format could change
@ -925,7 +985,7 @@ namespace Switch_Toolbox.Library.Forms
}
else
{
UpdateEdit(image);
UpdateEditCached(image);
ApplyEdit(image);
//Update the image with decoding as format could change
@ -945,8 +1005,55 @@ namespace Switch_Toolbox.Library.Forms
//Apply edits first to update mip map data
ApplyEdit(Image);
UpdateEdit(Image);
UpdateEditCached(Image);
}
}
private void copyImageToolStripMenuItem_Click(object sender, EventArgs e) {
Clipboard.SetImage(pictureBoxCustom1.Image);
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
Clipboard.SetImage(pictureBoxCustom1.Image);
}
private void editInExternalProgramToolStripMenuItem_Click(object sender, EventArgs e) {
EditInExternalProgram();
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (currentCacheIndex == 0)
return;
if (currentCacheIndex == -1)
{
currentCacheIndex = ImageCache.Count - 1;
}
else
{
currentCacheIndex -= 1;
}
UpdateEdit(ImageCache[currentCacheIndex]);
ApplyEdit(ImageCache[currentCacheIndex]);
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (currentCacheIndex == ImageCache.Count)
return;
if (currentCacheIndex == -1) {
currentCacheIndex = ImageCache.Count;
}
else
{
currentCacheIndex += 1;
}
UpdateEdit(ImageCache[currentCacheIndex]);
ApplyEdit(ImageCache[currentCacheIndex]);
}
}
}

View File

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="stContextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="pictureBoxCustom1.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
@ -325,8 +328,8 @@
mGk4zW7tt3aFGa0JAhPXKv5Grfv8ckaj93+veSuXxEAIUwAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="stContextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
<metadata name="stContextMenuStrip2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>184, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>

View File

@ -163,7 +163,7 @@ namespace Switch_Toolbox.Library.IO
data = STLibraryCompression.ZSTD.Decompress(fileReader.getSection(0, data.Length));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Zstb);
}
if (Magic == "ZLIB")
if (Magic == "ZLIB" || Path.GetExtension(FileName) == ".z")
{
if (data == null)
data = File.ReadAllBytes(FileName);

View File

@ -2,7 +2,7 @@
using System.Linq;
using System.Collections.Generic;
namespace Switch_Toolbox.Library.Test
namespace Switch_Toolbox.Library.NEW
{
//Todo fix swizzle issues with this one
public class GX2
@ -431,7 +431,7 @@ namespace Switch_Toolbox.Library.Test
static bool DebugSurface = false;
public static GX2Surface CreateGx2Texture(byte[] imageData, string Name, uint TileMode, uint AAMode,
uint Width, uint Height, uint Depth, uint Format, uint swizzle, uint mipSwizzle, uint SurfaceDim, uint MipCount)
uint Width, uint Height, uint Depth, uint Format, uint swizzle, uint SurfaceDim, uint MipCount)
{
var surfOut = getSurfaceInfo((GX2SurfaceFormat)Format, Width, Height, 1, 1, TileMode, 0, 0);
uint imageSize = (uint)surfOut.surfSize;
@ -488,9 +488,6 @@ namespace Switch_Toolbox.Library.Test
if (mipLevel != 0)
{
if (mipSwizzle != 0)
swizzleValue = mipSwizzle; //Overwrite swizzle value for mip maps for botw tex2
surfOut = GX2.getSurfaceInfo((GX2SurfaceFormat)Format, Width, Height, 1, 1, TileMode, 0, mipLevel);
if (mipLevel == 1)
@ -705,8 +702,6 @@ namespace Switch_Toolbox.Library.Test
dataOffset += ArrayImageize;
mipDataOffset += ArrayMipImageize;
break;
}
return result;

View File

@ -8,17 +8,75 @@ namespace Switch_Toolbox.Library
{
public class TegraX1Swizzle
{
public static byte[] GetImageData(STGenericTexture texture, byte[] ImageData, int ArrayLevel, int MipLevel)
public static List<uint[]> GenerateMipSizes(TEX_FORMAT Format, uint Width, uint Height, uint Depth, uint SurfaceCount, uint MipCount, uint ImageSize)
{
int target = 1;
uint bpp = STGenericTexture.GetBytesPerPixel(texture.Format);
uint blkWidth = STGenericTexture.GetBlockWidth(texture.Format);
List<uint[]> MipMapSizes = new List<uint[]>();
uint bpp = STGenericTexture.GetBytesPerPixel(Format);
uint blkWidth = STGenericTexture.GetBlockWidth(Format);
uint blkHeight = STGenericTexture.GetBlockHeight(Format);
uint blkDepth = STGenericTexture.GetBlockDepth(Format);
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
uint Pitch = 0;
uint DataAlignment = 512;
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
uint ArrayCount = SurfaceCount;
uint ArrayOffset = 0;
for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++)
{
uint SurfaceSize = 0;
int blockHeightShift = 0;
uint[] MipOffsets = new uint[MipCount];
for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
{
uint width = (uint)Math.Max(1, Width >> mipLevel);
uint height = (uint)Math.Max(1, Height >> mipLevel);
uint depth = (uint)Math.Max(1, Depth >> mipLevel);
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
blockHeightShift += 1;
uint width__ = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth);
uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight);
//Calculate the mip size instead
byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)];
SurfaceSize += (uint)AlignedData.Length;
MipOffsets[mipLevel] = (SurfaceSize);
//Get the first mip offset and current one and the total image size
int msize = (int)((MipOffsets[0] + ImageSize - MipOffsets[mipLevel]) / ArrayCount);
Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64);
SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);
}
ArrayOffset += (uint)(ImageSize / ArrayCount);
MipMapSizes.Add(MipOffsets);
}
return MipMapSizes;
}
public static byte[] GetImageData(STGenericTexture texture, byte[] ImageData, int ArrayLevel, int MipLevel, int target = 1)
{
uint bpp = STGenericTexture.GetBytesPerPixel(texture.Format);
uint blkWidth = STGenericTexture.GetBlockWidth(texture.Format);
uint blkHeight = STGenericTexture.GetBlockHeight(texture.Format);
uint blkDepth = STGenericTexture.GetBlockDepth(texture.Format);
uint blkDepth = STGenericTexture.GetBlockDepth(texture.Format);
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(texture.Height, blkHeight));
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
uint tileMode = 0;
uint Pitch = 0;
uint DataAlignment = 512;
@ -65,7 +123,7 @@ namespace Switch_Toolbox.Library
SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);
Console.WriteLine($"{width} {height} {blkWidth} {blkHeight} {target} {bpp} {TileMode} {(int)Math.Max(0, BlockHeightLog2 - blockHeightShift)} {data_.Length}");
byte[] result = deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, target, bpp, TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), data_);
byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, target, bpp, TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), data_);
//Create a copy and use that to remove uneeded data
byte[] result_ = new byte[size];
Array.Copy(result, 0, result_, 0, size);