public static void Reload()
 {
     CommandManager.ClearExternalCommands();
     CFormat.WriteLine("[CommandManager] Cleared loaded external commands.", ConsoleColor.Gray);
     CFormat.JumpLine();
     CommandManager.LoadExternalCommands(true);
 }
        public static void UnloadFile()
        {
            LoadedFiles();
            CFormat.JumpLine();
            CFormat.WriteLine("Please enter the number ID of the file you want to unload.", ConsoleColor.Gray);
            int id = CInput.UserPickInt(CommandManager.LoadedFileIDs.Count - 1);

            if (id == -1)
            {
                return;
            }
            CommandManager.UnloadFile(id);
        }
 public static void LoadedFiles()
 {
     if (CommandManager.LoadedFileIDs.Count == 0)
     {
         CFormat.WriteLine("[CommandManager] There are no external files loaded.", ConsoleColor.Gray);
     }
     else
     {
         CFormat.WriteLine("[CommandManager] List of loaded files: ", ConsoleColor.Gray);
         foreach (FileID listLoadedFile in CommandManager.LoadedFileIDs)
         {
             CFormat.WriteLine(string.Format("{0}({1}) {2}", CFormat.Indent(2), listLoadedFile.ID, listLoadedFile.Path), ConsoleColor.Gray);
         }
     }
 }
        public static void CreateTemplate(string path = null)
        {
            try
            {
                if (path == null)
                {
                    path = "TemplateFile" + Path.GetExtension("*.cs");
                }
                CFormat.WriteLine("Creating template file \"" + path + "\"");
                if (File.Exists(path))
                {
                    CFormat.WriteLine("A file named \"" + path + "\" already exists. Replace it?");
                    ConsoleAnswer answer = CInput.UserChoice(ConsoleAnswerType.YesNo, true);
                    if (answer == ConsoleAnswer.No)
                    {
                        int    num = 1;
                        string withoutExtension = Path.GetFileNameWithoutExtension(path);
                        while (File.Exists(path))
                        {
                            path = Path.Combine(Path.GetDirectoryName(path), withoutExtension + " (" + num + ")", Path.GetExtension(path));
                            ++num;
                        }
                    }
                    else if (answer == ConsoleAnswer.Escaped)
                    {
                        return;
                    }
                }

                using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
                    streamWriter.Write(Properties.Resources.FileTemplate);

                CFormat.WriteLine("Template file named \"" + path + "\" created!", ConsoleColor.Green);
            }
            catch (Exception ex)
            {
                CFormat.WriteLine("Could not create template file \"" + path + "\" Details: " + ex.Message, ConsoleColor.Red);
            }
        }
        public static void List()
        {
            CFormat.WriteLine("For more information about a command, type 'Help <command>'.", ConsoleColor.Gray);
            CFormat.JumpLine();
            CFormat.WriteLine("[Internal commands]", ConsoleColor.Green);
            foreach (Type library in CommandManager.InternalLibraryCallNames.Values)
            {
                if (CommandManager.InternalLibraries[library].Values.Count != 0)
                {
                    string libraryCallName   = CommandManager.InternalLibraryCallNames.FirstOrDefault(x => x.Value == library).Key;
                    string libraryHelpPrompt = library.GetCustomAttribute <MMasterLibrary>().HelpPrompt;
                    if (!String.IsNullOrEmpty(libraryHelpPrompt))
                    {
                        libraryHelpPrompt = " (" + libraryHelpPrompt + ")";
                    }

                    CFormat.WriteLine(libraryCallName + libraryHelpPrompt, ConsoleColor.Yellow);

                    foreach (MethodInfo methodInfo in CommandManager.InternalLibraries[library].Values)
                    {
                        MMasterCommand mMasterCommand = methodInfo.GetCustomAttribute <MMasterCommand>();
                        string         helpPrompt     = mMasterCommand.HelpPrompt;
                        if (!String.IsNullOrEmpty(helpPrompt))
                        {
                            helpPrompt = " (" + helpPrompt + ")";
                        }
                        CFormat.WriteLine(CFormat.Indent(3) + "." + methodInfo.Name + helpPrompt);
                    }
                    CFormat.JumpLine();
                }
            }

            if (CommandManager.ExternalLibraryCallNames.Count == 0)
            {
                return;
            }

            CFormat.WriteLine("[External commands]", ConsoleColor.Green);
            int num = 1;

            foreach (Type library in CommandManager.ExternalLibraryCallNames.Values)
            {
                string libraryCallName   = CommandManager.ExternalLibraryCallNames.FirstOrDefault(x => x.Value == library).Key;
                string libraryHelpPrompt = library.GetCustomAttribute <MMasterLibrary>().HelpPrompt;
                if (!String.IsNullOrEmpty(libraryHelpPrompt))
                {
                    libraryHelpPrompt = " (" + libraryHelpPrompt + ")";
                }

                CFormat.WriteLine(libraryCallName + libraryHelpPrompt, ConsoleColor.Yellow);
                foreach (MethodInfo methodInfo in CommandManager.ExternalLibraries[library].Values)
                {
                    MMasterCommand mMasterCommand = methodInfo.GetCustomAttribute <MMasterCommand>();
                    string         helpPrompt     = mMasterCommand.HelpPrompt;
                    if (!String.IsNullOrEmpty(helpPrompt))
                    {
                        helpPrompt = " (" + helpPrompt + ")";
                    }
                    CFormat.WriteLine(CFormat.Indent(3) + "." + methodInfo.Name + helpPrompt);
                }

                if (num < CommandManager.ExternalLibraryCallNames.Values.Count)
                {
                    CFormat.JumpLine();
                }
                ++num;
            }
        }
        public static void Help(string stringCall = null)
        {
            if (stringCall == null)
            {
                List();
            }
            else
            {
                try
                {
                    // if the call is a library
                    if (!stringCall.Contains(' ') && !stringCall.Contains('.'))
                    {
                        Type library = CParsedInput.ParseLibrary(stringCall);

                        if (library == null)
                        {
                            CFormat.WriteLine("This library does not exist.");
                            return;
                        }

                        string libraryCallName   = CommandManager.ExternalLibraryCallNames.FirstOrDefault(x => x.Value == library).Key;
                        string libraryHelpPrompt = library.GetCustomAttribute <MMasterLibrary>().HelpPrompt;
                        if (!String.IsNullOrEmpty(libraryHelpPrompt))
                        {
                            libraryHelpPrompt = " (" + libraryHelpPrompt + ")";
                        }

                        CFormat.WriteLine(libraryCallName + libraryHelpPrompt, ConsoleColor.Yellow);
                        foreach (MethodInfo methodInfo in CommandManager.ExternalLibraries[library].Values)
                        {
                            MMasterCommand mMasterCommand = methodInfo.GetCustomAttribute <MMasterCommand>();
                            string         helpPrompt     = mMasterCommand.HelpPrompt;
                            if (!String.IsNullOrEmpty(helpPrompt))
                            {
                                helpPrompt = " (" + helpPrompt + ")";
                            }
                            CFormat.WriteLine(CFormat.Indent(3) + "." + methodInfo.Name + helpPrompt);
                        }
                    }
                    else
                    {
                        CParsedInput parsedInput = new CParsedInput(stringCall, true);

                        string helpPrompt = parsedInput.CommandMethodInfo.GetCustomAttribute <MMasterCommand>().HelpPrompt;

                        if (helpPrompt == "")
                        {
                            CFormat.WriteLine(CFormat.GetArgsFormat(parsedInput.FullCallName, parsedInput.CommandMethodInfo.GetParameters()));
                        }
                        else
                        {
                            CFormat.WriteLine(helpPrompt, CFormat.GetArgsFormat(parsedInput.FullCallName, parsedInput.CommandMethodInfo.GetParameters()));
                        }
                    }
                }
                catch (WrongCallFormatException)
                {
                    CFormat.WriteLine("Wrong call format.", "The call should be as it follows: <Library>.<Command> [arg1] [arg2] [etc.]");
                }
                catch (LibraryNotExistingException)
                {
                    CFormat.WriteLine("This library does not exist.");
                }
                catch (CommandNotExistingException)
                {
                    CFormat.WriteLine("This command does not exist.");
                }
            }
        }
Example #7
0
 [MMasterCommand("Prints Hello!", "Command")] // ARGS : HelpPrompt, CallName. Both of these args are optional.
 public static void Command(string message)   // Call this command with 'Library.Command'
 {
     // Edit code here
     CFormat.WriteLine("Hello " + message + "!", ConsoleColor.Blue);
 }