147 lines
5.5 KiB
C#
147 lines
5.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
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();
|
||
}
|
||
|
||
static void MergeFiles(string directory, string outputFile)
|
||
{
|
||
if (!Directory.Exists(directory))
|
||
{
|
||
Debug.LogError("Directory does not exist: " + directory);
|
||
return;
|
||
}
|
||
|
||
// 获取目录下的所有文件
|
||
var srcFiles = getAllFilesPathEX(directory);
|
||
|
||
// 检查输出路径是否有效,如果目录不存在则创建
|
||
string outputDirectory = Path.GetDirectoryName(outputFile);
|
||
if (!Directory.Exists(outputDirectory))
|
||
{
|
||
Directory.CreateDirectory(outputDirectory);
|
||
}
|
||
|
||
// 使用 StreamWriter 合并文件
|
||
using (StreamWriter writer = new StreamWriter(outputFile, false))
|
||
{
|
||
foreach (string file in srcFiles)
|
||
{
|
||
// 读取每个文件的内容
|
||
var fileName = Path.GetFileName(file);
|
||
Debug.Log(file);
|
||
string fileContent = File.ReadAllText(file);
|
||
|
||
// 写入文件名
|
||
writer.WriteLine(fileName);
|
||
|
||
// 写入文件内容
|
||
writer.WriteLine(fileContent);
|
||
|
||
// 写入分隔符
|
||
writer.WriteLine(Constants.merge_files_split);
|
||
}
|
||
}
|
||
|
||
Debug.Log("Files merged successfully into: " + outputFile);
|
||
}
|
||
|
||
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 (ZipArchive zipArchive = new ZipArchive(zipFile, ZipArchiveMode.Create))
|
||
{
|
||
foreach (var filePath in files)
|
||
{
|
||
if (File.Exists(filePath))
|
||
{
|
||
// 获取文件的名称(不带路径)
|
||
string fileName = Path.GetFileName(filePath);
|
||
byte[] fileBytes = File.ReadAllBytes(filePath);
|
||
byte[] aseFileBytes = CommonUtil.Encrypt(fileBytes);
|
||
|
||
// 将文件添加到ZIP档案
|
||
ZipArchiveEntry entry = zipArchive.CreateEntry(fileName);
|
||
|
||
using (Stream entryStream = entry.Open())
|
||
{
|
||
entryStream.Write(aseFileBytes, 0, aseFileBytes.Length);
|
||
}
|
||
|
||
// using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
|
||
// {
|
||
// fileStream.CopyTo(entryStream);
|
||
// }
|
||
|
||
Debug.Log($"Added file: {fileName} to the ZIP archive.");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"File does not exist: {filePath}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
Debug.Log($"Files successfully compressed into: {zipPath}");
|
||
}
|
||
}
|