ObjectTranslatorPool.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Tencent is pleased to support the open source community by making xLua available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. #if USE_UNI_LUA
  9. using LuaAPI = UniLua.Lua;
  10. using RealStatePtr = UniLua.ILuaState;
  11. using LuaCSFunction = UniLua.CSharpFunctionDelegate;
  12. #else
  13. using LuaAPI = XLua.LuaDLL.Lua;
  14. using RealStatePtr = System.IntPtr;
  15. using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
  16. #endif
  17. using System.Collections.Generic;
  18. using System;
  19. namespace XLua
  20. {
  21. public class ObjectTranslatorPool
  22. {
  23. #if !SINGLE_ENV
  24. private Dictionary<RealStatePtr, WeakReference> translators = new Dictionary<RealStatePtr, WeakReference>();
  25. RealStatePtr lastPtr = default(RealStatePtr);
  26. #endif
  27. ObjectTranslator lastTranslator = default(ObjectTranslator);
  28. public static ObjectTranslatorPool Instance
  29. {
  30. get
  31. {
  32. return InternalGlobals.objectTranslatorPool;
  33. }
  34. }
  35. #if UNITY_EDITOR || XLUA_GENERAL
  36. public static ObjectTranslator FindTranslator(RealStatePtr L)
  37. {
  38. return InternalGlobals.objectTranslatorPool.Find(L);
  39. }
  40. #endif
  41. public ObjectTranslatorPool ()
  42. {
  43. }
  44. public void Add (RealStatePtr L, ObjectTranslator translator)
  45. {
  46. #if THREAD_SAFE || HOTFIX_ENABLE
  47. lock (this)
  48. #endif
  49. {
  50. lastTranslator = translator;
  51. #if !SINGLE_ENV
  52. var ptr = LuaAPI.xlua_gl(L);
  53. lastPtr = ptr;
  54. translators.Add(ptr , new WeakReference(translator));
  55. #endif
  56. }
  57. }
  58. public ObjectTranslator Find (RealStatePtr L)
  59. {
  60. #if THREAD_SAFE || HOTFIX_ENABLE
  61. lock (this)
  62. #endif
  63. {
  64. #if SINGLE_ENV
  65. return lastTranslator;
  66. #else
  67. var ptr = LuaAPI.xlua_gl(L);
  68. if (lastPtr == ptr) return lastTranslator;
  69. if (translators.ContainsKey(ptr))
  70. {
  71. lastPtr = ptr;
  72. lastTranslator = translators[ptr].Target as ObjectTranslator;
  73. return lastTranslator;
  74. }
  75. return null;
  76. #endif
  77. }
  78. }
  79. public void Remove (RealStatePtr L)
  80. {
  81. #if THREAD_SAFE || HOTFIX_ENABLE
  82. lock (this)
  83. #endif
  84. {
  85. #if SINGLE_ENV
  86. lastTranslator = default(ObjectTranslator);
  87. #else
  88. var ptr = LuaAPI.xlua_gl(L);
  89. if (!translators.ContainsKey (ptr))
  90. return;
  91. if (lastPtr == ptr)
  92. {
  93. lastPtr = default(RealStatePtr);
  94. lastTranslator = default(ObjectTranslator);
  95. }
  96. translators.Remove(ptr);
  97. #endif
  98. }
  99. }
  100. }
  101. }