Package horizons :: Package util :: Package loaders :: Module actionsetloader
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.loaders.actionsetloader

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import logging 
23  import os 
24  from typing import Dict 
25   
26  import horizons.globals 
27  from horizons.constants import PATHS 
28  from horizons.util.loaders.jsondecoder import JsonDecoder 
29  from horizons.util.loaders.loader import GeneralLoader 
30 31 32 -class ActionSetLoader:
33 """The ActionSetLoader loads action sets from a directory tree. The directories loaded 34 begin with 'as_' to tell tell the loader that they are an action set. directory 35 structure is as follows: <action_set>/<action>/<rotation>/<framenumber>.png 36 for example that would be: fisher1/work/90/0.png 37 Note that all directories except for the rotation dir, all dirs have to be empty and 38 must not include additional action sets. 39 """ 40 41 log = logging.getLogger("util.loaders.actionsetloader") 42 43 action_sets = {} # type: Dict[str, Dict[str, Dict[int, Dict[str, float]]]] 44 _loaded = False 45 46 @classmethod
47 - def _find_action_sets(cls, Dir):
48 """Traverses recursively starting from dir to find action sets. 49 It is similar to os.walk, but more optimized for this use case.""" 50 for entry in os.listdir(Dir): 51 full_path = os.path.join(Dir, entry) 52 if entry.startswith("as_"): 53 cls.action_sets[entry] = GeneralLoader._load_action(full_path) 54 else: 55 if os.path.isdir(full_path) and entry != ".DS_Store": 56 cls._find_action_sets(full_path)
57 58 @classmethod
59 - def load(cls):
60 if not cls._loaded: 61 cls.log.debug("Loading action_sets...") 62 if not horizons.globals.fife.use_atlases: 63 cls._find_action_sets(PATHS.ACTION_SETS_DIRECTORY) 64 else: 65 cls.action_sets = JsonDecoder.load(PATHS.ACTION_SETS_JSON_FILE) 66 cls.log.debug("Done!") 67 cls._loaded = True
68 69 @classmethod
70 - def get_sets(cls):
71 if not cls._loaded: 72 cls.load() 73 return cls.action_sets
74 75 @classmethod
76 - def get_set(cls, action_set_name):
77 """ 78 @param action_set_name: The name of the action_set. 79 @return None if action_set is not found. 80 """ 81 if not cls._loaded: 82 cls.load() 83 return cls.action_sets.get(action_set_name)
84