public byte[] Data { get { return _buffer; } }
- public ByteBuffer(byte[] buffer)
+ public ByteBuffer(byte[] buffer) : this(buffer, 0) { }
+
+ public ByteBuffer(byte[] buffer, int pos)
{
_buffer = buffer;
- _pos = 0;
+ _pos = pos;
}
- public int Position { get { return _pos; } }
+ public int Position {
+ get { return _pos; }
+ set { _pos = value; }
+ }
// Pre-allocated helper arrays for convertion.
private float[] floathelper = new[] { 0.0f };
_buffer[offset + count - 1 - i] = (byte)(data >> i * 8);
}
}
- _pos = offset;
}
protected ulong ReadLittleEndian(int offset, int count)
{
AssertOffsetAndLength(offset, sizeof(sbyte));
_buffer[offset] = (byte)value;
- _pos = offset;
}
public void PutByte(int offset, byte value)
{
AssertOffsetAndLength(offset, sizeof(byte));
_buffer[offset] = value;
- _pos = offset;
}
// this method exists in order to conform with Java ByteBuffer standards
Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize,
oldBufSize);
-
- _bb = new ByteBuffer(newBuf);
+ _bb = new ByteBuffer(newBuf, newBufSize);
+ System.Diagnostics.Debug.WriteLine(_bb.Position);
}
// Prepare to write an element of `size` after `additional_bytes`
{
Prep(_minAlign, sizeof(int));
AddOffset(rootTable);
+ _bb.Position = _space;
}
public ByteBuffer DataBuffer { get { return _bb; } }
{
AddByte((byte)fileIdentifier[i]);
}
- AddOffset(rootTable);
+ Finish(rootTable);
}
fbb.AddOffset(test1);
var testArrayOfString = fbb.EndVector();
-
Monster.StartMonster(fbb);
Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
Color.Green, (short)5, (sbyte)6));
Monster.AddTestbool(fbb, false);
var mon = Monster.EndMonster(fbb);
- fbb.Finish(mon);
+ Monster.FinishMonsterBuffer(fbb, mon);
// Dump to output directory so we can inspect later, if needed
using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset))
// Now assert the buffer
TestBuffer(fbb.DataBuffer);
+
+ //Attempt to mutate Monster fields and check whether the buffer has been mutated properly
+ // revert to original values after testing
+ Monster monster = Monster.GetRootAsMonster(fbb.DataBuffer);
+
+ // mana is optional and does not exist in the buffer so the mutation should fail
+ // the mana field should retain its default value
+ Assert.AreEqual(monster.MutateMana((short)10), false);
+ Assert.AreEqual(monster.Mana, (short)150);
+
+ // testType is an existing field and mutating it should succeed
+ Assert.AreEqual(monster.TestType, Any.Monster);
+ Assert.AreEqual(monster.MutateTestType(Any.NONE), true);
+ Assert.AreEqual(monster.TestType, Any.NONE);
+ Assert.AreEqual(monster.MutateTestType(Any.Monster), true);
+ Assert.AreEqual(monster.TestType, Any.Monster);
+
+ // get a struct field and edit one of its fields
+ Vec3 pos = monster.Pos;
+ Assert.AreEqual(pos.X, 1.0f);
+ pos.MutateX(55.0f);
+ Assert.AreEqual(pos.X, 55.0f);
+ pos.MutateX(1.0f);
+ Assert.AreEqual(pos.X, 1.0f);
+
+ TestBuffer(fbb.DataBuffer);
}
private void TestBuffer(ByteBuffer bb)