Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ add_library(lootlocker_core STATIC
src/core/rate_limiter.cpp
src/core/json.cpp
src/core/query_parameters.cpp
src/core/state_writer.cpp
src/core/executor.cpp
src/core/client.cpp
)
Expand Down
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,70 @@ definition.
over a shared, immutable document, which is what makes it cheap to pass around and safe to
hold across a callback. Writing needs its own type.

### Persistence

The SDK remembers a session between launches through a `IStateWriter`. The default writes one
file next to the game; a console or an engine injects its own.

```cpp
// The default: one file, next to the game. Constructed lazily, on first use, so a client
// that never persists anything performs no file I/O.
lootlocker::Config config;
config.domain_key = "your-domain";

// Or your own store, for a console's save-data API or an engine's settings.
//
// Keep the shared_ptr alive for as long as the client uses it: the client holds a weak_ptr,
// so a temporary would expire before the client was even constructed.
std::shared_ptr<MyEngineStateWriter> my_writer = std::make_shared<MyEngineStateWriter>();
config.state_writer = my_writer;

// Or nothing at all, if you would rather re-authenticate every launch.
std::shared_ptr<lootlocker::NullStateWriter> no_persistence =
std::make_shared<lootlocker::NullStateWriter>();
config.state_writer = no_persistence;
```

The interface is typed rather than strings-only, because a store that only speaks strings
forces every adopter to write the same conversions and get the same rounding wrong:

```cpp
class IStateWriter {
public:
virtual bool GetString(const std::string& key, std::string& out) = 0;
virtual void SetString(const std::string& key, const std::string& value) = 0;

virtual bool GetInt64(const std::string& key, std::int64_t& out) = 0;
virtual void SetInt64(const std::string& key, std::int64_t value) = 0;

virtual bool GetUInt64(const std::string& key, std::uint64_t& out) = 0;
virtual void SetUInt64(const std::string& key, std::uint64_t value) = 0;

virtual bool GetDouble(const std::string& key, double& out) = 0;
virtual void SetDouble(const std::string& key, double value) = 0;

virtual bool GetBool(const std::string& key, bool& out) = 0;
virtual void SetBool(const std::string& key, bool value) = 0;

virtual void Erase(const std::string& key) = 0;
virtual bool Has(const std::string& key) = 0;
};
```

`GetUInt64` exists separately from `GetInt64` because `player_id` is a `uint64` on the wire
and a valid id above `INT64_MAX` must round-trip exactly. A getter returns `false` and leaves
`out` untouched when the key is absent **or holds a different type** — it never converts,
because guessing is how a corrupt store becomes a wrong player id.

Implementations must not throw. Persistence is best-effort: a failed write never fails a
request.

`FileStateWriter` writes a flat `type:key=value` text file, whole, on every change. It is
**not atomic across a crash** — a power loss mid-write loses the file, and the cost is one
re-authentication. Write-temp-then-rename is not portable to every platform this SDK
targets, so the limitation is documented rather than hidden. A corrupt line is skipped on
load rather than losing the whole file.

### There is no parser seam

