diff options
| author | Michael VanOverbeek <[email protected]> | 2017-06-11 11:57:31 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-06-11 11:57:31 +0000 |
| commit | 997a81457ebb6d6523c36ca552cee143e0d92244 (patch) | |
| tree | da98985ced112b6d238811f3ca770014d67e34fc /ShiftOS_TheReturn | |
| parent | 0d75f701778a0900a58343c4c80c124279bc231f (diff) | |
| parent | 107a98686a105468b5f200ebcbd27343c1210ce4 (diff) | |
| download | shiftos_thereturn-997a81457ebb6d6523c36ca552cee143e0d92244.tar.gz shiftos_thereturn-997a81457ebb6d6523c36ca552cee143e0d92244.tar.bz2 shiftos_thereturn-997a81457ebb6d6523c36ca552cee143e0d92244.zip | |
Merge pull request #129 from RogueAI42/master
ReflectMan Saves The Day..............
Diffstat (limited to 'ShiftOS_TheReturn')
| -rw-r--r-- | ShiftOS_TheReturn/AppLauncherDaemon.cs | 53 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/AppearanceManager.cs | 20 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/Commands.cs | 89 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/NotificationDaemon.cs | 21 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/ReflectMan.cs | 80 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/Scripting.cs | 53 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/ServerManager.cs | 27 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/ShiftOS.Engine.csproj | 1 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/Shiftorium.cs | 22 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/Skinning.cs | 34 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/Story.cs | 65 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/TerminalBackend.cs | 66 |
12 files changed, 187 insertions, 344 deletions
diff --git a/ShiftOS_TheReturn/AppLauncherDaemon.cs b/ShiftOS_TheReturn/AppLauncherDaemon.cs index 716a6a3..c259556 100644 --- a/ShiftOS_TheReturn/AppLauncherDaemon.cs +++ b/ShiftOS_TheReturn/AppLauncherDaemon.cs @@ -63,55 +63,16 @@ namespace ShiftOS.Engine { List<LauncherItem> win = new List<LauncherItem>(); - foreach (var asmExec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) + foreach (var type in Array.FindAll(ReflectMan.Types, t => t.GetInterfaces().Contains(typeof(IShiftOSWindow)) && Shiftorium.UpgradeAttributesUnlocked(t))) { - if (asmExec.EndsWith(".dll") | asmExec.EndsWith(".exe")) - { - try - { - var asm = Assembly.LoadFrom(asmExec); - - if (asm.GetReferencedAssemblies().Contains("ShiftOS.Engine") || asm.FullName.Contains("ShiftOS.Engine")) + foreach (var attr in type.GetCustomAttributes(false)) + if (!(attr is MultiplayerOnlyAttribute && !KernelWatchdog.MudConnected)) + if (attr is LauncherAttribute) { - foreach (var type in asm.GetTypes()) - { - if (type.GetInterfaces().Contains(typeof(IShiftOSWindow))) - { - bool isAllowed = true; - foreach (var attr in type.GetCustomAttributes(false)) - { - if(attr is MultiplayerOnlyAttribute) - { - if(KernelWatchdog.MudConnected == false) - { - isAllowed = false; - - } - } - if (isAllowed == true) - { - if (attr is LauncherAttribute) - { - if (Shiftorium.UpgradeAttributesUnlocked(type)) - { - var launch = attr as LauncherAttribute; - if (launch.UpgradeInstalled) - { - win.Add(new LauncherItem { DisplayData = launch, LaunchType = type }); - } - } - } - } - } - } - } + var launch = attr as LauncherAttribute; + if (launch.UpgradeInstalled) + win.Add(new LauncherItem { DisplayData = launch, LaunchType = type }); } - } - catch - { - - } - } } foreach(var file in Utils.GetFiles(Paths.GetPath("applauncher"))) diff --git a/ShiftOS_TheReturn/AppearanceManager.cs b/ShiftOS_TheReturn/AppearanceManager.cs index e244b77..76060dd 100644 --- a/ShiftOS_TheReturn/AppearanceManager.cs +++ b/ShiftOS_TheReturn/AppearanceManager.cs @@ -58,25 +58,7 @@ namespace ShiftOS.Engine //HEY LETS FIND THE WINDOWS public static IEnumerable<Type> GetAllWindowTypes() { - List<Type> types = new List<Type>(); - foreach(var file in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - // hey if a thing is an exe or a dll show up plz kthx - if(file.EndsWith(".exe") || file.EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(file); - foreach(var type in asm.GetTypes()) - { - if (type.GetInterfaces().Contains(typeof(IShiftOSWindow))) - types.Add(type); - } - } - catch { } - } - } - return types; + return Array.FindAll(ReflectMan.Types, t => t.GetInterfaces().Contains(typeof(IShiftOSWindow))); } // hey you know that window we just made appear? well give it its title diff --git a/ShiftOS_TheReturn/Commands.cs b/ShiftOS_TheReturn/Commands.cs index f37bcb3..da4fb29 100644 --- a/ShiftOS_TheReturn/Commands.cs +++ b/ShiftOS_TheReturn/Commands.cs @@ -771,84 +771,41 @@ shiftorium.buy{{upgrade:""{upg.ID}""}}"); { var app = args["app"] as string; //ANNND now we start reflecting... - foreach (var asmExec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) + foreach (var type in Array.FindAll(ReflectMan.Types, t => t.BaseType == typeof(UserControl))) { - if (asmExec.EndsWith(".exe") || asmExec.EndsWith(".dll")) + var attr = type.GetCustomAttributes(false).FirstOrDefault(a => a is WinOpenAttribute && app == (a as WinOpenAttribute).ID); + if (attr != null) { - var asm = Assembly.LoadFile(asmExec); - try + if (SaveSystem.CurrentSave.Upgrades.ContainsKey(app)) { - foreach (var type in asm.GetTypes()) + if (Shiftorium.UpgradeInstalled(app)) { - if (type.BaseType == typeof(UserControl)) - { - foreach (var attr in type.GetCustomAttributes(false)) - { - if (attr is WinOpenAttribute) - { - if (app == (attr as WinOpenAttribute).ID) - { - if (SaveSystem.CurrentSave.Upgrades.ContainsKey(app)) - { - if (Shiftorium.UpgradeInstalled(app)) - { - IShiftOSWindow frm = Activator.CreateInstance(type) as IShiftOSWindow; - AppearanceManager.SetupWindow(frm); - return true; - } - else - { - throw new Exception($"{app} was not found on your system! Try looking in the shiftorium..."); - } - } - else - { - IShiftOSWindow frm = Activator.CreateInstance(type) as IShiftOSWindow; - AppearanceManager.SetupWindow(frm); - return true; - } - } - } - } - } + IShiftOSWindow frm = Activator.CreateInstance(type) as IShiftOSWindow; + AppearanceManager.SetupWindow(frm); + return true; + } + else + { + throw new Exception($"{app} was not found on your system! Try looking in the shiftorium..."); } } - catch { } - + else + { + IShiftOSWindow frm = Activator.CreateInstance(type) as IShiftOSWindow; + AppearanceManager.SetupWindow(frm); + return true; + } } } } else { - foreach (var asmExec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if (asmExec.EndsWith(".exe") || asmExec.EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(asmExec); - - foreach (var type in asm.GetTypes()) - { - if (type.GetInterfaces().Contains(typeof(IShiftOSWindow))) - { - foreach (var attr in type.GetCustomAttributes(false)) - { - if (attr is WinOpenAttribute) - { - if (Shiftorium.UpgradeAttributesUnlocked(type)) - { - Console.WriteLine("win.open{app:\"" + (attr as WinOpenAttribute).ID + "\"}"); - } - } - } - } - } - } - catch { } - } - } + + foreach (var type in Array.FindAll(ReflectMan.Types, t => t.GetInterfaces().Contains(typeof(IShiftOSWindow)) && Shiftorium.UpgradeAttributesUnlocked(t))) + foreach (var attr in Array.FindAll(type.GetCustomAttributes(false), a => a is WinOpenAttribute)) + if (Shiftorium.UpgradeAttributesUnlocked(type)) + Console.WriteLine("win.open{app:\"" + (attr as WinOpenAttribute).ID + "\"}"); return true; diff --git a/ShiftOS_TheReturn/NotificationDaemon.cs b/ShiftOS_TheReturn/NotificationDaemon.cs index 0725782..a532466 100644 --- a/ShiftOS_TheReturn/NotificationDaemon.cs +++ b/ShiftOS_TheReturn/NotificationDaemon.cs @@ -41,26 +41,7 @@ namespace ShiftOS.Engine /// <returns>An array of <see cref="Type"/>s containing the found objects.</returns> public static Type[] GetAllStatusIcons() { - List<Type> lst = new List<Type>(); - foreach(var exec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if(exec.ToLower().EndsWith(".exe") || exec.ToLower().EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(exec); - foreach(var type in asm.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IStatusIcon)))) - { - if (Shiftorium.UpgradeAttributesUnlocked(type)) - { - lst.Add(type); - } - } - } - catch { } - } - } - return lst.ToArray(); + return Array.FindAll(ReflectMan.Types, x => x.GetInterfaces().Contains(typeof(IStatusIcon)) && Shiftorium.UpgradeAttributesUnlocked(x)); } diff --git a/ShiftOS_TheReturn/ReflectMan.cs b/ShiftOS_TheReturn/ReflectMan.cs new file mode 100644 index 0000000..a0ead60 --- /dev/null +++ b/ShiftOS_TheReturn/ReflectMan.cs @@ -0,0 +1,80 @@ +/* + * MIT License + * + * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Engine +{ + public static class ReflectMan + { + private static Assembly[] asms = null; + public static Assembly[] Asms + { + get + { + if (asms == null) + LoadAssemblies(); + return asms; + } + } + + private static Type[] types = null; + public static Type[] Types + { + get + { + if (types == null) + FindTypes(); + return types; + } + } + + private static void LoadAssemblies() + { + var ret = new List<Assembly>(); + foreach (var exe in Array.FindAll(System.IO.Directory.GetFiles(Environment.CurrentDirectory), n => n.EndsWith(".exe") || n.EndsWith(".dll"))) + try + { + var asm = Assembly.LoadFile(exe); + if (asm.GetReferencedAssemblies().Contains("ShiftOS.Engine") || asm.FullName.Contains("ShiftOS.Engine")) + ret.Add(asm); + } + catch { } // probably not a managed assembly + asms = ret.ToArray(); + } + + private static void FindTypes() + { + var ret = new List<Type>(); + foreach (var asm in Asms) + ret.AddRange(asm.GetTypes()); + types = ret.ToArray(); + } + } +} 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<string, dynamic>((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<bool>(() => { return this.Running; }); Lua.proxy = null; diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs index 00f9653..df3e463 100644 --- a/ShiftOS_TheReturn/ServerManager.cs +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -134,32 +134,19 @@ Ping: {ServerManager.DigitalSocietyPing} ms { string[] split = msg.GUID.Split('|'); bool finished = false; - foreach (var exec in Directory.GetFiles(Environment.CurrentDirectory)) - { - if(exec.ToLower().EndsWith(".exe") || exec.ToLower().EndsWith(".dll")) + if (split[0] == SaveSystem.CurrentSave.SystemName) + foreach(var type in Array.FindAll(ReflectMan.Types, x => x.GetInterfaces().Contains(typeof(Server)) && Shiftorium.UpgradeAttributesUnlocked(x))) { - try + var attrib = type.GetCustomAttributes().FirstOrDefault(x => x is ServerAttribute) as ServerAttribute; + if(attrib != null) { - var asm = Assembly.LoadFile(exec); - foreach(var type in asm.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(Server)))) + if(split[1] == attrib.Port.ToString()) { - var attrib = type.GetCustomAttributes().FirstOrDefault(x => x is ServerAttribute) as ServerAttribute; - if(attrib != null) - { - if(split[0] == SaveSystem.CurrentSave.SystemName && split[1] == attrib.Port.ToString()) - { - if (Shiftorium.UpgradeAttributesUnlocked(type)) - { - type.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(x => x.Name == "MessageReceived")?.Invoke(Activator.CreateInstance(type), null); - finished = true; - } - } - } + type.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(x => x.Name == "MessageReceived")?.Invoke(Activator.CreateInstance(type), null); + finished = true; } } - catch { } } - } if (finished == false) { Forward(split[2], "Error", $"{split[0]}:{split[1]}: connection refused"); diff --git a/ShiftOS_TheReturn/ShiftOS.Engine.csproj b/ShiftOS_TheReturn/ShiftOS.Engine.csproj index f70c41e..39299fd 100644 --- a/ShiftOS_TheReturn/ShiftOS.Engine.csproj +++ b/ShiftOS_TheReturn/ShiftOS.Engine.csproj @@ -123,6 +123,7 @@ <DesignTime>True</DesignTime> <DependentUpon>Resources.resx</DependentUpon> </Compile> + <Compile Include="ReflectMan.cs" /> <Compile Include="SaveSystem.cs" /> <Compile Include="Scripting.cs" /> <Compile Include="Server.cs" /> diff --git a/ShiftOS_TheReturn/Shiftorium.cs b/ShiftOS_TheReturn/Shiftorium.cs index 7faf336..0362566 100644 --- a/ShiftOS_TheReturn/Shiftorium.cs +++ b/ShiftOS_TheReturn/Shiftorium.cs @@ -214,23 +214,11 @@ namespace ShiftOS.Engine { upgDb = new List<ShiftoriumUpgrade>(); //Now we probe for ShiftoriumUpgradeAttributes for mods. - foreach (var file in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) - { - if (file.EndsWith(".exe") || file.EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(file); - foreach (var type in asm.GetTypes()) + foreach (var type in ReflectMan.Types) { if (type.GetInterfaces().Contains(typeof(IShiftoriumProvider))) - { - if (type.GetCustomAttributes().FirstOrDefault(x => x is ShiftoriumProviderAttribute) != null) - { - var _p = Activator.CreateInstance(type, null) as IShiftoriumProvider; - upgDb.AddRange(_p.GetDefaults()); - } - } + if (type.GetCustomAttributes().Any(x => x is ShiftoriumProviderAttribute)) + upgDb.AddRange((Activator.CreateInstance(type, null) as IShiftoriumProvider).GetDefaults()); ShiftoriumUpgradeAttribute attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute; @@ -310,10 +298,6 @@ namespace ShiftOS.Engine } } - } - catch { } - } - } diff --git a/ShiftOS_TheReturn/Skinning.cs b/ShiftOS_TheReturn/Skinning.cs index d5114c5..ab2d6b1 100644 --- a/ShiftOS_TheReturn/Skinning.cs +++ b/ShiftOS_TheReturn/Skinning.cs @@ -242,38 +242,16 @@ namespace ShiftOS.Engine /// <returns>The resulting icon image.</returns> public static Image GetDefaultIcon(string id) { - if (_iconProber == null) + if (_iconProber != null) { - return new Bitmap(16, 16); - } - else - { - foreach (var f in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) + foreach (var type in Array.FindAll(ReflectMan.Types, t => t.Name == id)) { - if (f.EndsWith(".exe") || f.EndsWith(".dll")) - { - try - { - var asm = Assembly.LoadFile(f); - foreach (var type in asm.GetTypes()) - { - if (type.Name == id) - { - foreach (var attr in type.GetCustomAttributes(true)) - { - if (attr is DefaultIconAttribute) - { - return _iconProber.GetIcon(attr as DefaultIconAttribute); - } - } - } - } - } - catch { } - } + var attr = Array.Find(type.GetCustomAttributes(true), a => a is DefaultIconAttribute); + if (attr != null) + return _iconProber.GetIcon(attr as DefaultIconAttribute); } - return new Bitmap(16, 16); } + return new Bitmap(16, 16); } /// <summary> diff --git a/ShiftOS_TheReturn/Story.cs b/ShiftOS_TheReturn/Story.cs index e44d2be..c01c055 100644 --- a/ShiftOS_TheReturn/Story.cs +++ b/ShiftOS_TheReturn/Story.cs @@ -134,55 +134,42 @@ namespace ShiftOS.Engine /// <param name="stid">The storyline ID to start.</param> public static void Start(string stid) { - foreach (var exec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) + if (SaveSystem.CurrentSave.StoriesExperienced == null) + SaveSystem.CurrentSave.StoriesExperienced = new List<string>(); + foreach (var type in ReflectMan.Types) { - if(exec.EndsWith(".exe") || exec.EndsWith(".dll")) + foreach (var mth in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { - try + foreach (var attrib in Array.FindAll(mth.GetCustomAttributes(false), a => a is StoryAttribute)) { - if (SaveSystem.CurrentSave.StoriesExperienced == null) - SaveSystem.CurrentSave.StoriesExperienced = new List<string>(); - var asm = Assembly.LoadFile(exec); - foreach(var type in asm.GetTypes()) + var story = attrib as StoryAttribute; + if (story.StoryID == stid) { - foreach(var mth in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) + new Thread(() => { - foreach(var attrib in mth.GetCustomAttributes(false)) + Context = new Engine.StoryContext { - if(attrib is StoryAttribute) - { - var story = attrib as StoryAttribute; - if(story.StoryID == stid) - { - new Thread(() => - { - Context = new Engine.StoryContext - { - Id = stid, - Method = mth, - AutoComplete = true, - }; - SaveSystem.CurrentSave.PickupPoint = Context.Id; - Context.OnComplete += () => - { - StoryComplete?.Invoke(stid); - SaveSystem.CurrentSave.PickupPoint = null; - }; - mth.Invoke(null, null); - if (Context.AutoComplete) - { - Context.MarkComplete(); - } - }).Start(); - return; - } - } + Id = stid, + Method = mth, + AutoComplete = true, + }; + SaveSystem.CurrentSave.PickupPoint = Context.Id; + Context.OnComplete += () => + { + StoryComplete?.Invoke(stid); + SaveSystem.CurrentSave.PickupPoint = null; + }; + mth.Invoke(null, null); + if (Context.AutoComplete) + { + Context.MarkComplete(); } - } + }).Start(); + return; } } - catch (Exception ex) { throw ex; } } + } #if DEBUG throw new ArgumentException("Story ID not found: " + stid + " - Talk to Michael. NOW."); diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index 09ef3d6..81ea971 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -295,54 +295,40 @@ namespace ShiftOS.Engine public static void PopulateTerminalCommands() { Commands = new List<TerminalCommand>(); - foreach(var exec in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) + foreach(var type in ReflectMan.Types) { - if(exec.ToLower().EndsWith(".exe") || exec.ToLower().EndsWith(".dll")) + var ns = type.GetCustomAttributes(false).FirstOrDefault(x => x is Namespace) as Namespace; + if(ns != null) { - try + foreach(var mth in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { - var asm = Assembly.LoadFile(exec); - foreach(var type in asm.GetTypes()) + var cmd = mth.GetCustomAttributes(false).FirstOrDefault(x => x is Command); + if(cmd != null) { - var ns = type.GetCustomAttributes(false).FirstOrDefault(x => x is Namespace) as Namespace; - if(ns != null) + var tc = new TerminalCommand(); + tc.RequiresElevation = !(type.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); + + tc.NamespaceInfo = ns; + + tc.CommandInfo = cmd as Command; + tc.RequiresElevation = tc.RequiresElevation || !(mth.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); + tc.RequiredArguments = new List<string>(); + foreach (var arg in mth.GetCustomAttributes(false).Where(x=>x is RequiresArgument)) { - foreach(var mth in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) - { - var cmd = mth.GetCustomAttributes(false).FirstOrDefault(x => x is Command); - if(cmd != null) - { - var tc = new TerminalCommand(); - tc.RequiresElevation = !(type.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); - - tc.NamespaceInfo = ns; - - tc.CommandInfo = cmd as Command; - tc.RequiresElevation = tc.RequiresElevation || !(mth.GetCustomAttributes(false).FirstOrDefault(x => x is KernelModeAttribute) == null); - tc.RequiredArguments = new List<string>(); - foreach (var arg in mth.GetCustomAttributes(false).Where(x=>x is RequiresArgument)) - { - var rarg = arg as RequiresArgument; - tc.RequiredArguments.Add(rarg.argument); - } - var rupg = mth.GetCustomAttributes(false).FirstOrDefault(x => x is RequiresUpgradeAttribute) as RequiresUpgradeAttribute; - if (rupg != null) - tc.Dependencies = rupg.Upgrade; - else - tc.Dependencies = ""; - tc.CommandType = type; - tc.CommandHandler = mth; - if (!Commands.Contains(tc)) - Commands.Add(tc); - } - } + var rarg = arg as RequiresArgument; + tc.RequiredArguments.Add(rarg.argument); } + var rupg = mth.GetCustomAttributes(false).FirstOrDefault(x => x is RequiresUpgradeAttribute) as RequiresUpgradeAttribute; + if (rupg != null) + tc.Dependencies = rupg.Upgrade; + else + tc.Dependencies = ""; + tc.CommandType = type; + tc.CommandHandler = mth; + if (!Commands.Contains(tc)) + Commands.Add(tc); } } - catch(Exception e) - { - Console.WriteLine("[termdb] Error: " + e.ToString()); - } } } Console.WriteLine("[termdb] " + Commands.Count + " commands found."); |
