public void RegisterLUAFunctions() { Environment.RegisterFunction("sendMessage", this, GetType().GetMethod("SendMessage")); Environment.RegisterFunction("downloadString", this, GetType().GetMethod("DownloadString")); // DEBUG Environment.RegisterFunction("printToConsole", this, GetType().GetMethod("PrintToConsole")); }
public CozyLuaFunction RegisterFunction(string path, MethodBase function) { NLua.LuaFunction func = mLua.RegisterFunction(path, function); if (func != null) { return(new CozyLuaFunction(func)); } return(null); }
protected override void Initialized(object state) { base.Initialized(state); if (_ctx != null) { _ctx.Dispose(); } _ctx = new NLua.Lua(); _ctx.LoadCLRPackage(); _ctx.RegisterFunction("AddCommand", this, this.GetType().GetMethod("AddCommand")); _ctx.RegisterFunction("ArraySort", this, this.GetType().GetMethod("ArraySort")); //_ctx.RegisterFunction("HookBase", this, this.GetType().GetMethods() // .Where(x => x.Name == "HookBase" && x.GetParameters().Last().ParameterType == typeof(NLua.LuaFunction)) // .First()); _ctx.DoFile(Path); CallSelf("Initialized"); var plg = _ctx["export"] as NLua.LuaTable; if (plg != null) { this.TDSMBuild = (int)(double)plg["TDSMBuild"]; this.Author = plg["Author"] as String; this.Description = plg["Description"] as String; this.Name = plg["Name"] as String; this.Version = plg["Version"] as String; IsValid = true; var hooks = plg["Hooks"] as NLua.LuaTable; if (hooks != null) { foreach (KeyValuePair <Object, Object> hookTarget in hooks) { var hook = hookTarget.Key as String; var hookPoint = PluginManager.GetHookPoint(hook); if (hookPoint != null) { var details = hookTarget.Value as NLua.LuaTable; var priority = (HookOrder)(details["Priority"] ?? HookOrder.NORMAL); //var priority = FindOrder(details["Priority"] as String); var del = _ctx.GetFunction(hookPoint.DelegateType, "export.Hooks." + hook + ".Call"); HookBase(hookPoint, priority, del); //TODO maybe fake an actual delegate as this is only used for registering or make a TDSM event info class } } } } }
private void SetInitialState() { State = new NLua.Lua(); State.LoadCLRPackage(); State.DoString(@"import ('World','World.ToyWorldCore')"); State.DoString(@"import ('World','World.Lua')"); State["le"] = this; State["atlas"] = m_atlas; State.RegisterFunction("Help", typeof(LuaExecutor).GetMethod("Help")); if (m_atlas.Avatars.Count > 0) { AvatarCommander avatarCommander = new AvatarCommander(this, m_atlas); State["ac"] = avatarCommander; } AtlasManipulator atlasManipulator = new AtlasManipulator(m_atlas); State["am"] = atlasManipulator; State["lc"] = m_luaConsole; State.DebugHook += OnDebugHook; State.SetDebugHook(EventMasks.LUA_MASKLINE, 1000); }
/// <summary> /// 注册lua函数 /// </summary> /// <param name="LuaAPIClass">lua函数类</param> public void BindLuaApiClass(Object LuaAPIClass) { foreach (MethodInfo Info in LuaAPIClass.GetType().GetMethods()) { foreach (string LuaFunctionName in Attribute.GetCustomAttributes(Info).OfType <LuaFunction>().Select(LuaFunction => LuaFunction.FunctionName)) { LuaState.RegisterFunction(LuaFunctionName, LuaAPIClass, Info); } } }
/// <summary> /// 在沙盒中运行代码,仅允许安全地运行 /// </summary> /// <param name="code"></param> /// <returns></returns> public static string RunSandBox(string code) { using (var lua = new NLua.Lua()) { try { lua.State.Encoding = Encoding.UTF8; lua.RegisterFunction("apiGetPath", null, typeof(LuaApi).GetMethod("GetPath")); //获取程序运行目录 lua.RegisterFunction("apiGetAsciiHex", null, typeof(LuaApi).GetMethod("GetAsciiHex")); //获取字符串ascii编码的hex串 lua["lua_run_result_var"] = "";//返回值所在的变量 lua.DoFile(Common.AppDirectory + "lua/require/sandbox/head.lua"); lua.DoString(code); return(lua["lua_run_result_var"].ToString()); } catch (Exception e) { Common.CqApi.AddLoger(Sdk.Cqp.Enum.LogerLevel.Error, "沙盒lua脚本错误", e.ToString()); return("运行错误:" + e.ToString()); } } }
public void Test() { using (var lua = new NLua.Lua()) { lua.LoadCLRPackage(); lua.RegisterFunction("Calc", null, typeof(Out_Test).GetMethod("Calc")); lua.DoString("a,b,c,e=Calc(10,2)"); double a = (double)lua["a"]; double b = (double)lua["b"]; double c = (double)lua["c"]; string e = (string)lua["e"]; lua.DoString("print('this is from lua print')"); Console.WriteLine("a:{0},b:{1},c:{2},e:{3}", a, b, c, e); Console.Read(); } }
public void Test() { using (var lua = new NLua.Lua()) { lua.LoadCLRPackage(); lua.RegisterFunction("Calc", null, typeof(ref_Test).GetMethod("Calc")); lua.DoString("a=1;b=2;c=3;d=4;e=nil;"); lua.DoString("result,c,d,e=Calc(a,b,c,d,e)"); double a = (double)lua["a"]; double b = (double)lua["b"]; double c = (double)lua["c"]; double d = (double)lua["d"]; string e = (string)lua["e"]; lua.DoString("print('this is from lua print')"); Console.WriteLine("a:{0},b:{1},c:{2},d:{3},e:{4}", a, b, c, d, e); Console.WriteLine(lua["result"]); Console.Read(); } }
/// <summary> /// 初始化lua对象 /// </summary> /// <param name="lua"></param> /// <returns></returns> public static void Initial(NLua.Lua lua) { /////////////// //酷q类的接口// ////////////// lua.RegisterFunction("cqCode_At", null, typeof(LuaApi).GetMethod("CqCode_At")); //获取酷Q "At某人" 代码 lua.RegisterFunction("cqCqCode_Emoji", null, typeof(LuaApi).GetMethod("CqCode_Emoji")); //获取酷Q "emoji表情" 代码 lua.RegisterFunction("cqCqCode_Face", null, typeof(LuaApi).GetMethod("CqCode_Face")); //获取酷Q "表情" 代码 lua.RegisterFunction("cqCqCode_Shake", null, typeof(LuaApi).GetMethod("CqCode_Shake")); //获取酷Q "窗口抖动" 代码 lua.RegisterFunction("cqCqCode_Trope", null, typeof(LuaApi).GetMethod("CqCode_Trope")); //获取字符串的转义形式 lua.RegisterFunction("cqCqCode_UnTrope", null, typeof(LuaApi).GetMethod("CqCode_UnTrope")); //获取字符串的非转义形式 lua.RegisterFunction("cqCqCode_ShareLink", null, typeof(LuaApi).GetMethod("CqCode_ShareLink")); //获取酷Q "链接分享" 代码 lua.RegisterFunction("cqCqCode_ShareCard", null, typeof(LuaApi).GetMethod("CqCode_ShareCard")); //获取酷Q "名片分享" 代码 lua.RegisterFunction("cqCqCode_ShareGPS", null, typeof(LuaApi).GetMethod("CqCode_ShareGPS")); //获取酷Q "位置分享" 代码 lua.RegisterFunction("cqCqCode_Anonymous", null, typeof(LuaApi).GetMethod("CqCode_Anonymous")); //获取酷Q "匿名" 代码 lua.RegisterFunction("cqCqCode_Image", null, typeof(LuaApi).GetMethod("CqCode_Image")); //获取酷Q "图片" 代码 lua.RegisterFunction("cqCqCode_Music", null, typeof(LuaApi).GetMethod("CqCode_Music")); //获取酷Q "音乐" 代码 lua.RegisterFunction("cqCqCode_MusciDIY", null, typeof(LuaApi).GetMethod("CqCode_MusciDIY")); //获取酷Q "音乐自定义" 代码 lua.RegisterFunction("cqCqCode_Record", null, typeof(LuaApi).GetMethod("CqCode_Record")); //获取酷Q "语音" 代码 lua.RegisterFunction("cqSendGroupMessage", null, typeof(LuaApi).GetMethod("SendGroupMessage")); //发送群消息 lua.RegisterFunction("cqSendPrivateMessage", null, typeof(LuaApi).GetMethod("SendPrivateMessage")); //发送私聊消息 lua.RegisterFunction("cqSendDiscussMessage", null, typeof(LuaApi).GetMethod("SendDiscussMessage")); //发送讨论组消息 lua.RegisterFunction("cqSendPraise", null, typeof(LuaApi).GetMethod("SendPraise")); //发送赞 lua.RegisterFunction("cqRepealMessage", null, typeof(LuaApi).GetMethod("RepealMessage")); //撤回消息 lua.RegisterFunction("cqGetLoginQQ", null, typeof(LuaApi).GetMethod("GetLoginQQ")); //取登录QQ lua.RegisterFunction("cqGetLoginNick", null, typeof(LuaApi).GetMethod("GetLoginNick")); //获取当前登录QQ的昵称 lua.RegisterFunction("cqAppDirectory", null, typeof(LuaApi).GetMethod("GetAppDirectory")); //取应用目录 lua.RegisterFunction("cqGetMemberInfo", null, typeof(LuaApi).GetMethod("GetMemberInfo")); //获取群成员信息 lua.RegisterFunction("cqAddLoger", null, typeof(LuaApi).GetMethod("AddLoger")); //添加日志 lua.RegisterFunction("cqAddFatalError", null, typeof(LuaApi).GetMethod("AddFatalError")); //添加致命错误提示 lua.RegisterFunction("cqSetGroupWholeBanSpeak", null, typeof(LuaApi).GetMethod("SetGroupWholeBanSpeak")); //置全群禁言 lua.RegisterFunction("cqSetGroupMemberNewCard", null, typeof(LuaApi).GetMethod("SetGroupMemberNewCard")); //置群成员名片 lua.RegisterFunction("cqSetGroupManager", null, typeof(LuaApi).GetMethod("SetGroupManager")); //置群管理员 lua.RegisterFunction("cqSetAnonymousStatus", null, typeof(LuaApi).GetMethod("SetAnonymousStatus")); //置群匿名设置 lua.RegisterFunction("cqSetGroupMemberRemove", null, typeof(LuaApi).GetMethod("SetGroupMemberRemove")); //置群员移除 lua.RegisterFunction("cqSetDiscussExit", null, typeof(LuaApi).GetMethod("SetDiscussExit")); //置讨论组退出 lua.RegisterFunction("cqSetGroupSpecialTitle", null, typeof(LuaEnv).GetMethod("SetGroupSpecialTitle")); //置群成员专属头衔 lua.RegisterFunction("cqSetGroupAnonymousBanSpeak", null, typeof(LuaEnv).GetMethod("SetGroupAnonymousBanSpeak")); //置匿名群员禁言 lua.RegisterFunction("cqSetGroupBanSpeak", null, typeof(LuaEnv).GetMethod("SetGroupBanSpeak")); //置群员禁言 lua.RegisterFunction("cqSetFriendAddRequest", null, typeof(LuaApi).GetMethod("SetFriendAddRequest")); //置好友添加请求 lua.RegisterFunction("cqSetGroupAddRequest", null, typeof(LuaApi).GetMethod("SetGroupAddRequest")); //置群添加请求 ///////////// //工具类接口// ///////////// lua.RegisterFunction("apiGetPath", null, typeof(LuaApi).GetMethod("GetPath")); //获取程序运行目录 lua.RegisterFunction("apiGetBitmap", null, typeof(LuaApi).GetMethod("GetBitmap")); //获取图片对象 lua.RegisterFunction("apiPutText", null, typeof(LuaApi).GetMethod("PutText")); //摆放文字 lua.RegisterFunction("apiPutBlock", null, typeof(LuaApi).GetMethod("PutBlock")); //填充矩形 lua.RegisterFunction("apiSetImage", null, typeof(LuaApi).GetMethod("SetImage")); //摆放图片 lua.RegisterFunction("apiGetDir", null, typeof(LuaApi).GetMethod("GetDir")); //保存并获取图片路径 lua.RegisterFunction("apiGetImagePath", null, typeof(LuaApi).GetMethod("GetImagePath")); //获取qq消息中图片的网址 lua.RegisterFunction("apiHttpDownload", null, typeof(LuaApi).GetMethod("HttpDownload")); //下载文件 lua.RegisterFunction("apiHttpGet", null, typeof(LuaApi).GetMethod("HttpGet")); //GET 请求与获取结果 lua.RegisterFunction("apiHttpPost", null, typeof(LuaApi).GetMethod("HttpPost")); //POST 请求与获取结果 lua.RegisterFunction("apiBase64File", null, typeof(LuaApi).GetMethod("Base64File")); //获取在线文件的base64结果 lua.RegisterFunction("apiGetPictureWidth", null, typeof(LuaApi).GetMethod("GetPictureWidth")); //获取在线文件的base64结果 lua.RegisterFunction("apiGetPictureHeight", null, typeof(LuaApi).GetMethod("GetPictureHeight")); //获取在线文件的base64结果 lua.RegisterFunction("apiSetVar", null, typeof(LuaApi).GetMethod("SetVar")); //设置某值存入ram lua.RegisterFunction("apiGetVar", null, typeof(LuaApi).GetMethod("GetVar")); //取出某缓存的值 lua.RegisterFunction("apiGetAsciiHex", null, typeof(LuaApi).GetMethod("GetAsciiHex")); //获取字符串ascii编码的hex串 lua.RegisterFunction("apiGetHardDiskFreeSpace", null, typeof(Tools).GetMethod("GetHardDiskFreeSpace")); //获取指定驱动器的剩余空间总大小(单位为MB) lua.RegisterFunction("apiMD5Encrypt", null, typeof(Tools).GetMethod("MD5Encrypt")); //计算MD5 lua.RegisterFunction("apiTcpSend", null, typeof(TcpServer).GetMethod("Send")); //发送tcp广播数据 lua.RegisterFunction("apiSandBox", null, typeof(LuaEnv).GetMethod("RunSandBox")); //沙盒环境 /////////////// //XML操作接口// ////////////// lua.RegisterFunction("apiXmlReplayGet", null, typeof(XmlApi).GetMethod("replay_get")); //随机获取一条结果 lua.RegisterFunction("apiXmlListGet", null, typeof(XmlApi).GetMethod("list_get")); //获取所有回复的列表 lua.RegisterFunction("apiXmlDelete", null, typeof(XmlApi).GetMethod("del")); //删除所有匹配的条目 lua.RegisterFunction("apiXmlRemove", null, typeof(XmlApi).GetMethod("remove")); //删除完全匹配的第一个条目 lua.RegisterFunction("apiXmlInsert", null, typeof(XmlApi).GetMethod("insert")); //插入一个词条 lua.RegisterFunction("apiXmlSet", null, typeof(XmlApi).GetMethod("set")); //更改某条的值 lua.RegisterFunction("apiXmlGet", null, typeof(XmlApi).GetMethod("xml_get")); //获取某条的结果 lua.RegisterFunction("apiXmlRow", null, typeof(XmlApi).GetMethod("xml_row")); //按结果查源头(反查) lua.DoFile(Common.AppDirectory + "lua/require/head.lua"); }
public void RegisterLuaFunc(NLua.Lua lua, string luaName, object target, string rawFunc) { lua.RegisterFunction(luaName, target, target.GetType().GetMethod(rawFunc)); }
/// <summary> /// 初始化lua对象 /// </summary> /// <param name="lua"></param> /// <returns></returns> public static void Initial(NLua.Lua lua) { /////////////// //酷q类的接口// ////////////// lua.RegisterFunction("cqCode_At", null, typeof(LuaApi).GetMethod("CqCode_At")); //获取酷Q "At某人" 代码 lua.RegisterFunction("cqCqCode_Emoji", null, typeof(LuaApi).GetMethod("CqCode_Emoji")); //获取酷Q "emoji表情" 代码 lua.RegisterFunction("cqCqCode_Face", null, typeof(LuaApi).GetMethod("CqCode_Face")); //获取酷Q "表情" 代码 lua.RegisterFunction("cqCqCode_Shake", null, typeof(LuaApi).GetMethod("CqCode_Shake")); //获取酷Q "窗口抖动" 代码 lua.RegisterFunction("cqCqCode_Trope", null, typeof(LuaApi).GetMethod("CqCode_Trope")); //获取字符串的转义形式 lua.RegisterFunction("cqCqCode_UnTrope", null, typeof(LuaApi).GetMethod("CqCode_UnTrope")); //获取字符串的非转义形式 lua.RegisterFunction("cqCqCode_ShareLink", null, typeof(LuaApi).GetMethod("CqCode_ShareLink")); //获取酷Q "链接分享" 代码 lua.RegisterFunction("cqCqCode_ShareCard", null, typeof(LuaApi).GetMethod("CqCode_ShareCard")); //获取酷Q "名片分享" 代码 lua.RegisterFunction("cqCqCode_ShareGPS", null, typeof(LuaApi).GetMethod("CqCode_ShareGPS")); //获取酷Q "位置分享" 代码 lua.RegisterFunction("cqCqCode_Anonymous", null, typeof(LuaApi).GetMethod("CqCode_Anonymous")); //获取酷Q "匿名" 代码 lua.RegisterFunction("cqCqCode_Image", null, typeof(LuaApi).GetMethod("CqCode_Image")); //获取酷Q "图片" 代码 lua.RegisterFunction("cqCqCode_Music", null, typeof(LuaApi).GetMethod("CqCode_Music")); //获取酷Q "音乐" 代码 lua.RegisterFunction("cqCqCode_MusciDIY", null, typeof(LuaApi).GetMethod("CqCode_MusciDIY")); //获取酷Q "音乐自定义" 代码 lua.RegisterFunction("cqCqCode_Record", null, typeof(LuaApi).GetMethod("CqCode_Record")); //获取酷Q "语音" 代码 lua.RegisterFunction("cqSendGroupMessage", null, typeof(LuaApi).GetMethod("SendGroupMessage")); //发送群消息 lua.RegisterFunction("cqSendPrivateMessage", null, typeof(LuaApi).GetMethod("SendPrivateMessage")); //发送私聊消息 lua.RegisterFunction("cqSendDelyMessage", null, typeof(LuaApi).GetMethod("SendDelyMessage")); //发送延迟消息 lua.RegisterFunction("cqSendDiscussMessage", null, typeof(LuaApi).GetMethod("SendDiscussMessage")); //发送讨论组消息 lua.RegisterFunction("cqSendPraise", null, typeof(LuaApi).GetMethod("SendPraise")); //发送赞 lua.RegisterFunction("cqRepealMessage", null, typeof(LuaApi).GetMethod("RepealMessage")); //撤回消息 lua.RegisterFunction("cqGetLoginQQ", null, typeof(LuaApi).GetMethod("GetLoginQQ")); //取登录QQ lua.RegisterFunction("cqGetLoginNick", null, typeof(LuaApi).GetMethod("GetLoginNick")); //获取当前登录QQ的昵称 lua.RegisterFunction("cqAppDirectory", null, typeof(LuaApi).GetMethod("GetAppDirectory")); //取应用目录 lua.RegisterFunction("cqGetMemberInfo", null, typeof(LuaApi).GetMethod("GetMemberInfo")); //获取群成员信息 lua.RegisterFunction("cqGetMemberList", null, typeof(LuaApi).GetMethod("GetMemberList")); //获取全部群成员信息 lua.RegisterFunction("cqGetGroupList", null, typeof(LuaApi).GetMethod("GetGroupList")); //获取群列表 lua.RegisterFunction("cqAddLoger", null, typeof(LuaApi).GetMethod("AddLoger")); //添加日志 lua.RegisterFunction("cqAddFatalError", null, typeof(LuaApi).GetMethod("AddFatalError")); //添加致命错误提示 lua.RegisterFunction("cqSetGroupWholeBanSpeak", null, typeof(LuaApi).GetMethod("SetGroupWholeBanSpeak")); //置全群禁言 lua.RegisterFunction("cqSetGroupMemberNewCard", null, typeof(LuaApi).GetMethod("SetGroupMemberNewCard")); //置群成员名片 lua.RegisterFunction("cqSetGroupManager", null, typeof(LuaApi).GetMethod("SetGroupManager")); //置群管理员 lua.RegisterFunction("cqSetAnonymousStatus", null, typeof(LuaApi).GetMethod("SetAnonymousStatus")); //置群匿名设置 lua.RegisterFunction("cqSetGroupMemberRemove", null, typeof(LuaApi).GetMethod("SetGroupMemberRemove")); //置群员移除 lua.RegisterFunction("cqSetDiscussExit", null, typeof(LuaApi).GetMethod("SetDiscussExit")); //置讨论组退出 lua.RegisterFunction("cqSetGroupSpecialTitle", null, typeof(LuaApi).GetMethod("SetGroupSpecialTitle")); //置群成员专属头衔 lua.RegisterFunction("cqSetGroupAnonymousBanSpeak", null, typeof(LuaApi).GetMethod("SetGroupAnonymousBanSpeak")); //置匿名群员禁言 lua.RegisterFunction("cqSetGroupBanSpeak", null, typeof(LuaApi).GetMethod("SetGroupBanSpeak")); //置群员禁言 lua.RegisterFunction("cqSetFriendAddRequest", null, typeof(LuaApi).GetMethod("SetFriendAddRequest")); //置好友添加请求 lua.RegisterFunction("cqSetGroupAddRequest", null, typeof(LuaApi).GetMethod("SetGroupAddRequest")); //置群添加请求 lua.RegisterFunction("cqSetGroupExit", null, typeof(LuaApi).GetMethod("SetGroupExit")); //置群退出 ///////////// //工具类接口// ///////////// lua.RegisterFunction("apiGetPath", null, typeof(LuaApi).GetMethod("GetPath")); //获取程序运行目录 lua.RegisterFunction("apiGetAppName", null, typeof(LuaApi).GetMethod("GetAppName")); //获取插件包名 lua.RegisterFunction("apiGetBitmap", null, typeof(LuaApi).GetMethod("GetBitmap")); //获取图片对象 lua.RegisterFunction("apiPutText", null, typeof(LuaApi).GetMethod("PutText")); //摆放文字 lua.RegisterFunction("apiPutBlock", null, typeof(LuaApi).GetMethod("PutBlock")); //填充矩形 lua.RegisterFunction("apiSetImage", null, typeof(LuaApi).GetMethod("SetImage")); //摆放图片 lua.RegisterFunction("apiGetDir", null, typeof(LuaApi).GetMethod("GetDir")); //保存并获取图片路径 lua.RegisterFunction("apiGetImagePath", null, typeof(LuaApi).GetMethod("GetImagePath")); //获取qq消息中图片的网址 lua.RegisterFunction("apiHttpFileDownload", null, typeof(LuaApi).GetMethod("HttpFileDownload")); //下载文件 lua.RegisterFunction("apiHttpImageDownload", null, typeof(LuaApi).GetMethod("HttpImageDownload")); //爬取图片 lua.RegisterFunction("apiHttpGet", null, typeof(LuaApi).GetMethod("HttpGet")); //GET 请求与获取结果 lua.RegisterFunction("apiHttpPost", null, typeof(LuaApi).GetMethod("HttpPost")); //POST 请求与获取结果 lua.RegisterFunction("apiBase64File", null, typeof(LuaApi).GetMethod("Base64File")); //获取在线文件的base64结果 lua.RegisterFunction("apiGetPictureWidth", null, typeof(LuaApi).GetMethod("GetPictureWidth")); //获取在线文件的base64结果 lua.RegisterFunction("apiGetPictureHeight", null, typeof(LuaApi).GetMethod("GetPictureHeight")); //获取在线文件的base64结果 lua.RegisterFunction("apiSetVar", null, typeof(LuaApi).GetMethod("SetVar")); //设置某值存入ram lua.RegisterFunction("apiGetVar", null, typeof(LuaApi).GetMethod("GetVar")); //取出某缓存的值 lua.RegisterFunction("apiDirectoryList", null, typeof(LuaApi).GetMethod("DirectoryList")); //获取xml目录下的文件夹列表 lua.RegisterFunction("apiFileList", null, typeof(LuaApi).GetMethod("FileList")); //获取xml目录下的文件列表 lua.RegisterFunction("apiGetAsciiHex", null, typeof(LuaApi).GetMethod("GetAsciiHex")); //获取字符串ascii编码的hex串 lua.RegisterFunction("apiSetTimerScriptWait", null, typeof(LuaApi).GetMethod("SetTimerScriptWait")); //设置定时脚本运行间隔时间 lua.RegisterFunction("apiGetHardDiskFreeSpace", null, typeof(Tools).GetMethod("GetHardDiskFreeSpace")); //获取指定驱动器的剩余空间总大小(单位为MB) lua.RegisterFunction("apiMD5Encrypt", null, typeof(Tools).GetMethod("MD5Encrypt")); //计算MD5 lua.RegisterFunction("apiSandBox", null, typeof(LuaEnv).GetMethod("RunSandBox")); //沙盒环境 lua.RegisterFunction("apiOrderSearch", null, typeof(LuaApi).GetMethod("OrderSearch")); //单号识别 lua.RegisterFunction("apiNowSearch", null, typeof(LuaApi).GetMethod("NowSearch")); //快递查询 lua.RegisterFunction("apiOrderSub", null, typeof(LuaApi).GetMethod("OrderSub")); //快递订阅 lua.RegisterFunction("apiservice", null, typeof(TcpSer).GetMethod("service")); //开启tcp服务器 lua.RegisterFunction("apiclient", null, typeof(TcpSer).GetMethod("client")); //连接tcp服务器 lua.RegisterFunction("apiListenStart", null, typeof(HttpListenerPostParaHelper).GetMethod("ListenStart")); //开启httprequest监听 lua.RegisterFunction("apiGetResponseHeaders", null, typeof(LuaApi).GetMethod("GetResponseHeaders")); //获取response header头 lua.RegisterFunction("apiTimerStart", null, typeof(TimerRun).GetMethod("TimerStart")); //开启循环任务 lua.RegisterFunction("apiGeneralBasic", null, typeof(BaiDuApi).GetMethod("GeneralBasic")); //本地图片文字识别 lua.RegisterFunction("apiGeneralBasicUrl", null, typeof(BaiDuApi).GetMethod("GeneralBasicUrl")); //链接图片文字识别 lua.RegisterFunction("apiAdvancedGeneral", null, typeof(BaiDuApi).GetMethod("AdvancedGeneral")); //图像识别 lua.RegisterFunction("apiQREncode", null, typeof(QRCode).GetMethod("QREncode")); //二维码生成 lua.RegisterFunction("apiQRDecode", null, typeof(QRCode).GetMethod("QRDecode")); //二维码解码 lua.RegisterFunction("apiCombinImage", null, typeof(QRCode).GetMethod("CombinImage")); //二维码logo生成 /////////////// //XML操作接口// ////////////// lua.RegisterFunction("apiXmlReplayGet", null, typeof(XmlApi).GetMethod("replay_get")); //随机获取一条结果 lua.RegisterFunction("apiXmlListGet", null, typeof(XmlApi).GetMethod("alist_get")); //获取所有回复的列表 lua.RegisterFunction("apiXmlDelete", null, typeof(XmlApi).GetMethod("del")); //删除所有匹配的条目 lua.RegisterFunction("apiXmlRemove", null, typeof(XmlApi).GetMethod("remove")); //删除完全匹配的第一个条目 lua.RegisterFunction("apiXmlInsert", null, typeof(XmlApi).GetMethod("insert")); //插入一个词条 lua.RegisterFunction("apiXmlSet", null, typeof(XmlApi).GetMethod("set")); //更改某条的值 lua.RegisterFunction("apiXmlGet", null, typeof(XmlApi).GetMethod("xml_get")); //获取某条的结果 lua.RegisterFunction("apiXmlRow", null, typeof(XmlApi).GetMethod("xml_row")); //按结果查源头(反查) lua.RegisterFunction("apiXmlIdListGet", null, typeof(XmlApi).GetMethod("nlist_get")); //获取所有号码id列表 lua.DoFile(Common.AppDirectory + "lua/require/head.lua"); }
private void SetLuaEnv() { try { luaEngine.State.Encoding = Encoding.UTF8; luaEngine.LoadCLRPackage(); helpInfo += "CLR package is loaded, using:\r\nimport ('System.Web');\r\nlocal client = WebClient()\r\n\r\n"; // Keys luaEngine.DoString("Keys={};"); foreach (Keys key in Enum.GetValues(typeof(Keys))) { luaEngine.DoString($"Keys[\"{key.ToString()}\"]={(int)key};"); } helpInfo += "Keys[] is a table containing .Net Windows.Forms.Keys enum values\r\n\r\n"; // Moving luaEngine.RegisterFunction(nameof(Move2D), this, GetType().GetMethod(nameof(Move2D))); helpInfo += "void Move2D(table points) - something like { {[\"X\"] = 5673.50244, [\"Y\"] = 4510.01953, [\"Z\"] = 125.027237}, {[\"X\"] = 5680.97363, [\"Y\"] = 4487.585, [\"Z\"] = 130.122177} }\r\n"; // game luaEngine.RegisterFunction("UseItemByID", info, typeof(GameInterface).GetMethod("UseItemByID")); helpInfo += "void UseItemByID(uint id)\r\n"; luaEngine.RegisterFunction("UseItem", info, typeof(GameInterface).GetMethod("UseItem")); helpInfo += "void UseItem(int bagID, int slotID)\r\n"; luaEngine.RegisterFunction("CastSpellByName", info, typeof(GameInterface).GetMethod("CastSpellByName")); helpInfo += "void CastSpellByName(string spellName)\r\n"; luaEngine.RegisterFunction("SelectDialogOption", info, typeof(GameInterface).GetMethod("SelectDialogOption")); helpInfo += "void SelectDialogOption(string gossipText)\r\n"; luaEngine.RegisterFunction("BuyMerchantItem", info, typeof(GameInterface).GetMethod("BuyMerchantItem")); helpInfo += "void BuyMerchantItem(uint itemID, int count)\r\n"; luaEngine.RegisterFunction("SendToChat", info, typeof(GameInterface).GetMethod("SendToChat")); helpInfo += "void SendToChat(string command)\r\n"; // Info luaEngine.RegisterFunction(nameof(IsInGame), this, GetType().GetMethod(nameof(IsInGame))); helpInfo += "bool IsInGame()\r\n"; luaEngine.RegisterFunction(nameof(IsLoadingScreenVisible), this, GetType().GetMethod(nameof(IsLoadingScreenVisible))); helpInfo += "bool IsLoadingScreenVisible()\r\n"; luaEngine.RegisterFunction(nameof(MouseoverGUID), this, GetType().GetMethod(nameof(MouseoverGUID))); helpInfo += "string MouseoverGUID()\r\n"; luaEngine.RegisterFunction(nameof(IsSpellKnown), this, GetType().GetMethod(nameof(IsSpellKnown))); helpInfo += "bool IsSpellKnown(double spellID)\r\n"; luaEngine.RegisterFunction(nameof(IsLooting), this, GetType().GetMethod(nameof(IsLooting))); helpInfo += "bool IsLooting()\r\n"; luaEngine.RegisterFunction(nameof(ZoneID), this, GetType().GetMethod(nameof(ZoneID))); helpInfo += "double ZoneID()\r\n"; luaEngine.RegisterFunction(nameof(ZoneText), this, GetType().GetMethod(nameof(ZoneText))); helpInfo += "string ZoneText()\r\n"; luaEngine.RegisterFunction(nameof(Lua_GetValue), this, GetType().GetMethod(nameof(Lua_GetValue))); helpInfo += "string Lua_GetValue(string func)\r\n"; luaEngine.RegisterFunction(nameof(Lua_IsTrue), this, GetType().GetMethod(nameof(Lua_IsTrue))); helpInfo += "bool Lua_IsTrue(string condition)\r\n"; // Objects luaEngine.RegisterFunction(nameof(GetLocalPlayer), this, GetType().GetMethod(nameof(GetLocalPlayer))); helpInfo += "uservalue WoWPlayerMe GetLocalPlayer()\r\n"; luaEngine.RegisterFunction(nameof(GetNpcs), this, GetType().GetMethod(nameof(GetNpcs))); helpInfo += "double, uservalue List<WowNpc> GetNpcs()\r\n"; luaEngine.RegisterFunction(nameof(GetPlayers), this, GetType().GetMethod(nameof(GetPlayers))); helpInfo += "double, uservalue List<WowPlayer> GetPlayers()\r\n"; luaEngine.RegisterFunction(nameof(GetObjects), this, GetType().GetMethod(nameof(GetObjects))); helpInfo += "double, uservalue List<WowObject> GetObjects()\r\n"; luaEngine.RegisterFunction(nameof(GetTargetObject), this, GetType().GetMethod(nameof(GetTargetObject))); helpInfo += "uservalue dynamic GetTargetObject()\r\n"; // utilities luaEngine.RegisterFunction(nameof(MsgBox), this, GetType().GetMethod(nameof(MsgBox))); helpInfo += "void MsgBox(object text)\r\n"; luaEngine.RegisterFunction("PressKey", info, info.GetType().GetMethod("PressKey")); helpInfo += "void PressKey(int key)\r\n"; luaEngine.RegisterFunction(nameof(Log), this, GetType().GetMethod(nameof(Log))); helpInfo += "void Log(object text)\r\n"; luaEngine.RegisterFunction(nameof(Wait), this, GetType().GetMethod(nameof(Wait))); helpInfo += "void Wait(double ms) - this method contains call to StopExecutionIfCancellationRequested()\r\n"; luaEngine.RegisterFunction(nameof(NotifyUser), this, GetType().GetMethod(nameof(NotifyUser))); helpInfo += "void NotifyUser(object text)\r\n"; luaEngine.RegisterFunction(nameof(StopExecutionIfCancellationRequested), this, GetType().GetMethod(nameof(StopExecutionIfCancellationRequested))); helpInfo += "bool StopExecutionIfCancellationRequested()\r\n"; luaEngine.RegisterFunction("GetUIFrameByName", info, info.GetType().GetMethod("GetUIFrameByName")); helpInfo += "uservalue<WoWUIFrame> GetUIFrameByName(string frameName)\r\n"; // lua lib luaEngine.DoString("format=string.format;"); luaEngine.DoString("if (not table.count) then table.count = function(tbl) local count = 0; for index in pairs(tbl) do count = count+1; end return count; end end"); luaEngine.DoString("if (not table.first) then table.first = function(tbl) for _, value in pairs(tbl) do return value; end end end"); helpInfo += "real table.count(tbl)\r\nvalue table.first(tbl)\r\n"; luaEngine.DoString(GetNearestObjectByName); helpInfo += "uservalue WowNpc/WowObject/WowPlayer GetNearestObjectByName(listLength, list, name)\r\n"; luaEngine.DoString("print = function(...) local text = \"\"; for i = 1, select(\"#\", ...) do text = text..tostring(select(i, ...))..\" \" end SendToChat(\"/run print(\"..text..\")\"); end"); helpInfo += "void print(params string)\r\n"; } catch (Exception ex) { MsgBox(ex.Message); } }
public void Run(string code) { using (NLua.Lua lua = new NLua.Lua()) { lua["lua_run_result_var"] = ""; try { lua.RegisterFunction("httpGet_row", null, typeof(Tools).GetMethod("HttpGet")); lua.RegisterFunction("httpPost_row", null, typeof(Tools).GetMethod("HttpPost")); lua.RegisterFunction("setData_row", null, typeof(Tools).GetMethod("LuaSetXml")); lua.RegisterFunction("getData_row", null, typeof(Tools).GetMethod("LuaGetXml")); lua.RegisterFunction("fileDownload", null, typeof(Tools).GetMethod("FileDownload")); lua.RegisterFunction("getImg", null, typeof(LuaApi).GetMethod("GetBitmap")); lua.RegisterFunction("setImgText", null, typeof(LuaApi).GetMethod("PutText")); lua.RegisterFunction("putImgBlock", null, typeof(LuaApi).GetMethod("putBlock")); lua.RegisterFunction("setImgImage", null, typeof(LuaApi).GetMethod("setImage")); lua.RegisterFunction("getImgDir", null, typeof(LuaApi).GetMethod("GetDir")); lua.RegisterFunction("getPath", null, typeof(LuaApi).GetMethod("GetPath")); lua.DoFile(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "lua/head.lua"); lua.DoString(Encoding.UTF8.GetBytes(headRun)); lua.DoString(Encoding.UTF8.GetBytes(code)); if (Tools.CharNum(lua["lua_run_result_var"].ToString(), "\n") > 40 || Tools.CharNum(lua["lua_run_result_var"].ToString(), "\r") > 40) { result = "行数超过了20行,限制一下吧"; } else if (lua["lua_run_result_var"].ToString().Length > 2000) { result = "字数超过了2000,限制一下吧"; } else { result = lua["lua_run_result_var"].ToString(); } } catch (Exception e) { string err = e.Message; int l = err.IndexOf("lua/"); if (l >= 0) { err = err.Substring(l); } result = "代码崩掉啦\r\n" + err; } } }