LuaTable.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace LuaInterface
  6. {
  7. /*
  8. * Wrapper class for Lua tables
  9. *
  10. * Author: Fabio Mascarenhas
  11. * Version: 1.0
  12. */
  13. public class LuaTable : LuaBase
  14. {
  15. //internal int _Reference;
  16. //private Lua _Interpreter;
  17. public LuaTable(int reference, LuaState interpreter)
  18. {
  19. _Reference = reference;
  20. _Interpreter = interpreter;
  21. }
  22. //bool disposed = false;
  23. //~LuaTable()
  24. //{
  25. // Dispose(false);
  26. //}
  27. //public void Dispose()
  28. //{
  29. // Dispose(true);
  30. // GC.SuppressFinalize(this);
  31. //}
  32. //public virtual void Dispose(bool disposeManagedResources)
  33. //{
  34. // if (!this.disposed)
  35. // {
  36. // if (disposeManagedResources)
  37. // {
  38. // if (_Reference != 0)
  39. // _Interpreter.dispose(_Reference);
  40. // }
  41. // disposed = true;
  42. // }
  43. //}
  44. //~LuaTable()
  45. //{
  46. // _Interpreter.dispose(_Reference);
  47. //}
  48. /*
  49. * Indexer for string fields of the table
  50. */
  51. public object this[string field]
  52. {
  53. get
  54. {
  55. return _Interpreter.getObject(_Reference, field);
  56. }
  57. set
  58. {
  59. _Interpreter.setObject(_Reference, field, value);
  60. }
  61. }
  62. /*
  63. * Indexer for numeric fields of the table
  64. */
  65. public object this[object field]
  66. {
  67. get
  68. {
  69. return _Interpreter.getObject(_Reference, field);
  70. }
  71. set
  72. {
  73. _Interpreter.setObject(_Reference, field, value);
  74. }
  75. }
  76. public System.Collections.IDictionaryEnumerator GetEnumerator()
  77. {
  78. return _Interpreter.GetTableDict(this).GetEnumerator();
  79. }
  80. public ICollection Keys
  81. {
  82. get { return _Interpreter.GetTableDict(this).Keys; }
  83. }
  84. public ICollection Values
  85. {
  86. get { return _Interpreter.GetTableDict(this).Values; }
  87. }
  88. public void SetMetaTable(LuaTable metaTable)
  89. {
  90. push(_Interpreter.L);
  91. metaTable.push(_Interpreter.L);
  92. LuaDLL.lua_setmetatable(_Interpreter.L, -2);
  93. LuaDLL.lua_pop(_Interpreter.L, 1);
  94. }
  95. /*
  96. * Gets an string fields of a table ignoring its metatable,
  97. * if it exists
  98. */
  99. internal object rawget(string field)
  100. {
  101. return _Interpreter.rawGetObject(_Reference, field);
  102. }
  103. internal object rawgetFunction(string field)
  104. {
  105. object obj = _Interpreter.rawGetObject(_Reference, field);
  106. if (obj is LuaCSFunction)
  107. return new LuaFunction((LuaCSFunction)obj, _Interpreter);
  108. else
  109. return obj;
  110. }
  111. /*
  112. * Pushes this table into the Lua stack
  113. */
  114. public void push(IntPtr luaState)
  115. {
  116. LuaDLL.lua_getref(luaState, _Reference);
  117. }
  118. public override string ToString()
  119. {
  120. return "table";
  121. }
  122. //public override bool Equals(object o)
  123. //{
  124. // if (o is LuaTable)
  125. // {
  126. // LuaTable l = (LuaTable)o;
  127. // return _Interpreter.compareRef(l._Reference, _Reference);
  128. // }
  129. // else return false;
  130. //}
  131. //public override int GetHashCode()
  132. //{
  133. // return _Reference;
  134. //}
  135. }
  136. }