<s> """<STR_LIT>""" <EOL> import os <EOL> import json <EOL> import re <EOL> import sqlite3 <EOL> import logging <EOL> import starbound <EOL> import starbound . btreedb4 <EOL> from assets . blueprints import Blueprints <EOL> from assets . items import Items <EOL> from assets . species import Species <EOL> from assets . player import Player <EOL> from assets . monsters import Monsters <EOL> from assets . techs import Techs <EOL> from assets . images import Images <EOL> from assets . frames import Frames <EOL> from assets . common import asset_category <EOL> comment_re = re . compile ( <EOL> '<STR_LIT>' , <EOL> re . DOTALL | re . MULTILINE <EOL> ) <EOL> ignore_assets = re . compile ( "<STR_LIT>" , re . IGNORECASE ) <EOL> def parse_json ( content , key ) : <EOL> decoder = json . JSONDecoder ( strict = False ) <EOL> content = comment_re . sub ( lambda m : m . group ( <NUM_LIT:1> ) or '<STR_LIT>' , content ) <EOL> return decoder . decode ( content ) <EOL> def load_asset_file ( filename ) : <EOL> with open ( filename ) as f : <EOL> content = '<STR_LIT>' . join ( f . readlines ( ) ) <EOL> return parse_json ( content , filename ) <EOL> class Assets ( object ) : <EOL> def __init__ ( self , db_file , starbound_folder ) : <EOL> self . starbound_folder = starbound_folder <EOL> self . mods_folder = os . path . join ( self . starbound_folder , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . db = sqlite3 . connect ( db_file ) <EOL> self . vanilla_assets = os . path . join ( self . starbound_folder , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . image_cache = { } <EOL> def init_db ( self ) : <EOL> c = self . db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> c . execute ( """<STR_LIT>""" ) <EOL> self . db . commit ( ) <EOL> def total_indexed ( self ) : <EOL> c = self . db . cursor ( ) <EOL> try : <EOL> c . execute ( "<STR_LIT>" ) <EOL> except sqlite3 . OperationalError : <EOL> return <NUM_LIT:0> <EOL> return c . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def create_index ( self , asset_files = False ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> if not asset_files : <EOL> asset_files = self . find_assets ( ) <EOL> blueprints = Blueprints ( self ) <EOL> items = Items ( self ) <EOL> species = Species ( self ) <EOL> monsters = Monsters ( self ) <EOL> techs = Techs ( self ) <EOL> frames = Frames ( self ) <EOL> new_index_query = "<STR_LIT>" <EOL> c = self . db . cursor ( ) <EOL> for asset in asset_files : <EOL> yield ( asset [ <NUM_LIT:0> ] , asset [ <NUM_LIT:1> ] ) <EOL> tmp_data = None <EOL> if asset_category ( asset [ <NUM_LIT:0> ] ) != '<STR_LIT>' : <EOL> if asset [ <NUM_LIT:0> ] . endswith ( "<STR_LIT>" ) : <EOL> tmp_data = ( asset [ <NUM_LIT:0> ] , asset [ <NUM_LIT:1> ] , "<STR_LIT:image>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> elif blueprints . is_blueprint ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = blueprints . index_data ( asset ) <EOL> elif species . is_species ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = species . index_data ( asset ) <EOL> elif items . is_item ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = items . index_data ( asset ) <EOL> elif monsters . is_monster ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = monsters . index_data ( asset ) <EOL> elif techs . is_tech ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = techs . index_data ( asset ) <EOL> elif frames . is_frames ( asset [ <NUM_LIT:0> ] ) : <EOL> tmp_data = frames . index_data ( asset ) <EOL> else : <EOL> logging . warning ( "<STR_LIT>" % ( asset [ <NUM_LIT:0> ] , asset [ <NUM_LIT:1> ] ) ) <EOL> if tmp_data is not None : <EOL> c . execute ( new_index_query , tmp_data ) <EOL> self . db . commit ( ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> def find_assets ( self ) : <EOL> """<STR_LIT>""" <EOL> index = [ ] <EOL> vanilla_path = os . path . join ( self . starbound_folder , "<STR_LIT>" ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> vanilla_assets = self . scan_asset_folder ( vanilla_path ) <EOL> [ index . append ( x ) for x in vanilla_assets ] <EOL> mods_path = self . mods_folder <EOL> if not os . path . isdir ( mods_path ) : <EOL> return index <EOL> for mod in os . listdir ( mods_path ) : <EOL> mod_folder = os . path . join ( mods_path , mod ) <EOL> if os . path . isdir ( mod_folder ) : <EOL> logging . info ( "<STR_LIT>" + mod ) <EOL> mod_assets = self . scan_asset_folder ( mod_folder ) <EOL> [ index . append ( x ) for x in mod_assets ] <EOL> elif mod_folder . endswith ( "<STR_LIT>" ) : <EOL> logging . info ( "<STR_LIT>" + mod ) <EOL> mod_assets = self . scan_modpak ( mod_folder ) <EOL> [ index . append ( x ) for x in mod_assets ] <EOL> return index <EOL> def scan_modpak ( self , modpak ) : <EOL> db = starbound . open_file ( modpak ) <EOL> index = [ ( x , modpak ) for x in db . get_index ( ) ] <EOL> return index <EOL> def scan_asset_folder ( self , folder ) : <EOL> pak_path = os . path . join ( folder , "<STR_LIT>" ) <EOL> if os . path . isfile ( pak_path ) : <EOL> db = starbound . open_file ( pak_path ) <EOL> index = [ ( x , pak_path ) for x in db . get_index ( ) ] <EOL> return index <EOL> else : <EOL> index = [ ] <EOL> mod_assets = None <EOL> files = os . listdir ( folder ) <EOL> found_mod_info = False <EOL> for f in files : <EOL> if f . endswith ( "<STR_LIT>" ) : <EOL> modinfo = os . path . join ( folder , f ) <EOL> try : <EOL> modinfo_data = load_asset_file ( modinfo ) <EOL> path = "<STR_LIT>" <EOL> if "<STR_LIT:path>" in modinfo_data . keys ( ) : <EOL> path = modinfo_data [ "<STR_LIT:path>" ] <EOL> mod_assets = os . path . join ( folder , path ) <EOL> found_mod_info = True <EOL> except ValueError : <EOL> folder = os . path . join ( folder , "<STR_LIT>" ) <EOL> if os . path . isdir ( folder ) : <EOL> mod_assets = folder <EOL> if mod_assets is None : <EOL> return index <EOL> elif found_mod_info and self . is_packed_file ( mod_assets ) : <EOL> pak_path = os . path . normpath ( mod_assets ) <EOL> db = starbound . open_file ( pak_path ) <EOL> for x in db . get_index ( ) : <EOL> if re . match ( ignore_assets , x ) is None : <EOL> index . append ( ( x , pak_path ) ) <EOL> return index <EOL> elif not os . path . isdir ( mod_assets ) : <EOL> return index <EOL> for root , dirs , files in os . walk ( mod_assets ) : <EOL> for f in files : <EOL> if re . match ( ignore_assets , f ) is None : <EOL> asset_folder = os . path . normpath ( mod_assets ) <EOL> asset_file = os . path . normpath ( os . path . join ( root . replace ( folder , "<STR_LIT>" ) , f ) ) <EOL> index . append ( ( asset_file , asset_folder ) ) <EOL> return index <EOL> def is_packed_file ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return os . path . isfile ( path ) <EOL> def read ( self , key , path , image = False ) : <EOL> if self . is_packed_file ( path ) : <EOL> key = key . lower ( ) <EOL> db = starbound . open_file ( path ) <EOL> if image and key in self . image_cache : <EOL> return self . image_cache [ key ] <EOL> try : <EOL> data = db . get ( key ) <EOL> except KeyError : <EOL> if image and path != self . vanilla_assets : <EOL> img = self . read ( key , self . vanilla_assets , image ) <EOL> self . image_cache [ key ] = img <EOL> return img <EOL> else : <EOL> logging . exception ( "<STR_LIT>" % ( key , path ) ) <EOL> return None <EOL> if image : <EOL> img = data <EOL> self . image_cache [ key ] = img <EOL> return img <EOL> else : <EOL> try : <EOL> asset = parse_json ( data . decode ( "<STR_LIT:utf-8>" ) , key ) <EOL> return asset <EOL> except ValueError : <EOL> logging . exception ( "<STR_LIT>" % ( key , path ) ) <EOL> return None <EOL> else : <EOL> asset_file = os . path . join ( path , key [ <NUM_LIT:1> : ] ) <EOL> try : <EOL> if image : <EOL> img = open ( asset_file , "<STR_LIT:rb>" ) . read ( ) <EOL> self . image_cache [ key ] = img <EOL> return img <EOL> else : <EOL> asset = load_asset_file ( asset_file ) <EOL> return asset <EOL> except ( FileNotFoundError , ValueError ) : <EOL> if image and path != self . vanilla_assets : <EOL> if self . is_packed_file ( self . vanilla_assets ) : <EOL> img = self . read ( key . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) , self . vanilla_assets , image ) <EOL> self . image_cache [ key ] = img <EOL> return img <EOL> else : <EOL> img = self . read ( key , self . vanilla_assets , image ) <EOL> self . image_cache [ key ] = img <EOL> return img <EOL> else : <EOL> logging . exception ( "<STR_LIT>" % ( key , path ) ) <EOL> return None <EOL> def blueprints ( self ) : <EOL> return Blueprints ( self ) <EOL> def items ( self ) : <EOL> return Items ( self ) <EOL> def species ( self ) : <EOL> return Species ( self ) <EOL> def player ( self ) : <EOL> return Player ( self ) <EOL> def monsters ( self ) : <EOL> return Monsters ( self ) <EOL> def techs ( self ) : <EOL> return Techs ( self ) <EOL> def images ( self ) : <EOL> return Images ( self ) <EOL> def frames ( self ) : <EOL> return Frames ( self ) <EOL> def get_all ( self , asset_type ) : <EOL> c = self . assets . db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , ( asset_type , ) ) <EOL> return c . fetchall ( ) <EOL> def get_categories ( self , asset_type ) : <EOL> c = self . assets . db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , ( asset_type , ) ) <EOL> return [ x [ <NUM_LIT:0> ] for x in c . fetchall ( ) ] <EOL> def filter ( self , asset_type , category , name ) : <EOL> if category == "<STR_LIT>" : <EOL> category = "<STR_LIT:%>" <EOL> name = "<STR_LIT:%>" + name + "<STR_LIT:%>" <EOL> c = self . db . cursor ( ) <EOL> q = """<STR_LIT>""" <EOL> c . execute ( q , ( asset_type , category , name , name ) ) <EOL> result = c . fetchall ( ) <EOL> return result <EOL> def get_total ( self , asset_type ) : <EOL> c = self . assets . db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , ( asset_type ) ) <EOL> return c . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def missing_icon ( self ) : <EOL> return self . read ( "<STR_LIT>" , self . vanilla_assets , image = True ) <EOL> def get_mods ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> all_assets = [ x [ <NUM_LIT:0> ] . replace ( self . starbound_folder , "<STR_LIT>" ) for x in c . fetchall ( ) ] <EOL> return [ x for x in all_assets if not x . endswith ( "<STR_LIT>" ) ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> assets = Assets ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assets . init_db ( ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> count = <NUM_LIT:0> <EOL> for i in assets . create_index ( ) : <EOL> count += <NUM_LIT:1> <EOL> print ( count ) <EOL> logging . info ( "<STR_LIT>" ) </s>
