From 107a98686a105468b5f200ebcbd27343c1210ce4 Mon Sep 17 00:00:00 2001 From: RogueAI42 Date: Sun, 11 Jun 2017 17:34:38 +1000 Subject: ReflectMan Saves The Day.............. Refactored every part in the code that used that damn foreach loop. All assemblies are now loaded on startup into an array, and the results of GetType() on each are concatenated into another array. The parts of the code that were loading the assemblies and scanning them themselves now look to ReflectMan.Types, and all disk I/O is limited to the first time ReflectMan is accessed. While I was there I also replaced some other foreach loops with array comprehensions and such to speed things up - there is a noticeable improvement. It doesn't seem to have broken anything, but I'd appreciate if someone could do more stress testing for me. --- ShiftOS_TheReturn/Scripting.cs | 53 +++++------------------------------------- 1 file changed, 6 insertions(+), 47 deletions(-) (limited to 'ShiftOS_TheReturn/Scripting.cs') diff --git a/ShiftOS_TheReturn/Scripting.cs b/ShiftOS_TheReturn/Scripting.cs index 5021f50..081a6ae 100644 --- a/ShiftOS_TheReturn/Scripting.cs +++ b/ShiftOS_TheReturn/Scripting.cs @@ -205,55 +205,14 @@ end"); //This temporary proxy() method will be used by the API prober. Lua.proxy = new Func((objName) => { - foreach (var f in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if (f.EndsWith(".exe") || f.EndsWith(".dll")) - { - try - { - - var asm = Assembly.LoadFile(f); - foreach (var type in asm.GetTypes()) - { - if (type.Name == objName) - { - dynamic dynObj = Activator.CreateInstance(type); - return dynObj; - } - - } - } - catch { } - } - } + dynamic dynObj = ReflectMan.Types.FirstOrDefault(t => t.Name == objName); + if (dynObj != null) + return dynObj; throw new Exception("{CLASS_NOT_FOUND}"); }); - - foreach (var f in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if (f.EndsWith(".exe") || f.EndsWith(".dll")) - { - try - { - var thisasm = Assembly.LoadFile(f); - foreach (var type in thisasm.GetTypes()) - { - foreach (var attr in type.GetCustomAttributes(false)) - { - if (attr is ExposedAttribute) - { - var eattr = attr as ExposedAttribute; - Lua($"{eattr.Name} = proxy(\"{type.Name}\")"); - } - } - } - } - catch - { - - } - } - } + foreach (var type in ReflectMan.Types) + foreach (var attr in Array.FindAll(type.GetCustomAttributes(false), a => a is ExposedAttribute)) + Lua($"{(attr as ExposedAttribute).Name} = proxy(\"{type.Name}\")"); //Now we can null out the proxy() method as it can cause security risks. Lua.isRunning = new Func(() => { return this.Running; }); Lua.proxy = null; -- cgit v1.2.3