Guest User

Untitled

a guest
Jul 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using Styx.Helpers;
  4. using Microsoft.CSharp;
  5. using System.Reflection;
  6. using System.CodeDom.Compiler;
  7. using System.Collections.Generic;
  8. using Styx.CustomCombat.CombatInterface;
  9.  
  10. namespace Styx.Logic.Common.Combat.CustomCombat
  11. {
  12. public class CustomCombat : ICombat
  13. {
  14. readonly object _classInstance;
  15. readonly Type _classType;
  16.  
  17. public CustomCombat(Type implementsICombat)
  18. {
  19. _classInstance = Activator.CreateInstance(implementsICombat);
  20. _classType = implementsICombat;
  21.  
  22. }
  23. public CustomCombat(string path)
  24. {
  25. CompilerResults results;
  26. using (var provider = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v3.5" } }))
  27. {
  28. var options = new CompilerParameters
  29. {
  30. GenerateExecutable = false,
  31. IncludeDebugInformation = false,
  32. GenerateInMemory = true,
  33. WarningLevel = 3,
  34. TreatWarningsAsErrors = false,
  35. CompilerOptions = "/optimize",
  36. };
  37.  
  38. options.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);
  39.  
  40. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  41. {
  42. try
  43. {
  44. string location = assembly.Location;
  45. if (!String.IsNullOrEmpty(location))
  46. {
  47. options.ReferencedAssemblies.Add(location);
  48. }
  49. }
  50. catch (NotSupportedException)
  51. {
  52. // this happens for dynamic assemblies, so just ignore it.
  53. }
  54. }
  55.  
  56. results = provider.CompileAssemblyFromFile(options, path);
  57.  
  58. if (results.Errors.HasErrors)
  59. {
  60. var errors = new StringBuilder();
  61. foreach (CompilerError error in results.Errors)
  62. {
  63. errors.Append(error.FileName);
  64. errors.AppendFormat("Line: {0} Error: {1}", error.Line, error.ErrorText);
  65. }
  66.  
  67. Logging.Write(errors.ToString());
  68. }
  69. else
  70. {
  71. foreach (Type t in results.CompiledAssembly.GetTypes())
  72. {
  73. if (t.GetInterface(typeof(ICombat).FullName) != null)
  74. {
  75. // We've found the custom class.
  76. // Create an instance of it
  77. _classInstance = Activator.CreateInstance(t);
  78. _classType = t;
  79. }
  80. }
  81. }
  82. }
  83. }
  84.  
  85. public string Name
  86. {
  87. get { return InvokeMember<string>("Name", BindingFlags.GetProperty, false, null); }
  88. }
  89.  
  90. public bool NeedRest
  91. {
  92. get { return InvokeMember<bool>("NeedRest", BindingFlags.GetProperty, false, null); }
  93. }
  94.  
  95. public bool NeedPreCombatBuffs
  96. {
  97. get { return InvokeMember<bool>("NeedPreCombatBuffs", BindingFlags.GetProperty, false, null); }
  98. }
  99.  
  100. public bool NeedCombatBuffs
  101. {
  102. get { return InvokeMember<bool>("NeedCombatBuffs", BindingFlags.GetProperty, false, null); }
  103. }
  104.  
  105. public bool NeedHeal
  106. {
  107. get { return InvokeMember<bool>("NeedHeal", BindingFlags.GetProperty, false, null); }
  108. }
  109.  
  110. public bool NeedPullBuffs
  111. {
  112. get { return InvokeMember<bool>("NeedPullBuffs", BindingFlags.GetProperty, false, null); }
  113. }
  114.  
  115. public void Rest()
  116. {
  117. InvokeMember("Rest", BindingFlags.InvokeMethod, false, null);
  118. }
  119.  
  120. public void PreCombatBuff()
  121. {
  122. InvokeMember("PreCombatBuff", BindingFlags.InvokeMethod, false, null);
  123. }
  124.  
  125. public void CombatBuff()
  126. {
  127. InvokeMember("CombatBuff", BindingFlags.InvokeMethod, false, null);
  128. }
  129.  
  130. public void Heal()
  131. {
  132. InvokeMember("Heal", BindingFlags.InvokeMethod, false, null);
  133. }
  134.  
  135. public void PullBuff()
  136. {
  137. InvokeMember("PullBuff", BindingFlags.InvokeMethod, false, null);
  138. }
  139.  
  140. public void Pull()
  141. {
  142. InvokeMember("Pull", BindingFlags.InvokeMethod, false, null);
  143. }
  144.  
  145. public void Combat()
  146. {
  147. InvokeMember("Combat", BindingFlags.InvokeMethod, false, null);
  148. }
  149.  
  150. public void HandleFalling()
  151. {
  152. InvokeMember("HandleFalling", BindingFlags.InvokeMethod, false, null);
  153. }
  154.  
  155. private void InvokeMember(string memberName, BindingFlags type, bool isStatic, params object[] args)
  156. {
  157. try
  158. {
  159. BindingFlags staticOrInstance = isStatic ? BindingFlags.Static : BindingFlags.Instance;
  160. _classType.InvokeMember(memberName, BindingFlags.Public | BindingFlags.NonPublic | staticOrInstance | type,
  161. null, _classInstance, args);
  162. }
  163. catch (Exception ex)
  164. {
  165. Logging.WriteException(ex.InnerException);
  166. }
  167. }
  168.  
  169. private T InvokeMember<T>(string memberName, BindingFlags type, bool isStatic, params object[] args)
  170. {
  171.  
  172. BindingFlags staticOrInstance = isStatic ? BindingFlags.Static : BindingFlags.Instance;
  173.  
  174. try
  175. {
  176. return (T)_classType.InvokeMember(
  177. memberName,
  178. BindingFlags.Public | BindingFlags.NonPublic | staticOrInstance | type,
  179. null,
  180. _classInstance, args
  181. );
  182. }
  183. catch (Exception ex)
  184. {
  185. Logging.WriteException(ex.InnerException);
  186. return default(T);
  187. }
  188. }
  189. }
  190. }
Add Comment
Please, Sign In to add comment