Unlike the transport or the state writer, the JSON parser is not injectable. It is internal
Expand Down
17 changes: 17 additions & 0 deletions include/lootlocker/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace lootlocker {

class Client;
class IStateWriter;

namespace core {
class Executor;
Expand Down Expand Up @@ -79,6 +80,22 @@ class Client {
// more.
const std::string& GetInstanceIdentifier() const;

// Replaces the state writer. The client observes it through a `weak_ptr`, so the
// adopter keeps ownership.
//
// Unlike `SetTransport`, this is allowed at any time, because a state writer has no
// in-flight state to strand. That is a property of the implementation rather than an
// invitation: swapping the writer mid-game is not a use case anyone has, and a game
// that does it will find the new writer knows nothing the old one had already stored.
// Set it once, before the first request.
//
// An empty or dead `weak_ptr` falls back to the default `FileStateWriter`.
void SetStateWriter(std::weak_ptr<IStateWriter> writer);

// The state writer in use. Never empty: the client always has one, defaulting to
// `FileStateWriter`.
std::weak_ptr<IStateWriter> GetStateWriter() const;

private:
// The correct form: the function is declared in `core` above, so the friend
// declaration names it qualified rather than declaring a new one in this scope.
Expand Down
4 changes: 2 additions & 2 deletions include/lootlocker/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace lootlocker {

class StateWriter;
class IStateWriter;
class IHttpTransport;
class TimezoneProvider;

Expand Down Expand Up @@ -95,7 +95,7 @@ struct Config {

// Optional overrides. Empty means "use the SDK default".
std::weak_ptr<Logger> logger; // default writes to stderr
std::weak_ptr<StateWriter> state_writer; // default is FileStateWriter
std::weak_ptr<IStateWriter> state_writer; // default is FileStateWriter
std::weak_ptr<IHttpTransport> transport; // default is the libcurl one

// The least-verbose level the SDK emits (D26).
Expand Down
139 changes: 139 additions & 0 deletions include/lootlocker/state_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

// Persistence (D11, R18). The seam that lets the SDK remember a session between launches.
//
// The problem this solves: guest auth's whole point is that a player keeps the same account
// across restarts. That requires remembering `player_identifier`, and there is no portable
// way to write a file on every platform this SDK targets — a console has its own save-data
// API, an engine has its own settings store, and a plain application has a filesystem.
//
// So the SDK defines the interface and ships two implementations: `FileStateWriter`, which
// writes one file next to the game, and `NullStateWriter`, which writes nothing. An engine
// or a console injects its own through `Config::state_writer` or `Client::SetStateWriter`.
//
// **Typed accessors rather than strings-only.** A store that only speaks strings forces
// every adopter to write the same conversions and get the same rounding wrong — and the
// `player_id` case is exactly where that bites, because a `uint64` above `INT64_MAX` does
// not survive a round trip through a `double` or a signed integer.
//
// **Implementations must not throw** (D7). A failed write is reported by returning false
// from the getter or by silently doing nothing from the setter; the SDK treats persistence
// as best-effort and never fails a request because a save failed.

#include <cstdint>
#include <memory>
#include <string>

namespace lootlocker {

class IStateWriter {
public:
virtual ~IStateWriter() = default;

// Each getter returns false and leaves `out` untouched when the key is absent or holds
// a value of a different type. It does not convert: a key written as a string is not
// readable as an integer, because guessing is how a corrupt store becomes a wrong
// player id.
virtual bool GetString(const std::string& key, std::string& out) = 0;
virtual void SetString(const std::string& key, const std::string& value) = 0;

virtual bool GetInt64(const std::string& key, std::int64_t& out) = 0;
virtual void SetInt64(const std::string& key, std::int64_t value) = 0;

// Unsigned, because `player_id` is a `uint64` on the wire and a valid id above
// `INT64_MAX` must round-trip exactly (D4).
virtual bool GetUInt64(const std::string& key, std::uint64_t& out) = 0;
virtual void SetUInt64(const std::string& key, std::uint64_t value) = 0;

virtual bool GetDouble(const std::string& key, double& out) = 0;
virtual void SetDouble(const std::string& key, double value) = 0;

virtual bool GetBool(const std::string& key, bool& out) = 0;
virtual void SetBool(const std::string& key, bool value) = 0;

virtual void Erase(const std::string& key) = 0;
virtual bool Has(const std::string& key) = 0;
};

// Writes nothing, and reports every key as absent.
//
// For a game that would rather re-authenticate every launch, and for a test that wants the
// no-persistence path. It is a real implementation rather than a null pointer, so the
// client never has to branch on whether a writer exists.
class NullStateWriter : public IStateWriter {
public:
bool GetString(const std::string& key, std::string& out) override;
void SetString(const std::string& key, const std::string& value) override;

bool GetInt64(const std::string& key, std::int64_t& out) override;
void SetInt64(const std::string& key, std::int64_t value) override;

bool GetUInt64(const std::string& key, std::uint64_t& out) override;
void SetUInt64(const std::string& key, std::uint64_t value) override;

bool GetDouble(const std::string& key, double& out) override;
void SetDouble(const std::string& key, double value) override;

bool GetBool(const std::string& key, bool& out) override;
void SetBool(const std::string& key, bool value) override;

void Erase(const std::string& key) override;
bool Has(const std::string& key) override;
};

// Writes one file, next to the game by default.
//
// The format is a flat text file of `type:key=value` lines, written whole on every change.
// Deliberately not JSON: the SDK already has a JSON parser, but using it here would mean a
// corrupt save file is a parse error the adopter has to handle, and a flat format degrades
// to "skip the bad line" instead.
//
// **Not atomic across a crash.** A write truncates and rewrites, so a power loss mid-write
// loses the file. That is acceptable for a session token — the cost is one re-authentication
// — and the alternative (write-temp-then-rename) is not portable to every platform this SDK
// targets. Documented rather than hidden.
class FileStateWriter : public IStateWriter {
public:
// `path` is the file to write. Empty means the default, which is
// `lootlocker-state.txt` in the current working directory.
//
// The file is read once, at construction, so a writer is a snapshot plus a write-through
// cache. A second writer on the same path will not see the first one's later writes.
explicit FileStateWriter(const std::string& path = std::string());
~FileStateWriter() override;

FileStateWriter(const FileStateWriter&) = delete;
FileStateWriter& operator=(const FileStateWriter&) = delete;

bool GetString(const std::string& key, std::string& out) override;
void SetString(const std::string& key, const std::string& value) override;

bool GetInt64(const std::string& key, std::int64_t& out) override;
void SetInt64(const std::string& key, std::int64_t value) override;

bool GetUInt64(const std::string& key, std::uint64_t& out) override;
void SetUInt64(const std::string& key, std::uint64_t value) override;

bool GetDouble(const std::string& key, double& out) override;
void SetDouble(const std::string& key, double value) override;

bool GetBool(const std::string& key, bool& out) override;
void SetBool(const std::string& key, bool value) override;

void Erase(const std::string& key) override;
bool Has(const std::string& key) override;

// The path in use, after the default was resolved.
const std::string& GetPath() const;

// True when the last write succeeded. A writer whose file is unwritable still serves
// reads from its cache, so a game on a read-only filesystem degrades to
// re-authenticating rather than failing.
bool LastWriteSucceeded() const;

private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};

} // namespace lootlocker
42 changes: 41 additions & 1 deletion src/core/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <lootlocker/curl_http_transport.h>
#include <lootlocker/http_transport.h>
#include <lootlocker/log.h>
#include <lootlocker/state_writer.h>

#include "clock.h"
#include "executor.h"
Expand Down Expand Up @@ -30,6 +31,25 @@ struct Client::Impl {
// in, which is a supported configuration: the adopter injects their own.
std::shared_ptr<IHttpTransport> owned_transport;

// The state writer. The default is constructed lazily, on first use, so a client that
// never persists anything performs no file I/O - which is what keeps the "no implicit
// config" non-negotiable true (D9). An injected writer is held weakly and may expire at
// any time, so the default is also the fallback.
mutable std::shared_ptr<IStateWriter> default_state_writer;
std::weak_ptr<IStateWriter> state_writer;

// The writer to use, constructing the default on first call.
std::shared_ptr<IStateWriter> ResolveStateWriter() const
{
if (std::shared_ptr<IStateWriter> live = state_writer.lock()) {
return live;
}
if (!default_state_writer) {
default_state_writer = std::make_shared<FileStateWriter>();
}
return default_state_writer;
}

core::SteadyClock clock;
std::unique_ptr<core::Executor> executor;
};
Expand Down Expand Up @@ -110,9 +130,13 @@ Client::Client(const Config& config) : m_impl(new Impl())
#endif
}

// The state writer: the injected one wins, otherwise the file default is constructed
// lazily on first use. Deliberately NOT constructed here - a client that never persists
// anything must perform no file I/O, which is what keeps "no implicit config" true (D9).
m_impl->state_writer = config.state_writer;

core::ExecutorSettings settings;
settings.queue = config.queue;

// An invalid config means the policy was never validated, so it must not reach the
// limiter: a rejected 2-billion-second window would still derive 2 billion buckets and
// attempt a multi-gigabyte allocation during construction, which in a no-exceptions
Expand Down Expand Up @@ -167,6 +191,22 @@ const std::string& Client::GetInstanceIdentifier() const
return m_impl->instance_identifier;
}

void Client::SetStateWriter(std::weak_ptr<IStateWriter> writer)
{
// An empty or dead writer falls back to the default, which is constructed lazily on
// first use. A game that wants no persistence injects a `NullStateWriter` explicitly,
// which is a different statement from "I forgot".
m_impl->state_writer = writer;
}

std::weak_ptr<IStateWriter> Client::GetStateWriter() const
{
// Resolved at read time, not at set time: the injected writer is held weakly, so it may
// have died since. Returning the expired handle would leave the caller with nothing to
// persist to, which is the opposite of what a default is for.
return m_impl->ResolveStateWriter();
}

namespace core {

Executor& GetExecutor(Client& client)
Expand Down
Loading
Loading