OSP Code [WB][Modmerger] module_slots

Users who are viewing this thread

Ikaguia

Grandmaster Knight
have you ever got trouble with conflicted slots? (i.e: two troop slots set to 137)
have you ever thought module_constants was to messy because it had both slots and constants?
well, this is a solution, it will generate "module_slot_constants" which will be imported by modmerger, it uses a simple file, with a very simple sintax.
example:
Code:
############################################################################
## 0) slot name
## 1) slot type (defined above)
## 2) reserve x following slots
############################################################################

slots = [   
	(	"troop_level",troop_slot,0),		#slot_troop_level		= 1
	(	"troop_leaded_party",troop_slot,10),	#slot_troop_leaded_party	= 2
	(	"troop_inv_party",troop_slot,0),	#slot_troop_inv_party		= 13
	(	"agent_morale",agent_slot,0),		#slot_agent_morale		= 1
] # slots

how to use:
1- make a new file and name it "module_slots.py"
2- make a new file and name it "module_slots_constants.py"
3- paste the following code into "module_slots.py":
Code:
# -*- coding: cp1254 -*-
from process_common import *
from header_common import *
import string

#slot types
troop_slot		= 1
party_slot		= 2
faction_slot		= 3
scene_slot		= 4
party_template_slot	= 5
agent_slot		= 6
quest_slot		= 7
item_slot		= 8
player_slot		= 9
team_slot		= 10
scene_prop_slot		= 11
dummy_troop1		= 12#slots for dummy troop 1
last_slot_type		= 13#increase this every time you add a new slot_type

############################################################################
## 0) slot name
## 1) slot type (defined above)
## 2) reserve x following slots
############################################################################

slots = [   
#	(	"troop_level",troop_slot,0),		#slot_troop_level		= 1
#	(	"troop_leaded_party",troop_slot,10),	#slot_troop_leaded_party	= 2
#	(	"troop_inv_party",troop_slot,0),	#slot_troop_inv_party		= 13
#	(	"agent_morale",agent_slot,0),		#slot_agent_morale		= 1
] # slots

# collation of all *_slots.py from active mods
# import and merge related variables from all {active_mod}_slots.py for all active mods
try:
    from modmerger_options import options, mods_active
    from modmerger import mod_get_process_order, mod_is_active
    from util_common import add_objects, logger
    modcomp_name = "slots"
    var_list = ["slots",]
    #from modmerger import modmerge
    #modmerge(var_set)
    mod_process_order = mod_get_process_order(modcomp_name)
    vars_to_import= ["slots"]
    for x in mod_process_order:
        if(mod_is_active(x) and x <> "module_slots"): # must exclude this file since we are using this file as base
            try:
                #mergefn_name = "modmerge_%s"%(modcomp_name)
                target_module_name = "%s_%s"%(x,modcomp_name)
                _temp = __import__( target_module_name , globals(), locals(), vars_to_import,-1)
                logger.info("Merging objects for component \"%s\" from mod \"%s\"..."%(modcomp_name,x))
                add_objects(mod_options, _temp.mod_options) # import from target module.
                # TODO: collect option pages
            except ImportError:
                errstring = "Failed importing for component \"%s\" for mod \"%s\"." % (modcomp_name, x)
                logger.debug(errstring)
        else:
            errstring = "Mod \"%s\" not active for Component \"%s\"." % (x, modcomp_name)
            logger.debug(errstring)
except:
    raise
# collation end
# At this point, slots will contain the list of all slots specified.



## utility functions

from util_wrappers import *
from util_presentations import *

# helper wrapper to access mod_options
class SlotsWrapper(BaseWrapper):
    def __init__(self, _data):
        # verify _data
        if( not isinstance(_data,TupleType) or (len(_data)<2)):
            raise ValueError("ItemSetWrapper: Wrapped must be a tuple.")
        BaseWrapper.__init__(self,_data)
    def GetId(self):
        if len(self.data) > 0: 
            return self.data[0]
        return None
    def GetType(self):
        if len(self.data) > 1: 
            return self.data[1]
        return None
    def GetReservedSlots(self):
        if len(self.data) > 2: 
            return self.data[2]
        return None
## class SlotsWrapper

def generate_constants():
    file = open("./module_slots_constants.py","w")
    file.write("from module_slots import *\n")
    for i in range(0, last_slot_type):
        current_type_count = 0
        for i_slot in range(0, len(slots)):
            aSlot = SlotsWrapper(slots[i_slot])
            reservedSlots = aSlot.GetReservedSlots()
            if aSlot.GetType() == i:
                current_type_count += 1
                file.write("slot_%s = %d\n"%(convert_to_identifier(slots[i_slot][0]),current_type_count))
                current_type_count += reservedSlots
    file.close()
generate_constants()
4- paste the following code into "module_slots_constants.py":
Code:
from module_slots import *
5- add "module_slots" to active mods list on "modmerger_options.py"
6- copy your constants into the file
7- compile

obs:
you don't need to copy native constants into it too, you can just make a dummy slot and reserve the number of slots native uses.
 
Back
Top Bottom