1. 程式人生 > >轉表工具:Bytes轉Lua

轉表工具:Bytes轉Lua

最近專案在做C#程式碼轉換Lua程式碼重構,配置表用的Bytes型別檔案,所以寫了一個Bytes轉Lua的轉表工具,方便直接讀取配置表資料,奈何主程不讓用。以下是程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace BytesTableToLuaTable
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入要轉Lua的表名:");
            string tableName = Console.ReadLine();
            string readPath = "F:\\stargate/LsjProduct/Client/EditorData/Package/Table/"+ tableName;//讀取檔案路徑
            string writePath = "F:\\stargate/LsjProduct/Client/LuaScript/Config/"+ tableName;//寫入檔案資料夾
            ReadFirstLine(readPath,writePath);
        }

        public static void ReadFirstLine(string readPath,string writePath) {
            StreamReader sr = new StreamReader(readPath + ".bytes", Encoding.Default);
            String line;
            Dictionary<int,string> statusDict = new Dictionary<int, string>();
            int i = 0;
            while ((line = sr.ReadLine()) != null)
            {
                if (i == 0)
                {
                    string firstString1 = line.ToString().Replace("  "," ");
                    string firstString2 = firstString1.Replace("\n", " ");
                    string firstString3 = firstString2.Replace("\t", " ");
                    string[] firstStringArr = firstString3.Split(' ');
                    string[] strArr = new string[firstStringArr.Length];
                    for (int j = 0; j < strArr.Length; j++)
                    {
                        strArr[j] = firstStringArr[j].Trim();
                        statusDict[j] = strArr[j];
                        Console.WriteLine(strArr[j]);
                    }
                }
                break;
            }
            WriteByStreamWriter(readPath, writePath, statusDict);
        }

        public static void WriteByStreamWriter(string readPath,string writePath,Dictionary<int,string> statusDict) {
            foreach (var item in statusDict)
            {
                Console.Write(item.Key+":"+item.Value+"\n");
            }
            StreamReader sr = new StreamReader(readPath+".bytes", Encoding.Default);
            String line;
            FileStream fs = new FileStream(writePath+".lua", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            string []pathStringArr = readPath.Split('/');
            string tableName = pathStringArr[pathStringArr.Length - 1];
            sw.Write(tableName+ " = { \n");//寫入表名
            int i = 0;
            while ((line = sr.ReadLine()) != null)
            {
                if (i == 0) { i = 1;continue; }

                string firstString1 = line.ToString().Replace("  ", " ");
                string firstString2 = firstString1.Replace("\n", " ");
                string firstString3 = firstString2.Replace("\t", " ");
                string[] firstStringArr = firstString3.Split(' ');
                string[] strArr = new string[firstStringArr.Length];
          
                for (int j = 0; j < strArr.Length; j++)
                {
                    strArr[j] = firstStringArr[j].Trim();
                }

                string writeStr= "\t[" + i + "] = { ";
                for (int k = 0; k< strArr.Length; k++)
                {
                    writeStr += statusDict[k] + " = \"" + strArr[k] + "\" , ";
                }
                writeStr += "\"},\n";
                sw.Write(writeStr);
                i++;
            }
            sw.Write("}");//結束括號
            //清空緩衝區
            sw.Flush();
            //關閉流
            sw.Close();
            fs.Close();
        }
        
    }
}

工具能轉換單個配置表文件成Lua檔案,當然也有考慮將整個資料夾內檔案批量轉換,不過暫時用不上,就不寫了。