Example #1
0
        protected JsonConfig MakeDefault()
        {
            string[]   args  = this.GetCommandsList();
            JsonConfig reply = new JsonConfig();

            foreach (string arg in args)
            {
                string arg_type          = GetCommandArgTypes(arg).First();
                string arg_value_default = null;
                if (arg_type != "")
                {
                    string[] ArgHints = GetCommandArgHints(arg);
                    if (ArgHints.Length > 0)
                    {
                        arg_value_default = ArgHints[0];
                        arg_value_default = arg_value_default.Replace("[", "");
                        arg_value_default = arg_value_default.Replace("]", "");
                        arg_value_default = arg_value_default.Replace("\"", "");
                    }
                    else
                    {
                        LogFormater.Debug("[Notice] No args hint found for " + arg + "");
                    }
                }
                if (arg_value_default != null)
                {
                    Type         Dtype = reply.GetType();
                    PropertyInfo prop  = Dtype.GetProperty(arg, BindingFlags.Public | BindingFlags.Instance);
                    SetPropTypeAndValue(reply, prop, arg, arg_type, arg_value_default);
                }
            }
            return(reply);
        }
Example #2
0
 protected void SetPropTypeAndValue(JsonConfig reply, PropertyInfo prop, string arg, string arg_type, string arg_value_default)
 {
     if (prop != null)
     {
         if (prop.CanWrite == true)
         {
             if (arg_type == "Number")
             {
                 if (int.TryParse(arg_value_default, out int result) == true)
                 {
                     prop.SetValue(reply, result, null);
                 }
             }
             else if (arg_type == "Text")
             {
                 prop.SetValue(reply, arg_value_default, null);
             }
             else if (arg_type == "True|False")
             {
                 if (bool.TryParse(arg_value_default, out bool result) == true)
                 {
                     prop.SetValue(reply, result, null);
                 }
             }
             else if (arg_type == "Collection")
             {
                 prop.SetValue(reply, arg_value_default.Split(','), null);
             }
             else if (arg_type == "BigNumber")
             {
                 if (ulong.TryParse(arg_value_default, out ulong result) == true)
                 {
                     prop.SetValue(reply, result, null);
                 }
             }
             else if (arg_type == "Float")
             {
                 if (float.TryParse(arg_value_default, out float result) == true)
                 {
                     prop.SetValue(reply, result, null);
                 }
             }
             else
             {
                 LogFormater.Debug("unsupported arg_type: " + arg_type + " for " + arg + "");
             }
         }
         else
         {
             LogFormater.Warn("Unable to write to " + arg + "");
         }
     }
     else
     {
         LogFormater.Crit("unknown prop " + arg + "");
     }
 }
Example #3
0
        protected JsonConfig Process_prop(JsonConfig reply, string arg, string arg_value_default)
        {
            string arg_type = GetCommandArgTypes(arg).First();

            if (arg_type != "")
            {
                if (arg_value_default != null)
                {
                    Type         Dtype = reply.GetType();
                    PropertyInfo prop  = Dtype.GetProperty(arg, BindingFlags.Public | BindingFlags.Instance);
                    SetPropTypeAndValue(reply, prop, arg, arg_type, arg_value_default);
                }
            }
            else
            {
                LogFormater.Debug("unknown arg_type for " + arg + "");
            }
            return(reply);
        }
