Skip to content

Commit

Permalink
Go to the setup activity if the Theme Hospital files don't exist on l…
Browse files Browse the repository at this point in the history
…aunch
  • Loading branch information
alanwoolley committed Jul 12, 2023
1 parent 65d0cff commit c1aec84
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 51 deletions.
47 changes: 8 additions & 39 deletions AndroidManifest.xml
@@ -1,46 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<!--
Permissions
INTERNET - required to download the demo and music files
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE - required to R/W game files to storage
WAKE_LOCK - required to keep the device awake during play
ACCESS_NETWORK_STATE - required to see if the device has an internet connection
Beta:
READ_LOGS - Lets us read the system logs for additional debugging
GET_TASKS - Gives us the number of app processes running, for debugging.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />


<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:requiresSmallestWidthDp="300"
android:smallScreens="false"
android:xlargeScreens="true" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature
android:name="android.hardware.screen.landscape"
android:required="true" />
<uses-feature
android:name="android.hardware.touchscreen.multitouch"
android:required="true" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />

<application
android:name="uk.co.armedpineapple.cth.CTHApplication"
android:icon="@drawable/ic_launcher"
Expand All @@ -55,20 +22,22 @@
android:name="uk.co.armedpineapple.cth.GameActivity"
android:configChanges="layoutDirection|locale|orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
android:immersive="true"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:preferMinimalPostProcessing="true"
android:theme="@style/Theme.AppCompat.NoActionBar" />


<activity
android:name="uk.co.armedpineapple.cth.setup.SetupActivity"
android:exported="true">
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="uk.co.armedpineapple.cth.setup.SetupActivity"
android:excludeFromRecents="true"
android:exported="false" />

<activity
android:name="uk.co.armedpineapple.cth.settings.SettingsActivity"
android:excludeFromRecents="true"
Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation "com.lazygeniouz:dfc:1.0.7"
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.core:core-ktx:1.10.1'
Expand Down Expand Up @@ -133,6 +134,9 @@ android {
dataBinding true
buildConfig true
}
aaptOptions {
noCompress "zip"
}
}

// Create ZIP of CorsixTH game assets
Expand Down
48 changes: 45 additions & 3 deletions src/Java/uk/co/armedpineapple/cth/GameActivity.kt
Expand Up @@ -5,13 +5,24 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.annotation.Keep
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewModelScope
import com.google.common.io.ByteStreams
import com.google.common.io.Closeables
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.jetbrains.anko.AnkoLogger
import org.libsdl.app.SDLActivity
import uk.co.armedpineapple.cth.files.FilesService
import uk.co.armedpineapple.cth.settings.SettingsActivity
import uk.co.armedpineapple.cth.setup.SetupActivity
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
Expand All @@ -26,12 +37,43 @@ class GameActivity : SDLActivity(), AnkoLogger {
get() = (application as CTHApplication).configuration

override fun onCreate(savedInstanceState: Bundle?) {
val filesService = FilesService(this)

// Check whether the setup installation has run and the original TH files are available.
// If not, redirect over to the setup activity.
if (!filesService.hasOriginalFiles(configuration)) {
finishAndRemoveTask()
val intent = Intent(this, SetupActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
startActivity(intent)

super.onCreate(savedInstanceState)
return;
}

// Install the latest CTH game files in the background.
var installJob: Job? = null
if (!filesService.hasGameFiles(configuration)) {
Toast.makeText(this, "Upgrading", Toast.LENGTH_SHORT).show()
installJob = CoroutineScope(Dispatchers.IO).launch {
filesService.installGameFiles(configuration)
}
}

// Upgrading the game files will nuke the installation directory, so make sure that
// the latest configuration exists in there.
configuration.persist()

super.onCreate(savedInstanceState)
startLogger()
singleton = this

startLogger();

val filesService = FilesService(this)
// Make sure the game file installation installation has completed before moving on.
if (installJob != null) {
runBlocking {
installJob.join()
}
}
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/Java/uk/co/armedpineapple/cth/GameConfiguration.kt
Expand Up @@ -31,10 +31,9 @@ class GameConfiguration(private val ctx: Context, private val preferences: Share

init {
refresh()
persistGameConfigFile()
}

private fun persistGameConfigFile() {
fun persist() {
val tokenMap = hashMapOf<String, String>()

tokenMap.putAll(getStringPrefs(arrayOf(R.string.prefs_language)))
Expand Down
17 changes: 10 additions & 7 deletions src/Java/uk/co/armedpineapple/cth/files/FilesService.kt
Expand Up @@ -139,7 +139,9 @@ class FilesService(val ctx: Context) : AnkoLogger {
if (file.isDirectory()) {
val newDestination = File(destinationDirectory, file.name)
val childProgress =
if (progress == null) null else Channel<EstimatedFileOperationProgress>(Channel.CONFLATED) { p ->
if (progress == null) null else Channel<EstimatedFileOperationProgress>(
Channel.CONFLATED
) { p ->
// Report the progress back up the chain.
progress.trySend(EstimatedFileOperationProgress((currentFile + p.progress) / totalContents.toFloat()))
}
Expand Down Expand Up @@ -220,14 +222,15 @@ class FilesService(val ctx: Context) : AnkoLogger {
private fun copyAsset(
asset: String, ctx: Context, target: File
) {
ctx.assets.openFd(asset).use { assetIn ->
ctx.assets.open(asset).use { assetInputStream ->
target.deleteOnExit()
target.outputStream().use { assetOutStream ->
allocateStorage(assetIn.length, assetOutStream.fd, storageManager)

assetIn.createInputStream().use { assetInputStream ->
assetInputStream.copyTo(target.outputStream())
}
allocateStorage(
bytes = assetInputStream.available().toLong(),
fd = assetOutStream.fd,
storageMgr = storageManager
)
assetInputStream.copyTo(assetOutStream)
}
}
}
Expand Down

0 comments on commit c1aec84

Please sign in to comment.