Migrated remaining source files into their new directory structures. Namespace migration to follow suit.

Moved rScript source files into the Mud Engine.Scripting files.  Planning on removing the reference to rScript.dll and keep everything within the MudEngine.
This commit is contained in:
Scionwest_cp 2011-09-27 19:15:37 -07:00
parent c432edbef9
commit a00f60d22b
19 changed files with 651 additions and 20 deletions

View file

@ -62,30 +62,33 @@
<Compile Include="Commands\CommandEditZone.cs" />
<Compile Include="Commands\CommandEditRoom.cs" />
<Compile Include="DAL\SaveDataPaths.cs" />
<Compile Include="GameManagement\CommandEngine.cs" />
<Compile Include="Commands\CommandSystem.cs" />
<Compile Include="GameManagement\GameTime.cs" />
<Compile Include="GameManagement\GameWorld.cs" />
<Compile Include="GameManagement\ICommand.cs" />
<Compile Include="DAL\FileManager.cs" />
<Compile Include="DAL\SaveDataTypes.cs" />
<Compile Include="DAL\XmlSerialization.cs" />
<Compile Include="GameManagement\Game.cs" />
<Compile Include="GameManagement\ICommand.cs" />
<Compile Include="GameManagement\Log.cs" />
<Compile Include="GameManagement\SecurityRoles.cs" />
<Compile Include="GameObjects\Bag.cs" />
<Compile Include="GameObjects\BaseObject.cs" />
<Compile Include="GameObjects\Characters\BaseCharacter.cs" />
<Compile Include="GameObjects\Characters\BaseStats.cs" />
<Compile Include="GameObjects\Characters\DialogChat.cs" />
<Compile Include="GameObjects\Environment\Door.cs" />
<Compile Include="GameObjects\Environment\Realm.cs" />
<Compile Include="GameObjects\Environment\Room.cs" />
<Compile Include="GameObjects\Environment\StartingLocation.cs" />
<Compile Include="GameObjects\Environment\TerrainType.cs" />
<Compile Include="GameObjects\Environment\TravelDirections.cs" />
<Compile Include="GameObjects\Environment\Zone.cs" />
<Compile Include="GameObjects\Items\BaseItem.cs" />
<Compile Include="GameObjects\Currency.cs" />
<Compile Include="Items\Bag.cs" />
<Compile Include="Core\BaseObject.cs" />
<Compile Include="Core\BaseCharacter.cs" />
<Compile Include="GameManagement\BaseStats.cs" />
<Compile Include="Communication\DialogChat.cs" />
<Compile Include="Scripting\CompileEngine.cs" />
<Compile Include="Scripting\ScriptFactory.cs" />
<Compile Include="Scripting\ScriptObject.cs" />
<Compile Include="World\Door.cs" />
<Compile Include="World\Realm.cs" />
<Compile Include="World\Room.cs" />
<Compile Include="World\StartingLocation.cs" />
<Compile Include="World\TerrainType.cs" />
<Compile Include="World\TravelDirections.cs" />
<Compile Include="World\Zone.cs" />
<Compile Include="Core\BaseItem.cs" />
<Compile Include="Items\Currency.cs" />
<Compile Include="Communication\Server.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripting\MudScriptCompiler.cs" />
@ -96,10 +99,7 @@
<Name>rScripting</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Objects\" />
<Folder Include="World\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -0,0 +1,404 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
using rScripting.Compilers;
using rScripting.LateBinding;
namespace MudEngine.Scripting
{
/// <summary>
/// Provides Properties & Methods needed to compile script source code into .NET assemblies.
/// </summary>
public class CompileEngine
{
/// <summary>
/// The file extension used for the script files.
/// </summary>
public String ScriptExtension
{
get
{
return _ScriptExtension;
}
set
{
if (value.StartsWith("."))
_ScriptExtension = value;
else
_ScriptExtension = "." + value;
}
}
private String _ScriptExtension;
/// <summary>
/// Provides a collection of Assemblies that the compiler will add to its reference list.
/// </summary>
public List<String> AssemblyReferences { get; private set; }
/// <summary>
/// Provides a reference to the assembly generated during script compilation.
/// </summary>
public Assembly CompiledAssembly { get; set; }
/// <summary>
/// The compiler that will be used when the contents of ScriptRepository are compiled.
/// </summary>
public String Compiler { get; set; }
/// <summary>
/// Used to supply compiling options to various compilers if they support this feature.
/// </summary>
public Dictionary<String, String> CompilerOptions { get; set; }
/// <summary>
/// Used to check if the compilation contained any errors.
/// </summary>
public Boolean HasErrors { get; internal set; }
/// <summary>
/// String of errors that occurred during compilation, if any.
/// </summary>
public String Errors
{
get
{
if (_CompileMessages.Length == 0)
return "No Errors.";
else
{
StringBuilder builder = new StringBuilder();
foreach (String error in _CompileMessages)
{
builder.AppendLine(error);
}
return builder.ToString();
}
}
}
//Messages stored from the compilers CompilerResults property.
private String[] _CompileMessages;
//Returns all of the assemblies currently loaded in the current domain.
private Assembly[] _Assemblies
{
get
{
return AppDomain.CurrentDomain.GetAssemblies();
}
}
public CompileEngine() : this(".cs")
{
//Passes defaults off to the parameterized constructor.
}
public CompileEngine(String scriptExtensions)
{
_CompileMessages = new String[] { "No compiler messages available." };
CompilerOptions = new Dictionary<string, string>();
Compiler = "C#";
AssemblyReferences = new List<string>();
AssemblyReferences.Add("mscorlib.dll");
AssemblyReferences.Add("System.dll");
AssemblyReferences.Add("System.Core.dll");
ScriptExtension = scriptExtensions;
}
/// <summary>
/// Adds a reference to the supplied Assembly name to the compilers reference collection.
/// </summary>
/// <param name="assembly"></param>
public void AddAssemblyReference(String assembly)
{
if (!AssemblyReferences.Contains(assembly))
AssemblyReferences.Add(assembly);
}
/// <summary>
/// Adds a reference to the supplied Assembly to the compilers reference collection.
/// </summary>
/// <param name="assembly"></param>
public void AddAssemblyReference(Assembly assembly)
{
if (!AssemblyReferences.Contains(assembly.GetName().Name))
AssemblyReferences.Add(assembly.GetName().Name);
}
/// <summary>
/// Removes the supplied assembly from the compilers reference collection.
/// </summary>
/// <param name="assembly"></param>
public void RemoveAssemblyReference(String assembly)
{
if (AssemblyReferences.Contains(assembly))
AssemblyReferences.Remove(assembly);
}
/// <summary>
/// Clears the compilers reference collection, leaving it empty.
/// </summary>
public void ClearAssemblyReference()
{
AssemblyReferences.Clear();
}
/// <summary>
/// Compiles the scripts found within the CompileEngine.ScriptRepository directory that match the CompileEngine.ScriptExtension file extension.
/// The compiler will compile the scripts using the compiler specified with the CompileEngine.Compiler Property.
/// </summary>
/// <returns>Returns true if compilation was completed without any errors.</returns>
public Boolean Compile(String scriptRepository)
{
//Get the compiler that the developer has selected.
//If the developer chooses a compiler that is not part of the engine, the GetCompiler() method
//will check all the currently loaded assemblies in memory for a custom compiler implementing
//the ICompiler interface.
Type compiler = GetCompiler();
//Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler")
{
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false;
}
//Get the compiler parameters.
CompilerParameters param = GetParameters();
//Create a Instance of the compiler, either custom or internal.
ICompiler com = (ICompiler)Activator.CreateInstance(compiler);
//Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
//Compile the scripts.
Boolean isSuccess = com.Compile(param, scriptRepository);
HasErrors = !isSuccess;
//If the compilation failed, store all of the compiler errors
//into our _CompileMessages string.
if (!isSuccess)
{
List<String> compilerMessages = new List<string>();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false;
}
else
{
//Compiling completed without error, so we need to save
//a reference to the compiled assembly.
CompiledAssembly = com.Results.CompiledAssembly;
return true;
}
}
/// <summary>
/// Compiles the script supplied.
/// The compiler will compile the script using the compiler specified with the CompileEngine.Compiler Property.
/// </summary>
/// <returns>Returns true if compilation was completed without any errors.</returns>
public Boolean Compile(FileInfo sourceFile)
{
if (!sourceFile.Exists)
{
this._CompileMessages = new String[] { "Error: File " + sourceFile.FullName + " does not exists." };
return false;
}
//Get the compiler that the developer has selected.
//If the developer chooses a compiler that is not part of the engine, the GetCompiler() method
//will check all the currently loaded assemblies in memory for a custom compiler implementing
//the ICompiler interface.
Type compiler = GetCompiler();
//Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler")
{
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false;
}
//Get the compiler parameters.
CompilerParameters param = GetParameters();
//Create a Instance of the compiler, either custom or internal.
ICompiler com = (ICompiler)Activator.CreateInstance(compiler);
//Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
//Compile the script.
Boolean isSuccess = com.Compile(param, sourceFile);
HasErrors = !isSuccess;
//If the compilation failed, store all of the compiler errors
//into our _CompileMessages string.
if (!isSuccess)
{
List<String> compilerMessages = new List<string>();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false;
}
else
{
//Compiling completed without error, so we need to save
//a reference to the compiled assembly.
CompiledAssembly = com.Results.CompiledAssembly;
return true;
}
}
/// <summary>
/// Compiles the source code provided.
/// The compiler will compile the scripts using the compiler specified with the CompileEngine.Compiler Property.
/// </summary>
/// <returns>Returns true if compilation was completed without any errors.</returns>
public Boolean Compile(String[] sourceCode)
{
//Get the compiler that the developer has selected.
//If the developer chooses a compiler that is not part of the engine, the GetCompiler() method
//will check all the currently loaded assemblies in memory for a custom compiler implementing
//the ICompiler interface.
Type compiler = GetCompiler();
//Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler")
{
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false;
}
//Get the compiler parameters.
CompilerParameters param = GetParameters();
//Create a Instance of the compiler, either custom or internal.
ICompiler com = (ICompiler)Activator.CreateInstance(compiler);
//Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
//Compile the scripts.
Boolean isSuccess = com.Compile(param, sourceCode);
HasErrors = !isSuccess;
//If the compilation failed, store all of the compiler errors
//into our _CompileMessages string.
if (!isSuccess)
{
List<String> compilerMessages = new List<string>();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false;
}
else
{
//Compiling completed without error, so we need to save
//a reference to the compiled assembly.
CompiledAssembly = com.Results.CompiledAssembly;
return true;
}
}
/// <summary>
/// Gets compiler parameters that the compiler will be supplied with.
/// </summary>
/// <returns></returns>
private CompilerParameters GetParameters()
{
//Setup some default parameters that will be used by the compilers.
CompilerParameters param = new CompilerParameters(this.AssemblyReferences.ToArray());
param.GenerateExecutable = false;
param.GenerateInMemory = true;
//Left out, Add as CompileEngine properties in the future.
//param.TreatWarningsAsErrors = true;
//param.WarningLevel = 0;
//param.IncludeDebugInformation = true;
return param;
}
/// <summary>
/// Gets the compiler that will be used during the compilation of the scripts.
/// If a custom compiler is used, then the method will check every assembly in memory
/// and find the custom one requested. If none are found, then it will return a new
/// Object of type ICompiler.
/// </summary>
/// <returns></returns>
private Type GetCompiler()
{
Type compiler = typeof(ICompiler);
//Internal CSharpRaw compiler Type specified, so we'll use that.
if ((this.Compiler.ToLower() == "MudCompiler"))
{
compiler = typeof(MudEngine.Scripting.MudScriptCompiler);
return compiler;
}
//Build a collection of available compilers by scanning all the assemblies loaded in memory.
//If any of the assemblies contain a Type that uses the ICompiler interface, we will assume that the
//assembly is a add-on assembly for rScript, adding a new compiler to the CompileEngine.
//Only used if a non-internal compiler is specified
else
{ //Non-internal compiler supplied, so loop through every assembly loaded in memory
foreach (Assembly a in _Assemblies)
{
Boolean isCompiler = false;
//Create an array of all Types within this assembly
Type[] types = a.GetTypes();
//Itterate through each Type; See if any implement the ICompiler interface.
foreach (Type t in a.GetTypes())
{
//If this Type implements ICompiler, then our compiler field needs to reference the Type.
if ((t.GetInterface("ICompiler") != null) && (t.Name.ToLower() == Compiler.ToLower()))
{
//compiler needs to reference this custom compiler Type.
compiler = t;
isCompiler = true;
break;
}
}
//If we found a matching compiler, then exit this loop.
if (isCompiler)
break;
}
}
return compiler;
}
}
}

