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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "fsCCloudSave.h"

#if defined fsCloudSave

#include <fsCore/src/fsCAppSettings.h>
#include <fsCore/src/fsCGlobalSavedData.h>
#include <fsCore/src/debug/fsLog.h>

#include <fsPlatform/cloudsave/fsCloudSaveFactory.h>

#include "fsCAppCoreMain.h"
#include "fsCProfileManager.h"

namespace
{
	// Bounded so a quitting app is never held up for long by the network.
	constexpr std::int64_t kShutdownTimeoutMs = 2000;

	std::filesystem::path userSettingsPathGet(const fsStr& pFileName)
	{
		const fsCResourceName name(pFileName, fsCResourceName::eRES_LOCATION_USER_SETTINGS);
		return std::filesystem::u8path(name.nameGet().utf8Get());
	}
}

fsCCloudSave::fsCCloudSave()
{
}

fsCCloudSave::~fsCCloudSave()
{
	shutdown();
}

void fsCCloudSave::bootBeforeProfilesLoad()
{
	fsCProfileManager* const profiles = fsCProfileManager::instanceGet();

	fsCloudSaveConfig serviceConfig;
	const fsStr localFolder = fsCAppCoreMain::instanceGet()->settingsGet()->strGetWithDefault("cloudSaveLocalFolder", "");
	serviceConfig.localFolderPath = localFolder.emptyGet() ?
		userSettingsPathGet("cloudsave_local").u8string() : std::string(localFolder.utf8Get());

	auto service = fsCloudSaveServiceCreate();
	service->initialize(serviceConfig);

	fsCCloudSaveSync::Config config;
	config.statePath = userSettingsPathGet("cloudsave.state");
	config.deviceId = profiles->savedDataGet()->uniqueIDGet().utf8Get();
	config.log = [](const std::string& pMsg) { fsLogMsg("%s", pMsg.c_str()); };

	mSync = std::make_unique<fsCCloudSaveSync>(std::move(service), std::move(config));
	mBoot = mSync->boot(profilesGet());

	profiles->saveListenerSet(this);
}

void fsCCloudSave::bootAfterProfilesLoad()
{
	if (!mSync || fsCCloudSaveSync::eBootAction::eRESTORE != mBoot.action)
	{
		return;
	}

	fsCProfileManager* const profiles = fsCProfileManager::instanceGet();
	profiles->profileRestore(fsStr(mBoot.restorePlayerName.c_str()), fsStr(mBoot.restoreData.c_str(), mBoot.restoreData.size()));

	// newProfileCreate() appends, so the restored profile is the last one.
	const fsS32 restored = profiles->savedDataGet()->numProfilesGet() - 1;
	mSync->restoreComplete(profileMake(profiles->savedDataGet()->profileFileNameGet(restored),
		profiles->savedDataGet()->profileNameGet(restored)));

	mBoot = fsCCloudSaveSync::BootResult();
}

void fsCCloudSave::update()
{
	if (mSync)
	{
		mSync->update();
	}
}

void fsCCloudSave::flush()
{
	if (mSync)
	{
		mSync->flush();
	}
}

void fsCCloudSave::shutdown()
{
	if (!mSync)
	{
		return;
	}

	if (fsCProfileManager::instanceGet())
	{
		fsCProfileManager::instanceGet()->saveListenerSet(nullptr);
	}
	mSync->shutdown(kShutdownTimeoutMs);
	mSync.reset();
}

void fsCCloudSave::userDataSaved(const fsCResourceName& pFileName, const fsStr& pPlayerName)
{
	if (mSync && syncEligibleGet(pPlayerName))
	{
		mSync->profileSaved(profileMake(pFileName, pPlayerName));
	}
}

void fsCCloudSave::profileDeleted(const fsCResourceName& pFileName)
{
	if (mSync)
	{
		mSync->profileDeleted(profileMake(pFileName, fsStr()).fileKey);
	}
}

fsBool fsCCloudSave::syncEligibleGet(const fsStr& pPlayerName)
{
	// Tester profiles must never reach a player's cloud. (Autotest names live in gfHogCore,
	// which time-management titles do not link.)
	return pPlayerName != fsCProfileManager::mReleaseTesterName &&
		pPlayerName != fsCProfileManager::mDevTesterName;
}

fsCloudSaveProfile fsCCloudSave::profileMake(const fsCResourceName& pFileName, const fsStr& pPlayerName)
{
	fsCloudSaveProfile profile;
	profile.path = std::filesystem::u8path(pFileName.nameGet().utf8Get());
	// File name only: the settings folder can move between app versions (iOS containers).
	profile.fileKey = profile.path.filename().u8string();
	profile.playerName = pPlayerName.utf8Get();
	return profile;
}

std::vector<fsCloudSaveProfile> fsCCloudSave::profilesGet() const<--- The member function 'fsCCloudSave::profilesGet' can be static.
{
	const fsCGlobalSavedData* const savedData = fsCProfileManager::instanceGet()->savedDataGet();

	std::vector<fsCloudSaveProfile> profiles;
	for (fsS32 profileInd = 0; profileInd < savedData->numProfilesGet(); ++profileInd)
	{
		const fsStr name = savedData->profileNameGet(profileInd);
		if (syncEligibleGet(name))
		{
			profiles.push_back(profileMake(savedData->profileFileNameGet(profileInd), name));
		}
	}
	return profiles;
}

#endif