using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace YooAsset.Editor
{
///
/// 构建参数
///
public abstract class BuildParameters
{
///
/// 构建输出的根目录
///
public string BuildOutputRoot;
///
/// 内置文件的根目录
///
public string BuildinFileRoot;
///
/// 构建管线
///
public string BuildPipeline;
///
/// 构建的平台
///
public BuildTarget BuildTarget;
///
/// 构建模式
///
public EBuildMode BuildMode;
///
/// 构建的包裹名称
///
public string PackageName;
///
/// 构建的包裹版本
///
public string PackageVersion;
///
/// 是否启用共享资源打包
///
public bool EnableSharePackRule = false;
///
/// 验证构建结果
///
public bool VerifyBuildingResult = false;
///
/// 资源包名称样式
///
public EFileNameStyle FileNameStyle;
///
/// 内置文件的拷贝选项
///
public EBuildinFileCopyOption BuildinFileCopyOption;
///
/// 内置文件的拷贝参数
///
public string BuildinFileCopyParams;
///
/// 资源包加密服务类
///
public IEncryptionServices EncryptionServices;
private string _pipelineOutputDirectory = string.Empty;
private string _packageOutputDirectory = string.Empty;
private string _packageRootDirectory = string.Empty;
private string _buildinRootDirectory = string.Empty;
///
/// 检测构建参数是否合法
///
public virtual void CheckBuildParameters()
{
// 检测当前是否正在构建资源包
if (UnityEditor.BuildPipeline.isBuildingPlayer)
{
string message = BuildLogger.GetErrorMessage(ErrorCode.ThePipelineIsBuiding, "The pipeline is buiding, please try again after finish !");
throw new Exception(message);
}
// 检测是否有未保存场景
if (BuildMode != EBuildMode.SimulateBuild)
{
if (EditorTools.HasDirtyScenes())
{
string message = BuildLogger.GetErrorMessage(ErrorCode.FoundUnsavedScene, "Found unsaved scene !");
throw new Exception(message);
}
}
// 检测构建参数合法性
if (BuildTarget == BuildTarget.NoTarget)
{
string message = BuildLogger.GetErrorMessage(ErrorCode.NoBuildTarget, "Please select the build target platform !");
throw new Exception(message);
}
if (string.IsNullOrEmpty(PackageName))
{
string message = BuildLogger.GetErrorMessage(ErrorCode.PackageNameIsNullOrEmpty, "Package name is null or empty !");
throw new Exception(message);
}
if (string.IsNullOrEmpty(PackageVersion))
{
string message = BuildLogger.GetErrorMessage(ErrorCode.PackageVersionIsNullOrEmpty, "Package version is null or empty !");
throw new Exception(message);
}
if (string.IsNullOrEmpty(BuildOutputRoot))
{
string message = BuildLogger.GetErrorMessage(ErrorCode.BuildOutputRootIsNullOrEmpty, "Build output root is null or empty !");
throw new Exception(message);
}
if (string.IsNullOrEmpty(BuildinFileRoot))
{
string message = BuildLogger.GetErrorMessage(ErrorCode.BuildinFileRootIsNullOrEmpty, "Buildin file root is null or empty !");
throw new Exception(message);
}
// 强制构建删除包裹目录
if (BuildMode == EBuildMode.ForceRebuild)
{
string packageRootDirectory = GetPackageRootDirectory();
if (EditorTools.DeleteDirectory(packageRootDirectory))
{
BuildLogger.Log($"Delete package root directory: {packageRootDirectory}");
}
}
// 检测包裹输出目录是否存在
if (BuildMode != EBuildMode.SimulateBuild)
{
string packageOutputDirectory = GetPackageOutputDirectory();
if (Directory.Exists(packageOutputDirectory))
{
string message = BuildLogger.GetErrorMessage(ErrorCode.PackageOutputDirectoryExists, $"Package outout directory exists: {packageOutputDirectory}");
throw new Exception(message);
}
}
// 如果输出目录不存在
string pipelineOutputDirectory = GetPipelineOutputDirectory();
if (EditorTools.CreateDirectory(pipelineOutputDirectory))
{
BuildLogger.Log($"Create pipeline output directory: {pipelineOutputDirectory}");
}
}
///
/// 获取构建管线的输出目录
///
///
public virtual string GetPipelineOutputDirectory()
{
if (string.IsNullOrEmpty(_pipelineOutputDirectory))
{
_pipelineOutputDirectory = $"{BuildOutputRoot}/{BuildTarget}/{PackageName}/{YooAssetSettings.OutputFolderName}";
}
return _pipelineOutputDirectory;
}
///
/// 获取本次构建的补丁输出目录
///
public virtual string GetPackageOutputDirectory()
{
if (string.IsNullOrEmpty(_packageOutputDirectory))
{
_packageOutputDirectory = $"{BuildOutputRoot}/{BuildTarget}/{PackageName}/{PackageVersion}";
}
return _packageOutputDirectory;
}
///
/// 获取本次构建的补丁根目录
///
public virtual string GetPackageRootDirectory()
{
if (string.IsNullOrEmpty(_packageRootDirectory))
{
_packageRootDirectory = $"{BuildOutputRoot}/{BuildTarget}/{PackageName}";
}
return _packageRootDirectory;
}
///
/// 获取内置资源的根目录
///
public virtual string GetBuildinRootDirectory()
{
if (string.IsNullOrEmpty(_buildinRootDirectory))
{
_buildinRootDirectory = $"{BuildinFileRoot}/{PackageName}";
}
return _buildinRootDirectory;
}
}
}