Exemplo n.º 1
0
        def loadAssets(self, path_to_folder=Globals.AssetsRootPath):
            # This dictionary will hold the contents of asset files in the format:
            #   key = the files path, e.g. "assets/readme.txt"
            #   value = the conents of the file
            assets = {}

            # Go through each file in the specified dir and add contents to the dictionary
            for root, subdirs, files in os.walk(path_to_folder):
                for file_name in files:
                    file_path = os.path.join(root,file_name)
                    # Normalize the given path
                    norm_path = os.path.normcase(os.path.normpath(file_path))
                    if '.DS_Store' in norm_path:
                        continue    # ignore .DS_Store
                    with open(norm_path) as f:
                        assets[norm_path] = f.read()

            # Do parsing of any custom scripts and yaml
            parser = Parser()  # instantiate parser on the fly
            for path in assets:
                if path[-len('.ignore'):] == '.ignore':
                    continue
                if os.path.normcase(os.path.normpath('assets/scripts')) in path:
                    # replace string with parsed (string, BodyNode)[]
                    assets[path] = parser.parseScript(assets[path])
                elif os.path.normcase(os.path.normpath('assets/config')) in path:
                    # replace string with parsed Python dict
                    assets[path] = yaml.load(assets[path])

            self.assets = assets

            # construct reverse item mapping
            itemsConfig = self.getConfig(Globals.ItemsConfigPath)
            for item in itemsConfig:
                self.reverseItem[itemsConfig[item]['name']] = item

            # construct reverse room mapping
            areasConfig = self.getConfig(Globals.AreasConfigPath)
            for area in areasConfig:
                self.reverseRoom[area] = {}
                roomsConfig = self.getConfig(areasConfig[area]['roomsConfig'])
                for room in roomsConfig:
                    self.reverseRoom[area][roomsConfig[room]['name']] = room
Exemplo n.º 2
0
        def loadAssets(self):
            # if assets already in memory do not reload
            if self.isLoaded:
                return

            # set root to assets for later reference
            self.root_path = Globals.AssetsRootPath

            # This dictionary will hold the contents of asset files in the format:
            #   key = the files path, e.g. "assets/readme.txt"
            #   value = the conents of the file
            assets = {}

            # Returns whether all files can read and parse successfully
            success_flag = True

            # recognized filename extensions
            whitelist_ext = ['.txt', '.wtxt', '.yml']
            # Go through each file in the specified dir and add contents to the dictionary
            for root, subdirs, files in os.walk(self.root_path):
                for file_name in files:
                    try:
                        # Normalize the given path
                        norm_path = joinAndNorm(root, file_name)
                        # Check that filename ends with recognized extension
                        ext_not_valid = True
                        for ext in whitelist_ext:
                            if len(norm_path) >= len(
                                    ext) and norm_path[-len(ext):] == ext:
                                ext_not_valid = False
                                break
                        if ext_not_valid:
                            continue
                        # Store file text into dictionary
                        with codecs.open(norm_path, "r", "utf-8") as f:
                            assets[norm_path] = f.read().replace(
                                '\r\n', '\n').replace('\r', '\n')
                    except:
                        success_flag = False

            # Do parsing of any custom scripts and yaml
            parser = Parser()  # instantiate parser on the fly
            for path in assets:
                try:
                    if joinAndNorm(self.root_path, 'scripts') in path:
                        if 'fragments' in path:
                            # replace string with parsed BodyNode
                            assets[path] = parser.parseScriptFragment(
                                assets[path])
                        else:
                            # replace string with parsed (string, BodyNode)[]
                            assets[path] = parser.parseScript(assets[path])
                    elif joinAndNorm(self.root_path, 'config') in path:
                        # replace string with parsed Python dict
                        assets[path] = yaml.load(assets[path])
                    elif joinAndNorm(self.root_path, 'maps') in path:
                        # replace string with split string
                        assets[path] = assets[path].split('\n')
                except:
                    success_flag = False
                    if Globals.IsDev:
                        print(path)
                        traceback.print_exc()

            self.assets = assets

            # construct reverse item mapping
            itemsConfig = self.getConfig(Globals.ItemsConfigPath)
            for item in itemsConfig:
                self.reverseItem[itemsConfig[item]['name'].lower()] = item

            # construct reverse room mapping
            areasConfig = self.getConfig(Globals.AreasConfigPath)
            for area in areasConfig:
                self.reverseRoom[area] = {}
                roomsConfig = self.getConfig(areasConfig[area]['roomsConfig'])
                for room in roomsConfig:
                    self.reverseRoom[area][roomsConfig[room]
                                           ['name'].lower()] = room

            self.isLoaded = True
            return success_flag