public GUIAddon(string a_filePath) { m_lua = new Lua(); ((GameState)Game.getInstance().getCurrentState()).getGUI().registerFunctions(m_lua); m_lua.DoFile(a_filePath); m_onLoad = m_lua.GetFunction("OnLoad"); m_onUpdate = m_lua.GetFunction("OnUpdate"); m_onDraw = m_lua.GetFunction("OnDraw"); }
/// <summary> /// Unwaps an object comming from LuaInterface for use in DynamicLua. /// </summary> public static object UnWrapObject(object wrapped, Lua state, string name = null) { if (wrapped is LuaTable) return new DynamicLuaTable(wrapped as LuaTable, state, name); else if (wrapped is LuaFunction) return new DynamicLuaFunction(wrapped as LuaFunction); else if (wrapped is MulticastDelegate) return new DynamicLuaFunction(state.GetFunction(name)); else return wrapped; }
private bool IsBotTemplate(string file) { LuaInterface.Lua lua = new LuaInterface.Lua(); try { lua.DoFile(file); LuaFunction on_event = lua.GetFunction("OnEvent"); if(on_event == null) return false; return true; } catch (Exception e) { AppContext.WriteLog(e.Message); return false; } }
/// <summary> /// 新建文件 /// </summary> /// <param name="sender">事件发送者</param> /// <param name="e">事件参数</param> private void cmdTreePhFolder_newFile_Click(object sender, EventArgs e) { if (treePh.SelectedNode != null) { // 开始和lua交互 string strFullPath = treePh.SelectedNode.FullPath; Lua plua = new Lua(); object[] oRet = null; plua.DoString(DataBaseManager.GetDataBaseManager().GetScriptData(this.getFileIDsFromPath("Template")[0].ToString())); LuaFunction fun = plua.GetFunction("LoadParms"); if (fun != null) { object[] args = { strFullPath }; oRet = fun.Call(args); if (oRet == null) { return; } } string strControls = oRet[0] as string; // 孙韬返回的内容 object[] strParms; // 文件名 string strName = ""; string[] asp1 = { "|" }; ModelForm mf = new ModelForm(strControls.Split(asp1, StringSplitOptions.RemoveEmptyEntries)); if (mf.ShowDialog() == DialogResult.OK) { List<string> list = mf.InputList; if (list.Count >= 2) { strParms = list.ToArray(); strName = strParms[1] as string; string strCode = ""; fun = plua.GetFunction("LoadScript"); if (fun != null) { oRet = fun.Call(strParms); strCode = oRet[0] as string; } else { return; } // 交互完毕 if (string.IsNullOrEmpty(strName)) { return; } if (CheckNodeExist(treePh.SelectedNode, strName)) { MessageBox.Show("当前树结点已经存在!", "新建文件", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } Hashtable infoTable = new Hashtable(); infoTable["type"] = "file"; // 存数据库 DataBaseManager dbm = DataBaseManager.GetDataBaseManager(); string strPath = treePh.SelectedNode.FullPath; string[] asp = { "\\" }; string[] as_Path = strPath.Split(asp, StringSplitOptions.RemoveEmptyEntries); if (as_Path.Length < 2 && as_Path[0] == "scripts") { MessageBox.Show("暂时不支持在其他地图以外的目录建文件", "新建文件", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string id = dbm.CreateScriptData(string.Format("{0}\\{1}", strPath, strName)); if (id == null) { MessageBox.Show("新建文件失败!", "新建文件", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { // 新建成功,更新树缓存 treePh.Tag = dbm.GetScriptInformation(); } infoTable["id"] = id; infoTable["scriptType"] = "databaseFile"; TreeNode newNode = treePh.SelectedNode.Nodes.Add(strName, strName); newNode.ImageKey = "file"; newNode.SelectedImageKey = "file"; newNode.Tag = infoTable; newNode.ContextMenuStrip = this.popTreePh_File; treePh.SelectedNode.Expand(); treePh.SelectedNode = newNode; // 模拟点击节点 this.treePh_NodeMouseDoubleClick(this, new TreeNodeMouseClickEventArgs(newNode, MouseButtons.Left, 2, 1, 1)); EditForm fe = this.ActiveMdiChild as EditForm; // 模板内容写入 if (fe != null) { fe.luaEditBox.Text = strCode; } } } } }
/* * Sample test script that shows some of the capabilities of * LuaInterface */ public static void Main() { Console.WriteLine("Starting interpreter..."); Lua l=new Lua(); Console.WriteLine("Reading test.lua file..."); l.DoFile("test.lua"); double width=l.GetNumber("width"); double height=l.GetNumber("height"); string message=l.GetString("message"); double color_r=l.GetNumber("color.r"); double color_g=l.GetNumber("color.g"); double color_b=l.GetNumber("color.b"); Console.WriteLine("Printing values of global variables width, height and message..."); Console.WriteLine("width: "+width); Console.WriteLine("height: "+height); Console.WriteLine("message: "+message); Console.WriteLine("Printing values of the 'color' table's fields..."); Console.WriteLine("color.r: "+color_r); Console.WriteLine("color.g: "+color_g); Console.WriteLine("color.b: "+color_b); width=150; Console.WriteLine("Changing width's value and calling Lua function print to show it..."); l["width"]=width; l.GetFunction("print").Call(width); message="LuaNet Interface Test"; Console.WriteLine("Changing message's value and calling Lua function print to show it..."); l["message"]=message; l.GetFunction("print").Call(message); color_r=30; color_g=10; color_b=200; Console.WriteLine("Changing color's fields' values and calling Lua function print to show it..."); l["color.r"]=color_r; l["color.g"]=color_g; l["color.b"]=color_b; l.DoString("print(color.r,color.g,color.b)"); Console.WriteLine("Printing values of the tree table's fields..."); double leaf1=l.GetNumber("tree.branch1.leaf1"); string leaf2=l.GetString("tree.branch1.leaf2"); string leaf3=l.GetString("tree.leaf3"); Console.WriteLine("leaf1: "+leaf1); Console.WriteLine("leaf2: "+leaf2); Console.WriteLine("leaf3: "+leaf3); leaf1=30; leaf2="new leaf2"; Console.WriteLine("Changing tree's fields' values and calling Lua function print to show it..."); l["tree.branch1.leaf1"]=leaf1; l["tree.branch1.leaf2"]=leaf2; l.DoString("print(tree.branch1.leaf1,tree.branch1.leaf2)"); Console.WriteLine("Returning values from Lua with 'return'..."); object[] vals=l.DoString("return 2,3"); Console.WriteLine("Returned: "+vals[0]+" and "+vals[1]); Console.WriteLine("Calling a Lua function that returns multiple values..."); object[] vals1=l.GetFunction("func").Call(2,3); Console.WriteLine("Returned: "+vals1[0]+" and "+vals1[1]); Console.WriteLine("Creating a table and filling it from C#..."); l.NewTable("tab"); l.NewTable("tab.tab"); l["tab.a"]="a!"; l["tab.b"]=5.5; l["tab.tab.c"]=6.5; l.DoString("print(tab.a,tab.b,tab.tab.c)"); Console.WriteLine("Setting a table as another table's field..."); l["tab.a"]=l["tab.tab"]; l.DoString("print(tab.a.c)"); Console.WriteLine("Registering a C# static method and calling it from Lua..."); // Pause so we can connect with the debugger // Thread.Sleep(30000); l.RegisterFunction("func1",null,typeof(TestLuaInterface).GetMethod("func")); vals1=l.GetFunction("func1").Call(2,3); Console.WriteLine("Returned: "+vals1[0]); TestLuaInterface obj=new TestLuaInterface(); Console.WriteLine("Registering a C# instance method and calling it from Lua..."); l.RegisterFunction("func2",obj,typeof(TestLuaInterface).GetMethod("funcInstance")); vals1=l.GetFunction("func2").Call(2,3); Console.WriteLine("Returned: "+vals1[0]); Console.WriteLine("Testing throwing an exception..."); obj.ThrowUncaughtException(); Console.WriteLine("Testing catching an exception..."); obj.ThrowException(); Console.WriteLine("Testing inheriting a method from Lua..."); obj.LuaTableInheritedMethod(); Console.WriteLine("Testing overriding a C# method with Lua..."); obj.LuaTableOverridedMethod(); Console.WriteLine("Stress test RegisterFunction (based on a reported bug).."); obj.RegisterFunctionStressTest(); Console.WriteLine("Test structures..."); obj.TestStructs(); Console.WriteLine("Test Nullable types..."); obj.TestNullable(); Console.WriteLine("Test functions..."); obj.TestFunctions(); Console.WriteLine("Test event exceptions..."); obj.TestEventException(); }
//won't passing a params object be equivalent to just passing array as first param? Can't have arbitrary amount of arguments when calling a method (in code) during runtime? public static void processScript(String script) { Lua lua = new Lua(); lua[""] = ""; lua.GetFunction("orz").Call(); }
static void Main(string[] args) { Helper helper = Helper.GetHelper(); helper.RecordLogText("开始准备合并AIType.tab文件"); string fileName = Path.Combine(Application.StartupPath, @"config.ini"); if (File.Exists(fileName)) { string content = helper.GetFileContent(fileName); IniStructure iniStructure = IniStructure.ReadIniWithContent(content); string rootPath = iniStructure.GetValue("General", "RootDirectory"); string parameterCountString = iniStructure.GetValue("General", "ParameterCount"); int parameterCount = int.Parse(parameterCountString); // 需要导出一张旧的AIType表作为中间文件 fileName = Path.Combine(rootPath, @"settings\AIType.tab"); content = helper.GetFileContent(fileName); StringBuilder defaultLine = new StringBuilder(); defaultLine.Append("0\t"); for (int i = 0; i < parameterCount; i++) { defaultLine.Append(string.Format("{0}\t", iniStructure.GetValue("DefaultValue", string.Format("ParameterValue{0}", i)))); } defaultLine.Remove(defaultLine.Length - 1, 1); string newContent = CreateNewAITypeContent(content, defaultLine.ToString()); helper.SaveData(fileName, newContent); string mapCountString = iniStructure.GetValue("General", "MapCount"); int mapCount = int.Parse(mapCountString); List<string> mapList = new List<string>(); for (int i = 0; i < mapCount; i++) { string mapEnable = iniStructure.GetValue("MapList", string.Format("Enable{0}", i)); if (mapEnable == "1") { mapList.Add(iniStructure.GetValue("MapList", string.Format("MapName{0}", i))); } } try { Lua lua = new Lua(); // lua虚拟机 string luaFile = Path.Combine(Application.StartupPath, "exportScript.lua"); lua.DoFile(luaFile); LuaFunction function = lua.GetFunction("ExportAIData"); string filePath = Path.Combine(rootPath, @"settings\NpcTemplate.tab"); function.Call(filePath, mapList); helper.RecordLogText("AIType.tab合并完成"); } catch (Exception ex) { helper.RecordLogText("合并AIType文件时产生异常:" + ex.Message); } fileName = Path.Combine(Application.StartupPath, "log.txt"); helper.SaveData(fileName, helper.LogText); } else { helper.RecordLogText(string.Format("配置文件{0}不存在", fileName)); } }
/// <summary> /// /// </summary> /// <param name="fullPath"></param> /// <returns></returns> public LuaFunction GetFunction(string fullPath) { return(_lua.GetFunction(fullPath)); }