Programming a Programming Computer Game – .Net Run Time Type Creator
Programming a Programming Computer Game – .Net Run Time Type Creator
Join the DZone community and get the full member experience.
Join For FreeThe game interface would be a map with a text panel on the right where the user would enter code that the creatures would execute to make their decisions. There were commands for where to move, what to eat, when to breed, etc. This was also a really nice space from which to play with algorithms like neural nets, evolutionary algorithms, clustering, A*, etc. We played around with it a bit and had a fair amount of fun, but we eventually realized that even for us who had built it, the game was too complicated for anyone to actually play. At least not in real time. So we abandoned it as a fun experiment.
RunTimeTypeCreator
public static T CreateType(string source,
IEnumerable<string> assemblies,
out List compilationErrors)
where T : class
It will attempt to create an instance of the type T from the source passed in. The source will be compiled with references to all the assemblies in the assemblies parameter. So for example you could do this with it.
namespace RunTimeTypeCreator.Tests
{
public class RunTimeTypeCreatorTests
{
public static bool TestVariable = false;
public void Example()
{
const string source = @"
using RunTimeTypeCreator.Tests;
public class TestTypeClass : RunTimeTypeCreatorTests.ITestType
{
public bool Success { get { return RunTimeTypeCreatorTests.TestVariable; } }
}";
List<string> compilationErrors;
var type = RunTimeTypeCreator.CreateType<ITestType>(source,
new[] { "RunTimeTypeCreator.Tests.dll" }, //the name of this assembly
out compilationErrors);
TestVariable = false;
//will print false
Console.WriteLine(type.Success)
TestVariable = true;
//will print true
Console.WriteLine(type.Success)
}
public interface ITestType
{
bool Success { get; }
}
}
}
Which is kind a cool I think. Here's a quick run through of how it works, this just shows the code minus bits of validation and error reporting, so if you want the full thing I would recommend getting it from github
var csc = new CSharpCodeProvider();
var parameters = new CompilerParameters
{
//we don't want a physical executable in this case
GenerateExecutable = false,
//this is what allows it to access variables in our domain
GenerateInMemory = true
};
//add all the assmeblies we care about
foreach (var assembly in assemblies)
parameters.ReferencedAssemblies.Add(assembly);
//compile away, will load the class into memory
var result = csc.CompileAssemblyFromSource(parameters, source);
//we compiled succesfully so now just use reflection to get the type we want
var types = result.CompiledAssembly.GetTypes()
.Where(x => typeof(T).IsAssignableFrom(x)).ToList();
var parameterlessConstructor = types.First().GetConstructor(new Type[] { });
//create the type and return
return (T)Activator.CreateInstance(types.First());
Full code is available here on github. I also had some other code around validating what the user was doing, Making sure they weren't trying to access the file system, open ports or creating memory leaks/recursive loops. I'll try and clean this up and post it at a future date.
Published at DZone with permission of Daniel Slater . See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}