Created parameter-extracting functions for placeholder texts!

This commit is contained in:
Yair Morgenstern 2020-07-20 00:04:21 +03:00
parent 00983dd00e
commit 95e1e8279d
3 changed files with 40 additions and 29 deletions

View file

@ -5,6 +5,8 @@ import com.unciv.UniqueAbility
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.models.ruleset.unit.UnitType
import com.unciv.models.translations.equalsPlaceholderText
import com.unciv.models.translations.getPlaceholderParameters
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.set
@ -118,12 +120,12 @@ object BattleDamage {
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
for (ability in attacker.unit.getUniques()) {
val regexResult = Regex(BONUS_AS_ATTACKER).matchEntire(ability) //to do: extend to defender, and penalyy
if (regexResult == null) continue
val bonus = regexResult.groups[1]!!.value.toFloat() / 100
if (modifiers.containsKey("Attacker Bonus"))
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
else modifiers["Attacker Bonus"] = bonus
if(ability.equalsPlaceholderText("Bonus as Attacker []%")) {
val bonus = ability.getPlaceholderParameters()[0].toFloat() / 100
if (modifiers.containsKey("Attacker Bonus"))
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
else modifiers["Attacker Bonus"] = bonus
}
}
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))

View file

@ -70,7 +70,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
}
private fun tryReadTranslationForLanguage(language: String){
private fun tryReadTranslationForLanguage(language: String) {
val translationStart = System.currentTimeMillis()
val translationFileName = "jsons/translations/$language.properties"
@ -79,13 +79,13 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
val languageTranslations: HashMap<String, String>
try { // On some devices we get a weird UnsupportedEncodingException
// which is super odd because everyone should support UTF-8
languageTranslations = TranslationFileReader.read(Gdx.files.internal(translationFileName))
}catch (ex:Exception){
languageTranslations = TranslationFileReader.read(Gdx.files.internal(translationFileName))
} catch (ex: Exception) {
return
}
// try to load the translations from the mods
for(modFolder in Gdx.files.local("mods").list()) {
for (modFolder in Gdx.files.local("mods").list()) {
val modTranslationFile = modFolder.child(translationFileName)
if (modTranslationFile.exists()) {
val translationsForMod = Translations()
@ -98,7 +98,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
createTranslations(language, languageTranslations)
val translationFilesTime = System.currentTimeMillis() - translationStart
println("Loading translation file for $language - "+translationFilesTime+"ms")
println("Loading translation file for $language - " + translationFilesTime + "ms")
}
private fun createTranslations(language: String,
@ -106,7 +106,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
targetTranslations: Translations = this) {
for (translation in languageTranslations) {
val hashKey = if (translation.key.contains('['))
translation.key.replace(squareBraceRegex,"[]")
translation.key.replace(squareBraceRegex, "[]")
else translation.key
if (!containsKey(hashKey))
targetTranslations[hashKey] = TranslationEntry(translation.key)
@ -114,7 +114,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
// 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 = targetTranslations[hashKey]
if (entry!=null) entry[language] = translation.value
if (entry != null) entry[language] = translation.value
}
}
@ -221,11 +221,8 @@ val curlyBraceRegex = Regex("""\{([^}]*)\}""")
* but with placeholder or sentence brackets removed.
*/
fun String.tr(): String {
val activeMods = if (UncivGame.Current.isGameInfoInitialized()) {
UncivGame.Current.gameInfo.gameParameters.mods
} else {
null
}
val activeMods = if (UncivGame.Current.isGameInfoInitialized())
UncivGame.Current.gameInfo.gameParameters.mods else null
// There might still be optimization potential here!
if (contains("[")) { // Placeholders!
@ -243,19 +240,22 @@ fun String.tr(): String {
*/
// Convert "work on [building] has completed in [city]" to "work on [] has completed in []"
val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex,"[]")
val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex, "[]")
// That is now the key into the translation HashMap!
val translationEntry = UncivGame.Current.translations.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
val translationEntry = UncivGame.Current.translations
.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
if (translationEntry==null ||
!translationEntry.containsKey(UncivGame.Current.settings.language)){
if (translationEntry == null ||
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
// Translation placeholder doesn't exist for this language, default to English
return this.replace(eitherSquareBraceRegex,"")
return this.replace(eitherSquareBraceRegex, "")
}
val termsInMessage = squareBraceRegex.findAll(this).map { it.groups[1]!!.value }.toList()
// Take the terms in the message, WITHOUT square brackets
val termsInMessage = this.getPlaceholderParameters()
// Take the term from the placeholder, INCLUDING the square brackets
val termsInTranslationPlaceholder = squareBraceRegex.findAll(translationEntry.entry).map { it.value }.toList()
if (termsInMessage.size!=termsInTranslationPlaceholder.size)
if (termsInMessage.size != termsInTranslationPlaceholder.size)
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
var languageSpecificPlaceholder = translationEntry[UncivGame.Current.settings.language]!!
@ -271,3 +271,10 @@ fun String.tr(): String {
return UncivGame.Current.translations.getText(this, UncivGame.Current.settings.language, activeMods)
}
fun String.equalsPlaceholderText(str:String): Boolean {
if (first() != str.first()) return false // for quick negative return 95% of the time
return this.replace(squareBraceRegex, "[]") == str
}
fun String.getPlaceholderParameters() = squareBraceRegex.findAll(this).map { it.groups[1]!!.value }.toList()

View file

@ -9,6 +9,8 @@ import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.logic.map.MapUnit
import com.unciv.models.UnitAction
import com.unciv.models.translations.equalsPlaceholderText
import com.unciv.models.translations.getPlaceholderParameters
import com.unciv.ui.utils.*
import com.unciv.ui.worldscreen.WorldScreen
import kotlin.concurrent.thread
@ -20,16 +22,16 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() {
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
when {
unitAction.startsWith("Upgrade to") -> {
unitAction.equalsPlaceholderText("Upgrade to [] ([] gold)") -> {
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
// What you find between the first [ and the first ] that comes after it, will be group no. 1
val unitToUpgradeTo = Regex("""Upgrade to \[([^\]]*)\]""").find(unitAction)!!.groups[1]!!.value
val unitToUpgradeTo = unitAction.getPlaceholderParameters()[0]
return UnitIconAndKey(ImageGetter.getUnitIcon(unitToUpgradeTo), 'u')
}
unitAction.startsWith("Create ") -> {
unitAction.equalsPlaceholderText("Create []") -> {
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
// What you find between the first [ and the first ] that comes after it, will be group no. 1
val improvementName = Regex("""Create \[([^]]*)\]""").find(unitAction)!!.groups[1]!!.value
val improvementName = unitAction.getPlaceholderParameters()[0]
return UnitIconAndKey(ImageGetter.getImprovementIcon(improvementName), 'i')
}
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')