/* * LuaHelper.cs * Authors: Adam Nabinger * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System.IO; using LuaInterface; using System; using System.Reflection; public static class LuaHelper { private const string LuaFile = @"lua51.dll"; static LuaHelper() { if (!File.Exists(LuaFile)) { using (FileStream output = new FileStream(LuaFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) using (BinaryWriter writer = new BinaryWriter(output)) { writer.Write(LuaAccess.Resource.lua51); } } } public static Lua GetNewVM() { return new Lua(); } public static void RegisterAllFunctions(Lua VM, object obj) { foreach (MethodInfo info in obj.GetType().GetMethods()) { VM.RegisterFunction(info.Name, obj, info); } } public static void RegisterLuaCallbacks(Lua VM, object obj) { Type type = obj.GetType(); foreach (MethodInfo info in type.GetMethods()) { foreach (Attribute attribute in Attribute.GetCustomAttributes(info)) { LuaCallback luaCallback = attribute as LuaCallback; if (luaCallback != null) { string funcName = luaCallback.GetFunctionName(); string[] funcParams = luaCallback.GetFunctionParameters(); ParameterInfo[] paramInfo = info.GetParameters(); if (funcParams != null && (funcParams.Length != paramInfo.Length)) { System.Diagnostics.Debug.Assert(false, "The function " + funcName + " has an LuaCallback attribute whose parameters do not match the C# function."); break; } VM.RegisterFunction(funcName, obj, info); } } } } }