hengyang_client/wb_unity_pro/Assets/Scripts/Editor/ReadAssetBase.cs

273 lines
9.8 KiB
C#
Raw Permalink Normal View History

2025-09-01 20:15:53 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using taurus.client;
using taurus.unity;
2025-12-18 20:23:31 +08:00
using System.Text;
2025-09-01 20:15:53 +08:00
2025-12-18 20:23:31 +08:00
class ReadAssetBase : EditorWindow
2025-09-01 20:15:53 +08:00
{
2025-12-18 20:23:31 +08:00
private bool ios = true;
private bool android = true;
private bool android32 = true;
// 版本号
private static string baseVersion = "";
#if UNITY_IPHONE
static string PACK_PATH = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Pack", "iOS");
static string PACK_PATH_ALL = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Pack_All", "iOS");
#else
static string PACK_PATH = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Pack", "Android");
static string PACK_PATH_ALL = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Pack_All", "Android");
#endif
static string PACK_PATH_Config = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Pack", "config");
2025-09-01 20:15:53 +08:00
static string READ_PATH = Path.Combine(Application.streamingAssetsPath, "Pack");
static string READ_ZIP = Path.Combine(Application.streamingAssetsPath, "Pack.byte");
static string READ_VERSION = Path.Combine(Application.streamingAssetsPath, "version.txt");
[MenuItem("BuildTools/Read Pack In Local")]
2025-12-18 20:23:31 +08:00
public static void ShowWindow()
{
// 弹出窗口
var window = GetWindow<ReadAssetBase>("Read Pack In Local");
window.minSize = new Vector2(300, 150);
}
private void OnGUI()
{
// 第三行:版本号输入框
baseVersion = EditorGUILayout.TextField("版本号", baseVersion);
GUILayout.FlexibleSpace();
// 最后一行:确定、取消按钮
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("确定", GUILayout.Width(80)))
{
ReadAssetBaseRun();
}
if (GUILayout.Button("取消", GUILayout.Width(80)))
{
Close();
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(8);
}
//[MenuItem("BuildTools/Read Pack In Local")]
2025-09-01 20:15:53 +08:00
static public void ReadAssetBaseRun()
{
if (!Directory.Exists(PACK_PATH))
{
Debug.LogError("请打包后再运行解压操作");
return;
}
2025-12-18 20:23:31 +08:00
if (baseVersion.Trim().Equals(""))
{
Debug.LogError("请输入版本号");
return;
}
2025-09-01 20:15:53 +08:00
try
{
EditorApplication.LockReloadAssemblies();
EditorUtility.DisplayProgressBar("正在解压资源包中", "请稍候,正在处理资源...", 0f);
if (Directory.Exists(READ_PATH))
{
Directory.Delete(READ_PATH, true);
}
Directory.CreateDirectory(READ_PATH);
if (File.Exists(READ_ZIP))
{
File.Delete(READ_ZIP);
}
if (File.Exists(READ_VERSION))
{
File.Delete(READ_VERSION);
}
2025-12-18 20:23:31 +08:00
if (!File.Exists(PACK_PATH_ALL))
{
Directory.CreateDirectory(PACK_PATH_ALL);
}
2025-09-01 20:15:53 +08:00
2025-12-18 20:23:31 +08:00
if (!File.Exists(PACK_PATH_Config))
{
Directory.CreateDirectory(PACK_PATH_Config);
}
string baseString = Path.Combine(Application.streamingAssetsPath, "init2_1.json");
string baseBytes = File.ReadAllText(baseString);
2025-09-01 20:15:53 +08:00
ArrayList baseList = (ArrayList)MiniJSON.Json.Deserialize(baseBytes);
2025-12-18 20:23:31 +08:00
string extendString = Path.Combine(Application.streamingAssetsPath, "init1_1.json");
string extendBytes = File.ReadAllText(extendString);
ArrayList extendList = (ArrayList)MiniJSON.Json.Deserialize(extendBytes);
//把PACK_PATH当前打包的内容复制到PACK_PATH_ALL里再通过All去加载配置文件和本地资源包
for (int i = 0; i < baseList.Count; i++)
{
var tem = (Hashtable)baseList[i];
string _base_path = Path.Combine("base", tem["name"] as string);
string version = tem["version"] as string;
var zip_path = Path.Combine(PACK_PATH, _base_path, $"asset_pack{version}.bytes");
var temp = Path.Combine(PACK_PATH_ALL, _base_path, $"asset_pack{version}.bytes");
if (File.Exists(zip_path))
{
var dir = Path.GetDirectoryName(temp);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.Copy(zip_path, temp, true);
}
if (File.Exists(temp))
{
long size = new FileInfo(temp).Length;
tem["size"] = size;
}
else
{
tem["size"] = 0;
}
}
//生成对应的json文件
string newBaseJson = MiniJSON.Json.Serialize(baseList);
string arv_config = Path.Combine(PACK_PATH_Config, string.Format("asset_config{0}.json", baseVersion));
File.WriteAllText(arv_config, newBaseJson);
StringBuilder redisScript = new StringBuilder();
for (int i = 0; i < extendList.Count; i++)
{
var tem = (Hashtable)extendList[i];
string extendBundle = tem["bundle"] as string;
string[] extendNameTem = extendBundle.Split('/');
string _extend_path = Path.Combine(extendNameTem);
string version = tem["version"] as string;
var zip_path = Path.Combine(PACK_PATH, _extend_path, $"asset_pack{version}.bytes");
var temp = Path.Combine(PACK_PATH_ALL, _extend_path, $"asset_pack{version}.bytes");
if (File.Exists(zip_path))
{
var dir = Path.GetDirectoryName(temp);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.Copy(zip_path, temp, true);
}
string gameId = tem["game_id"] as string;
long size = 0L;
if (File.Exists(temp))
{
size = new FileInfo(temp).Length;
}
redisScript.AppendLine($"HSET game:{gameId} version \"{version}\"");
redisScript.AppendLine($"HSET game:{gameId} size {size}");
redisScript.AppendLine($"HINCRBY game:{gameId} cache_ver 1");
redisScript.AppendLine();
}
// 写入 redis_update.txt
string redisPath = Path.Combine(PACK_PATH_Config, "redis_update.txt");
File.WriteAllText(redisPath, redisScript.ToString());
//再去PACK_PATH_ALL生成懒人资源包和配置文件
2025-09-01 20:15:53 +08:00
for (int i = 0; i < baseList.Count; i++)
{
var tem = (Hashtable)baseList[i];
string _base_path = Path.Combine("base", tem["name"] as string);
2025-12-18 20:23:31 +08:00
string version = tem["version"] as string;
2025-09-01 20:15:53 +08:00
__UnPack(version, _base_path);
}
for (int i = 0; i < extendList.Count; i++)
{
var tem = (Hashtable)extendList[i];
string extendBundle = tem["bundle"] as string;
string[] extendNameTem = extendBundle.Split('/');
string _extend_path = Path.Combine(extendNameTem);
string version = tem["version"] as string;
__UnPack(version, _extend_path);
}
2025-12-18 20:23:31 +08:00
//string arv = Path.Combine(READ_PATH, string.Format("asset_config{0}.json", baseVersion));
//File.WriteAllText(arv, baseBytes);
//string rv = Path.Combine(READ_PATH, "version.txt");
//File.WriteAllText(rv, baseVersion);
2025-09-01 20:15:53 +08:00
File.WriteAllText(READ_VERSION, baseVersion);
IFilePack zf = null;
zf = new FilePack20(READ_ZIP, PackMode.Write);
zf.PackFile(READ_PATH, ".meta|.manifest");
zf = null;
2025-12-18 20:23:31 +08:00
//Directory.Delete(READ_PATH, true);
2025-09-01 20:15:53 +08:00
EditorUtility.DisplayDialog("完成", "处理完毕!", "确定");
}
finally
{
EditorUtility.ClearProgressBar();
EditorApplication.UnlockReloadAssemblies();
}
}
static void __UnPack(string version, string _base_path)
{
try
{
2025-12-18 20:23:31 +08:00
string dir = PACK_PATH_ALL;
2025-09-01 20:15:53 +08:00
var zip_path = Path.Combine(dir, _base_path, $"asset_pack{version}.bytes");
if (!File.Exists(zip_path))
{
Debug.LogError("打包文件不完全,请将打包所有资源包");
throw new System.Exception();
}
IFilePack zip = null;
zip = new FilePack20(zip_path, PackMode.Read);
zip.UnPackFile(READ_PATH);
var lua_pack_path = Path.Combine(READ_PATH, "lua_pack");
if (File.Exists(lua_pack_path))
{
var lua_dir = Path.Combine(READ_PATH, "lua/");
if (!Directory.Exists(READ_PATH))
Directory.CreateDirectory(READ_PATH);
zip = new FilePack20(lua_pack_path, PackMode.Read);
zip.UnPackFile(lua_dir);
File.Delete(lua_pack_path);
}
string rv = Path.Combine(READ_PATH, _base_path, "version.txt");
string directory = Path.GetDirectoryName(rv);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory); // 递归创建所有不存在的文件夹
}
File.WriteAllText(rv, version, System.Text.Encoding.ASCII);
}
catch
{
Directory.Delete(READ_PATH, true);
}
}
}