Main/Assets/Editor/DIY/FLSTool/FlsUtilsUITool.cs
2025-01-25 04:38:09 +08:00

259 lines
9.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using System.Security;
using Thousandto.Core.Base;
using Thousandto.Cfg;
using Thousandto.Cfg.Data;
using Thousandto.Plugins.Common.UniScene;
using UnityEngine;
namespace Thousandto.Package
{
/// <summary>
/// 生成fls文件的UI操作界面
/// </summary>
public class FlsUtilsUITool : EditorWindow
{
private string _lanDir = "CH";
public static void openWindow()
{
FlsUtilsUITool window = GetWindow<FlsUtilsUITool>("Fls工具");
window.maxSize = new Vector2(480, 480);
window.Show();
}
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("语言路径:");
_lanDir = EditorGUILayout.TextField(_lanDir, GUILayout.MaxWidth(200));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("生成fls"))
{
genBaseFlsFiles();
}
if (GUILayout.Button("生成fls并且拷贝分段资源"))
{
genBaseFlsFiles();
copyRes();
}
GUILayout.EndHorizontal();
}
public void GenBinaryRes(string flsPath)
{
_lanDir = flsPath;
genBaseFlsFiles();
#if !UNITY_WEBPLAYER
copyRes();
#endif
}
//生成基础fls文件fls1可能缺资源后面在来补
private void genBaseFlsFiles()
{
if (string.IsNullOrEmpty(_lanDir))
{
return;
}
FlsUtils.Build(_lanDir);
var newFlsList = Utils.GetFiles(FlsUtils.SubFlsRoot, "*.fls", SearchOption.TopDirectoryOnly, PkgConstDefine.CommonExceptionEx);
List<Dictionary<string, string>> flsDataDict = new List<Dictionary<string, string>>();
//读取所有fls内容存放到列表中
for (int i = 0; i < newFlsList.Count; ++i)
{
var flsContent = File.ReadAllLines(newFlsList[i]);
Dictionary<string, string> flsData = new Dictionary<string, string>();
//跳过第一条
for (int m = 1; m < flsContent.Length; ++m )
{
if (!flsData.Keys.Contains(flsContent[m]))
{
flsData.Add(flsContent[m], flsContent[m]);
}
}
flsDataDict.Add(flsData);
}
if (flsDataDict.Count == 0)
{
UnityEngine.Debug.LogError("------------生成fls失败----------");
return;
}
Dictionary<string, string> flsDataTemp = new Dictionary<string, string>();
for (int m = flsDataDict.Count - 1; m >= 1; --m)
{
flsDataTemp.Clear();
var itor = flsDataDict[m].Keys.GetEnumerator();
while (itor.MoveNext())
{
if (!flsDataDict[0].ContainsKey(itor.Current))
{
flsDataTemp.Add(itor.Current, itor.Current);
}
}
flsDataDict[m] = flsDataTemp;
//重新保存fls2
saveFls(flsDataTemp, string.Format(FlsUtils.SubFlsRoot + "fls{0}.fls", m + 1));
}
//从ResAll中读取所有资源然后从2 3段fls资源里面比对不存在2 3段中的资源放到fls1中
var allResList = Utils.GetFiles(FlsUtils.SubAllRoot, "*.*", SearchOption.AllDirectories, PkgConstDefine.CommonExceptionEx);
for (int i = 0; i < allResList.Count; ++i)
{
string file = allResList[i].Replace(FlsUtils.SubAllRoot, "").Replace("\\", "/").TrimStart('/');
bool existInOtherFls = false;
for (int m = flsDataDict.Count - 1; m >= 1; --m)
{
var flsData = flsDataDict[m].Keys;
if (flsData.Contains(file))
{
existInOtherFls = true;
break;
}
}
string lan = _lanDir;
if (_lanDir.IndexOf("/") > 0)
{
lan = _lanDir.Substring(0, _lanDir.IndexOf("/"));
}
//不包含在2 3 段包里面,则添加到首段包
if (!existInOtherFls && !flsDataDict[0].Keys.Contains(file))
{
//检查是否在对应的语言路径里面
if (checkLanDir(file, lan))
{
flsDataDict[0].Add(file, file);
}
}
}
//保存fls1
string saveFlsPath = FlsUtils.SubFlsRoot + "fls1.fls";
saveFls(flsDataDict[0], saveFlsPath);
}
private void saveFls(Dictionary<string, string> data, string filePath)
{
int count = 0;
int size = 0;
//计算Count和size
calcResCountAndSize(data, out count, out size);
StreamWriter sw = new StreamWriter(filePath);
sw.WriteLine(string.Format("{0},{1}", count, size));
var itor = data.Keys.GetEnumerator();
while (itor.MoveNext())
{
sw.WriteLine(itor.Current);
}
sw.Close();
}
//拷贝分段资源到对应的分段目录
private static void copyRes()
{
//删除旧的分段资源
Utils.DeleteFileOrDir("../SubResource/Android/Res1");
Utils.DeleteFileOrDir("../SubResource/Android/Res2");
Utils.DeleteFileOrDir("../SubResource/Android/Res3");
var newFlsList = Utils.GetFiles(FlsUtils.SubFlsRoot, "*.fls", SearchOption.TopDirectoryOnly, PkgConstDefine.CommonExceptionEx);
for (int i = 0; i < newFlsList.Count; ++i)
{
var flsContent = File.ReadAllLines(newFlsList[i]);
UnityEngine.Debug.LogError("Copy file cout: " + flsContent.Length);
for (int m = 1; m < flsContent.Length; ++m)
{
string toDir = string.Format("../SubResource/Android/Res{0}/", i + 1);
string sourceFile = FlsUtils.SubAllRoot + flsContent[m];
if (!File.Exists(sourceFile))
{
UnityEngine.Debug.LogError("文件不存在: " + sourceFile);
continue;
}
string toFile = toDir + flsContent[m];
string toDirParent = toFile.Substring(0, toFile.LastIndexOf("/"));
if (!Directory.Exists(toDirParent))
{
Directory.CreateDirectory(toDirParent);
}
File.Copy(sourceFile, toFile, true);
}
}
}
//计算count和size
private static void calcResCountAndSize(Dictionary<string, string> data, out int count, out int size)
{
count = 0;
size = 0;
var itor = data.Keys.GetEnumerator();
while (itor.MoveNext())
{
string file = FlsUtils.SubAllRoot + itor.Current;
if (File.Exists(file))
{
FileInfo fileInfo = new FileInfo(file);
size += (int)fileInfo.Length;
count++;
}
}
}
//检查资源是否在对应的语言路径里面
private static bool checkLanDir(string file, string lan)
{
//GameUI
if (file.IndexOf("GameUI") >= 0 && file.IndexOf("GameUI_" + lan) == -1)
{
return false;
}
//UI texture
if (file.IndexOf("Texture/UI") >= 0 && file.IndexOf("Texture/UI/" + lan) == -1)
{
return false;
}
//npc声音
if (file.IndexOf("Sound/Speech") >= 0 && file.IndexOf("Sound/Speech/" + lan) == -1)
{
return false;
}
//视频
if (file.IndexOf(".mp4") >= 0 && file.IndexOf(lan + ".mp4") == -1)
{
return false;
}
//GameCfgData
if (file.IndexOf("GameCfgData") >= 0 && file.IndexOf("GameCfgData_" + lan) == -1)
{
return false;
}
return true;
}
}
}