/* Jurgen Resource Load Manager Copyright (C) 2014 Adonis S. Deliannis This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Speech.Synthesis; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Jurgen { class Program { #region Static Member Definitions private static PerformanceCounter _cpuCounter; private static PerformanceCounter _memCounter; private static PerformanceCounter _sysUptime; private static PerformanceCounter _totalDiskIdle; private static SpeechSynthesizer synth; private static string _warnXPercent = "Warning, Your CPU is over 80 percent! For the love of God, take it easy!"; private static string _warnLessThanXMB = "Warning, You are running low on Memory! Please, close some programs!"; private static string _errMsgShow = "I'm sorry, {0} is not a valid switch!"; private static string _paramOverflow = "Parameter overflow!"; private static string _notEnoughParams = "Not enough Parameters"; private static string _help = "For help, type help"; private static string _cmdHelp = "There's a help command ya know! help"; private static string _errNoCmd = "{0} is not an available command"; private static int _cpuThreshold = 80; private static int _cpuThresholdMax = 100; private static int _memThreshold = 4096; private static List cpuMaxedOut; private static Random rand = new Random ( ); #endregion #region Constant Values public const string AUTHOR = "Adonis S. Deliannis ( Blizzardo1 )"; public const string APPNAME = "Jurgen Resource Load Manager"; public const string VERSION = "1.0"; public const string YEAR = "2014"; public const string DESCRIPTION = "Jurgen Resource Load Manager displays a few resources to the user"; public const string DEVELOPMENT_STAGE = "Beta"; public const string INTRO = "Yurggen Resource Load Manager Version 1 point Oh Beta!"; // Original width and height of the console public const int WIDTH = 80; public const int HEIGHT = 25; // ------------------------------------------ public const string BOXNAME = "JurgenBox"; public const string CONDITIONS = @"As the copyright says, this Software is absolutely Free, and thanks to @Barnacules for uploading the original project to github, and making a youtube video about it. Therefore, I have no conditions about this software. Thanks again. :), Adonis S. Deliannis ( Blizzardo1 )"; public const string COPYRIGHT = @" {0} Copyright (C) {1} {2} This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see ."; #endregion /// /// Main Brain of the Vein well... of this entire program, A.K.A. The Entry Point /// /// args have been removed as this program does not require Arguments static void Main ( ) { // Declare a new instance of synth synth = new SpeechSynthesizer ( ); cpuMaxedOut = new List ( ); Console.SetWindowSize ( WIDTH + 42, HEIGHT + 32 ); // Print Program and Copyright Info Console.WriteLine ( "{0} {1} - {2}", APPNAME, VERSION, DEVELOPMENT_STAGE ); Console.WriteLine ( "Made by {0} under the GPLv3", AUTHOR ); // Have Jurgen speak the Introduction JSpeak ( INTRO ); // Initialise the Performance Counters _cpuCounter = new PerformanceCounter ( "Processor Information", "% Processor Time", "_Total" ); _memCounter = new PerformanceCounter ( "Memory", "Available MBytes" ); _sysUptime = new PerformanceCounter ( "System", "System Up Time" ); _totalDiskIdle = new PerformanceCounter ( "PhysicalDisk", "% Idle Time", "_Total" ); // Add our 5 Messages cpuMaxedOut.Add ( "WARNING! YOU ARE NOW AT CRITICAL MASS! PRESS THAT FUCKING BUTTON!" ); cpuMaxedOut.Add ( "WARNING! STOP CHASING DEM SQUIRRELS!" ); cpuMaxedOut.Add ( "WARNING! I'M MELTING! I'M MELTING!" ); cpuMaxedOut.Add ( "WARNING! YOU ARE 1 PERCENT EVIL, 99 PERCENT HOT GAS!" ); cpuMaxedOut.Add ( "RED ALERT! RED ALERT! RED ALERT! RED ALERT! RED ALERT! RED ALERT! I FARTED OUT A HOT GAS" ); // Hide the Console Cursor so it doesn't flicker Console.CursorVisible = false; // Display the contents to the screen RenderScreen ( ); } private static void RenderScreen ( ) { // Run forever until someone breaks the system while(true) { // Try looping, if something happens, display a message to the user. try { do { while ( !Console.KeyAvailable ) { // Set up all of our variables to display to the user float cpuLoad = _cpuCounter.NextValue ( ); float memLoad = _memCounter.NextValue ( ); float sysTime = _sysUptime.NextValue ( ); // Neatly display the Up Time to the user ;) string formatted = TimeSpanToFriendly ( TimeSpan.FromSeconds ( sysTime ) ); ////////////////////////////////////////////////////////////////////////// /* Display the current info we have initialised here */ Console.WriteLine ( "CPU Load: {0:0.00}%", cpuLoad ); Console.WriteLine ( "Available RAM: {0}MB - {1:0.00}GB", memLoad, memLoad / 1024 ); Console.WriteLine ( "Total Disk Idle: {0:0} %", _totalDiskIdle.NextValue ( ) ); // Add some of your own! Console.WriteLine ( "System UpTime: {0}", formatted ); ////////////////////////////////////////////////////////////////////////// // If we are getting higher than our threshold, spurt out a message if ( cpuLoad > _cpuThreshold ) { // If we are getting higher than our max threshold, spurt out a Critical Error! if ( cpuLoad == _cpuThresholdMax ) JSpeak ( cpuMaxedOut[ rand.Next ( 0, cpuMaxedOut.Count ) ] ); else JSpeak ( _warnXPercent ); } // If we are getting lower than our threshold, spurt out a message if ( memLoad < _memThreshold ) { JSpeak ( _warnLessThanXMB ); } // Wait then clear Thread.Sleep ( 1000 ); Console.Clear ( ); } // Do the above while we haven't pressed the Escape Key } while ( Console.ReadKey ( true ).Key != ConsoleKey.Escape ); // Because we dropped to shell, we need to provide the cursor again Console.CursorVisible = true; while ( true ) { // JurgenBox> CommandPrompt Console.Write ( "{0}> ", BOXNAME ); string[] msg = Console.ReadLine ( ).Split(' '); // Our list of commands if ( msg[ 0 ].ToLower ( ) == "show" ) { // Check to see if we have an equal length of 2 // JurgenBox> show c // JurgenBox> show w if ( msg.Length > 2 ) Error ( "{0} {1}", _paramOverflow, _help ); else if ( msg.Length < 2 ) Error ( "Must provide c or w as a parameter" ); else { if ( msg[ 1 ].ToLower ( ) == "c" ) { Console.WriteLine ( CONDITIONS ); } else if ( msg[ 1 ].ToLower ( ) == "w" ) { Console.WriteLine ( COPYRIGHT, DESCRIPTION, YEAR, AUTHOR ); } else { Error ( _errMsgShow, msg[ 1 ] ); } } } // JurgenBox> speak message goes here else if ( msg[ 0 ].ToLower() == "speak" ) { int index = 1; string _msg = string.Join ( " ", msg, index, msg.Length - index ); JSpeak ( _msg ); } // JurgenBox> help else if ( msg[ 0 ].ToLower ( ) == "help" ) { Console.WriteLine ( "exit | quit - Will exit the shell but continue to run the program" ); Console.WriteLine ( "help - Displays this help file" ); Console.WriteLine ( "show - shows the copyright information" ); Console.WriteLine ( " c - Show Terms and Conditions" ); Console.WriteLine ( " w - Show Copyright" ); Console.WriteLine ( "speak - Jurgen Speaks what you write, so BE CAREFUL WHAT YOU SAY!"); } // JurgenBox> exit // JurgenBox> quit else if ( msg[ 0 ].ToLower ( ) == "quit" || msg[ 0 ].ToLower ( ) == "exit" ) { // Because we're breaking from shell, hide the Cursor again. Console.CursorVisible = false; break; } // What to do if you don't put anything else if ( string.IsNullOrEmpty ( msg[ 0 ] ) ) { Warning ( _cmdHelp ); } // What to do when you don't type in a registered command else { Error ( _errNoCmd, msg[0] ); } } } // OH NOEZ! WE GOTZ US A LITTLE EWWOR! FIX IT! FIX IT! catch ( Exception ex ) { Console.WriteLine ( ex.Message ); } } } /// /// Display an Error message to the User /// /// A Formatted /// An Optional array for the public static void Error ( string Message, params object[] args ) { ConsoleColor resetColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write ( "[Error] " ); Console.WriteLine ( Message, args ); Console.ForegroundColor = resetColour; } /// /// Display an Informative message to the User /// /// A Formatted /// An Optional array for the public static void Info ( string Message, params object[] args ) { ConsoleColor resetColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Cyan; Console.Write ( "[Info] " ); Console.WriteLine ( Message, args ); Console.ForegroundColor = resetColour; } /// /// Display a Warning message to the User /// /// A Formatted /// An Optional array for the public static void Warning ( string Message, params object[] args ) { ConsoleColor resetColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write ( "[Warning] " ); Console.WriteLine ( Message, args ); Console.ForegroundColor = resetColour; } /// /// Return a human readable time /// /// The Timespan to translate to a String /// A well translated; Human-Readable string representing the Time public static string TimeSpanToFriendly ( TimeSpan ts ) { return string.Format ( "{0} {1}, {2} {3}, {4} {5}, and {6} {7}", ts.Days, ts.Days > 1 | ts.Days < 1 ? "days" : "day", ts.Hours, ts.Hours > 1 | ts.Hours < 1 ? "hours" : "hour", ts.Minutes, ts.Minutes > 1 | ts.Minutes < 1 ? "minutes" : "minute", ts.Seconds, ts.Seconds > 1 | ts.Seconds < 1 ? "seconds" : "second" ); } /// /// Have Jurgen speak the current message /// /// A to have the Synth speak public static void JSpeak ( string message ) { JSpeak ( message, VoiceGender.Male ); } /// /// Have Jurgen speak the current message with a different Voice /// /// A to have the Synth speak /// A to change the voice of the Synth public static void JSpeak ( string message, VoiceGender gender ) { JSpeak ( message, gender, 1 ); } /// /// Have Jurgen speak the current message with a different Voice and rate /// /// A to have the Synth speak /// A to change the voice of the Synth /// An to change the rate of how fast the Synth speaks public static void JSpeak ( string message, VoiceGender gender, int rate ) { synth.Rate = rate; synth.SelectVoiceByHints ( gender ); synth.Speak ( message ); } } }