193 lines
6.4 KiB
C#
193 lines
6.4 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using Cysharp.Threading.Tasks;
|
||
using Sirenix.OdinInspector;
|
||
using Sirenix.OdinInspector.Editor;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using UnityEngine.Serialization;
|
||
|
||
namespace HK.Editor
|
||
{
|
||
[System.Serializable]
|
||
public class OSSConfig : HK.OSSConfig
|
||
{
|
||
[ReadOnly] public BuildTargetPlatform platform;
|
||
public string appVersion;
|
||
|
||
public string GetOssUploadUrl()
|
||
{
|
||
// https://kiosk-assets-bundle.oss-cn-hongkong.aliyuncs.com/Framework
|
||
return $"CDN/{GetPlatformName()}/{appVersion}";
|
||
}
|
||
|
||
string GetPlatformName()
|
||
{
|
||
switch (platform)
|
||
{
|
||
case BuildTargetPlatform.StandaloneWindows:
|
||
return "PC";
|
||
break;
|
||
case BuildTargetPlatform.StandaloneWindows64:
|
||
return "PC";
|
||
break;
|
||
case BuildTargetPlatform.Android:
|
||
return "Android";
|
||
break;
|
||
case BuildTargetPlatform.WebGL:
|
||
return "WebGL";
|
||
break;
|
||
case BuildTargetPlatform.IOS:
|
||
return "iOS";
|
||
break;
|
||
case BuildTargetPlatform.StandaloneOSX:
|
||
break;
|
||
case BuildTargetPlatform.StandaloneLinux64:
|
||
break;
|
||
case BuildTargetPlatform.TvOS:
|
||
break;
|
||
case BuildTargetPlatform.PS4:
|
||
break;
|
||
default:
|
||
throw new ArgumentOutOfRangeException();
|
||
}
|
||
|
||
return "";
|
||
}
|
||
}
|
||
|
||
[System.Serializable]
|
||
public class ResourceConfig
|
||
{
|
||
public string rootFolder = "Bundles";
|
||
public BuildTargetPlatform platform = BuildTargetPlatform.StandaloneWindows64;
|
||
public string packageName = "DefaultPackage";
|
||
[ReadOnly] public string latestVersionPath;
|
||
|
||
public string GetPackageFolder()
|
||
{
|
||
return Path.Combine(rootFolder, platform.ToString().Replace(" ", ""), packageName);
|
||
}
|
||
}
|
||
|
||
public class UploadResourcesEditor : OdinEditorWindow
|
||
{
|
||
[MenuItem("Tool/Upload Resources")]
|
||
static void GetWindow()
|
||
{
|
||
// GetWindow<UploadResourcesEditor>().Show();
|
||
var window = GetWindow<UploadResourcesEditor>();
|
||
window.ossConfig = new OSSConfig();
|
||
window.ossConfig.appVersion = "v1.0";
|
||
window.ossConfig.platform = BuildTargetPlatform.StandaloneWindows;
|
||
window.Show();
|
||
}
|
||
|
||
public OSSConfig ossConfig;
|
||
public ResourceConfig resourceConfig;
|
||
|
||
[Button("GetAssetVersion")]
|
||
public void GetAssetVersion()
|
||
{
|
||
string _latestVersion;
|
||
string versionRootPath = resourceConfig.GetPackageFolder();
|
||
Debug.Log(versionRootPath);
|
||
versionRootPath = Path.GetFullPath(versionRootPath);
|
||
// 2. 检查版本根目录是否存在
|
||
if (!Directory.Exists(versionRootPath))
|
||
{
|
||
Debug.Log($"版本根目录不存在:{versionRootPath}\n请检查目录配置是否正确");
|
||
return;
|
||
}
|
||
|
||
// 3. 获取所有版本目录(如2025-08-25-669)
|
||
var versionDirectories = Directory.GetDirectories(versionRootPath)
|
||
.Select(Path.GetFileName)
|
||
.Where(dir => IsValidDateVersionFormat(dir)) // 过滤日期格式的目录
|
||
.ToList();
|
||
|
||
if (versionDirectories.Count == 0)
|
||
{
|
||
Debug.Log($"未找到任何版本目录:{versionRootPath}\n请确认已构建资源");
|
||
return;
|
||
}
|
||
|
||
// 4. 按日期排序(最新的日期在最后)
|
||
var sortedVersions = versionDirectories
|
||
.OrderBy(dir => ParseDateFromVersion(dir)) // 按日期升序
|
||
.ToList();
|
||
|
||
// 5. 最新版本是最后一个
|
||
_latestVersion = sortedVersions.Last();
|
||
|
||
resourceConfig.latestVersionPath = Path.Combine(versionRootPath, _latestVersion);
|
||
ossConfig.platform = resourceConfig.platform;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证目录名是否为日期格式(如2025-08-25-669)
|
||
/// </summary>
|
||
private bool IsValidDateVersionFormat(string dirName)
|
||
{
|
||
// 分割格式:日期-编号(如2025-08-25-669 → 前3部分是日期)
|
||
string[] parts = dirName.Split('-');
|
||
if (parts.Length < 3)
|
||
return false;
|
||
|
||
// 尝试解析日期部分(前3段:年-月-日)
|
||
return DateTime.TryParse($"{parts[0]}-{parts[1]}-{parts[2]}", out _);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从目录名中解析日期(用于排序)
|
||
/// </summary>
|
||
private DateTime ParseDateFromVersion(string dirName)
|
||
{
|
||
string[] parts = dirName.Split('-');
|
||
DateTime.TryParse($"{parts[0]}-{parts[1]}-{parts[2]}", out DateTime date);
|
||
return date;
|
||
}
|
||
|
||
CancellationTokenSource source;
|
||
bool isUploading = false;
|
||
|
||
[ReadOnly] [ShowIf("isUploading")] [ProgressBar(0, 100, 0, 255, 0)]
|
||
public float progress;
|
||
|
||
[Button("上传资源到本地服务器")]
|
||
public async void UploadResources()
|
||
{
|
||
Debug.Log("Upload Resources Start");
|
||
isUploading = true;
|
||
progress = 0;
|
||
GetAssetVersion();
|
||
|
||
var ossUploadUrl = ossConfig.GetOssUploadUrl();
|
||
Debug.Log(ossUploadUrl);
|
||
|
||
AliyunOSSManager.Instance.Initialize(ossConfig);
|
||
source = new CancellationTokenSource();
|
||
await AliyunOSSManager.Instance.UploadDirectoryAsync(resourceConfig.latestVersionPath, ossUploadUrl,
|
||
source.Token,
|
||
ProgressCallback);
|
||
isUploading = false;
|
||
Debug.Log("Upload Resources Finished");
|
||
}
|
||
|
||
[ShowIf("isUploading")]
|
||
[Button("取消上传资源到本地服务器")]
|
||
public void CancelUploadResources()
|
||
{
|
||
progress = 0;
|
||
source.Cancel();
|
||
}
|
||
|
||
private void ProgressCallback(int arg1, int arg2)
|
||
{
|
||
progress = ((float)arg1 / arg2) * 100;
|
||
Debug.Log($"progress {arg1}/{arg2}");
|
||
}
|
||
}
|
||
} |