Example #4
0
        protected void InterfaceCommands(string area, API_supported_interface shared_interface, bool track_commands = false)
        {
            string[] cmds           = shared_interface.GetCommandsList();
            string   interface_name = shared_interface.GetType().Name;

            foreach (string c in cmds)
            {
                if (track_commands == true)
                {
                    if (seen_command_names.ContainsKey(c) == false)
                    {
                        seen_command_names.Add(c, interface_name);
                    }
                    else
                    {
                        LogFormater.Debug("command " + c + " from " + interface_name + " overlaps an ready loaded command from " + seen_command_names[c] + "");
                    }
                }
                string workspace = shared_interface.GetCommandWorkspace(c);


                StringBuilder sb = new StringBuilder();
                sb.Append(html_header);
                sb.Append("<h3>Build: ");
                sb.Append(buildVersion);
                sb.Append("</h3><br/>");
                sb.Append("<h4>Interface: <a href='[[AREA]].html'>[[AREA]]</a>");
                if (workspace != "")
                {
                    sb.Append(" / <a href='[[AREA]][[WORKSPACE]].html'>[[WORKSPACE]]</a>");
                }
                sb.Append("</h4><hr/><h3>[[COMMAND]]</h3>");
                int      loop      = 0;
                int      minargs   = shared_interface.GetCommandArgs(c);
                string[] arg_types = shared_interface.GetCommandArgTypes(c);
                string[] arg_hints = shared_interface.GetCommandArgHints(c);
                if (area == "Core")
                {
                    StringBuilder ExampleCall = new StringBuilder();
                    ExampleCall.Append("Example: [[COMMAND]]");
                    ExampleCall.Append("|||");
                    string addon = "";
                    while (loop < minargs)
                    {
                        ExampleCall.Append(addon);
                        addon = "~#~";
                        string hint_value = "";
                        if (arg_hints.Length > loop)
                        {
                            if (arg_hints[loop] != null)
                            {
                                string[] bits = arg_hints[loop].Split("<br/>", StringSplitOptions.RemoveEmptyEntries);
                                if (bits.Length >= 1)
                                {
                                    hint_value = bits[0];
                                }
                            }
                        }
                        if (hint_value != "")
                        {
                            ExampleCall.Append(hint_value);
                        }
                        else
                        {
                            ExampleCall.Append("?");
                            LogFormater.Debug("[WikiMake] " + area + " Command " + c + " missing some required hint values");
                        }
                        loop++;
                    }
                    sb.Append(ExampleCall.ToString());
                    sb.Append("<hr style='border-top: 1px dashed #dcdcdc;'>");
                }



                sb.Append("[[HELP]]");
                if (arg_types.Length > 0)
                {
                    sb.Append("<hr/><h4>Args helper</h4>");
                    loop = 0;

                    sb.Append("<table class='table table-striped table-bordered'><thead><tr><td>Num</td><th>Type</th><th>Required</th><th>Hint</th></tr></thead><tbody>");
                    while (loop < arg_types.Length)
                    {
                        string hint = "";
                        if (arg_hints.Length > loop)
                        {
                            hint = arg_hints[loop];
                        }
                        string Required = "X";
                        if ((loop + 1) > minargs)
                        {
                            Required = "";
                        }
                        sb.Append("<tr><td>" + (loop + 1).ToString() + "</td><td>" + arg_types[loop] + "</td><td>" + Required + "</td><td>" + hint + "</td></tr>");

                        loop++;
                    }
                    sb.Append("</tbody></table>");
                }
                sb.Append(html_footer);
                sb.Replace("[[COMMAND]]", c);
                sb.Replace("[[HELP]]", shared_interface.GetCommandHelp(c));
                sb.Replace("[[MINARGS]]", minargs.ToString());
                sb.Replace("[[WORKSPACE]]", workspace);
                sb.Replace("[[AREA]]", area);
                sb.Replace("[[SUBFOLDER]]", "");
                sb.Replace("[[RETURNROOT]]", "../");
                sb = DebugModeCreateWiki.MenuActive(sb, area);
                string target_file = "" + area + "" + workspace + "" + c + ".html";
                io.WriteFile(target_file, sb.ToString());
            }
        }
Example #5
0
 public virtual void Debug(string message)
 {
     Log2File(LogFormater.Debug(message, false), ConsoleLogLogLevel.Debug);
 }
Example #6
0
        public CliHardware(string[] args)
        {
            SimpleIO   io         = new SimpleIO();
            JsonConfig Config     = new JsonConfig();
            string     loadFolder = "debug";
            string     json_file  = "bot.json";

#if DEBUG
            LogFormater.Debug("!! RUNNING IN DEBUG !!");
#else
            LogFormater.Status("Hardware config");
            if (args.Length == 1)
            {
                loadFolder = args[0];
            }
            else
            {
                loadFolder = "default";
                io.ChangeRoot("default");
                LogFormater.Warn("Using: using default folder");
            }
#endif
            io.ChangeRoot(loadFolder);
            if (SimpleIO.DirExists("wiki") == false)
            {
                LogFormater.Info("Basic Wiki [Creating]");
                new DebugModeCreateWiki(AssemblyInfo.GetGitHash(), io);
                LogFormater.Info("Basic Wiki [Ready]");
                io = new SimpleIO();
            }
            bool ok_to_try_start = false;
            if (SimpleIO.FileType(json_file, "json") == true)
            {
                if (io.Exists(json_file))
                {
                    string json = io.ReadFile(json_file);
                    if (json.Length > 0)
                    {
                        try
                        {
                            Config          = JsonConvert.DeserializeObject <JsonConfig>(json);
                            ok_to_try_start = true;
                        }
                        catch (Exception e)
                        {
                            LogFormater.Warn("Unable to read config file\n moving config to " + json_file + ".old and creating a empty config\nError was: " + e.Message + "");
                            io.MarkOld(json_file);
                            Config = new JsonConfig();
                            io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                        }
                    }
                    else
                    {
                        LogFormater.Warn("Json config is empty creating an empty one for you");
                        io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                    }
                }
                else
                {
                    LogFormater.Warn("Json config does not Exist creating it for you");
                    io.WriteJsonConfig(MakeJsonConfig.GetDefault(), json_file);
                }
            }
            else
            {
                LogFormater.Crit("you must select a .json file for config! example \"BetterSecondBot.exe mybot\" will use the mybot.json file!");
            }
            if (ok_to_try_start == true)
            {
                Config = MakeJsonConfig.Http_config_check(Config);
                new Discord_super(Config, false, loadFolder);
            }
        }