Exemplo n.º 1
0
 public LuaValue this[LuaValue key]
 {
     get
     {
         if (m_values.ContainsKey(key))
         {
             return(m_values[key]);
         }
         return(LuaValue.Nil);
     }
     set
     {
         if (key.IsNil())
         {
             throw new LuaError("table index is nil");
         }
         else if (!value.IsNil())
         {
             m_values[key] = value;
         }
         else
         {
             m_values.Remove(key);
         }
     }
 }
Exemplo n.º 2
0
 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()));
     }
 }