We now load translation percentages on initialization so we won't need to mid-game, fixed "create" and the "resume" both trying to do the same things

This commit is contained in:
Yair Morgenstern 2019-12-19 17:48:17 +02:00
parent ebfd79636f
commit a41874e1a5
5 changed files with 31 additions and 21 deletions

View file

@ -21,8 +21,8 @@ android {
applicationId "com.unciv.app"
minSdkVersion 14
targetSdkVersion 29
versionCode 347
versionName "3.4.0-patch3"
versionCode 348
versionName "3.4.0-patch4"
archivesBaseName = "Unciv"
}

View file

@ -69,27 +69,32 @@ class UncivGame(val version: String) : Game() {
else{
translations.tryReadTranslationForCurrentLanguage()
}
translations.loadPercentageCompleteOfLanguages()
if (settings.userId == "") { // assign permanent user id
settings.userId = UUID.randomUUID().toString()
settings.save()
}
Gdx.app.postRunnable {
CameraStageBaseScreen.resetFonts()
if (GameSaver().getSave("Autosave").exists()) {
try {
loadGame("Autosave")
} catch (ex: Exception) { // silent fail if we can't read the autosave
startNewGame()
}
} else setScreen(LanguagePickerScreen())
autoLoadGame()
thread { startMusic() }
isInitialized = true
}
}
}
fun autoLoadGame(){
if (!GameSaver().getSave("Autosave").exists())
return setScreen(LanguagePickerScreen())
try {
loadGame("Autosave")
} catch (ex: Exception) { // silent fail if we can't read the autosave
startNewGame()
}
}
fun startMusic(){
val musicFile = Gdx.files.local(musicLocation)
@ -128,16 +133,18 @@ class UncivGame(val version: String) : Game() {
worldScreen.shouldUpdate=true // This can set the screen to the policy picker or tech picker screen, so the input processor must come before
}
// This is ALWAYS called after create() on Android - google "Android life cycle"
override fun resume() {
super.resume()
if(!isInitialized) return // The stuff from Create() is still happening, so the main screen will load eventually
ImageGetter.refreshAltas()
// This is to solve a rare problem that I still don't understand its cause -
// Sometimes, resume() is called and the gameInfo doesn't have any civilizations.
// My guess is that resume() was called but create() wasn't, or perhaps was aborted too early,
// and the original (and empty) initial GameInfo remained.
if(!::gameInfo.isInitialized || gameInfo.civilizations.isEmpty())
return create()
// if(!::gameInfo.isInitialized || gameInfo.civilizations.isEmpty())
// return autoLoadGame()
if(::worldScreen.isInitialized) worldScreen.dispose() // I hope this will solve some of the many OuOfMemory exceptions...
loadGame(gameInfo)

View file

@ -8,6 +8,8 @@ import kotlin.collections.HashMap
class Translations : LinkedHashMap<String, TranslationEntry>(){
val percentCompleteOfLanguages = HashMap<String,Int>()
fun get(text:String,language:String): String {
if(!hasTranslation(text,language)) return text
return get(text)!![language]!!
@ -54,7 +56,11 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
for (translation in languageTranslations) {
if (!containsKey(translation.key))
this[translation.key] = TranslationEntry(translation.key)
this[translation.key]!![language] = translation.value
// why not in one line, Because there were actual crashes.
// I'm pretty sure I solved this already, but hey double-checking doesn't cost anything.
val entry = this[translation.key]
if(entry!=null) entry[language] = translation.value
}
val translationFilesTime = System.currentTimeMillis() - translationStart
@ -119,7 +125,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
}
}
fun getPercentageCompleteOfLanguages(): HashMap<String, Int> {
fun loadPercentageCompleteOfLanguages() {
val translationStart = System.currentTimeMillis()
@ -127,21 +133,18 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
Gdx.files.internal("jsons/translationsByLanguage/template.properties")
.reader().forEachLine { if(it.contains(" = ")) allTranslations+=1 }
val languageToPercentCompleted = HashMap<String,Int>()
for(language in getLanguagesWithTranslationFile()){
val translationFileName = "jsons/translationsByLanguage/$language.properties"
var translationsOfThisLanguage=0
Gdx.files.internal(translationFileName).reader()
.forEachLine { if(it.contains(" = ") && !it.endsWith(" = "))
translationsOfThisLanguage+=1 }
languageToPercentCompleted[language] = translationsOfThisLanguage*100/allTranslations
percentCompleteOfLanguages[language] = translationsOfThisLanguage*100/allTranslations
}
val translationFilesTime = System.currentTimeMillis() - translationStart
println("Loading percentage complete of languages - "+translationFilesTime+"ms")
return languageToPercentCompleted
}
}

View file

@ -5,7 +5,6 @@ import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.UncivGame
import com.unciv.models.translations.Translations
import com.unciv.models.translations.tr
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.ImageGetter
@ -55,7 +54,8 @@ class LanguagePickerScreen: PickerScreen(){
"If you want to help translating the game into your language, \n"+
" instructions are in the Github readme! (Menu > Community > Github)",skin)).pad(10f).row()
val languageCompletionPercentage = Translations().getPercentageCompleteOfLanguages()
val languageCompletionPercentage = UncivGame.Current.translations
.percentCompleteOfLanguages
languageTables.addAll(languageCompletionPercentage
.map { LanguageTable(it.key,if(it.key=="English") 100 else it.value) }
.sortedByDescending { it.percentComplete} )

View file

@ -269,7 +269,7 @@ class WorldScreenOptionsTable(val worldScreen:WorldScreen) : PopupTable(worldScr
val languageSelectBox = SelectBox<Language>(skin)
val languageArray = Array<Language>()
val ruleSet = worldScreen.gameInfo.ruleSet
Translations().getPercentageCompleteOfLanguages()
UncivGame.Current.translations.percentCompleteOfLanguages
.map { Language(it.key, if(it.key=="English") 100 else it.value) }
.sortedByDescending { it.percentComplete }
.forEach { languageArray.add(it) }