ObjectTranslator.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Copyright (c) 2015-2017 topameng(topameng@qq.com)
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Runtime.CompilerServices;
  22. using UnityEngine;
  23. namespace LuaInterface
  24. {
  25. public class ObjectTranslator
  26. {
  27. private class DelayGC
  28. {
  29. public DelayGC(int id, UnityEngine.Object obj, float time)
  30. {
  31. this.id = id;
  32. this.time = time;
  33. this.obj = obj;
  34. }
  35. public int id;
  36. public UnityEngine.Object obj;
  37. public float time;
  38. }
  39. private class CompareObject : IEqualityComparer<object>
  40. {
  41. public new bool Equals(object x, object y)
  42. {
  43. return object.ReferenceEquals(x, y);
  44. }
  45. public int GetHashCode(object obj)
  46. {
  47. return RuntimeHelpers.GetHashCode(obj);
  48. }
  49. }
  50. public bool LogGC { get; set; }
  51. public readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>(257, new CompareObject());
  52. public readonly LuaObjectPool objects = new LuaObjectPool();
  53. private List<DelayGC> gcList = new List<DelayGC>();
  54. private Action<object, int> removeInvalidObject;
  55. #if !MULTI_STATE
  56. private static ObjectTranslator _translator = null;
  57. #endif
  58. public ObjectTranslator()
  59. {
  60. LogGC = false;
  61. #if !MULTI_STATE
  62. _translator = this;
  63. #endif
  64. removeInvalidObject = RemoveObject;
  65. }
  66. public int AddObject(object obj)
  67. {
  68. int index = objects.Add(obj);
  69. if (!TypeChecker.IsValueType(obj.GetType()))
  70. {
  71. objectsBackMap[obj] = index;
  72. }
  73. return index;
  74. }
  75. public static ObjectTranslator Get(IntPtr L)
  76. {
  77. #if !MULTI_STATE
  78. return _translator;
  79. #else
  80. return LuaState.GetTranslator(L);
  81. #endif
  82. }
  83. //fixed 枚举唯一性问题(对象唯一,没有实现__eq操作符)
  84. void RemoveObject(object o, int udata)
  85. {
  86. int index = -1;
  87. if (objectsBackMap.TryGetValue(o, out index) && index == udata)
  88. {
  89. objectsBackMap.Remove(o);
  90. }
  91. }
  92. //lua gc一个对象(lua 库不再引用,但不代表c#没使用)
  93. public void RemoveObject(int udata)
  94. {
  95. //只有lua gc才能移除
  96. object o = objects.Remove(udata);
  97. if (o != null)
  98. {
  99. if (!TypeChecker.IsValueType(o.GetType()))
  100. {
  101. RemoveObject(o, udata);
  102. }
  103. if (LogGC)
  104. {
  105. Debugger.Log("gc object {0}, id {1}", o, udata);
  106. }
  107. }
  108. }
  109. public object GetObject(int udata)
  110. {
  111. return objects.TryGetValue(udata);
  112. }
  113. //预删除,但不移除一个lua对象(移除id只能由gc完成)
  114. public void Destroy(int udata)
  115. {
  116. object o = objects.Destroy(udata);
  117. if (o != null)
  118. {
  119. if (!TypeChecker.IsValueType(o.GetType()))
  120. {
  121. RemoveObject(o, udata);
  122. }
  123. if (LogGC)
  124. {
  125. Debugger.Log("destroy object {0}, id {1}", o, udata);
  126. }
  127. }
  128. }
  129. //Unity Object 延迟删除
  130. public void DelayDestroy(int id, float time)
  131. {
  132. UnityEngine.Object obj = (UnityEngine.Object)GetObject(id);
  133. if (obj != null)
  134. {
  135. gcList.Add(new DelayGC(id, obj, time));
  136. }
  137. }
  138. public bool Getudata(object o, out int index)
  139. {
  140. index = -1;
  141. return objectsBackMap.TryGetValue(o, out index);
  142. }
  143. public void Destroyudata(int udata)
  144. {
  145. objects.Destroy(udata);
  146. }
  147. public void SetBack(int index, object o)
  148. {
  149. objects.Replace(index, o);
  150. }
  151. bool RemoveFromGCList(int id)
  152. {
  153. int index = gcList.FindIndex((p) => { return p.id == id; });
  154. if (index >= 0)
  155. {
  156. gcList.RemoveAt(index);
  157. return true;
  158. }
  159. return false;
  160. }
  161. //延迟删除处理
  162. void DestroyUnityObject(int udata, UnityEngine.Object obj)
  163. {
  164. object o = objects.TryGetValue(udata);
  165. if (object.ReferenceEquals(o, obj))
  166. {
  167. RemoveObject(o, udata);
  168. //一定不能Remove, 因为GC还可能再来一次
  169. objects.Destroy(udata);
  170. if (LogGC)
  171. {
  172. Debugger.Log("destroy object {0}, id {1}", o, udata);
  173. }
  174. }
  175. UnityEngine.Object.Destroy(obj);
  176. }
  177. public void Collect()
  178. {
  179. if (gcList.Count == 0)
  180. {
  181. return;
  182. }
  183. float delta = Time.deltaTime;
  184. for (int i = gcList.Count - 1; i >= 0; i--)
  185. {
  186. float time = gcList[i].time - delta;
  187. if (time <= 0)
  188. {
  189. DestroyUnityObject(gcList[i].id, gcList[i].obj);
  190. gcList.RemoveAt(i);
  191. }
  192. else
  193. {
  194. gcList[i].time = time;
  195. }
  196. }
  197. }
  198. public void StepCollect()
  199. {
  200. objects.StepCollect(removeInvalidObject);
  201. }
  202. public void Dispose()
  203. {
  204. objectsBackMap.Clear();
  205. objects.Clear();
  206. #if !MULTI_STATE
  207. _translator = null;
  208. #endif
  209. }
  210. }
  211. }