Example #1
0
        private bool CallLuaImport(LuaEx lua, string strTableName, string filename, ref string[] fields, ref string[] values)
        {
            //LuaEx lua = new LuaEx();
            lua["Conn"] = m_conn; // 注册sql连接
            String luafile = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\import.lua";
            if (!File.Exists(luafile))
                return false;
            try
            {
                lua.DoFile(luafile);
                LuaFunction fun = lua.GetFunction("onimport");
                if (fun != null)
                {
                    ImportInfoHelper helper = new ImportInfoHelper();
                    ArrayList feildlist = new ArrayList();
                    if (fields != null)
                        feildlist.AddRange(fields);

                    helper.Fields = feildlist;
                    helper.Values = values;
                    object[] retobjs = fun.Call(strTableName, Path.GetDirectoryName(filename), filename, helper);

                    //fields = (string[])feildlist.ToArray(typeof(string));
                    fields = (string[])(helper.Fields).ToArray(typeof(string));
                    values = helper.Values;

                    if (retobjs != null && retobjs.GetLength(0) > 0)
                    {
                        bool ret = (bool)retobjs[0];
                        return ret;
                    }
                }
            }
            catch (Exception ex)
            {
                //ScriptDebugForm frm = Program.MainForm.DebugForm;
                //frm.OutputBox.Text += ex.Message + "\r\n";
                //frm.Show();
                //frm.BringToFront();
                Helper.AddLog(ex.Message);
                throw;
            }
            return false;
        }
Example #2
0
/*        public string[] CloneValue(string[] strvalues)
        {
            if (strvalues != null)
                return (string[])(strvalues.Clone());
            return null;
        }*/
        private bool CallLuaExport(string strLuaFile, string strTableName, ref string fields, ref string values)
        {
            LuaEx lua = new LuaEx();
            lua["Conn"] = m_conn; // 注册sql连接
            lua["RootDir"] = Program.RootDir;
            lua.DoString("function trace(txt)\r\n    MainForm.LogEditBox.Visible = true\r\n    MainForm.LogEditBox.OutputBox.Text = MainForm.LogEditBox.Text ..txt..'\\r\\n' \r\n end");
            lua.RegisterFunction("writefile", this, typeof(FileFolderHelper).GetMethod("WriteStringToFile"));
            lua.RegisterFunction("msgbox", this, typeof(TabExport).GetMethod("ShowMessage"));
            lua.RegisterFunction("GetDataTableRow", this, typeof(TabExport).GetMethod("GetDataTableRow"));
            //lua.RegisterFunction("clonevalues", this, typeof(TabExport).GetMethod("CloneValue"));            
            
            String luafile = strLuaFile;
            try
            {
                if (!File.Exists(luafile))
                    return false;
                try
                {
                    lua.DoFile(luafile);
                    LuaFunction fun = lua.GetFunction("onexport");
                    if (fun != null)
                    {
                        ImportInfoHelper helper = new ImportInfoHelper();
                        ArrayList feildlist = new ArrayList();
                        feildlist.AddRange(fields.Split(new char[] { '\t' }));
                        helper.Fields = feildlist;
                        helper.Values = values.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        object[] retobjs = fun.Call(m_strModlName, strTableName, Program.RootDir, helper);

                        if (retobjs != null && retobjs.GetLength(0) > 0)
                        {
                            if (retobjs[0] is bool)
                            {
                                StringBuilder strFields = new StringBuilder();
                                foreach (string strLine in feildlist)
                                {
                                    strFields.Append(strLine);
                                    strFields.Append('\t');
                                }
                                strFields.Remove(strFields.Length - 1, 1);
                                strFields.Append("\r\n");
                                fields = strFields.ToString();

                                StringBuilder strValues = new StringBuilder();
                                foreach (string strLine in helper.Values)
                                {
                                    strValues.Append(strLine);
                                    strValues.Append("\r\n");
                                }
                                values = strValues.ToString();

                                return (bool)retobjs[0];
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ScriptDebugForm frm = Program.MainForm.DebugForm;
                    frm.OutputBox.Text += ex.Message + "\r\n";
                    frm.Show();
                    frm.BringToFront();
                }
            }
            finally
            {
                lua.Dispose();
            }
            return false;
        }