2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
2019-07-28 23:04:23 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2020-03-23 12:57:18 +00:00
|
|
|
using Wabbajack.Common;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.Paths;
|
2022-10-07 22:14:01 +00:00
|
|
|
#pragma warning disable SYSLIB0001
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Compression.BSA;
|
|
|
|
|
|
|
|
public static class BinaryHelperExtensions
|
2019-07-28 23:04:23 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
private static readonly Encoding Windows1252;
|
|
|
|
|
|
|
|
static BinaryHelperExtensions()
|
2019-07-28 23:04:23 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
Windows1252 = Encoding.GetEncoding(1252);
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
private static Encoding GetEncoding(VersionType version)
|
|
|
|
{
|
|
|
|
return version switch
|
2020-01-10 13:16:41 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
VersionType.TES3 => Encoding.ASCII,
|
|
|
|
VersionType.SSE => Windows1252,
|
|
|
|
_ => Encoding.UTF7
|
|
|
|
};
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string ReadStringLen(this BinaryReader rdr, VersionType version)
|
|
|
|
{
|
|
|
|
var len = rdr.ReadByte();
|
|
|
|
if (len == 0) return string.Empty;
|
2019-08-11 22:57:32 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var bytes = rdr.ReadBytes(len - 1);
|
|
|
|
rdr.ReadByte();
|
|
|
|
return GetEncoding(version).GetString(bytes);
|
|
|
|
}
|
2019-09-08 22:44:15 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string ReadStringLenNoTerm(this BinaryReader rdr, VersionType version)
|
|
|
|
{
|
|
|
|
var len = rdr.ReadByte();
|
|
|
|
var bytes = rdr.ReadBytes(len);
|
|
|
|
return GetEncoding(version).GetString(bytes);
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string ReadStringTerm(this BinaryReader rdr, VersionType version)
|
|
|
|
{
|
|
|
|
var acc = new List<byte>();
|
|
|
|
while (true)
|
2019-08-22 04:27:24 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
var c = rdr.ReadByte();
|
|
|
|
|
|
|
|
if (c == '\0') break;
|
|
|
|
|
|
|
|
acc.Add(c);
|
2019-08-22 04:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return GetEncoding(version).GetString(acc.ToArray());
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns \0 terminated bytes for a string encoded with a given BSA version's encoding format
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="val"></param>
|
|
|
|
/// <param name="version"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static byte[] ToBZString(this RelativePath val, VersionType version)
|
|
|
|
{
|
|
|
|
var b = GetEncoding(version).GetBytes((string) val);
|
|
|
|
var b2 = new byte[b.Length + 2];
|
|
|
|
b.CopyTo(b2, 1);
|
|
|
|
b2[0] = (byte) (b.Length + 1);
|
|
|
|
return b2;
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns bytes for unterminated string with a count at the start
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="val"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static byte[] ToBSString(this RelativePath val)
|
|
|
|
{
|
|
|
|
var b = Encoding.ASCII.GetBytes((string) val);
|
|
|
|
var b2 = new byte[b.Length + 1];
|
|
|
|
b.CopyTo(b2, 1);
|
|
|
|
b2[0] = (byte) b.Length;
|
2019-09-14 04:35:42 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return b2;
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string ReadStringLenTerm(this ReadOnlyMemorySlice<byte> bytes, VersionType version)
|
|
|
|
{
|
|
|
|
if (bytes.Length <= 1) return string.Empty;
|
|
|
|
return GetEncoding(version).GetString(bytes.Slice(1, bytes[0]));
|
|
|
|
}
|
2019-07-29 04:52:04 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string ReadStringTerm(this ReadOnlyMemorySlice<byte> bytes, VersionType version)
|
|
|
|
{
|
|
|
|
if (bytes.Length <= 1) return string.Empty;
|
|
|
|
return GetEncoding(version).GetString(bytes[..^1]);
|
|
|
|
}
|
2019-07-29 04:52:04 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns bytes for a string with a length prefix, version is the BSA version
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="val"></param>
|
|
|
|
/// <param name="version"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static byte[] ToTermString(this string val, VersionType version)
|
|
|
|
{
|
|
|
|
var b = GetEncoding(version).GetBytes(val);
|
|
|
|
var b2 = new byte[b.Length + 1];
|
|
|
|
b.CopyTo(b2, 0);
|
|
|
|
b[0] = (byte) b.Length;
|
|
|
|
return b2;
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static byte[] ToTermString(this RelativePath val, VersionType version)
|
|
|
|
{
|
|
|
|
return ((string) val).ToTermString(version);
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static ulong GetBSAHash(this string name)
|
|
|
|
{
|
|
|
|
name = name.Replace('/', '\\');
|
|
|
|
return GetBSAHash(Path.ChangeExtension(name, null), Path.GetExtension(name));
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static ulong GetBSAHash(this RelativePath name)
|
|
|
|
{
|
|
|
|
return ((string) name).GetBSAHash();
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static ulong GetFolderBSAHash(this RelativePath name)
|
|
|
|
{
|
|
|
|
return GetBSAHash((string) name, "");
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public static ulong GetBSAHash(this string name, string ext)
|
|
|
|
{
|
|
|
|
name = name.ToLowerInvariant();
|
|
|
|
ext = ext.ToLowerInvariant();
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
return 0;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var hashBytes = new[]
|
2020-08-15 01:27:33 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
(byte) (name.Length == 0 ? '\0' : name[name.Length - 1]),
|
|
|
|
(byte) (name.Length < 3 ? '\0' : name[name.Length - 2]),
|
|
|
|
(byte) name.Length,
|
|
|
|
(byte) name[0]
|
|
|
|
};
|
|
|
|
var hash1 = BitConverter.ToUInt32(hashBytes, 0);
|
|
|
|
switch (ext)
|
2019-07-28 23:04:23 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
case ".kf":
|
|
|
|
hash1 |= 0x80;
|
|
|
|
break;
|
|
|
|
case ".nif":
|
|
|
|
hash1 |= 0x8000;
|
|
|
|
break;
|
|
|
|
case ".dds":
|
|
|
|
hash1 |= 0x8080;
|
|
|
|
break;
|
|
|
|
case ".wav":
|
|
|
|
hash1 |= 0x80000000;
|
|
|
|
break;
|
2020-04-20 21:36:33 +00:00
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
uint hash2 = 0;
|
|
|
|
for (var i = 1; i < name.Length - 2; i++) hash2 = hash2 * 0x1003f + (byte) name[i];
|
|
|
|
|
|
|
|
uint hash3 = 0;
|
|
|
|
for (var i = 0; i < ext.Length; i++) hash3 = hash3 * 0x1003f + (byte) ext[i];
|
|
|
|
|
|
|
|
return ((ulong) (hash2 + hash3) << 32) + hash1;
|
2019-07-28 23:04:23 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|