public IntPtr GetUserdata(LuaValue key) { ExpectType(key, LuaValueType.Userdata); return(this[key].GetUserdata()); }
public string ToString(LuaValue key) { return(this[key].ToString()); }
public bool IsObject <T>(LuaValue key) where T : LuaObject { return(this[key].IsObject <T>()); }
public bool IsUserdata(LuaValue key) { return(this[key].IsUserdata()); }
public LuaObject GetObject(LuaValue key) { ExpectType(key, LuaValueType.Object); return(this[key].GetObject()); }
public bool IsObject(LuaValue key, Type type) { return(this[key].IsObject(type)); }
public LuaTable GetTable(LuaValue key) { ExpectType(key, LuaValueType.Table); return(this[key].GetTable()); }
private void EncodeImpl(BinaryWriter output, LuaValue value) { if (value.IsNil()) { output.Write((byte)BLONValueType.Nil); } else if (value.IsBool()) { var b = value.GetBool(); if (b) { output.Write((byte)BLONValueType.True); } else { output.Write((byte)BLONValueType.False); } } else if (value.IsNumber()) { if (value.IsInteger()) { var n = value.GetLong(); if (n >= 0) { if (n == 0) { output.Write((byte)BLONValueType.Zero); } else if (n == 1) { output.Write((byte)BLONValueType.One); } else if (n <= Byte.MaxValue) { output.Write((byte)BLONValueType.UInt8); output.Write((byte)n); } else if (n <= UInt16.MaxValue) { output.Write((byte)BLONValueType.UInt16); output.Write((ushort)n); } else if (n <= UInt32.MaxValue) { output.Write((byte)BLONValueType.UInt32); output.Write((uint)n); } else { output.Write((byte)BLONValueType.Int64); output.Write(n); } } else { if (n >= -Byte.MaxValue) { output.Write((byte)BLONValueType.UInt8_Negative); output.Write((byte)-n); } else if (n >= -UInt16.MaxValue) { output.Write((byte)BLONValueType.UInt16_Negative); output.Write((ushort)-n); } else if (n >= -UInt32.MaxValue) { output.Write((byte)BLONValueType.UInt32_Negative); output.Write((uint)-n); } else { output.Write((byte)BLONValueType.Int64); output.Write(n); } } } else { var d = value.GetDouble(); if (EncodeDoubleAsFloat && d >= float.MinValue && d <= float.MaxValue) { output.Write((byte)BLONValueType.Float32); output.Write((float)d); } else { output.Write((byte)BLONValueType.Float64); output.Write(d); } } } else if (value.IsString()) { byte cacheIndex; if (m_reverseStringCache.TryGetValue(value, out cacheIndex)) { output.Write((byte)BLONValueType.PreviouslyCachedString); output.Write(cacheIndex); } else { var s = value.GetByteString(); bool cached = TryCacheString(s); if (s.Length <= Byte.MaxValue) { output.Write((byte)(cached ? BLONValueType.String8_Cached : BLONValueType.String8)); output.Write((byte)s.Length); } else if (s.Length <= UInt16.MaxValue) { output.Write((byte)(cached ? BLONValueType.String16_Cached : BLONValueType.String16)); output.Write((ushort)s.Length); } else { output.Write((byte)(cached ? BLONValueType.String32_Cached : BLONValueType.String32)); output.Write(s.Length); } output.Write(s); } } else if (value.IsTable()) { var t = value.GetTable(); if (t.Count <= Byte.MaxValue) { output.Write((byte)BLONValueType.Table8); output.Write((byte)t.Count); } else if (t.Count <= UInt16.MaxValue) { output.Write((byte)BLONValueType.Table16); output.Write((ushort)t.Count); } else { output.Write((byte)BLONValueType.Table32); output.Write(t.Count); } foreach (var k in t.Keys) { var v = t[k]; if (!k.IsNil() && !v.IsNil()) { EncodeImpl(output, k); EncodeImpl(output, v); } } } else { throw new InvalidDataException(string.Format("Cannot encode type {0}", value.GetTypeName())); } }
public bool IsByteString(LuaValue key) { return(this[key].IsByteString()); }
public bool IsTable(LuaValue key) { return(this[key].IsTable()); }
public LuaError(string message, int level = 1) : base(message) { Value = new LuaValue(message); Level = level; }
public LuaError(LuaValue message, int level = 1) : base(message.ToString()) { Value = message; Level = level; }
private bool Equals(LuaValue other) { switch (m_type) { case LuaValueType.Nil: default: { return(other.m_type == m_type); } case LuaValueType.Boolean: { return(other.m_type == LuaValueType.Boolean && other.m_value.Boolean == m_value.Boolean); } case LuaValueType.Integer: { if (other.m_type == LuaValueType.Integer) { return(other.m_value.Integer == m_value.Integer); } else if (other.m_type == LuaValueType.Number) { #if LUA_32BITS return(other.m_value.Number == (float)m_value.Integer); #else return(other.m_value.Number == (double)m_value.Integer); #endif } else { return(false); } } case LuaValueType.Number: { if (other.m_type == LuaValueType.Number) { return(other.m_value.Number == m_value.Number); } else if (other.m_type == LuaValueType.Integer) { #if LUA_32BITS return((float)other.m_value.Integer == m_value.Number); #else return((double)other.m_value.Integer == m_value.Number); #endif } else { return(false); } } case LuaValueType.String: { if (other.m_type == LuaValueType.String) { return(other.m_value.String.Equals(m_value.String)); } else if (other.m_type == LuaValueType.ByteString) { var str = Encoding.UTF8.GetBytes(m_value.String); var ostr = other.m_value.ByteString; return(ByteStringEquals(str, ostr)); } else { return(false); } } case LuaValueType.ByteString: { if (other.m_type == LuaValueType.ByteString) { var str = m_value.ByteString; var ostr = other.m_value.ByteString; return(ByteStringEquals(str, ostr)); } else if (other.m_type == LuaValueType.String) { var str = m_value.ByteString; var ostr = Encoding.UTF8.GetBytes(other.m_value.String); return(ByteStringEquals(str, ostr)); } else { return(false); } } case LuaValueType.Table: { return(other.m_type == LuaValueType.Table && other.m_value.Table == m_value.Table); } case LuaValueType.Object: { return(other.m_type == LuaValueType.Object && other.m_value.Object == m_value.Object); } case LuaValueType.Function: { return(other.m_type == LuaValueType.Function && other.m_value.Function == m_value.Function); } case LuaValueType.CFunction: { return(other.m_type == LuaValueType.CFunction && other.m_value.CFunction == m_value.CFunction); } case LuaValueType.Coroutine: { return(other.m_type == LuaValueType.Coroutine && other.m_value.Coroutine == m_value.Coroutine); } case LuaValueType.Userdata: { return(other.m_type == LuaValueType.Userdata && other.m_value.Userdata == m_value.Userdata); } } }
private LuaError GenerateTypeError(string expectedType, LuaValue key) { throw new LuaError(string.Format("Expected {0} for key {1}, got {2}", expectedType, key.ToString(), GetTypeName(key))); }
public bool IsObject(LuaValue key) { return(this[key].IsObject()); }
public bool IsNil(LuaValue key) { return(this[key].IsNil()); }
private void Encode(BinaryWriter output, LuaValue value) { EncodeImpl(output, value); }