Skip to content
Open
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
run: |
dotnet test tests/PKForge.Domain.Tests/PKForge.Domain.Tests.csproj --no-restore --configuration Release
dotnet test tests/PKForge.Engine.Tests/PKForge.Engine.Tests.csproj --no-restore --configuration Release
dotnet test tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj --no-restore --configuration Release
- name: Decode permanent release signing key
if: github.ref_type == 'tag'
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "external/PKHeX-Plugins"]
path = external/PKHeX-Plugins
url = https://github.com/santacrab2/PKHeX-Plugins.git
[submodule "external/pkforge-sync-server"]
path = external/pkforge-sync-server
url = https://github.com/YoruDev-Ryland/pkforge-sync-server.git
32 changes: 32 additions & 0 deletions PKForge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{07C2787E
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromePreview", "tools\ChromePreview\ChromePreview.csproj", "{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKForge.Sync", "src\PKForge.Sync\PKForge.Sync.csproj", "{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKForge.Sync.Tests", "tests\PKForge.Sync.Tests\PKForge.Sync.Tests.csproj", "{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -117,12 +123,38 @@ Global
{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x64.Build.0 = Release|Any CPU
{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x86.ActiveCfg = Release|Any CPU
{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x86.Build.0 = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x64.ActiveCfg = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x64.Build.0 = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x86.ActiveCfg = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x86.Build.0 = Debug|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|Any CPU.Build.0 = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x64.ActiveCfg = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x64.Build.0 = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x86.ActiveCfg = Release|Any CPU
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x86.Build.0 = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x64.Build.0 = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x86.ActiveCfg = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x86.Build.0 = Debug|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|Any CPU.Build.0 = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x64.ActiveCfg = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x64.Build.0 = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x86.ActiveCfg = Release|Any CPU
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{59362056-D892-4D4D-A404-C344D7A25EEF} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84}
{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Not affiliated with Nintendo, Game Freak, or The Pokémon Company.
- **A Bank like Pokémon Home.** Store your whole collection in a cross-game Bank with
as many themed boxes as you need. Organize with the gamepad, keep living dexes, and pull
anything back out into any save whenever you need it.
- **Bank Sync (optional).** Keep one Bank on all your devices through a
[PKForge Sync server](https://github.com/YoruDev-Ryland/pkforge-sync-server) you run with Docker.
Every device keeps working offline, and changes made on two devices are shown to you, never
silently lost.
- **Track your collection.** The Living Dex tracker follows all 1025 Pokémon across
every generation, including shiny variants, so you always know what you still need.
- **Living Dex Autopilot.** Builds a living dex from the Pokémon you already own across
Expand Down Expand Up @@ -185,6 +189,7 @@ Join for updates, support, bug reports, feature requests, or just to chat about
git submodule update --init --recursive
dotnet test tests/PKForge.Domain.Tests/PKForge.Domain.Tests.csproj
dotnet test tests/PKForge.Engine.Tests/PKForge.Engine.Tests.csproj
dotnet test tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj
dotnet build src/PKForge.App/PKForge.App.csproj -f net10.0-android
```

Expand All @@ -197,6 +202,7 @@ src/PKForge.Domain contracts and DTOs, no engine or Android dependencie
src/PKForge.Engine adapters over pinned PKHeX.Core
src/PKForge.AutoMod compiles the Auto Legality Mod against our Core
src/PKForge.Infrastructure bank, backups, atomic save writer
src/PKForge.Sync Bank sync with a PKForge Sync server (offline-first, conflict-aware)
src/PKForge.App MAUI app, SkiaSharp UI, gamepad and second screen
src/PKForge.Chrome the design system: tokens and painters, pure Skia
tools/ChromePreview renders the design system off-device
Expand Down
1 change: 1 addition & 0 deletions external/pkforge-sync-server
Submodule pkforge-sync-server added at 40b1ae
47 changes: 44 additions & 3 deletions src/PKForge.App/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ public sealed class App : Application
/// <summary>Raised when Android resumes the app (including after the install-permission screen).</summary>
public static event Action? Resumed;
public static event Action? Suspended;

/// <summary>Android can stop the activity and recreate it without an OnResume; see <see cref="Domain.AppForeground"/>.</summary>
private static readonly Domain.AppForeground Foreground = new();

protected override void OnSleep()
{
Suspended?.Invoke();
Music?.PauseForBackground();
if (Foreground.EnterBackground())
{
Suspended?.Invoke();
Music?.PauseForBackground();
Sync?.Suspend();
}
base.OnSleep();
}

private static PKForge.Sync.SyncCoordinator? Sync =>
IPlatformApplication.Current?.Services.GetService<PKForge.Sync.SyncCoordinator>();

private static Platforms.Android.MusicPlayer? Music =>
IPlatformApplication.Current?.Services.GetService<Domain.IMusicPlayer>() as Platforms.Android.MusicPlayer;

Expand All @@ -20,13 +31,38 @@ protected override void OnStart()
{
base.OnStart();
Music?.MaybeAutostart();
// Bank Sync: a first sync now, and another whenever the network comes back.
Connectivity.Current.ConnectivityChanged += (_, args) =>
{
if (args.NetworkAccess != NetworkAccess.None) Sync?.NetworkAvailable();
};
Sync?.RequestSync();
}

protected override void OnResume()
{
base.OnResume();
BackInFront();
}

/// <summary>
/// A recreated activity (a configuration change, or Android reclaiming it while the process
/// lives) only activates its new window and never calls OnResume, which left Bank Sync paused
/// until the app restarted. Activation finishes the return in that case.
/// </summary>
private static void OnWindowActivated(object? sender, EventArgs args)
{
if (BackInFront()) Services.AppLog.Info("app", "Back in front without OnResume (the activity was recreated)");
}

/// <summary>Undoes OnSleep, once per return. False when the app never left.</summary>
private static bool BackInFront()
{
if (!Foreground.EnterForeground()) return false;
Music?.ResumeFromBackground();
Sync?.Resume();
Resumed?.Invoke();
return true;
}

public App()
Expand All @@ -36,6 +72,9 @@ public App()
// Every page reads its chrome colors when it is built, so the scheme comes first.
Services.ColorThemeSetting.ApplyStored();

// Bank Sync: the living dex start box now lives with the Bank; move it before anything touches boxes.
Services.LivingDexAutopilot.MoveLegacyStartBox();

// Warm both Skia faces off the ctor: the party view paints before the first
// save opens, and its cached nickname font must never pin the placeholder.
_ = Views.PixelFont.WarmAsync();
Expand Down Expand Up @@ -100,7 +139,9 @@ protected override Window CreateWindow(IActivationState? activationState)
{
var services = IPlatformApplication.Current?.Services
?? throw new InvalidOperationException("MAUI services are unavailable.");
return new Window(CreateRoot(services));
var window = new Window(CreateRoot(services));
window.Activated += OnWindowActivated;
return window;
}
catch (Exception error)
{
Expand Down
23 changes: 22 additions & 1 deletion src/PKForge.App/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using PKForge.Domain;
using PKForge.Engine;
using PKForge.Infrastructure;
using PKForge.Sync;
using SkiaSharp.Views.Maui.Controls.Hosting;

namespace PKForge.App;
Expand Down Expand Up @@ -40,9 +41,28 @@ public static MauiApp CreateMauiApp()
builder.Services.AddSingleton<IEncounterLookup, EncounterLookupService>();
builder.Services.AddSingleton<IBackupService>(_ =>
new FileBackupService(Path.Combine(FileSystem.AppDataDirectory, "backups")));
var bankRoot = Path.Combine(FileSystem.AppDataDirectory, "bank");
builder.Services.AddSingleton(_ => new FileBankService(bankRoot));
builder.Services.AddSingleton(_ => BankBoxDecor.Decor);
builder.Services.AddSingleton(_ => new SyncStateStore(Path.Combine(FileSystem.AppDataDirectory, "sync")));
builder.Services.AddSingleton<ISyncSecrets, SecureSyncSecrets>();
builder.Services.AddSingleton<SyncHttp>();
builder.Services.AddSingleton(sp => new SyncConnection(
sp.GetRequiredService<SyncStateStore>(), sp.GetRequiredService<ISyncSecrets>(), sp.GetRequiredService<SyncHttp>().For));
builder.Services.AddSingleton(sp => new SyncedBankService(
sp.GetRequiredService<FileBankService>(), sp.GetRequiredService<BankDecor>(), sp.GetRequiredService<SyncStateStore>()));
builder.Services.AddSingleton(_ => new FileBankSnapshots(bankRoot, Path.Combine(FileSystem.AppDataDirectory, "bank-snapshots")));
builder.Services.AddSingleton(sp => new SyncCoordinator(
sp.GetRequiredService<SyncConnection>(), sp.GetRequiredService<SyncedBankService>(), sp.GetRequiredService<SyncStateStore>(),
sp.GetRequiredService<FileBankSnapshots>())
{
Trace = message => Services.AppLog.Info("sync", message),
});
builder.Services.AddSingleton<BankRestorePoints>();
builder.Services.AddSingleton<IBankService>(sp =>
{
var bank = new FileBankService(Path.Combine(FileSystem.AppDataDirectory, "bank"));
var bank = new BankServiceSwitch(
sp.GetRequiredService<FileBankService>(), sp.GetRequiredService<SyncedBankService>(), sp.GetRequiredService<SyncConnection>());
// One-time index migration, off the UI thread: legacy entries get their exact format
// (only when provable) and sprite traits recorded, then the index is marked so no
// entry is read again on later launches. Concurrent bank edits are never undone.
Expand Down Expand Up @@ -103,6 +123,7 @@ public static MauiApp CreateMauiApp()
builder.Services.AddTransient<HomePage>();
builder.Services.AddTransient<SecondScreenBoxPage>();
builder.Services.AddTransient<BankPage>();
builder.Services.AddTransient<BankSyncPage>();
App.Trace("builder.Build()");
return builder.Build();
}
Expand Down
2 changes: 2 additions & 0 deletions src/PKForge.App/PKForge.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,7 @@
<ProjectReference Include="../PKForge.Chrome/PKForge.Chrome.csproj" />
<ProjectReference Include="../PKForge.Engine/PKForge.Engine.csproj" />
<ProjectReference Include="../PKForge.Infrastructure/PKForge.Infrastructure.csproj" />
<ProjectReference Include="../PKForge.Sync/PKForge.Sync.csproj" />
</ItemGroup>

</Project>
11 changes: 10 additions & 1 deletion src/PKForge.App/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="PKForge" android:icon="@mipmap/pkforge" android:roundIcon="@mipmap/pkforge_round" android:allowBackup="true" android:supportsRtl="true" />
<!-- Bank Sync: photograph another device's pairing QR code. -->
<uses-permission android:name="android.permission.CAMERA" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
<!-- usesCleartextTraffic: a self-hosted Bank Sync server on the home network is often plain http.
PKForge.Sync's ServerAddress refuses http:// for anything that is not a home-network host. -->
<application android:label="PKForge" android:icon="@mipmap/pkforge" android:roundIcon="@mipmap/pkforge_round" android:allowBackup="true" android:supportsRtl="true" android:usesCleartextTraffic="true" />
</manifest>
3 changes: 3 additions & 0 deletions src/PKForge.App/Platforms/Android/linker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
Preserve both engine assemblies completely. -->
<assembly fullname="PKHeX.Core" preserve="all" />
<assembly fullname="PKHeX.Core.AutoMod" preserve="all" />
<!-- Bank Sync messages and state are (de)serialized by reflection: keep their types whole. -->
<assembly fullname="PKForge.Sync" preserve="all" />
<assembly fullname="PKForge.Sync.Protocol" preserve="all" />
</linker>
16 changes: 8 additions & 8 deletions src/PKForge.App/Services/BankBoxDecor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace PKForge.App.Services;

/// <summary>The Bank's box names and wallpapers, stored beside its index.</summary>
/// <summary>
/// The Bank's box names, wallpapers and settings, stored beside its index. One instance for the whole
/// app (sync applies another device's decor through the same objects the pages read).
/// </summary>
public static class BankBoxDecor
{
private static readonly Lazy<BankBoxNames> LazyNames = new(() => new BankBoxNames(Root));
private static readonly Lazy<BankBoxWallpapers> LazyWallpapers = new(() => new BankBoxWallpapers(Root));
private static readonly Lazy<BankDecor> LazyDecor = new(() => new BankDecor(Path.Combine(FileSystem.AppDataDirectory, "bank")));

private static string Root => Path.Combine(FileSystem.AppDataDirectory, "bank");

public static BankBoxNames Names => LazyNames.Value;

public static BankBoxWallpapers Wallpapers => LazyWallpapers.Value;
public static BankDecor Decor => LazyDecor.Value;
public static BankBoxNames Names => Decor.Names;
public static BankBoxWallpapers Wallpapers => Decor.Wallpapers;

/// <summary>The box's name, or "Box 07" when it has none.</summary>
public static string Label(int box) => Names.Get(box) ?? $"Box {box + 1:00}";
Expand Down
39 changes: 39 additions & 0 deletions src/PKForge.App/Services/BankRestorePoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using PKForge.Domain;
using PKForge.Infrastructure;
using PKForge.Sync;

namespace PKForge.App.Services;

/// <summary>
/// The Bank's own restore points (copies taken before Bank Sync replaced the whole Bank), listed beside the
/// save restore points. Restoring stops Bank Sync first: otherwise the old Bank would go out as new edits.
/// </summary>
public sealed class BankRestorePoints(FileBankSnapshots snapshots, FileBankService bank, BankDecor decor, SyncCoordinator sync)
{
private const string IdPrefix = "bank:";

public static bool IsBank(BackupInfo point) => point.BackupId.StartsWith(IdPrefix, StringComparison.Ordinal);

public bool SyncIsOn => sync.Connection.IsConnected;

public IReadOnlyList<BackupInfo> List() =>
[.. snapshots.List().Select(s => new BackupInfo(IdPrefix + s.Id, s.CreatedUtc, "", "PKForge Bank", "Bank", 0, s.SizeBytes,
s.PokemonCount is { } count ? $"{s.Reason} {count} Pokémon." : s.Reason))];

/// <summary>Puts the Bank back as it was in <paramref name="point"/>; the current Bank becomes a restore point first.</summary>
public async Task RestoreAsync(BackupInfo point)
{
if (!IsBank(point)) throw new ArgumentException("Not a Bank restore point.", nameof(point));
if (sync.Connection.IsConnected)
{
sync.SignOut();
await sync.WhenIdleAsync();
}
await Task.Run(() =>
{
snapshots.Restore(point.BackupId[IdPrefix.Length..], $"Before restoring the Bank from {point.CreatedUtc:yyyy-MM-dd HH:mm} UTC.");
bank.Reload();
decor.Reload();
});
}
}
32 changes: 32 additions & 0 deletions src/PKForge.App/Services/BankServiceSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using PKForge.Domain;
using PKForge.Infrastructure;
using PKForge.Sync;

namespace PKForge.App.Services;

/// <summary>
/// The Bank the app uses: the plain <see cref="FileBankService"/> in Local mode, the
/// <see cref="SyncedBankService"/> over the same files while this device is connected to a sync server.
/// Both wrap one Bank, so switching never moves or copies anything.
/// </summary>
public sealed class BankServiceSwitch(FileBankService local, SyncedBankService synced, SyncConnection connection) : IBankService
{
private IBankService Current => connection.IsConnected ? synced : local;

public IReadOnlyList<BankEntry> GetAll() => Current.GetAll();
public int BoxCount => Current.BoxCount;
public BankEntry Add(byte[] data, BankEntryInfo info) => Current.Add(data, info);
public IReadOnlyList<BankEntry> AddMany(IReadOnlyList<BankDeposit> deposits) => Current.AddMany(deposits);
public byte[] GetData(Guid id) => Current.GetData(id);
public void Move(Guid id, int box, int slot) => Current.Move(id, box, slot);
public void Remove(Guid id) => Current.Remove(id);
public int RemoveMany(IReadOnlyList<Guid> ids) => Current.RemoveMany(ids);
public void Replace(Guid id, byte[] data, BankEntryInfo info) => Current.Replace(id, data, info);
public void AddBox() => Current.AddBox();
public void RemapBoxes(BankBoxRemap remap) => Current.RemapBoxes(remap);
public int UpdateInfo(IReadOnlyList<(Guid Id, BankEntryInfo Info)> updates) => Current.UpdateInfo(updates);
public int MigrationVersion => Current.MigrationVersion;
public int CompleteMigration(int version, IReadOnlyList<(Guid Id, BankEntryInfo Expected, BankEntryInfo Info)> updates) =>
Current.CompleteMigration(version, updates);
public int Place(IReadOnlyList<(Guid Id, int Box, int Slot)> placements) => Current.Place(placements);
}
Loading