muddesigner/MudEngine/Runtime/MudCompiler.cs
Scionwest_cp a365256d53 Changes project wide with this check-in.
MudCompiler:
No longer works.  Needs to be re-wrote to support the new Alpha 2.0 engine

MudDesigenr:
Removed most of the forms since we are not working on it.  Only form left is Project Manager, which will be removed shortly as well.

MudGame:
No longer runs.  All of the source code was removed due to MudEngine Alpha 2.0 source changing drastically.

MudEngine:
Alpha 2.0 source code finally checked-in.  It contains the full re-build of the engine.  A lot of new abstract classes have been added.
2011-10-01 22:20:23 -07:00

124 lines
No EOL
5.1 KiB
C#

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using Microsoft.CSharp;
using MudEngine.Core;
namespace MudEngine.Runtime
{
/// <summary>
/// Standard C# source code compiler.
/// </summary>
internal class MudCompiler : ICompiler
{
/// <summary>
/// The file extension used for the script files.
/// </summary>
public String ScriptExtension { get; set; }
/// <summary>
/// Provides a collection of Assemblies that the compiler will add to its reference list.
/// </summary>
public List<String> AssemblyReferences { get; set; }
/// <summary>
/// The results of the compilation
/// </summary>
public CompilerResults Results { get; set; }
/// <summary>
/// Provides compiling options to various compilers, if they support this feature.
/// </summary>
public Dictionary<String, String> CompilerOptions { get; set; }
/// <summary>
/// Compiles the source files found within the scriptRepository directory matching the ICompiler.ScriptExtension
/// The Compiler defaults to the C# 4.0 compiler if none other is supplied via the ICompiler.CompilerOptions argument.
/// </summary>
/// <param name="param">Compiler Parameters that can be supplied to customize the compilation of the source.</param>
/// <returns>Returns true if the compilation was completed without error.</returns>
public Boolean Compile(CompilerParameters param, String scriptRepository)
{
//Make sure we have a compiler version supplied.
if (!CompilerOptions.ContainsKey("CompilerVersion"))
CompilerOptions.Add("CompilerVersion", "v4.0");
//Instance a reference to the C# code provider, this is what will perform the compiling.
CSharpCodeProvider provider = new CSharpCodeProvider(CompilerOptions);
//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;
else
return true;
}
/// <summary>
/// Compiles the source files found within the scriptFile argument.
/// The Compiler defaults to the C# 4.0 compiler if none other is supplied via the ICompiler.CompilerOptions argument.
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public Boolean Compile(CompilerParameters param, FileInfo scriptFile)
{
//Make sure we have a compiler version supplied.
if (!CompilerOptions.ContainsKey("CompilerVersion"))
CompilerOptions.Add("CompilerVersion", "v4.0");
CSharpCodeProvider provider = new CSharpCodeProvider(CompilerOptions);
//Make sure the file exists prior to attempting to compile it.
if (scriptFile.Exists)
{
//Compile the script and provide the Results property with a referece to the compilation results.
Results = provider.CompileAssemblyFromFile(param, scriptFile.FullName);
}
else
{
Results.Errors.Add(new CompilerError(scriptFile.FullName, 0, 0, "rS01", "The supplied filename does not exist."));
return false;
}
if (Results.Errors.HasErrors)
return false;
else
return true;
}
/// <summary>
/// Compiles the source code found within the scriptSourceCode argument
/// The Compiler defaults to the C# 4.0 compiler if none other is supplied via the ICompiler.CompilerOptions argument.
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public Boolean Compile(CompilerParameters param, String[] scriptSourceCode)
{
if (!CompilerOptions.ContainsKey("CompilerVersion"))
CompilerOptions.Add("CompilerVersion", "v4.0");
CSharpCodeProvider provider = new CSharpCodeProvider(CompilerOptions);
if (scriptSourceCode.Length == 0)
{
Results.Errors.Add(new CompilerError("None", 0, 0, "rS02", "No Source provided."));
return false;
}
else
{
Results = provider.CompileAssemblyFromSource(param, scriptSourceCode);
}
if (Results.Errors.HasErrors)
return false;
else
return true;
}
}
}