CLRSharp_Env.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CLRSharp
  5. {
  6. public class CLRSharp_Environment : ICLRSharp_Environment
  7. {
  8. public string version
  9. {
  10. get
  11. {
  12. return "0.36.5Alpha";
  13. }
  14. }
  15. public ICLRSharp_Logger logger
  16. {
  17. get;
  18. private set;
  19. }
  20. public CLRSharp_Environment(ICLRSharp_Logger logger)
  21. {
  22. this.logger = logger;
  23. logger.Log_Warning("CLR# Ver:" + version + " Inited.");
  24. this.RegCrossBind(new CrossBind_IEnumerable());
  25. this.RegCrossBind(new CrossBind_IEnumerator());
  26. }
  27. Dictionary<string, ICLRType> mapType = new Dictionary<string, ICLRType>();
  28. //public Dictionary<string, Mono.Cecil.ModuleDefinition> mapModule = new Dictionary<string, Mono.Cecil.ModuleDefinition>();
  29. public void LoadModule(System.IO.Stream dllStream)
  30. {
  31. LoadModule(dllStream, null, null);
  32. }
  33. public void LoadModule(System.IO.Stream dllStream, System.IO.Stream pdbStream, Mono.Cecil.Cil.ISymbolReaderProvider debugInfoLoader)
  34. {
  35. var module = Mono.Cecil.ModuleDefinition.ReadModule(dllStream);
  36. if (debugInfoLoader != null && pdbStream != null)
  37. {
  38. module.ReadSymbols(debugInfoLoader.GetSymbolReader(module, pdbStream));
  39. }
  40. //mapModule[module.Name] = module;
  41. if (module.HasTypes)
  42. {
  43. foreach (var t in module.Types)
  44. {
  45. mapType[t.FullName] = new Type_Common_CLRSharp(this, t);
  46. }
  47. }
  48. if (module.HasAssemblyReferences)
  49. {
  50. foreach (var ar in module.AssemblyReferences)
  51. {
  52. if (moduleref.Contains(ar.Name) == false)
  53. moduleref.Add(ar.Name);
  54. }
  55. }
  56. }
  57. public List<System.Reflection.Assembly> assemblylist;
  58. public void AddSerachAssembly(System.Reflection.Assembly assembly)
  59. {
  60. if (assemblylist == null)
  61. assemblylist = new List<System.Reflection.Assembly>();
  62. assemblylist.Add(assembly);
  63. }
  64. public void LoadModule_OnlyName(System.IO.Stream dllStream)
  65. {
  66. var module = Mono.Cecil.ModuleDefinition.ReadModule(dllStream);
  67. if (moduleref.Contains(module.Name) == false)
  68. moduleref.Add(module.Name);
  69. if (module.HasAssemblyReferences)
  70. {
  71. foreach (var ar in module.AssemblyReferences)
  72. {
  73. if (moduleref.Contains(ar.Name) == false)
  74. moduleref.Add(ar.Name);
  75. }
  76. }
  77. }
  78. public CodeBody CreateCodeBody(Method_Common_CLRSharp method)
  79. {
  80. return new CodeBody(this, method.method_CLRSharp);
  81. }
  82. List<string> moduleref = new List<string>();
  83. public string[] GetAllTypes()
  84. {
  85. string[] array = new string[mapType.Count];
  86. mapType.Keys.CopyTo(array, 0);
  87. return array;
  88. }
  89. public string[] GetModuleRefNames()
  90. {
  91. return moduleref.ToArray();
  92. }
  93. //得到类型的时候应该得到模块内Type或者真实Type
  94. //一个统一的Type,然后根据具体情况调用两边
  95. public ICLRType GetType(string fullname)
  96. {
  97. ICLRType type = null;
  98. bool b = mapType.TryGetValue(fullname, out type);
  99. if (!b)
  100. {
  101. List<ICLRType> subTypes = new List<ICLRType>();
  102. if (fullname.Contains("<>") || fullname.Contains("/"))//匿名类型
  103. {
  104. string[] subts = fullname.Split('/');
  105. ICLRType ft = GetType(subts[0]);
  106. if (ft is ICLRType_Sharp)
  107. {
  108. for (int i = 1; i < subts.Length; i++)
  109. {
  110. ft = ft.GetNestType(this, subts[i]);
  111. }
  112. return ft;
  113. }
  114. }
  115. string fullnameT = fullname;//.Replace('/', '+');
  116. if (fullnameT.Contains("<"))
  117. {
  118. string outname = "";
  119. int depth = 0;
  120. int lastsplitpos = 0;
  121. for (int i = 0; i < fullname.Length; i++)
  122. {
  123. string checkname = null;
  124. if (fullname[i] == '/')
  125. {
  126. }
  127. else if (fullname[i] == '<')
  128. {
  129. if (i != 0)
  130. depth++;
  131. if (depth == 1)//
  132. {
  133. lastsplitpos = i;
  134. outname += "[";
  135. continue;
  136. }
  137. }
  138. else if (fullname[i] == '>')
  139. {
  140. if (depth == 1)
  141. {
  142. checkname = fullnameT.Substring(lastsplitpos + 1, i - lastsplitpos - 1);
  143. var subtype = GetType(checkname);
  144. subTypes.Add(subtype);
  145. if (subtype is ICLRType_Sharp) subtype = GetType(typeof(CLRSharp_Instance));
  146. outname += "[" + subtype.FullNameWithAssembly + "]";
  147. lastsplitpos = i;
  148. }
  149. //if(depth>0)
  150. depth--;
  151. if (depth == 0)
  152. {
  153. outname += "]";
  154. continue;
  155. }
  156. else if (depth < 0)
  157. {
  158. depth = 0;
  159. }
  160. }
  161. else if (fullname[i] == ',')
  162. {
  163. if (depth == 1)
  164. {
  165. checkname = fullnameT.Substring(lastsplitpos + 1, i - lastsplitpos - 1);
  166. var subtype = GetType(checkname);
  167. subTypes.Add(subtype);
  168. if (subtype is ICLRType_Sharp) subtype = GetType(typeof(CLRSharp_Instance));
  169. outname += "[" + subtype.FullNameWithAssembly + "],";
  170. lastsplitpos = i;
  171. }
  172. }
  173. if (depth == 0)
  174. {
  175. outname += fullnameT[i];
  176. }
  177. }
  178. fullnameT = outname;
  179. // fullnameT = fullnameT.Replace('<', '[');
  180. //fullnameT = fullnameT.Replace('>', ']');
  181. }
  182. fullnameT = fullnameT.Replace('/', '+');
  183. System.Type t = System.Type.GetType(fullnameT);
  184. if (t == null)
  185. {
  186. if (assemblylist != null)
  187. {
  188. foreach (var i in assemblylist)
  189. {
  190. t = i.GetType(fullnameT);
  191. if (t != null)
  192. break;
  193. }
  194. }
  195. if (t == null)
  196. {
  197. foreach (var rm in moduleref)
  198. {
  199. t = System.Type.GetType(fullnameT + "," + rm);
  200. if (t != null)
  201. {
  202. fullnameT = fullnameT + "," + rm;
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. if (t != null)
  209. {
  210. type = new Type_Common_System(this, t, subTypes.ToArray());
  211. }
  212. mapType[fullname] = type;
  213. }
  214. return type;
  215. }
  216. public ICLRType GetType(System.Type systemType)
  217. {
  218. ICLRType type = null;
  219. bool b = mapType.TryGetValue(systemType.FullName, out type);
  220. if (!b)
  221. {
  222. type = new Type_Common_System(this, systemType, null);
  223. mapType[systemType.FullName] = type;
  224. }
  225. return type;
  226. }
  227. public void RegType(ICLRType type)
  228. {
  229. mapType[type.FullName] = type;
  230. }
  231. /// <summary>
  232. /// 交叉绑定工具,让脚本继承程序类型用的
  233. /// </summary>
  234. Dictionary<Type, ICrossBind> crossBind = new Dictionary<Type, ICrossBind>();
  235. public void RegCrossBind(ICrossBind bind)
  236. {
  237. crossBind[bind.Type] = bind;
  238. }
  239. public ICrossBind GetCrossBind(Type type)
  240. {
  241. ICrossBind bind = null;
  242. crossBind.TryGetValue(type, out bind);
  243. return bind;
  244. }
  245. }
  246. }