#nullable enable
using Barotrauma.IO;
using Microsoft.Xna.Framework;
using Steamworks;
using System;
using System.Diagnostics.CodeAnalysis;
namespace Barotrauma.Steam;
internal static partial class RemoteStorageHelper
{
public static readonly Color SteamColor = Color.DodgerBlue;
public static readonly string DebugPrefix = $"‖color:{SteamColor.ToStringHex()}‖[Remote Storage]‖end‖";
/// Attempts to read a file from remote storage into a byte array.
/// The remote file to read from.
/// The bytes read from the remote file. Returns if the operation failed.
///
/// if the operation was successful.
/// if the operation failed.
///
public static bool TryRead(this SteamRemoteStorage.RemoteFile remoteFile, [NotNullWhen(returnValue: true)] out byte[]? bytes, bool logError = true)
{
bytes = SteamRemoteStorage.FileRead(remoteFile.Filename);
bool success = bytes != null;
if (logError && !success)
{
DebugConsole.ThrowError($"{DebugPrefix} Failed to read file \"{remoteFile.Filename}\" from remote storage: operation failed.");
}
return success;
}
/// Attempts to write a file to remote storage.
/// The path of the local file to read from.
/// The name of the remote file to write to. If , the file name of is used.
/// If , overwriting existing remote files is allowed.
///
/// if the operation was successful.
/// if the operation failed.
///
public static bool TryWrite(string localPath, string? saveAs = null, bool allowOverwrite = false, bool logError = true)
{
string fileName = saveAs ?? Path.GetFileName(localPath);
if (!allowOverwrite && SteamRemoteStorage.FileExists(fileName))
{
if (logError)
{
DebugConsole.ThrowError($"{DebugPrefix} Failed to write file \"{fileName}\" to remote storage: file already exists.");
}
return false;
}
byte[] data;
try
{
data = File.ReadAllBytes(localPath);
}
catch (Exception exception)
{
if (logError)
{
DebugConsole.ThrowError($"{DebugPrefix} Failed to read file \"{fileName}\" while writing to remote storage: {exception}");
}
return false;
}
bool success = SteamRemoteStorage.FileWrite(fileName, data);
if (logError && !success)
{
DebugConsole.ThrowError($"{DebugPrefix} Failed to write file \"{fileName}\" to remote storage: operation failed.");
}
return success;
}
/// Attempts to delete a file from remote storage.
/// The name of the remote file to delete.
///
/// if the operation was successful.
/// if the operation failed.
///
public static bool TryDelete(string fileName, bool logError = true)
{
bool success = SteamRemoteStorage.FileDelete(fileName);
if (logError && !success)
{
DebugConsole.ThrowError($"{DebugPrefix} Failed to delete file \"{fileName}\" from remote storage: operation failed.");
}
return success;
}
/// Checks if a file is stored remotely.
/// The name of the remote file to check.
///
/// if the file is stored.
/// if the file is not stored or the operation failed.
///
public static bool IsStored(string fileName) => SteamRemoteStorage.FileExists(fileName);
}