diff src/parpg/gamemodel.py @ 12:d60f1dab8469

Fixed resource path dependencies issue that caused PARPG to crash on start. * PARPG should now run without issue (system installation not tested). * Utilized FIFE's VFS module to remove path dependencies from most PARPG modules. * The new parpg.vfs module is a singleton with a single global variable, VFS, which is a reference to the global VFS instance. Although a singleton is not ideal it should be replaced once PARPG's core code is refactored. * The parpg.vfs singleton is initialized in the parpg.applicaiton.PARPGApplication class with the absolute path to the data directory via the parpg.settings module and corresponding configuration file. * A new DataPath entry was added to the default system configuration file template under the [parpg] section to support the new parpg.vfs module. * Updated the parpg-assets subrepo to revision 3 to fix some dialog file format issues (for details see commit message for parpg-assets). * Fixed a few bugs in the parpg.dialogueparsers.YAMLDialogueParser class related to exception handling.
author M. George Hansen <technopolitica@gmail.com>
date Mon, 06 Jun 2011 15:56:14 -1000
parents 1fd2201f5c36
children 32faacaf6f28
line wrap: on
line diff
--- a/src/parpg/gamemodel.py	Tue May 31 02:46:20 2011 -0700
+++ b/src/parpg/gamemodel.py	Mon Jun 06 15:56:14 2011 -1000
@@ -22,6 +22,7 @@
 from fife import fife
 from fife.extensions.serializers.xmlobject import XMLObjectLoader 
 
+from parpg import vfs
 from gamestate import GameState
 from objects import createObject
 from objects.composed import CarryableItem, CarryableContainer
@@ -61,8 +62,7 @@
         self.map_change = False
         self.load_saver = False
         self.savegame = None
-        quests_directory = os.path.join(self.settings.system_path,
-                                        self.settings.parpg.QuestsPath)
+        quests_directory = settings.parpg.QuestsPath
         self.game_state = GameState(quests_dir=quests_directory)
         #self.game_state.quest_engine = 
         #self.game_state.quest_engine.readQuests()
@@ -78,23 +78,16 @@
         self.fife_model = engine.getModel()
 
         # set values from settings
