예제 #1
0
 public void LoadCommand(IMETAboltCommand command)
 {
     command.StartCommand(instance);
     CommandsByName[command.Name.ToLower()] = command;
     lock (CommandsLoaded) CommandsLoaded.Add(command);
 }
예제 #2
0
        /// <summary>
        /// Loads commands
        /// </summary>
        /// <param name="type">Type to try to load</param>
        /// <returns>True if useful types were found and loaded</returns>
        public bool LoadType(Type type)
        {
            bool loadedType = false;

            if (typeof(IMETAboltCommand).IsAssignableFrom(type) && type != typeof(METAboltCommand))
            {
                try
                {
                    var c = type.GetConstructor(new Type[] { typeof(METAboltInstance) });
                    if (c != null)
                    {
                        IMETAboltCommand plug = (IMETAboltCommand)c.Invoke(new object[] { instance });
                        LoadCommand(plug);
                        return(true);
                    }
                    c = type.GetConstructor(Type.EmptyTypes);
                    if (c != null)
                    {
                        IMETAboltCommand plug = (IMETAboltCommand)c.Invoke(new object[0]);
                        LoadCommand(plug);
                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log("ERROR in METAbolt Command: " + type + " because " + ex.Message + " " + ex.StackTrace,
                               Helpers.LogLevel.Debug);
                    throw ex;
                }
                return(false);
            }

            if (typeof(ICommandInterpreter).IsAssignableFrom(type))
            {
                if (GetType() == type)
                {
                    return(false);
                }
                foreach (var ci in type.GetConstructors())
                {
                    if (ci.GetParameters().Length > 0)
                    {
                        continue;
                    }
                    try
                    {
                        ICommandInterpreter plug = (ICommandInterpreter)ci.Invoke(new object[0]);
                        LoadInterpreter(plug);
                        loadedType = true;
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("ERROR in METAbolt ICommandInterpreter: " + type + " because " + ex.Message + " " + ex.StackTrace, Helpers.LogLevel.Debug);
                        throw ex;
                    }
                }
            }

            return(loadedType);
        }