配置文件和lua源码使用zip格式

This commit is contained in:
fatiao 2025-03-01 20:19:37 +08:00
parent 7a0e410503
commit 86de9e61ca
7 changed files with 349 additions and 56 deletions

View File

@ -94,6 +94,13 @@ public static class Constants
public const string LuaLogicDir = "Assets/Lua";
public const string LuaPbDir = "Assets/Lua/Pb";
public const string PubSec = "Assets/ToLua/pubsec";
public const string LuaDirMergeFile = "MergeFile_ToLua.zip";
public const string LuaLogicDirMergeFile = "MergeFile_Lua.zip";
public const string LuaPbDirMergeFile = "MergeFile_Pb.zip";
public const string PubSecMergeFile = "MergeFile_PubSec.zip";
public const string XmlConfigMergeFile = "MergeFile_Xml.zip";
public const string CsvConfigMergeFile = "MergeFile_Csv.zip";
public const string ABLuaLogicDir = "Assets/Content/Lua";
public const string ABToLuaDir = "Assets/Content/ToLua";
@ -148,6 +155,7 @@ public static class Constants
public const string enemy_actor_born_point = "EnemyActorBornPoint";
public const string design_go = "Design";
public const string coming_camera_target = "Camera_on-off";
public const string merge_files_split = "%[~!@#$%split~!@#$%]%";
public const int c_delay_skillcasting_time = 2;

View File

