Removed some this's (this.) and converted some Foreach statements to LINQ statements.

This commit is contained in:
darxval_cp 2012-05-31 18:55:23 -07:00
parent c3c91250e2
commit bec840f5e4
4 changed files with 40 additions and 77 deletions

View file

@ -2,6 +2,7 @@
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
#if WINDOWS_PC #if WINDOWS_PC
@ -102,7 +103,7 @@ namespace MudEngine.Scripting
public CompileEngine(String scriptExtensions) public CompileEngine(String scriptExtensions)
{ {
_CompileMessages = new String[] { "No compiler messages available." }; _CompileMessages = new[] { "No compiler messages available." };
CompilerOptions = new Dictionary<string, string>(); CompilerOptions = new Dictionary<string, string>();
Compiler = "C#"; Compiler = "C#";
@ -170,7 +171,7 @@ namespace MudEngine.Scripting
//Incase a non-default compiler was specified and we could not find it in memory, fail. //Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler") if (compiler.Name == "ICompiler")
{ {
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." }; _CompileMessages = new[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false; return false;
} }
@ -183,7 +184,7 @@ namespace MudEngine.Scripting
//Setup it's properties to match that of our CompileEngine. //Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences; com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension; com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions; com.CompilerOptions = CompilerOptions;
//Compile the scripts. //Compile the scripts.
Boolean isSuccess = com.Compile(param, scriptRepository); Boolean isSuccess = com.Compile(param, scriptRepository);
@ -193,13 +194,7 @@ namespace MudEngine.Scripting
//into our _CompileMessages string. //into our _CompileMessages string.
if (!isSuccess) if (!isSuccess)
{ {
List<String> compilerMessages = new List<string>(); _CompileMessages = com.Results.Output.Cast<string>().ToArray();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false; return false;
} }
else else
@ -224,7 +219,7 @@ namespace MudEngine.Scripting
#if WINDOWS_PC #if WINDOWS_PC
if (!sourceFile.Exists) if (!sourceFile.Exists)
{ {
this._CompileMessages = new String[] { "Error: File " + sourceFile.FullName + " does not exists." }; _CompileMessages = new[] { "Error: File " + sourceFile.FullName + " does not exists." };
return false; return false;
} }
@ -237,7 +232,7 @@ namespace MudEngine.Scripting
//Incase a non-default compiler was specified and we could not find it in memory, fail. //Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler") if (compiler.Name == "ICompiler")
{ {
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." }; _CompileMessages = new[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false; return false;
} }
@ -250,7 +245,7 @@ namespace MudEngine.Scripting
//Setup it's properties to match that of our CompileEngine. //Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences; com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension; com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions; com.CompilerOptions = CompilerOptions;
//Compile the script. //Compile the script.
Boolean isSuccess = com.Compile(param, sourceFile); Boolean isSuccess = com.Compile(param, sourceFile);
@ -260,13 +255,7 @@ namespace MudEngine.Scripting
//into our _CompileMessages string. //into our _CompileMessages string.
if (!isSuccess) if (!isSuccess)
{ {
List<String> compilerMessages = new List<string>(); _CompileMessages = com.Results.Output.Cast<string>().ToArray();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false; return false;
} }
else else
@ -298,36 +287,30 @@ namespace MudEngine.Scripting
//Incase a non-default compiler was specified and we could not find it in memory, fail. //Incase a non-default compiler was specified and we could not find it in memory, fail.
if (compiler.Name == "ICompiler") if (compiler.Name == "ICompiler")
{ {
this._CompileMessages = new string[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." }; _CompileMessages = new[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false; return false;
} }
//Get the compiler parameters. //Get the compiler parameters.
CompilerParameters param = GetParameters(); var param = GetParameters();
//Create a Instance of the compiler, either custom or internal. //Create a Instance of the compiler, either custom or internal.
ICompiler com = (ICompiler)Activator.CreateInstance(compiler); var com = (ICompiler)Activator.CreateInstance(compiler);
//Setup it's properties to match that of our CompileEngine. //Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences; com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension; com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions; com.CompilerOptions = CompilerOptions;
//Compile the scripts. //Compile the scripts.
Boolean isSuccess = com.Compile(param, sourceCode); var isSuccess = com.Compile(param, sourceCode);
HasErrors = !isSuccess; HasErrors = !isSuccess;
//If the compilation failed, store all of the compiler errors //If the compilation failed, store all of the compiler errors
//into our _CompileMessages string. //into our _CompileMessages string.
if (!isSuccess) if (!isSuccess)
{ {
List<String> compilerMessages = new List<string>(); _CompileMessages = com.Results.Output.Cast<string>().ToArray();
foreach (String message in com.Results.Output)
{
compilerMessages.Add(message);
}
_CompileMessages = compilerMessages.ToArray();
return false; return false;
} }
else else
@ -372,7 +355,7 @@ namespace MudEngine.Scripting
Type compiler = typeof(ICompiler); Type compiler = typeof(ICompiler);
//Internal CSharpRaw compiler Type specified, so we'll use that. //Internal CSharpRaw compiler Type specified, so we'll use that.
if ((this.Compiler.ToLower() == "c#") || (this.Compiler.ToLower() == "csharp")) if ((Compiler.ToLower() == "c#") || (Compiler.ToLower() == "csharp"))
{ {
compiler = typeof(CSharp); compiler = typeof(CSharp);
return compiler; return compiler;
@ -384,24 +367,20 @@ namespace MudEngine.Scripting
//Only used if a non-internal compiler is specified //Only used if a non-internal compiler is specified
else else
{ //Non-internal compiler supplied, so loop through every assembly loaded in memory { //Non-internal compiler supplied, so loop through every assembly loaded in memory
foreach (Assembly a in _Assemblies) foreach (var a in _Assemblies)
{ {
Boolean isCompiler = false; var isCompiler = false;
//Create an array of all Types within this assembly //Create an array of all Types within this assembly
Type[] types = a.GetTypes(); var types = a.GetTypes();
//Itterate through each Type; See if any implement the ICompiler interface. //Itterate through each Type; See if any implement the ICompiler interface.
foreach (Type t in a.GetTypes()) foreach (var t in a.GetTypes().Where(t => (t.GetInterface("ICompiler") != null) && (t.Name.ToLower() == Compiler.ToLower())))
{ {
//If this Type implements ICompiler, then our compiler field needs to reference the Type. //compiler needs to reference this custom compiler Type.
if ((t.GetInterface("ICompiler") != null) && (t.Name.ToLower() == Compiler.ToLower())) compiler = t;
{ isCompiler = true;
//compiler needs to reference this custom compiler Type. break;
compiler = t;
isCompiler = true;
break;
}
} }
//If we found a matching compiler, then exit this loop. //If we found a matching compiler, then exit this loop.

View file

@ -12,24 +12,24 @@ namespace MudEngine.Scripting
{ {
public ScriptEnumerator(ObjectCollection objectManager) public ScriptEnumerator(ObjectCollection objectManager)
{ {
this._ObjectCollection = objectManager; _ObjectCollection = objectManager;
} }
public void Reset() public void Reset()
{ {
this._currentIndex = -1; _currentIndex = -1;
} }
public BaseScript Current public BaseScript Current
{ {
get get
{ {
if (this._currentIndex < 0) if (_currentIndex < 0)
return null; return null;
else if (this._currentIndex > this._ObjectCollection.Length) else if (_currentIndex > _ObjectCollection.Length)
return null; return null;
else else
return this._ObjectCollection[this._currentIndex]; return _ObjectCollection[_currentIndex];
} }
} }

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using MudEngine.Core; using MudEngine.Core;
@ -27,15 +28,7 @@ namespace MudEngine.Scripting
_AssemblyCollection = new List<Assembly>(); _AssemblyCollection = new List<Assembly>();
//See if a file exists first with this assembly name. //See if a file exists first with this assembly name.
if (File.Exists(assembly)) a = File.Exists(assembly) ? Assembly.Load(new AssemblyName(assembly)) : Assembly.Load(assembly);
{
a = Assembly.Load(new AssemblyName(assembly));
}
//If not, then try and load it differently
else
{
a = Assembly.Load(assembly);
}
if (a == null) if (a == null)
return; return;
@ -54,7 +47,7 @@ namespace MudEngine.Scripting
//Add the supplied assembly to our AssemblyCollection //Add the supplied assembly to our AssemblyCollection
_AssemblyCollection.Add(assembly); _AssemblyCollection.Add(assembly);
this.Assembly = assembly; Assembly = assembly;
} }
#endif #endif
/// <summary> /// <summary>
@ -142,24 +135,18 @@ namespace MudEngine.Scripting
Type script = typeof(BaseScript); Type script = typeof(BaseScript);
Boolean foundScript = false; Boolean foundScript = false;
if (this._AssemblyCollection.Count == 0) if (_AssemblyCollection.Count == 0)
return null; return null;
try try
{ {
foreach (Assembly a in _AssemblyCollection) foreach (var a in _AssemblyCollection.Where(a => a != null))
{ {
if (a == null) foreach (var t in a.GetTypes().Where(t => t.BaseType.Name == baseScript))
continue;
foreach (Type t in a.GetTypes())
{ {
if (t.BaseType.Name == baseScript) script = t;
{ foundScript = true;
script = t; break;
foundScript = true;
break;
}
} }
if (foundScript) if (foundScript)

View file

@ -10,10 +10,7 @@ namespace sEngine.Scripting
public ScriptObject(Object instance) public ScriptObject(Object instance)
{ {
if (instance == null) Instance = instance ?? new Object();
Instance = new Object();
else
Instance = instance;
} }
~ScriptObject() ~ScriptObject()
@ -83,7 +80,7 @@ namespace sEngine.Scripting
} }
catch catch
{ {
StringBuilder sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append("Error invoking method. Does the method exist?"); sb.Append("Error invoking method. Does the method exist?");
return sb.ToString(); return sb.ToString();
} }