Example #1
0
 public void Init(string strAPIInfoFile)
 {
     m_strCode = "";
     m_strAPIInfoFile = strAPIInfoFile;
     m_lua = new Lua();
     m_lua.DoFile(m_strAPIInfoFile);
     m_luatable = m_lua.GetTable("APIInfo");
     m_nClassCount = m_luatable.Keys.Count;
     m_strClass = new string[m_nClassCount];
     m_strClassInCode = new string[m_nClassCount];
     m_strIniFileName = Application.StartupPath + "\\Plugins\\LuaCheck\\API.temp.lua";
     m_lua1 = new Lua();
     m_lua1.DoFile(m_strIniFileName);
     int n = 0;
     foreach (string str in m_luatable.Keys)
     {
         m_strClass[n] = (string)(((LuaTable)m_luatable[str])["Name"]);
         n++;
     }
     
     //object ob;
     //foreach (ob in m_luatable)
     //{
     //    strClass[] = ((LuaTable)ob)["Name"];
     
     //}
     //object ob = m_luatable["1"];
     //object ob1 = ((LuaTable)ob)["Name"];
 }
Example #2
0
        public static LuaTable CreateTable(this LuaInterface.Lua lua, string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            lua.NewTable(tableName);
            return(lua.GetTable(tableName));
        }
Example #3
0
        private LuaTable ToLuaTable(MemoryTable table)
        {
            String name = "memory_table_" + new Random().Next().ToString();

            lua.NewTable(name);
            LuaTable newTable = lua.GetTable(name);

            foreach (KeyValuePair <String, Object> obj in newTable)
            {
                if (obj.GetType() == typeof(MemoryTable))
                {
                    newTable[obj.Key] = ToLuaTable((MemoryTable)obj.Value);
                }
                else
                {
                    newTable[obj.Key] = obj.Value;
                }
            }
            return(newTable);
        }
Example #4
0
        /// <summary>
        /// Loads a world from a lua file
        /// </summary>
        /// <param name="filename">file to load the world from</param>
        /// <returns>true on success</returns>
        public bool load(string filename)
        {
            this.fname = filename;

            Lua lua = new Lua();
            var result = lua.DoFile(filename);
            foreach (DictionaryEntry member in lua.GetTable("world_data"))
            {
                if (member.Key.ToString() == "width")
                {
                    this.width = Convert.ToInt32(member.Value);
                }
                else if (member.Key.ToString() == "height")
                {
                    this.height = Convert.ToInt32(member.Value);
                }
                if (member.Key.ToString() == "paths")
                {
                    collision_segment = new ArrayList();
                    LuaTable paths = (LuaTable)member.Value;
                    foreach (DictionaryEntry path in paths)
                    {
                        ArrayList collision_path = new ArrayList();
                        LuaTable p = (LuaTable)path.Value;
                        foreach (DictionaryEntry point in p)
                        {
                            LuaTable point_data = (LuaTable)point.Value;
                            int x = Convert.ToInt32(point_data.Values.Cast<double>().ElementAt(0) * ppu);
                            int y = Convert.ToInt32(point_data.Values.Cast<double>().ElementAt(1) * ppu);
                            Console.WriteLine("X:" + x + ", Y:" + y);
                            collision_path.Add(new Point(x, y));
                        }
                        collision_segment.Add(collision_path);
                    }

                }
            }
            return true;
        }
Example #5
0
        private void LoadAccessory()
        {
            RunScript(@"datainfo\accessoryid.lub");
            RunScript(@"datainfo\accname.lub");

            var tbl = _luaparser.GetTable("ACCESSORY_IDs");

            foreach (DictionaryEntry de in tbl)
            {
                if (!Statics.Accessories.ContainsKey(Convert.ToInt32(de.Value)))
                {
                    Statics.Accessories.Add(Convert.ToInt32(de.Value), new Tuple <string, string>(de.Key.ToString(), ""));
                }
            }

            tbl = _luaparser.GetTable("AccNameTable");
            foreach (DictionaryEntry de in tbl)
            {
                if (Statics.Accessories.ContainsKey(Convert.ToInt32(de.Key)))
                {
                    Statics.Accessories[Convert.ToInt32(de.Key)] = new Tuple <string, string>(Statics.Accessories[Convert.ToInt32(de.Key)].Item1, de.Value.ToString());
                }
            }
        }
Example #6
0
        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;
        }
