Compare commits

...
5 changed files with 115 additions and 2 deletions

No files matched your search

@@ -0,0 +1,67 @@
package com.droiddeck.launcher.stores
import android.content.Context
import java.io.File
import org.json.JSONArray
import org.json.JSONObject
internal object StoreLibraryCache {
private fun file(context: Context, store: Store) = File(context.filesDir, "store-library/${store.id}.json")
fun load(context: Context, store: Store): List<CatalogItem>? {
val f = file(context, store)
if (!f.isFile) return null
return runCatching { decode(store, f.readText()) }.getOrNull()
}
fun save(context: Context, store: Store, items: List<CatalogItem>) {
val f = file(context, store)
runCatching {
f.parentFile?.mkdirs()
val tmp = File(f.parentFile, f.name + ".tmp")
tmp.writeText(encode(items))
if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) }
}
}
fun clear(context: Context, store: Store) {
file(context, store).delete()
}
internal fun encode(items: List<CatalogItem>): String {
val out = JSONArray()
items.forEach { i ->
out.put(JSONObject()
.put("id", i.id).put("title", i.title)
.put("image", i.imageUrl ?: "").put("tall", i.tallImageUrl ?: "").put("logo", i.logoUrl ?: "")
.put("tags", i.tags).put("free", i.isFree).put("hasPrice", i.hasPrice)
.put("finalPrice", i.finalPrice).put("originalPrice", i.originalPrice).put("discount", i.discountPercent)
.put("url", i.storeUrl).put("developer", i.developer).put("publisher", i.publisher)
.put("releaseDate", i.releaseDate).put("description", i.description)
.put("owned", i.owned).put("size", i.sizeBytes).put("extra", JSONObject(i.extra))
.put("mature", i.mature).put("subscription", i.subscription).put("includedWith", i.includedWith))
}
return out.toString()
}
internal fun decode(store: Store, text: String): List<CatalogItem> {
val raw = JSONArray(text)
return (0 until raw.length()).mapNotNull { n ->
val o = raw.optJSONObject(n) ?: return@mapNotNull null
val extra = o.optJSONObject("extra")
val map = HashMap<String, String>()
extra?.keys()?.forEach { k -> map[k] = extra.optString(k) }
CatalogItem(
store = store, id = o.optString("id"), title = o.optString("title"),
imageUrl = o.optString("image").ifBlank { null }, tallImageUrl = o.optString("tall").ifBlank { null },
logoUrl = o.optString("logo").ifBlank { null },
tags = o.optString("tags"), isFree = o.optBoolean("free"), hasPrice = o.optBoolean("hasPrice"),
finalPrice = o.optString("finalPrice"), originalPrice = o.optString("originalPrice"), discountPercent = o.optInt("discount"),
storeUrl = o.optString("url"), developer = o.optString("developer"), publisher = o.optString("publisher"),
releaseDate = o.optString("releaseDate"), description = o.optString("description"),
owned = o.optBoolean("owned"), sizeBytes = o.optLong("size"), extra = map,
mature = o.optBoolean("mature"), subscription = o.optBoolean("subscription"), includedWith = o.optString("includedWith"),
)
}
}
}
@@ -202,6 +202,7 @@ object StoresState {
backend?.signOut(app)
if (store == Store.GOG) com.droiddeck.launcher.stores.gog.GogComet.replaceAccount { StoreAccounts.clear(app, store) }
else if (store != Store.XBOX || backend == null) StoreAccounts.clear(app, store)
StoreLibraryCache.clear(app, store)
if (store == Store.GAMEJOLT) {
com.droiddeck.launcher.stores.gamejolt.GameJoltCollections.clear()
com.droiddeck.launcher.stores.gamejolt.GameJoltUpdates.clear(app)
@@ -14,6 +14,7 @@ import com.droiddeck.launcher.stores.StoreFront
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.StoreLoginActivity
import com.droiddeck.launcher.stores.StoreShelves
import com.droiddeck.launcher.stores.StoreLibraryCache
import com.droiddeck.launcher.stores.StoresState
import com.droiddeck.launcher.stores.WebCookies
import com.droiddeck.launcher.stores.download.DownloadEntry
@@ -163,7 +164,13 @@ object RockstarLibrary {
post(gen) { StoresState.status.remove(Store.ROCKSTAR); StoresState.problems[Store.ROCKSTAR] = app.getString(R.string.stores_sync_sign_in) }
return
}
if (StoresState.library[Store.ROCKSTAR].isNullOrEmpty()) post(gen) { StoresState.status[Store.ROCKSTAR] = app.getString(R.string.stores_sync_game_list) }
val cached = StoreLibraryCache.load(app, Store.ROCKSTAR)
post(gen) {
if (StoresState.library[Store.ROCKSTAR] == null) {
if (cached != null) StoresState.library[Store.ROCKSTAR] = cached
else StoresState.status[Store.ROCKSTAR] = app.getString(R.string.stores_sync_game_list)
}
}
val account = RockstarApps.fetch(t.ticket) { StoresState.logLine(it) }
val games = games(account)
owned(account).filter { o -> games.none { it.id == o.id } }.takeIf { it.isNotEmpty() }?.let { elsewhere ->
@@ -175,6 +182,7 @@ object RockstarLibrary {
StoresState.library[Store.ROCKSTAR] = games
StoresState.shelves[Store.ROCKSTAR]?.let { StoresState.shelves[Store.ROCKSTAR] = StoreFront.markOwned(it, games, RockstarCatalog::sameGame) }
}
if (generation.get() == gen) StoreLibraryCache.save(app, Store.ROCKSTAR, games)
StoresState.logLine("Rockstar: ${games.size} owned games")
} catch (e: Exception) {
StoresState.logLine("Rockstar: library sync failed: ${e.javaClass.simpleName} ${e.message.orEmpty().take(300)}")
@@ -15,6 +15,7 @@ import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.download.DownloadEntry
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.StoreLoginActivity
import com.droiddeck.launcher.stores.StoreLibraryCache
import com.droiddeck.launcher.stores.StoresState
import com.droiddeck.launcher.stores.WebCookies
import org.json.JSONObject
@@ -206,7 +207,13 @@ object UbisoftLibrary {
post(gen) { StoresState.status.remove(Store.UBISOFT); StoresState.problems[Store.UBISOFT] = app.getString(R.string.stores_sync_sign_in) }
return
}
if (StoresState.library[Store.UBISOFT].isNullOrEmpty()) post(gen) { StoresState.status[Store.UBISOFT] = app.getString(R.string.stores_sync_game_list) }
val cached = StoreLibraryCache.load(app, Store.UBISOFT)
post(gen) {
if (StoresState.library[Store.UBISOFT] == null) {
if (cached != null) StoresState.library[Store.UBISOFT] = cached
else StoresState.status[Store.UBISOFT] = app.getString(R.string.stores_sync_game_list)
}
}
val all = ArrayList<CatalogItem>()
var offset = 0
while (true) {
@@ -241,6 +248,7 @@ object UbisoftLibrary {
StoresState.library[Store.UBISOFT] = games
StoresState.shelves[Store.UBISOFT]?.let { StoresState.shelves[Store.UBISOFT] = StoreFront.markOwned(it, games, UbisoftStore::sameGame) }
}
if (generation.get() == gen) StoreLibraryCache.save(app, Store.UBISOFT, games)
StoresState.logLine("Ubisoft: ${games.size} owned games")
} catch (e: Exception) {
StoresState.logLine("Ubisoft: library sync failed: ${e.javaClass.simpleName} ${e.message.orEmpty().take(200)}")
@@ -0,0 +1,29 @@
package com.droiddeck.launcher.stores
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], application = android.app.Application::class)
class StoreLibraryCacheTest {
@Test
fun listsRoundTripWithTheirStoreAndExtras() {
val games = listOf(
CatalogItem(
Store.UBISOFT, "4553", "Far Cry 3 Blood Dragon", "https://img/wide.jpg", tallImageUrl = "https://img/tall.jpg",
logoUrl = "https://img/logo.png", owned = true, sizeBytes = 2_000_000L, extra = mapOf("productId" to "205", "spaceId" to "abc"),
subscription = true, includedWith = "Ubisoft+",
),
CatalogItem(Store.UBISOFT, "1", "No art", null),
)
assertEquals(games, StoreLibraryCache.decode(Store.UBISOFT, StoreLibraryCache.encode(games)))
}
@Test
fun anEmptyLibraryStaysEmptyRatherThanUnknown() {
assertEquals(emptyList<CatalogItem>(), StoreLibraryCache.decode(Store.ROCKSTAR, StoreLibraryCache.encode(emptyList())))
}
}