NHSE/NHSE.Core/Util/ArrayUtil.cs
Kurt 9d1309827e Pre 3.0.0 support
Adds data for supporting 3.0.0, still needs savefile structures/hash range dumped and incorporated into MainOffsets30/PersonalOffsets30. Offsets/range is currently a copy of 2.0, which is obviously not correct. Saves will fail to load as encrypted values are not at the expected offset (pls no complaints).
CTRL-I from main form still opens the injector interface; RAM offset not yet documented.
2026-01-14 11:17:15 -06:00

73 lines
2.1 KiB
C#

using System;
namespace NHSE.Core;
/// <summary>
/// Array reusable logic
/// </summary>
public static class ArrayUtil
{
//public static int ReplaceOccurrences(this Span<byte> array, ReadOnlySpan<byte> pattern, ReadOnlySpan<byte> swap)
//{
// int count = 0;
// int ofs = 0;
// while (true)
// {
// var index = array[ofs..].IndexOf(pattern);
// if (index == -1)
// return count;
// ofs += index;
//
// swap.CopyTo(array[ofs..]);
// ofs += swap.Length; // skip past swapped data
// ++count;
// }
//}
public static int ReplaceOccurrences(this Span<byte> array, ReadOnlySpan<byte> pattern, ReadOnlySpan<byte> swap)
{
int count = 0;
while (true)
{
int ofs = IndexOfBytes(array, pattern);
if (ofs == -1)
return count;
swap.CopyTo(array[ofs..]);
++count;
}
}
/// <summary>
/// Finds a provided <see cref="pattern"/> within the supplied <see cref="array"/>.
/// </summary>
/// <param name="array">Array to look in</param>
/// <param name="pattern">Pattern to look for</param>
/// <param name="startIndex">Starting offset to look from</param>
/// <param name="length">Amount of entries to look through</param>
/// <returns>Index the pattern occurs at; if not found, returns -1.</returns>
public static int IndexOfBytes(ReadOnlySpan<byte> array, ReadOnlySpan<byte> pattern, int startIndex = 0, int length = -1)
{
int len = pattern.Length;
int endIndex = length > 0
? startIndex + length
: array.Length - len - startIndex;
endIndex = Math.Min(array.Length - pattern.Length, endIndex);
int i = startIndex;
int j = 0;
while (true)
{
if (pattern[j] != array[i + j])
{
if (++i == endIndex)
return -1;
j = 0;
}
else if (++j == len)
{
return i;
}
}
}
}