@ -2,7 +2,11 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using Cysharp.Threading.Tasks;
using Mono.Xml;
using UnityEngine.AddressableAssets;
using Object = UnityEngine.Object;
@ -47,7 +51,19 @@ public class ConfigMgr : Singleton<ConfigMgr>
ConfigDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>(19);
ConfigLongthDictionary = new Dictionary<string, int>(19);
mXmlConfigDict = new Dictionary<string, string>();
GetConfigAsset();
GetConfigAsset().Forget();
var testStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<BattleFields>\n <BattleField battlePos=\"0;0;0\" battleForward=\"-8.742278E-08;0;-1\" battleRot=\"0;180;0\" battleDist=\"2\" spawnDist=\"28\">\n <CamCfg moveSpeed=\"10\" pos=\"0;3;20\" rotSpeed=\"150\" fovSpeed=\"60\" fov=\"75\" angle=\"0;0;0\">\n </CamCfg>\n <CamCfg moveSpeed=\"10\" pos=\"0;3;-20\" rotSpeed=\"150\" fovSpeed=\"60\" fov=\"75\" angle=\"0;180;0\">\n </CamCfg>\n </BattleField>\n <TeamBorn camRot=\"20;180;0\" p4=\"1.2;0;-58\" p0=\"1.63;2.38;-62\" forward=\"0;0;0\" camFov=\"50\" p1=\"-0.98;2.38;-62\" p3=\"-1.85;0.5;-61.5\" p2=\"2.45;0.5;-61.5\" camPos=\"0.33;6;-49\" p5=\"-0.55;0;-58\">\n </TeamBorn>\n</BattleFields>";
SecurityParser doc = new SecurityParser();
try
{
doc.LoadXml(testStr);
}
catch (System.Exception e)
{
DebugHelper.Assert(false, "exception = {0}", e.Message);
return;
}
}
public override void UnInit()
@ -59,26 +75,32 @@ public class ConfigMgr : Singleton<ConfigMgr>
base.UnInit();
}
private void GetConfigAsset()
private async UniTask GetConfigAsset()
{
if (InitFinished)
return;
Resources.LoadAsync<TextAsset>("files_xml").completed += op =>
{
ResourceRequest request = op as ResourceRequest;
var textAsset = request.asset as TextAsset;
var paths = textAsset.text.Split(',');
ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnLoadXmlCallback, Constants.XmlConfig, paths);
};
var xml_zip_data = await AssetsMgr.Instance.LoadLocalFileData(Application.streamingAssetsPath + "/" + Constants.XmlConfigMergeFile);
UnzipXmlFiles(xml_zip_data);
Resources.LoadAsync<TextAsset>("files_csv").completed += op =>
{
ResourceRequest request = op as ResourceRequest;
var textAsset = request.asset as TextAsset;
var paths = textAsset.text.Split(',');
ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.CsvConfig, paths);
};
var csv_zip_data = await AssetsMgr.Instance.LoadLocalFileData(Application.streamingAssetsPath + "/" + Constants.CsvConfigMergeFile);
UnzipCsvFiles(csv_zip_data);
// Resources.LoadAsync<TextAsset>("files_xml").completed += op =>
// {
// ResourceRequest request = op as ResourceRequest;
// var textAsset = request.asset as TextAsset;
// var paths = textAsset.text.Split(',');
// ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnLoadXmlCallback, Constants.XmlConfig, paths);
// };
//
// Resources.LoadAsync<TextAsset>("files_csv").completed += op =>
// {
// ResourceRequest request = op as ResourceRequest;
// var textAsset = request.asset as TextAsset;
// var paths = textAsset.text.Split(',');
// ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.CsvConfig, paths);
// };
}
public void ResetKeywords()
@ -214,6 +236,70 @@ public class ConfigMgr : Singleton<ConfigMgr>
}
#endif
}
private void UnzipXmlFiles(byte[] zipBytes)
{
using (MemoryStream ms = new MemoryStream(zipBytes))
{
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var fileName = entry.Name;
using (var entryStream = entry.Open())
{
byte[] fileBytes = new byte[entry.Length];
entryStream.Read(fileBytes, 0, fileBytes.Length);
string text = Encoding.UTF8.GetString(UnicodeUtil.ConvertToNonBOM(fileBytes));
mXmlConfigDict.Add(Path.GetFileNameWithoutExtension(fileName), text);
}
}
}
}
m_bInitXMLFinished = true;
CheckCfgOk();
}
private void UnzipCsvFiles(byte[] zipBytes)
{
string keyword = "LanguagePackage";
string keywordname = string.Format("{0}{1}", keyword, CurLangKey);
string curname = string.Empty;
using (MemoryStream ms = new MemoryStream(zipBytes))
{
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var fileName = entry.Name;
using (var entryStream = entry.Open())
{
byte[] fileBytes = new byte[entry.Length];
entryStream.Read(fileBytes, 0, fileBytes.Length);
string text = Encoding.UTF8.GetString(UnicodeUtil.ConvertToNonBOM(fileBytes));
curname = Path.GetFileNameWithoutExtension(fileName);
if (curname.Contains(keyword) && !curname.Equals(keywordname) || text.Length <= 0)
continue;
Dictionary<string, Dictionary<string, string>> ts = getData(curname, text);
if (ConfigDictionary.ContainsKey(curname))
{
ConfigDictionary[curname] = ts;
}
else
{
ConfigDictionary.Add(curname, ts);
}
}
}
}
}
m_bInitCSVFinished = true;
keyword = string.Empty;
keywordname = string.Empty;
curname = string.Empty;
CheckCfgOk();
}
void OnLoadXmlCallback(List<TextAsset> objs, string path_, string[] assetNames_)
{
@ -450,11 +536,12 @@ public class ConfigMgr : Singleton<ConfigMgr>
}
else
{
var path = tableName;
var textAsset = await AssetsMgr.Instance.LoadAddressableAssetAsync<TextAsset>(path);
var data = getData(tableName, textAsset.text);
ConfigDictionary.Add(tableName, data);
return data;
// var path = tableName;
// var textAsset = await AssetsMgr.Instance.LoadAddressableAssetAsync<TextAsset>(path);
// var data = getData(tableName, textAsset.text);
// ConfigDictionary.Add(tableName, data);
// return data;
return null;
}
}

View File

@ -1122,6 +1122,52 @@ public class AssetsMgr : SingletonMono<AssetsMgr>
return 1;
}
public async UniTask<byte[]> LoadLocalFileData(string path)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(path))
{
// 使用 UniTask 转换协程
var operation = webRequest.SendWebRequest().ToUniTask();
// 等待请求完成
await operation;
// 检查请求结果
if (webRequest.result == UnityWebRequest.Result.Success)
{
return webRequest.downloadHandler.data;
}
else
{
Debug.LogError("Error: " + webRequest.error);
return null;
}
}
}
public async UniTask<string> LoadLocalFileText(string path)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(path))
{
// 使用 UniTask 转换协程
var operation = webRequest.SendWebRequest().ToUniTask();
// 等待请求完成
await operation;
// 检查请求结果
if (webRequest.result == UnityWebRequest.Result.Success)
{
return webRequest.downloadHandler.text;
}
else
{
Debug.LogError("Error: " + webRequest.error);
return null;
}
}
}
public async UniTask<T> LoadAddressableAssetAsync<T>(string assetAddress) where T : UnityEngine.Object
{
AssetReference assetReference = new AssetReference(assetAddress);

View File

@ -795,7 +795,7 @@ public class BuffData
}
}
await LoadEventsDataAsync(gender,(int)jobType,jobStage,jobBranch);
LoadEventsData(gender,(int)jobType,jobStage,jobBranch);
}
public void SetFunData(string funStr)

