## GGUtil
##
## Utilities for dealing with Great Generals.
##
## Notes
## - Must be initialized externally by calling init()
##
## Copyright (c) 2008 The BUG Mod.
##
## Author: EmperorFool
from CvPythonExtensions import *
gc = CyGlobalContext()
g_ePromo = -1
g_promoButton = ""
g_cGreatGeneral = ""
def init():
global g_ePromo
g_ePromo = gc.getInfoTypeForString("PROMOTION_LEADER")
global g_promoButton
g_promoButton = gc.getPromotionInfo(g_ePromo).getButton()
global g_cGreatGeneral
g_cGreatGeneral = FontUtil.getChar(FontSymbols.GREAT_GENERAL_CHAR)
def getPromotionId():
return g_ePromo
def getPromotion():
return gc.getPromotionInfo(g_ePromo)
def getGreatGeneralText(iNeededExp):
return BugUtil.getText("INTERFACE_NEXT_GREAT_GENERAL_XP",
(g_cGreatGeneral, iNeededExp))
def getText(key, values=(), default=None, replaceFontTags=True):
"""
Looks up a translated message in XML with a tuple of replacement parameters.
It is safe to pass in a single value instead of tuple/list.
If the key isn't found, the default is returned.
"""
if values is None:
values = ()
elif not isinstance(values, (tuple, list)):
values = (values,)
if isinstance(key, unicode):
warn("getText - received Unicode key %s", key)
key = str(key)
text = localText.getText(key, values)
if (text and text != key):
if replaceFontTags:
import FontUtil
text = FontUtil.replaceSymbols(text)
return text
else:
if default is not None:
return default
else:
debug("BugUtil.getText - XML key %s not found", key)
return "XML key %s not found" % key
def replaceSymbols(text, unknownReplacement=""):
def replace(match):
try:
return getChar(match.group(1))
except BugUtil.ConfigError:
return unknownReplacement
return SYMBOL_REGEXP.sub(replace, text)
def getChar(symbolOrKey):
try:
return symbolChars[getSymbol(symbolOrKey)]
except KeyError:
raise BugUtil.ConfigError("unknown font symbol or key '%s'" % str(symbolOrKey))
def getSymbol(symbolOrKey):
if isinstance(symbolOrKey, FontSymbols):
return symbolOrKey
try:
return keySymbols[symbolOrKey]
except KeyError:
try:
return keySymbols[symbolOrKey.upper() + "_CHAR"]
except KeyError:
raise BugUtil.ConfigError("unknown font symbol or key '%s'" % str(symbolOrKey))