def __buildCache(geometryID, geometryName, defaultXml, isFullCache, isDevelopmentArena=False): global g_geometryCache sectionName = ARENA_TYPE_XML_PATH + geometryName + '.xml' section = ResMgr.openSection(sectionName) if section is None: if isDevelopmentArena: return False raise SoftException("Can't open '%s'" % sectionName) geometryCfg = __readGeometryCfg(geometryID, geometryName, section, defaultXml) geometryType = GeometryType(geometryCfg) g_geometryCache[geometryID] = __addBonusTypeOverrides( geometryType, section, defaultXml) if isFullCache: spaceName = os.path.basename(geometryCfg['geometry']) spaceData = __readSpaceCfg(spaceName) g_spaceCache[geometryID] = spaceData for gameplayCfg in __readGameplayCfgs(geometryName, section, defaultXml, geometryCfg): arenaType = ArenaType(geometryType, gameplayCfg) g_cache[arenaType.id] = arenaType g_gameplayNames.add(arenaType.gameplayName) ResMgr.purge(sectionName, True) return True
def __readItemFolder(itemCls, folder, itemName, storage): itemsFileName = os.path.join(folder, itemName + 's', 'list.xml') dataSection = ResMgr.openSection(itemsFileName) try: _readItems(cache, itemCls, (None, itemName + 's/list.xml'), dataSection, itemName, storage) finally: ResMgr.purge(itemsFileName) return
def init(self, nofail=True): cfg = self.__cfg try: try: section = ResMgr.openSection(_CONFIG_FILE) cfg.update(((k, CollectionDescriptor(**v)) for k, v in _readCollections( section['collections']).iteritems())) cfg['years'] = [key for key in cfg if 'ny' in key] except Exception: self.ffini() if nofail: raise LOG_CURRENT_EXCEPTION() finally: ResMgr.purge(_CONFIG_FILE)
def __readShared(self, clearCache=False): if clearCache: ResMgr.purge(TREE_SHARED_REL_FILE_PATH) shared = {'settings': {}, 'grids': {}, 'default': {}, 'lines': {}} section = ResMgr.openSection(TREE_SHARED_REL_FILE_PATH) if section is None: _xml.raiseWrongXml(None, TREE_SHARED_REL_FILE_PATH, 'can not open or read') xmlCtx = (None, TREE_SHARED_REL_FILE_PATH) precessed = _xml.getChildren(xmlCtx, section, 'settings-set') for name, settingsSec in precessed: settingsName = settingsSec.asString xPath = '{0:>s}/{1:>s}/{2:>s}'.format(TREE_SHARED_REL_FILE_PATH, name, settingsName) xmlCtx = (None, xPath) settings = {} for _, settingSec in settingsSec.items(): name = _xml.readString(xmlCtx, settingSec, 'name') if name not in DISPLAY_SETTINGS: LOG_ERROR('Setting is invalid', name) continue reader = DISPLAY_SETTINGS[name] value = getattr(_xml, reader)(xmlCtx, settingSec, 'value') settings[name] = value for name in DISPLAY_SETTINGS.iterkeys(): if name not in settings: raise _ConfigError(xmlCtx, 'Setting not found') shared['settings'][settingsName] = settings if self.__availableNations is None: self.__availableNations = self.__readAvailableNations( (None, TREE_SHARED_REL_FILE_PATH), section) self.__readSharedMetrics(shared, xmlCtx, section) if self.__override: subSec = section['overrides/{0:>s}'.format(self.__override)] if subSec: xmlCtx = (None, '{0:>s}/overrides/{1:>s}'.format( TREE_SHARED_REL_FILE_PATH, '', self.__override)) self.__readSharedMetrics(shared, xmlCtx, subSec) self.__readDefaultLine(shared, xmlCtx, section) return shared
def getAvailableNations(self): if self.__availableNations is None: section = ResMgr.openSection(TREE_SHARED_REL_FILE_PATH) if section is None: _xml.raiseWrongXml(None, TREE_SHARED_REL_FILE_PATH, 'can not open or read') xmlCtx = (None, TREE_SHARED_REL_FILE_PATH) self.__availableNations = self.__readAvailableNations( xmlCtx, section) return self.__availableNations[:]
def __buildCache(geometryID, geometryName, defaultXml): global g_geometryCache sectionName = ARENA_TYPE_XML_PATH + geometryName + '.xml' section = ResMgr.openSection(sectionName) if section is None: raise SoftException("Can't open '%s'" % sectionName) geometryCfg = __readGeometryCfg(geometryID, geometryName, section, defaultXml) if geometryCfg['isDevelopment'] and not IS_DEVELOPMENT: return else: geometryType = GeometryType(geometryCfg) g_geometryCache[geometryID] = __addBonusTypeOverrides(geometryType, section, defaultXml) for gameplayCfg in __readGameplayCfgs(geometryName, section, defaultXml, geometryCfg): arenaType = ArenaType(geometryType, gameplayCfg) g_cache[arenaType.id] = arenaType g_gameplayNames.add(arenaType.gameplayName) ResMgr.purge(sectionName, True) return
def _readItemTypes(): xmlPath = ITEM_DEFS_PATH + 'item_types.xml' section = ResMgr.openSection(xmlPath) if section is None: _xml.raiseWrongXml(None, xmlPath, 'can not open or read') xmlCtx = (None, xmlPath) res = {} for index, name in enumerate(ITEM_TYPE_NAMES): if name.startswith('_'): continue itemSection = _xml.getSubsection(xmlCtx, section, name) ctx = (xmlCtx, name) tagNames = [] tags = {} if itemSection.has_key('tags'): for tagSection in itemSection['tags'].values(): tagName = intern(tagSection.name) if tags.has_key(tagName): _xml.raiseWrongXml(xmlCtx, 'tags' + tagName, 'tag name is not unique') tagDescr = {'name': tagName, 'index': len(tagNames)} if IS_CLIENT: tagDescr['userString'] = i18n.makeString( tagSection.readString('userString')) tagDescr['description'] = i18n.makeString( tagSection.readString('description')) tags[tagName] = tagDescr tagNames.append(tagName) itemType = {'index': index, 'tags': tags, 'tagNames': tuple(tagNames)} if IS_CLIENT: itemType['userString'] = i18n.makeString( itemSection.readString('userString')) itemType['description'] = i18n.makeString( itemSection.readString('description')) res[name] = itemType section = None itemSection = None tagSection = None ResMgr.purge(xmlPath, True) return res
def readCustomizationCacheFromXml(cache, folder): """Fill customization cache with items read from folder.""" def __readItemFolder(itemCls, folder, itemName, storage): itemsFileName = os.path.join(folder, itemName + 's', 'list.xml') dataSection = ResMgr.openSection(itemsFileName) try: _readItems(cache, itemCls, (None, itemName + 's/list.xml'), dataSection, itemName, storage) finally: ResMgr.purge(itemsFileName) return pgFile = os.path.join(folder, 'priceGroups', 'list.xml') _readPriceGroups(cache, (None, 'priceGroups/list.xml'), ResMgr.openSection(pgFile), 'priceGroup') ResMgr.purge(pgFile) pgFile = os.path.join(folder, 'default_colors.xml') _readDefaultColors(cache, (None, 'default_colors.xml'), ResMgr.openSection(pgFile), 'default_color') ResMgr.purge(pgFile) __readItemFolder(cc.PaintItem, folder, 'paint', cache.paints) __readItemFolder(cc.CamouflageItem, folder, 'camouflage', cache.camouflages) __readItemFolder(cc.ModificationItem, folder, 'modification', cache.modifications) __readItemFolder(cc.DecalItem, folder, 'decal', cache.decals) __readItemFolder(cc.StyleItem, folder, 'style', cache.styles) return None
def init(isFullCache=True): global g_gameplayNames global g_cache global g_geometryNamesToIDs global g_gameplaysMask rootSection = ResMgr.openSection(_LIST_XML) if rootSection is None: raise SoftException("Can't open '%s'" % _LIST_XML) defaultXml = ResMgr.openSection(_DEFAULT_XML) if defaultXml is None: raise SoftException("Can't open '%s'" % _DEFAULT_XML) defaultGameplayTypesXml = defaultXml['gameplayTypes'] if defaultGameplayTypesXml is None or not defaultGameplayTypesXml: raise SoftException("No defaults for 'gameplayTypes'") geometriesSet = set() for key, value in rootSection.items(): isDevelopmentArena = value.readBool('isDevelopment') geometryID = value.readInt('id') if geometryID in geometriesSet: raise SoftException('Geometry ID=%d is not unique' % geometryID) buildResult = __buildCache(geometryID, value.readString('name'), defaultXml, isFullCache, isDevelopmentArena) if buildResult: geometriesSet.add(geometryID) ResMgr.purge(_LIST_XML, True) ResMgr.purge(_DEFAULT_XML, True) g_gameplaysMask = getGameplaysMask(g_gameplayNames) g_geometryNamesToIDs = { arenaType.geometryName: arenaType.geometryID for arenaType in g_cache.itervalues() } return
def __readSettings(xmlPath): configXml = ResMgr.openSection(xmlPath) if configXml is None: _xml.raiseWrongXml(None, xmlPath, 'can not open or read') xmlCtx = (None, xmlPath) unique = set() resDict = {} for id_, childSection in enumerate( _xml.getChildren(xmlCtx, configXml, 'nation_groups')): _, changeableVehicles = childSection for vehicle in changeableVehicles.values(): vehicleType = vehicle.asString if vehicleType in unique: ctx = (xmlCtx, 'group') _xml.raiseWrongXml(ctx, '', '%s is not unique' % (vehicleType, )) continue unique.add(vehicleType) nationGroup = resDict.setdefault(id_, NationGroup(id_)) nationGroup.tankList.append(vehicleType) ResMgr.purge(xmlPath, True) return resDict
def __readItemFolder(itemCls, folder, itemName, storage): progressionFileName = os.path.join(folder, itemName + 's', 'progression.xml') dataSection = ResMgr.openSection(progressionFileName) progression = {} if dataSection: try: _readProgression((None, itemName + 's/progression.xml'), dataSection, progression) finally: ResMgr.purge(progressionFileName) itemsFileName = os.path.join(folder, itemName + 's', 'list.xml') dataSection = ResMgr.openSection(itemsFileName) try: _readItems(cache, itemCls, (None, itemName + 's/list.xml'), dataSection, itemName, storage, progression) finally: ResMgr.purge(itemsFileName) return
def readCustomizationCacheFromXml(cache, folder): def __readItemFolder(itemCls, folder, itemName, storage): progressionFileName = os.path.join(folder, itemName + 's', 'progression.xml') dataSection = ResMgr.openSection(progressionFileName) progression = {} if dataSection: try: _readProgression((None, itemName + 's/progression.xml'), dataSection, progression) finally: ResMgr.purge(progressionFileName) itemsFileName = os.path.join(folder, itemName + 's', 'list.xml') dataSection = ResMgr.openSection(itemsFileName) try: _readItems(cache, itemCls, (None, itemName + 's/list.xml'), dataSection, itemName, storage, progression) finally: ResMgr.purge(itemsFileName) return pgFile = os.path.join(folder, 'priceGroups', 'list.xml') _readPriceGroups(cache, (None, 'priceGroups/list.xml'), ResMgr.openSection(pgFile), 'priceGroup') ResMgr.purge(pgFile) pgFile = os.path.join(folder, 'default.xml') _readDefault(cache, (None, 'default.xml'), ResMgr.openSection(pgFile), 'default') ResMgr.purge(pgFile) pgFile = os.path.join(folder, 'fonts', 'list.xml') _readFonts(cache, (None, 'fonts/list.xml'), ResMgr.openSection(pgFile), 'font') ResMgr.purge(pgFile) pgFile = os.path.join(folder, 'personal_numbers', 'prohibitedNumbers.xml') _readProhibitedNumbers((None, 'personal_numbers/prohibitedNumbers.xml'), ResMgr.openSection(pgFile)) ResMgr.purge(pgFile) __readItemFolder(cc.PaintItem, folder, 'paint', cache.paints) __readItemFolder(cc.CamouflageItem, folder, 'camouflage', cache.camouflages) __readItemFolder(cc.ModificationItem, folder, 'modification', cache.modifications) __readItemFolder(cc.DecalItem, folder, 'decal', cache.decals) __readItemFolder(cc.ProjectionDecalItem, folder, 'projection_decal', cache.projection_decals) __readItemFolder(cc.StyleItem, folder, 'style', cache.styles) __readItemFolder(cc.InsigniaItem, folder, 'insignia', cache.insignias) __readItemFolder(cc.PersonalNumberItem, folder, 'personal_number', cache.personal_numbers) __readItemFolder(cc.SequenceItem, folder, 'sequence', cache.sequences) __readItemFolder(cc.AttachmentItem, folder, 'attachment', cache.attachments) _validateStyles(cache) return None
def __readNation(self, shared, nation, clearCache=False): xmlPath = NATION_TREE_REL_FILE_PATH % nation if clearCache: ResMgr.purge(xmlPath) section = ResMgr.openSection(xmlPath) if section is None: LOG_ERROR('can not open or read nation tree: ', nation, xmlPath) return {} else: xmlCtx = (None, xmlPath) settingsName = _xml.readString(xmlCtx, section, 'settings') if settingsName not in shared['settings']: LOG_ERROR('not found settings (<settings> tag): ', settingsName, xmlPath) return {} precessed = _xml.getSubsection(xmlCtx, section, 'grid') gridName = precessed.asString if gridName not in shared['grids']: LOG_ERROR('not found grid (<grid> tag): ', gridName, xmlPath) return {} xPath = '{0:>s}/grid'.format(xmlPath) xmlCtx = (None, xPath) grid = shared['grids'][gridName] settings = shared['settings'][settingsName] rows = _xml.readInt(xmlCtx, precessed, 'rows') columns = _xml.readInt(xmlCtx, precessed, 'columns') self.__displaySettings[nations.INDICES[nation]] = settings nationIndex = self.__availableNations.index(nation) hasRoot = settings['hasRoot'] if hasRoot: coords = self.__makeGridCoordsWithRoot(grid, nationIndex, rows, columns) else: coords = self.__makeGridCoordsWoRoot(grid, rows, columns) getIDsByName = vehicles.g_list.getIDsByName makeIntCDByID = vehicles.makeIntCompactDescrByID getVehicle = vehicles.g_cache.vehicle precessed = _xml.getChildren(xmlCtx, section, 'nodes') displayInfo = {} for name, nodeSection in precessed: xPath = '{0:>s}/nodes/{1:>s}'.format(xmlPath, name) xmlCtx = (None, xPath) uName = '{0:>s}:{1:>s}'.format(nation, name) try: nationID, vTypeID = getIDsByName(uName) except Exception: raise _ConfigError( xmlCtx, 'Unknown vehicle type name {0:>s}'.format(uName)) nodeCD = makeIntCDByID(_VEHICLE_TYPE_NAME, nationID, vTypeID) vType = getVehicle(nationID, vTypeID) nextLevel = filter( lambda data: getTypeOfCompactDescr(data[1][1]) == _VEHICLE, enumerate(vType.unlocksDescrs)) for unlockDescr in vType.unlocksDescrs: self.__unlockPrices[unlockDescr[1]][ vType.compactDescr] = unlockDescr[0] for idx, data in nextLevel: xpCost = data[0] nextCD = data[1] required = data[2:] self.__nextLevels[nodeCD][nextCD] = (idx, xpCost, set(required)) self.__topLevels[nextCD].add(nodeCD) for itemCD in required: self.__topItems[itemCD].add(nodeCD) row = _xml.readInt(xmlCtx, nodeSection, 'row') column = _xml.readInt(xmlCtx, nodeSection, 'column') if hasRoot and row > 1 and column is 1: raise _ConfigError( xmlCtx, 'In first column must be one node - root node, {0:>s} ' .format(uName)) elif row > rows or column > columns: raise _ConfigError( xmlCtx, 'Invalid row or column index: {0:>s}, {1:d}, {2:d}'. format(uName, row, column)) lines = self.__readNodeLines(nodeCD, nation, xmlCtx, nodeSection, shared) displayInfo[nodeCD] = { 'row': row, 'column': column, 'position': coords[column - 1][row - 1], 'lines': lines } return displayInfo