View File

@ -143,16 +143,16 @@ public class SkillActionEventCfgMgr : Singleton<SkillActionEventCfgMgr>
void ReadEventCfg()
{
mBuffEvents.Clear();
// foreach (var p in ConfigMgr.Instance.XmlConfigDict)
// {
// string frameEventName = p.Key;
// if (!frameEventName.Contains("buff_")) continue;
//
// if(!ReadBuffEventFile(p.Value))
// {
// DebugHelper.LogError(frameEventName+" 加载有问题");
// }
// }
foreach (var p in ConfigMgr.Instance.XmlConfigDict)
{
string frameEventName = p.Key;
if (!frameEventName.Contains("buff_")) continue;
if(!ReadBuffEventFile(p.Value))
{
DebugHelper.LogError(frameEventName+" 加载有问题");
}
}
}
bool ReadBuffEventFile(string ta)

View File

@ -4,6 +4,8 @@ using System.IO;
using System.Text;
using UnityEngine;
using System.Collections.Generic;
using System.IO.Compression;
using Cysharp.Threading.Tasks;
public class LuaMgr : SingletonMono<LuaMgr>
{
@ -23,7 +25,7 @@ public class LuaMgr : SingletonMono<LuaMgr>
public override void InitMgr()
{
base.InitMgr();
LoadLuaFiles();
LoadLuaFiles().Forget();
}
public void Clear()
@ -46,34 +48,105 @@ public class LuaMgr : SingletonMono<LuaMgr>
base.Dispose();
}
void LoadLuaFiles()
async UniTask LoadLuaFiles()
{
Resources.LoadAsync<TextAsset>("files_tolua").completed += op =>
{
ResourceRequest request = op as ResourceRequest;
var textAsset = request.asset as TextAsset;
var paths = textAsset.text.Split(',');
ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.ABToLuaDir, paths);
};
Resources.LoadAsync<TextAsset>("files_lua").completed += op =>
{
ResourceRequest request = op as ResourceRequest;
var textAsset = request.asset as TextAsset;
var paths = textAsset.text.Split(',');
ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.ABLuaLogicDir, paths);
};
Resources.LoadAsync<TextAsset>("files_pb").completed += op =>
{
ResourceRequest request = op as ResourceRequest;
var textAsset = request.asset as TextAsset;
var paths = textAsset.text.Split(',');
ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnLoadPbCallback, Constants.ABLuaPbDir, paths);
};
var tolua_zip_data = await AssetsMgr.Instance.LoadLocalFileData(Application.streamingAssetsPath + "/" + Constants.LuaDirMergeFile);
UnzipLuaFiles(tolua_zip_data);
var lua_zip_data = await AssetsMgr.Instance.LoadLocalFileData(Application.streamingAssetsPath + "/" + Constants.LuaLogicDirMergeFile);
UnzipLuaFiles(lua_zip_data);
var pb_zip_data = await AssetsMgr.Instance.LoadLocalFileData(Application.streamingAssetsPath + "/" + Constants.LuaPbDirMergeFile);
UnzipPbFiles(pb_zip_data);
// Resources.LoadAsync<TextAsset>("files_tolua").completed += op =>
// {
// ResourceRequest request = op as ResourceRequest;
// var textAsset = request.asset as TextAsset;
// var paths = textAsset.text.Split(',');
// ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.ABToLuaDir, paths);
// };
// Resources.LoadAsync<TextAsset>("files_lua").completed += op =>
// {
// ResourceRequest request = op as ResourceRequest;
// var textAsset = request.asset as TextAsset;
// var paths = textAsset.text.Split(',');
// ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnCallBack, Constants.ABLuaLogicDir, paths);
// };
// Resources.LoadAsync<TextAsset>("files_pb").completed += op =>
// {
// ResourceRequest request = op as ResourceRequest;
// var textAsset = request.asset as TextAsset;
// var paths = textAsset.text.Split(',');
// ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnLoadPbCallback, Constants.ABLuaPbDir, paths);
// };
//ResourceMgr.Instance.LoadLuaAsset(OnCallBack, Constants.ABLuaDir);
//ResourceMgr.Instance.LoadLuaAsset(OnCallBack, Constants.ABLuaLogicDir);
//ResourceMgr.Instance.LoadLuaAsset(OnLoadPbCallback, Constants.ABLuaPbDir);
}
private void UnzipLuaFiles(byte[] zipBytes)
{
using (MemoryStream ms = new MemoryStream(zipBytes))
{
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
// 处理每个文件
Debug.Log($"Extracting: {entry.FullName}, {entry.Name}");
var fileName = entry.Name;
using (var entryStream = entry.Open())
{
byte[] fileBytes = new byte[entry.Length];
entryStream.Read(fileBytes, 0, fileBytes.Length);
if (LuaDic.ContainsKey(fileName))
{
LuaDic.Remove(fileName);
}
LuaDic.Add(fileName, fileBytes);
}
}
}
}
LoadCount++;
if (LoadCount == LuaDirCount)
{
OnLoadFinished();
}
}
private void UnzipPbFiles(byte[] zipBytes)
{
using (MemoryStream ms = new MemoryStream(zipBytes))
{
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var fileName = entry.Name;
using (var entryStream = entry.Open())
{
byte[] fileBytes = new byte[entry.Length];
entryStream.Read(fileBytes, 0, fileBytes.Length);
if (fileName.Contains(".lua"))
{
continue;
}
if (luaPbDic.ContainsKey(fileName))
{
luaPbDic.Remove(fileName);
}
luaPbDic.Add(fileName, fileBytes);
}
}
}
}
LoadCount++;
if (LoadCount == LuaDirCount)
{
OnLoadFinished();
}
}
private void OnCallBack(List<TextAsset> objs, string dir, string[] assetNames)
{
if (objs == null || objs.Count <= 0)
@ -90,7 +163,6 @@ public class LuaMgr : SingletonMono<LuaMgr>
//DebugHelper.LogError(string.Format("LuaDic.ContainsKey {0}", tx.name));
LuaDic.Remove(tx.name);
}
LuaDic.Add(tx.name, tx.bytes);
}
LoadCount++;

View File

@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using System;
using System.Text;
public class UnicodeUtil {
public static string Convert(string unicodeString) {
@ -21,4 +22,83 @@ public class UnicodeUtil {
outStr = (char)int.Parse(str.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
return outStr;
}
// UTF-8 BOM 字节序列
private static readonly byte[] Utf8BOM = new byte[] { 0xEF, 0xBB, 0xBF };
// 判断字节数组是否包含 UTF-8 BOM
public static bool HasUtf8BOM(byte[] bytes)
{
if (bytes.Length < 3)
{
return false; // 字节数组长度小于 3 不可能包含 UTF-8 BOM
}
// 检查前 3 个字节是否匹配 UTF-8 BOM
for (int i = 0; i < 3; i++)
{
if (bytes[i] != Utf8BOM[i])
{
return false;
}
}
return true;
}
// 删除字节数组中的 UTF-8 BOM
public static byte[] RemoveUtf8BOM(byte[] bytes)
{
if (HasUtf8BOM(bytes))
{
// 删除 BOM返回去掉前 3 个字节的新字节数组
byte[] result = new byte[bytes.Length - 3];
Array.Copy(bytes, 3, result, 0, result.Length);
return result;
}
// 如果没有 BOM直接返回原始字节数组
return bytes;
}
// 将带有 BOM 的字节数组转换为不带 BOM 的字节数组
public static byte[] ConvertToNonBOM(byte[] bytes)
{
return RemoveUtf8BOM(bytes);
}
private static bool AreArraysEqual(byte[] arr1, byte[] arr2)
{
if (arr1.Length != arr2.Length)
return false;
for (int i = 0; i < arr1.Length; i++)
{
if (arr1[i] != arr2[i])
return false;
}
return true;
}
public static string GetEncoding(byte[] bytes)
{
Encoding[] encodings = { Encoding.UTF8, Encoding.ASCII, Encoding.Unicode, Encoding.BigEndianUnicode, Encoding.GetEncoding("ISO-8859-1") };
foreach (var encoding in encodings)
{
try
{
string result = encoding.GetString(bytes);
byte[] testBytes = encoding.GetBytes(result);
if (AreArraysEqual(testBytes, bytes))
{
return encoding.EncodingName;
}
}
catch
{
// Ignore errors and continue to try other encodings
}
}
return "Unknown Encoding";
}
}