1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "fsCloudSaveServiceSteam.h"

#include <steam_api.h>

#include <fsPlatform/steam/fsSteamSession.h>

#include <limits>

void fsCloudSaveServiceSteam::initialize(const fsCloudSaveConfig& config)
{
    if (!config.providerName.empty())
    {
        mProviderName = config.providerName;
    }
    mSessionAcquired = true;
    fsSteamSession::acquire();
}

const std::string& fsCloudSaveServiceSteam::providerNameGet() const
{
    return mProviderName;
}

bool fsCloudSaveServiceSteam::availableGet() const
{
    ISteamRemoteStorage* const storage = fsSteamSession::initialisedGet() ? SteamRemoteStorage() : nullptr;
    // Either switch off means Steam keeps the file locally only, so treat it as no cloud.
    return storage && storage->IsCloudEnabledForAccount() && storage->IsCloudEnabledForApp();
}

void fsCloudSaveServiceSteam::read(const std::string& name, ReadCallback done)
{
    mPending.push_back([this, name, done]()
    {
        ISteamRemoteStorage* const storage = availableGet() ? SteamRemoteStorage() : nullptr;
        if (!storage)
        {
            done(fsCloudSaveResult::eUNAVAILABLE, std::string());
            return;
        }
        if (!storage->FileExists(name.c_str()))
        {
            done(fsCloudSaveResult::eNOT_FOUND, std::string());
            return;
        }

        const int32 size = storage->GetFileSize(name.c_str());
        std::string data(size > 0 ? static_cast<std::size_t>(size) : 0, '\0');
        const int32 read = size > 0 ? storage->FileRead(name.c_str(), &data[0], size) : 0;
        if (read != size)
        {
            done(fsCloudSaveResult::eERROR, std::string());
            return;
        }
        done(fsCloudSaveResult::eOK, data);
    });
}

void fsCloudSaveServiceSteam::write(const std::string& name, const std::string& data, DoneCallback done)
{
    mPending.push_back([this, name, data, done]()
    {
        ISteamRemoteStorage* const storage = availableGet() ? SteamRemoteStorage() : nullptr;
        if (!storage)
        {
            done(fsCloudSaveResult::eUNAVAILABLE);
            return;
        }
        if (data.size() > static_cast<std::size_t>(std::numeric_limits<int32>::max()))
        {
            done(fsCloudSaveResult::eQUOTA_EXCEEDED);
            return;
        }

        // FileWrite fails when the app's Steam Cloud quota is full.
        const bool written = storage->FileWrite(name.c_str(), data.data(), static_cast<int32>(data.size()));
        done(written ? fsCloudSaveResult::eOK : fsCloudSaveResult::eQUOTA_EXCEEDED);
    });
}

void fsCloudSaveServiceSteam::remove(const std::string& name, DoneCallback done)
{
    mPending.push_back([this, name, done]()
    {
        ISteamRemoteStorage* const storage = availableGet() ? SteamRemoteStorage() : nullptr;
        if (!storage)
        {
            done(fsCloudSaveResult::eUNAVAILABLE);
            return;
        }
        if (!storage->FileExists(name.c_str()))
        {
            done(fsCloudSaveResult::eNOT_FOUND);
            return;
        }
        done(storage->FileDelete(name.c_str()) ? fsCloudSaveResult::eOK : fsCloudSaveResult::eERROR);
    });
}

void fsCloudSaveServiceSteam::update()
{
    fsSteamSession::tick();

    std::vector<std::function<void()>> pending;
    pending.swap(mPending);
    for (auto& call : pending)<--- Variable 'call' can be declared as reference to const
    {
        call();
    }
}

void fsCloudSaveServiceSteam::shutdown()
{
    update();
    if (mSessionAcquired)
    {
        fsSteamSession::release();
        mSessionAcquired = false;
    }
}