134 lines
3.7 KiB
C#
134 lines
3.7 KiB
C#
/*
|
||
* @Author: 白哉
|
||
* @Description:
|
||
* @Date: 2026年03月10日 星期二 14:03:29
|
||
* @Modify:
|
||
*/
|
||
|
||
|
||
using System;
|
||
using Logic.CrashSight;
|
||
using Steamworks;
|
||
using UnityEngine;
|
||
|
||
|
||
namespace TH1_Logic.Tools
|
||
{
|
||
public static class SteamCloudStorage
|
||
{
|
||
/// <summary>
|
||
/// 是否可用(Steamworks 已初始化且云存储已启用)
|
||
/// </summary>
|
||
public static bool IsAvailable()
|
||
{
|
||
try
|
||
{
|
||
return SteamManager.Initialized && SteamRemoteStorage.IsCloudEnabledForAccount()
|
||
&& SteamRemoteStorage.IsCloudEnabledForApp();
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 写入文件到 Steam Cloud
|
||
/// </summary>
|
||
/// <param name="fileName">虚拟文件名,如 "achievement.json"</param>
|
||
/// <param name="data">UTF-8 字节数据</param>
|
||
/// <returns>是否成功</returns>
|
||
public static bool WriteFile(string fileName, byte[] data)
|
||
{
|
||
if (!IsAvailable()) return false;
|
||
|
||
try
|
||
{
|
||
bool success = SteamRemoteStorage.FileWrite(fileName, data, data.Length);
|
||
if (!success)
|
||
{
|
||
LogSystem.LogError($"[SteamCloud] 写入失败: {fileName}");
|
||
}
|
||
return success;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogSystem.LogError($"[SteamCloud] 写入异常: {fileName} | {e.Message}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从 Steam Cloud 读取文件
|
||
/// </summary>
|
||
/// <param name="fileName">虚拟文件名</param>
|
||
/// <returns>文件内容字节数组,失败返回 null</returns>
|
||
public static byte[] ReadFile(string fileName)
|
||
{
|
||
if (!IsAvailable()) return null;
|
||
|
||
try
|
||
{
|
||
if (!SteamRemoteStorage.FileExists(fileName)) return null;
|
||
|
||
int size = SteamRemoteStorage.GetFileSize(fileName);
|
||
if (size <= 0) return null;
|
||
|
||
byte[] buffer = new byte[size];
|
||
int readBytes = SteamRemoteStorage.FileRead(fileName, buffer, size);
|
||
|
||
if (readBytes <= 0) return null;
|
||
|
||
// 实际读取长度可能小于 buffer 大小
|
||
if (readBytes < size)
|
||
{
|
||
byte[] trimmed = new byte[readBytes];
|
||
Array.Copy(buffer, trimmed, readBytes);
|
||
return trimmed;
|
||
}
|
||
|
||
return buffer;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogSystem.LogError($"[SteamCloud] 读取异常: {fileName} | {e.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除 Steam Cloud 上的文件
|
||
/// </summary>
|
||
public static bool DeleteFile(string fileName)
|
||
{
|
||
if (!IsAvailable()) return false;
|
||
|
||
try
|
||
{
|
||
return SteamRemoteStorage.FileDelete(fileName);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogSystem.LogError($"[SteamCloud] 删除异常: {fileName} | {e.Message}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查文件是否存在于 Steam Cloud
|
||
/// </summary>
|
||
public static bool FileExists(string fileName)
|
||
{
|
||
if (!IsAvailable()) return false;
|
||
|
||
try
|
||
{
|
||
return SteamRemoteStorage.FileExists(fileName);
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |