103 lines
4.2 KiB
C#
103 lines
4.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using ICSharpCode.SharpZipLib.Zip;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
|
||
public class RoTool : EditorWindow
|
||
{
|
||
[MenuItem("RO_Tool/[运行前准备] 合并Lua脚本到StreamingAssets目录!!", priority = 1)]
|
||
public static void MergeLuaAssets()
|
||
{
|
||
Debug.Log("Merge Lua Files to Big File From Asset To Content Directory");
|
||
CompressFiles(Constants.LuaDir, "Assets/StreamingAssets/" + Constants.LuaDirMergeFile);
|
||
CompressFiles(Constants.LuaLogicDir, "Assets/StreamingAssets/" + Constants.LuaLogicDirMergeFile);
|
||
CompressFiles(Constants.LuaPbDir, "Assets/StreamingAssets/" + Constants.LuaPbDirMergeFile);
|
||
AssetDatabase.Refresh();
|
||
}
|
||
|
||
[MenuItem("RO_Tool/[运行前准备] 合并配置文件到StreamingAssets目录!!", priority = 1)]
|
||
public static void MergeConfigAssets()
|
||
{
|
||
Debug.Log("Merge Config Files to Big File From Asset To Content Directory");
|
||
CompressFiles(Constants.XmlConfig, "Assets/StreamingAssets/" + Constants.XmlConfigMergeFile);
|
||
CompressFiles(Constants.CsvConfig, "Assets/StreamingAssets/" + Constants.CsvConfigMergeFile);
|
||
AssetDatabase.Refresh();
|
||
}
|
||
|
||
private static HashSet<string> s_ValidExtMap = new HashSet<string>() { ".prefab", ".txt", ".xml", ".txt", ".lua", ".csv", ".ogg", ".wav", ".ttf", ".bytes", ".pb" };
|
||
private static List<string> getAllFilesPathEX(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
|
||
{
|
||
List<string> all = new List<string>();
|
||
if (!Directory.Exists(path))
|
||
{
|
||
return all;
|
||
}
|
||
string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
|
||
for (int j = 0; j < allFiles.Length; ++j)
|
||
{
|
||
string fileName = allFiles[j];
|
||
string ext = Path.GetExtension(fileName);
|
||
if (string.IsNullOrEmpty(ext)) continue;
|
||
ext = ext.ToLower();
|
||
if (s_ValidExtMap.Contains(ext))
|
||
{
|
||
all.Add(fileName.Replace('\\', '/'));
|
||
}
|
||
}
|
||
return all;
|
||
}
|
||
|
||
// 压缩文件
|
||
static private void CompressFiles(string inputDirectory, string zipPath)
|
||
{
|
||
var files = getAllFilesPathEX(inputDirectory);
|
||
// 确保输出文件夹存在
|
||
string directory = Path.GetDirectoryName(zipPath);
|
||
if (!Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
// 创建一个ZIP文件并开始写入
|
||
using (FileStream zipFile = new FileStream(zipPath, FileMode.Create))
|
||
{
|
||
using (ZipOutputStream zipStream = new ZipOutputStream(zipFile))
|
||
{
|
||
zipStream.SetLevel(6);
|
||
zipStream.Password = Constants.ZipPW;
|
||
foreach (var filePath in files)
|
||
{
|
||
if (File.Exists(filePath))
|
||
{
|
||
// 获取文件的名称(不带路径)
|
||
string fileName = Path.GetFileName(filePath);
|
||
FileInfo fileInfo = new FileInfo(filePath);
|
||
byte[] fileBytes = File.ReadAllBytes(filePath);
|
||
//byte[] aseFileBytes = CommonUtil.Encrypt(fileBytes);
|
||
Debug.Log($"Added file: {fileName} to the ZIP archive.");
|
||
// 将文件添加到ZIP档案
|
||
ZipEntry entry = new ZipEntry(fileName) {
|
||
//DateTime = fileInfo.LastWriteTime,
|
||
//AESKeySize = 128 // 使用 AES 256 位加密
|
||
};
|
||
zipStream.PutNextEntry(entry);
|
||
zipStream.Write(fileBytes, 0, fileBytes.Length);
|
||
zipStream.CloseEntry();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"File does not exist: {filePath}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
Debug.Log($"Files successfully compressed into: {zipPath}");
|
||
}
|
||
}
|