using System.Collections.Immutable; using VersionedSerialization; using VersionedSerialization.Attributes; namespace Il2CppInspector.Next; public struct Pointer(ulong value = 0) : IReadable, IEquatable> where T : IReadable, new() { [CustomSerialization("reader.ReadNUInt();", "is32Bit ? 4 : 8")] private ulong _value = value; public readonly ulong PointerValue => _value; public readonly bool Null => _value == 0; public void Read(ref TReader reader, in StructVersion version = default) where TReader : IReader, allows ref struct { _value = reader.ReadNUInt(); } public static int Size(in StructVersion version = default, bool is32Bit = false) { return is32Bit ? 4 : 8; } public readonly T Read(ref SpanReader reader, in StructVersion version) { reader.Offset = (int)PointerValue; return reader.ReadVersionedObject(version); } public readonly ImmutableArray ReadArray(ref SpanReader reader, long count, in StructVersion version) { reader.Offset = (int)PointerValue; return reader.ReadVersionedObjectArray(count, version); } public static implicit operator Pointer(ulong value) => new(value); public static implicit operator ulong(Pointer ptr) => ptr.PointerValue; #region Equality operators + ToString public static bool operator ==(Pointer left, Pointer right) => left._value == right._value; public static bool operator !=(Pointer left, Pointer right) => !(left == right); public readonly override bool Equals(object? obj) => obj is Pointer other && Equals(other); public readonly bool Equals(Pointer other) => this == other; public readonly override int GetHashCode() => HashCode.Combine(_value); public readonly override string ToString() => $"0x{_value:X} <{typeof(T).Name}>"; #endregion }