How andantetribe/localprefs works
Project Type and Purpose
• This is an open-source, modular library (SDK) targeting both .NET Standard (2.1+) and Unity.
• Its goal is to provide a unified, high-performance “local preferences” (save/load) API that overcomes Unity’s PlayerPrefs limitations.
High-Level Architecture & Main Components
a. Core Layer (src/LocalPrefs)
• ILocalPrefs interface – abstraction for save/load operations
• LocalPrefs.Shared – singleton entry point
• FileAccessor – pluggable file I/O
• CryptoFileAccessor – decorator adding encryption/decryption
b. Serializer Adapters
• JsonLocalPrefs (src/LocalPrefs.Json) – implements ILocalPrefs via System.Text.Json
• MessagePackLocalPrefs (src/LocalPrefs.MessagePack) – implements ILocalPrefs via MessagePack-CSharp
c. Internal Utilities (src/LocalPrefs.Internal)
• ByteBufferWriter, PooledList – buffers, pooling for MessagePack
d. Unity Integration (src/LocalPrefs.Unity)
• UnityInitializer – auto-configures LocalPrefs.Shared at startup
• WebGL plugins (.jslib) – native bindings for IndexedDB/LocalStorage
• IDBUtils, LSUtils, IDBStream, LSStream – Web platform backends
e. Test Projects
• LocalPrefs.Tests – .NET unit tests for core and adapters
• LocalPrefs.Unity/Assets/Tests – Unity edit-mode & play-mode tests
f. CI/CD & Packaging (/.github/workflows, /bin)
• dotnet-test, unity-test workflow badges
• NuGet build+publish workflows for Core, Json, MessagePack packages
• Unity meta-file checker, release automation
Component Relationships & Data Flows
• Core Layer defines the abstraction (ILocalPrefs) and orchestration (LocalPrefs.Shared).
→ JsonLocalPrefs and MessagePackLocalPrefs implement this interface.
→ Each serializer adapter uses a FileAccessor (or CryptoFileAccessor) to read/write bytes.
• CryptoFileAccessor wraps a FileAccessor and is passed into JsonLocalPrefs/MessagePackLocalPrefs.
• UnityInitializer (Unity package) consumes LocalPrefs.Shared; on Unity startup it sets the save path depending on platform (persistentDataPath or LocalStorage/IndexedDB).
• On WebGL, jslib plugins provide low-level I/O – exposed via IDBUtils/LSUtils – and are consumed by FileAccessor under the hood when running in web.
Architectural Patterns & Design Principles
• Layered Architecture: Core abstraction → concrete adapters → platform plug-ins
• Dependency Inversion: high-level modules (LocalPrefs.Shared) depend on ILocalPrefs, not implementations
• Decorator Pattern: CryptoFileAccessor wraps FileAccessor for optional encryption
• Strategy Pattern: choice between JSON vs. MessagePack via interchangeable implementations
• Plugin Architecture: WebGL native .jslib plugins injected at runtime