MudCompiler:
- Removed External Script compilation support for now. I'll add it back once I provide SourceFile and SourceCode compiling support to the MudScriptCompiler. At the moment only Whole Directory compiling is supported. MudDesigner: - Removed all of the source code, with the exception of the designer generated source, from frmProjectManager. It will need to be re-wrote due to the removal of the MudScriptEngine. MudEngine: - Deleted Scripting.GameObject - Deleted Scripting.GameObjectCollection - Deleted Scripting.ScriptEngine - Deleted classes were replaced by the rScript engine. Only class needed now is the MudScriptCompiler, which handles all of the custom MudEngine script compiling, using the rScript Engine. - Removed old Scripting.ScriptEngine references from within GameManagement.Game - GameManagement.Game no longer checks to see if MudEngine.dll exists. If it didn't exist, the engine wouldn't be running to perform that check in the first place. - GameManagement.Game no longer adds MudEngine.dll as a referenced assembly. The MudScriptCompiler handles that during compilation. - MudScriptCompiler.Compile() always returns false when SourceFile or SourceCode is passed as an argument. Only Script Directories can be compiled at this time. MudGame: - Removed references to Scripting.ScriptEngine from MudGame.Program - Re-wrote how scripted Type's that inherit and replace MudEngine.GameManagement.Game. Scripts are compiled prior to Game.Start() being invoked, allowing GameManagement.Game to be replaced with an inherited class from a compiled script. TODO: Look at a way to prevent Game.Start() from compiling the scripts again, as they have already been compiled once. It's not a big hit on startup time, but it needs to be wrote the proper way.
This commit is contained in:
parent
7a0d1c5a74
commit
3f73247d0e
10 changed files with 38 additions and 689 deletions
|
@ -18,7 +18,8 @@ namespace MUDCompiler
|
|||
Console.WriteLine();
|
||||
Console.WriteLine("1): Compile Scripts");
|
||||
Console.WriteLine("2): Exit Compiler");
|
||||
Console.Write("Enter Selection: ");
|
||||
//Console.Write("Enter Selection: ");
|
||||
Console.WriteLine("Out of engine compiling is currently not supported.");
|
||||
|
||||
String command = Console.ReadLine();
|
||||
|
||||
|
@ -46,21 +47,6 @@ namespace MUDCompiler
|
|||
|
||||
static void CompileScripts()
|
||||
{
|
||||
MudEngine.GameManagement.Game game = new MudEngine.GameManagement.Game();
|
||||
ScriptEngine se = new ScriptEngine(game, ScriptEngine.ScriptTypes.SourceFiles);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Compiling...");
|
||||
|
||||
se.Initialize();
|
||||
se.ScriptExtension = ".mud";
|
||||
se.ScriptPath = "Scripts";
|
||||
if (se.CompileScripts())
|
||||
Console.WriteLine("Compiling completed without error.");
|
||||
else
|
||||
Console.WriteLine(se.ErrorMessage);
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.Read();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,144 +17,23 @@ namespace MudDesigner
|
|||
{
|
||||
public partial class frmProjectManager : Form
|
||||
{
|
||||
String[] _ProjectFiles;
|
||||
String _ProjectPath;
|
||||
String _ScriptPath;
|
||||
const String SettingsFile = "Settings.ini";
|
||||
|
||||
dynamic _Game;
|
||||
ScriptEngine _ScriptEngine;
|
||||
Client client;
|
||||
Thread r;
|
||||
|
||||
public frmProjectManager()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_ProjectPath = Path.Combine(Environment.CurrentDirectory, "Projects");
|
||||
_ScriptPath = Path.Combine(Environment.CurrentDirectory, "Scripts");
|
||||
|
||||
if (!Directory.Exists(_ProjectPath))
|
||||
Directory.CreateDirectory(_ProjectPath);
|
||||
|
||||
if (!Directory.Exists(_ScriptPath))
|
||||
Directory.CreateDirectory(_ScriptPath);
|
||||
|
||||
if (!File.Exists(SettingsFile))
|
||||
{
|
||||
Log.Write("Settings.ini missing!", false);
|
||||
FileManager.WriteLine(SettingsFile, "Scripts", "ScriptPath");
|
||||
FileManager.WriteLine(SettingsFile, ".cs", "ScriptExtension");
|
||||
FileManager.WriteLine(SettingsFile, "True", "ServerEnabled");
|
||||
}
|
||||
|
||||
_ScriptEngine = new ScriptEngine(new Game(), ScriptEngine.ScriptTypes.Both);
|
||||
_ScriptEngine.Initialize();
|
||||
|
||||
GameObject go = _ScriptEngine.GetObject("Game");
|
||||
|
||||
if (go == null)
|
||||
{
|
||||
_Game = new Game();
|
||||
go = new GameObject(_Game, "Game");
|
||||
_ScriptEngine = new ScriptEngine(_Game, ScriptEngine.ScriptTypes.Both);
|
||||
}
|
||||
else
|
||||
{
|
||||
_Game = (Game)go.Instance;
|
||||
_ScriptEngine = new ScriptEngine(_Game, ScriptEngine.ScriptTypes.Both);
|
||||
}
|
||||
|
||||
//TODO: Do I need to Re-initialize _ScriptEngine?
|
||||
|
||||
RefreshProjects();
|
||||
|
||||
client = new Client();
|
||||
client.Initialize("localhost", 555);
|
||||
|
||||
comServerType.Items.Add("Local Server");
|
||||
comServerType.Items.Add("Test Server");
|
||||
comServerType.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
private void RefreshProjects()
|
||||
{
|
||||
_ProjectFiles = Directory.GetFiles(Environment.CurrentDirectory, "*.ini");
|
||||
|
||||
foreach (String filename in _ProjectFiles)
|
||||
{
|
||||
if (Path.GetFileNameWithoutExtension(filename).ToLower() == "settings")
|
||||
continue;
|
||||
|
||||
_Game.Load(filename);
|
||||
|
||||
lstProjects.Items.Add(_Game.GameTitle);
|
||||
}
|
||||
}
|
||||
|
||||
private void btnNewProject_Click(object sender, EventArgs e)
|
||||
{
|
||||
frmInputBox input = new frmInputBox("Enter the name of your project.");
|
||||
input.ShowDialog();
|
||||
|
||||
if (input.IsCancel)
|
||||
return;
|
||||
else if (String.IsNullOrEmpty(input.Input))
|
||||
return;
|
||||
|
||||
lstProjects.Items.Add(input.Input);
|
||||
|
||||
_Game.GameTitle = input.Input;
|
||||
//Setup save data paths.
|
||||
_Game.DataPaths = new SaveDataPaths(Path.Combine("Projects", _Game.GameTitle, _Game.DataPaths.Environment), Path.Combine("Projects", _Game.GameTitle, _Game.DataPaths.Players), Path.Combine("Projects", _Game.GameTitle, _Game.DataPath.Scripts));
|
||||
|
||||
_Game.Save();
|
||||
|
||||
input = null;
|
||||
|
||||
ShowDesigner();
|
||||
}
|
||||
|
||||
private void ShowDesigner()
|
||||
{
|
||||
frmDesigner form = new frmDesigner(_Game, client);
|
||||
|
||||
form.Show();
|
||||
this.Hide();
|
||||
|
||||
if (comServerType.SelectedItem.ToString() == "Test Server")
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
frmInputBox input = new frmInputBox("Enter the Port that your local server is currently running on.");
|
||||
|
||||
input.ShowDialog();
|
||||
|
||||
if (input.IsCancel)
|
||||
return;
|
||||
|
||||
client.Initialize("localhost", Convert.ToInt32(input.Input));
|
||||
|
||||
if (!client.Connect() || !client.Send("hello", false)) // test send + client data
|
||||
{
|
||||
MessageBox.Show("Failed to connect to a local server. Is the server running?", "Mud Designer");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
while (form.Visible)
|
||||
{
|
||||
Application.DoEvents();
|
||||
}
|
||||
|
||||
//Refresh the project list incase the project was renamed.
|
||||
lstProjects.Items.Clear();
|
||||
|
||||
RefreshProjects();
|
||||
|
||||
this.Show();
|
||||
form = null;
|
||||
}
|
||||
|
||||
private void btnClose_Click(object sender, EventArgs e)
|
||||
|
@ -164,10 +43,6 @@ namespace MudDesigner
|
|||
|
||||
private void editProjectToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (File.Exists(lstProjects.SelectedItem.ToString() + ".ini"))
|
||||
_Game.Load(lstProjects.SelectedItem.ToString() + ".ini");
|
||||
|
||||
ShowDesigner();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,12 +53,6 @@ namespace MudEngine.GameManagement
|
|||
[Browsable(false)]
|
||||
public SaveDataPaths DataPaths { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scripting engine used by the game.
|
||||
/// </summary>
|
||||
[Browsable(false)]
|
||||
public ScriptEngine scriptEngine { get; internal set; }
|
||||
|
||||
[Browsable(false)]
|
||||
public rScripting.CompileEngine Scripting { get; internal set; }
|
||||
|
||||
|
@ -201,7 +195,6 @@ namespace MudEngine.GameManagement
|
|||
{
|
||||
//Instance all of the Games Objects.
|
||||
CurrencyList = new List<Currency>();
|
||||
scriptEngine = new Scripting.ScriptEngine(this); //TODO - Remove
|
||||
Scripting = new rScripting.CompileEngine(".cs");
|
||||
World = new GameWorld(this);
|
||||
WorldTime = new GameTime(this);
|
||||
|
@ -246,17 +239,9 @@ namespace MudEngine.GameManagement
|
|||
if (!Directory.Exists(DataPaths.Players))
|
||||
Directory.CreateDirectory(DataPaths.Players);
|
||||
|
||||
//Load both pre-compiled and file based scripts - TODO - Remove
|
||||
//scriptEngine.ScriptType = ScriptEngine.ScriptTypes.Both;
|
||||
//scriptEngine.Initialize();
|
||||
|
||||
//Instance the new scripting engine
|
||||
Scripting.Compiler = "MudScriptCompiler";
|
||||
if (!System.IO.File.Exists("MudEngine.dll"))
|
||||
Log.Write("CRITICAL ERROR: Un-able to locate MudEngine.dll");
|
||||
|
||||
Scripting.AddAssemblyReference("MudEngine.dll");
|
||||
|
||||
//Check for compiler errors after script compiling completes.
|
||||
if (!Scripting.Compile(DataPaths.Scripts))
|
||||
{
|
||||
Log.Write("CRITICAL ERROR: Game Script Repository failed to compile!");
|
||||
|
|
|
@ -86,9 +86,6 @@
|
|||
<Compile Include="Networking\Server.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scripting\MudScriptCompiler.cs" />
|
||||
<Compile Include="Scripting\GameObject.cs" />
|
||||
<Compile Include="Scripting\GameObjectCollection.cs" />
|
||||
<Compile Include="Scripting\ScriptEngine.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using MudEngine.Scripting;
|
||||
|
||||
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 Boolean IsActive { get; set; }
|
||||
|
||||
public GameObject(object instance, String name)
|
||||
{
|
||||
Instance = instance;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public object CreateObject()
|
||||
{
|
||||
return Instance;
|
||||
}
|
||||
|
||||
public Boolean 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
//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>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -60,6 +60,7 @@ namespace MudEngine.Scripting
|
|||
//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);
|
||||
|
||||
|
@ -118,7 +119,7 @@ namespace MudEngine.Scripting
|
|||
String[] ConvertedScripts = Directory.GetFiles("temp", "*" + this.ScriptExtension, SearchOption.AllDirectories);
|
||||
|
||||
//Compile the scripts and provide the Results property with a reference to the compilation results.
|
||||
param.GenerateInMemory = false;
|
||||
param.ReferencedAssemblies.Add("MudEngine.dll");
|
||||
Results = provider.CompileAssemblyFromFile(param, ConvertedScripts);
|
||||
System.IO.Directory.Delete(modifiedScriptsPath, true);
|
||||
|
||||
|
@ -137,6 +138,9 @@ namespace MudEngine.Scripting
|
|||
/// <returns></returns>
|
||||
public Boolean Compile(CompilerParameters param, FileInfo scriptFile)
|
||||
{
|
||||
//TODO: Add single-file compilation support
|
||||
return false; //Single file compiling not implemented
|
||||
|
||||
//Make sure we have a compiler version supplied.
|
||||
if (!CompilerOptions.ContainsKey("CompilerVersion"))
|
||||
CompilerOptions.Add("CompilerVersion", "v4.0");
|
||||
|
@ -169,6 +173,9 @@ namespace MudEngine.Scripting
|
|||
/// <returns></returns>
|
||||
public Boolean Compile(CompilerParameters param, String[] scriptSourceCode)
|
||||
{
|
||||
//Source Code compiling not implemented.
|
||||
return false; //TODO: Add source code compiling support.
|
||||
|
||||
if (!CompilerOptions.ContainsKey("CompilerVersion"))
|
||||
CompilerOptions.Add("CompilerVersion", "v4.0");
|
||||
|
||||
|
|
|
@ -1,380 +0,0 @@
|
|||
//Microsoft .NET Framework
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.CodeDom.Compiler;
|
||||
#if !MOBILE
|
||||
using Microsoft.CSharp;
|
||||
#endif
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
using MudEngine.FileSystem;
|
||||
using MudEngine.GameObjects;
|
||||
using MudEngine.GameObjects.Characters;
|
||||
using MudEngine.GameManagement;
|
||||
|
||||
namespace MudEngine.Scripting
|
||||
{
|
||||
public class ScriptEngine
|
||||
{
|
||||
public enum ScriptTypes
|
||||
{
|
||||
Assembly,
|
||||
SourceFiles,
|
||||
Both
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Path to the the script files directory
|
||||
/// </summary>
|
||||
public String ScriptPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ScriptPath;
|
||||
}
|
||||
set
|
||||
{
|
||||
_ScriptPath = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Root), value);
|
||||
}
|
||||
}
|
||||
String _ScriptPath;
|
||||
|
||||
public String InstallPath { get; private set; }
|
||||
public GameObjectCollection ObjectCollection { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Collection of currently loaded objects created from compiled scripts
|
||||
/// </summary>
|
||||
public List<GameObject> GameObjects { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Collection of currently loaded game commecnts that can be used. These must be compiled scripts inheriting from IGameCommand
|
||||
/// </summary>
|
||||
public List<IGameCommand> GameCommands { 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 };
|
||||
}
|
||||
}
|
||||
|
||||
internal ScriptTypes ScriptType { get; set; }
|
||||
private Assembly _ScriptAssembly;
|
||||
private List<Assembly> _AssemblyCollection;
|
||||
private String[] _ErrorMessages;
|
||||
private String _SettingsFile;
|
||||
Game _Game;
|
||||
|
||||
public ScriptEngine(Game game) : this(game, ScriptTypes.Assembly)
|
||||
{
|
||||
_Game = game;
|
||||
}
|
||||
|
||||
|
||||
/// <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(Game game, ScriptTypes scriptTypes)
|
||||
{
|
||||
//Initialize our engine fields
|
||||
_SettingsFile = "Settings.ini";
|
||||
|
||||
ScriptExtension = FileManager.GetData(_SettingsFile, "ScriptExtension");
|
||||
if (String.IsNullOrEmpty(ScriptExtension))
|
||||
ScriptExtension = ".cs";
|
||||
|
||||
//Get our current install path
|
||||
ScriptPath = FileManager.GetData(_SettingsFile, "ScriptPath");
|
||||
if (String.IsNullOrEmpty(ScriptPath))
|
||||
ScriptPath = "Scripts";
|
||||
|
||||
InstallPath = Environment.CurrentDirectory;
|
||||
GameObjects = new List<GameObject>();
|
||||
_AssemblyCollection = new List<Assembly>();
|
||||
ObjectCollection = new GameObjectCollection();
|
||||
ScriptType = scriptTypes;
|
||||
|
||||
_Game = game;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compiles a collection of scripts stored in ScriptEngine.ScriptPath. Not supported on XBox360.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Boolean CompileScripts()
|
||||
{
|
||||
#if !MOBILE
|
||||
//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 scripts. MUD Scripts are wrote without defining a namespace
|
||||
if (Directory.Exists("temp"))
|
||||
Directory.Delete("temp", true);
|
||||
|
||||
Directory.CreateDirectory("temp");
|
||||
|
||||
//Setup the additional sourcecode that's needed in the script.
|
||||
String[] usingStatements = new String[]
|
||||
{
|
||||
"using System;",
|
||||
"using System.Collections.Generic;",
|
||||
"using System.Text;",
|
||||
"using System.Linq;",
|
||||
"using MudEngine.GameObjects;",
|
||||
"using MudEngine.GameObjects.Characters;",
|
||||
"using MudEngine.GameObjects.Environment;",
|
||||
"using MudEngine.GameObjects.Items;",
|
||||
"using MudEngine.GameManagement;",
|
||||
"using MudEngine.FileSystem;",
|
||||
"using MudEngine.Scripting;"
|
||||
};
|
||||
|
||||
foreach (String script in scripts)
|
||||
{
|
||||
String tempPath = "temp";
|
||||
String source = "\nnamespace MudScripts{\n}";
|
||||
|
||||
FileStream fr = new FileStream(script, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||
FileStream fw = new FileStream(Path.Combine(tempPath, Path.GetFileName(script)), FileMode.Create, FileAccess.Write);
|
||||
StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Default);
|
||||
StreamReader sr = new StreamReader(fr, System.Text.Encoding.Default);
|
||||
|
||||
String content = sr.ReadToEnd();
|
||||
foreach (String statement in usingStatements)
|
||||
source = source.Insert(0, statement);
|
||||
|
||||
source = source.Insert(source.Length - 1, content);
|
||||
sw.Write(source);
|
||||
sr.Close();
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
}
|
||||
String oldPath = ScriptPath;
|
||||
ScriptPath = "temp";
|
||||
|
||||
//Prepare the compiler.
|
||||
Dictionary<String, String> providerOptions = new Dictionary<String,String>();
|
||||
providerOptions.Add("CompilerVersion", "v4.0");
|
||||
|
||||
String[] referencedAssemblies = new String[]
|
||||
{
|
||||
"mscorlib.dll",
|
||||
"System.dll",
|
||||
"System.Core.dll",
|
||||
"MudEngine.dll"
|
||||
};
|
||||
|
||||
CompilerParameters param = new CompilerParameters(referencedAssemblies);
|
||||
param.GenerateExecutable = false;
|
||||
param.GenerateInMemory = true;
|
||||
if (!param.GenerateInMemory)
|
||||
param.OutputAssembly = Path.Combine(oldPath, "Scripts.dll");
|
||||
|
||||
param.IncludeDebugInformation = false;
|
||||
param.TreatWarningsAsErrors = true;
|
||||
|
||||
//Compile the scripts with the C# CodeProvider
|
||||
CSharpCodeProvider codeProvider = new CSharpCodeProvider(providerOptions);
|
||||
//codeProvider.LanguageOptions = LanguageOptions.CaseInsensitive;
|
||||
CompilerResults results = new CompilerResults(new TempFileCollection());
|
||||
scripts = Directory.GetFiles(ScriptPath, "*" + ScriptExtension, SearchOption.AllDirectories);
|
||||
results = codeProvider.CompileAssemblyFromFile(param, scripts);
|
||||
|
||||
//Delete the temp folder
|
||||
Directory.Delete("temp", true);
|
||||
ScriptPath = oldPath;
|
||||
|
||||
//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
|
||||
{
|
||||
_ScriptAssembly = results.CompiledAssembly;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the script engine, loading the compiled scripts into memory
|
||||
/// </summary>
|
||||
/// <param name="scriptAssembly"></param>
|
||||
public void Initialize()
|
||||
{
|
||||
if ((ScriptType == ScriptTypes.Assembly) || (ScriptType == ScriptTypes.Both))
|
||||
{
|
||||
Log.Write("Loading Assembly based Scripts...");
|
||||
InitializeAssembly();
|
||||
}
|
||||
|
||||
if ((ScriptType == ScriptTypes.SourceFiles) || (ScriptType == ScriptTypes.Both))
|
||||
{
|
||||
Log.Write("Loading Source File based Scripts...");
|
||||
InitializeSourceFiles();
|
||||
}
|
||||
|
||||
foreach (Assembly assembly in _AssemblyCollection)
|
||||
{
|
||||
Log.Write("Checking " + Path.GetFileName(assembly.Location) + " for scripts...");
|
||||
|
||||
foreach (Type t in assembly.GetTypes())
|
||||
{
|
||||
if (t.BaseType == null)
|
||||
continue;
|
||||
if (t.BaseType.Name == "BaseObject")
|
||||
{
|
||||
GameObjects.Add(new GameObject(Activator.CreateInstance(t, new object[] {_Game}), t.Name));
|
||||
Log.Write(t.Name + " script loaded.");
|
||||
continue;
|
||||
}
|
||||
else if (t.BaseType.Name == "BaseCharacter")
|
||||
{
|
||||
GameObject obj = new GameObject(Activator.CreateInstance(t, new object[] {_Game}), t.Name);
|
||||
GameObjects.Add(obj);
|
||||
//obj.GetProperty().CurrentRoom = _Game.InitialRealm.InitialZone.InitialRoom;
|
||||
Log.Write(t.Name + " script loaded.");
|
||||
continue;
|
||||
}
|
||||
else if (t.BaseType.Name == "Game")
|
||||
{
|
||||
GameObject obj = new GameObject(Activator.CreateInstance(t, null), t.Name);
|
||||
GameObjects.Add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
//Lastly, send this assembly off to the CommandEngine so we can load commands from it for use as well.
|
||||
CommandEngine.LoadCommandLibrary(assembly);
|
||||
}
|
||||
_AssemblyCollection.Clear();
|
||||
}
|
||||
|
||||
private void InitializeAssembly()
|
||||
{
|
||||
if (!Directory.Exists(ScriptPath))
|
||||
{
|
||||
Log.Write("Supplied script path does not exist! No scripts loaded.");
|
||||
return;
|
||||
}
|
||||
String[] libraries = Directory.GetFiles(ScriptPath, "*.dll", SearchOption.AllDirectories);
|
||||
|
||||
if (libraries.Length == 0)
|
||||
{
|
||||
Log.Write("No possible script libraries found.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (String library in libraries)
|
||||
{
|
||||
Boolean isOK = true;
|
||||
|
||||
foreach (Assembly assembly in _AssemblyCollection)
|
||||
{
|
||||
if (assembly.ManifestModule.ScopeName == Path.GetFileName(library))
|
||||
{
|
||||
isOK = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isOK)
|
||||
continue;
|
||||
|
||||
Log.Write("Found possible script libary: " + Path.GetFileName(library));
|
||||
_AssemblyCollection.Add(Assembly.LoadFile(library));
|
||||
}
|
||||
_AssemblyCollection.Add(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
|
||||
private void InitializeSourceFiles()
|
||||
{
|
||||
if (!Directory.Exists(ScriptPath))
|
||||
Directory.CreateDirectory(ScriptPath);
|
||||
|
||||
String[] scripts = Directory.GetFiles(ScriptPath, "*.cs", SearchOption.AllDirectories);
|
||||
|
||||
if (scripts.Length == 0)
|
||||
{
|
||||
Log.Write("No un-compiled scripts located!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CompileScripts())
|
||||
{
|
||||
Log.Write("Error Compiling Scripts:");
|
||||
foreach (String error in _ErrorMessages)
|
||||
{
|
||||
Log.Write("Error: " + error);
|
||||
}
|
||||
}
|
||||
else
|
||||
_AssemblyCollection.Add(_ScriptAssembly);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public GameObject GetObjectOf(String baseTypeName)
|
||||
{
|
||||
foreach (GameObject obj in GameObjects)
|
||||
{
|
||||
if (obj.Instance.GetType().BaseType.Name == baseTypeName)
|
||||
return obj;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,10 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="rScripting, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\rScript\rScripting\bin\Release\rScripting.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
|
|
@ -15,7 +15,6 @@ namespace MudGame
|
|||
static class Program
|
||||
{
|
||||
const String SettingsFile = "Settings.ini";
|
||||
static dynamic _Game;
|
||||
|
||||
static void Main(String[] args)
|
||||
{
|
||||
|
@ -45,49 +44,32 @@ namespace MudGame
|
|||
Log.FlushMessages();
|
||||
|
||||
Log.Write("Launching...", true);
|
||||
/* - Replaced with new startup sequence.
|
||||
* Engine uses my rScript Scripting Engine instead of a custom Mud Designer script Engine.
|
||||
* Easier to maintain, and works better.
|
||||
*
|
||||
* TODO - Remove Old Script Engine code.
|
||||
*
|
||||
ScriptEngine scriptEngine;
|
||||
scriptEngine = new ScriptEngine(game, ScriptEngine.ScriptTypes.Both);
|
||||
|
||||
//scriptEngine.CompileScripts();
|
||||
Log.Write("Initializing Script Engine for Script Compilation...", true);
|
||||
scriptEngine.Initialize();
|
||||
|
||||
GameObject obj = scriptEngine.GetObjectOf("Game");
|
||||
//Console.WriteLine(Log.GetMessages());
|
||||
//Log.FlushMessages();
|
||||
|
||||
if (obj == null)
|
||||
//Search for a custom Game Type before we launch our game.
|
||||
//Compile the scripts
|
||||
rScripting.CompileEngine compiler = new rScripting.CompileEngine(".cs");
|
||||
compiler.Compiler = "MudScriptCompiler";
|
||||
if (!compiler.Compile("Scripts"))
|
||||
{
|
||||
game = new Game();
|
||||
obj = new GameObject(game, "Game");
|
||||
scriptEngine = new ScriptEngine((Game)obj.Instance, ScriptEngine.ScriptTypes.Both);
|
||||
Log.Write("Failed compiling script files.");
|
||||
Log.Write(compiler.Errors);
|
||||
}
|
||||
else
|
||||
|
||||
//If there were errors during compilation, then skip the custom scripts and use the default Game Type.
|
||||
if (!compiler.HasErrors)
|
||||
{
|
||||
//Search the scripts for a Type inheriting from Game
|
||||
rScripting.LateBinding.ScriptFactory factory = new rScripting.LateBinding.ScriptFactory(compiler.CompiledAssembly);
|
||||
foreach (Type t in compiler.CompiledAssembly.GetTypes())
|
||||
{
|
||||
if (t.BaseType.Name == "Game")
|
||||
{
|
||||
rScripting.LateBinding.ScriptObject obj = factory.GetScript(t.Name);
|
||||
game = (Game)obj.Instance;
|
||||
scriptEngine = new ScriptEngine(game, ScriptEngine.ScriptTypes.Both);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Force TCP
|
||||
game.ServerType = ProtocolType.Tcp;
|
||||
|
||||
//Setup the scripting engine and load our script library
|
||||
//MUST be called before game.Start()
|
||||
//scriptEngine.Initialize();
|
||||
//game.scriptEngine = scriptEngine; //Pass this script engine off to the game to use now.
|
||||
|
||||
|
||||
Log.Write("");
|
||||
Log.Write("Starting " + obj.GetProperty().GameTitle + "...", true);
|
||||
Log.Write("");
|
||||
//Console.WriteLine(Log.GetMessages());
|
||||
//Log.FlushMessages();
|
||||
*/
|
||||
|
||||
//Server is only enabled if the option is in the settings file
|
||||
//Allows developers to remove the option from the settings file and letting
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue