void TextureRepo::Init() { PathVect_t Paths; mFilesys.GetFileNames( Paths, "textures" ); for( PathVect_t::const_iterator i = Paths.begin(), e = Paths.end(); i != e; ++i ) { boost::filesystem::path const& Path = *i; if( Path.extension().string() != ".png" ) { continue; } AutoFile TexFile = mFilesys.Open( Path ); if( !TexFile.get() ) { continue; } PngTexture Png( *TexFile ); if( !Png.IsValid() ) { continue; } Texture* Tex = new Texture( Png.GetWidth(), Png.GetHeight(), Png.GetChannels(), Png.GetData() ); int32_t Id = AutoId( Path.generic_string() ); mElements.insert( Id, Tex ); } }
static void test_closed(AutoFile &f) { BOOST_CHECK( !f.is_open() ); BOOST_CHECK( f.eof() ); BOOST_CHECK( f.size() == 0 ); BOOST_CHECK( f.fh() == 0 ); }
static void test_open(AutoFile &f) { BOOST_CHECK( f.is_open() ); BOOST_CHECK( !f.eof() ); BOOST_CHECK( f.size() > 0 ); BOOST_CHECK( f.fh() != 0 ); }
Texture& TextureRepo::operator()( int32_t Id ) { ElementMap_t::iterator i = mElements.find( Id ); if( i != mElements.end() ) { return *( i->second ); } if( std::find( mUnavailElements.begin(), mUnavailElements.end(), Id ) != mUnavailElements.end() ) { return const_cast<Texture&>( mDefaultElement ); } do { std::string Path; if( !IdStorage::Get().GetName( Id, Path ) ) { break; } AutoFile TexFile = mFilesys.Open( Path ); if( !TexFile.get() ) { break; } std::auto_ptr<TextureBase> TexBase; if( boost::iequals( boost::filesystem::path( Path ).extension().string(), ".png" ) ) { TexBase.reset( new PngTexture( *TexFile ) ); } else if( boost::iequals( boost::filesystem::path( Path ).extension().string(), ".tga" ) ) { TexBase.reset( new TgaTexture( *TexFile ) ); } if( !TexBase.get() ) { break; } std::auto_ptr<Texture> Tex( new Texture( TexBase->GetWidth(), TexBase->GetHeight(), TexBase->GetChannels(), TexBase->GetData() ) ); if( Tex->TexId() == 0 ) { break; } mElements.insert( Id, Tex.get() ); return *Tex.release(); } while( false ); mUnavailElements.push_back( Id ); return const_cast<Texture&>( mDefaultElement ); }
void ActorFactory::Init() { PathVect_t Paths; Filesys& FSys = Filesys::Get(); FSys.GetFileNames( Paths, "actors" ); for( PathVect_t::const_iterator i = Paths.begin(), e = Paths.end(); i != e; ++i ) { boost::filesystem::path const& Path = *i; if( Path.extension().string() != ".json" ) { continue; } AutoFile JsonFile = FSys.Open( *i ); if( !JsonFile.get() ) { continue; } JsonReader Reader( *JsonFile ); if( !Reader.IsValid() ) { continue; } Json::Value Root = Reader.GetRoot(); if( !Root.isArray() ) { continue; } for( Json::Value::iterator i = Root.begin(), e = Root.end(); i != e; ++i ) { Json::Value& ActorsDesc = *i; try { if( !AddActorCreatorFromOneDesc( ActorsDesc, mActorCreators ) ) { return; } } catch( std::exception const& err ) { L1( "Exception caught while parsing %s : %s", Path.generic_string().c_str(), err.what() ); } } } }
void MapSystem::OnMapLoad( core::MapLoadEvent const& Evt ) { ClearMapElements(); PathVect_t Paths; Filesys& FSys = Filesys::Get(); FSys.GetFileNames( Paths, Evt.mMapName ); for( PathVect_t::const_iterator i = Paths.begin(), e = Paths.end(); i != e; ++i ) { boost::filesystem::path const& Path = *i; PathVect_t LevelPaths; FSys.GetFileNames( LevelPaths, Path ); if( Path.extension().string() != ".json" ) { continue; } AutoFile JsonFile = FSys.Open( *i ); if( !JsonFile.get() ) { continue; } JsonReader Reader( *JsonFile ); if( !Reader.IsValid() ) { continue; } Json::Value Root = Reader.GetRoot(); if( !Root.isArray() ) { continue; } for( Json::Value::iterator i = Root.begin(), e = Root.end(); i != e; ++i ) { Json::Value& mapElementDesc = *i; if( !AddMapElementFromOneTextureDesc( mapElementDesc ) ) { return; } } } EventServer<core::MapLoadedEvent>::Get().SendEvent( core::MapLoadedEvent() ); }
void ItemLoaderRepo::Init() { PathVect_t Paths; Filesys& FSys = Filesys::Get(); FSys.GetFileNames( Paths, "items" ); for( PathVect_t::const_iterator i = Paths.begin(), e = Paths.end(); i != e; ++i ) { boost::filesystem::path const& Path = *i; if( Path.extension().string() != ".json" ) { continue; } AutoFile JsonFile = FSys.Open( *i ); if( !JsonFile.get() ) { continue; } JsonReader Reader( *JsonFile ); if( !Reader.IsValid() ) { continue; } Json::Value Root = Reader.GetRoot(); if( !Root.isArray() ) { continue; } for( Json::Value::iterator i = Root.begin(), e = Root.end(); i != e; ++i ) { Json::Value& ItemDesc = *i; if( !LoadItemFromOneDesc( ItemDesc ) ) { return; } } } }
void ExtractPackage( const std::string& PackageName, std::string FolderName ) { if( FolderName.empty() ) { fs::path Path( PackageName ); FolderName = Path.stem().string(); } Package Pkg( AutoFile( new OsFile( PackageName ) ) ); typedef std::vector<fs::path> paths_t; paths_t IncludedFiles; Pkg.GetFileNames( IncludedFiles ); for( paths_t::const_iterator i = IncludedFiles.begin(), e = IncludedFiles.end(); i != e; ++i ) { fs::path p = ( *i ); AutoFile f = Pkg.Open( p ); p = FolderName / p; fs::create_directories( p.parent_path() ); OsFile o( p, std::ios_base::out | std::ios_base::trunc ); std::string Buffer; f->ReadAll( Buffer ); o.Write( Buffer ); } }
void MapRepo::Init() { platform::Filesys& Fs = platform::Filesys::Get(); // list of available levels/maps std::vector<boost::filesystem::path> paths; Fs.GetFileNames(paths, "map"); for ( auto path : paths ) { if ( path.filename() == "description.json") { AutoFile JsonFile = Fs.Open( path ); if ( !JsonFile.get() ) { L1("cannot open %s file\n", path.string().c_str() ); continue; } JsonReader Reader( *JsonFile ); if ( !Reader.IsValid() ) { L1("%s is not a valid JSON file", path.filename().string().c_str()); continue; } Json::Value& Root = Reader.GetRoot(); if ( !Root.isObject() ) { continue; } path.remove_filename(); std::string foldername = path.stem().string(); // query 'generated' info, this distinguishes regular maps from rogue maps // info is available in map_elements.json { // default value Root["generated"] = false; AutoFile MapElementsFile = Fs.Open("map/"+foldername+"/map_elements.json"); if (!MapElementsFile.get()) { L1("Cannot open file %s/map_elements.json", foldername.c_str()); continue; } JsonReader MapElementsReader( *MapElementsFile ); if (!MapElementsReader.IsValid()) { L1("%s/map_elements.json is not a valid JSON file", foldername.c_str()); continue; } const Json::Value& MapElementsRoot = MapElementsReader.GetRoot(); if (MapElementsRoot.isArray()) { for ( auto const& item: MapElementsRoot ) { std::string name; if (Json::GetStr(item["name"], name)) { if ("level_generated" == name) { Root["generated"] = true; break; } } } } } // index map description with real map name/foldername int32_t id = AutoId( foldername ); mElements.insert(id, new Json::Value(Root)); } } }
void Font::Load( std::string const& Path ) { AutoFile Fnt = Filesys::Get().Open( Path ); if( !Fnt.get() || !Fnt->IsValid() ) { return; } JsonReader Reader( *Fnt ); if( !Reader.IsValid() ) { return; } Json::Value& Root = Reader.GetRoot(); if( !Root.isObject() ) { return; } std::string TexName; if( !GetStr( Root["file"], TexName ) ) { return; } Texture const& Tex = TextureRepo::Get()( AutoId( boost::filesystem::path( TexName ).generic_string() ) ); if( !Tex.TexId() || !Tex.Width() || !Tex.Height() ) { return; } Json::Value& Characters = Root["characters"]; if( !Characters.isArray() || Characters.empty() ) { return; } const int Size = ( int )Characters.size(); CharDesc FirstChar, LastChar; if( !LoadChar( FirstChar, Tex, Characters[0] ) || !LoadChar( LastChar, Tex, Characters[Size - 1] ) ) { return; } Characters_t Chars( LastChar.Code - FirstChar.Code + 1 ); Chars[0] = FirstChar.Phase; Chars[Size - 1] = LastChar.Phase; mMaxHeight = 0; for( int i = 1; i < Size - 1; ++i ) { CharDesc Desc; if( !LoadChar( Desc, Tex, Characters[i] ) ) { return; } assert( Desc.Code >= FirstChar.Code && Desc.Code <= LastChar.Code ); GLfloat const Height = Desc.Phase.Size.y; if( Height > mMaxHeight ) { mMaxHeight = Height; } Chars[Desc.Code - FirstChar.Code] = Desc.Phase; } if( mMaxHeight <= std::numeric_limits<float>::epsilon() ) { return; } mFirstChar = FirstChar.Code; mLastChar = LastChar.Code; using std::swap; swap( mChars, Chars ); mTexId = Tex.TexId(); }