TH1/Unity/Assets/Scripts/TH1_Logic/Editor/OssEditorWindow.cs

268 lines
9.1 KiB
C#

/*
* @Author: 白哉
* @Description: 回放编辑器
* @Date: 2025年04月22日 星期二 15:04:14
* @Modify:
*/
using System;
using System.IO;
using System.Text;
using MemoryPack;
using Steamworks;
using TH1_Logic.Net;
using TH1_Logic.Oss;
using UnityEditor;
using UnityEngine;
namespace Logic.Editor
{
public class OssEditorWindow : EditorWindow
{
// 滑条
private Vector2 _barPosition;
// 背景
private GUIStyle _redBoxStyle;
private GUIStyle _whiteBoxStyle;
private StsTokenService _stsService;
private OssUploadService _uploadService;
private StsCredentials _cachedCredentials;
private const string FunctionUrl = "https://get-sts-token-qltjykaafr.cn-shanghai.fcapp.run";
[MenuItem("Tools/Oss 编辑器")]
private static void ShowWindow()
{
var window = CreateWindow<OssEditorWindow>();
window.titleContent = new GUIContent("Oss 编辑器");
window.Show();
}
private void OnEnable()
{
_stsService = new StsTokenService(FunctionUrl);
_uploadService = new OssUploadService();
}
private void OnGUI()
{
if (_redBoxStyle == null)
{
_redBoxStyle = InspectorUtils.GetHelpBoxStyle();
InspectorUtils.AddBorder(_redBoxStyle, new Color(0.5f, 0.4f, 0.4f, 0.6f));
}
if (_whiteBoxStyle == null)
{
_whiteBoxStyle = InspectorUtils.GetHelpBoxStyle();
InspectorUtils.AddBorder(_whiteBoxStyle, new Color(1f, 1f, 1f, 0.2f));
}
GUI.skin.button.wordWrap = true;
_barPosition = EditorGUILayout.BeginScrollView(_barPosition);
if (InspectorUtils.InspectorButtonWithTextWidth("测试1: 请求STS令牌"))
{
TestRequestStsToken();
}
if (InspectorUtils.InspectorButtonWithTextWidth("测试2: 上传文件到OSS"))
{
TestUploadFile();
}
if (InspectorUtils.InspectorButtonWithTextWidth("测试3: 完整流程(获取令牌+上传)"))
{
TestFullFlow();
}
if (InspectorUtils.InspectorButtonWithTextWidth("测试4: 上传文件到OSS(错误路径)"))
{
TestUploadErrorNameFile();
}
if (InspectorUtils.InspectorButtonWithTextWidth("测试5: 上传文件到OSS(大文件)"))
{
TestUploadBigFile();
}
if (InspectorUtils.InspectorButtonWithTextWidth("测试存档反序列化"))
{
TestDeserializeArchive();
}
EditorGUILayout.EndScrollView();
}
public async void TestRequestStsToken()
{
var id = LobbyManager.Instance.Lobby.GetSelfMemberId();
Debug.Log("=== 开始测试 STS 令牌请求 ===");
Debug.Log($"SteamID: {id}");
try
{
// 获取 Auth Ticket
var ticket = new byte[1024];
var identity = new SteamNetworkingIdentity();
SteamUser.GetAuthSessionTicket(ticket, 1024, out var ticketSize, ref identity);
var authTicket = BitConverter.ToString(ticket, 0, (int)ticketSize).Replace("-", "").ToLower();
_cachedCredentials = await _stsService.RequestStsTokenAsync(id.ToString(), authTicket);
Debug.Log("<color=green>STS 令牌获取成功!</color>");
Debug.Log($"AccessKeyId: {_cachedCredentials.accessKeyId.Substring(0, 10)}...");
Debug.Log($"Endpoint: {_cachedCredentials.endpoint}");
Debug.Log($"Bucket: {_cachedCredentials.bucket}");
Debug.Log($"ObjectKey: {_cachedCredentials.objectKey}");
Debug.Log($"ExpiresIn: {_cachedCredentials.expiresIn} 秒");
}
catch (System.Exception ex)
{
Debug.LogError($"<color=red>STS 令牌获取失败:</color> {ex.Message}");
}
}
public async void TestUploadFile()
{
if (_cachedCredentials == null)
{
Debug.LogError("请先执行 测试1 获取STS令牌");
return;
}
Debug.Log("=== 开始测试 OSS 上传 ===");
try
{
var testData = Encoding.UTF8.GetBytes("{\"test\":true,\"timestamp\":" + System.DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "}");
Debug.Log($"上传数据大小: {testData.Length} bytes");
var result = await _uploadService.UploadFileAsync(_cachedCredentials, testData);
if (result)
{
Debug.Log("<color=green>文件上传成功!</color>");
Debug.Log($"文件路径: {_cachedCredentials.bucket}/{_cachedCredentials.objectKey}");
}
else
{
Debug.LogError("<color=red>文件上传失败</color>");
}
}
catch (System.Exception ex)
{
Debug.LogError($"<color=red>上传异常:</color> {ex.Message}");
}
}
public async void TestUploadErrorNameFile()
{
if (_cachedCredentials == null)
{
Debug.LogError("请先执行 测试1 获取STS令牌");
return;
}
Debug.Log("=== 开始测试 OSS 上传 ===");
try
{
var testData = Encoding.UTF8.GetBytes("{\"test\":true,\"timestamp\":" + System.DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "}");
Debug.Log($"上传数据大小: {testData.Length} bytes");
var result = await _uploadService.TestInvalidObjectKeyAsync(_cachedCredentials, testData);
}
catch (System.Exception ex)
{
Debug.LogError($"<color=red>上传异常:</color> {ex.Message}");
}
}
public async void TestUploadBigFile()
{
if (_cachedCredentials == null)
{
Debug.LogError("请先执行 测试1 获取STS令牌");
return;
}
Debug.Log("=== 开始测试 OSS 上传 ===");
try
{
var result = await _uploadService.TestOversizedFileAsync(_cachedCredentials);
}
catch (System.Exception ex)
{
Debug.LogError($"<color=red>上传异常:</color> {ex.Message}");
}
}
public async void TestFullFlow()
{
Debug.Log("=== 开始完整流程测试 ===");
var manager = new OssManager();
var testData = Encoding.UTF8.GetBytes("{\"match\":\"test\",\"score\":100}");
var id = LobbyManager.Instance.Lobby.GetSelfMemberId();
var result = await manager.UploadGameDataAsync(id.ToString(), testData);
Debug.Log(result
? "<color=green>完整流程测试成功!</color>"
: "<color=red>完整流程测试失败</color>");
}
// 测试存档反序列化
public void TestDeserializeArchive()
{
string path = "F:/gamerecord/";
if (!Directory.Exists(path))
{
Debug.LogError($"路径不存在: {path}");
return;
}
var datFiles = Directory.GetFiles(path, "*.dat");
if (datFiles.Length == 0)
{
Debug.LogWarning($"路径下没有找到 .dat 文件: {path}");
return;
}
Debug.Log($"=== 开始测试存档反序列化,共找到 {datFiles.Length} 个文件 ===");
int successCount = 0;
int failCount = 0;
foreach (var filePath in datFiles)
{
string fileName = Path.GetFileName(filePath);
byte[] data = File.ReadAllBytes(filePath);
Debug.Log($"读取文件: {fileName}, 大小: {data.Length} bytes");
var ossData = MemoryPackSerializer.Deserialize<OssData>(data);
if (ossData != null)
{
Debug.Log($"<color=green>[成功] {fileName} 反序列化成功</color>");
successCount++;
}
else
{
Debug.LogError($"<color=red>[失败] {fileName} 反序列化结果为 null</color>");
failCount++;
}
}
Debug.Log($"=== 反序列化完成: 成功 {successCount} / 失败 {failCount} / 共 {datFiles.Length} 个文件 ===");
}
}
}