420 lines
17 KiB
C#
420 lines
17 KiB
C#
using System.Linq;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Text;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using XLua;
|
||
using Debug = UnityEngine.Debug;
|
||
|
||
|
||
namespace BFEditor
|
||
{
|
||
public class BattleBMFontConfigSetting : EditorWindow
|
||
{
|
||
static Dictionary<char, char> specialSymbolDic = new Dictionary<char, char> {
|
||
['暴'] = 'b',
|
||
['击'] = 'j',
|
||
['擊'] = 'j',
|
||
['格'] = 'g',
|
||
['挡'] = 'd',
|
||
['擋'] = 'd',
|
||
['反'] = 'f',
|
||
['伤'] = 's',
|
||
['傷'] = 's',
|
||
['破'] = 'p',
|
||
['抵'] = 'D',
|
||
['消'] = 'x',
|
||
|
||
['~'] = ' ',
|
||
};
|
||
public const string BMFC_PATH_KEY = "BMFC_PATH_KEY";
|
||
public const string RES_PATH_KEY = "RES_PATH_KEY";
|
||
|
||
private string bmfcPath = ""; // bmfont配置文件的输入输出路径
|
||
private string resPath = ""; // 美术字的文件夹路径
|
||
private void OnEnable()
|
||
{
|
||
bmfcPath = PlayerPrefs.GetString(BMFC_PATH_KEY, "");
|
||
resPath = PlayerPrefs.GetString(RES_PATH_KEY, "");
|
||
}
|
||
|
||
private void OnGUI()
|
||
{
|
||
#region 设置bmfc文件路径
|
||
GUILayout.Space(18);
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label("选择bmfc文件路径");
|
||
GUILayout.TextField(bmfcPath, GUILayout.Width(300));
|
||
if (GUILayout.Button("选择", GUILayout.Width(80)))
|
||
{
|
||
string openPath = EditorUtility.OpenFilePanel("select bmfc path", bmfcPath, "");
|
||
if (openPath.CompareTo("") != 0){
|
||
bmfcPath = openPath;
|
||
}
|
||
}
|
||
GUILayout.EndHorizontal();
|
||
GUILayout.Space(10);
|
||
#endregion
|
||
#region 设置美术字文件夹路径
|
||
GUILayout.Space(18);
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label("选择美术字文件夹路径");
|
||
GUILayout.TextField(resPath, GUILayout.Width(300));
|
||
if (GUILayout.Button("选择", GUILayout.Width(80)))
|
||
{
|
||
string openPath = EditorUtility.OpenFolderPanel("select texture path", resPath, "");
|
||
if (openPath.CompareTo("") != 0){
|
||
resPath = openPath;
|
||
}
|
||
}
|
||
GUILayout.EndHorizontal();
|
||
GUILayout.Space(10);
|
||
#endregion
|
||
|
||
#region 生成新的配置
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Space(190);
|
||
if (GUILayout.Button("生成新的bmfc", GUILayout.Width(110), GUILayout.Height(20)))
|
||
{
|
||
OnClickGenerate();
|
||
}
|
||
GUILayout.EndHorizontal();
|
||
GUILayout.Space(10);
|
||
#endregion
|
||
}
|
||
|
||
|
||
private void OnClickGenerate()
|
||
{
|
||
if(bmfcPath == "" || resPath == "")
|
||
{
|
||
EditorUtility.DisplayDialog("失败", "导出失败!\n请检查路径", "ok");
|
||
return;
|
||
}
|
||
PlayerPrefs.SetString(BMFC_PATH_KEY, bmfcPath);
|
||
PlayerPrefs.SetString(RES_PATH_KEY, resPath);
|
||
|
||
Dictionary<string, string> allFontDic = new Dictionary<string, string>();
|
||
Dictionary<int, int> specialUnicodeDic = new Dictionary<int, int>();
|
||
|
||
Dictionary<string, string> uniquePathDic = new Dictionary<string, string>();
|
||
List<string> uniquePathList = new List<string>();
|
||
BattleBMFontConfigList battleBMFontConfigList = new BattleBMFontConfigList();
|
||
battleBMFontConfigList.fileInfoList = new List<List<Dictionary<int, string>>>();
|
||
|
||
DirectoryInfo directoryInfo = new DirectoryInfo(resPath);
|
||
IEnumerable<DirectoryInfo> dirs = directoryInfo.EnumerateDirectories();
|
||
string fontAssetName;
|
||
foreach(var dir in dirs)
|
||
{
|
||
fontAssetName = "battle_" + dir.Name;
|
||
// 特殊处理数字以及特殊字符文件夹
|
||
if(dir.Name.Contains("number"))
|
||
{
|
||
IEnumerable<DirectoryInfo> numberDirs = dir.EnumerateDirectories();
|
||
foreach(var world in numberDirs)
|
||
{
|
||
fontAssetName = "battle_" + world.Name;
|
||
string uniFontName;
|
||
if(!allFontDic.TryGetValue(fontAssetName, out uniFontName))
|
||
{
|
||
allFontDic.Add(fontAssetName, fontAssetName);
|
||
}
|
||
DealLastDir(world, fontAssetName, uniquePathDic, uniquePathList, battleBMFontConfigList);
|
||
}
|
||
}
|
||
else if(dir.Name.Contains("special"))
|
||
{
|
||
IEnumerable<FileInfo> files = dir.EnumerateFiles();
|
||
foreach(var v in files)
|
||
{
|
||
if(v.Name.Contains(".png"))
|
||
{
|
||
string contentStr = GetFileContentStr(v.FullName);
|
||
string pathInfo;
|
||
if(!uniquePathDic.TryGetValue(contentStr, out pathInfo))
|
||
{
|
||
uniquePathDic.Add(contentStr, v.FullName);
|
||
uniquePathList.Add(v.FullName);
|
||
}
|
||
|
||
int index = uniquePathList.Count - 1;
|
||
if(pathInfo != null)
|
||
{
|
||
for(int i = 0; i < uniquePathList.Count; i ++)
|
||
{
|
||
if(uniquePathList[i] == pathInfo)
|
||
{
|
||
index = i;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
char[] nameArray = v.Name.ToArray();
|
||
char name;
|
||
if(specialSymbolDic.TryGetValue(nameArray[0], out name))
|
||
{
|
||
nameArray[0] = specialSymbolDic[nameArray[0]];
|
||
}
|
||
int unicodeID = Convert.ToInt32(nameArray[0]);
|
||
specialUnicodeDic.Add(index, unicodeID);
|
||
|
||
if(battleBMFontConfigList.fileInfoList.Count < uniquePathList.Count)
|
||
{
|
||
List<Dictionary<int, string>> infoList = new List<Dictionary<int, string>>();
|
||
battleBMFontConfigList.fileInfoList.Add(infoList);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else{
|
||
// 正常处理
|
||
IEnumerable<DirectoryInfo> languageDirs = dir.EnumerateDirectories();
|
||
foreach(var languageDir in languageDirs)
|
||
{
|
||
fontAssetName = "battle_" + dir.Name + "_" + languageDir.Name;
|
||
string uniFontName;
|
||
if(!allFontDic.TryGetValue(fontAssetName, out uniFontName))
|
||
{
|
||
allFontDic.Add(fontAssetName, fontAssetName);
|
||
}
|
||
IEnumerable<DirectoryInfo> worldDirs = languageDir.EnumerateDirectories();
|
||
foreach(var world in worldDirs)
|
||
{
|
||
DealLastDir(world, fontAssetName, uniquePathDic, uniquePathList, battleBMFontConfigList);
|
||
// IEnumerable<FileInfo> files = world.EnumerateFiles();
|
||
// foreach(var v in files)
|
||
// {
|
||
// if(v.Name.Contains(".png"))
|
||
// {
|
||
// string contentStr = GetFileContentStr(v.FullName);
|
||
// string pathInfo;
|
||
// if(!uniquePathDic.TryGetValue(contentStr, out pathInfo))
|
||
// {
|
||
// uniquePathDic.Add(contentStr, v.FullName);
|
||
// uniquePathList.Add(v.FullName);
|
||
// }
|
||
|
||
// int index = uniquePathList.Count - 1;
|
||
// if(pathInfo != null)
|
||
// {
|
||
// for(int i = 0; i < uniquePathList.Count; i ++)
|
||
// {
|
||
// if(uniquePathList[i] == pathInfo)
|
||
// {
|
||
// index = i;
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// char[] nameArray = v.Name.ToArray();
|
||
// int unicodeID = Convert.ToInt32(nameArray[0]);
|
||
// Dictionary<int, string> info = new Dictionary<int, string>();
|
||
// info.Add(unicodeID, fontAssetName);
|
||
// if(battleBMFontConfigList.fileInfoList.Count < uniquePathList.Count)
|
||
// {
|
||
// List<Dictionary<int, string>> infoList = new List<Dictionary<int, string>>();
|
||
// infoList.Add(info);
|
||
// battleBMFontConfigList.fileInfoList.Add(infoList);
|
||
// }
|
||
// else
|
||
// {
|
||
// battleBMFontConfigList.fileInfoList[index].Add(info);
|
||
// }
|
||
// }
|
||
// }
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
foreach (int index in specialUnicodeDic.Keys)
|
||
{
|
||
if(index > battleBMFontConfigList.fileInfoList.Count - 1)
|
||
{
|
||
BF.BFLog.LogError("数组越界,请检查");
|
||
return;
|
||
}
|
||
|
||
List<Dictionary<int, string>> infoList = new List<Dictionary<int, string>>();
|
||
foreach (string fontAsset in allFontDic.Keys)
|
||
{
|
||
Dictionary<int, string> info = new Dictionary<int, string>();
|
||
info.Add(specialUnicodeDic[index], fontAsset);
|
||
infoList.Add(info);
|
||
}
|
||
battleBMFontConfigList.fileInfoList[index] = infoList;
|
||
}
|
||
}
|
||
string baseConfig = "";
|
||
string[] allLines = File.ReadAllLines(bmfcPath);
|
||
for(int index = 0; index < allLines.Length; index ++ )
|
||
{
|
||
if(allLines[index].Contains("icon="))
|
||
{
|
||
break;
|
||
}
|
||
baseConfig = baseConfig + allLines[index] + "\n";
|
||
if(allLines[index].Contains("# imported icon images"))
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
using(StreamWriter sw = File.CreateText(bmfcPath))
|
||
{
|
||
string formatStr = "icon=\"{0}\",{1},0,0,0";
|
||
sw.WriteLine(baseConfig);
|
||
for(int index = 0; index < uniquePathList.Count; index ++ )
|
||
{
|
||
sw.WriteLine(String.Format(formatStr, uniquePathList[index], index));
|
||
}
|
||
}
|
||
string json = ParseBattleBMFontConfigMap.ToStr(battleBMFontConfigList);
|
||
string jsonPath = Path.GetDirectoryName(bmfcPath) + "/battleBMFontConfigList.bbcl";
|
||
using (StreamWriter sw = File.CreateText(jsonPath))
|
||
{
|
||
sw.WriteLine(json);
|
||
}
|
||
Close();
|
||
EditorUtility.DisplayDialog("成功", "重新生成bmfc成功!", "ok");
|
||
}
|
||
|
||
void DealLastDir(DirectoryInfo dir, string fontAssetName, Dictionary<string, string> uniquePathDic, List<string> uniquePathList, BattleBMFontConfigList battleBMFontConfigList)
|
||
{
|
||
IEnumerable<FileInfo> files = dir.EnumerateFiles();
|
||
foreach(var v in files)
|
||
{
|
||
if(v.Name.Contains(".png"))
|
||
{
|
||
string contentStr = GetFileContentStr(v.FullName);
|
||
string pathInfo;
|
||
if(!uniquePathDic.TryGetValue(contentStr, out pathInfo))
|
||
{
|
||
uniquePathDic.Add(contentStr, v.FullName);
|
||
uniquePathList.Add(v.FullName);
|
||
}
|
||
|
||
int index = uniquePathList.Count - 1;
|
||
if(pathInfo != null)
|
||
{
|
||
for(int i = 0; i < uniquePathList.Count; i ++)
|
||
{
|
||
if(uniquePathList[i] == pathInfo)
|
||
{
|
||
index = i;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
char[] nameArray = v.Name.ToArray();
|
||
char name;
|
||
if(specialSymbolDic.TryGetValue(nameArray[0], out name))
|
||
{
|
||
nameArray[0] = specialSymbolDic[nameArray[0]];
|
||
}
|
||
int unicodeID = Convert.ToInt32(nameArray[0]);
|
||
Dictionary<int, string> info = new Dictionary<int, string>();
|
||
info.Add(unicodeID, fontAssetName);
|
||
if(battleBMFontConfigList.fileInfoList.Count < uniquePathList.Count)
|
||
{
|
||
List<Dictionary<int, string>> infoList = new List<Dictionary<int, string>>();
|
||
infoList.Add(info);
|
||
battleBMFontConfigList.fileInfoList.Add(infoList);
|
||
}
|
||
else
|
||
{
|
||
battleBMFontConfigList.fileInfoList[index].Add(info);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
string GetFileContentStr(string filePath)
|
||
{
|
||
byte[] bytes = File.ReadAllBytes(filePath);
|
||
string content = "";
|
||
for(int i = 0; i< bytes.Length; i++)
|
||
{
|
||
content = content + bytes[i].ToString();
|
||
}
|
||
return content;
|
||
}
|
||
|
||
[MenuItem("资源工具/字体/BattleBMFontConfigSetting", false, 100)]
|
||
public static void OpenBattleBMFontConfigSettingWindow()
|
||
{
|
||
var window = (BattleBMFontConfigSetting)EditorWindow.GetWindowWithRect(typeof(BattleBMFontConfigSetting),
|
||
new Rect(Screen.width / 2, Screen.height / 2, 500, 120), true);
|
||
window.Show();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
[Serializable]
|
||
public class BattleBMFontConfigList {
|
||
public List<List<Dictionary<int, string>>> fileInfoList;
|
||
}
|
||
|
||
public static class ParseBattleBMFontConfigMap
|
||
{
|
||
public static string ToStr(BattleBMFontConfigList battleBMFontConfigList)
|
||
{
|
||
string str = String.Empty;
|
||
List<List<Dictionary<int, string>>> fileInfoList = battleBMFontConfigList.fileInfoList;
|
||
for(int i = 0; i < fileInfoList.Count; i ++)
|
||
{
|
||
if(str != String.Empty)
|
||
{
|
||
str = str + "@";
|
||
}
|
||
for (var j = 0; j < fileInfoList[i].Count; j++)
|
||
{
|
||
Dictionary<int, string> infoDic = fileInfoList[i][j];
|
||
foreach (var unicodeID in infoDic.Keys)
|
||
{
|
||
str = str + unicodeID + "=" + infoDic[unicodeID];
|
||
break;
|
||
}
|
||
if(j != fileInfoList[i].Count - 1)
|
||
{
|
||
str = str + ",";
|
||
}
|
||
}
|
||
}
|
||
return str;
|
||
}
|
||
|
||
public static BattleBMFontConfigList ToObject(string str)
|
||
{
|
||
BattleBMFontConfigList battleBMFontConfigList = new BattleBMFontConfigList();
|
||
battleBMFontConfigList.fileInfoList = new List<List<Dictionary<int, string>>>();
|
||
string[] fileInfoList = str.Replace("\n", String.Empty).Replace("\r", String.Empty).Split('@');
|
||
for(int i = 0; i < fileInfoList.Length; i++)
|
||
{
|
||
List<Dictionary<int, string>> list1 = new List<Dictionary<int, string>>();
|
||
battleBMFontConfigList.fileInfoList.Add(list1);
|
||
string[] infoList = fileInfoList[i].Split(',');
|
||
for(int j = 0; j < infoList.Length; j++)
|
||
{
|
||
string[] info = infoList[j].Split('=');
|
||
Dictionary<int, string> infoDic = new Dictionary<int, string>();
|
||
infoDic.Add(int.Parse(info[0]), info[1]);
|
||
battleBMFontConfigList.fileInfoList[i].Add(infoDic);
|
||
}
|
||
}
|
||
return battleBMFontConfigList;
|
||
}
|
||
}
|
||
}
|