Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalDb #969

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Handle build ID and locale updates separately
  • Loading branch information
ideka committed Jul 20, 2024
commit 3442147c1e76bb2ea2b2355561198e289c19f239
2 changes: 0 additions & 2 deletions Blish HUD/GameServices/LocalDb/DbHandler.Collections.cs
Original file line number Diff line number Diff line change
@@ -572,8 +572,6 @@ async Task<IEnumerable<T>> allPages<T>(IPaginatedClient<T> client, CancellationT
(WvwUpgrade x) => x.Id);

SQLiteContext.Create(_dbPath, _collections.Values);

GameService.Gw2Mumble.Info.BuildIdChanged += BuildIdChanged;
}

internal IMetaCollection? GetCollection(string name)
66 changes: 40 additions & 26 deletions Blish HUD/GameServices/LocalDb/DbHandler.cs
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@ public readonly bool IsTheSame(Version? other) {
}
}

internal Locale? ForcedLocale { get; set; }

private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private readonly Meta _meta;
private readonly string _metaPath;
@@ -37,51 +39,63 @@ public readonly bool IsTheSame(Version? other) {
internal IDbAccess GetAccess()
=> new DbAccess(new SQLiteContext(_dbPath, true), this);

internal int CountMismatchedLocaleCollections(Locale locale) {
lock (_lock) {
return _meta.Versions.Count(x => x.Value.Locale != locale);
}
}

internal async Task UpdateCollections() {
var version = new Version() {
BuildId = GameService.Gw2Mumble.Info.BuildId,
Locale = GameService.Overlay.UserLocale.Value,
};
int buildId = GameService.Gw2Mumble.Info.BuildId;

// Not ready to update
if (version.BuildId == 0) {
if (buildId == 0) {
return;
}

await using var db = new SQLiteContext(_dbPath, false);

// Make sure all collections are up-to-date
await Task.WhenAll(_collections.Select(async kv => {
string name = kv.Key;
ILoadCollection collection = kv.Value;
do {
// Make sure all collections are up-to-date
await Task.WhenAll(_collections.Select(async kv => {
string name = kv.Key;
ILoadCollection collection = kv.Value;

// Collection is already loading, skip
if (collection.Loading != null) {
return;
}

// BuildID already matches and there isn't a forced locale or forced locale also matches, skip
if (collection.CurrentVersion?.BuildId == buildId &&
(!ForcedLocale.HasValue || ForcedLocale == collection.CurrentVersion?.Locale)) {
return;
}

if (version.IsTheSame(collection.CurrentVersion) || collection.Loading != null) {
return;
}
await collection.Load(db, new Version() {
BuildId = buildId,
Locale = ForcedLocale ?? GameService.Overlay.UserLocale.Value,
}, _cts.Token);

await collection.Load(db, version, _cts.Token);
lock (_lock) {
if (collection.CurrentVersion is Version newVersion) {
_meta.Versions[name] = newVersion;
} else {
_meta.Versions.Remove(name);
}

lock (_lock) {
if (collection.CurrentVersion is Version newVersion) {
_meta.Versions[name] = newVersion;
} else {
_meta.Versions.Remove(name);
File.WriteAllText(_metaPath, JsonConvert.SerializeObject(_meta));
}
}));

File.WriteAllText(_metaPath, JsonConvert.SerializeObject(_meta));
}
}));
// If there's a forced locale and there's still collections with a different locale, go again.
} while (ForcedLocale is Locale forcedLocale && CountMismatchedLocaleCollections(forcedLocale) > 0);

// Vacuum database
await db.Connection.ExecuteScalarAsync<string>("VACUUM");
}

private void BuildIdChanged(object sender, ValueEventArgs<int> e) {
_ = UpdateCollections();
}

public void Dispose() {
GameService.Gw2Mumble.Info.BuildIdChanged -= BuildIdChanged;
_cts.Cancel();
}
}
18 changes: 18 additions & 0 deletions Blish HUD/GameServices/LocalDbService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blish_HUD.LocalDb;
using Gw2Sharp.WebApi;
using Microsoft.Xna.Framework;
using System.IO;

@@ -30,20 +31,37 @@ protected override void Load() {
Path.Combine(_basePath, DATABASE_FILENAME));

UpdateCollections();

Gw2Mumble.Info.BuildIdChanged += BuildIdChanged;
}

protected override void Update(GameTime gameTime) { /* NOOP */ }

protected override void Unload() {
Gw2Mumble.Info.BuildIdChanged -= BuildIdChanged;

_handler.Dispose();
}

internal void UpdateCollections() {
_ = _handler.UpdateCollections();
}

internal int CountMismatchedLocaleCollections(Locale locale) {
return _handler.CountMismatchedLocaleCollections(locale);
}

internal void ForceLocale(Locale locale) {
_handler.ForcedLocale = locale;
UpdateCollections();
}

internal bool CollectionExists(string name) {
return _handler.GetCollection(name) != null;
}

private void BuildIdChanged(object sender, ValueEventArgs<int> e) {
UpdateCollections();
}
}
}