Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using Styx.Helpers;
- using Microsoft.CSharp;
- using System.Reflection;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using Styx.CustomCombat.CombatInterface;
- namespace Styx.Logic.Common.Combat.CustomCombat
- {
- public class CustomCombat : ICombat
- {
- readonly object _classInstance;
- readonly Type _classType;
- public CustomCombat(Type implementsICombat)
- {
- _classInstance = Activator.CreateInstance(implementsICombat);
- _classType = implementsICombat;
- }
- public CustomCombat(string path)
- {
- CompilerResults results;
- using (var provider = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v3.5" } }))
- {
- var options = new CompilerParameters
- {
- GenerateExecutable = false,
- IncludeDebugInformation = false,
- GenerateInMemory = true,
- WarningLevel = 3,
- TreatWarningsAsErrors = false,
- CompilerOptions = "/optimize",
- };
- options.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);
- foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
- {
- try
- {
- string location = assembly.Location;
- if (!String.IsNullOrEmpty(location))
- {
- options.ReferencedAssemblies.Add(location);
- }
- }
- catch (NotSupportedException)
- {
- // this happens for dynamic assemblies, so just ignore it.
- }
- }
- results = provider.CompileAssemblyFromFile(options, path);
- if (results.Errors.HasErrors)
- {
- var errors = new StringBuilder();
- foreach (CompilerError error in results.Errors)
- {
- errors.Append(error.FileName);
- errors.AppendFormat("Line: {0} Error: {1}", error.Line, error.ErrorText);
- }
- Logging.Write(errors.ToString());
- }
- else
- {
- foreach (Type t in results.CompiledAssembly.GetTypes())
- {
- if (t.GetInterface(typeof(ICombat).FullName) != null)
- {
- // We've found the custom class.
- // Create an instance of it
- _classInstance = Activator.CreateInstance(t);
- _classType = t;
- }
- }
- }
- }
- }
- public string Name
- {
- get { return InvokeMember<string>("Name", BindingFlags.GetProperty, false, null); }
- }
- public bool NeedRest
- {
- get { return InvokeMember<bool>("NeedRest", BindingFlags.GetProperty, false, null); }
- }
- public bool NeedPreCombatBuffs
- {
- get { return InvokeMember<bool>("NeedPreCombatBuffs", BindingFlags.GetProperty, false, null); }
- }
- public bool NeedCombatBuffs
- {
- get { return InvokeMember<bool>("NeedCombatBuffs", BindingFlags.GetProperty, false, null); }
- }
- public bool NeedHeal
- {
- get { return InvokeMember<bool>("NeedHeal", BindingFlags.GetProperty, false, null); }
- }
- public bool NeedPullBuffs
- {
- get { return InvokeMember<bool>("NeedPullBuffs", BindingFlags.GetProperty, false, null); }
- }
- public void Rest()
- {
- InvokeMember("Rest", BindingFlags.InvokeMethod, false, null);
- }
- public void PreCombatBuff()
- {
- InvokeMember("PreCombatBuff", BindingFlags.InvokeMethod, false, null);
- }
- public void CombatBuff()
- {
- InvokeMember("CombatBuff", BindingFlags.InvokeMethod, false, null);
- }
- public void Heal()
- {
- InvokeMember("Heal", BindingFlags.InvokeMethod, false, null);
- }
- public void PullBuff()
- {
- InvokeMember("PullBuff", BindingFlags.InvokeMethod, false, null);
- }
- public void Pull()
- {
- InvokeMember("Pull", BindingFlags.InvokeMethod, false, null);
- }
- public void Combat()
- {
- InvokeMember("Combat", BindingFlags.InvokeMethod, false, null);
- }
- public void HandleFalling()
- {
- InvokeMember("HandleFalling", BindingFlags.InvokeMethod, false, null);
- }
- private void InvokeMember(string memberName, BindingFlags type, bool isStatic, params object[] args)
- {
- try
- {
- BindingFlags staticOrInstance = isStatic ? BindingFlags.Static : BindingFlags.Instance;
- _classType.InvokeMember(memberName, BindingFlags.Public | BindingFlags.NonPublic | staticOrInstance | type,
- null, _classInstance, args);
- }
- catch (Exception ex)
- {
- Logging.WriteException(ex.InnerException);
- }
- }
- private T InvokeMember<T>(string memberName, BindingFlags type, bool isStatic, params object[] args)
- {
- BindingFlags staticOrInstance = isStatic ? BindingFlags.Static : BindingFlags.Instance;
- try
- {
- return (T)_classType.InvokeMember(
- memberName,
- BindingFlags.Public | BindingFlags.NonPublic | staticOrInstance | type,
- null,
- _classInstance, args
- );
- }
- catch (Exception ex)
- {
- Logging.WriteException(ex.InnerException);
- return default(T);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment