From bcd9f46b0a7c50956bb009d522ef85ae66c34db5 Mon Sep 17 00:00:00 2001 From: Scionwest_cp Date: Thu, 8 Mar 2012 19:44:18 -0800 Subject: [PATCH] Added improved logging through the engine for the server console. --- MudEngine/WinPC_Engine/Core/CommandSystem.cs | 20 ++++++-- MudEngine/WinPC_Engine/Core/Logger.cs | 3 +- MudEngine/WinPC_Engine/Game/StandardGame.cs | 51 ++++++++++++++++--- MudEngine/WinPC_Engine/Game/World.cs | 9 ++++ .../GameScripts/Commands/CreatePlayer.cs | 17 +++++-- .../GameScripts/Commands/SetRole.cs | 2 +- MudEngine/WinPC_Engine/Networking/Server.cs | 5 ++ MudEngine/WinPC_Engine/Scripting/CSharp.cs | 20 +++++--- .../WinPC_Engine/Scripting/CompileEngine.cs | 6 ++- MudEngine/WinPC_Server/Program.cs | 9 +++- 10 files changed, 115 insertions(+), 27 deletions(-) diff --git a/MudEngine/WinPC_Engine/Core/CommandSystem.cs b/MudEngine/WinPC_Engine/Core/CommandSystem.cs index a19b0e5..6a05a2e 100644 --- a/MudEngine/WinPC_Engine/Core/CommandSystem.cs +++ b/MudEngine/WinPC_Engine/Core/CommandSystem.cs @@ -143,9 +143,9 @@ namespace MudEngine.Core /// Loads all of the commands found within the assembly specified. /// /// - public static void LoadCommandLibrary(Assembly commandLibrary) + public static ICommand[] LoadCommandLibrary(Assembly commandLibrary) { - LoadCommandLibrary(commandLibrary, true); + return LoadCommandLibrary(commandLibrary, true); } /// @@ -155,14 +155,20 @@ namespace MudEngine.Core /// /// /// - public static void LoadCommandLibrary(Assembly commandLibrary, bool purgeLoadedCommands) + public static ICommand[] LoadCommandLibrary(Assembly commandLibrary, bool purgeLoadedCommands) { //Check if we need to purge all of the commands. if (purgeLoadedCommands) PurgeCommands(); if (commandLibrary == null) - return; + return null; + + //Custom commands are temporarily stored here. + //Since Commands are stored in the general Command collection + //that can contain internal commands as well, we want to + //return only what we just loaded. + List commandsFound = new List(); //Loop through each Type in the assembly provided. foreach (Type type in commandLibrary.GetTypes()) @@ -192,8 +198,14 @@ namespace MudEngine.Core //Everything checks out ok. Add the command to our collection. Commands.Add(cmd.Name, cmd); + commandsFound.Add(cmd); } } + + if (commandsFound.Count > 0) + return commandsFound.ToArray(); + else + return null; } /// diff --git a/MudEngine/WinPC_Engine/Core/Logger.cs b/MudEngine/WinPC_Engine/Core/Logger.cs index a48825e..fc4e3eb 100644 --- a/MudEngine/WinPC_Engine/Core/Logger.cs +++ b/MudEngine/WinPC_Engine/Core/Logger.cs @@ -49,7 +49,8 @@ namespace MudEngine.Core System.IO.File.Delete(LogFilename); //Clear the cache. - _Messages.Clear(); + if (_Messages != null) + _Messages.Clear(); } public static void WriteLine(String message, Importance importance) diff --git a/MudEngine/WinPC_Engine/Game/StandardGame.cs b/MudEngine/WinPC_Engine/Game/StandardGame.cs index 2f0bc17..0a2f72f 100644 --- a/MudEngine/WinPC_Engine/Game/StandardGame.cs +++ b/MudEngine/WinPC_Engine/Game/StandardGame.cs @@ -12,6 +12,7 @@ using MudEngine.Game.Characters; using MudEngine.DAL; using MudEngine.Game.Environment; using MudEngine.Scripting; +using MudEngine.Core.Interfaces; namespace MudEngine.Game { @@ -105,7 +106,6 @@ namespace MudEngine.Game /// public StandardGame(String name, Int32 port) { - Logger.WriteLine("Initializing Mud Game"); this.Name = name; this.Website = "http://scionwest.net"; this.Description = "A sample Mud game created using the Mud Designer kit."; @@ -118,19 +118,27 @@ namespace MudEngine.Game this.Server = new Server(this, port); //Setup default save paths. - DataPaths paths = new DataPaths(); - - this.SavePaths = paths; + this.SavePaths = new DataPaths(); SetupPaths(); this.World = new World(this); } + /// + /// Runs a script compiler and scans for custom scripts that + /// inherit StandardGame and then returns them. This provides + /// support for custom Game rules via Script. + /// + /// public StandardGame Initialize() { + //Instance a new compiler CompileEngine compiler = new CompileEngine(); + Logger.WriteLine("Checking for custom Game scripts."); + compiler.AddAssemblyReference(Path.Combine(this.SavePaths.GetPath(DataTypes.Root), Assembly.GetExecutingAssembly().Location)); + Boolean result = compiler.Compile(this.SavePaths.GetPath(DataTypes.Scripts)); if (result) @@ -139,11 +147,18 @@ namespace MudEngine.Game StandardGame game = (StandardGame)factory.FindInheritedScripted("StandardGame", "Mud Game"); if (game == null) + { + Logger.WriteLine("No custom Game rules located. Defaulting to Standard setup."); return null; + } else + { + Logger.WriteLine("Located " + game.GetType().Name + " ruleset."); return game; + } } - + else + Logger.WriteLine("Failed to perform startup compilation! " + compiler.Errors); return null; } @@ -158,13 +173,15 @@ namespace MudEngine.Game public virtual Boolean Start(Int32 maxPlayers, Int32 maxQueueSize) { Logger.WriteLine("Starting up " + this.Name); - + //Instance Script Engine + Logger.WriteLine("Preparing script engine..."); CompileEngine compiler = new CompileEngine("cs"); //compiler.AddAssemblyReference(Assembly.GetExecutingAssembly().FullName); compiler.AddAssemblyReference(Path.Combine(this.SavePaths.GetPath(DataTypes.Root), Assembly.GetExecutingAssembly().Location)); //Compile any scripts + Logger.WriteLine("Compiling game scripts."); Boolean result = compiler.Compile(this.SavePaths.GetPath(DataTypes.Scripts)); if (!result) { @@ -175,12 +192,32 @@ namespace MudEngine.Game } else { + Logger.WriteLine("Compilation completed."); this.ScriptFactory = new ScriptFactory(compiler.CompiledAssembly); } //Load the default engine Commands + Logger.WriteLine("Loading internal game commands."); CommandSystem.LoadCommands(); - CommandSystem.LoadCommandLibrary(this.ScriptFactory.Assembly, false); + if (CommandSystem.Commands.Count > 0) + { + foreach (ICommand command in CommandSystem.Commands.Values) + Logger.WriteLine("Loaded Command: " + command.Name); + } + else + Logger.WriteLine("No internal game commands located."); + + Logger.WriteLine("Loading scripted game commands."); + ICommand[] commands = CommandSystem.LoadCommandLibrary(this.ScriptFactory.Assembly, false); + if (commands.Length > 0) + { + foreach (ICommand command in commands) + { + Logger.WriteLine("Loaded Command: " + command.Name); + } + } + else + Logger.WriteLine("No scripted game commands located."); //Load World this.World.Initialize(); diff --git a/MudEngine/WinPC_Engine/Game/World.cs b/MudEngine/WinPC_Engine/Game/World.cs index 35941f6..063204b 100644 --- a/MudEngine/WinPC_Engine/Game/World.cs +++ b/MudEngine/WinPC_Engine/Game/World.cs @@ -5,6 +5,7 @@ using System.Text; using MudEngine.Game.Environment; using MudEngine.Core.Interfaces; +using MudEngine.Core; namespace MudEngine.Game { @@ -28,15 +29,22 @@ namespace MudEngine.Game public void Initialize() { + Logger.WriteLine("Initializing game world..."); Realm realm = new Realm(this.Game, "Azeroth", ""); + realm.Initialize(); + + //Zone initialize method is called by Realm. Zone zone = realm.CreateZone("Bablo", ""); + //Room initialize method is called by Zone zone.CreateRoom("Bedroom", ""); zone.CreateRoom("Hallway", ""); zone.LinkRooms("Bedroom", "Hallway", AvailableTravelDirections.East); this.StartLocation = zone.GetRoom("Bedroom"); + + Logger.WriteLine("Initialization completed."); } public void Save() @@ -45,6 +53,7 @@ namespace MudEngine.Game public void Load() { + Logger.WriteLine("World Loading has not been implemented as of yet!"); } public void Destroy() diff --git a/MudEngine/WinPC_Engine/GameScripts/Commands/CreatePlayer.cs b/MudEngine/WinPC_Engine/GameScripts/Commands/CreatePlayer.cs index 5afd3f5..c587b40 100644 --- a/MudEngine/WinPC_Engine/GameScripts/Commands/CreatePlayer.cs +++ b/MudEngine/WinPC_Engine/GameScripts/Commands/CreatePlayer.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using System.IO; using System.Diagnostics; +using MudEngine.Core; using MudEngine.Core.Interfaces; using MudEngine.Game; using MudEngine.Game.Characters; @@ -43,7 +44,7 @@ namespace MudEngine.GameScripts.Commands //Don't allow anything other than the Login command to start the //character creation process. - if (callingType != "CommandLogin") + if (callingType != "Login") { character.SendMessage("Invalid Command Used."); return false; @@ -80,11 +81,19 @@ namespace MudEngine.GameScripts.Commands } } - character.Move(game.World.StartLocation); + try + { - //TODO: Create a class and setup Stats. - character.Save(character.Filename, false); + character.Move(game.World.StartLocation); + //TODO: Create a class and setup Stats. + character.Save(character.Filename, false); + } + catch (Exception ex) + { + Logger.WriteLine(ex.Message); + return false; + } return true; } } diff --git a/MudEngine/WinPC_Engine/GameScripts/Commands/SetRole.cs b/MudEngine/WinPC_Engine/GameScripts/Commands/SetRole.cs index 8c0f504..7948b82 100644 --- a/MudEngine/WinPC_Engine/GameScripts/Commands/SetRole.cs +++ b/MudEngine/WinPC_Engine/GameScripts/Commands/SetRole.cs @@ -39,7 +39,7 @@ namespace MudEngine.GameScripts.Commands names.Add(match.Value.ToLower()); } - if (names.Count < 1) + if (names.Count < 1 && character.Role == CharacterRoles.Admin) { character.SendMessage("You must provide a target character name."); ShowHelp(character); diff --git a/MudEngine/WinPC_Engine/Networking/Server.cs b/MudEngine/WinPC_Engine/Networking/Server.cs index c5c476e..3b209f9 100644 --- a/MudEngine/WinPC_Engine/Networking/Server.cs +++ b/MudEngine/WinPC_Engine/Networking/Server.cs @@ -59,6 +59,7 @@ namespace MudEngine.Networking public void Start(Int32 maxConnections, Int32 maxQueueSize) { + Logger.WriteLine("Game Server System Starting..."); if (this.Status != ServerStatus.Stopped) return; @@ -78,10 +79,14 @@ namespace MudEngine.Networking this._ServerThread = new Thread(ServerLoop); this._ServerThread.Start(); + + Logger.WriteLine("Server status: Running"); } catch { + Logger.WriteLine("Failed to star the Engines Networking Server!"); this.Status = ServerStatus.Stopped; + Logger.WriteLine("Server status: Stopped"); } } diff --git a/MudEngine/WinPC_Engine/Scripting/CSharp.cs b/MudEngine/WinPC_Engine/Scripting/CSharp.cs index 9cf0f00..328d60d 100644 --- a/MudEngine/WinPC_Engine/Scripting/CSharp.cs +++ b/MudEngine/WinPC_Engine/Scripting/CSharp.cs @@ -6,6 +6,8 @@ using System.IO; using Microsoft.CSharp; #endif +using MudEngine.Core; + namespace MudEngine.Scripting { /// @@ -51,14 +53,18 @@ namespace MudEngine.Scripting //Create an array of script files found within the ScriptRepository matching the ScriptExtension properties. String[] scripts = Directory.GetFiles(scriptRepository, "*" + this.ScriptExtension, SearchOption.AllDirectories); - //Compile the scripts and provide the Results property with a reference to the compilation results. - Results = provider.CompileAssemblyFromFile(param, scripts); - - //if the compiler has errors, return false. - if (Results.Errors.HasErrors) - return false; + if (scripts.Length > 0) + { + //Compile the scripts and provide the Results property with a reference to the compilation results. + Results = provider.CompileAssemblyFromFile(param, scripts); + //if the compiler has errors, return false. + if (Results.Errors.HasErrors) + return false; + else + return true; + } else - return true; + return false; } /// diff --git a/MudEngine/WinPC_Engine/Scripting/CompileEngine.cs b/MudEngine/WinPC_Engine/Scripting/CompileEngine.cs index 4169357..0235bae 100644 --- a/MudEngine/WinPC_Engine/Scripting/CompileEngine.cs +++ b/MudEngine/WinPC_Engine/Scripting/CompileEngine.cs @@ -7,6 +7,9 @@ using System.Text; #if WINDOWS_PC using Microsoft.CSharp; #endif + +using MudEngine.Core; + namespace MudEngine.Scripting { /// @@ -170,7 +173,7 @@ namespace MudEngine.Scripting this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." }; return false; } - + //Get the compiler parameters. CompilerParameters param = GetParameters(); @@ -407,7 +410,6 @@ namespace MudEngine.Scripting } } - return compiler; } #endif diff --git a/MudEngine/WinPC_Server/Program.cs b/MudEngine/WinPC_Server/Program.cs index 19a5871..0da9f13 100644 --- a/MudEngine/WinPC_Server/Program.cs +++ b/MudEngine/WinPC_Server/Program.cs @@ -22,6 +22,8 @@ namespace WinPC_Server Logger.LogFilename = "StandardGame.Log"; Logger.Enabled = true; Logger.ConsoleOutPut = true; + Logger.ClearLog(); //Delete previous file. + Logger.WriteLine("Server app starting..."); //Instance and setup our game StandardGame game = new StandardGame("Sample Mud Game"); @@ -33,7 +35,12 @@ namespace WinPC_Server game = g; //Start the game and server. - game.Start(100, 20); + Boolean started = game.Start(100, 20); + + if (started) + Logger.WriteLine(game.GetType().Name + " started and running."); + else + Logger.WriteLine(game.GetType().Name + " failed to start!"); //Game loops until it is disabled. while (game.Enabled)