mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-29 06:51:30 -04:00
Created parameter-extracting functions for placeholder texts!
This commit is contained in:
parent
00983dd00e
commit
95e1e8279d
@ -5,6 +5,8 @@ import com.unciv.UniqueAbility
|
|||||||
import com.unciv.logic.map.MapUnit
|
import com.unciv.logic.map.MapUnit
|
||||||
import com.unciv.logic.map.TileInfo
|
import com.unciv.logic.map.TileInfo
|
||||||
import com.unciv.models.ruleset.unit.UnitType
|
import com.unciv.models.ruleset.unit.UnitType
|
||||||
|
import com.unciv.models.translations.equalsPlaceholderText
|
||||||
|
import com.unciv.models.translations.getPlaceholderParameters
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.HashMap
|
import kotlin.collections.HashMap
|
||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
@ -118,12 +120,12 @@ object BattleDamage {
|
|||||||
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
|
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
|
||||||
|
|
||||||
for (ability in attacker.unit.getUniques()) {
|
for (ability in attacker.unit.getUniques()) {
|
||||||
val regexResult = Regex(BONUS_AS_ATTACKER).matchEntire(ability) //to do: extend to defender, and penalyy
|
if(ability.equalsPlaceholderText("Bonus as Attacker []%")) {
|
||||||
if (regexResult == null) continue
|
val bonus = ability.getPlaceholderParameters()[0].toFloat() / 100
|
||||||
val bonus = regexResult.groups[1]!!.value.toFloat() / 100
|
if (modifiers.containsKey("Attacker Bonus"))
|
||||||
if (modifiers.containsKey("Attacker Bonus"))
|
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
|
||||||
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
|
else modifiers["Attacker Bonus"] = bonus
|
||||||
else modifiers["Attacker Bonus"] = bonus
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))
|
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))
|
||||||
|
@ -70,7 +70,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun tryReadTranslationForLanguage(language: String){
|
private fun tryReadTranslationForLanguage(language: String) {
|
||||||
val translationStart = System.currentTimeMillis()
|
val translationStart = System.currentTimeMillis()
|
||||||
|
|
||||||
val translationFileName = "jsons/translations/$language.properties"
|
val translationFileName = "jsons/translations/$language.properties"
|
||||||
@ -79,13 +79,13 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
|
|||||||
val languageTranslations: HashMap<String, String>
|
val languageTranslations: HashMap<String, String>
|
||||||
try { // On some devices we get a weird UnsupportedEncodingException
|
try { // On some devices we get a weird UnsupportedEncodingException
|
||||||
// which is super odd because everyone should support UTF-8
|
// which is super odd because everyone should support UTF-8
|
||||||
languageTranslations = TranslationFileReader.read(Gdx.files.internal(translationFileName))
|
languageTranslations = TranslationFileReader.read(Gdx.files.internal(translationFileName))
|
||||||
}catch (ex:Exception){
|
} catch (ex: Exception) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to load the translations from the mods
|
// 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)
|
val modTranslationFile = modFolder.child(translationFileName)
|
||||||
if (modTranslationFile.exists()) {
|
if (modTranslationFile.exists()) {
|
||||||
val translationsForMod = Translations()
|
val translationsForMod = Translations()
|
||||||
@ -98,7 +98,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
|
|||||||
createTranslations(language, languageTranslations)
|
createTranslations(language, languageTranslations)
|
||||||
|
|
||||||
val translationFilesTime = System.currentTimeMillis() - translationStart
|
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,
|
private fun createTranslations(language: String,
|
||||||
@ -106,7 +106,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
|
|||||||
targetTranslations: Translations = this) {
|
targetTranslations: Translations = this) {
|
||||||
for (translation in languageTranslations) {
|
for (translation in languageTranslations) {
|
||||||
val hashKey = if (translation.key.contains('['))
|
val hashKey = if (translation.key.contains('['))
|
||||||
translation.key.replace(squareBraceRegex,"[]")
|
translation.key.replace(squareBraceRegex, "[]")
|
||||||
else translation.key
|
else translation.key
|
||||||
if (!containsKey(hashKey))
|
if (!containsKey(hashKey))
|
||||||
targetTranslations[hashKey] = TranslationEntry(translation.key)
|
targetTranslations[hashKey] = TranslationEntry(translation.key)
|
||||||
@ -114,7 +114,7 @@ class Translations : LinkedHashMap<String, TranslationEntry>(){
|
|||||||
// why not in one line, Because there were actual crashes.
|
// 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.
|
// I'm pretty sure I solved this already, but hey double-checking doesn't cost anything.
|
||||||
val entry = targetTranslations[hashKey]
|
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.
|
* but with placeholder or sentence brackets removed.
|
||||||
*/
|
*/
|
||||||
fun String.tr(): String {
|
fun String.tr(): String {
|
||||||
val activeMods = if (UncivGame.Current.isGameInfoInitialized()) {
|
val activeMods = if (UncivGame.Current.isGameInfoInitialized())
|
||||||
UncivGame.Current.gameInfo.gameParameters.mods
|
UncivGame.Current.gameInfo.gameParameters.mods else null
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
// There might still be optimization potential here!
|
// There might still be optimization potential here!
|
||||||
if (contains("[")) { // Placeholders!
|
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 []"
|
// 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!
|
// 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 ||
|
if (translationEntry == null ||
|
||||||
!translationEntry.containsKey(UncivGame.Current.settings.language)){
|
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
|
||||||
// Translation placeholder doesn't exist for this language, default to English
|
// 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()
|
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!")
|
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
||||||
|
|
||||||
var languageSpecificPlaceholder = translationEntry[UncivGame.Current.settings.language]!!
|
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)
|
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()
|
@ -9,6 +9,8 @@ import com.unciv.Constants
|
|||||||
import com.unciv.UncivGame
|
import com.unciv.UncivGame
|
||||||
import com.unciv.logic.map.MapUnit
|
import com.unciv.logic.map.MapUnit
|
||||||
import com.unciv.models.UnitAction
|
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.utils.*
|
||||||
import com.unciv.ui.worldscreen.WorldScreen
|
import com.unciv.ui.worldscreen.WorldScreen
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
@ -20,16 +22,16 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() {
|
|||||||
|
|
||||||
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
|
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
|
||||||
when {
|
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 ].
|
// 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
|
// 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')
|
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 ].
|
// 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
|
// 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')
|
return UnitIconAndKey(ImageGetter.getImprovementIcon(improvementName), 'i')
|
||||||
}
|
}
|
||||||
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')
|
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user