Skip to content

Commit

Permalink
Add support for msstore auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 3, 2023
1 parent a27226c commit 2cf1761
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/desktop/src/windowsMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import dev.schlaubi.tonbrett.app.desktop.uwp_helper.StringResult
import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.*
import dev.schlaubi.tonbrett.common.Route
import io.ktor.http.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mu.KotlinLogging
import java.lang.foreign.Arena
import java.lang.foreign.MemorySegment
Expand All @@ -17,6 +21,7 @@ actual val loginType = Route.Auth.Type.PROTOCOL

actual fun prepareAuthentication(onAuth: () -> Unit): Unit = exitProcess(0)

@OptIn(DelicateCoroutinesApi::class)
actual fun start(args: Array<String>) {
val argsString = args.joinToString(" ")
if (argsString.startsWith("tonbrett://login")) {
Expand All @@ -28,6 +33,7 @@ actual fun start(args: Array<String>) {
e.printStackTrace()
Thread.sleep(50000)
}
GlobalScope.launch(Dispatchers.IO) { request_msstore_auto_update() }
startApplication()
} else {
val needsAuth = runCatching { getToken() }.isFailure
Expand Down
1 change: 1 addition & 0 deletions app/desktop/uwp_helper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app/desktop/uwp_helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ crate-type = ["cdylib"]

[dependencies.windows]
version = "0.51.1"
features = ["System", "Foundation", "Win32_Foundation", "Security_Credentials"]
features = ["System", "Foundation", "Win32_Foundation", "Security_Credentials", "Services_Store", "Foundation_Collections"]

[dependencies.tokio]
version = "1.32.0"
features = ["macros", "rt-multi-thread"]

[build-dependencies.cbindgen]
version = "0.26.0"
default-features = false
default-features = false
[dependencies]
log = "0.4.20"
1 change: 1 addition & 0 deletions app/desktop/uwp_helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tasks {
"--include-function", "get_token",
"--include-function", "store_token",
"--include-function", "copy_string_from_get_string_result_into_buffer",
"--include-function", "request_msstore_auto_update",
"--include-struct", "StringResult",
"--include-typedef", "uint16_t",
header,
Expand Down
28 changes: 28 additions & 0 deletions app/desktop/uwp_helper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::{c_char, CStr};

use windows::Services::Store::StoreContext;
use windows::{
core::h,
core::Result,
Expand All @@ -13,6 +14,33 @@ use windows::{
const RESOURCE: &HSTRING = h!("dev.schlaubi.tonbrett/api_token");
const USERNAME: &HSTRING = h!("_");

#[tokio::main]
#[no_mangle]
pub async extern "C" fn request_msstore_auto_update() -> bool {
let result = _request_msstore_auto_update().await;
return match result {
Err(err) => {
log::error!("Could not request msstore update: {}", err);
true
}
_ => false,
};
}

async fn _request_msstore_auto_update() -> Result<()> {
let context = StoreContext::GetDefault()?;
let updates = context.GetAppAndOptionalStorePackageUpdatesAsync()?.await?;

if updates.Size()? > 0 {
context
.RequestDownloadAndInstallStorePackageUpdatesAsync(&updates)?
.await
.map(|_| ())
} else {
Ok(())
}
}

#[tokio::main]
#[no_mangle]
pub async unsafe extern "C" fn launch_uri(uri: *const c_char) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "dev.schlaubi.tonbrett"
version = "1.16.6"
version = "1.16.7"

repositories {
mavenCentral()
Expand Down

0 comments on commit 2cf1761

Please sign in to comment.