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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
#if WINDOWS_PC
@ -102,7 +103,7 @@ namespace MudEngine.Scripting
public CompileEngine(String scriptExtensions)
{
_CompileMessages = new String[] { "No compiler messages available." };
_CompileMessages = new[] { "No compiler messages available." };
CompilerOptions = new Dictionary<string, string>();
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.
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;
}
@ -183,7 +184,7 @@ namespace MudEngine.Scripting
//Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
com.CompilerOptions = CompilerOptions;
//Compile the scripts.
Boolean isSuccess = com.Compile(param, scriptRepository);
@ -193,13 +194,7 @@ namespace MudEngine.Scripting
//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();
_CompileMessages = com.Results.Output.Cast<string>().ToArray();
return false;
}
else
@ -224,7 +219,7 @@ namespace MudEngine.Scripting
#if WINDOWS_PC
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;
}
@ -237,7 +232,7 @@ namespace MudEngine.Scripting
//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 + "'." };
_CompileMessages = new[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false;
}
@ -250,7 +245,7 @@ namespace MudEngine.Scripting
//Setup it's properties to match that of our CompileEngine.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
com.CompilerOptions = CompilerOptions;
//Compile the script.
Boolean isSuccess = com.Compile(param, sourceFile);
@ -260,13 +255,7 @@ namespace MudEngine.Scripting
//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();
_CompileMessages = com.Results.Output.Cast<string>().ToArray();
return false;
}
else
@ -298,36 +287,30 @@ namespace MudEngine.Scripting
//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 + "'." };
_CompileMessages = new[] { "Compilation Failed.", "Unable to locate the specified compiler of Type '" + Compiler + "'." };
return false;
}
//Get the compiler parameters.
CompilerParameters param = GetParameters();
var param = GetParameters();
//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.
com.AssemblyReferences = AssemblyReferences;
com.ScriptExtension = ScriptExtension;
com.CompilerOptions = this.CompilerOptions;
com.CompilerOptions = CompilerOptions;
//Compile the scripts.
Boolean isSuccess = com.Compile(param, sourceCode);
var 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();
_CompileMessages = com.Results.Output.Cast<string>().ToArray();
return false;
}
else
@ -372,7 +355,7 @@ namespace MudEngine.Scripting
Type compiler = typeof(ICompiler);
//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);
return compiler;
@ -384,24 +367,20 @@ namespace MudEngine.Scripting
//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)
foreach (var a in _Assemblies)
{
Boolean isCompiler = false;
var isCompiler = false;
//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.
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.
if ((t.GetInterface("ICompiler") != null) && (t.Name.ToLower() == Compiler.ToLower()))
{
//compiler needs to reference this custom compiler Type.
compiler = t;
isCompiler = true;
break;
}
//compiler needs to reference this custom compiler Type.
compiler = t;
isCompiler = true;
break;
}
//If we found a matching compiler, then exit this loop.

View file

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

View file

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

View file

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