def initNoCustomAssetsSetting(): """ Checks if the NoCustomAssets setting is enabled when running as a mod. """ global _noCustomAssetsSettingInitDone if _noCustomAssetsSettingInitDone: return if isMod(): global _noCustomAssets # EF: don't check game option because BUG is initialized only once at startup # if CyGame().isOption(GameOptionTypes.GAMEOPTION_LOCK_MODS): # _noCustomAssets = True # BugUtil.debug("BugPath - Lock Modified Assets is set") # else: if getModDir() and getModFolder(): BugUtil.debug("BugPath - checking for NoCustomAssets") try: from configobj import ConfigObj config = ConfigObj(join(getModDir(), getModFolder() + ".ini"), encoding="utf_8") _noCustomAssets = config["CONFIG"].as_bool("NoCustomAssets") except: BugUtil.trace("BugPath - failed to parse mod INI file for NoCustomAssets") BugUtil.info("BugPath - NoCustomAssets is %s", _noCustomAssets) _noCustomAssetsSettingInitDone = True
def onCombatResult(argsList): CyUnitW, CyUnitL = argsList # Captives if CyUnitW.isMadeAttack() and not CyUnitL.isAnimal() and CyUnitL.getDomainType() == giDomainLand and CyUnitW.getDomainType() == giDomainLand: # Check that the losing unit is not an animal and the unit does not have a capture type defined in the XML if CyUnitL.getCaptureUnitType((gc.getPlayer(CyUnitL.getOwner())).getCivilizationType()) == -1: iCaptureProbability = CyUnitW.captureProbabilityTotal() iCaptureResistance = CyUnitL.captureResistanceTotal() iChance = iCaptureProbability - iCaptureResistance if iChance > 100: iChance = 100 BugUtil.info("CaptureSlaves: Chance to capture a captive is %d (%d - %d)", iChance, iCaptureProbability, iCaptureResistance) iRandom = CyGame().getSorenRandNum(100, "Slave") # 0-99 if iChance > iRandom: if CyUnitL.isHasUnitCombat(gc.getInfoTypeForString('UNITCOMBAT_SPECIES_NEANDERTHAL')): iUnit = gc.getInfoTypeForString('UNIT_CAPTIVE_NEANDERTHAL') sMessage = CyTranslator().getText("TXT_KEY_MESSAGE_NEANDERTHAL_CAPTIVE",()) else: iUnit = gc.getInfoTypeForString('UNIT_CAPTIVE_MILITARY') sMessage = CyTranslator().getText("TXT_KEY_MESSAGE_MILITARY_CAPTIVE",()) iPlayerW = CyUnitW.getOwner() CyPlayerW = gc.getPlayer(iPlayerW) X = CyUnitW.getX() Y = CyUnitW.getY() CyUnit = CyPlayerW.initUnit(iUnit, X, Y, UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION) if CyUnitW.isHiddenNationality(): CyUnit.doHNCapture() if CyPlayerW.isHuman(): CyInterface().addMessage(iPlayerW, False, 15, sMessage, '', 0, 'Art/Interface/Buttons/Civics/Serfdom.dds', ColorTypes(44), X, Y, True, True)
def init(): """ Checks for the presence of the BUFFY DLL and sets the global flags. """ if isEnabled(): if BugPath.isMac(): BugUtil.debug("BUFFY is not active on Mac (no DLL)") else: try: if gc.isBuffy(): global IS_DLL_PRESENT, IS_DLL_IN_CORRECT_PATH, IS_ACTIVE IS_DLL_PRESENT = True IS_ACTIVE = True BugUtil.info("BUFFY is active (API version %d)", gc.getBuffyApiVersion()) try: dllPath = gc.getGame().getDLLPath() exePath = gc.getGame().getExePath() dllPathThenUp3 = os.path.dirname( os.path.dirname(os.path.dirname(dllPath))) if dllPathThenUp3 == exePath: IS_DLL_IN_CORRECT_PATH = True except: pass # DLL path is borked except: BugUtil.info("BUFFY is not active (no DLL)")
def isReplayWinner(self, replay): szWinText = localText.getText("TXT_KEY_GAME_WON", ("(.*)", "(.*)")) reWinText = re.compile(szWinText) leaderGroup = 1 typeGroup = 2 leader = replay.getLeaderName() msgNum = replay.getNumReplayMessages() - 1 BugUtil.debug("scanning replay for %s with %d msgs", leader, msgNum + 1) count = 0 while msgNum >= 0: msg = replay.getReplayMessageText(msgNum) if count > 100: BugUtil.debug("no victory message in first 100; skipping") break matches = reWinText.match(msg) if matches: type = matches.group(typeGroup) if leader in matches.group(leaderGroup).split('/'): BugUtil.info("replay: %s win for %s", type, leader) return True, type BugUtil.info("replay: %s loss for %s", type, leader) return False, type msgNum -= 1 return False, localText.getText("TXT_KEY_NONE", ())
def initNoCustomAssetsSetting(): """ Checks if the NoCustomAssets setting is enabled when running as a mod. """ global _noCustomAssetsSettingInitDone if _noCustomAssetsSettingInitDone: return if isMod(): global _noCustomAssets # EF: don't check game option because BUG is initialized only once at startup # if CyGame().isOption(GameOptionTypes.GAMEOPTION_LOCK_MODS): # _noCustomAssets = True # BugUtil.debug("BugPath - Lock Modified Assets is set") # else: if getModDir() and getModFolder(): BugUtil.debug("BugPath - checking for NoCustomAssets") try: from configobj import ConfigObj config = ConfigObj(join(getModDir(), getModFolder() + ".ini"), encoding='utf_8') _noCustomAssets = config["CONFIG"].as_bool("NoCustomAssets") except: BugUtil.trace( "BugPath - failed to parse mod INI file for NoCustomAssets" ) BugUtil.info("BugPath - NoCustomAssets is %s", _noCustomAssets) _noCustomAssetsSettingInitDone = True
def handle(self, element, how, module, function, toModule, asName, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): BugUtil.extendFunction(module, function, toModule, asName, how) else: BugUtil.info( "BugConfig - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, function, self.resolveDll(element, dll))
def _getMod(self, id): if id in self._mods: return self._mods[id] elif not self._inited: BugUtil.info("BugCore - creating uninitialized mod %s", id) return self._newMod(id) else: BugUtil.error("BugCore - invalid mod %s", id)
def _getMod(self, id): if id in self._mods: return self._mods[id] elif not self._inited: BugUtil.info("BugCore - creating uninitialized mod %s", id) return self._newMod(id) else: BugUtil.error("BugCore - invalid mod %s", id)
def _getMod(self, mod_id): 'get mod by id' if mod_id in self._mods: return self._mods[mod_id] if not self._inited: BugUtil.info("BugCore - creating uninitialized mod %s", mod_id) return self._newMod(mod_id) raise BugUtil.error("BugCore - invalid mod %s", mod_id)
def handle(self, element, module, clazz, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): BugUtil.callFunction(module, clazz, CvEventInterface.getEventManager(), *element.args, **element.kwargs) else: BugUtil.info( "BugConfig - ignoring <%s> from %s.%s, requires dll version %s", element.tag, module, clazz, self.resolveDll(element, dll))
def registerSymbol(key, symbol, ordinal): BugUtil.info("FontUtil - registering symbol '%s' for %d", key, ordinal) if key in keySymbols: raise BugUtil.ConfigError("duplicate font symbol key '%s'" % key) if symbol in symbolPrimaryKeys: raise BugUtil.ConfigError("duplicate font symbol for key '%s'" % key) keySymbols[key] = symbol symbolPrimaryKeys[symbol] = key symbolOrdinals[symbol] = ordinal symbolChars[symbol] = u"%c" % ordinal
def registerSymbol(key, symbol, ordinal): BugUtil.info("FontUtil - registering symbol '%s' for %d", key, ordinal) if key in keySymbols: raise BugUtil.ConfigError("duplicate font symbol key '%s'" % key) if symbol in symbolPrimaryKeys: raise BugUtil.ConfigError("duplicate font symbol for key '%s'" % key) keySymbols[key] = symbol symbolPrimaryKeys[symbol] = key symbolOrdinals[symbol] = ordinal symbolChars[symbol] = u"%c" % ordinal
def handle(self, element, module, function, immediate, dll): immediate = self.isTrue(immediate) dll = BugDll.decode(dll) if self.isDllOkay(element, dll): func = BugUtil.getFunction(module, function, True, *element.args, **element.kwargs) if immediate: func() else: BugInit.addInit(module, func) else: BugUtil.info("BugConfig - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, function, self.resolveDll(element, dll))
def handle(self, element, type, module, function, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): CvEventInterface.getEventManager().addEventHandler( type, BugUtil.getFunction(module, function, True, *element.args, **element.kwargs)) else: BugUtil.info( "BugConfig - ignoring <%s> %s, requires dll version %s", element.tag, type, self.resolveDll(element, dll))
def handle(self, element, module, function, immediate, dll): immediate = self.isTrue(immediate) dll = BugDll.decode(dll) if self.isDllOkay(element, dll): func = BugUtil.getFunction(module, function, True, *element.args, **element.kwargs) if immediate: func() else: BugInit.addInit(module, func) else: BugUtil.info("BugConfig - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, function, self.resolveDll(element, dll))
def handle(self, element, keys, module, function, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): CvEventInterface.getEventManager().addShortcutHandler( keys, BugUtil.getFunction(module, function, *element.args, **element.kwargs)) else: BugUtil.info( "InputUtil - ignoring <%s> %s, requires dll version %s", element.tag, keys, self.resolveDll(element, dll))
def handle(self, element, how, module, function, toModule, asName, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): BugUtil.extendFunction(module, function, toModule, asName, how) else: BugUtil.info( "BugConfig - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, function, self.resolveDll(element, dll), )
def handle(self, element, module, clazz, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): BugUtil.callFunction(module, clazz, CvEventInterface.getEventManager(), *element.args, **element.kwargs) else: BugUtil.info( "BugConfig - ignoring <%s> from %s.%s, requires dll version %s", element.tag, module, clazz, self.resolveDll(element, dll), )
def handle(self, element, type, module, function, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): CvEventInterface.getEventManager().addEventHandler( type, BugUtil.getFunction(module, function, True, *element.args, **element.kwargs) ) else: BugUtil.info( "BugConfig - ignoring <%s> %s, requires dll version %s", element.tag, type, self.resolveDll(element, dll), )
def handle(self, element, id, name, fromKey, offset, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): if not fromKey: if not self.lastSymbol: raise BugUtil.ConfigError("<%s> %s requires an offset symbol" % (element.tag, id)) fromKey = self.lastSymbol if offset is None: offset = 1 else: offset = int(offset) self.lastSymbol = addOffsetSymbol(id, fromKey, offset, name) else: BugUtil.info("FontUtil - ignoring <%s> %s, requires dll version %s", element.tag, id, self.resolveDll(element, dll))
def handle(self, element, module, clazz, handlers, listeners, override, log, dll): override = self.isTrue(override) dll = BugDll.decode(dll) if self.isDllOkay(element, dll): if clazz: utils = BugUtil.callFunction(module, clazz) element.setState("gameutils", utils) if handlers or listeners: if handlers: for handler in handlers.replace(",", " ").split(): addHandler(BugUtil.bindFunction(utils, handler), override, log) if listeners: for listener in listeners.replace(",", " ").split(): func = None if not listener.endswith(LISTENER_SUFFIX): try: func = BugUtil.bindFunction( utils, listener + LISTENER_SUFFIX) except BugUtil.ConfigError: pass if not func: try: func = BugUtil.bindFunction( utils, listener) except BugUtil.ConfigError: raise BugUtil.ConfigError( "Game utils %s.%s must define function %s or %s", module, clazz, listener, listener + "Listener") addListener(func, log) else: addClassUtils(utils, override, log) else: if handlers or listeners: if handlers: for handler in handlers.replace(",", " ").split(): addHandler(BugUtil.lookupFunction(module, handler), override, log) if listeners: for listener in listeners.replace(",", " ").split(): addListener( BugUtil.lookupFunction(module, listener), log) else: addModuleUtils(BugUtil.lookupModule(module), override, log) else: BugUtil.info( "BugGameUtils - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, clazz, self.resolveDll(element, dll))
def onLoadGame(self, argsList): """ Load saved reminders. """ self.clearReminders() queues = SdToolKit.sdGetGlobal(SD_MOD_ID, SD_QUEUES_ID) if queues: self.setReminders(queues) else: # check for old save format (single queue) queue = SdToolKit.sdGetGlobal(SD_MOD_ID, SD_QUEUE_ID) if queue: BugUtil.info("Reminder - Converting single-queue format") self.setReminders(Reminders(queue)) SdToolKit.sdDelGlobal(SD_MOD_ID, SD_QUEUE_ID)
def onLoadGame(self, argsList): """ Load saved reminders. """ self.clearReminders() queues = SdToolKit.sdGetGlobal(SD_MOD_ID, SD_QUEUES_ID) if queues: self.setReminders(queues) else: # check for old save format (single queue) queue = SdToolKit.sdGetGlobal(SD_MOD_ID, SD_QUEUE_ID) if queue: BugUtil.info("Reminder - Converting single-queue format") self.setReminders(Reminders(queue)) SdToolKit.sdDelGlobal(SD_MOD_ID, SD_QUEUE_ID)
def init(): """ Checks for the presence of the BUG DLL and grabs its Python API version if found. """ try: if gc.isBull(): global IS_PRESENT, VERSION IS_PRESENT = True VERSION = gc.getBullApiVersion() BugUtil.info("BugDll - %s %s, API version %d", gc.getBullName(), gc.getBullVersion(), VERSION) if hasattr(CyGlobalContext, "setIsBug"): import BugInit BugInit.addInit("setIsBug", setIsBug) else: BugUtil.debug("BugDll - setIsBug() not found") except: BugUtil.debug("BugDll - BULL not present")
def createWidget(name): """ Creates and returns the next unique WidgetTypes constant to be used with custom UI widgets. If <name> already exists, a warning is logged and the widget is returned. Otherwise the new widget is assigned to WidgetTypes.<name> and returned. """ if hasattr(WidgetTypes, name): BugUtil.warn("WidgetTypes.%s already exists", name) return getattr(WidgetTypes, name) else: global g_nextWidget BugUtil.info("WidgetUtil - WidgetTypes.%s = %d", name, g_nextWidget) widget = WidgetTypes(g_nextWidget) setattr(WidgetTypes, name, widget) g_nextWidget += 1 return widget
def createWidget(name): """ Creates and returns the next unique WidgetTypes constant to be used with custom UI widgets. If <name> already exists, a warning is logged and the widget is returned. Otherwise the new widget is assigned to WidgetTypes.<name> and returned. """ if hasattr(WidgetTypes, name): BugUtil.warn("WidgetTypes.%s already exists", name) return getattr(WidgetTypes, name) else: global g_nextWidget BugUtil.info("WidgetUtil - WidgetTypes.%s = %d", name, g_nextWidget) widget = WidgetTypes(g_nextWidget) setattr(WidgetTypes, name, widget) g_nextWidget += 1 return widget
def init(): """ Checks for the presence of the BUG DLL and grabs its Python API version if found. """ try: if gc.isBull(): global IS_PRESENT, VERSION IS_PRESENT = True VERSION = gc.getBullApiVersion() BugUtil.info("BugDll - %s %s, API version %d", gc.getBullName(), gc.getBullVersion(), VERSION) if hasattr(CyGlobalContext, "setIsBug"): import BugInit BugInit.addInit("setIsBug", setIsBug) else: BugUtil.debug("BugDll - setIsBug() not found") except: BugUtil.debug("BugDll - BULL not present")
def handle(self, element, name, type, default, module, handler, listener, log, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): utils = element.getState("gameutils") if utils: func = BugUtil.bindFunction(utils, handler) if listener: listenerFunc = BugUtil.bindFunction(utils, listener) elif not module: raise BugUtil.ConfigError("Element <%s> requires attribute module or be enclosed in <gameutils> that defines a class", element.tag) else: func = BugUtil.lookupFunction(module, handler) if listener: listenerFunc = BugUtil.lookupFunction(module, listener) createCallback(name, func, self.createValue(type, default), log) if listener: addNamedListener(name, listenerFunc, log) else: BugUtil.info("BugGameUtils - ignoring <%s> %s, requires dll version %s", element.tag, name, self.resolveDll(element, dll))
def handle(self, element, module, clazz, handlers, listeners, override, log, dll): override = self.isTrue(override) dll = BugDll.decode(dll) if self.isDllOkay(element, dll): if clazz: utils = BugUtil.callFunction(module, clazz) element.setState("gameutils", utils) if handlers or listeners: if handlers: for handler in handlers.replace(",", " ").split(): addHandler(BugUtil.bindFunction(utils, handler), override, log) if listeners: for listener in listeners.replace(",", " ").split(): func = None if not listener.endswith(LISTENER_SUFFIX): try: func = BugUtil.bindFunction(utils, listener + LISTENER_SUFFIX) except BugUtil.ConfigError: pass if not func: try: func = BugUtil.bindFunction(utils, listener) except BugUtil.ConfigError: raise BugUtil.ConfigError("Game utils %s.%s must define function %s or %s", module, clazz, listener, listener + "Listener") addListener(func, log) else: addClassUtils(utils, override, log) else: if handlers or listeners: if handlers: for handler in handlers.replace(",", " ").split(): addHandler(BugUtil.lookupFunction(module, handler), override, log) if listeners: for listener in listeners.replace(",", " ").split(): addListener(BugUtil.lookupFunction(module, listener), log) else: addModuleUtils(BugUtil.lookupModule(module), override, log) else: BugUtil.info("BugGameUtils - ignoring <%s> %s.%s, requires dll version %s", element.tag, module, clazz, self.resolveDll(element, dll))
def onCombatResult(argsList): CyUnitW, CyUnitL = argsList # Captives # Check that the losing unit is not an animal and the unit does not have a capture type defined in the XML if (CyUnitW.isMadeAttack() and not CyUnitL.isAnimal() and CyUnitL.getDomainType() == giDomainLand and CyUnitW.getDomainType() == giDomainLand and CyUnitL.getCaptureUnitType() == -1): iCaptureProbability = CyUnitW.captureProbabilityTotal() iCaptureResistance = CyUnitL.captureResistanceTotal() iChance = iCaptureProbability - iCaptureResistance BugUtil.info( "CaptureSlaves: Chance to capture a captive is %d (%d - %d)", iChance, iCaptureProbability, iCaptureResistance) if iChance > GAME.getSorenRandNum(100, "Slave"): # 0-99 if CyUnitL.isHasUnitCombat( GC.getInfoTypeForString('UNITCOMBAT_SPECIES_NEANDERTHAL')): iUnit = GC.getInfoTypeForString('UNIT_CAPTIVE_NEANDERTHAL') sMessage = TRNSLTR.getText("TXT_KEY_MSG_NEANDERTHAL_CAPTIVE", ()) else: iUnit = GC.getInfoTypeForString('UNIT_CAPTIVE_MILITARY') sMessage = TRNSLTR.getText("TXT_KEY_MSG_MILITARY_CAPTIVE", ()) iPlayerW = CyUnitW.getOwner() X = CyUnitW.getX() Y = CyUnitW.getY() CyUnit = GC.getPlayer(iPlayerW).initUnit( iUnit, X, Y, UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION) if CyUnitW.isHiddenNationality(): CyUnit.doHNCapture() if iPlayerW == GAME.getActivePlayer(): CvUtil.sendMessage(sMessage, iPlayerW, 8, 'Art/Interface/Buttons/Civics/Serfdom.dds', ColorTypes(44), X, Y, True, True)
def init(): """ Checks for the presence of the BUFFY DLL and sets the global flags. """ if isEnabled(): if BugPath.isMac(): BugUtil.debug("BUFFY is not active on Mac (no DLL)") else: try: if gc.isBuffy(): global IS_DLL_PRESENT, IS_DLL_IN_CORRECT_PATH, IS_ACTIVE IS_DLL_PRESENT = True IS_ACTIVE = True BugUtil.info("BUFFY is active (API version %d)", gc.getBuffyApiVersion()) try: dllPath = gc.getGame().getDLLPath() exePath = gc.getGame().getExePath() dllPathThenUp3 = os.path.dirname(os.path.dirname(os.path.dirname(dllPath))) if dllPathThenUp3 == exePath: IS_DLL_IN_CORRECT_PATH = True except: pass # DLL path is borked except: BugUtil.info("BUFFY is not active (no DLL)")
def isReplayWinner(self, replay): szWinText = localText.getText("TXT_KEY_GAME_WON", ("(.*)", "(.*)")) reWinText = re.compile(szWinText) leaderGroup = 1 typeGroup = 2 leader = replay.getLeaderName() msgNum = replay.getNumReplayMessages() - 1 BugUtil.debug("scanning replay for %s with %d msgs", leader, msgNum + 1) count = 0 while msgNum >= 0: msg = replay.getReplayMessageText(msgNum) if count > 100: BugUtil.debug("no victory message in first 100; skipping") break matches = reWinText.match(msg) if matches: type = matches.group(typeGroup) if leader in matches.group(leaderGroup).split('/'): BugUtil.info("replay: %s win for %s", type, leader) return True, type BugUtil.info("replay: %s loss for %s", type, leader) return False, type msgNum -= 1 return False, localText.getText("TXT_KEY_NONE", ())
def handle(self, element, keys, module, function, dll): dll = BugDll.decode(dll) if self.isDllOkay(element, dll): CvEventInterface.getEventManager().addShortcutHandler(keys, BugUtil.getFunction(module, function, *element.args, **element.kwargs)) else: BugUtil.info("InputUtil - ignoring <%s> %s, requires dll version %s", element.tag, keys, self.resolveDll(element, dll))
def safeInfoPath(message, path): try: BugUtil.info(message, path) except: pass
def safeInfoPath(message, path): try: BugUtil.info(message, path) except: pass