コード例 #1
0
        /// <summary>
        /// Mains the specified args.
        /// </summary>
        /// <param name="args">The arguements need to run the app</param>
        static void Main(string[] args)
        {
            // Give them a header no matter what
            Console.Write("+---------------------------------------+\r\n");
            Console.Write("|         L4DStats .NET Parser          |\r\n");
            Console.Write("|             0.3.0 Beta                |\r\n");

            if (args.Length == 0)
            {
                Console.Write("|                                       |\r\n");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("| Error no arguements passed:           |\r\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("|                                       |\r\n");
                Console.Write("| Arg1: Path of Log Directory           |\r\n");
                Console.Write("| Arg2: Server DNS Name - no http://    |\r\n");
                Console.Write("| Arg3: Remote Stats Pass Key           |\r\n");
                Console.Write("| Arg4: This servers unique ID (1 - 99) |\r\n");
                Console.Write("+---------------------------------------+\r\n");
            }
            else if (args.Length == 1)
            {
                if (args[0] == "?" || args[0] == "/?")
                {
                    Console.Write("| Arg1: Path of Log Directory           |\r\n");
                    Console.Write("| Arg2: Server DNS Name - no http://    |\r\n");
                    Console.Write("| Arg3: Remote Stats Pass Key           |\r\n");
                    Console.Write("| Arg4: This servers unique ID (1 - 99) |\r\n");
                    Console.Write("+---------------------------------------+\r\n");
                    return;
                }
                else
                {
                    Console.Write("|                                       |\r\n");
                    Console.Write("|           www.L4DStats.co.uk          |\r\n");
                    Console.Write("+---------------------------------------+\r\n");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("ERROR: ");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Please ensure you add a double backslash (\\\\) to the end of your directory path if it is enclosed in \" 's");
                    return;
                }
            }
            else if (args.Length == 4)
            {
                string logDirectory = args[0];
                string xmlRPCServer = args[1];
                string xmlRPCKey = args[2];
                string serverID = args[3];
                L4DStats L4DStats = new L4DStats();
                ArrayList gameStats = new ArrayList();
                ArrayList games = new ArrayList();
                FileInfo[] logFiles;

                Console.Write("|                                       |\r\n");
                Console.Write("|           www.L4DStats.co.uk          |\r\n");
                Console.Write("+---------------------------------------+\r\n");

                // Make sure that the user passed us a directory with a trailing slash
                if (logDirectory.Trim().EndsWith("\\") == false)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("INFO: ");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Ensure the Log Directory provided has a trailing slash");
                    return;
                }

                // Look in the directory that the user provided for a list of log files
                try
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(logDirectory);
                    logFiles = dirInfo.GetFiles("*.log");
                }
                catch (System.IO.DirectoryNotFoundException)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("ERROR: ");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("The directory (" + logDirectory + ") doesn't exist.");
                    return;
                }

                if (logFiles.Length == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("INFO: ");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("No valid log files found. Exiting.");
                    return;
                }

                //-------------------------------------------------------------------------------------------
                // Stage 1 - Finding Valid Setup logs (These tell us what Map we are going to be using)
                //-------------------------------------------------------------------------------------------
                // Console.WriteLine("\r\n\r\n----------------------------------");
                Console.WriteLine("\r\nStarting Stage 1 (Log Pre-Processing):\r\n");
                Console.WriteLine("Searching for Setup logs in " + logDirectory);

                // For each one of these log files send it off to be parsed
                // At this stage we only care about whether this is a 'setup' log file
                foreach (FileInfo log in logFiles)
                {
                    bool validFile = false;
                    string logLine = null;
                    string mapName = string.Empty;

                    // Set up all the variables we are going to need
                    try
                    {
                        //Console.WriteLine("Attempting to open: " + logDirectory + log.Name);

                        if (File.Exists(logDirectory + log.Name))
                        {
                            StreamReader re = File.OpenText(logDirectory + log.Name);

                            // If the log file contains 'Loading Map' then its a setup file
                            // This can probably be shortened to: re.Read or something
                            while ((logLine = re.ReadLine()) != null)
                            {
                                // We've found 'Loading Map'
                                if (logLine.Contains("Loading map"))
                                {
                                    // Set the bool to true so the next step knows
                                    // we have a valid file to work with
                                    validFile = true;

                                    // Split the current line on the " char
                                    string[] mapLineSplit = logLine.Split(new char[] { '"' });

                                    // Thankfully the MapName is always after the first "
                                    mapName = mapLineSplit[1];

                                    break;
                                }
                            }

                            // Save them resources!
                            re.Close();
                        }
                        else
                        {
                            // File was deleted by the previous loop
                            // We could break here but the validFile bool check will prevent any further processing
                        }
                    }
                    catch (System.IO.FileLoadException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("WARNING: ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("File found but was not loadable (Locked?) Skipping...");
                        validFile = false;
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("ERROR: ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("File Directory Not Found. Skipping...");
                        validFile = false;
                    }
                    catch (System.IO.IOException)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("ERROR: ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("A generic I/O error occured whilst accessing " + logDirectory + log.Name + " Skipping...");
                        validFile = false;
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("ERROR: ");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine("An un-anticipated error occured. Skipping...");
                        validFile = false;
                    }

                    // The logfile is a setup file!
                    if (validFile == true)
                    {
                        Console.WriteLine(log.Name.ToString() + " is a valid setup file");

                        // Pass the valid file details through to the Log Processer
                        L4DStats.IndivGame gameStruct = L4DStats.ProcessLogs(logDirectory, log.Name.ToString(), mapName);

                        // Once the game struct has been returned it can be packaged up and sent off for remote addition
                        if (gameStruct.Closed == false || gameStruct.Kills < 1)
                        {
                            Console.WriteLine("Insufficient Stats to process Game - skipping\r\n\r\n");
                        }
                        else
                        {
                            L4DStats.PackageStatsForXMLRPC(gameStruct.LogPrefix, gameStruct.MapName, gameStruct.Kills, gameStruct.Stats, xmlRPCServer, xmlRPCKey, serverID);
                        }
                    }
                }

                //-------------------------------------------------------------------------------------------
                // Finished
                //-------------------------------------------------------------------------------------------
                Console.WriteLine("\r\n\r\n--------------------------------------");
                Console.WriteLine("Finished!\r\n");
            }
            else if (args.Length > 4)
            {
                Console.Write("|                                       |\r\n");
                Console.Write("|           www.L4DStats.co.uk          |\r\n");
                Console.Write("+---------------------------------------+\r\n");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("ERROR: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("Too Many Arguments");
            }
            else
            {
                Console.Write("|                                       |\r\n");
                Console.Write("|           www.L4DStats.co.uk          |\r\n");
                Console.Write("+---------------------------------------+\r\n");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("ERROR: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("Argument Missing");
            }
        }
コード例 #2
0
        /// <summary>
        /// Handles the Click event of the startProcessingButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void startProcessingButton_Click(object sender, EventArgs e)
        {
            debugOutputRichTextBox.Clear();
            currentStageProgressBar.Value = 0;
            currentTask.ForeColor = System.Drawing.Color.Black;

            string logDirectory = DirectoryTextBox.Text.ToString() + "\\";
            string xmlRPCServer = XMLRPCServerTextBox.Text.ToString();
            string xmlRPCKey = XMLRPCKeyTextBox.Text.ToString();
            string serverID = ServerIDTextBox.Text.ToString();

            L4DStats L4DStats = new L4DStats();
            ArrayList gameStats = new ArrayList();
            ArrayList games = new ArrayList();

            debugOutputRichTextBox.AppendText("       L4DStats Windows Parser\r\n");
            debugOutputRichTextBox.AppendText("             0.2 Beta\r\n");
            debugOutputRichTextBox.AppendText("\r\n");
            debugOutputRichTextBox.AppendText("  www.NetworksAreMadeOfString.co.uk\r\n");
            currentTask.Text = "Looking in log directory for setup logs...";

            // Look in the directory that the user provided for a list of log files
            DirectoryInfo dirInfo = new DirectoryInfo(logDirectory);
            FileInfo[] logFiles;
            try
            {
                logFiles = dirInfo.GetFiles("*.log");
            }
            catch (System.IO.FileNotFoundException)
            {
                MessageBox.Show("Please provide the entire absoloute path for some reason the GUI version can't handle relative paths", "Log Dir Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                currentTask.Text = "Something bad happened!";
                currentTask.ForeColor = System.Drawing.Color.Red;
                return;
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                MessageBox.Show("Please provide the entire absoloute path for some reason the GUI version can't handle relative paths", "Log Dir Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                currentTask.Text = "Something bad happened!";
                currentTask.ForeColor = System.Drawing.Color.Red;
                return;
            }
            catch
            {
                MessageBox.Show("Something bad happened!", "Log Dir Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                currentTask.Text = "Something bad happened!";
                currentTask.ForeColor = System.Drawing.Color.Red;
                return;
            }

            debugOutputRichTextBox.AppendText("\r\nStarting Stage 1 (Log Pre-Processing):\r\n");
            currentTask.Text = "Starting Stage 1 (Log Pre-Processing)....";

            /* For each one of these log files send it off to be parsed
                   At this stage we only care about whether this is a 'setup' log file*/
                    foreach (FileInfo log in logFiles)
                    {
                        // Set up all the variables we are going to need
                        string logLine = null;
                        bool validFile = false;
                        string mapName = string.Empty;

                        try
                        {
                            StreamReader re = File.OpenText(logDirectory + log.Name);

                            // If the log file contains 'Loading Map' then its a setup file
                            // This can probably be shortened to: re.Read or something
                            while ((logLine = re.ReadLine()) != null)
                            {
                                // We've found 'Loading Map'
                                if (logLine.Contains("Loading map"))
                                {
                                    // Set the bool to true so the next step knows
                                    // we have a valid file to work with
                                    validFile = true;

                                    // Split the current line on the " char
                                    string[] split = logLine.Split(new char[] { '"' });

                                    // Thankfully the MapName is always after the first "
                                    mapName = split[1];

                                    // break;
                                }
                            }

                            // Save them resources!
                            re.Close();
                        }
                        catch (System.IO.FileLoadException)//IOException
                        {
                            debugOutputRichTextBox.AppendText("File found but was not loadable (Locked?) Skipping...");
                            validFile = false;
                        }
                        catch (System.IO.DirectoryNotFoundException)//IOException
                        {
                            debugOutputRichTextBox.AppendText("File Directory Not Found. Skipping...");
                            validFile = false;
                        }
                        catch (System.IO.IOException)//IOException
                        {
                            debugOutputRichTextBox.AppendText("A generic I/O error occured. Skipping...");
                            validFile = false;
                        }
                        catch
                        {
                            debugOutputRichTextBox.AppendText("An un-anticipated error occured. Skipping...");
                            validFile = false;
                        }

                        // The logfile is a setup file!
                        if (validFile == true)
                        {
                            // Create a string array containing the Logname and MapName
                            string[] game = { log.Name.ToString(), mapName };

                            // Add it to our Games List
                            games.Add(game);

                            debugOutputRichTextBox.AppendText("Found valid Setup log file " + log.Name.ToString() + "\r\n");
                        }
                    }

                    currentStageProgressBar.Value = 20;

                    //-------------------------------------------------------------------------------------------
                    // Stage 2 - Loop through each one of the 'valid' Games and try to process the stats in its
                    //           sister log file (most logs start at 000.log and the stats in 001.log
                    //-------------------------------------------------------------------------------------------
                    debugOutputRichTextBox.AppendText("\r\n\r\n----------------------------------");
                    debugOutputRichTextBox.AppendText("Starting Stage 2 (Log Processing):\r\n");
                    currentTask.Text = "Starting Stage 2 (Log Processing)....";

                    // Game[LogFile,MapName]
                    foreach (string[] game in games)
                    {
                        L4DStats.IndivGame gameStruct = L4DStats.ProcessLogs(logDirectory, game[0], game[1]);
                        gameStats.Add(gameStruct);
                    }

                    currentStageProgressBar.Value = 40;

                    //-------------------------------------------------------------------------------------------
                    // Stage 3 - Loop through each one of the processed games and package it up in XML for the
                    //           XMLRPC server and post it
                    //-------------------------------------------------------------------------------------------
                    debugOutputRichTextBox.AppendText("\r\n\r\n--------------------------------------");
                    debugOutputRichTextBox.AppendText("Starting Stage 3 (XML Parse & RPC):\r\n");
                    currentTask.Text = "Starting Stage 3 (XML Parse & RPC)....";
                    int i = 0;
                    foreach (L4DStats.IndivGame game in gameStats)
                    {
                        debugOutputRichTextBox.AppendText("Processing Game " + i.ToString() + ":\r\n\r\n");
                        if (game.Closed == false)
                        {
                            debugOutputRichTextBox.AppendText("Insufficient Stats to process Game - skipping\r\n\r\n");
                        }
                        else
                        {
                            L4DStats.PackageStatsForXMLRPC(game.LogPrefix, game.MapName, game.Kills, game.Stats, xmlRPCServer, xmlRPCKey, serverID);
                        }

                        i++;
                    }

                    currentStageProgressBar.Value = 60;

                    //-------------------------------------------------------------------------------------------
                    // Stage 4 - Now get rid of those files so we don't process them again!
                    //-------------------------------------------------------------------------------------------
                    debugOutputRichTextBox.AppendText("\r\n\r\n--------------------------------------");
                    debugOutputRichTextBox.AppendText("Starting Stage 4 (Cleanup):\r\n");
                    currentTask.Text = "Starting Stage 4 (Cleanup)....";
                    currentStageProgressBar.Value = 80;

                    //-------------------------------------------------------------------------------------------
                    // Finished - Now get rid of those files so we don't process them again!
                    //-------------------------------------------------------------------------------------------
                    debugOutputRichTextBox.AppendText("\r\n\r\n--------------------------------------");
                    debugOutputRichTextBox.AppendText("Finished!\r\n");
                    currentTask.Text = "Finished!";
                    currentTask.ForeColor = System.Drawing.Color.Green;
                    currentStageProgressBar.Value = 100;
        }