0. 前言
项目已经开源,源代码相关文章在见下方链接,本文为该项目的结构解释。
[编程工具]UnityGameFramework配表多语言导出工具(零)使用概述以及源码
https://blog.csdn.net/Blue_carrot_/article/details/130280639
1. 项目结构
项目的结构也比较简单,如下
文件 | 内容 |
---|---|
Program.cs | 程序入口 |
Exportor.cs | 主程序,配表内容导出处理 |
Property.cs | 属性,配表属性检查以及格式化处理 |
Config.cs | 配置,配置加载 |
Log.cs | uitl,控制台内容打印 |
XmlUtils.cs | uitl,xml格式化处理 |
XssUtil.cs | uitl,xlsx文件打开 |
然后呢目前就讲比较简单的部分,包括uitl、config、log部分,后面主程序再讲别的。
2. Log
这个文件主要是为了方便做日志打印内容,其实也可以叫做LogUtil。
- 主要函数:PrintLog做正常打印,PrintError做一个错误打印,PrintCellError具体表格位置错误打印
- 控制台字体颜色:为了打印的内容比较明显,做一下字体颜色区分,错误时Console.ForegroundColor = ConsoleColor.Red;
- 位置字符转化:通常表格会用字母表示列,比如第五列通常叫“E”,所以ConvertToLetter做一下变换,方便查看。
如下:
public static void PrintLog(string log)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("[log]" + log);
}
public static void PrintError(string log)
{
Console.ForegroundColor = ConsoleColor.Red;
errorFlag = true;
Console.WriteLine("[error]" + log);
}
public static void PrintCellError(int row, int column, string errorMsg)
{
row++;
column++;
PrintError("第{0}行第{1}列({2},{3})-{4}", row, column, row, ConvertToLetter(column), errorMsg);
}
//https://docs.microsoft.com/zh-cn/office/troubleshoot/excel/convert-excel-column-numbers
private static string ConvertToLetter(int i)
{
int a, b;
string letter = "";
while (i > 0)
{
a = (int)((i - 1) / 26);
b = (i - 1) % 26;
letter = ((char)(b + 65)).ToString() + letter;
i = a;
}
return letter;
}
3. XmlUtil
这个主要是为了导出多语言时的回车,空格等字符的转化,防止xml格式错误,主要是EncodeString。文件是从fgui中copy的,文件中也包含DecodeString,需要可以到项目中再查看
private static string[] ESCAPES = new string[] {
"&", "&",
"<", "<",
">", ">",
"'", "'",
"\"", """,
"\t", "	",
"\n", "
",
"\r", "
"
};
public static void EncodeString(StringBuilder sb, int start, bool isAttribute = false)
{
int count;
int len = isAttribute ? ESCAPES.Length : 6;
for (int i = 0; i < len; i += 2)
{
count = sb.Length - start;
sb.Replace(ESCAPES[i], ESCAPES[i + 1], start, count);
}
}
4. xssutil
这个主要是为了xlsx文件的方便打开,毕竟本质就是一个表格导出工具,这个也是最重要的。我们其实需要的只是表格的字符数据,其他格式都不重要,所以只要能把字符都以常见的格式加载出来就可以了。比如加载为string[][],那这样就不用每次都从行列去获取xssfWorkBook中去获取。具体代码和内容之前也写过就不重复了,详见以下链接。
https://blog.csdn.net/Blue_carrot_/article/details/130279694
5. Config
配置我是安排在了xlsx文件中,比起在纯文本文件,这种可视程度高一些。而配置方式是打算用key+value的形式来配置,如下
所以我们获取相关配置的方式就可以这样处理,当然还要具体到某个配置,所以具体代码还是得到项目中看看。
public static void LoadFromXss()
{
var xssString = XssUtil.GetXssfData(ConfigLoadXssFile);
if (xssString == null )
{
Log.PrintError("获取配置文件失败,file = [{0}], configsheet = [{1}].", ConfigLoadXssFile, ConfigLoadSheet);
}
else
{
// 加载函数
Func< Dictionary<string, string>, string, string> loadStringFromDic = (Dictionary<string, string> dic, string key) =>
{
string result = "";
if (dic.ContainsKey(key))
{
result = dic[key];
}
else
{
Log.PrintError("获取配置或样本失败,key = [{0}].", key);
}
return result;
};
Func< Dictionary<string, string>, string, bool> loadBoolFromDic = (Dictionary<string, string> dic, string key) =>
{
bool result = false;
if (!bool.TryParse(loadStringFromDic(dic, key), out result))
{
Log.PrintError("获取配置或样本失败,类型应该为bool,key = [{0}].", key);
return result;
}
return result;
};
Func<Dictionary<string, string>, string, char> loadCharFromDic = (Dictionary<string, string> dic, string key) =>
{
char result;
if (!char.TryParse(loadStringFromDic(dic, key), out result))
{
Log.PrintError("获取配置或样本失败,类型应该为char,key = [{0}].", key);
return result;
}
return result;
};
// 加载配置
var attrDic = new Dictionary<string, string>();
var sheetString = xssString[ConfigLoadSheet];
for (int i = 1; i < sheetString.Count; i++)
{
// 忽略第一行和第一列
var rowString = sheetString[i];
if (rowString.Count >= 3)
{
attrDic.Add(rowString[1], rowString[2]);
}
}
LoadPath = loadStringFromDic(attrDic,"LoadPath");
IsReplaceTableAndEnterChar = loadBoolFromDic(attrDic, "IsReplaceTableAndEnterChar");
PropertyArrSplitChar = loadCharFromDic(attrDic, "PropertyArrSplitChar");
// ---还有一些其他的配置太多了,就不写完了
// 检查配置
if (!Directory.Exists(LoadPath))
{
Log.PrintError("表格文件加载路径错误,未找到对应路径,LoadPath=" + Config.LoadPath);
}
}
}
6. 结束
就先到这吧,剩下的Program、Property、Exportor内容就下次再讲吧。