Example #7
0
        private void Export(string strMap)
        {
            string strPath = @"D:\Kingsoft\sword3-products\trunk\client\scripts\Map\" + strMap;
            Lua mLua = new Lua();
            mLua.DoFile(strPath + @"\include\对话.ls");

            string[] as_head = new string[]
            { 
                "COMMON_NPC_STRING",
                "COMMON_DOODAD_STRING",
                "COMMON_ITEM_STRING",
                "COMMON_TRAP_STRING",
                "COMMON_OTHER_STRING",

                "WANHUA_DOODAD_STRING",
                "WANHUA_ITEM_STRING",
                "WANHUA_COMMON_NPC_STRING"
            };           

            //遍历文件
            foreach(FileInfo fi in new DirectoryInfo(strPath).GetFiles("*.*", SearchOption.AllDirectories))
            {
                string strCode = Helper.FileToString(fi.FullName);
                if(fi.Name.ToLower().EndsWith(".lua"))
                {
                    //遍历表名
                    foreach(string strHead in as_head)
                    {
                        LuaTable lt_head = mLua.GetTable(strHead);
                        if (lt_head == null) continue;
                        //遍历某个表
                        foreach (string strKey in lt_head.Keys)
                        {
                            if (lt_head[strKey] is LuaTable)
                            {
                                string strPreDim = "";
                                LuaTable lt = lt_head[strKey] as LuaTable;
                                strPreDim = "LS_" + strKey + " = {";
                                foreach(object substring in lt.Values)
                                {
                                    strPreDim += "\r\n\t\"" + substring.ToString().Replace("\n", "\\n") + "\",";
                                }
                                if(strCode.IndexOf(strHead + "[\"" + strKey + "\"]") != -1)
                                    strCode = strPreDim.TrimEnd(new char[] { ',' }) + "\r\n}\r\n" + strCode;
                                strCode = strCode.Replace("math.random(", "Random(");
                                strCode = strCode.Replace(strHead + "[\"" + strKey + "\"]", "LS_" + strKey);                         
                            }
                            else if(lt_head[strKey] is string)
                            {
                                strCode = strCode.Replace(strHead + "[\"" + strKey + "\"]", "\"" + lt_head[strKey].ToString().Replace("\n", "\\n") + "\"");
                            }
                        }
                    }
                    strCode = strCode.Replace("Include(\"scripts/Map/" + strMap + "/include/对话.ls\");", "");
                }
                else if (fi.Name == "对话.ls")
                {
                    //donothing
                    continue;
                }
                else if (fi.Name.ToLower().EndsWith(".ls"))
                {
                    //对于用户自定义ls,不管了
                }
                else
                {
                    //SVN文件,不导
                    continue;
                }
                
                //转换后的东东存成文件
                string strOutPath = fi.FullName.Replace(strPath, @"D:\xxx\" + strMap);
                Helper.WriteStringToFile(strCode, strOutPath);
            }


        }
Example #8
0
 public static LuaTable CreateTable(this LuaInterface.Lua lua)
 {
     lua.NewTable("my");
     return(lua.GetTable("my"));
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public LuaTable CreateTable(string name)
 {
     _lua.NewTable(name);
     return(_lua.GetTable(name));
 }
Example #10
0
 public void Run()
 {
     Lua lua = new Lua();
     try
     {
         lua.DoFile(m_strExecFile);
     }
     catch (Exception Ex)
     {
         Console.WriteLine(Ex.Message);
         return;
     }
     LuaTable luaTable = lua.GetTable("EVENT_LIST");
     foreach (object Key in luaTable.Keys)
     {
         string strEventFunction = (string)luaTable[Key];
         //整理参数格式为   “param1, param2, param3, ”
         int nIndexP = strEventFunction.IndexOf("(") + 1;
         int nIndexE = strEventFunction.IndexOf(")");
         string strFunctionName = strEventFunction.Substring(0, nIndexP - 1);
         string strParam = strEventFunction.Substring(nIndexP, nIndexE - nIndexP) + ", ";
         nIndexP = 0;
         nIndexE = 0;
         strFunctionName = strFunctionName + "(";
         while (true)
         {
             nIndexP = nIndexE;
             nIndexE = strParam.IndexOf(", ", nIndexP) + 2;
             if (nIndexE == 1)
             {
                 break;
             }
             string strParamName = strParam.Substring(nIndexP, nIndexE - 2 - nIndexP);
             string sb = GetStringFromFile("GetParam", strParamName, "nil");
             if (sb == "nil")
             {
                 MessageBox.Show("配置文件中无法找到逻辑检查代入参数:" + strParamName);
             }
             strFunctionName = strFunctionName + sb + ", ";
         }
         if (strEventFunction.IndexOf("(") + 1 != strEventFunction.IndexOf(")"))
         {
             strFunctionName = strFunctionName.Substring(0, strFunctionName.Length - 2);
         }
         strFunctionName = strFunctionName + ")";
         try
         {
              lua.DoString(strFunctionName);
         }
         catch (Exception Ex)
         {   
             //过滤因为调用了不存在的Event引起的错误
             string strFunc = strFunctionName.Substring(0, strFunctionName.IndexOf("("));
             int nIndexEx = Ex.Message.IndexOf(strFunc);
             if (nIndexEx == -1)
             {
                 Console.WriteLine(Ex.Message);
             }
         }
     }
 }
Example #11
0
 private string GetStringFromFile(string strTableName, string strFieldName, string strDefReturn)
 {
     Lua lua = new Lua();
     lua.DoFile(m_strIniFile);
     LuaTable tbl = lua.GetTable(strTableName);
     string strReturn = "";
     try
     {
         strReturn = (string)tbl[strFieldName];
     }
     catch (Exception e)
     {
         return strDefReturn;
     }
     return strReturn == null ? strDefReturn : strReturn;
 }
Example #12
0
        /// <summary>
        /// 初始化逻辑管理器
        /// </summary>
        public void InitLogicManager()
        {
            string filePath = Path.Combine(Application.StartupPath, configFileName);
            
            if (File.Exists(filePath))
            {
                Lua lua = new Lua();

                lua.DoFile(filePath);
                LuaTable configTable = lua.GetTable("Config") as LuaTable;

                string[] diagramNameArray = new string[maxDiagramCount];

                foreach (object key in configTable.Keys)
                {
                    LuaTable logicManagerTable = configTable[key] as LuaTable;

                    if (logicManagerTable != null)
                    {
                        // 读取逻辑管理器信息
                        string logicManagerFilePath = logicManagerTable["LogicManagerFilePath"] as string;
                        string logicManagerClassName = logicManagerTable["LogicManagerClassName"] as string;
                        LogicBaseManager logicManager = null;

                        if (!string.IsNullOrEmpty(logicManagerFilePath) && !string.IsNullOrEmpty(logicManagerClassName))
                        {
                            logicManager = LoadLogicManager(logicManagerFilePath, logicManagerClassName);
                        }
                        else
                        {
                            logicManager = new LogicBaseManager();
                        }

                        // 读取显示名称
                        string logicManagerDisplayName = logicManagerTable["DisplayName"] as string;

                        if (!string.IsNullOrEmpty(logicManagerDisplayName))
                        {
                            logicManager.DisplayName = logicManagerDisplayName;
                        }                        

                        // 读取脚本生成插件信息
                        string compilerFilePath = logicManagerTable["CompilerFilePath"] as string;
                        string compilerClassName = logicManagerTable["CompilerClassName"] as string;

                        if (!string.IsNullOrEmpty(compilerFilePath) && !string.IsNullOrEmpty(compilerClassName))
                        {
                            logicManager.InitCompiler(compilerFilePath, compilerClassName);
                        }

                        // 检查是否是默认分类
                        int index = (int)(double)logicManagerTable["Index"];
                        diagramNameArray[index - 1] = logicManagerDisplayName;                        

                        // 读取数据元信息
                        LuaTable dataElementGroupTable = logicManagerTable["DataElement"] as LuaTable;

                        if (dataElementGroupTable != null)
                        {
                            foreach (object graphType in dataElementGroupTable.Keys)
                            {
                                LuaTable dataElementConfigTable = dataElementGroupTable[graphType] as LuaTable;

                                if (dataElementConfigTable != null)
                                {
                                    string dataElementFilePath = dataElementConfigTable["FilePath"] as string;
                                    string dataElementClassName = dataElementConfigTable["ClassName"] as string;

                                    logicManager.RegistDataElementInfo(graphType as string, dataElementFilePath, dataElementClassName);
                                }                                
                            }
                        }

                        logicManagerDictionary[logicManager.DisplayName] = logicManager;
                    }                    
                }

                foreach (string displayName in diagramNameArray)
                {
                    if (displayName != null)
                    {
                        diagramNameList.Add(displayName);
                    }
                }                
            }            
        }