def __enter__(self): '''Returns the class instance to use for Context Management''' # Get the AddonCFG instance for the .cfg file self.config = AddonCFG(self.filepath) # Add the AddonCFG instance to config_files ConfigInstances.add(self.config) # Create the list of sections to add cvars and text to self.sections = list() # Return the instance return self
def load(): # Create the cfg file with ConfigContextManager( path(__file__).parent.split('scripts')[~0][1:]) as config: # Create the gg_warmup_round instance with config.cfg_cvar('gg_warmup_round') as cvar: cvar.name = 'WARMUP ROUND' cvar.notes.append('Players cannot ' + 'level up during the warmup round.') cvar.notes.append('Warmup round is triggered ' + 'at the start of each map change.') cvar.options.append('0 = Disabled.') cvar.options.append('1 = Enabled.') cvar.default = 0 cvar.text = 'Enables or disables warmupround.' # Create the gg_warmup_timer instance with config.cfg_cvar('gg_warmup_timer') as cvar: cvar.name = 'WARMUP ROUND TIMER' cvar.options.append('The amount of time (in ' + 'seconds) that the warmup round will last.') cvar.default = 30 cvar.text = ('The amount of time (in ' + 'seconds) that the the warmup round will last.') # Create the gg_warmup_weapon instance with config.cfg_cvar('gg_warmup_weapon') as cvar: cvar.name = 'WARMUP ROUND WEAPON' cvar.notes.append('Only supports "weapon_*" entities.') cvar.notes.append('Warmup round is triggered at ' + 'the start of each map change.') cvar.options.append(' awp \tscout\taug \tmac10' + '\ttmp \tmp5navy\tump45\tp90') cvar.options.append(' galil\tfamas\tak47\tsg552\t' + 'sg550\tg3sg1\tm249\tm3') cvar.options.append(' xm1014\tm4a1\tglock\tusp ' + '\tp228\tdeagle\telite\tfiveseven') cvar.options.append(' hegrenade\tknife') cvar.options.append('') cvar.options.append(' 0 = The first level weapon') cvar.options.append(' weapon1,weapon2,weapon3 = For ' + 'each warmup, one of these weapons is chosen') cvar.options.append(' #random = For ' + 'each warmup, a random weapon is chosen.') cvar.default = 'hegrenade' cvar.text = ('The weapon that players ' + 'will use during the warmup round.') # Create the cfg section config.cfg_section('WARMUP START AND END CFG SETTINGS') # Create the gg_warmup_start_file instance with config.cfg_cvar('gg_warmup_start_file') as cvar: cvar.name = 'WARMUP ROUND START CFG FILE' cvar.description.append('Set to the .cfg ' + 'file to be executed when Warmup Round starts.') cvar.notes.append('The cfg file should contain the ' + 'GunGame values you wish to use for the current map.') cvar.notes.append('Make sure to turn off addons that should ' + 'not be used during Warmup Round "prior" to turning on ' + 'any addons that should be used during Warmup Round.') cvar.notes.append('The path to the file "must" ' + 'be relative to the "../cfg/gungame51/" folder') cvar.default = ( 'included_addon_configs/warmup_round_start_default') cvar.text = 'CFG file to be executed when Warmup Round starts.' # Create the gg_warmup_end_file instance with config.cfg_cvar('gg_warmup_end_file') as cvar: cvar.name = 'WARMUP ROUND END CFG FILE' cvar.description.append('Set to the .cfg ' + 'file to be executed when Warmup Round ends.') cvar.notes.append('The cfg file should contain the ' + 'GunGame values you wish to use for the current map.') cvar.notes.append('Make sure to turn off any addons that ' + 'were used during Warmup Round and are not needed for ' + 'the current match, "prior" to turning on any addons ' + 'that are needed for the current match.') cvar.notes.append('The path to the file "must" ' + 'be relative to the "../cfg/gungame51/" folder') cvar.default = ( 'included_addon_configs/warmup_round_end_default') cvar.text = 'CFG file to be executed when Warmup Round ends.' # Create the extension section config.cfg_section('WARMUP ROUND EXTENSION SETTINGS') with config.cfg_cvar('gg_warmup_round_min_players') as cvar: cvar.name = 'MINUMUM HUMAN PLAYERS' cvar.description.append('Set to the minimum ' + 'number of players needed for Warmup Round to end.') cvar.default = 0 cvar.text = ( 'Number of human players needed for Warmup Round to end.') with config.cfg_cvar('gg_warmup_round_max_extensions') as cvar: cvar.name = 'MAX EXTENSIONS' cvar.description.append('Number of extensions ' 'allowed before Warmup Round automatically ends.') cvar.default = 1 cvar.text = ('Maximum number of ' 'extensions allowed before Warmup Round ends.') with config.cfg_cvar('gg_warmup_round_players_reached') as cvar: cvar.name = 'MIN HUMAN PLAYERS REACHED' cvar.description.append( 'Determines whether or not to end Warmup Round ' + 'when the minimum number of players has been reached.') cvar.options.append( '0 = Never end Warmup as soon as min players is reached.') cvar.options.append('1 = Only end Warmup ' + 'if in "extended" time when min players is reached.') cvar.options.append( '2 = End Warmup Round as soon as min players is reached.') cvar.default = 0 cvar.text = ( 'Allows Warmup Round to end when min players is reached.') # Get the path to the default Warmup Round Start cfg file start_path = path(get_game_dir( 'cfg/gungame51/included_addon_configs/warmup_round_start_default.cfg')) # Does the file exist? if not start_path.isfile(): # Create the AddonCFG instance start = AddonCFG(start_path) # Add basic description of how to use the file start.text('-' * 74 + '//') start.text('//'.rjust(76)) start.text('warmup_round_start_default.cfg'.center(74) + '//') start.text('//'.rjust(76)) start.text(('This is the default file (using the ' + 'value of gg_warmup_start_file)').center(74) + '//') start.text(('used to determine the gameplay ' + '"during" Warmup Round').center(74) + '//') start.text('//'.rjust(76)) start.text(('As an example, if the server should ' + 'have gg_deathmatch during Warmup').center(74) + '//') start.text(('and gg_elimination, gg_turbo, and ' + 'gg_teamwork for the actual match,').center(74) + '//') start.text( 'the contents could look like the following'.center(74) + '//') start.text('//'.rjust(76)) start.text((' // Turn off any addons that ' + 'should not be ran during warmup').ljust(74) + '//') start.text(' gg_elimination 0'.ljust(74) + '//') start.text(' gg_turbo 0'.ljust(74) + '//') start.text(' gg_teamwork 0'.ljust(74) + '//') start.text('//'.rjust(76)) start.text((' // Turn on any addons that ' + 'should be ran during warmup').ljust(74) + '//') start.text(' gg_deathmatch 1'.ljust(74) + '//') start.text('//'.rjust(76)) start.text('-' * 74 + '//') # Write the file start.write() # Get the path to the default Warmup Round End cfg file end_path = path(get_game_dir( 'cfg/gungame51/included_addon_configs/warmup_round_end_default.cfg')) # Does the file exist? if not end_path.isfile(): # Create the AddonCFG instance end = AddonCFG(end_path) # Add basic description of how to use the file end.text('-' * 74 + '//') end.text('//'.rjust(76)) end.text('warmup_round_end_default.cfg'.center(74) + '//') end.text('//'.rjust(76)) end.text(('This is the default file (using the ' + 'value of gg_warmup_end_file)').center(74) + '//') end.text(('used to change the gameplay from ' + 'Warmup to the actual match').center(74) + '//') end.text('//'.rjust(76)) end.text(('As an example, if the server should ' + 'have gg_deathmatch during Warmup').center(74) + '//') end.text(('and gg_elimination, gg_turbo, and ' + 'gg_teamwork for the actual match,').center(74) + '//') end.text( 'the contents could look like the following'.center(74) + '//') end.text('//'.rjust(76)) end.text((' // Turn off any addons that ' + 'were ran during warmup,').ljust(74) + '//') end.text(' // but need to be off during the match'.ljust(74) + '//') end.text(' gg_deathmatch 0'.ljust(74) + '//') end.text('//'.rjust(76)) end.text((' // Turn on any addons that ' + 'should be ran for the match').ljust(74) + '//') end.text(' gg_elimination 1'.ljust(74) + '//') end.text(' gg_turbo 1'.ljust(74) + '//') end.text(' gg_teamwork 1'.ljust(74) + '//') end.text('//'.rjust(76)) end.text('-' * 74 + '//') # Write the file end.write()
class ConfigContextManager(object): '''Context Management class used to create config files''' def __init__(self, filepath): '''Called when the class is first initialized''' # Split the given file path. config_path = path(filepath).splitpath() # Get the first part of the path config_type = config_path[0] # Set the filename of the cfg file self._filename = config_path[1] + '.cfg' # Is the path from an included or custom addon? if config_type in ('included', 'custom'): # Get the name of the addon self.name = ' '.join( config_path[1].split('_')).title().replace('Gg ', 'GG ') # Set the description to use self.description = ('This file defines GunGame ' + config_type.capitalize() + ' Addon settings.') # Set the path within ../cfg/gungame51/ for the .cfg file self.cfgpath = config_type + '_addon_configs/' + self._filename # Is this from the core of GunGame? else: # Set name to None to be set later by the *_config.py file itself self.name = None # Set desc to None to be set later by the *_config.py file itself self.description = None # Set the path within ../cfg/gungame51/ for the .cfg file self.cfgpath = self._filename # Set the path to the .cfg file self.filepath = _base_config_path.joinpath(self.cfgpath) def __enter__(self): '''Returns the class instance to use for Context Management''' # Get the AddonCFG instance for the .cfg file self.config = AddonCFG(self.filepath) # Add the AddonCFG instance to config_files ConfigInstances.add(self.config) # Create the list of sections to add cvars and text to self.sections = list() # Return the instance return self def cfg_cvar(self, cvarname): '''Used to create cvars and their text for the .cfg file''' # Set "notify" to False notify = False # Is this cvar the name of the file? # Used for auto adding the "notify" flag for included/custom addons if cvarname == self._filename[:~3]: # Set the "notify" flag to True notify = True # Get the CvarContextManager instance for the current cvar section = CvarContextManager(cvarname, notify, self.config) # Add the CvarContextManager instance to the list of sections self.sections.append(section) # Return the section return section def cfg_section(self, section_name): '''Used to create separated sections within the .cfg file''' # Add the new section name to the list of sections self.sections.append(section_name.upper()) def __exit__(self, exc_type, exc_value, _traceback): '''Verifies that there is a description and creates the .cfg file''' # Was an error encountered? if _traceback: # Print the traceback es.dbgmsg(0, _traceback) # Return return False # Does the .cfg file have a description if self.description is None: # Raise an error raise ValueError( 'No description set for .cfg file "' + self._filename + '"') # Create the first line of the header self.config.text('*' * 76) # Is there nothing to add to the filename in the header? if self.name is None: # Set the topline to be just the filename topline = self._filename # Is there a name that needs to be added to the filename in the header? else: # Set the topline to be the filename and the name topline = self._filename + ' -- ' + self.name # Add the topline to the header self.config.text('*' + topline.center(74) + '*') # Add a blank line to the header self.config.text('*' + ' ' * 74 + '*') # Add the description to the header self.config.text('*' + self.description.center(74) + '*') # Add a blank line to the header self.config.text('*' + ' ' * 74 + '*') # Add the note lines to the header self.config.text('*' + 'Note: Any alteration of this file requires a'.center(74) + '*') self.config.text('*' + 'server restart or a reload of GunGame.'.center(74) + '*') # Add the last line of the header self.config.text('*' * 76) # Loop through all sections to add to the .cfg file for section in self.sections: # Is the current section just text? if isinstance(section, str): # Add 2 blank lines for separation self.config.text('\n') # Start the section header self.config.text('+' * 76) # Add the section header name self.config.text('|' + section.center(74) + '|') # End the section header self.config.text('+' * 76) # Add a blank line for separation self.config.text('') # Is the section for a cvar? else: if section.name: # Add a blank line for separation self.config.text('') # Start the cvar section header self.config.text('=' * 76) # Add the section name self.config.text('>> ' + section.name) # End the cvar section header self.config.text('=' * 76) # Print the description section.description._print_to_text() # Print the instructions section.instructions._print_to_text() # Print any extra text section.extra._print_to_text() # Print the notes section.notes._print_to_text() # Print the examples section.examples._print_to_text() # Print the options section.options._print_to_text() # Is there default_text to print? if not section.default_text is None: # Is the default_text a string? if isinstance(section.default_text, str): # Is there any text to print? if section.default_text: # Add the string to the cfg file self.config.text(section.default_text) # Is the default_text a list? elif isinstance(section.default_text, list): # Loop through each line in the list for line in section.default_text: # Add the line to the cfg file self.config.text(line) # Is the default value a string? elif isinstance(section.default, str): # Add "" around the value when printing the default self.config.text('Default Value: "' + str(section.default) + '"') # Is the default value not a string? else: # Add the default value section self.config.text('Default Value: ' + str(section.default)) # Create the ServerVar instance for the cvar current = self.config.cvar( section.cvarname, section.default, section.text) # Is the cvar supposed to be set to notify? if section.notify: # Add the notify flag current.addFlag('notify') # Write the .cfg file self.config.write() # Return return True