Exemplo n.º 1
0
        public static void RunCSharpLua(MCCSAPI api)
        {
            List <IntPtr> uuid = new List <IntPtr>();
            const String  path = "./cslr";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                Console.WriteLine("[INFO] [CSLR] 已创建文件夹");
            }
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("[INFO] [CSLR] CSharpLuaRunner加载中");
            if (!File.Exists("./KeraLua.dll"))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[IPYR] 无法找到依赖库 请将KeraLua.dll与Lua54.dll放到BDS根目录");
                Console.ForegroundColor = ConsoleColor.White;
            }
            var           LuaFun    = new List <dynamic>();
            DirectoryInfo Allfolder = new DirectoryInfo(path);
            var           mc        = new MCLUAAPI(api);

            GC.KeepAlive(mc);
            foreach (FileInfo file in Allfolder.GetFiles("*.cs.lua"))
            {
                try
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("[INFO] [CSLR] 正在加载" + file.Name);
                    Lua lua = new Lua();
                    lua.DoFile(file.FullName);
                    Console.WriteLine("[INFO] [CSLR] " + file.Name + "加载成功");
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[ERROR] [CSLR] " + e.Message);
                    Console.WriteLine("[ERROR] [CSLR] 加载" + file.Name + "失败");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }

            api.addBeforeActListener(EventKey.onLoadName, x =>
            {
                var a = BaseEvent.getFrom(x) as LoadNameEvent;
                uuid.Add(a.playerPtr);
                ptr.Add(a.uuid, a.playerPtr);
                CallLuaFunc(LuaFun, func =>
                {
                    CsPlayer p  = new CsPlayer(api, a.playerPtr);
                    string list = "{\'playername\':\'" + a.playername + "\',\'uuid\':\'" + a.uuid + "\',\'xuid\':\'" + a.xuid + "\',\'IPport\':\'" + p.IpPort + "\'}";
                    var re      = func.load_name(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onPlayerLeft, x =>
            {
                var a = BaseEvent.getFrom(x) as PlayerLeftEvent;
                uuid.Remove(a.playerPtr);
                ptr.Remove(a.uuid);
                CallLuaFunc(LuaFun, func =>
                {
                    string list = "{\'playername\':\'" + a.playername + "\',\'uuid\':\'" + a.uuid + "\',\'xuid\':\'" + a.xuid + "\'}";
                    var re      = func.player_left(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onServerCmd, x =>
            {
                var a = BaseEvent.getFrom(x) as ServerCmdEvent;

                if (a.cmd.StartsWith("cslr "))
                {
                    string[] sArray = a.cmd.Split(new char[2] {
                        ' ', ' '
                    });

                    if (sArray[1] == "help")
                    {
                        Console.WriteLine("[INFO] [CSLR] help   使用帮助\n[INFO] [CSLR] info    CSLR信息\n[INFO] [CSLR] list  插件列表\n[INFO] [CSLR] reload  重载插件");
                        return(false);
                    }

                    if (sArray[1] == "info")
                    {
                        MessageBox.Show("感谢使用CSharpLuaRunner\n作者:SeaIceNX", "当前版本" + version, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Console.Write("[INFO] [CSLR] 窗体关闭 控制台已恢复");
                        return(false);
                    }

                    if (sArray[1] == "list")
                    {
                        DirectoryInfo folder = new DirectoryInfo(path);
                        int total            = 0;
                        Console.WriteLine("[INFO] [CSLR] 正在读取插件列表");
                        foreach (FileInfo file in Allfolder.GetFiles("*.cs.lua"))
                        {
                            Console.WriteLine(" - " + file.Name + " | ID: " + total);
                            total += 1;
                        }
                        Console.WriteLine($"[INFO] [CSLR]共加载了{total}个插件");
                        return(false);
                    }

                    if (sArray[1] == "reload")
                    {
                        LuaFun.Clear();
                        ShareDatas.Clear();
                        DirectoryInfo folder = new DirectoryInfo(path);
                        foreach (FileInfo file in folder.GetFiles("*.cs.lua"))
                        {
                            try
                            {
                                Console.ForegroundColor = ConsoleColor.White;
                                Console.WriteLine("[INFO] [CSLR] 正在加载" + file.Name);
                                Lua lua = new Lua();
                                lua.DoFile(file.FullName);
                                Console.WriteLine("[INFO] [CSLR] " + file.Name + "加载成功");
                            }
                            catch (Exception e)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("[ERROR] [CSLR] " + e.Message);
                                Console.WriteLine("[ERROR] [CSLR] 加载" + file.Name + "失败");
                                Console.ForegroundColor = ConsoleColor.White;
                            }
                        }
                        Console.WriteLine("[INFO] [CSLR] 重载成功");
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    var re = true;
                    CallLuaFunc(LuaFun, func =>
                    {
                        string list = $"{{\'cmd\':\' {a.cmd }\'}}";
                        re          = func.server_command(list);
                    });
                    return(re);
                }
            });

            api.addBeforeActListener(EventKey.onEquippedArmor, x =>
            {
                var a = BaseEvent.getFrom(x) as EquippedArmorEvent;
                CallLuaFunc(LuaFun, func =>
                {
                    string list = "{\'playername\':\'" + a.playername + "\',\'itemid\':\'" + a.itemid + "\',\'itemname\':\'" + a.itemname + "\',\'itemcount\':\'" + a.itemcount + "\',\'itemaux\':\'" + a.itemaux + "\',\'slot\':\'" + a.slot + "\',\'Pos\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "]}";
                    var re      = func.equippedarm(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onAttack, x =>
            {
                var a  = BaseEvent.getFrom(x) as AttackEvent;
                var re = true;
                CallLuaFunc(LuaFun, func =>
                {
                    string list = "{\'actorname\':\'" + a.actorname + "\',\'dimensionid\':\'" + a.dimensionid + "\',\'playername\':\'" + a.playername + "\',\'Pos\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "]}";
                    re          = func.attack(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onInputText, x =>
            {
                var a  = BaseEvent.getFrom(x) as InputTextEvent;
                var re = true;
                CallLuaFunc(LuaFun, func =>
                {
                    string list = "{\'msg\':\'" + a.msg + "\',\'dimensionid\':\'" + a.dimensionid + "\',\'uuid\':\'" + CsGetUuid(uuid, a.playername, api) + "\',\'playername\':\'" + a.playername + "\',\'XYZ\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "]}";
                    re          = func.inputtext(list);
                });
                return(re);
            });
            api.addBeforeActListener(EventKey.onDestroyBlock, x =>
            {
                var a       = BaseEvent.getFrom(x) as DestroyBlockEvent;
                var re      = true;
                string list = "{\'blockid\':\'" + a.blockid + "\',\'uuid\':\'" + CsGetUuid(uuid, a.playername, api) + "\',\'position\':[" + Convert.ToInt32(a.position.x) + "," + Convert.ToInt32(a.position.y) + "," + Convert.ToInt32(a.position.z) + "],\'blockname\':\'" + a.blockname + "\',\'dimensionid\':\'" + a.dimensionid + "\',\'playername\':\'" + a.playername + "\',\'Pos\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "]}";
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.destroyblock(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onMobDie, x =>
            {
                var a       = BaseEvent.getFrom(x) as MobDieEvent;
                var re      = true;
                string list = "{\'mobname\':\'" + a.mobname + "\',\'mobtype\':\'" + a.mobtype + "\',\'XYZ\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "],\'srcname\':\'" + a.srcname + "\',\'dimensionid\':\'" + a.dimensionid + "\',\'playername\':\'" + a.playername + "\'}";
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.mobdie(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onRespawn, x =>
            {
                var a       = BaseEvent.getFrom(x) as RespawnEvent;
                string list = "{\'XYZ\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "],\'dimensionid\':\'" + a.dimensionid + "\',\'playername\':\'" + a.playername + "\',\'uuid\':\'" + CsGetUuid(uuid, a.playername, api) + "\'}";

                CallLuaFunc(LuaFun, func =>
                {
                    var re = func.respawn(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onInputCommand, x =>
            {
                var a       = BaseEvent.getFrom(x) as InputCommandEvent;
                var re      = true;
                string list = "{\'cmd\':\'" + a.cmd + "\',\'XYZ\':[" + Convert.ToInt32(a.XYZ.x) + "," + Convert.ToInt32(a.XYZ.y) + "," + Convert.ToInt32(a.XYZ.z) + "],\'dimensionid\':\'" + a.dimensionid + "\',\'playername\':\'" + a.playername + "\',\'uuid\':\'" + CsGetUuid(uuid, a.playername, api) + "\'}";
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.inputcommand(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onFormSelect, x =>
            {
                var a       = BaseEvent.getFrom(x) as FormSelectEvent;
                string list = $"{{\'playername\':\'{a.playername}\',\'selected\':{a.selected},\'uuid\':\'{a.uuid}\',\'formid\':\'{a.formid}\'}}";
                CallLuaFunc(LuaFun, func =>
                {
                    var re = func.formselect(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onUseItem, x =>
            {
                var a       = BaseEvent.getFrom(x) as UseItemEvent;
                string list = $"{{\'playername\':\'{a.playername}\',\'itemid\':\'{a.itemid}\',\'itemaux\':\'{a.itemaux}\',\'itemname\':\'{a.itemname}\',\'XYZ\':[{a.XYZ.x},{a.XYZ.y},{a.XYZ.z}],\'postion\':[{a.position.x},{a.position.y},{a.position.z}],\'blockname\':\'{a.blockname}\',\'blockid\':\'{a.blockid}\'}}";
                var re      = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.useitem(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onPlacedBlock, x =>
            {
                var a       = BaseEvent.getFrom(x) as PlacedBlockEvent;
                string list = $"{{\'playername\':\'{a.playername}\',\'blockid\':\'{a.blockid}\',\'blockname\':\'{a.blockname}\',\'XYZ\':[{a.XYZ.x},{a.XYZ.y},{a.XYZ.z}],\'postion\':[{a.position.x},{a.position.y},{a.position.z}],\'dimensionid\':\'{a.dimensionid}\'}}";
                var re      = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.placeblock(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onLevelExplode, x =>
            {
                var a       = BaseEvent.getFrom(x) as LevelExplodeEvent;
                string list = $"{{\'explodepower\':\'{a.explodepower}\',\'blockid\':\'{a.blockid}\',\'blockname\':\'{a.blockname}\',\'entity\':\'{a.entity}\',\'entityid\':\'{a.entityid}\',\'dimensionid\':\'{a.dimensionid}\',\'postion\':[{a.position.x},{a.position.y},{a.position.z}]}}";
                var re      = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.levelexplode(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onNpcCmd, x =>
            {
                var a       = BaseEvent.getFrom(x) as NpcCmdEvent;
                string list = $"{{\'npcname\':\'{a.npcname}\',\'actionid\':\'{a.actionid}\',\'actions\':\'{a.actions}\',\'dimensionid\':\'{a.dimensionid}\',\'entity\':\'{a.entity}\',\'entityid\':\'{a.entityid}\',\'postion\':[{a.position.x},{a.position.y},{a.position.z}]}}";

                var re = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.npccmd(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onBlockCmd, x =>
            {
                var a       = BaseEvent.getFrom(x) as BlockCmdEvent;
                string list = $"{{\'cmd\':\'{a.cmd}\',\'dimensionid\':\'{a.dimensionid}\',\'postion\':[{a.position.x},{a.position.y},{a.position.z}],\'type\':\'{a.type}\',\'tickdelay\':\'{a.tickdelay}\'}}";
                var re      = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.blockcmd(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onPistonPush, x =>
            {
                var a       = BaseEvent.getFrom(x) as PistonPushEvent;
                var re      = true;
                string list = $"{{\'targetposition\':[{a.targetposition.x},{a.targetposition.y},{a.targetposition.z}],\'blockid\':\'{a.blockid}\',\'blockname\':\'{ a.blockname }\',\'dimensionid\':\'{ a.dimensionid }\',\'targetblockid\':\'{a.targetblockid}\',\'targetblockname\':\'{a.targetblockname}}}";
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.pistonpush(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onStartOpenChest, x =>
            {
                var a       = BaseEvent.getFrom(x) as StartOpenChestEvent;
                string list = $"{{\'playername\':\'{a.playername}\',\'blockid\':\'{a.blockid}\',\'blockname\':\'{a.blockname}\',\'XYZ\':[{a.XYZ.x},{a.XYZ.y},{a.XYZ.z}],\'postion\':[{a.position.x},{a.position.y},{a.position.z}],\'dimensionid\':\'{a.dimensionid}\'}}";
                var re      = true;
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.openchest(list);
                });
                return(re);
            });

            api.addBeforeActListener(EventKey.onStopOpenChest, x =>
            {
                var a       = BaseEvent.getFrom(x) as StopOpenChestEvent;
                string list = $"{{\'playername\':\'{a.playername}\',\'blockid\':\'{a.blockid}\',\'blockname\':\'{a.blockname}\',\'XYZ\':[{a.XYZ.x},{a.XYZ.y},{a.XYZ.z}],\'postion\':[{a.position.x},{a.position.y},{a.position.z}],\'dimensionid\':\'{a.dimensionid}\'}}";
                CallLuaFunc(LuaFun, func =>
                {
                    var re = func.closechest(list);
                });
                return(true);
            });

            api.addBeforeActListener(EventKey.onServerCmdOutput, x =>
            {
                var a       = BaseEvent.getFrom(x) as ServerCmdOutputEvent;
                var re      = true;
                string list = $"{{\'output\':\'{a.output.Replace("\n", null).Replace("\'", "\\\'")}\'}}";
                CallLuaFunc(LuaFun, func =>
                {
                    re = func.server_cmdoutput(list);
                });
                return(re);
            });
        }
Exemplo n.º 2
0
        public static void RunLua(MCCSAPI api)
        {
            Console.ForegroundColor = ConsoleColor.White;
            logo.logooo();
            List <IntPtr> uuid          = new List <IntPtr>();
            string        plugins       = "plugins/";
            string        settingdir    = plugins + "settings/";      // 固定配置文件目录
            string        settingpath   = settingdir + "IronLua.ini"; // 固定配置文件
            string        setingversion = "1.0.3";
            string        CONFIG        = $@"#配置文件版本,请勿乱动
CONFIGVERSION={setingversion}
#插件加载文件夹,若不存在需要自行创建
LUAPATH=./plugins/ilua/
#库文件所在文件夹,ILR会自动补全lua的库
LIBPATH=./plugins/ilua/Lib/
#是否自动更新
AUTOUPDATE=True
#调试模式是否开启
DBG=False";

            if (!File.Exists(settingpath))
            {
                Directory.CreateDirectory(settingdir);
                File.WriteAllText(settingpath, CONFIG);
            }
            string[] ini = File.ReadAllLines(settingpath);
            foreach (string i in ini)
            {
                if (!i.StartsWith("#"))
                {
                    CONFIGINI[i.ToString().Split('=')[0]] = i.ToString().Split('=')[1];
                    Console.WriteLine("[ILUAR] {0} >> {1}", i.ToString().Split('=')[0], i.ToString().Split('=')[1]);
                }
            }
            if (CONFIGINI["CONFIGVERSION"] != setingversion)
            {
                File.WriteAllText(settingpath, $"#配置文件版本,请勿乱动\nCONFIGVERSION={setingversion}\n#插件加载文件夹,若不存在需要自行创建\nLUAPATH={CONFIGINI["LUAPATH"]}\n#库文件所在文件夹,ILR会自动补全lua的库\nLIBPATH={CONFIGINI["LIBPATH"]}\n#是否自动更新\nAUTOUPDATE={CONFIGINI["AUTOUPDATE"]}\n#调试模式是否开启\nDBG={CONFIGINI["DBG"]}");
            }

            new Thread(() =>
            {
                var htool   = new ILR.ToolFunc();
                var libupde = JsonConvert.DeserializeObject <Lib>(htool.HttpGet(GetHasa("aHR0cDovL3dpa2kuc2Jhb29yLmNvb2wvTGliL2xpYnMuanNvbg==")));
                foreach (string i in libupde.libs)
                {
                    if (!File.Exists((string)CONFIGINI["LIBPATH"] + i.ToString()))
                    {
                        File.WriteAllText((string)CONFIGINI["LIBPATH"] + i.ToString(), htool.HttpGet(GetHasa("aHR0cDovL3dpa2kuc2Jhb29yLmNvb2wvTGliLw==") + i.ToString()));
                    }
                }
                Console.WriteLine("[ILUAR] lua库更新检查完成");
                Console.WriteLine("[ILUAR] 云端库最后更新时间:" + libupde.time);
            }).Start();
            new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(300000);
                    Console.WriteLine(GetHasa("5qyi6L+O5L2/55SoSXJvbkx1YVJ1bm5lciDkvZzogIXvvJpTYmFvb3IgZ2l0aHViOmh0dHBzOi8vZ2l0aHViLmNvbS9TYmFvb3ItZmx5L0NTUi1Jcm9uTHVhUnVubmVy"));
                    Console.WriteLine(GetHasa("SXJvbkx1YVJ1bm5lciBsb2FkZWQhIGF1dGhvcjogU2Jhb29yIGdpdGh1YjpodHRwczovL2dpdGh1Yi5jb20vU2Jhb29yLWZseS9DU1ItSXJvbkx1YVJ1bm5lcg=="));
                }
            }).Start();
            if (!Directory.Exists((string)CONFIGINI["LUAPATH"]))
            {
                Directory.CreateDirectory((string)CONFIGINI["LUAPATH"]);
            }
            if (!Directory.Exists((string)CONFIGINI["LIBPATH"]))
            {
                Directory.CreateDirectory((string)CONFIGINI["LIBPATH"]);
            }
            lua = new Lua();
            lua.State.Encoding     = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;
            lua["tool"]            = new ILR.ToolFunc();
            lua["mc"]     = api;
            lua["luaapi"] = new MCLUAAPI(api);
            lua.LoadCLRPackage();
            DirectoryInfo Allfolder = new DirectoryInfo((string)CONFIGINI["LUAPATH"]);

            Console.WriteLine("[ILUAR] Load! version = " + version);
            Console.WriteLine("[ILUAR] 本平台基于AGPL协议发行。");
            Console.WriteLine("[ILUAR] Reading Plugins...");
            foreach (FileInfo file in Allfolder.GetFiles("*.net.lua"))
            {
                try
                {
                    Console.WriteLine("[ILUAR] Load " + (string)CONFIGINI["LUAPATH"] + file.Name);
                    lua.DoFile(file.FullName);
                    Console.Write("[ILUAR] ");
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(file.Name + " load success");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[ILUAR] " + e.Message);
                    Console.WriteLine("[ILUAR] Filed to load " + file.Name);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
            foreach (FileInfo file in Allfolder.GetFiles("*.ilp.lua"))
            {
                try
                {
                    Console.WriteLine("[ILUAR] Load " + (string)CONFIGINI["LUAPATH"] + file.Name);
                    string text = ILRDecrypt(File.ReadAllText(file.FullName));
                    lua.DoString(text);
                    Console.Write("[ILUAR] ");
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(file.Name + " load success");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[ILUAR] " + e.Message.Replace("Base-64", "ILR-Protect"));
                    Console.WriteLine("[ILUAR] Filed to load " + file.Name);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
            api.addBeforeActListener(EventKey.onLoadName, x =>
            {
                var a = BaseEvent.getFrom(x) as LoadNameEvent;
                ptr.Add(a.uuid, a.playerPtr);
                return(true);
            });

            api.addBeforeActListener(EventKey.onPlayerLeft, x =>
            {
                var a = BaseEvent.getFrom(x) as PlayerLeftEvent;
                ptr.Remove(a.uuid);
                return(true);
            });
        }