i want to edit something form the source from outside the source like npc dialogs , should i move them to database or it's possible to do that ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
using System.CodeDom.Compiler;
namespace Sdl_Game_Project.ScriptEngine
{
/// <summary>
/// A C# script engine.
/// Notice: This will be optimized to support a new thread for better performance.
/// </summary>
public class CSharpEngine
{
/// <summary>
/// Handling a script file.
/// </summary>
/// <param name="scriptFile">The file to handle.</param>
/// <param name="methodName">The method to invoke.</param>
/// <param name="nameSpace">The namespace.</param>
/// <param name="className">The class.</param>
/// <param name="Params">The parameters of the methods. Null if none parameters.</param>
public static void Handle(string scriptFile, string methodName, string nameSpace, string className, object[] Params = null)
{
if (!scriptFile.EndsWith(".cs"))
{
throw new Exception("Make sure the script file is a CSharp (C#) file.");
}
string code = File.ReadAllText(scriptFile);
Dictionary<string, string> providerOptions = new Dictionary<string, string>()
{
{"CompilerVersion", Sdl.GameCore.ScriptFramework}
};
CompilerParameters compilerParams = new CompilerParameters()
{
GenerateInMemory = true,
GenerateExecutable = false
};
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
compilerParams.ReferencedAssemblies.Add(assembly.Location);
}
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, code);
if (results == null)
throw new Exception("Unknown compiler error.");
if (results.Errors.Count != 0)
throw new Exception("Failed to compile the game script.");
MethodInfo method = FindMethod(results.CompiledAssembly, methodName, nameSpace, className);
if (method != null)
method.Invoke(null, (Params == null ? null : Params));
}
/// <summary>
/// Finds a method within an assembly.
/// </summary>
/// <param name="assembly">The assembly</param>
/// <param name="methodName">The method to find.</param>
/// <param name="nameSpace">The namespace.</param>
/// <param name="className">The class.</param>
/// <returns>Returns null if no methods were found.</returns>
internal static MethodInfo FindMethod(Assembly assembly, string methodName, string nameSpace, string className)
{
foreach (Type type in assembly.GetTypes())
{
if (type.Namespace == nameSpace && type.IsClass && type.Name == className)
{
foreach (MethodInfo method in type.GetMethods())
{
if (method.IsStatic && method.Name == methodName)
{
return method;
}
}
}
}
return null;
}
}
}