View file

@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace MudEngine.Scripting
{
public class ScriptFactory
{
//The assembly loaded that will be used.
private List<Assembly> _AssemblyCollection;
#if WINDOWS_PC
/// <summary>
/// Constructor for a Windows PC Script Factory
/// </summary>
/// <param name="assembly"></param>
public ScriptFactory(String assembly)
{
Assembly a;
_AssemblyCollection = new List<Assembly>();
//See if a file exists first with this assembly name.
if (File.Exists(assembly))
{
a = Assembly.Load(assembly);
}
//If not, then try and load it differently
else
{
a = Assembly.Load(new AssemblyName(assembly));
}
if (a == null)
return;
//Add the assembly to our assembly collection.
_AssemblyCollection.Add(a);
}
/// <summary>
/// Alternate Constructor for a Windows PC ScriptFactory
/// </summary>
/// <param name="assembly"></param>
public ScriptFactory(Assembly assembly)
{
_AssemblyCollection = new List<Assembly>();
//Add the supplied assembly to our AssemblyCollection
_AssemblyCollection.Add(assembly);
}
#elif WINDOWS_PHONE
public ScriptFactory()
{
_AssemblyCollection = new List<Assembly>();
foreach(System.Windows.AssemblyPart part in System.Windows.Deployment.Current.Parts)
{
String assemName = part.Source.Replace(".dll", String.Empty);
Assembly a = Assembly.Load(assemName);
_AssemblyCollection.Add(a);
}
}
#endif
/// <summary>
/// Adds another assembly to the factories assembly collection.
/// </summary>
/// <param name="assembly">provides the name of the assembly, or file name that needs to be loaded.</param>
public void AddAssembly(String assembly)
{
Assembly a;
//See if a file exists first with this assembly name.
if (File.Exists(assembly))
{
a = Assembly.Load(new AssemblyName(assembly));
}
//If not, then try and load it differently
else
{
a = Assembly.Load(assembly);
}
//Add the assembly to our assembly collection.
_AssemblyCollection.Add(a);
}
/// <summary>
/// Adds another assembly to the factories assembly collection.
/// </summary>
/// <param name="assembly">Provides a reference to the assembly that will be added to the collection.</param>
public void AddAssembly (Assembly assembly)
{
//Add the supplied assembly to our AssemblyCollection
_AssemblyCollection.Add(assembly);
}
public ScriptObject GetScript(String scriptName)
{
Type script = typeof(Object);
Boolean foundScript = false;
if (_AssemblyCollection.Count == 0)
return new ScriptObject(null);
try
{
foreach (Assembly a in _AssemblyCollection)
{
//The assembly can be null if accessing after a failed compilation.
if (a == null)
continue;
foreach (Type t in a.GetTypes())
{
if (t.Name == scriptName)
{
script = t;
foundScript = true;
break;
}
}
if (foundScript)
break;
}
}
catch
{
throw new Exception("Error encounted during factory instancing of script " + scriptName + ".");
}
ScriptObject obj = new ScriptObject(Activator.CreateInstance(script));
return obj;
}
}
}

