private void RunLua(object obj) { try { string luaFile = obj as string; Lua luaVM = new Lua(); luaVM.RegisterFunction("FMSignal", this, this.GetType().GetMethod("FMSignal")); luaVM.RegisterFunction("Single", this, this.GetType().GetMethod("Single")); luaVM.RegisterFunction("Output", this, this.GetType().GetMethod("Output")); luaVM.RegisterFunction("OutputLine", this, this.GetType().GetMethod("OutputLine")); luaVM.RegisterFunction("Sleep", this, this.GetType().GetMethod("Sleep")); luaVM.RegisterFunction("SetAcVolt", this, this.GetType().GetMethod("SetAcVolt")); luaVM.RegisterFunction("SetDcVolt", this, this.GetType().GetMethod("SetDcVolt")); luaVM.RegisterFunction("SetAcCurrent", this, this.GetType().GetMethod("SetAcCurrent")); luaVM.RegisterFunction("SetDcCurrent", this, this.GetType().GetMethod("SetDcCurrent")); luaVM.RegisterFunction("MeterValue", this, this.GetType().GetMethod("MeterValue")); luaVM.DoFile(luaFile); luaVM.Close(); } catch (Exception) { } finally { /* this.Invoke(new MethodInvoker(delegate { this.buttonLuaRun.Text = "运行脚本"; })); * */ if (this.IsHandleCreated) { MethodInvoker meth = new MethodInvoker(delegate { this.buttonLuaRun.Text = "运行脚本"; }); this.BeginInvoke(meth); } } }
/// <summary> /// Does simple syntax checking of a LUA plugin. NOTE: Doe not find runtime errors! /// </summary> /// <param name="code">Code to compile</param> /// <param name="suppressExceptions">Set to false if you would like to see /// the exception thrown, good for debugging</param> /// <returns>True if compiling to bytecode is successful</returns> public static Boolean canCompile(String code, Boolean suppressExceptions = true) { Lua tempLua = null; try { tempLua = new Lua(); tempLua.LoadString(code, "CompileCheck"); } catch (Exception ex) { if (suppressExceptions) return false; else throw ex; } finally { if (tempLua != null) tempLua.Close(); } return true; }
internal void loadFile(String filename) { if (string.IsNullOrEmpty(luaFile)) throw new Exception("No Lua file specified"); if (!System.IO.File.Exists(filename)) throw new Exception("Cannot find file " + filename); // store the file data this.theData = System.IO.File.ReadAllBytes(filename); interp = new Lua(); this.usedSetData = false; #region register predefined functions & variables // register the functions interp.RegisterFunction("read", this, this.GetType().GetMethod("read")); interp.RegisterFunction("read2", this, this.GetType().GetMethod("read2")); interp.RegisterFunction("readWORD", this, this.GetType().GetMethod("readWORD")); interp.RegisterFunction("readlWORD", this, this.GetType().GetMethod("readlWORD")); interp.RegisterFunction("readDWORD", this, this.GetType().GetMethod("readDWORD")); interp.RegisterFunction("readlDWORD", this, this.GetType().GetMethod("readlDWORD")); interp.RegisterFunction("readString", this, this.GetType().GetMethod("readString")); interp.RegisterFunction("readString2", this, this.GetType().GetMethod("readString2")); interp.RegisterFunction("stringToInt", this, this.GetType().GetMethod("stringToInt")); interp.RegisterFunction("setData", this, this.GetType().GetMethod("setData")); interp.RegisterFunction("setData2", this, this.GetType().GetMethod("setData2")); interp.RegisterFunction("addData", this, this.GetType().GetMethod("addData")); interp.RegisterFunction("addData2", this, this.GetType().GetMethod("addData2")); interp.RegisterFunction("toHexadecimal", this, this.GetType().GetMethod("ToHexadecimal")); // register the default variables interp["filesize"] = theData.Length; interp["filename"] = filename.Substring(filename.LastIndexOfAny(new char[] { '\\', '/' }) + 1); interp["filepath"] = filename.Substring(0, filename.LastIndexOfAny(new char[] { '\\', '/' }) + 1); #endregion // read the plugin try { interp.DoFile(this.luaFile); // if the variable invalid has been set, display it, and reset the data if (interp["invalid"] != null) { MainWindow.ShowError(interp.GetString("invalid")); this.bData.Data = new byte[0]; } else { #region format // try to read the format if (interp["format"] != null) { int format = (int)(double)interp["format"]; switch (this.Parent.BindingType) { case BindingType.GRAPHICS: if (format < 1 || format > 7) MainWindow.ShowError("Plugin warning: the format of the graphics should range from 1 up to and including 7.\n" + "Value " + format + " is ignored"); else GraphicsData.GraphFormat = (GraphicsFormat)format; break; case BindingType.PALETTE: if (format < 5 || format > 7) MainWindow.ShowError("Plugin warning: the format of the palette should range from 5 up to and including 7.\n" + "Value " + format + " is ignored"); else PaletteData.PalFormat = (PaletteFormat)format; break; } } #endregion if (this.parentBinding.BindingType == BindingType.PALETTE || (int)GraphicsData.GraphFormat >= 5) { #region palette order if (interp["order"] != null) { string s = ((string)interp["order"]).ToUpper(); if (s.Length != 3 || !s.Contains("R") || !s.Contains("G") || !s.Contains("B")) MainWindow.ShowError("Plugin warning: the colour order is invalid.\n" + "Value " + s + " is ignored."); else PaletteData.PalOrder = (PaletteOrder)Enum.Parse(typeof(PaletteOrder), s); } #endregion #region alpha location if (interp["alphaAtStart"] != null) { bool atStart = (bool)interp["alphaAtStart"]; PaletteData.AlphaSettings.Location = atStart ? AlphaLocation.START : AlphaLocation.END; } #endregion #region ignore alpha if (interp["ignoreAlpha"] != null) { bool ignore = (bool)interp["ignoreAlpha"]; PaletteData.AlphaSettings.IgnoreAlpha = ignore; } #endregion #region enable stretch if (interp["enableAlphaStrech"] != null) { bool enable = (bool)interp["enableAlphaStrech"]; PaletteData.AlphaSettings.Stretch = enable; } #endregion #region stretch settings if (interp["alphaStretch"] != null) { LuaTable t = interp.GetTable("alphaStretch"); if (t["min"] != null) PaletteData.AlphaSettings.Minimum = (byte)(double)t["min"]; else if(t[0] != null) PaletteData.AlphaSettings.Minimum = (byte)(double)t[0]; if (t["max"] != null) PaletteData.AlphaSettings.Maximum = (byte)(double)t["max"]; else if (t[0] != null) PaletteData.AlphaSettings.Maximum = (byte)(double)t[1]; } #endregion } #region Endianness if (interp["bigendian"] != null) { bool isBE = (bool)interp["bigendian"]; switch (this.Parent.BindingType) { case BindingType.GRAPHICS: GraphicsData.IsBigEndian = isBE; break; case BindingType.PALETTE: PaletteData.IsBigEndian = isBE; break; } } #endregion #region tile size if (interp["tilesize"] != null) { System.Drawing.Point size, oldSize; switch(this.parentBinding.BindingType) { case BindingType.GRAPHICS: size = oldSize = GraphicsData.TileSize; break; case BindingType.PALETTE: size = oldSize = PaletteData.TileSize; break; default: throw new Exception(string.Format("Unknown BindingType {0:s}", this.parentBinding.BindingType.ToString())); } try { LuaTable t = interp.GetTable("tilesize"); if (t["x"] != null) size.X = (int)(double)t["x"]; else if (t[0] != null) size.X = (int)(double)t[0]; if (t["y"] != null) size.Y = (int)(double)t["y"]; else if (t[1] != null) size.Y = (int)(double)t[1]; switch (this.parentBinding.BindingType) { case BindingType.GRAPHICS: GraphicsData.TileSize = size; break; case BindingType.PALETTE: PaletteData.TileSize = size; break; } } catch (Exception) { MainWindow.ShowError("Plugin warning: invalid tile size provided.\nValue is ignored."); switch (this.parentBinding.BindingType) { case BindingType.GRAPHICS: GraphicsData.TileSize = oldSize; break; case BindingType.PALETTE: PaletteData.TileSize = oldSize; break; } } } #endregion #region tiled if (interp["tiled"] != null) { bool tl; switch (this.parentBinding.BindingType) { case BindingType.GRAPHICS: tl = GraphicsData.Tiled; try { GraphicsData.Tiled = (bool)interp["tiled"]; } catch (Exception) { MainWindow.ShowError("Plugin warning: invalid tile size provided.\nValue is ignored."); GraphicsData.Tiled = tl; } break; case BindingType.PALETTE: tl = PaletteData.Tiled; try { PaletteData.Tiled = (bool)interp["tiled"]; } catch (Exception) { MainWindow.ShowError("Plugin warning: invalid tile size provided.\nValue is ignored."); PaletteData.Tiled = tl; } break; default: throw new Exception(string.Format("Unknown BindingType {0:s}", this.parentBinding.BindingType.ToString())); } } #endregion if (this.Parent.BindingType == BindingType.GRAPHICS) { #region width if (interp["width"] != null) { uint origW = GraphicsData.Width; try { uint w = (uint)(double)interp["width"]; GraphicsData.Width = w; } catch (Exception) { GraphicsData.Width = origW; MainWindow.ShowError("Plugin warning: invalid width.\n" + "Value " + interp.GetString("width") + " is ignored."); } } #endregion #region height if (interp["height"] != null) { uint origH = GraphicsData.Height; try { uint h = (uint)(double)interp["height"]; GraphicsData.Height = h; } catch (Exception) { GraphicsData.Height = origH; MainWindow.ShowError("Plugin warning: invalid height.\n" + "Value " + interp.GetString("height") + " is ignored."); } } #endregion } if (!usedSetData) this.bData.Data = this.theData; if (interp["warning"] != null) MainWindow.ShowWarning(interp.GetString("warning")); } } catch (Exception e) { MainWindow.ShowError("Plugin error: \n" + e.Message); } // close and delete the interpreter, and delete the data interp.Close(); interp = null; this.theData = null; }
/// <summary> /// /// </summary> public void Close() { _lua.Close(); }
private void RunLua(object obj) { try { string luaFile = obj as string; Lua luaVM = new Lua(); luaVM.RegisterFunction("FMSignal", this, this.GetType().GetMethod("FMSignal")); luaVM.RegisterFunction("Single", this, this.GetType().GetMethod("Single")); luaVM.RegisterFunction("Output", this, this.GetType().GetMethod("Output")); luaVM.RegisterFunction("OutputLine", this, this.GetType().GetMethod("OutputLine")); luaVM.RegisterFunction("Sleep", this, this.GetType().GetMethod("Sleep")); luaVM.DoFile(luaFile); luaVM.Close(); } catch (Exception) { } finally { this.Invoke(new MethodInvoker(delegate { this.lua_button.Text = "运行脚本"; })); } }
internal void Stop() { lua.Close(); }