using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
[Serializable]
public class AssetArtScanner
{
///
/// 扫描器GUID
///
public string ScannerGUID = string.Empty;
///
/// 扫描器名称
///
public string ScannerName = string.Empty;
///
/// 扫描器描述
///
public string ScannerDesc = string.Empty;
///
/// 扫描模式
/// 注意:文件路径或文件GUID
///
public string ScannerSchema = string.Empty;
///
/// 存储目录
///
public string SaveDirectory = string.Empty;
///
/// 收集列表
///
public List Collectors = new List();
///
/// 白名单
///
public List WhiteList = new List();
///
/// 检测关键字匹配
///
public bool CheckKeyword(string keyword)
{
if (ScannerName.Contains(keyword) || ScannerDesc.Contains(keyword))
return true;
else
return false;
}
///
/// 是否在白名单里
///
public bool CheckWhiteList(string guid)
{
return WhiteList.Contains(guid);
}
///
/// 检测配置错误
///
public void CheckConfigError()
{
if (string.IsNullOrEmpty(ScannerName))
throw new Exception($"Scanner name is null or empty !");
if (string.IsNullOrEmpty(ScannerSchema))
throw new Exception($"Scanner {ScannerName} schema is null !");
if (string.IsNullOrEmpty(SaveDirectory) == false)
{
if (Directory.Exists(SaveDirectory) == false)
throw new Exception($"Scanner {ScannerName} save directory is invalid : {SaveDirectory}");
}
}
///
/// 加载扫描模式实例
///
public ScannerSchema LoadSchema()
{
if (string.IsNullOrEmpty(ScannerSchema))
return null;
string filePath;
if (ScannerSchema.StartsWith("Assets/"))
{
filePath = ScannerSchema;
}
else
{
string guid = ScannerSchema;
filePath = AssetDatabase.GUIDToAssetPath(guid);
}
var schema = AssetDatabase.LoadMainAssetAtPath(filePath) as ScannerSchema;
if (schema == null)
Debug.LogWarning($"Failed load scanner schema : {filePath}");
return schema;
}
///
/// 运行扫描器生成报告类
///
public ScanReport RunScanner()
{
if (Collectors.Count == 0)
Debug.LogWarning($"Scanner {ScannerName} collector is empty !");
ScannerSchema schema = LoadSchema();
if (schema == null)
throw new Exception($"Failed to load schema : {ScannerSchema}");
var report = schema.RunScanner(this);
report.FileSign = ScannerDefine.ReportFileSign;
report.FileVersion = ScannerDefine.ReportFileVersion;
report.SchemaType = schema.GetType().FullName;
report.ScannerGUID = ScannerGUID;
return report;
}
}
}