View file

@ -0,0 +1,92 @@
using System;
using System.Reflection;
using System.Text;
namespace MudEngine.Scripting
{
public class ScriptObject
{
public Object Instance { get; set; }
public ScriptObject(Object instance)
{
if (instance == null)
Instance = new Object();
else
Instance = instance;
}
~ScriptObject()
{
//TODO: Add ability to call a Shutdown() method within this Instance.
Instance = null;
}
public void SetProperty(String propertyName, object propertyValue)
{
PropertyInfo propertyInfo = Instance.GetType().GetProperty(propertyName);
if (propertyValue is String)
{
if (propertyInfo.PropertyType.Name is String)
{
propertyInfo.SetValue(Instance, propertyValue, null);
}
}
}
public object GetProperty(String propertyName)
{
String[] tokens = propertyName.Split('.');
PropertyInfo previousProperty = Instance.GetType().GetProperty(tokens[0]);
return previousProperty.GetValue(Instance, null);
}
#if WINDOWS_PC
public dynamic GetProperty()
{
return Instance;
}
#endif
public object GetField(String propertyName)
{
String[] tokens = propertyName.Split('.');
FieldInfo previousField = Instance.GetType().GetField(tokens[0]);
return previousField.GetValue(Instance);
}
#if WINDOWS_PC
public dynamic GetField()
{
return Instance;
}
#endif
public Object InvokeMethod(String methodName)
{
return InvokeMethod(methodName, null);
}
public Object InvokeMethod(String methodName, params Object[] parameters)
{
MethodInfo method = Instance.GetType().GetMethod(methodName);
try
{
if (parameters == null || parameters.Length == 0)
return method.Invoke(Instance, null);
else
return method.Invoke(Instance, parameters);
}
catch
{
StringBuilder sb = new StringBuilder();
sb.Append("Error invoking method. Does the method exist?");
return sb.ToString();
}
}
}
}