using ObjectLayoutInspector;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace App
{
    public static class BytesPrinter
    {
        public static void PrintBytes<T>(T value, bool printLayout = false)
        {
            unsafe
            {
                var size = SizeCalculator.Instance.SizeOf(() => value);
                var bytes = new byte[size];

                var pointer = Unsafe.AsPointer(ref value);
                IntPtr head = typeof(T).IsValueType? new IntPtr(pointer): *(IntPtr*)pointer - IntPtr.Size;
                Marshal.Copy(head, bytes, 0, size);
                Console.WriteLine($"[{size}]{BitConverter.ToString(bytes)}");
                if(printLayout) TypeLayout.PrintLayout(typeof(T));
            }
        }

        public static string AsString(this IntPtr ptr) => BitConverter.ToString(BitConverter.GetBytes(ptr.ToInt64()));
    }
}
