Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "story.h"
00036
00037 #include <iostream>
00038 #include <fstream>
00039
00040 #include "common/wave_ex.h"
00041 #include "perf/perf.h"
00042 #include "util/token_stream.h"
00043
00044
00045 namespace story {
00046
00047
00048 static const char * s_storyFilename = "story.data";
00049
00050
00051
00052 Story::~Story(void) throw() { }
00053
00054
00056
00057
00058
00060
00061 class StoryImpl : public Story {
00062 public:
00063
00064 ~StoryImpl(void) throw() { }
00065
00066
00067 void initialize(IN const char * locale,
00068 IN const char * story_dir,
00069 IN std::istream& stream);
00070
00071
00072 const char * getUuid(void) const throw() { return m_uuid.c_str(); }
00073 const char * getTitle(void) const throw() { return m_title.c_str(); }
00074 const char * getStartingMapId(void) const throw() {
00075 return m_startingMapId.c_str(); }
00076 std::string getObjectPath(IN const char * objectType,
00077 IN const char * objectId) const;
00078 smart_ptr<i18n::Manager> getI18nManager(IN const char * path);
00079
00080 private:
00081
00082
00083 std::string m_uuid;
00084 std::string m_title;
00085 std::string m_startingMapId;
00086 std::string m_storyDir;
00087 std::string m_locale;
00088 smart_ptr<nstream::Manager> m_nstreamMgr;
00089 };
00090
00091
00092
00093 void
00094 StoryImpl::initialize
00095 (
00096 IN const char * locale,
00097 IN const char * story_dir,
00098 IN std::istream& stream
00099 )
00100 {
00101 perf::Timer timer("story::initialize");
00102 ASSERT(locale, "null");
00103 ASSERT(story_dir, "null");
00104 ASSERT(stream.good(), "bad?");
00105
00106 m_locale = locale;
00107
00108
00109 m_storyDir = story_dir;
00110
00111
00112
00113 m_nstreamMgr = nstream::getFilesystemManager(story_dir);
00114 ASSERT_THROW(m_nstreamMgr,
00115 "Failed to create a named stream manager for local directory: " <<
00116 story_dir);
00117
00118
00119 int len = m_storyDir.length();
00120 if (len < 1) {
00121 WAVE_EX(wex);
00122 wex << "Invalid story directory: " << story_dir;
00123 }
00124 if ('/' != story_dir[len - 1]) {
00125
00126 m_storyDir += "/";
00127 }
00128
00129
00130 expectToken(stream, "{");
00131
00132
00133 expectToken(stream, "uuid");
00134 getNextToken(stream, m_uuid);
00135 DPRINTF("Loading story with uuid='%s'", m_uuid.c_str());
00136
00137 expectToken(stream, "title");
00138 getNextToken(stream, m_title);
00139 DPRINTF("title = '%s'", m_title.c_str());
00140
00141 expectToken(stream, "startingMapId");
00142 getNextToken(stream, m_startingMapId);
00143 DPRINTF("Starting map has id='%s'", m_startingMapId.c_str());
00144
00145
00146 expectToken(stream, "}");
00147 }
00148
00149
00150
00152
00153
00154
00156
00157 std::string
00158 StoryImpl::getObjectPath
00159 (
00160 IN const char * objectType,
00161 IN const char * objectId
00162 )
00163 const
00164 {
00165 ASSERT(objectType, "null");
00166 ASSERT(objectId, "null");
00167
00168
00169 std::string full_path = m_storyDir;
00170 full_path += objectType;
00171 full_path += "/";
00172 full_path += objectId;
00173
00174 return full_path;
00175 }
00176
00177
00178
00179 smart_ptr<i18n::Manager>
00180 StoryImpl::getI18nManager
00181 (
00182 IN const char * path
00183 )
00184 {
00185 ASSERT(path, "null");
00186 ASSERT(m_nstreamMgr, "null");
00187
00188
00189 std::string relpath = "strings/";
00190 relpath += m_locale;
00191 relpath += "/";
00192 relpath += path;
00193
00194
00195 smart_ptr<nstream::Stream> stream =
00196 nstream::openNamedStream(m_nstreamMgr, relpath.c_str());
00197 ASSERT(stream, "null");
00198
00199 smart_ptr<i18n::Manager> mgr = i18n::Manager::create(m_locale.c_str());
00200 ASSERT(mgr, "null");
00201
00202 mgr->parseStrings(stream);
00203
00204 return mgr;
00205 }
00206
00207
00208
00210
00211
00212
00214
00215 smart_ptr<Story>
00216 Story::create
00217 (
00218 IN const char * locale,
00219 IN const char * story_dir
00220 )
00221 {
00222 ASSERT(locale, "null");
00223 ASSERT(story_dir, "null");
00224
00225
00226 std::string filename = story_dir;
00227 filename += "/";
00228 filename += s_storyFilename;
00229
00230 DPRINTF("Attempting to load story from '%s'...", filename.c_str());
00231 std::ifstream stream(filename.c_str());
00232 if (!stream.good()) {
00233 WAVE_EX(wex);
00234 wex << "Failed to load story from file '" << filename << "'.";
00235 wex << " Bad directory '" << story_dir << "'?";
00236 }
00237 expectToken(stream, "story");
00238
00239
00240 smart_ptr<StoryImpl> local = new StoryImpl;
00241 ASSERT(local, "out of memory");
00242 local->initialize(locale, story_dir, stream);
00243
00244 return local;
00245 }
00246
00247
00248
00249 };
00250