C# - lua格式化

上一篇我们将C#数据结构转换成Lua table,我们发现它只是一行字符串,不方便阅读,我们这篇将其格式化。


#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using UnityEditor.ProjectWindowCallback;
using System.Text.RegularExpressions;
using System;
using System.Text;
using TileEditor;

public class JsonToLua {
    public static void OpenOrCreateLua(Dictionary<string, object>dict, string path)
    {
        FileStream file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);

        string gameDataStr = "local config = ";

        string luaStr = HELua.Encode (dict);

        luaStr = SwitchToLuaFormat (luaStr);
        gameDataStr += luaStr;
        gameDataStr += "\nreturn config";

        // clear text
        file.Seek(0, SeekOrigin.Begin);
        file.SetLength(0);

        char[] data = gameDataStr.ToCharArray();
        Encoder myEncoder = Encoding.UTF8.GetEncoder ();
        byte[] myByte = new byte[data.Length];
        myEncoder.GetBytes (data, 0, data.Length, myByte, 0, true);
        file.Write (myByte, 0, myByte.Length);
        file.Close ();
    }
    /// lua格式化
    private static string SwitchToLuaFormat(string gameData)
    {
        int tabCount = 0;
        string luaStr = "";
        for (int i = 0; i < gameData.Length; i++) {
            if (gameData [i] == '{') {
                if (IsNeedLineFeedByLeftBracket (gameData, i)) {
                    tabCount++;
                    luaStr += gameData [i] + "\n" + GetTabStringByTabCount (tabCount);
                    if (i < gameData.Length-1 && gameData[i + 1] == ' ') {
                        i++;
                    }
                } else {
                    luaStr += gameData [i];
                }
            } else if (gameData[i] == '}') {
                if (IsNeedLineFeedByRightBracket (gameData, i - 1)) {
                    tabCount--;
                    luaStr += "\n" + GetTabStringByTabCount (tabCount) + gameData [i];
                    if (i < gameData.Length-1 && gameData[i + 1] == ' ') {
                        i++;
                    }
                } else {
                    luaStr += gameData [i];
                }
                if (i < gameData.Length - 1 && gameData[i + 1] == ',') {
                    luaStr += gameData [i + 1] + "\n" + GetTabStringByTabCount (tabCount);
                    i++;
                    if (i < gameData.Length - 1 && gameData [i + 1] == ' ') {
                        i++;
                    }
                }
            } else if(gameData[i] == ','){
                if (IsNeedLineFeedByComma (gameData, i)) {
                    luaStr += gameData [i] + "\n" + GetTabStringByTabCount (tabCount);
                    if (i < gameData.Length - 1 && gameData [i + 1] == ' ') {
                        i++;
                    }
                } else {
                    luaStr += gameData [i];
                }
            } else {
                luaStr += gameData [i];
            }
        }
        return luaStr;
    }

    /// 遇到左括号是否要换行
    private static bool IsNeedLineFeedByLeftBracket(string gameData, int index)
    {
        if (index == gameData.Length) {
            return false;
        }
        for (int i = index + 1; i < gameData.Length; i++) {
            if (gameData[i] == '}') {
                return false;
            } else if ((gameData[i] == '=') || (gameData[i] == '{')) {
                return true;
            }
        }
        return false;
    }

    /// 遇到右括号是否要换行
    private static bool IsNeedLineFeedByRightBracket(string gameData, int index)
    {
        if (index == 0) {
            return false;
        }
        for (int i = index; i >= 0; i--) {
            if (gameData [i] == '=' || gameData[i] == '}') {
                return true;
            } else if (gameData [i] == '{') {
                return false;
            }
        }
        return false;
    }

    /// 遇到逗号是否需要换行
    private static bool IsNeedLineFeedByComma(string gameData, int index)
    {
        if (index == 0) {
            return false;
        }
        for (int i = index - 1; i >= 0; i--) {
            if (gameData[i] == '=') {
                return true;
            } else if (gameData[i] == '{') {
                return false;
            }
        }
        return false;
    }


    private static string GetTabStringByTabCount(int tabCount)
    {
        string tabStr = "";
        if (tabCount >= 0) {
            for (int i = 0; i < tabCount; i++) {
                tabStr += "\t";
            }
        } else {
            Debug.LogError ("tab count are lower then zero!");
        }

        return tabStr;
    }
}    
#endif

现在就是正常的lua缩进后的文件,方便我们阅读。

你可能感兴趣的:(C#,lua,unity,格式化)