1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

changing callbacks for Pitboss Games in K-Mod

Discussion in 'Civ 4 - K-Mod: Far Beyond the Sword' started by Alrik2002, Feb 11, 2014.

  1. Alrik2002

    Alrik2002 Warlord

    Joined:
    May 24, 2012
    Messages:
    214
    Location:
    Berlin, Germany
    Hi,

    I would like to add some features using the callbacks in a similar file like CvGameUtils. I´ve made a new one and have registered it, so that it all works perfectly in SP and MP, when no pitboss-host is used. If I use a pitboss-host I have OOS-Errors, because the gamestats of the pitboss differs from all the other players, when a function of my individual CvGameUtils.py is used.

    While searching the code and the internet I found the following parts.

    BugInit.py
    Code:
    def init():
    	"""Performs the one-time initialization of the BUG core and all mods."""
    	global g_initDone
    	if g_initDone:
    		BugUtil.debug("BugInit - init() already complete")
    		return
    	if not CyGame().isFinalInitialized():
    		BugUtil.debug("BugInit - game not fully initialized")
    		return
    [COLOR="Red"]	# K-Mod. Don't use any BUG features if this is a PitBoss server.
    	if CyGame().isPitbossHost():
    		BugUtil.debug("BugInit - BUG disabled for PitBoss host")
    		return
    	# K-Mod end[/COLOR]
    BugEventManager.py
    Code:
    def configure(logging=None, noLogEvents=None):
    	# """Sets the global event manager's logging options."""
    	# if g_eventManager:
    		# g_eventManager.setLogging(logging)
    		# g_eventManager.setNoLogEvents(noLogEvents)
    	# else:
    		# BugUtil.error("BugEventManager - BugEventManager not setup before configure()")
    	# K-Mod. I've expanded the purpose of this function.
    	"""Sets the global event manager's logging options. And registers some BUG events handlers."""
    
    	if not g_eventManager:
    		BugUtil.error("BugEventManager - BugEventManager not setup before configure()")
    		return
    
    	g_eventManager.setLogging(logging)
    	g_eventManager.setNoLogEvents(noLogEvents)
    
    [COLOR="Red"]	# K-Mod. Don't use register BUG events for PitBoss host.
    	# (Note: actually, if this is a PitBoss host, this function won't even be called
    	#  because the BUG core will not initialize any mod components in PitBoss mode.)
    	if CyGame().isPitbossHost():
    		BugUtil.debug("BugEventManager - skipping event registration for PitBoss host")
    		return
    	# K-Mod end[/COLOR]
    I checked the logfiles of the pitboss and saw, that it differs a lot from the logfile of the computer I use to play Civ. According to the logfile of the pitboss my individual CvGameUtils.py is loaded but there are no messages about handler registration. For me it seems like the pitboss doesn´t use my individual CvGameUtils.py and the players use them, because their computers use the default-method of Bug to register the handlers. Do you have any ideas how I could solve the problem or where I should integrate my code instead of using my individual CvGameUtils.py?

    What would be the worst case if I would comment out the change in BUGinit.py like this:
    Code:
    def init():
    	"""Performs the one-time initialization of the BUG core and all mods."""
    	global g_initDone
    	if g_initDone:
    		BugUtil.debug("BugInit - init() already complete")
    		return
    	if not CyGame().isFinalInitialized():
    		BugUtil.debug("BugInit - game not fully initialized")
    		return
    	[COLOR="Red"]# K-Mod. Don't use any BUG features if this is a PitBoss server.
    	#if CyGame().isPitbossHost():
    	#	BugUtil.debug("BugInit - BUG disabled for PitBoss host")
    	#	return
    	# K-Mod end[/COLOR]
    Thank you very much!
     
  2. karadoc

    karadoc AI programmer

    Joined:
    Oct 3, 2005
    Messages:
    1,568
    Location:
    Australia
    To be honest, I don't remember why I changed it to not load BUG for pitboss games. But I've done a search of the git log and found this.

    Apparently this is the reason:
    So, by the looks of that log entry, there was a legitimate reason for disabling BUG. I don't remember exactly what kind of problems it was causing, but my guess is that the pitboss server will sometimes crash if that BUG stuff is enabled. I guess you could test it by re-enabling it if you like.

    As for your gameplay changes, I don't have a good guess for why they aren't being used by the pitboss server. What particular python changes have you made?
     
  3. Alrik2002

    Alrik2002 Warlord

    Joined:
    May 24, 2012
    Messages:
    214
    Location:
    Berlin, Germany
    Hi karadoc,

    thank your for your answer. I´ve tested to comment out the few lines and the oos is gone. While loading the game in the pitboss I got some python errors:

    Code:
    Traceback (most recent call last):
      File "BugInit", line 98, in callInits
      File "Scoreboard", line 102, in init
      File "Scoreboard", line 153, in __init__
    RuntimeError: unidentifiable C++ exception
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "autologEventManager", line 308, in onLoadGame
      File "autologEventManager", line 1448, in storeWhip
    AttributeError: 'NoneType' object has no attribute 'getNumCities'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 176, in onLoadGame
      File "Civ4lerts", line 237, in _reset
      File "Civ4lerts", line 299, in reset
      File "PlayerUtil", line 547, in playerCities
    AttributeError: 'NoneType' object has no attribute 'firstCity'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 176, in onLoadGame
      File "Civ4lerts", line 237, in _reset
      File "Civ4lerts", line 299, in reset
      File "PlayerUtil", line 547, in playerCities
    AttributeError: 'NoneType' object has no attribute 'firstCity'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 260, in onBeginActivePlayerTurn
      File "Civ4lerts", line 225, in checkAllActivePlayerCities
      File "PlayerUtil", line 547, in playerCities
    AttributeError: 'NoneType' object has no attribute 'firstCity'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 746, in onBeginActivePlayerTurn
      File "TradeUtil", line 153, in getGoldTradePartners
      File "TradeUtil", line 268, in getTradePartnersByTeam
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 784, in onBeginActivePlayerTurn
      File "TradeUtil", line 153, in getGoldTradePartners
      File "TradeUtil", line 268, in getTradePartnersByTeam
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "MoreCiv4lerts", line 140, in onBeginActivePlayerTurn
      File "PyHelpers", line 46, in getTeam
      File "PyHelpers", line 42, in getTeamID
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 260, in onBeginActivePlayerTurn
      File "Civ4lerts", line 225, in checkAllActivePlayerCities
      File "PlayerUtil", line 547, in playerCities
    AttributeError: 'NoneType' object has no attribute 'firstCity'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 746, in onBeginActivePlayerTurn
      File "TradeUtil", line 153, in getGoldTradePartners
      File "TradeUtil", line 268, in getTradePartnersByTeam
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "Civ4lerts", line 784, in onBeginActivePlayerTurn
      File "TradeUtil", line 153, in getGoldTradePartners
      File "TradeUtil", line 268, in getTradePartnersByTeam
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    Traceback (most recent call last):
      File "BugEventManager", line 365, in _handleDefaultEvent
      File "MoreCiv4lerts", line 140, in onBeginActivePlayerTurn
      File "PyHelpers", line 46, in getTeam
      File "PyHelpers", line 42, in getTeamID
    AttributeError: 'NoneType' object has no attribute 'getTeam'
    I´m able to solve some of these when I use:
    Code:
    if CyGame().isPitbossHost():
    	return
    or

    Code:
    if gc.getGame().getActivePlayer() == -1:
    	return
    I´ve seen your entry at github too, and I was not sure which python errors would come later in the game so I changed it back and tried other solutions, so far without success.

    I´ve two files with problems (AlriksEventManager and AlriksMultiModGameUtils). I´m pretty sure that I´ve checked them in correctly in the init-file, since they are loaded at the players computers. Until now, I´ve only tried to get the AlriksMultiModGameUtils loaded by the pitboss.

    At first I´ve searched how the BUG-Mod loads the GameUtils. I found the following entry in the file CvGameInterfaceFile.py:

    Code:
    ## CvGameInterfaceFile
    ##
    ## Overrides the use of CvGameUtils with BUG's modular game utils dispatcher.
    ##
    ## Copyright (c) 2009 The BUG Mod.
    ##
    ## Author: EmperorFool
    
    import BugGameUtils
    
    GameUtils = BugGameUtils.getDispatcher()
    Where this is called from I didn´t found.

    BugGameUtils:
    Code:
    def getDispatcher():
    	return g_dispatcher
    and

    Code:
    def init(log=None):
    	BugUtil.debug("BugGameUtils - initializing")
    	global g_dispatcher
    	g_dispatcher = Dispatcher(log)
    After this I don´t understand what BUG ist doing exactly. I assume it´s setting the defaults for each callback and it´s checking if listeners or handlers are changing these. I´ve also found the following lines:

    Code:
    class Dispatcher:
    	
    	def __init__(self, log=None):
    		if log is None:
    			self._log = DEFAULT_LOGGING
    		else:
    			self._log = log
    		self._callbacks = {}
    [COLOR="Red"]		self._baseUtils = CvGameUtils.CvGameUtils()
    		clazz = CvGameUtils.CvGameUtils[/COLOR]
    		for name, func in clazz.__dict__.iteritems():
    			if not name.startswith("_") and isinstance(func, types.FunctionType):
    				self._createCallback(name, BugUtil.bindFunction(self._baseUtils, name), None, self._log)
    I thought the easiest way to use my new AlriksMultiModGameUtils would it be to change the name to CvGameUtils and copy it in the mod, since I´ve also a version with all the standard callbacks in it. That didn´t work.

    Then I tried to load the AlriksMultiModGameUtils out of python (import in another python file). The logs say it´s loaded, but the modified functions aren´t used.

    Then I tried to set my AlriksMultiModGameUtils as the GameUtils which should be used by the pitboss using a method like in CvGameInterfaceFile.py (shown above):

    Code:
    def init():
                    """Performs the one-time initialization of the BUG core and all mods."""
                    global g_initDone
                    if g_initDone:
                                   BugUtil.debug("BugInit - init() already complete")
                                   return
                    if not CyGame().isFinalInitialized():
                                   BugUtil.debug("BugInit - game not fully initialized")
                                   return
                    # K-Mod. Don't use any BUG features if this is a PitBoss server.
                    if CyGame().isPitbossHost():
                                   BugUtil.debug("BugInit - BUG disabled for PitBoss host")
                   [COLOR="Red"]GameUtils = AlriksMultiModGameUtils.AlriksMultiModGameUtils ()
                                                   return[/COLOR]
                    # K-Mod end
    That didn´t work either.:confused:

    Sorry for the very long posting and thank you very much for your help.
     

Share This Page