public static void RunIronPython(MCCSAPI api) { List <IntPtr> uuid = new List <IntPtr>(); const String path = "./ipy"; bool pfapi = false; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("[IPYR] IronPython插件运行平台开始装载。"); if (!File.Exists("./ipy/NOWEB")) { Console.WriteLine("[IPYR] 登记中,请稍候..."); string porrt = FindPort("server.properties"); string urldata = HttpGet("*****", ""); var webmsg = JsonConvert.DeserializeObject <Urldata>(urldata); if (webmsg.load) { Console.WriteLine("[IPYR] 登记成功,IronPythonRunner开始装载..."); } else { Console.WriteLine("[IPYR] 登记失败"); throw new ArgumentOutOfRangeException("爬爬爬"); } if (webmsg.version != version) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[IPYR] IronPythonRunner有新版本需要您更新!"); Console.WriteLine("[IPYR] 当前版本:" + version + ",新版本:" + webmsg.version); Console.ForegroundColor = ConsoleColor.White; } string[] PArray = webmsg.message.Split('*'); foreach (string i in PArray) { Console.WriteLine("[IPYR]|网络公告| " + i.ToString()); } localip = webmsg.IP; } if (File.Exists("./csr/PFEssentials.csr.dll")) { Console.WriteLine("[IPYR] 找到PFessentials,加载PFessAPI"); pfapi = true; } if (!File.Exists("./IronPython27.zip")) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[IPYR] 无法找到依赖库,请将IronPython27.zip放到BDS根目录!"); Console.ForegroundColor = ConsoleColor.White; } Console.WriteLine("[IPYR] 读取插件列表"); var PyFun = new List <dynamic>(); DirectoryInfo Allfolder = new DirectoryInfo(path); var mc = new MCPYAPI(api); GC.KeepAlive(mc); var tool = new ToolFunc(); GC.KeepAlive(tool); var _pfapi = new PFessAPI(); GC.KeepAlive(_pfapi); foreach (FileInfo file in Allfolder.GetFiles("*.net.py")) { try { Console.WriteLine("[IPYR] Load\\" + file.Name); ScriptEngine pyEngine = Python.CreateEngine(); var Libpath = pyEngine.GetSearchPaths(); List <string> LST = new List <string>(Libpath.Count) { "C:\\Program Files\\IronPython 2.7\\Lib", ".\\IronPython27.zip" }; pyEngine.SetSearchPaths(LST.ToArray()); pyEngine.CreateModule("mc"); pyEngine.CreateModule("tool"); if (pfapi) { pyEngine.CreateModule("pfapi"); } dynamic py = pyEngine.ExecuteFile(file.FullName); py.SetVariable("mc", new MCPYAPI(api)); py.SetVariable("tool", new ToolFunc()); if (pfapi) { py.SetVariable("pfapi", new PFessAPI()); } var main = py.load_plugin(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(file.Name + " Load Successful"); Console.ForegroundColor = ConsoleColor.White; PyFun.Add(py); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.WriteLine("Failed to load " + file.Name); Console.ForegroundColor = ConsoleColor.White; } } var tmp = new Action <string, dynamic>((k, obj) => { Console.WriteLine("[IPYR] 接受对象 " + k); foreach (var py in PyFun) { py.SetVariable(k, obj); } }); GC.KeepAlive(tmp); var FuncPointer = Marshal.GetIUnknownForObject(tmp); Console.Write("*" + FuncPointer); api.setSharePtr("ipyr", FuncPointer); #region 监听器 api.addBeforeActListener(EventKey.onLoadName, x => { var a = BaseEvent.getFrom(x) as LoadNameEvent; uuid.Add(a.playerPtr); ptr.Add(a.uuid, a.playerPtr); CallPyFunc(PyFun, func => { CsPlayer p = new CsPlayer(api, a.playerPtr); string list = "{\'playername\':\'" + a.playername + "\',\'uuid\':\'" + a.uuid + "\',\'xuid\':\'" + a.xuid + "\',\'IPport\':\'" + p.IpPort + "\'}"; //string[] list = { a.playername, a.uuid, a.xuid }; 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); CallPyFunc(PyFun, 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("ipy ")) { string[] sArray = a.cmd.Split(new char[2] { ' ', ' ' }); if (sArray[1] == "info") { string msg = "窗体关闭,控制台已恢复"; MessageBox.Show("感谢使用IronPythonRunner\n作者:Sbaoor", "IronPythonRunner", MessageBoxButtons.OK, MessageBoxIcon.Information); Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss} "); Console.Write("INFO]["); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("IPYR"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("] " + msg); return(false); }