void AddMetaRefs(ref CSharpScriptEngine engine, IEnumerable<XElement> elms) { foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { engine.AddReference(MetadataReference.CreateFromFile(ass.Location)); } }
/// <summary> /// Read & Load the scripts /// </summary> public void load() { _scripts.Clear(); XDocument doc = XDocument.Load(XmlDirectory); var scripts = from script in doc.Descendants("Scripts") select script; foreach (XElement script in scripts.Elements("Script")) { Quest quest = null; string file = DefaultDirectory + @script.Attribute("File").Value; string language = script.Attribute("Language").Value; string classSpace = script.Attribute("ClassSpace").Value; XElement xreferences = script.Element("References"); var references = from reference in xreferences.Descendants("Reference") select reference; if (file.EndsWith(".cs")) { CSharpScriptEngine engine = new CSharpScriptEngine(file); engine.AssemblySpaceName = classSpace; AddMetaRefs(ref engine, references); engine.Build(); try { Quest q = engine.NewInstance() as Quest;//loaded quest _scripts.Add(q); if (q.PrintDebug) { Logger.WriteLine("Quest: [{0}]: Loaded Quest. [C#]", q.Name); } engine.Dispose(); } catch (Exception e) { Logger.WriteLine("Error in Script not instance of Quest: " + file); Logger.WriteLine("Error es: " + e.Message); } } else if (file.EndsWith(".vb")) { VBScriptEngine engine = new VBScriptEngine(file); engine.AssemblySpaceName = classSpace; AddMetaRefs(ref engine, references); engine.Build(); try { Quest q = (Quest)engine.NewInstance(); _scripts.Add(q); if (q.PrintDebug) { Logger.WriteLine("Quest: [{0}]: Loaded Quest. [VB]", q.Name); } engine.Dispose(); } catch { Logger.WriteLine("Error in Script not instance of Quest: " + file); } } else if (file.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)) { LuaEngine engine = new LuaEngine(file); int q_Id = (int)engine.GetProperties("QuestId"); string name = (string)engine.GetProperties("Name"); string descr = (string)engine.GetProperties("Descr"); quest = new Quest(q_Id, name, descr); quest.loadByScriptEngine(engine); } else if (file.EndsWith(".py")) { //action } } Logger.WriteLine("[Scripting]: Loaded {0} Quests.", _scripts.Where(q => q.IsRealQuest).Count()); }