This document is about: QUANTUM 1
SWITCH TO

Serializing a Frame

C#

static unsafe byte[] PtrToArray(byte* ptr, int size) {
  Assert.Check(size >= 0);

  var bytes = new byte[size];

  if (size > 1) {
    fixed (byte* bytesPtr = bytes) {
      Native.Copy((void*)bytesPtr, (void*)ptr, (ulong)size);
    }
  }

  return bytes;
}

static unsafe void ArrayToPtr(byte* ptr, int size, byte[] bytes) {
  Assert.Check(size >= 0);

  if (size > 1) {
    fixed (byte* bytesPtr = bytes) {
      Native.Copy((void*)ptr, (void*)bytesPtr, (ulong)size);
    }
  }
}

public void GetFrameState(out byte[] globals, out byte[] entities) {
  globals = PtrToArray((byte*)_globals, sizeof(_globals_));
  entities = PtrToArray((byte*)_entities, sizeof(_entities_));
}

public void SetFrameState(byte[] globals, byte[] entities) {
  Assert.Check(globals != null);
  Assert.Check(entities != null);

  Assert.Check(sizeof(_globals_) == globals.Length);
  Assert.Check(sizeof(_entities_) == entities.Length);

  ArrayToPtr((byte*)_globals, sizeof(_globals_), globals);
  ArrayToPtr((byte*)_entities, sizeof(_entities_), entities);
}
Back to top