예제 #1
0
 /// <summary>Initialise a fresh config file at <see cref="sConfigFilePath"/>.</summary>
 private static void InitialiseFreshConfigfile()
 {
     ApplicationLogger.Log("Creating Fresh Config File at " + FullPath, LOGGER_SOURCE, LOG_SEVERITY.WARNING);
     if (!Directory.Exists(Location))// If the directory does not exist...
     {
         ApplicationLogger.Log("tConfig Directory Path does not exist: [" + Location + "] - Creating Directory...", LOGGER_SOURCE, LOG_SEVERITY.WARNING);
         Directory.CreateDirectory(Location);            // ...Create it
     }
     using (StreamWriter sw = File.CreateText(FullPath)) // Create a fresh config file template (will also overwrite if one already exists)
     {
         sw.WriteLine(COMMENT_CHAR + " Any lines that start with '" + COMMENT_CHAR + "' will be ignored");
         sw.WriteLine(COMMENT_CHAR + " Please do not change the values in front of the '='.");
         sw.WriteLine(COMMENT_CHAR + "Any log level k-v are represented as an integer: " +
                      LOG_SEVERITY.DEBUG + " = " + LOG_SEVERITY.DEBUG.GetHashCode() + "  -> " +
                      LOG_SEVERITY.VERBOSE + " = " + LOG_SEVERITY.VERBOSE.GetHashCode() + "  -> " +
                      LOG_SEVERITY.INFO + " = " + LOG_SEVERITY.INFO.GetHashCode() + "  -> " +
                      LOG_SEVERITY.WARNING + " = " + LOG_SEVERITY.WARNING.GetHashCode() + "  -> " +
                      LOG_SEVERITY.ERROR + " = " + LOG_SEVERITY.ERROR.GetHashCode() + "  -> " +
                      LOG_SEVERITY.CRITICAL + " = " + LOG_SEVERITY.CRITICAL.GetHashCode() + ".");
         sw.WriteLine("");
         sw.WriteLine(COMMENT_CHAR + " Bot Set-up:");
         sw.WriteLine(KEY_BOT_TOKEN + " =           Token Here");
         sw.WriteLine(KEY_LOG_LEVEL + " =           " + LOG_SEVERITY.DEBUG.GetHashCode());
         sw.WriteLine("");
         sw.WriteLine("# Database Set-up:");
         sw.WriteLine(KEY_DATABASE_ADDRESS + "=    localhost");
         sw.WriteLine(KEY_DATABASE_NAME + "=       Name Here");
         sw.WriteLine(KEY_DATABASE_USERNAME + "=   Username here");
         sw.WriteLine(KEY_DATABASE_PASSWORD + "=   Password here");
     }
     ApplicationLogger.Log("Fresh Config file created", LOGGER_SOURCE, LOG_SEVERITY.INFO);
 }
예제 #2
0
        /// <summary>Process the arguments passed in to the application.</summary>
        /// <param name="args">Arguments passed in to the application.</param>
        private static void ProcessArgs(string[] args)
        {
            const string LOG_SOURCE = "Args";

            ApplicationLogger.Log("Starting: Processing Arguments And Config.....", LOG_SOURCE, LOG_SEVERITY.INFO);

            if (args == null)//if the array is null, set it as an empty array.
            {
                args = new string[] { }
            }
            ;

            //If there are no args, then set the default config directory.
            if (args.Length == 0)
            {
                ApplicationLogger.Log("No Arguments, config file path remains: '" + ConfigurationFile.FullPath + "'.", LOG_SOURCE, LOG_SEVERITY.WARNING);
            }
            //file path is already defaulted to local directory.
            else
            {
                string arg = args[0];//The first arg should be the path to the config file
                ApplicationLogger.Log("Setting to config directory to '" + arg + "'.", LOG_SOURCE, LOG_SEVERITY.VERBOSE);
                ConfigurationFile.Location = arg;
                ApplicationLogger.Log("Config path set to '" + ConfigurationFile.FullPath + "'.", LOG_SOURCE, LOG_SEVERITY.VERBOSE);
            }

            ApplicationLogger.Log("Completed: Arguments And Config Processing", LOG_SOURCE, LOG_SEVERITY.INFO);
        }
    }
예제 #3
0
        /// <summary>The main starting point of any application.</summary>
        /// <remarks>If you had to read this to figure out what main is.....git gud scrub.</remarks>
        /// <param name="args">Arguments passed in to the application, from the command line, or shortcut.</param>
        static async Task Main(string[] args)
        {
            PrintApplicationInfo();//Print the application info to the log

            //Check if this application is already running...
            if (ProcessRunningCheck())
            {//...if it is, shut this instance down.
                ApplicationLogger.Log("Only one instance of " + ProcessName + " Can be run a a time.",
                                      "ProcessRunningCheck",
                                      LOG_SEVERITY.ERROR);
                ApplicationLogger.Log("Shutting down this instance ... Goodbye!!",
                                      "ProcessRunningCheck",
                                      LOG_SEVERITY.CRITICAL);
                Console.ReadKey();
                return; //returning in main exits the application.
            }

            //Initialising:
            //Process the args passed into the program
            ProcessArgs(args);
            //Now process the config file
            if (!ConfigurationFile.ProcessConfigFile())//Will return false if a config line is invalid.
            {
                ApplicationLogger.Log("Configuration file is invalid, Shutting down", "MAIN", LOG_SEVERITY.WARNING);
                Console.ReadKey();
                return;
            }

            Connection DiscordConnection = new Connection(DConfigFactory.Generate(mdl.Bot.Severity));
            await DiscordConnection.ConnectAsync(mdl.Bot.Token);

            Console.ReadKey();
        }
