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缩进后的文件,方便我们阅读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值