FModel/FModel/PakReader/BinaryHelper.cs
Fabian 1b2f6c636f Uhm ok so I guess that's the big commit
- Added utoc/ucas (io store) loading support
- Added new unversioned asset parsing support ONLY for asset we have mappings yet
- Integrated that new parsing as good as possible into fmodel's old structure (which certainly wasn't prepared for such a change lol)
- ONLY for new package format disabled caching of files in debug mode (was annoying)
- Type and enum mappings while by default be loaded from a github repo BUT in debug mode it will first attempt to load them from local files (relative to the exe, also for debugging conveniance) but fallback to the ones from the repo
- F12 hotkey for reloading current type mappings (Mainly made for debug but also works in release)
2020-10-28 21:41:53 +01:00

64 lines
2.1 KiB
C#

using System;
using System.Linq;
using System.Runtime.CompilerServices;
namespace FModel.PakReader
{
static class BinaryHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Align(long val, long alignment)
{
return val + alignment - 1 & ~(alignment - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Flip(uint value) => (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
static readonly uint[] _Lookup32 = Enumerable.Range(0, 256).Select(i => {
string s = i.ToString("X2");
return s[0] + ((uint)s[1] << 16);
}).ToArray();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToHex(params byte[] bytes)
{
if (bytes == null) return null;
var length = bytes.Length;
var result = new char[length * 2];
for (int i = 0; i < length; i++)
{
var val = _Lookup32[bytes[i]];
result[2 * i] = (char)val;
result[2 * i + 1] = (char)(val >> 16);
}
return new string(result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToStringKey(this byte[] byteKey)
{
return "0x" + BitConverter.ToString(byteKey).Replace("-", "");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] ToBytesKey(this string stringKey)
{
byte[] arr = new byte[stringKey.Length >> 1];
for (int i = 0; i < stringKey.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(stringKey[i << 1]) << 4) + (GetHexVal(stringKey[(i << 1) + 1])));
}
return arr;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetHexVal(char hex)
{
int val = (int)hex;
return val - (val < 58 ? 48 : 55);
}
}
}