예제 #4
0
        /// <summary>Print the application name and version number.</summary>
        static private void PrintApplicationInfo()
        {
            AssemblyName aName = Assembly.GetExecutingAssembly().GetName();

            ApplicationLogger.Log(
                "Running: " + aName.Name + "  V" + aName.Version.ToString(),
                "Initialisation",
                LOG_SEVERITY.INFO);
        }
예제 #5
0
        /// <summary>Process the config file. Can also Initialise a fresh file if it doesn't exist.</summary>
        /// <returns>TRUE if a valid config file, FALSE otherwise.</returns>
        public static bool ProcessConfigFile()
        {
            ApplicationLogger.Log("Starting: Processing Config File......", LOGGER_SOURCE, LOG_SEVERITY.INFO);
            ApplicationLogger.Log("Location: " + FullPath, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);

            if (!File.Exists(FullPath))//If the config file does not exist...
            {
                ApplicationLogger.Log("Config file does not exist", LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                InitialiseFreshConfigfile(); //Initialise a fresh one.
                ApplicationLogger.Log("Please complete the Configuration file at: " + FullPath, LOGGER_SOURCE, LOG_SEVERITY.ERROR);
                return(false);               // return false because the config file has just been created and isn't valid in its initialised state.
            }

            string[] sLinesFromFile;
            try
            {
                sLinesFromFile = File.ReadAllLines(FullPath);
            }
            catch (Exception e)
            {
                ApplicationLogger.Log("Exception when reading configuration file: " + e.Message, LOGGER_SOURCE, LOG_SEVERITY.ERROR);
                return(false);
            }

            for (int i = 0; i < sLinesFromFile.Length; i++)
            {
                ApplicationLogger.Log("Processing line [" + i + "]: " + sLinesFromFile[i], LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                if (!ProcessLine(sLinesFromFile[i])) //If the line isn't valid, return false.
                {
                    ApplicationLogger.Log("Line Invalid: " + i, LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                    return(false);
                }
            }
            ApplicationLogger.Log("Completed: Processing Config File", LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
            return(true);//if we have reached this point, then the config file is valid.
        }
예제 #6
0
        /// <summary>Process a line from the config file.</summary>
        /// <param name="lineFromFile">A line from the file.</param>
        /// <returns>True if a valid line, false otherwise.</returns>
        private static bool ProcessLine(string lineFromFile)
        {
            if (lineFromFile == null)
            {
                lineFromFile = "";
            }

            lineFromFile = lineFromFile.Trim(); //Remove the spaces from the start and end of the line.

            if (lineFromFile.Length == 0)       //if the line is blank, "".
            {
                ApplicationLogger.Log("Blank line found - ignoring...", LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                return(true); //Return true, as a blank line is valid.
            }

            if (lineFromFile[0] == COMMENT_CHAR) //Check if the line is a comment.
            {
                ApplicationLogger.Log("Commented line found - ignoring...", LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                return(true); //Return true, as a commented line is a valid line.
            }

            //A valid line is in the format of 'Key=Value' or 'key = value', or '   key  =    value    '.
            string[] splitLine = lineFromFile.Split('='); //Split the line around an '='.

            if (splitLine.Length != 2)
            {
                ApplicationLogger.Log("Split length not valid, required: 2,  actual: " + splitLine.Length, LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                return(false);//If the split length is not 2, then this line is invalid.
            }

            string key   = splitLine[0].Trim();
            string value = splitLine[1].Trim();

            switch (key)//Switch the KEY.
            {
            case KEY_BOT_TOKEN:
                ApplicationLogger.Log("Setting Bot Token: " + value, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                mdl.Bot.Token = value;
                ApplicationLogger.Log("Bot Token set to:" + mdl.Bot.Token, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            case KEY_LOG_LEVEL:
                LOG_SEVERITY ll = LOG_SEVERITY.DEBUG;

                int iValue = 0;
                if (!int.TryParse(value, out iValue))    //If the value is not an integer....
                {
                    ApplicationLogger.Log("Value [" + value + "] not an integer.", LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                    return(false);   //the key-value is invalid
                }

                if (Enum.IsDefined(typeof(LOG_SEVERITY), iValue))    //if the value is not defined in the enum....
                {
                    ll = (LOG_SEVERITY)iValue;
                }
                else
                {
                    ApplicationLogger.Log("Value [" + value + "] not defined in Log_Level.", LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                    return(false);   //The key-value pair is not valid
                }

                ApplicationLogger.Log("Setting Bot Log Level: " + ll, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                mdl.Bot.Severity = ll;
                ApplicationLogger.Log("Bot Log Level set to:" + mdl.Bot.Severity, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            case KEY_DATABASE_ADDRESS:
                ApplicationLogger.Log("Setting Database Address: " + value, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                mdl.Database.Address = value;
                ApplicationLogger.Log("Database Address set to:" + mdl.Database.Address, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            case KEY_DATABASE_NAME:
                ApplicationLogger.Log("Setting Database Name: " + value, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                mdl.Database.Name = value;
                ApplicationLogger.Log("Database Name set to:" + mdl.Database.Name, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            case KEY_DATABASE_USERNAME:
                ApplicationLogger.Log("Setting Database Username: "******"Database Username set to:" + mdl.Database.Username, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            case KEY_DATABASE_PASSWORD:
                ApplicationLogger.Log("Setting Database Password: "******"Database Password set to:" + mdl.Database.Password, LOGGER_SOURCE, LOG_SEVERITY.VERBOSE);
                break;

            default:
                ApplicationLogger.Log("Unknown key:" + key, LOGGER_SOURCE, LOG_SEVERITY.WARNING);
                return(false);
            }
            //If a config line is invalid return false.
            return(true);
        }