81 lines
2.7 KiB
C#
81 lines
2.7 KiB
C#
#if UNITY_EDITOR
|
|
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public static class DlcPackageBuilderEditor
|
|
{
|
|
private const string PackageExtension = "bsnkdlc";
|
|
|
|
[MenuItem("Bansonic/DLC/Build .bsnkdlc From Folder")]
|
|
private static void BuildPackageFromFolder()
|
|
{
|
|
string sourceFolder = EditorUtility.OpenFolderPanel("选择 DLC 源文件夹", Application.dataPath, string.Empty);
|
|
if (string.IsNullOrWhiteSpace(sourceFolder) || !Directory.Exists(sourceFolder))
|
|
{
|
|
return;
|
|
}
|
|
|
|
string defaultName = new DirectoryInfo(sourceFolder).Name + "." + PackageExtension;
|
|
string outputPath = EditorUtility.SaveFilePanel("保存 .bsnkdlc", Path.GetDirectoryName(sourceFolder), defaultName, PackageExtension);
|
|
if (string.IsNullOrWhiteSpace(outputPath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
BuildArchive(sourceFolder, outputPath);
|
|
EditorUtility.DisplayDialog("DLC 打包完成", "已生成:\n" + outputPath, "确定");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError("[DLC] Build .bsnkdlc failed: " + ex);
|
|
EditorUtility.DisplayDialog("DLC 打包失败", ex.Message, "确定");
|
|
}
|
|
}
|
|
|
|
private static void BuildArchive(string sourceFolder, string outputPath)
|
|
{
|
|
if (File.Exists(outputPath))
|
|
{
|
|
File.Delete(outputPath);
|
|
}
|
|
|
|
string tempZipPath = outputPath + ".tmpzip";
|
|
if (File.Exists(tempZipPath))
|
|
{
|
|
File.Delete(tempZipPath);
|
|
}
|
|
|
|
try
|
|
{
|
|
using (FileStream stream = new FileStream(tempZipPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
|
|
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create))
|
|
{
|
|
string[] files = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
|
|
for (int i = 0; i < files.Length; i++)
|
|
{
|
|
string filePath = files[i];
|
|
string relativePath = filePath.Substring(sourceFolder.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
|
relativePath = relativePath.Replace('\\', '/');
|
|
archive.CreateEntryFromFile(filePath, relativePath, System.IO.Compression.CompressionLevel.Optimal);
|
|
}
|
|
}
|
|
|
|
File.Move(tempZipPath, outputPath);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(tempZipPath))
|
|
{
|
|
File.Delete(tempZipPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|