MudEngine:
- Implemented Scripting Engine; Not fully completed - Migrated PlayerBasic from MudDesigner to MudEngine.
This commit is contained in:
parent
d40b8690d5
commit
35c10eed68
7 changed files with 339 additions and 1 deletions
3
MudEngine/..svnbridge/.svnbridge
Normal file
3
MudEngine/..svnbridge/.svnbridge
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?><ItemProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Properties><Property><Name>svn:ignore</Name><Value>bin
|
||||||
|
obj
|
||||||
|
</Value></Property></Properties></ItemProperties>
|
15
MudEngine/GameObjects/Characters/Controlled/PlayerBasic.cs
Normal file
15
MudEngine/GameObjects/Characters/Controlled/PlayerBasic.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
//Microsoft .NET Framework
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
//MUD Engine
|
||||||
|
using MudEngine.GameObjects.Characters;
|
||||||
|
|
||||||
|
namespace MudEngine.GameObjects.Characters.Controlled
|
||||||
|
{
|
||||||
|
public class PlayerBasic : BaseCharacter
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,7 +39,7 @@ namespace MudEngine.GameObjects.Environment
|
||||||
|
|
||||||
Zone z = new Zone();
|
Zone z = new Zone();
|
||||||
foreach (var zone in filterQuery)
|
foreach (var zone in filterQuery)
|
||||||
return (Zone)z.Load(zone); ;
|
return (Zone)z.Load(zone);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
86
MudEngine/Scripting/GameObject.cs
Normal file
86
MudEngine/Scripting/GameObject.cs
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using BlitScript;
|
||||||
|
|
||||||
|
namespace MudEngine.Scripting
|
||||||
|
{
|
||||||
|
public class GameObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The script instance for this game object
|
||||||
|
/// </summary>
|
||||||
|
public object Instance { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Type name for this object
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determins if this object will recieve Update/Draw calls from the ScriptEngine
|
||||||
|
/// </summary>
|
||||||
|
public bool IsActive { get; set; }
|
||||||
|
|
||||||
|
public object CreateObject()
|
||||||
|
{
|
||||||
|
return Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteObject()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 dynamic SetProperty()
|
||||||
|
{
|
||||||
|
return Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object GetProperty(string propertyName)
|
||||||
|
{
|
||||||
|
string[] tokens = propertyName.Split('.');
|
||||||
|
PropertyInfo previousProperty = Instance.GetType().GetProperty(tokens[0]);
|
||||||
|
|
||||||
|
return previousProperty.GetValue(Instance, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public dynamic GetProperty()
|
||||||
|
{
|
||||||
|
return Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
MudEngine/Scripting/GameObjectCollection.cs
Normal file
30
MudEngine/Scripting/GameObjectCollection.cs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
//Microsoft .NET Framework
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
//MUD Engine
|
||||||
|
using MudEngine.FileSystem;
|
||||||
|
|
||||||
|
namespace MudEngine.Scripting
|
||||||
|
{
|
||||||
|
public class GameObjectCollection
|
||||||
|
{
|
||||||
|
internal List<GameObject> _GameObjects;
|
||||||
|
|
||||||
|
public GameObjectCollection()
|
||||||
|
{
|
||||||
|
_GameObjects = new List<GameObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Save(string filename)
|
||||||
|
{
|
||||||
|
FileManager.Save(filename, _GameObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Load(string filename)
|
||||||
|
{
|
||||||
|
_GameObjects = (List<GameObject>)FileManager.Load(filename, _GameObjects);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
190
MudEngine/Scripting/ScriptEngine.cs
Normal file
190
MudEngine/Scripting/ScriptEngine.cs
Normal file
|
@ -0,0 +1,190 @@
|
||||||
|
//Microsoft .NET Framework
|
||||||
|
using System;
|
||||||
|
using System.CodeDom;
|
||||||
|
using System.CodeDom.Compiler;
|
||||||
|
using Microsoft.CSharp;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
|
||||||
|
namespace MudEngine.Scripting
|
||||||
|
{
|
||||||
|
public class ScriptEngine
|
||||||
|
{
|
||||||
|
public enum ScriptTypes
|
||||||
|
{
|
||||||
|
Assembly,
|
||||||
|
SourceFiles
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Path to the the script files directory
|
||||||
|
/// </summary>
|
||||||
|
public string ScriptPath { get; set; }
|
||||||
|
public string InstallPath { get; private set; }
|
||||||
|
public GameObjectCollection ObjectCollection { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// File Extension for the scripts
|
||||||
|
/// </summary>
|
||||||
|
public string ScriptExtension { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error Messages logged during script compilation
|
||||||
|
/// </summary>
|
||||||
|
public string ErrorMessage
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
string errorMessages = "Script Compilation Failed!\n";
|
||||||
|
//Construct our error message.
|
||||||
|
foreach (string error in _ErrorMessages)
|
||||||
|
errorMessages += error + "\n";
|
||||||
|
|
||||||
|
return errorMessages;
|
||||||
|
}
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
_ErrorMessages = new string[] { value };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScriptTypes _ScriptTypes;
|
||||||
|
private Assembly _ScriptAssembly;
|
||||||
|
private string[] _ErrorMessages;
|
||||||
|
|
||||||
|
public ScriptEngine() : this(ScriptTypes.Assembly)
|
||||||
|
{
|
||||||
|
//Empty constructor. Only here for end-user ease of use. ScriptEngine(Game, ScriptTypes) is called from here.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Instances a new copy of the script engine
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scriptTypes">Tells the engine what kind of scripts will be loaded. Source File or assembly based.</param>
|
||||||
|
public ScriptEngine(ScriptTypes scriptTypes)
|
||||||
|
{
|
||||||
|
//Initialize our engine fields
|
||||||
|
_ScriptTypes = scriptTypes;
|
||||||
|
ScriptExtension = ".cs";
|
||||||
|
|
||||||
|
//Get our current install path
|
||||||
|
ScriptPath = Environment.CurrentDirectory;
|
||||||
|
InstallPath = Environment.CurrentDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compiles a collection of scripts stored in ScriptEngine.ScriptPath. Not supported on XBox360.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool CompileScripts()
|
||||||
|
{
|
||||||
|
//Ensure the script path exists.
|
||||||
|
if (!System.IO.Directory.Exists(ScriptPath))
|
||||||
|
{
|
||||||
|
ErrorMessage = "Invalid Script path supplied.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//Build an array of scripts
|
||||||
|
string[] scripts = System.IO.Directory.GetFiles(ScriptPath, "*" + ScriptExtension, System.IO.SearchOption.AllDirectories);
|
||||||
|
|
||||||
|
//Prepare the compiler.
|
||||||
|
Dictionary<string, string> providerOptions = new Dictionary<string,string>();
|
||||||
|
providerOptions.Add("CompilerVersion", "v3.5");
|
||||||
|
|
||||||
|
CompilerParameters param = new CompilerParameters(new string[] {"mscorlib.dll", "System.dll", "BlitScript.dll"});
|
||||||
|
param.GenerateExecutable = false;
|
||||||
|
param.GenerateInMemory = true;
|
||||||
|
param.OutputAssembly = "Scripts.dll";
|
||||||
|
param.IncludeDebugInformation = false;
|
||||||
|
|
||||||
|
//Compile the scripts with the C# CodeProvider
|
||||||
|
CSharpCodeProvider codeProvider = new CSharpCodeProvider(providerOptions);
|
||||||
|
CompilerResults results = new CompilerResults(new TempFileCollection());
|
||||||
|
results = codeProvider.CompileAssemblyFromFile(param, scripts);
|
||||||
|
|
||||||
|
//if we encountered errors we need to log them to our ErrorMessages property
|
||||||
|
if (results.Errors.Count >= 1)
|
||||||
|
{
|
||||||
|
List<string> errorCollection = new List<string>();
|
||||||
|
foreach (CompilerError error in results.Errors)
|
||||||
|
{
|
||||||
|
string prefix = "Error: ";
|
||||||
|
if (error.IsWarning)
|
||||||
|
prefix = "Warning: ";
|
||||||
|
|
||||||
|
errorCollection.Add(prefix + error.FileName + "(" + error.Line + ") - " + error.ErrorText);
|
||||||
|
_ErrorMessages = errorCollection.ToArray();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes the script engine, loading the compiled scripts into memory
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scriptAssembly"></param>
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
if (_ScriptTypes == ScriptTypes.Assembly)
|
||||||
|
{
|
||||||
|
InitializeAssembly();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
InitializeSourceFiles();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeAssembly()
|
||||||
|
{
|
||||||
|
if (!System.IO.File.Exists("Scripts.dll"))
|
||||||
|
{
|
||||||
|
ErrorMessage = "Failed to load Script Assembly!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ScriptAssembly = Assembly.LoadFile(Path.Combine(InstallPath, "Scripts.dll"));
|
||||||
|
|
||||||
|
foreach (Type type in _ScriptAssembly.GetTypes())
|
||||||
|
{
|
||||||
|
//TODO: Re-implement StartupObject instancing only during Initialize calls.
|
||||||
|
//Remaining scripts should be instanced via a ScriptEngine.LoadObjectList() method.
|
||||||
|
//if (type.BaseType == typeof(StartupObject))
|
||||||
|
//{
|
||||||
|
GameObject gameObject = new GameObject();
|
||||||
|
gameObject.Instance = Activator.CreateInstance(type);
|
||||||
|
gameObject.Name = type.Name;
|
||||||
|
|
||||||
|
ObjectCollection._GameObjects.Add(gameObject);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeSourceFiles()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameObject GetObject(string objectName)
|
||||||
|
{
|
||||||
|
IEnumerable<GameObject> objectQuery =
|
||||||
|
from gameObject in ObjectCollection._GameObjects
|
||||||
|
where gameObject.Name == objectName
|
||||||
|
select gameObject;
|
||||||
|
|
||||||
|
foreach (GameObject gameObject in objectQuery)
|
||||||
|
{
|
||||||
|
if (gameObject.Name == objectName)
|
||||||
|
return gameObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
MudEngine/Scripting/StartupObject.cs
Normal file
14
MudEngine/Scripting/StartupObject.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace MudEngine.Scripting
|
||||||
|
{
|
||||||
|
public class StartupObject
|
||||||
|
{
|
||||||
|
public StartupObject()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue