Пример #1
0
        protected bool GetParameter(string message, out ComplexMessage parameters)
        {
            string[] strs = message.Split(new string[] { " ", Constants.CQNewLine }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (strs.Length > 0 && strs[0].ToLower() == ResponseCommand)
            {
                if (strs.Length == 1)
                {
                    parameters = null;
                }
                else
                {
                    var splitter = new CommandSplitter();
                    var command  = splitter.Split(strs[1]);
                    parameters = command?.Select(x => ComplexMessage.Parse(x).ToMessageElement()).ToComplexMessage();
                }

                return(true);
            }
            else
            {
                parameters = null;
                return(false);
            }
        }
Пример #2
0
        public static bool Process(string command)
        {
            return(parser.ParseArguments <ExitOption, ConfigOption, LoadOption, BuildOption, ExecOption, HelpOption>(
                       CommandSplitter.Split(command))
                   .MapResult(
                       (ExitOption opt) => {
                return true;
            },
                       (ConfigOption opt) => {
                if (opt.Key is null)
                {
                    foreach (var item in General.ConfigManager.Configuration.Keys)
                    {
                        Console.Write($"{item}: ");
                        Console.Write($"{General.ConfigManager.Configuration[item]}\n");
                    }
                }
                else
                {
                    if (opt.NewValue is null)
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            Console.WriteLine(General.ConfigManager.Configuration[opt.Key]);
                        }
                    }
                    else
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            General.ConfigManager.Configuration[opt.Key] = opt.NewValue;
                            General.ConfigManager.Save();
                            Console.WriteLine("New value has been applied");
                        }
                    }
                }
                return false;
            },
                       (LoadOption opt) => {
                if (General.CurrentAppStatus == AppStatus.Build)
                {
                    //load
                    try {
                        General.LoadedModule = Assembly.Load(File.ReadAllBytes("Test.dll"));
                        General.ScriptSettings.CleanSettings();
                        General.CurrentAppStatus = AppStatus.Loaded;
                    } catch (Exception e) {
                        ConsoleAssistance.WriteLine("Loading assembly error" + Environment.NewLine + e.Message, ConsoleColor.Yellow);
                    }
                }
                else
                {
                    //unload
                    General.CurrentAppStatus = AppStatus.Build;
                }

                return false;
            },
                       (BuildOption opt) => {
                if (!CheckStatus(AppStatus.Build))
                {
                    return false;
                }

                File.Delete("Test.dll");
                try {
                    //read code
                    var fs = new StreamReader("setup.cs", Encoding.UTF8);
                    var code = General.CodeTemplate.Replace("{PersonalCode}", fs.ReadToEnd());
                    fs.Close();
                    fs.Dispose();

                    //compile
                    var compiler = CSharpCompilation.Create("bpm_Plugin")
                                   .WithOptions(new CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary))
                                   .AddReferences(MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location))
                                   .AddReferences(MetadataReference.CreateFromFile(typeof(Console).GetTypeInfo().Assembly.Location))
                                   .AddReferences(MetadataReference.CreateFromFile(typeof(File).GetTypeInfo().Assembly.Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.IO")).Location))
                                   //.AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Text")).Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Linq")).Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Collections")).Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.ValueTuple")).Location))
                                   .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime.Extensions")).Location))
                                   //.AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.IO.FileSystem")).Location))
                                   .AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));

                    var res = compiler.Emit("Test.dll");
                    string r = "";
                    foreach (var item in res.Diagnostics)
                    {
                        r += item.ToString() + Environment.NewLine;
                    }

                    if (!res.Success)
                    {
                        ConsoleAssistance.WriteLine("Compile error:" + Environment.NewLine + r, ConsoleColor.Yellow);
                    }
                    else
                    {
                        ConsoleAssistance.WriteLine("Compile OK:" + Environment.NewLine + r);
                    }
                } catch (Exception e) {
                    ConsoleAssistance.WriteLine("Compile runtime error:" + Environment.NewLine + e.Message, ConsoleColor.Yellow);
                }

                return false;
            },
                       (ExecOption opt) => {
                if (!CheckStatus(AppStatus.Loaded))
                {
                    return false;
                }
                if (!CheckSettings())
                {
                    return false;
                }

                switch (opt.MethodName)
                {
                case "Install":
                    OutputRunningResult(((bool status, string desc))(General.LoadedModule.GetType("Plugin").GetMethod("Install").Invoke(null,
                                                                                                                                        new object[] { General.ConfigManager.Configuration["GamePath"],
                                                                                                                                                       Information.WorkPath.Path,
                                                                                                                                                       (Func <Dictionary <string, string> >)(General.ScriptSettings.GetSettings),
                                                                                                                                                       (Action <Dictionary <string, string> >)(General.ScriptSettings.SetSettings),
                                                                                                                                                       General.ConfigManager.Configuration["I18N"] })));
                    OutputSettingsValue(General.ScriptSettings);
                    break;
Пример #3
0
        public static bool Process(string command)
        {
            return(parser.ParseArguments <ExitOption, ConfigOption, SwitchOption, ClientOption, ImportOption, LsOption, ShowOption, AddpkgOption, EditpkgOption, DelpkgOption, AddverOption, EditverOption, DelverOption, HelpOption>(
                       CommandSplitter.Split(command))
                   .MapResult(
                       (ExitOption opt) => {
                if (opt.IsForce)
                {
                    return true;
                }
                else
                {
                    if (!General.IsMaintaining)
                    {
                        General.CoreTcpProcessor.StopListen();
                        Console.WriteLine("Waiting the release of resources...");
                        if (General.ManualResetEventList.Count != 0)
                        {
                            WaitHandle.WaitAll(General.ManualResetEventList.ToArray());
                        }
                    }
                    else
                    {
                        General.GeneralDatabase.Close();
                    }

                    General.RecordFileManager.Close();
                    return true;
                }
            },
                       (ConfigOption opt) => {
                if (opt.Key is null)
                {
                    foreach (var item in General.ConfigManager.Configuration.Keys)
                    {
                        Console.Write($"{item}: ");
                        Console.Write($"{General.ConfigManager.Configuration[item]}\n");
                    }
                }
                else
                {
                    if (opt.NewValue is null)
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            Console.WriteLine(General.ConfigManager.Configuration[opt.Key]);
                        }
                    }
                    else
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            General.ConfigManager.Configuration[opt.Key] = opt.NewValue;
                            General.ConfigManager.Save();
                            Console.WriteLine("New value has been applied");
                        }
                    }
                }
                return false;
            },
                       (SwitchOption opt) => {
                if (!General.IsMaintaining)
                {
                    General.CoreTcpProcessor.StopListen();
                    Console.WriteLine("Waiting the release of resources...");
                    if (General.ManualResetEventList.Count != 0)
                    {
                        WaitHandle.WaitAll(General.ManualResetEventList.ToArray());
                    }

                    General.GeneralDatabase.Open();

                    General.IsMaintaining = true;
                    ConsoleAssistance.WriteLine("Switch to maintain mode successfully.", ConsoleColor.Yellow);
                }
                else
                {
                    General.GeneralDatabase.Close();
                    //force update verify code
                    ConsoleAssistance.WriteLine("Updating verify code....", ConsoleColor.White);
                    General.VerifyBytes = SignVerifyHelper.SignData(Information.WorkPath.Enter("package.db").Path, Information.WorkPath.Enter("pri.key").Path);
                    General.ConfigManager.Configuration["VerifyBytes"] = Convert.ToBase64String(General.VerifyBytes);
                    General.ConfigManager.Save();

                    General.CoreTcpProcessor.StartListen();

                    General.IsMaintaining = false;
                    ConsoleAssistance.WriteLine("Switch to running mode successfully.", ConsoleColor.Yellow);
                }
                return false;
            },
                       (ClientOption opt) => {
                if (!CheckStatus(false))
                {
                    return false;
                }

                ConsoleAssistance.WriteLine($"Current client: {General.ManualResetEventList.Count}", ConsoleColor.Yellow);
                return false;
            },
                       (ImportOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }

                ConsoleAssistance.WriteLine("import is a dangerous command. It will load all script and run it without any error judgement! It couldn't be stopped before all of commands has been executed!", ConsoleColor.Yellow);
                var confirm = new Random().Next(100, 9999);
                ConsoleAssistance.WriteLine($"Type this random number to confirm your operation: {confirm}", ConsoleColor.Yellow);
                if (Console.ReadLine() == confirm.ToString())
                {
                    if (System.IO.File.Exists(opt.FilePath))
                    {
                        ImportStack.AppendImportedCommands(opt.FilePath);
                    }
                    else
                    {
                        ConsoleAssistance.WriteLine("Cannot find specific file", ConsoleColor.Red);
                    }
                }
                return false;
            },
                       (LsOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }

                if (opt.Condition is null)
                {
                    PackageManager.Ls(General.GeneralDatabase, "");
                }
                else
                {
                    PackageManager.Ls(General.GeneralDatabase, opt.Condition);
                }
                return false;
            },
                       (ShowOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.Show(General.GeneralDatabase, opt.FullPackageName);
                return false;
            },
                       (AddpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.AddPackage(General.GeneralDatabase, opt);
                return false;
            },
                       (EditpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.EditPackage(General.GeneralDatabase, opt);
                return false;
            },
                       (DelpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.RemovePackage(General.GeneralDatabase, opt.Name);
                return false;
            },
                       (AddverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.AddVersion(General.GeneralDatabase, opt);
                return false;
            },
                       (EditverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.EditVersion(General.GeneralDatabase, opt);
                return false;
            },
                       (DelverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.RemoveVersion(General.GeneralDatabase, opt.Name);
                return false;
            },
                       (HelpOption opt) => {
                OutputHelp();
                return false;
            },
                       errs => { ConsoleAssistance.WriteLine("Unknow command. Use help to find the correct command", ConsoleColor.Red); return false; }));
        }
        public void TestMultipleCommandsSplit()
        {
            IList <string> commands = CommandSplitter.Split("test1 && test2");

            AssertList(commands, "test1", "test2");
        }
        public void TestMultipleCommandsWithArgSplit()
        {
            IList <string> commands = CommandSplitter.Split("test1 --arg1 && test2 --arg2");

            AssertList(commands, "test1 --arg1", "test2 --arg2");
        }
        public void TestSingleCommandWithArgsSingleQuotesAndInnerQuotesSplit()
        {
            IList <string> commands = CommandSplitter.Split("test --arg1 'argument \"&&\" quotes'");

            AssertList(commands, "test --arg1 'argument \"&&\" quotes'");
        }
        public void TestSingleCommandWithArgsQuotesAndInnerSingleQuotesSplit()
        {
            IList <string> commands = CommandSplitter.Split("test --arg1 \"argument '&&' quotes\"");

            AssertList(commands, "test --arg1 \"argument '&&' quotes\"");
        }
        public void TestSingleCommandWithArgsSplit()
        {
            IList <string> commands = CommandSplitter.Split("test --arg1 --arg2");

            AssertList(commands, "test --arg1 --arg2");
        }
        public void TestSingleCommandSplit()
        {
            IList <string> commands = CommandSplitter.Split("test");

            AssertList(commands, "test");
        }
        public void TestMoar()
        {
            IList <string> commands = CommandSplitter.Split("bind t 'echo \"test-1\" && echo \"test-2\"'");

            AssertList(commands, "bind t 'echo \"test-1\" && echo \"test-2\"'");
        }
        public void TestMultipleCommandsWithArgsSingleQuotesAndInnerQuotesSplit()
        {
            IList <string> commands = CommandSplitter.Split("test1 --arg1 'a1 \"&&\" a2' && test2 --arg2 'b1 \"&&\" b2'");

            AssertList(commands, "test1 --arg1 'a1 \"&&\" a2'", "test2 --arg2 'b1 \"&&\" b2'");
        }
        public void TestMultipleCommandsWithArgsQuotesAndInnerQuotesSplit()
        {
            IList <string> commands = CommandSplitter.Split("test1 --arg1 \"a1 \\\"&&\\\" a2\" && test2 --arg2 \"b1 \\\"&&\\\" b2\"");

            AssertList(commands, "test1 --arg1 \"a1 \\\"&&\\\" a2\"", "test2 --arg2 \"b1 \\\"&&\\\" b2\"");
        }