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

Start to impliment RSTB table.

This commit is contained in:
KillzXGaming 2019-06-10 20:54:23 -04:00
parent d1adbc5547
commit e22005bbea
12 changed files with 225 additions and 2 deletions

Binary file not shown.

View File

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox.Library.IO;
namespace FirstPlugin.FileFormats
{
//Parsing based on wiki https://zeldamods.org/wiki/ResourceSizeTable.product.rsizetable
//Functions from https://github.com/zeldamods/rstb/blob/master/rstb/rstb.py
//Copyright 2018 leoetlino <leo@leolam.fr>
//Licensed under GPLv2+
// Copy of license can be found under Lib/Licenses
public class RSTB
{
public Dictionary<string, uint> NameTables { get; set; }
public Dictionary<uint, uint> Crc32Tables { get; set; }
public void LoadFile(string FileName) {
Read(new FileReader(FileName));
}
public RSTB()
{
Crc32Tables = new Dictionary<uint, uint>();
NameTables = new Dictionary<string, uint>();
}
public void Read(FileReader reader)
{
reader.ReadSignature(4, "RSTB");
uint Crc32TableCount = reader.ReadUInt32();
uint NameTableCount = reader.ReadUInt32();
for (int i = 0; i < Crc32TableCount; i++)
{
uint Crc32 = reader.ReadUInt32();
uint Size = reader.ReadUInt32();
Crc32Tables.Add(Crc32, Size);
}
for (int i = 0; i < NameTableCount; i++)
{
string Name = reader.ReadString(128);
uint Size = reader.ReadUInt32();
NameTables.Add(Name, Size);
}
}
public void Write(FileWriter writer)
{
writer.WriteSignature("RSTB");
writer.Write(Crc32Tables.Count);
writer.Write(NameTables.Count);
foreach (var table in Crc32Tables)
{
writer.Write(table.Key);
writer.Write(table.Value);
}
foreach (var table in NameTables)
{
writer.Write(table.Key.ToByteArray(128));
writer.Write(table.Value);
}
}
public uint GetSize(string Name)
{
uint Crc32 = Name.EncodeCrc32();
if (Crc32Tables.ContainsKey(Crc32))
return Crc32Tables[Crc32];
if (NameTables.ContainsKey(Name))
return NameTables[Name];
return 0;
}
public uint SetSize(string Name)
{
if (IsNeededInNameMap(Name))
{
if (Name.Length > 128)
throw new Exception("Name is too long! Must be smaller than 128 characters!");
}
uint Crc32 = Name.EncodeCrc32();
if (Crc32Tables.ContainsKey(Crc32))
return Crc32Tables[Crc32];
if (NameTables.ContainsKey(Name))
return NameTables[Name];
return 0;
}
public int GetBufferSize()
{
return (4 + 4 + 4) + 8 * Crc32Tables.Count + (132 * NameTables.Count);
}
public bool IsInTable(string Name)
{
uint Crc32 = Name.EncodeCrc32();
if (Crc32Tables.ContainsKey(Crc32) || NameTables.ContainsKey(Name))
return true;
return false;
}
public bool IsNeededInNameMap(string Name)
{
uint Crc32 = Name.EncodeCrc32();
foreach (var existingName in NameTables.Keys)
if (Crc32Tables.ContainsKey(existingName.EncodeCrc32()))
return true;
return false;
}
}
}

View File

@ -234,6 +234,7 @@
<Compile Include="FileFormats\Audio\Archives\BFGRP.cs" />
<Compile Include="FileFormats\GFBMDL\GFBMDL.cs" />
<Compile Include="FileFormats\Hashes\SAHT.cs" />
<Compile Include="FileFormats\SizeTables\RSTB.cs" />
<Compile Include="FileFormats\Shader\NSWShaderDecompile.cs" />
<Compile Include="FileFormats\Shader\NUSHDB.cs" />
<Compile Include="FileFormats\Shader\NVNSH.cs" />

View File

@ -1 +1 @@
5e2bb78fbebca632406427d80546922252d8f7ff
a5f8e3bc83ca333f1641f1a9eb9720cb2bff4cb6

View File

@ -0,0 +1,87 @@
// Copyright(c) 2014 Mads Breusch Klinkby. All rights reserved.
// Licensed under the MIT License; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://opensource.org/licenses/MIT
// Published at https://github.com/klinkby/klinkby.checksum
using System.Text;
namespace System.Security.Cryptography
{
/// <summary>
/// Computes a CRC32 checksum.
/// </summary>
/// <remarks>Based on <see cref="http://sanity-free.org/12/crc32_implementation_in_csharp.html"/></remarks>
public static class Crc32
{
readonly static uint[] Table = CreateTable();
static Crc32()
{ }
/// <summary>
/// Compute the checksum of a UTF8 text.
/// </summary>
/// <param name="text">Text to calculate</param>
/// <returns>Checksum</returns>
public static uint Compute(string text)
{
return Compute(text, Encoding.UTF8);
}
/// <summary>
/// Compute the checksum of a text using a specific encoding.
/// </summary>
/// <param name="text">Text to calculate</param>
/// <param name="encoding">Text encoding</param>
/// <returns>Checksum</returns>
public static uint Compute(string text, Encoding encoding)
{
if (string.IsNullOrEmpty(text)) return 0;
byte[] bytes = encoding.GetBytes(text);
return Compute(bytes);
}
/// <summary>
/// Compute the checksum of a binary buffer.
/// </summary>
/// <param name="bytes">Buffer to calculate</param>
/// <returns></returns>
public static uint Compute(byte[] bytes)
{
uint crc = 0xffffffff;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(((crc) & 0xff) ^ bytes[i]);
crc = (crc >> 8) ^ Table[index];
}
return unchecked((~crc));
}
static uint[] CreateTable()
{
const uint poly = 0xedb88320;
var table = new uint[256];
uint temp = 0;
for (uint i = 0; i < table.Length; ++i)
{
temp = i;
for (int j = 8; j > 0; --j)
{
if ((temp & 1) == 1)
{
temp = (uint)((temp >> 1) ^ poly);
}
else
{
temp >>= 1;
}
}
table[i] = temp;
}
return table;
}
}
}

View File

@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace Switch_Toolbox.Library.IO
{
@ -25,6 +26,16 @@ namespace Switch_Toolbox.Library.IO
return true;
}
public static byte[] ToByteArray(this string str, int length)
{
return Encoding.ASCII.GetBytes(str.PadRight(length, ' '));
}
public static uint EncodeCrc32(this string str)
{
return Crc32.Compute(str);
}
public static bool Matches(this byte[] arr, uint startIndex, params char[] magic)
{
if (arr.Length < magic.Length + startIndex) return false;

View File

@ -205,6 +205,7 @@
<Compile Include="Audio\VGAudioFile.cs" />
<Compile Include="Collections\SortExtensions.cs" />
<Compile Include="Config.cs" />
<Compile Include="Cryptography\crc32.cs" />
<Compile Include="DrawableContainer.cs" />
<Compile Include="FileFormats\3DS\ETC1.cs" />
<Compile Include="FileFormats\Animation\SMD.cs" />