/*
* Created by SharpDevelop.
* User: elijah
* Date: 12/20/2011
* Time: 10:34 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using KSP.IO;
using System.Reflection;
using SharpLua.LuaTypes;
namespace SharpLua.Library
{
public class ScriptLib
{
public static void RegisterModule(LuaTable enviroment)
{
LuaTable module = new LuaTable();
RegisterFunctions(module);
enviroment.SetNameValue("script", module);
}
public static void RegisterFunctions(LuaTable module)
{
module.Register("reference", Reference);
module.Register("create", Create);
module.Register("import", Import);
module.Register("getinfo", GetInfo);
module.Register("dump", Dump);
module.Register("call", Call);
LuaTable mt = new LuaTable();
mt.Register("__index", new LuaFunc((LuaValue[] args) =>
{
return Create(args);
}));
mt.Register("__call", new LuaFunc((LuaValue[] args) =>
{
return Create(args);
}));
module.MetaTable = mt;
}
public static LuaValue Reference(LuaValue[] args)
{
foreach (LuaValue obj in args)
{
try {
Library.BaseLib.Print(new LuaValue[] {new LuaString(AssemblyCache.Instance.LoadAssembly(obj.Value.ToString()).ToString())});
} catch (Exception ex) {
Library.BaseLib.Print(new LuaValue[] {new LuaString("Error adding reference to '" + obj.Value.ToString() + "'\n" + ex.Message)});
}
}
return LuaNil.Nil;
}
///
/// Creates a .NET object
/// format: script.create(type, ...) where ... is constructor args
///
///
///
public static LuaValue Create(LuaValue[] args)
{
Type t = null;
t = AssemblyCache.FindType(args[0].Value.ToString());
if (t == null)
BaseLib.Print(new LuaValue[] { new LuaString("Cannot find type '" + args[0].Value.ToString() + "' in loaded assemblies!")});
List