-        maps_file = os.path.join(self.settings.system_path, 
-                                 self.settings.parpg.MapsPath,
-                                 self.settings.parpg.MapsFile)
-        self.game_state.maps_file = maps_file
-        all_agents_file = os.path.join(self.settings.system_path,
-                                       self.settings.parpg.MapsPath,
-                                       self.settings.parpg.AllAgentsFile)
-        self.all_agents_file = all_agents_file
-        objects_dir = os.path.join(self.settings.system_path,
-                                    self.settings.parpg.ObjectsPath)
-        self.objects_directory = objects_dir
-        object_db_file = os.path.join(self.objects_directory,
-                                      self.settings.parpg.ObjectDatabaseFile)
-        self.object_db_file = object_db_file
-        dialogues_dir = os.path.join(self.settings.system_path, 
-                                     self.settings.parpg.DialoguesPath)
-        self.dialogues_directory = dialogues_dir
+        maps_directory = settings.parpg.MapsPath
+        self.game_state.maps_file = '/'.join([maps_directory,
+                                              settings.parpg.MapsFile])
+        self.all_agents_file = '/'.join([maps_directory,
+                                         settings.parpg.AllAgentsFile])
+        objects_directory = self.settings.parpg.ObjectsPath
+        self.objects_directory = objects_directory
+        self.object_db_file = '/'.join([objects_directory,
+                                        settings.parpg.ObjectDatabaseFile])
+        self.dialogue_directory = settings.parpg.DialoguesPath
         self.dialogues = {}
         self.agent_import_files = {}
         self.obj_loader = XMLObjectLoader(
@@ -373,8 +366,8 @@
     
     def readMapFiles(self):
         """Read all a available map-files and store them"""
-        maps_data = file(self.game_state.maps_file)
-        self.map_files = yaml.load(maps_data)["Maps"]
+        maps_file = vfs.VFS.open(self.game_state.maps_file)
+        self.map_files = yaml.load(maps_file)["Maps"]
     
     def addAgent(self, namespace, agent):
         """Adds an agent to the agents dictionary
@@ -410,7 +403,7 @@
         #Get the agents of the map        
         map_agents_file = self.map_files[map_name].\
                             replace(".xml", "_agents.yaml")   
-        agents_data = file(map_agents_file)
+        agents_data = vfs.VFS.open(map_agents_file)
         agents = yaml.load_all(agents_data)
         for agent in agents:
             if not agent == None:
@@ -418,10 +411,10 @@
     
     def readAllAgents(self):
         """Read the agents of the all_agents_file and store them"""
-        agents_data = file(self.all_agents_file)
-        agents = yaml.load_all(agents_data)
+        agents_file = vfs.VFS.open(self.all_agents_file)
+        agents = yaml.load_all(agents_file)
         for agent in agents:
-            if not agent == None:
+            if agent is not None:
                 self.addAgent(self.ALL_AGENTS_KEY, agent)  
                 
     def getAgentsOfMap(self, map_name):
@@ -730,44 +723,40 @@
 
     def readObjectDB(self):
         """Reads the Object Information Database from a file. """
-        database_file = file(self.object_db_file, "r")
+        database_file = vfs.VFS.open(self.object_db_file)
         database = yaml.load_all(database_file)
         for object_info in database:
             self.object_db.update(object_info)
 
     def getAgentImportFiles(self):
         """Searches the agents directory for import files """
-        files = locateFiles("*.xml", self.objects_directory)
-        for xml_file in files:
-            xml_file = os.path.relpath(xml_file).replace("\\", "/")
+        filepaths = locateFiles("*.xml", self.objects_directory)
+        for filepath in filepaths:
             try:
+                xml_file = vfs.VFS.open(filepath)
                 root = ElementTree.parse(xml_file).getroot()
                 if root.tag == "object":
-                    self.agent_import_files[root.attrib["id"]] = xml_file
+                    self.agent_import_files[root.attrib["id"]] = filepath
             except SyntaxError as error:
-                assert(isinstance(error, SyntaxError))
-                logging.critical("Error parsing file {0}: "
-                                       "{1}".format(xml_file, error.msg))
-                sys.exit(1)
+                logging.error("Error parsing file {0}: {1}".format(filepath,
+                                                                   error))
     
     def getDialogues(self):
         """Searches the dialogue directory for dialogues """
-        files = locateFiles("*.yaml", self.dialogues_directory)
+        files = locateFiles("*.yaml", self.dialogue_directory)
         dialogue_parser = YamlDialogueParser()
         for dialogue_filepath in files:
-            dialogue_filepath = os.path.relpath(dialogue_filepath) \
-                                .replace("\\", "/")
             # Note Technomage 2010-11-13: the new DialogueEngine uses its own
             #     parser now, YamlDialogueParser.
 #            dialogues = yaml.load_all(file(dialogue_file, "r"))
-            with file(dialogue_filepath, 'r') as dialogue_file:
-                try:
-                    dialogue = dialogue_parser.load(dialogue_file)
-                except (DialogueFormatError,) as error:
-                    logging.error('unable to load dialogue file {0}: {1}'
-                                  .format(dialogue_filepath, error))
-                else:
-                    self.dialogues[dialogue.npc_name] = dialogue
+            dialogue_file = vfs.VFS.open(dialogue_filepath)
+            try:
+                dialogue = dialogue_parser.load(dialogue_file)
+            except DialogueFormatError as error:
+                logging.error('unable to load dialogue file {0}: {1}'
+                              .format(dialogue_filepath, error))
+            else:
+                self.dialogues[dialogue.npc_name] = dialogue
             # Note Technomage 2010-11-13: the below code is used to load
             #     multiple dialogues from a single file. Is this functionality
             #     used/necessary?