Advertisement
Blizzardo1

Jurgen Resource Load Manager

Aug 28th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.58 KB | None | 0 0
  1. /*
  2.     Jurgen Resource Load Manager  Copyright (C) 2014  Adonis S. Deliannis
  3.     This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  4.     This is free software, and you are welcome to redistribute it
  5.     under certain conditions; type `show c' for details.
  6.  */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Speech.Synthesis;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16.  
  17. namespace Jurgen
  18. {
  19.     class Program
  20.     {
  21.         #region Static Member Definitions
  22.         private static PerformanceCounter _cpuCounter;
  23.         private static PerformanceCounter _memCounter;
  24.         private static PerformanceCounter _sysUptime;
  25.         private static PerformanceCounter _totalDiskIdle;
  26.         private static SpeechSynthesizer synth;
  27.  
  28.         private static string _warnXPercent = "Warning, Your CPU is over 80 percent! For the love of God, take it easy!";
  29.         private static string _warnLessThanXMB = "Warning, You are running low on Memory! Please, close some programs!";
  30.         private static string _errMsgShow = "I'm sorry, {0} is not a valid switch!";
  31.         private static string _paramOverflow = "Parameter overflow!";
  32.         private static string _notEnoughParams = "Not enough Parameters";
  33.         private static string _help = "For help, type help";
  34.         private static string _cmdHelp = "There's a help command ya know! help";
  35.         private static string _errNoCmd = "{0} is not an available command";
  36.  
  37.         private static int _cpuThreshold = 80;
  38.         private static int _cpuThresholdMax = 100;
  39.         private static int _memThreshold = 4096;
  40.         private static List<string> cpuMaxedOut;
  41.         private static Random rand = new Random ( );
  42.         #endregion
  43.  
  44.         #region Constant Values
  45.         public const string AUTHOR = "Adonis S. Deliannis ( Blizzardo1 )";
  46.         public const string APPNAME = "Jurgen Resource Load Manager";
  47.         public const string VERSION = "1.0";
  48.         public const string YEAR = "2014";
  49.         public const string DESCRIPTION = "Jurgen Resource Load Manager displays a few resources to the user";
  50.         public const string DEVELOPMENT_STAGE = "Beta";
  51.         public const string INTRO = "Yurggen Resource Load Manager Version 1 point Oh Beta!";
  52.  
  53.         // Original width and height of the console
  54.         public const int WIDTH = 80;
  55.         public const int HEIGHT = 25;
  56.         // ------------------------------------------
  57.  
  58.         public const string BOXNAME = "JurgenBox";
  59.         public const string CONDITIONS = @"As the copyright says, this Software is absolutely Free, and thanks to @Barnacules for uploading the original project
  60. to github, and making a youtube video about it. Therefore, I have no conditions about this software. Thanks again. :), Adonis S. Deliannis ( Blizzardo1 )";
  61.         public const string COPYRIGHT = @"    {0}
  62.    Copyright (C) {1}  {2}
  63.  
  64.    This program is free software: you can redistribute it and/or modify
  65.    it under the terms of the GNU General Public License as published by
  66.    the Free Software Foundation, either version 3 of the License, or
  67.    (at your option) any later version.
  68.  
  69.    This program is distributed in the hope that it will be useful,
  70.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  71.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  72.    GNU General Public License for more details.
  73.  
  74.    You should have received a copy of the GNU General Public License
  75.    along with this program.  If not, see <http://www.gnu.org/licenses/>.";
  76.         #endregion
  77.  
  78.         /// <summary>
  79.         /// Main Brain of the Vein well... of this entire program, A.K.A. The Entry Point
  80.         /// </summary>
  81.         /// <remarks><see cref="System.String[]"/> args have been removed as this program does not require Arguments</remarks>
  82.         static void Main ( )
  83.         {
  84.             // Declare a new instance of synth
  85.             synth = new SpeechSynthesizer ( );
  86.             cpuMaxedOut = new List<string> ( );
  87.  
  88.             Console.SetWindowSize ( WIDTH + 42, HEIGHT + 32 );
  89.            
  90.             // Print Program and Copyright Info
  91.             Console.WriteLine ( "{0} {1} - {2}", APPNAME, VERSION, DEVELOPMENT_STAGE );
  92.             Console.WriteLine ( "Made by {0} under the GPLv3", AUTHOR );
  93.  
  94.             // Have Jurgen speak the Introduction
  95.             JSpeak ( INTRO );
  96.  
  97.             // Initialise the Performance Counters
  98.             _cpuCounter = new PerformanceCounter ( "Processor Information", "% Processor Time", "_Total" );
  99.             _memCounter = new PerformanceCounter ( "Memory", "Available MBytes" );
  100.             _sysUptime = new PerformanceCounter ( "System", "System Up Time" );
  101.             _totalDiskIdle = new PerformanceCounter ( "PhysicalDisk", "% Idle Time", "_Total" );
  102.  
  103.             // Add our 5 Messages
  104.             cpuMaxedOut.Add ( "WARNING! YOU ARE NOW AT CRITICAL MASS! PRESS THAT FUCKING BUTTON!" );
  105.             cpuMaxedOut.Add ( "WARNING! STOP CHASING DEM SQUIRRELS!" );
  106.             cpuMaxedOut.Add ( "WARNING! I'M MELTING! I'M MELTING!" );
  107.             cpuMaxedOut.Add ( "WARNING! YOU ARE 1 PERCENT EVIL, 99 PERCENT HOT GAS!" );
  108.             cpuMaxedOut.Add ( "RED ALERT! RED ALERT! RED ALERT! RED ALERT! RED ALERT! RED ALERT! I FARTED OUT A HOT GAS" );
  109.  
  110.             // Hide the Console Cursor so it doesn't flicker
  111.             Console.CursorVisible = false;
  112.  
  113.             // Display the contents to the screen
  114.             RenderScreen ( );
  115.         }
  116.  
  117.         private static void RenderScreen ( )
  118.         {
  119.             // Run forever until someone breaks the system
  120.             while(true)
  121.             {
  122.                 // Try looping, if something happens, display a message to the user.
  123.                 try
  124.                 {
  125.                     do
  126.                     {
  127.                         while ( !Console.KeyAvailable )
  128.                         {
  129.                             // Set up all of our variables to display to the user
  130.                             float cpuLoad = _cpuCounter.NextValue ( );
  131.                             float memLoad = _memCounter.NextValue ( );
  132.                             float sysTime = _sysUptime.NextValue ( );
  133.  
  134.                             // Neatly display the Up Time to the user ;)
  135.                             string formatted = TimeSpanToFriendly ( TimeSpan.FromSeconds ( sysTime ) );
  136.  
  137.                             //////////////////////////////////////////////////////////////////////////
  138.                             /* Display the current info we have initialised here                    */
  139.                             Console.WriteLine ( "CPU Load:        {0:0.00}%", cpuLoad );
  140.                             Console.WriteLine ( "Available RAM:   {0}MB - {1:0.00}GB", memLoad, memLoad / 1024 );
  141.                             Console.WriteLine ( "Total Disk Idle: {0:0} %", _totalDiskIdle.NextValue ( ) );
  142.                             // Add some of your own!
  143.                             Console.WriteLine ( "System UpTime:   {0}", formatted );
  144.                             //////////////////////////////////////////////////////////////////////////
  145.  
  146.                             // If we are getting higher than our threshold, spurt out a message
  147.                             if ( cpuLoad > _cpuThreshold )
  148.                             {
  149.                                 // If we are getting higher than our max threshold, spurt out a Critical Error!
  150.                                 if ( cpuLoad == _cpuThresholdMax )
  151.                                     JSpeak ( cpuMaxedOut[ rand.Next ( 0, cpuMaxedOut.Count ) ] );
  152.                                 else
  153.                                     JSpeak ( _warnXPercent );
  154.                             }
  155.  
  156.                             // If we are getting lower than our threshold, spurt out a message
  157.                             if ( memLoad < _memThreshold )
  158.                             {
  159.                                 JSpeak ( _warnLessThanXMB );
  160.                             }
  161.  
  162.                             // Wait then clear
  163.  
  164.                             Thread.Sleep ( 1000 );
  165.                             Console.Clear ( );
  166.                         }
  167.                         // Do the above while we haven't pressed the Escape Key
  168.                     } while ( Console.ReadKey ( true ).Key != ConsoleKey.Escape );
  169.  
  170.                     // Because we dropped to shell, we need to provide the cursor again
  171.                     Console.CursorVisible = true;
  172.                     while ( true )
  173.                     {
  174.                         // JurgenBox> CommandPrompt
  175.                         Console.Write ( "{0}> ", BOXNAME );
  176.                         string[] msg = Console.ReadLine ( ).Split(' ');
  177.  
  178.                         // Our list of commands
  179.                         if ( msg[ 0 ].ToLower ( ) == "show" )
  180.                         {
  181.                             // Check to see if we have an equal length of 2
  182.                             // JurgenBox> show c
  183.                             // JurgenBox> show w
  184.                             if ( msg.Length > 2 )
  185.                                 Error ( "{0} {1}", _paramOverflow, _help );
  186.                             else if ( msg.Length < 2 )
  187.                                 Error ( "Must provide c or w as a parameter" );
  188.                             else
  189.                             {
  190.  
  191.                                 if ( msg[ 1 ].ToLower ( ) == "c" )
  192.                                 {
  193.                                     Console.WriteLine ( CONDITIONS );
  194.                                 }
  195.                                 else if ( msg[ 1 ].ToLower ( ) == "w" )
  196.                                 {
  197.                                     Console.WriteLine ( COPYRIGHT, DESCRIPTION, YEAR, AUTHOR );
  198.                                 }
  199.                                 else
  200.                                 {
  201.                                     Error ( _errMsgShow, msg[ 1 ] );
  202.                                 }
  203.                             }
  204.                         }
  205.                         // JurgenBox> speak message goes here
  206.                         else if ( msg[ 0 ].ToLower() == "speak" )
  207.                         {
  208.                             int index = 1;
  209.                             string _msg = string.Join ( " ", msg, index, msg.Length - index );
  210.                             JSpeak ( _msg );
  211.                         }
  212.                         // JurgenBox> help
  213.                         else if ( msg[ 0 ].ToLower ( ) == "help" )
  214.                         {
  215.                             Console.WriteLine ( "exit | quit - Will exit the shell but continue to run the program" );
  216.                             Console.WriteLine ( "help        - Displays this help file" );
  217.                             Console.WriteLine ( "show <c|w>  - shows the copyright information" );
  218.                             Console.WriteLine ( "          c - Show Terms and Conditions" );
  219.                             Console.WriteLine ( "          w - Show Copyright" );
  220.                             Console.WriteLine ( "speak <msg> - Jurgen Speaks what you write, so BE CAREFUL WHAT YOU SAY!");
  221.                         }
  222.                         // JurgenBox> exit
  223.                         // JurgenBox> quit
  224.                         else if ( msg[ 0 ].ToLower ( ) == "quit" || msg[ 0 ].ToLower ( ) == "exit" )
  225.                         {
  226.                             // Because we're breaking from shell, hide the Cursor again.
  227.                             Console.CursorVisible = false;
  228.                             break;
  229.                         }
  230.                         // What to do if you don't put anything
  231.                         else if ( string.IsNullOrEmpty ( msg[ 0 ] ) )
  232.                         {
  233.                             Warning ( _cmdHelp );
  234.                         }
  235.                         // What to do when you don't type in a registered command
  236.                         else
  237.                         {
  238.                             Error ( _errNoCmd, msg[0] );
  239.                         }
  240.  
  241.                     }
  242.                 }
  243.                 // OH NOEZ! WE GOTZ US A LITTLE EWWOR! FIX IT! FIX IT!
  244.                 catch ( Exception ex )
  245.                 {
  246.                     Console.WriteLine ( ex.Message );
  247.                 }
  248.             }
  249.         }
  250.  
  251.         /// <summary>
  252.         /// Display an Error message to the User
  253.         /// </summary>
  254.         /// <param name="Message">A Formatted <see cref="System.String"/></param>
  255.         /// <param name="args">An <c>Optional</c> <see cref="System.Object"/> array for the <paramref name="Message"/> </param>
  256.         public static void Error ( string Message, params object[] args )
  257.         {
  258.             ConsoleColor resetColour = Console.ForegroundColor;
  259.             Console.ForegroundColor = ConsoleColor.Red;
  260.             Console.Write ( "[Error] " );
  261.             Console.WriteLine ( Message, args );
  262.             Console.ForegroundColor = resetColour;
  263.         }
  264.  
  265.         /// <summary>
  266.         /// Display an Informative message to the User
  267.         /// </summary>
  268.         /// <param name="Message">A Formatted <see cref="System.String"/></param>
  269.         /// <param name="args">An <c>Optional</c> <see cref="System.Object"/> array for the <paramref name="Message"/> </param>
  270.         public static void Info ( string Message, params object[] args )
  271.         {
  272.             ConsoleColor resetColour = Console.ForegroundColor;
  273.             Console.ForegroundColor = ConsoleColor.Cyan;
  274.             Console.Write ( "[Info] " );
  275.             Console.WriteLine ( Message, args );
  276.             Console.ForegroundColor = resetColour;
  277.         }
  278.  
  279.         /// <summary>
  280.         /// Display a Warning message to the User
  281.         /// </summary>
  282.         /// <param name="Message">A Formatted <see cref="System.String"/></param>
  283.         /// <param name="args">An <c>Optional</c> <see cref="System.Object"/> array for the <paramref name="Message"/> </param>
  284.         public static void Warning ( string Message, params object[] args )
  285.         {
  286.             ConsoleColor resetColour = Console.ForegroundColor;
  287.             Console.ForegroundColor = ConsoleColor.Yellow;
  288.             Console.Write ( "[Warning] " );
  289.             Console.WriteLine ( Message, args );
  290.             Console.ForegroundColor = resetColour;
  291.         }
  292.  
  293.         /// <summary>
  294.         /// Return a human readable time
  295.         /// </summary>
  296.         /// <param name="ts">The Timespan to translate to a String</param>
  297.         /// <returns>A well translated; Human-Readable string representing the Time</returns>
  298.         public static string TimeSpanToFriendly ( TimeSpan ts )
  299.         {
  300.             return string.Format ( "{0} {1}, {2} {3}, {4} {5}, and {6} {7}",
  301.                 ts.Days,
  302.                 ts.Days > 1 | ts.Days < 1 ? "days" : "day",
  303.                 ts.Hours,
  304.                 ts.Hours > 1 | ts.Hours < 1 ? "hours" : "hour",
  305.                 ts.Minutes,
  306.                 ts.Minutes > 1 | ts.Minutes < 1 ? "minutes" : "minute",
  307.                 ts.Seconds,
  308.                 ts.Seconds > 1 | ts.Seconds < 1 ? "seconds" : "second"
  309.                 );
  310.         }
  311.  
  312.         /// <summary>
  313.         /// Have Jurgen speak the current message
  314.         /// </summary>
  315.         /// <param name="message">A <see cref="System.String"/> to have the Synth speak</param>
  316.         public static void JSpeak ( string message )
  317.         {
  318.             JSpeak ( message, VoiceGender.Male );
  319.         }
  320.  
  321.         /// <summary>
  322.         /// Have Jurgen speak the current message with a different Voice
  323.         /// </summary>
  324.         /// <param name="message">A <see cref="System.String"/> to have the Synth speak</param>
  325.         /// <param name="gender"> A <see cref="System.Speech.Synthesis.VoiceGender"/> to change the voice of the Synth</param>
  326.         public static void JSpeak ( string message, VoiceGender gender )
  327.         {
  328.             JSpeak ( message, gender, 1 );
  329.         }
  330.  
  331.         /// <summary>
  332.         /// Have Jurgen speak the current message with a different Voice and rate
  333.         /// </summary>
  334.         /// <param name="message">A <see cref="System.String"/> to have the Synth speak</param>
  335.         /// <param name="gender"> A <see cref="System.Speech.Synthesis.VoiceGender"/> to change the voice of the Synth</param>
  336.         /// <param name="rate">An <see cref="System.Int32"/> to change the rate of how fast the Synth speaks</param>
  337.         public static void JSpeak ( string message, VoiceGender gender, int rate )
  338.         {
  339.             synth.Rate = rate;
  340.             synth.SelectVoiceByHints ( gender );
  341.             synth.Speak ( message );
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement