Skip to content
Browse files

Simplified (P)File classes

- ozCore
  * (P)File
    + immutable (setPath() removed)
    + stat() made private, automatically stat'd in ctor
    + mapping functionality removed for PFile (isMapped(), map(), unmap(),
      inputStream())
  * JSON: arrays iterators
  • Loading branch information...
1 parent 75425c3 commit cb35dd6d35c0069bb5436d4d7574f6718ae714f5 @ducakar committed Jan 18, 2013
Showing with 524 additions and 862 deletions.
  1. +4 −4 src/builder/BSP.cc
  2. +26 −59 src/builder/Builder.cc
  3. +1 −1 src/builder/Class.cc
  4. +26 −26 src/builder/Context.cc
  5. +1 −1 src/builder/FragPool.cc
  6. +5 −8 src/builder/Lingua.cc
  7. +5 −7 src/builder/MD2.cc
  8. +6 −7 src/builder/MD3.cc
  9. +1 −2 src/builder/OBJ.cc
  10. +1 −1 src/builder/Terra.cc
  11. +7 −10 src/builder/UI.cc
  12. +8 −5 src/client/BSP.cc
  13. +1 −1 src/client/BSP.hh
  14. +4 −6 src/client/Caelum.cc
  15. +1 −1 src/client/CinematicProxy.cc
  16. +2 −2 src/client/Client.cc
  17. +15 −10 src/client/Context.cc
  18. +2 −2 src/client/GameStage.cc
  19. +3 −3 src/client/Input.cc
  20. +6 −18 src/client/Lua.cc
  21. +9 −7 src/client/MD2.cc
  22. +3 −2 src/client/MD2.hh
  23. +7 −3 src/client/MD3.cc
  24. +2 −1 src/client/MD3.hh
  25. +2 −2 src/client/MenuStage.cc
  26. +2 −2 src/client/Profile.cc
  27. +8 −6 src/client/SMM.cc
  28. +5 −4 src/client/SMM.hh
  29. +10 −8 src/client/Shader.cc
  30. +6 −7 src/client/Terra.cc
  31. +5 −3 src/client/ui/CreditsMenu.cc
  32. +7 −4 src/client/ui/Font.cc
  33. +2 −2 src/client/ui/Font.hh
  34. +4 −3 src/client/ui/GalileoFrame.cc
  35. +2 −2 src/client/ui/MainMenu.cc
  36. +2 −2 src/client/ui/MissionMenu.cc
  37. +4 −4 src/client/ui/Mouse.cc
  38. +1 −2 src/client/ui/Style.cc
  39. +14 −17 src/common/Lingua.cc
  40. +10 −12 src/matrix/BSP.cc
  41. +14 −29 src/matrix/Liber.cc
  42. +3 −9 src/matrix/Lua.cc
  43. +3 −4 src/matrix/NamePool.cc
  44. +5 −5 src/matrix/Terra.cc
  45. +3 −9 src/nirvana/Lua.cc
  46. +1 −1 src/ozCore/Buffer.cc
  47. +1 −1 src/ozCore/Buffer.hh
  48. +61 −130 src/ozCore/File.cc
  49. +47 −39 src/ozCore/File.hh
  50. +14 −46 src/ozCore/JSON.cc
  51. +7 −26 src/ozCore/JSON.hh
  52. +50 −181 src/ozCore/PFile.cc
  53. +42 −66 src/ozCore/PFile.hh
  54. +2 −2 src/ozCore/String.cc
  55. +2 −2 src/ozCore/String.hh
  56. +1 −1 src/tests/test.cc
  57. +38 −40 src/tools/ozGettext.cc
  58. +0 −4 src/tools/ozManifest.cc
View
8 src/builder/BSP.cc
@@ -48,7 +48,7 @@ void BSP::load()
PFile bspFile( String::str( "baseq3/maps/%s.bsp", name.cstr() ) );
JSON config;
- if( !config.load( &configFile ) ) {
+ if( !config.load( configFile ) ) {
OZ_ERROR( "BSP config loading failed" );
}
@@ -83,11 +83,12 @@ void BSP::load()
OZ_ERROR( "Invalid BSP config" );
}
- if( !bspFile.map() ) {
+ if( bspFile.type() != File::REGULAR ) {
OZ_ERROR( "BSP reading failed" );
}
- InputStream is = bspFile.inputStream( Endian::LITTLE );
+ Buffer buffer = bspFile.read();
+ InputStream is = buffer.inputStream( Endian::LITTLE );
char id[4];
id[0] = is.readChar();
@@ -499,7 +500,6 @@ void BSP::load()
}
}
- bspFile.unmap();
config.clear( true );
}
View
85 src/builder/Builder.cc
@@ -106,8 +106,6 @@ void Builder::copyFiles( const char* srcDir, const char* destDir, const char* ex
foreach( file, dirList.iter() ) {
String fileName = file->name();
- file->stat();
-
if( file->type() == File::DIRECTORY ) {
if( recurse ) {
copyFiles( srcDir + ( "/" + file->name() ), destDir + ( "/" + file->name() ), ext, true );
@@ -118,19 +116,11 @@ void Builder::copyFiles( const char* srcDir, const char* destDir, const char* ex
{
Log::print( "Copying '%s' ...", fileName.cstr() );
- if( !file->map() ) {
- OZ_ERROR( "Failed to map '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
File destFile( sDestDir + fileName );
-
- if( !destFile.write( is.begin(), is.capacity() ) ) {
+ if( !destFile.write( file->read() ) ) {
OZ_ERROR( "Failed to write '%s'", file->path().cstr() );
}
- file->unmap();
-
Log::printEnd( " OK" );
continue;
}
@@ -231,8 +221,6 @@ void Builder::buildBSPTextures()
DArray<PFile> dirList = dir.ls();
foreach( subDir, dirList.iter() ) {
- subDir->stat();
-
if( subDir->type() != File::DIRECTORY ) {
continue;
}
@@ -316,28 +304,24 @@ void Builder::buildBSPTextures()
DArray<PFile> texList = subDir.ls();
foreach( file, texList.iter() ) {
+ if( file->type() != File::REGULAR ) {
+ continue;
+ }
+
String name = file->name();
String path = file->path();
if( name.beginsWith( "COPYING" ) || name.beginsWith( "README" ) ) {
Log::print( "Copying '%s' ...", path.cstr() );
- if( !file->map() ) {
- OZ_ERROR( "Failed to read '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
- File destFile( String::str( "tex/%s/%s", subDir.name().cstr(), name.cstr() ) );
-
File::mkdir( "tex" );
File::mkdir( "tex/" + subDir.name() );
- if( !destFile.write( is.begin(), is.capacity() ) ) {
+ File destFile( String::str( "tex/%s/%s", subDir.name().cstr(), name.cstr() ) );
+ if( !destFile.write( file->read() ) ) {
OZ_ERROR( "Failed to write '%s'", destFile.path().cstr() );
}
- file->unmap();
-
Log::printEnd( " OK" );
continue;
}
@@ -488,34 +472,30 @@ void Builder::buildModels()
DArray<PFile> fileList = dir->ls();
foreach( file, fileList.iter() ) {
+ if( file->type() != File::REGULAR ) {
+ continue;
+ }
+
String name = file->name();
String path = file->path();
if( name.beginsWith( "COPYING" ) || name.beginsWith( "README" ) ) {
Log::print( "Copying '%s' ...", path.cstr() );
- if( !file->map() ) {
- OZ_ERROR( "Failed to read '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
File destFile( path );
-
- if( !destFile.write( is.begin(), is.length() ) ) {
+ if( !destFile.write( file->read() ) ) {
OZ_ERROR( "Failed to write '%s'", destFile.path().cstr() );
}
- file->unmap();
-
Log::printEnd( " OK" );
continue;
}
}
- if( PFile( dir->path() + "/data.obj" ).stat() ) {
+ if( PFile( dir->path() + "/data.obj" ).type() != File::MISSING ) {
obj.build( dir->path() );
}
- else if( PFile( dir->path() + "/tris.md2" ).stat() ) {
+ else if( PFile( dir->path() + "/tris.md2" ).type() != File::MISSING ) {
md2.build( dir->path() );
}
else {
@@ -542,15 +522,17 @@ void Builder::copySounds()
DArray<PFile> dirList = dir.ls();
foreach( subDir, dirList.iter() ) {
- subDir->stat();
-
if( subDir->type() != File::DIRECTORY ) {
continue;
}
DArray<PFile> sndList = subDir->ls();
foreach( file, sndList.iter() ) {
+ if( file->type() != File::REGULAR ) {
+ continue;
+ }
+
String name = file->name();
String path = file->path();
@@ -576,20 +558,11 @@ void Builder::copySounds()
File::mkdir( "snd" );
File::mkdir( "snd/" + subDir->name() );
- if( !file->map() ) {
- OZ_ERROR( "Failed to copy '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
-
File destFile( file->path() );
-
- if( !destFile.write( is.begin(), is.capacity() ) ) {
+ if( !destFile.write( file->read() ) ) {
OZ_ERROR( "Failed to write '%s'", destFile.path().cstr() );
}
- file->unmap();
-
Log::printEnd( " OK" );
}
}
@@ -599,28 +572,24 @@ void Builder::copySounds()
DArray<PFile> texList = subDir.ls();
foreach( file, texList.iter() ) {
+ if( file->type() != File::REGULAR ) {
+ continue;
+ }
+
String name = file->name();
String path = file->path();
if( name.beginsWith( "COPYING" ) || name.beginsWith( "README" ) ) {
Log::print( "Copying '%s' ...", path.cstr() );
- if( !file->map() ) {
- OZ_ERROR( "Failed to read '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
- File destFile( path );
-
File::mkdir( "snd" );
File::mkdir( "snd/" + subDir.name() );
- if( !destFile.write( is.begin(), is.capacity() ) ) {
+ File destFile( path );
+ if( !destFile.write( file->read() ) ) {
OZ_ERROR( "Failed to write '%s'", destFile.path().cstr() );
}
- file->unmap();
-
Log::printEnd( " OK" );
continue;
}
@@ -683,7 +652,7 @@ void Builder::buildMissions()
PFile srcFile( mission->path() + "/description.png" );
File outFile( mission->path() + "/description.ozImage" );
- if( !srcFile.stat() ) {
+ if( srcFile.type() == File::MISSING ) {
continue;
}
@@ -727,9 +696,7 @@ void Builder::packArchive( const char* name, bool useCompression, bool use7zip )
OZ_ERROR( use7zip ? "Packing 7zip archive failed" : "Packing ZIP archive failed" );
}
- archive.stat();
int size = archive.size();
-
if( size >= 0 ) {
Log::println();
Log::println( "Archive size: %.2f MiB = %.2f MB",
View
2 src/builder/Class.cc
@@ -767,7 +767,7 @@ void Class::build( BufferStream* os, const char* className )
{
PFile configFile( String::str( "class/%s.json", className ) );
- if( !config.load( &configFile ) ) {
+ if( !config.load( configFile ) ) {
OZ_ERROR( "Failed to load '%s'", configFile.path().cstr() );
}
View
52 src/builder/Context.cc
@@ -312,7 +312,7 @@ Context::Image Context::loadImage( const char* path, int forceFormat )
PFile file( path );
String realPath = file.realDir() + "/" + file.path();
- if( !file.stat() ) {
+ if( file.type() == File::MISSING ) {
OZ_ERROR( "File '%s' does not exits", realPath.cstr() );
}
@@ -440,43 +440,43 @@ void Context::loadTextures( Texture* diffuseTex, Texture* masksTex, Texture* nor
PFile diffuse, masks, specular, emission, normals;
for( int i = 0; i < aLength( IMAGE_EXTENSIONS ); ++i ) {
- if( diffuse.path().isEmpty() || !diffuse.stat() ) {
- diffuse.setPath( diffuseBasePath + IMAGE_EXTENSIONS[i] );
+ if( diffuse.path().isEmpty() || diffuse.type() == File::MISSING ) {
+ diffuse = PFile( diffuseBasePath + IMAGE_EXTENSIONS[i] );
}
- if( !diffuse.stat() ) {
- diffuse.setPath( diffuse1BasePath + IMAGE_EXTENSIONS[i] );
+ if( diffuse.type() == File::MISSING ) {
+ diffuse = PFile( diffuse1BasePath + IMAGE_EXTENSIONS[i] );
}
- if( masks.path().isEmpty() || !masks.stat() ) {
- masks.setPath( masksBasePath + IMAGE_EXTENSIONS[i] );
+ if( masks.path().isEmpty() || masks.type() == File::MISSING ) {
+ masks = PFile( masksBasePath + IMAGE_EXTENSIONS[i] );
}
- if( specular.path().isEmpty() || !specular.stat() ) {
- specular.setPath( specularBasePath + IMAGE_EXTENSIONS[i] );
+ if( specular.path().isEmpty() || specular.type() == File::MISSING ) {
+ specular = PFile( specularBasePath + IMAGE_EXTENSIONS[i] );
}
- if( !specular.stat() ) {
- specular.setPath( specular1BasePath + IMAGE_EXTENSIONS[i] );
+ if( specular.type() == File::MISSING ) {
+ specular = PFile( specular1BasePath + IMAGE_EXTENSIONS[i] );
}
- if( emission.path().isEmpty() || !emission.stat() ) {
- emission.setPath( emissionBasePath + IMAGE_EXTENSIONS[i] );
+ if( emission.path().isEmpty() || emission.type() == File::MISSING ) {
+ emission = PFile( emissionBasePath + IMAGE_EXTENSIONS[i] );
}
- if( normals.path().isEmpty() || !normals.stat() ) {
- normals.setPath( normalsBasePath + IMAGE_EXTENSIONS[i] );
+ if( normals.path().isEmpty() || normals.type() == File::MISSING ) {
+ normals = PFile( normalsBasePath + IMAGE_EXTENSIONS[i] );
}
- if( !normals.stat() ) {
- normals.setPath( normals1BasePath + IMAGE_EXTENSIONS[i] );
+ if( normals.type() == File::MISSING ) {
+ normals = PFile( normals1BasePath + IMAGE_EXTENSIONS[i] );
}
- if( !normals.stat() ) {
- normals.setPath( normals2BasePath + IMAGE_EXTENSIONS[i] );
+ if( normals.type() == File::MISSING ) {
+ normals = PFile( normals2BasePath + IMAGE_EXTENSIONS[i] );
}
- if( !normals.stat() ) {
- normals.setPath( normals3BasePath + IMAGE_EXTENSIONS[i] );
+ if( normals.type() == File::MISSING ) {
+ normals = PFile( normals3BasePath + IMAGE_EXTENSIONS[i] );
}
}
- if( diffuse.stat() ) {
+ if( diffuse.type() != File::MISSING ) {
Image diffuseImage = loadImage( diffuse.path() );
*diffuseTex = Texture( &diffuseImage, wrap, magFilter, minFilter );
@@ -486,7 +486,7 @@ void Context::loadTextures( Texture* diffuseTex, Texture* masksTex, Texture* nor
OZ_ERROR( "Missing texture '%s' (.png, .jpeg, .jpg and .tga checked)", basePath.cstr() );
}
- if( masks.stat() ) {
+ if( masks.type() != File::MISSING ) {
Image masksImage = loadImage( masks.path(), GL_RGB );
*masksTex = Texture( &masksImage, wrap, magFilter, minFilter );
@@ -497,10 +497,10 @@ void Context::loadTextures( Texture* diffuseTex, Texture* masksTex, Texture* nor
specularImage.dib = nullptr;
emissionImage.dib = nullptr;
- if( specular.stat() ) {
+ if( specular.type() != File::MISSING ) {
specularImage = loadImage( specular.path(), GL_RGB );
}
- if( emission.stat() ) {
+ if( emission.type() != File::MISSING ) {
emissionImage = loadImage( emission.path(), GL_LUMINANCE );
}
@@ -565,7 +565,7 @@ void Context::loadTextures( Texture* diffuseTex, Texture* masksTex, Texture* nor
}
}
- if( bumpmap && normals.stat() ) {
+ if( bumpmap && normals.type() != File::MISSING ) {
Image normalsImage = loadImage( normals.path() );
*normalsTex = Texture( &normalsImage, wrap, magFilter, minFilter );
View
2 src/builder/FragPool.cc
@@ -41,7 +41,7 @@ void FragPool::build( BufferStream* os, const char* className )
PFile configFile( String::str( "frag/%s.json", className ) );
JSON config;
- if( !config.load( &configFile ) ) {
+ if( !config.load( configFile ) ) {
OZ_ERROR( "Failed to load '%s'", configFile.path().cstr() );
}
View
13 src/builder/Lingua.cc
@@ -34,11 +34,12 @@ void Lingua::buildCatalogue( const char* directory, const char* catalogue )
PFile srcFile( String::str( "%s/%s.po", directory, catalogue ) );
File outFile( String::str( "%s/%s.ozCat", directory, catalogue ) );
- if( !srcFile.map() ) {
- OZ_ERROR( "Cannot map catalogue source file '%s'", srcFile.path().cstr() );
+ if( srcFile.type() != File::REGULAR ) {
+ OZ_ERROR( "Cannot read catalogue source file '%s'", srcFile.path().cstr() );
}
- InputStream is = srcFile.inputStream();
+ Buffer buffer = srcFile.read();
+ InputStream is = buffer.inputStream();
List<String> messages;
@@ -133,8 +134,6 @@ void Lingua::buildCatalogue( const char* directory, const char* catalogue )
}
}
- srcFile.unmap();
-
if( !lastOriginal.isEmpty() && !lastOriginal.equals( lastTranslation ) ) {
messages.add( lastOriginal );
messages.add( lastTranslation );
@@ -170,8 +169,6 @@ void Lingua::build()
}
foreach( langDir, languages.iter() ) {
- langDir->stat();
-
if( langDir->type() != File::DIRECTORY ) {
continue;
}
@@ -195,7 +192,7 @@ void Lingua::build()
DArray<PFile> missions = missionsDir.ls();
foreach( mission, missions.citer() ) {
- linguaDir.setPath( mission->path() + "/lingua" );
+ linguaDir = PFile( mission->path() + "/lingua" );
languages = linguaDir.ls();
foreach( catalogue, languages.citer() ) {
View
12 src/builder/MD2.cc
@@ -211,14 +211,14 @@ void MD2::build( const char* path )
Log::println( "Prebuilding MD2 model '%s' {", path );
Log::indent();
- JSON config;
- config.load( &configFile );
+ JSON config( configFile );
- if( !modelFile.map() ) {
- OZ_ERROR( "MD2 reading failed" );
+ if( modelFile.type() != File::REGULAR ) {
+ OZ_ERROR( "MD2 file read failed" );
}
- InputStream is = modelFile.inputStream( Endian::LITTLE );
+ Buffer buffer = modelFile.read();
+ InputStream is = buffer.inputStream( Endian::LITTLE );
MD2Header header;
@@ -339,8 +339,6 @@ void MD2::build( const char* path )
triangles[i].texCoords[2] = is.readShort();
}
- modelFile.unmap();
-
compiler.beginMesh();
compiler.enable( Compiler::UNIQUE );
compiler.enable( Compiler::CLOCKWISE );
View
13 src/builder/MD3.cc
@@ -58,11 +58,13 @@ void MD3::buildMesh( const char* name, int frame )
Log::print( "Mesh '%s' ...", name );
PFile file( String::str( "%s/%s.md3", sPath.cstr(), name ) );
- if( !file.map() ) {
- OZ_ERROR( "Cannot mmap MD3 model part file '%s'", file.path().cstr() );
+
+ if( file.type() != File::REGULAR ) {
+ OZ_ERROR( "Cannot read MD3 model part file '%s'", file.path().cstr() );
}
- InputStream is = file.inputStream( Endian::LITTLE );
+ Buffer buffer = file.read();
+ InputStream is = buffer.inputStream( Endian::LITTLE );
MD3Header header;
@@ -265,17 +267,14 @@ void MD3::buildMesh( const char* name, int frame )
indexBase += surface.nVertices;
}
- file.unmap();
-
Log::printEnd( " OK" );
}
void MD3::load()
{
PFile configFile( sPath + "/config.json" );
- JSON config;
- config.load( &configFile );
+ JSON config( configFile );
scale = config["scale"].get( 0.04f );
skin = config["skin"].get( "" );
View
3 src/builder/OBJ.cc
@@ -279,8 +279,7 @@ void OBJ::load()
int currentMaterial = 0;
- JSON config;
- config.load( &configFile );
+ JSON config( configFile );
float scale = config["scale"].get( 1.0f );
Vec3 translation = config["translate"].get( Vec3::ZERO );
View
2 src/builder/Terra.cc
@@ -40,7 +40,7 @@ void Terra::load()
PFile imageFile( "terra/" + name + ".png" );
JSON config;
- if( !config.load( &configFile ) ) {
+ if( !config.load( configFile ) ) {
OZ_ERROR( "Failed to loa terra configuration '%s'", configFile.path().cstr() );
}
View
17 src/builder/UI.cc
@@ -54,7 +54,7 @@ const char* const UI::ICON_NAMES[] = {
void UI::buildCursors()
{
- if( !PFile( "ui/cur" ).stat() ) {
+ if( PFile( "ui/cur" ).type() == File::MISSING ) {
return;
}
@@ -109,7 +109,7 @@ void UI::buildCursors()
void UI::buildIcons()
{
- if( !PFile( "ui/icon" ).stat() ) {
+ if( PFile( "ui/icon" ).type() == File::MISSING ) {
return;
}
@@ -150,16 +150,13 @@ void UI::copyScheme()
PFile srcFile( "ui/style.json" );
File outFile( "ui/style.json" );
- Buffer buffer = srcFile.read();
- if( buffer.isEmpty() ) {
- return;
- }
+ if( srcFile.type() == File::REGULAR ) {
+ Log::print( "Copying UI colour scheme '%s' ...", srcFile.path().cstr() );
- Log::print( "Copying UI colour scheme '%s' ...", srcFile.path().cstr() );
+ outFile.write( srcFile.read() );
- outFile.write( buffer.begin(), buffer.length() );
-
- Log::printEnd( " OK" );
+ Log::printEnd( " OK" );
+ }
}
}
View
13 src/client/BSP.cc
@@ -34,7 +34,7 @@ namespace client
{
BSP::BSP( const matrix::BSP* bsp_ ) :
- bsp( bsp_ ), file( "bsp/" + bsp->name + ".ozcBSP" ), isPreloaded( false ), isLoaded( false )
+ bsp( bsp_ ), isPreloaded( false ), isLoaded( false )
{
foreach( model, bsp->models.citer() ) {
context.requestSMM( *model );
@@ -54,16 +54,19 @@ BSP::~BSP()
void BSP::preload()
{
- if( !file.map() ) {
- OZ_ERROR( "BSP file '%s' mmap failed", file.path().cstr() );
+ PFile file( "bsp/" + bsp->name + ".ozcBSP" );
+
+ buffer = file.read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "BSP file '%s' read failed", file.path().cstr() );
}
isPreloaded = true;
}
void BSP::load()
{
- InputStream istream = file.inputStream();
+ InputStream istream = buffer.inputStream();
waterFogColour = istream.readVec4();
lavaFogColour = istream.readVec4();
@@ -72,7 +75,7 @@ void BSP::load()
hard_assert( !istream.isAvailable() );
- file.setPath( "" );
+ buffer.deallocate();
isLoaded = true;
}
View
2 src/client/BSP.hh
@@ -36,7 +36,7 @@ class BSP
const matrix::BSP* bsp;
Mesh mesh;
- PFile file;
+ Buffer buffer;
public:
View
10 src/client/Caelum.cc
@@ -167,12 +167,12 @@ void Caelum::load()
Math::sin( orbis.caelum.heading ),
0.0f );
- PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Caelum file '%s' mmap failed", path.cstr() );
+ Buffer buffer = PFile( path ).read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Caelum file '%s' read failed", path.cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
int vboSize = MAX_STARS * 4 * int( sizeof( float[3] ) );
int iboSize = MAX_STARS * 6 * int( sizeof( ushort ) );
@@ -196,8 +196,6 @@ void Caelum::load()
nightColour = Vec4( NIGHT_COLOUR );
nightLuminance = ( nightColour.x + nightColour.y + nightColour.z ) / 3.0f;
- file.unmap();
-
OZ_GL_CHECK_ERROR();
update();
View
2 src/client/CinematicProxy.cc
@@ -55,7 +55,7 @@ void CinematicProxy::executeSequence( const char* path, const Lingua* missionLin
JSON sequence;
PFile file( path );
- if( !sequence.load( &file ) ) {
+ if( !sequence.load( file ) ) {
OZ_ERROR( "Failed to load sequence from '%s'", file.path().cstr() );
}
View
4 src/client/Client.cc
@@ -246,7 +246,7 @@ int Client::init( int argc, char** argv )
File::rm( configDir + "/client.rc" );
File configFile( configDir + "/client.json" );
- if( config.load( &configFile ) ) {
+ if( config.load( configFile ) ) {
Log::printEnd( "Configuration read from '%s'", configFile.path().cstr() );
if( String::equals( config["_version"].get( "" ), OZ_VERSION ) ) {
@@ -526,7 +526,7 @@ void Client::shutdown()
Log::print( "Writing configuration to '%s' ...", configFile.path().cstr() );
- config.save( &configFile );
+ config.save( configFile );
Log::printEnd( " OK" );
}
View
25 src/client/Context.cc
@@ -318,11 +318,13 @@ uint Context::readTextureLayer( InputStream* istream )
uint Context::loadTextureLayer( const char* path )
{
PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Texture file '%s' mmap failed", path );
+ Buffer buffer = file.read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Texture file '%s' mmap failed", file.path().cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
return readTextureLayer( &is );
}
@@ -343,11 +345,13 @@ Texture Context::readTexture( InputStream* istream )
Texture Context::loadTexture( const char* path )
{
PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Texture file '%s' mmap failed", path );
+ Buffer buffer = file.read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Texture file '%s' read failed", path );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
return readTexture( &is );
}
@@ -399,11 +403,13 @@ uint Context::requestSound( int id )
const String& path = liber.sounds[id].path;
PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Sound file '%s' mmap failed", path.cstr() );
+ Buffer buffer = file.read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Sound file '%s' read failed", path.cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
uint length;
ubyte* data;
@@ -432,7 +438,6 @@ uint Context::requestSound( int id )
alBufferData( resource.id, format, data, int( length ), audioSpec.freq );
SDL_FreeWAV( data );
- file.unmap();
OZ_AL_CHECK_ERROR();
View
4 src/client/GameStage.cc
@@ -264,15 +264,15 @@ bool GameStage::update()
if( input.keys[Input::KEY_QUICKLOAD] && !input.oldKeys[Input::KEY_QUICKLOAD] ) {
File quicksaveFile( QUICKSAVE_FILE );
- if( quicksaveFile.stat() ) {
+ if( quicksaveFile.type() == File::REGULAR ) {
stateFile = QUICKSAVE_FILE;
reload();
}
}
if( input.keys[Input::KEY_AUTOLOAD] && !input.oldKeys[Input::KEY_AUTOLOAD] ) {
File autosaveFile( AUTOSAVE_FILE );
- if( autosaveFile.stat() ) {
+ if( autosaveFile.type() == File::REGULAR ) {
stateFile = AUTOSAVE_FILE;
reload();
}
View
6 src/client/Input.cc
@@ -469,7 +469,7 @@ void Input::update()
mouseX = +float( dx );
mouseY = -float( dy );
-# if SDL_MAJOR_VERSION < 2 && !defined( _WIN32 )
+# ifndef _WIN32
if( window.hasGrab ) {
// Compensate lack of mouse acceleration when receiving raw (non-accelerated) mouse input. This
// code is not based on actual code from X.Org, but experimentally tuned to match default X
@@ -547,7 +547,7 @@ void Input::init()
Log::print( "Initialising Input from '%s' ...", configFile.path().cstr() );
JSON inputConfig;
- configExists = inputConfig.load( &configFile );
+ configExists = inputConfig.load( configFile );
if( !String::equals( inputConfig["_version"].get( "" ), OZ_VERSION ) ) {
configExists = false;
@@ -663,7 +663,7 @@ void Input::destroy()
keyboardConfig.add( "sensitivity.y", keySensY );
keyMapConfig = keyMapToJSON();
- if( !inputConfig.save( &configFile ) ) {
+ if( !inputConfig.save( configFile ) ) {
OZ_ERROR( "Failed to write '%s'", configFile.path().cstr() );
}
View
24 src/client/Lua.cc
@@ -85,21 +85,15 @@ void Lua::create( const char* mission_ )
}
foreach( file, files.iter() ) {
- if( !file->hasExtension( "lua" ) ) {
+ if( file->type() != File::REGULAR || !file->hasExtension( "lua" ) ) {
continue;
}
- if( !file->map() ) {
- OZ_ERROR( "Failed to read mission script '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
+ Buffer buffer = file->read();
- if( l_dobuffer( is.begin(), is.capacity(), file->path() ) != 0 ) {
+ if( !buffer.isEmpty() && l_dobuffer( buffer.begin(), buffer.length(), file->path() ) != 0 ) {
OZ_ERROR( "Client Lua script error in %s", file->path().cstr() );
}
-
- file->unmap();
}
staticCall( "onCreate" );
@@ -135,21 +129,15 @@ void Lua::read( InputStream* istream )
}
foreach( file, files.iter() ) {
- if( !file->hasExtension( "lua" ) ) {
+ if( file->type() != File::REGULAR || !file->hasExtension( "lua" ) ) {
continue;
}
- if( !file->map() ) {
- OZ_ERROR( "Failed to read mission script '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
+ Buffer buffer = file->read();
- if( l_dobuffer( is.begin(), is.capacity(), file->path() ) != 0 ) {
+ if( !buffer.isEmpty() && l_dobuffer( buffer.begin(), buffer.length(), file->path() ) != 0 ) {
OZ_ERROR( "Client Lua script error in %s", file->path().cstr() );
}
-
- file->unmap();
}
const char* name = istream->readString();
View
16 src/client/MD2.cc
@@ -221,30 +221,32 @@ void MD2::AnimState::advance()
hard_assert( 0.0f <= frameRatio && frameRatio < 1.0f );
}
-MD2::MD2( int id ) :
- file( liber.models[id].path ), isPreloaded( false ), isLoaded( false )
+MD2::MD2( int id_ ) :
+ id( id_ ), isPreloaded( false ), isLoaded( false )
{}
void MD2::preload()
{
- if( !file.map() ) {
- OZ_ERROR( "MD2 model file '%s' mmap failed", file.path().cstr() );
+ PFile file( liber.models[id].path );
+
+ buffer = file.read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "MD2 model file '%s' read failed", file.path().cstr() );
}
isPreloaded = true;
}
void MD2::load()
{
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
weaponTransf = is.readMat44();
-
mesh.load( &is, shader.hasVertexTexture ? GL_STATIC_DRAW : GL_STREAM_DRAW );
hard_assert( !is.isAvailable() );
- file.setPath( "" );
+ buffer.deallocate();
isLoaded = true;
}
View
5 src/client/MD2.hh
@@ -104,8 +104,9 @@ class MD2
private:
- Mesh mesh;
- PFile file;
+ int id;
+ Buffer buffer;
+ Mesh mesh;
public:
View
10 src/client/MD3.cc
@@ -32,13 +32,16 @@ namespace oz
namespace client
{
-MD3::MD3( int id ) :
- file( liber.models[id].path ), isPreloaded( false ), isLoaded( false )
+MD3::MD3( int id_ ) :
+ id( id_ ), isPreloaded( false ), isLoaded( false )
{}
void MD3::preload()
{
- if( !file.map() ) {
+ PFile file( liber.models[id].path );
+
+ buffer = file.read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "MD3 model file '%s' mmap failed", file.path().cstr() );
}
@@ -47,6 +50,7 @@ void MD3::preload()
void MD3::load()
{
+ buffer.deallocate();
isLoaded = true;
}
View
3 src/client/MD3.hh
@@ -127,7 +127,8 @@ class MD3
// AnimInfo torsoAnimList[TORSO_ANIM_MAX];
// Joint joints[MAX_FRAMES][JOINTS_MAX];
- PFile file;
+ int id;
+ Buffer buffer;
public:
View
4 src/client/MenuStage.cc
@@ -66,10 +66,10 @@ void MenuStage::load()
File autosaveFile( GameStage::AUTOSAVE_FILE );
File quicksaveFile( GameStage::QUICKSAVE_FILE );
- if( autosaveFile.stat() ) {
+ if( autosaveFile.type() == File::REGULAR ) {
showAutosaved = true;
}
- if( quicksaveFile.stat() ) {
+ if( quicksaveFile.type() == File::REGULAR ) {
showQuicksaved = true;
}
View
4 src/client/Profile.cc
@@ -42,7 +42,7 @@ void Profile::init()
File profileFile( config["dir.config"].asString() + "/profile.json" );
JSON profileConfig;
- bool configExists = profileConfig.load( &profileFile );
+ bool configExists = profileConfig.load( profileFile );
if( profileConfig.isNull() ) {
profileConfig.setObject();
@@ -136,7 +136,7 @@ void Profile::init()
}
if( !configExists ) {
- profileConfig.save( &profileFile );
+ profileConfig.save( profileFile );
}
profileConfig.clear( true );
View
14 src/client/SMM.cc
@@ -36,13 +36,16 @@ namespace oz
namespace client
{
-SMM::SMM( int id ) :
- file( liber.models[id].path ), isPreloaded( false ), isLoaded( false )
+SMM::SMM( int id_ ) :
+ id( id_ ), isPreloaded( false ), isLoaded( false )
{}
void SMM::preload()
{
- if( !file.map() ) {
+ PFile file( liber.models[id].path );
+
+ buffer = file.read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "SMM file '%s' mmap failed", file.path().cstr() );
}
@@ -51,13 +54,12 @@ void SMM::preload()
void SMM::load()
{
- InputStream is = file.inputStream();
-
+ InputStream is = buffer.inputStream();
mesh.load( &is, GL_STATIC_DRAW );
hard_assert( !is.isAvailable() );
- file.setPath( "" );
+ buffer.deallocate();
isLoaded = true;
}
View
9 src/client/SMM.hh
@@ -38,13 +38,14 @@ class SMM
{
private:
- Mesh mesh;
- PFile file;
+ int id;
+ Buffer buffer;
+ Mesh mesh;
public:
- bool isPreloaded;
- bool isLoaded;
+ bool isPreloaded;
+ bool isLoaded;
explicit SMM( int id );
View
18 src/client/Shader.cc
@@ -124,20 +124,20 @@ Shader::Light::Light( const Point& pos_, const Vec4& diffuse_ ) :
void Shader::compileShader( uint id, const char* path, const char** sources, int* lengths ) const
{
PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Shader source '%s' mmap failed", path );
+
+ Buffer buffer = file.read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Shader source '%s' read failed", path );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
sources[2] = is.begin();
lengths[2] = is.capacity();
glShaderSource( id, 3, sources, lengths );
glCompileShader( id );
- file.unmap();
-
int result;
glGetShaderiv( id, GL_COMPILE_STATUS, &result );
@@ -168,7 +168,7 @@ void Shader::loadProgram( int id )
PFile configFile( "glsl/" + name + ".json" );
JSON programConfig;
- if( !programConfig.load( &configFile ) ) {
+ if( !programConfig.load( configFile ) ) {
OZ_ERROR( "Failed to read shader program configuration '%s'", configFile.path().cstr() );
}
@@ -376,9 +376,11 @@ void Shader::init()
sources[0] = defines;
lengths[0] = defines.length();
- Buffer buffer = PFile( "glsl/header.glsl" ).read();
+ PFile file( "glsl/header.glsl" );
+ Buffer buffer = file.read();
+
if( buffer.isEmpty() ) {
- OZ_ERROR( "header.glsl reading failed" );
+ OZ_ERROR( "'%s' read failed", file.path().cstr() );
}
sources[1] = buffer.begin();
View
13 src/client/Terra.cc
@@ -160,14 +160,15 @@ void Terra::load()
id = orbis.terra.id;
const String& name = liber.terrae[id].name;
- String path = "terra/" + name + ".ozcTerra";
- PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Terra file mmap failed" );
+ PFile file( "terra/" + name + ".ozcTerra" );
+ Buffer buffer = file.read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Terra file '%s' read failed", file.path().cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
waterTexId = context.readTextureLayer( &is );
detailTexId = context.readTextureLayer( &is );
@@ -228,8 +229,6 @@ void Terra::load()
liquidFogColour = is.readVec4();
hard_assert( !is.isAvailable() );
-
- file.unmap();
}
void Terra::unload()
View
8 src/client/ui/CreditsMenu.cc
@@ -157,8 +157,10 @@ CreditsMenu::CreditsMenu() :
lines.add( "Davorin Učakar" );
foreach( creditsFile, creditsFiles.iter() ) {
- if( !creditsFile->map() ) {
- OZ_ERROR( "Failed to map '%s'", creditsFile->path().cstr() );
+ Buffer buffer = creditsFile->read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Failed to read '%s'", creditsFile->path().cstr() );
}
lines.add( "" );
@@ -172,7 +174,7 @@ CreditsMenu::CreditsMenu() :
lines.add( "" );
String contents = "";
- InputStream is = creditsFile->inputStream();
+ InputStream is = buffer.inputStream();
while( is.isAvailable() ) {
contents += is.readLine() + "\n";
View
11 src/client/ui/Font.cc
@@ -69,13 +69,15 @@ void Font::draw( const char* s, uint texId_, int* width, int* height ) const
void Font::init( const char* name, int height_ )
{
height = height_;
- file.setPath( String::str( "ui/font/%s.ttf", name ) );
- if( !file.map() ) {
+ PFile file( String::str( "ui/font/%s.ttf", name ) );
+
+ buffer = file.read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "Failed to read font file '%s'", file.path().cstr() );
}
- InputStream istream = file.inputStream();
+ InputStream istream = buffer.inputStream();
handle = TTF_OpenFontRW( SDL_RWFromConstMem( istream.begin(), istream.capacity() ), true, height );
if( handle == nullptr ) {
@@ -88,7 +90,8 @@ void Font::destroy()
if( handle != nullptr ) {
TTF_CloseFont( static_cast<TTF_Font*>( handle ) );
}
- file.unmap();
+
+ buffer.deallocate();
}
}
View
4 src/client/ui/Font.hh
@@ -50,8 +50,8 @@ class Font
private:
- PFile file;
- void* handle;
+ Buffer buffer;
+ void* handle;
public:
View
7 src/client/ui/GalileoFrame.cc
@@ -42,14 +42,15 @@ namespace ui
uint GalileoFrame::loadTexture( const char* path ) const
{
PFile file( path );
- if( !file.map() ) {
+
+ Buffer buffer = file.read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "Failed reading galileo texture '%s'", path );
}
- InputStream istream = file.inputStream();
+ InputStream istream = buffer.inputStream();
uint texId = context.readTextureLayer( &istream );
- file.unmap();
return texId;
}
View
4 src/client/ui/MainMenu.cc
@@ -138,14 +138,14 @@ void MainMenu::onUpdate()
File autosaveFile( GameStage::AUTOSAVE_FILE );
File quicksaveFile( GameStage::QUICKSAVE_FILE );
- if( autosaveFile.stat() ) {
+ if( autosaveFile.type() == File::REGULAR ) {
OZ_MAIN_CALL( this, {
Button* continueButton = new Button( OZ_GETTEXT( "Continue" ), loadAutosaved, 200, 30 );
_this->add( continueButton, -20, 360 );
} )
}
- if( quicksaveFile.stat() ) {
+ if( quicksaveFile.type() == File::REGULAR ) {
OZ_MAIN_CALL( this, {
Button* quickLoadButton = new Button( OZ_GETTEXT( "Quickload" ), loadQuicksaved, 200, 30 );
_this->add( quickLoadButton, -20, 320 );
View
4 src/client/ui/MissionMenu.cc
@@ -197,7 +197,7 @@ MissionMenu::MissionMenu() :
PFile descriptionFile( missionDir->path() + "/description.json" );
JSON descriptionConfig;
- if( !descriptionConfig.load( &descriptionFile ) ) {
+ if( !descriptionConfig.load( descriptionFile ) ) {
continue;
}
@@ -212,7 +212,7 @@ MissionMenu::MissionMenu() :
PFile image( missionDir->path() + "/description.ozImage" );
uint imageId = 0;
- if( image.stat() ) {
+ if( image.type() != File::MISSING ) {
imageId = context.loadTextureLayer( image.path() );
}
View
8 src/client/ui/Mouse.cc
@@ -110,18 +110,18 @@ void Mouse::init()
for( int i = 0; i < CURSORS_MAX; ++i ) {
PFile file( String::str( "ui/cur/%s.ozCur", NAMES[i] ) );
- if( !file.map() ) {
+
+ Buffer buffer = file.read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "Cursor loading failed" );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
cursors[i].size = is.readInt();
cursors[i].hotspotX = is.readInt();
cursors[i].hotspotY = is.readInt();
cursors[i].texId = context.readTextureLayer( &is );
-
- file.unmap();
}
Log::printEnd( " OK" );
View
3 src/client/ui/Style.cc
@@ -75,8 +75,7 @@ void Style::init()
PFile configFile( "ui/style.json" );
- JSON config;
- config.load( &configFile );
+ JSON config( configFile );
const JSON& fontsConfig = config["fonts"];
View
31 src/common/Lingua.cc
@@ -45,15 +45,15 @@ String Lingua::detectLanguage( const char* language_ )
String lang = language_;
if( !lang.isEmpty() ) {
- if( !PFile( "lingua/" + lang ).stat() ) {
+ if( PFile( "lingua/" + lang ).type() == File::MISSING ) {
lang = "";
}
return lang;
}
lang = getenv( "LANGUAGE" );
- if( !lang.isEmpty() && PFile( "lingua/" + lang ).stat() ) {
+ if( !lang.isEmpty() && PFile( "lingua/" + lang ).type() != File::MISSING ) {
return lang;
}
@@ -63,7 +63,7 @@ String Lingua::detectLanguage( const char* language_ )
if( underscore >= 2 ) {
lang = lang.substring( 0, underscore );
- if( PFile( "lingua/" + lang ).stat() ) {
+ if( PFile( "lingua/" + lang ).type() != File::MISSING ) {
return lang;
}
}
@@ -74,7 +74,7 @@ String Lingua::detectLanguage( const char* language_ )
if( underscore >= 2 ) {
lang = lang.substring( 0, underscore );
- if( PFile( "lingua/" + lang ).stat() ) {
+ if( PFile( "lingua/" + lang ).type() != File::MISSING ) {
return lang;
}
}
@@ -109,11 +109,12 @@ bool Lingua::initMission( const char* mission )
PFile file( String::str( "mission/%s/lingua/%s.ozCat", mission, language.cstr() ) );
- if( !file.map() ) {
+ Buffer buffer = file.read();
+ if( buffer.isEmpty() ) {
return false;
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
int length = is.readInt();
@@ -133,8 +134,6 @@ bool Lingua::initMission( const char* mission )
messages[index] = msg;
}
- file.unmap();
-
return true;
}
@@ -145,7 +144,7 @@ bool Lingua::init( const char* language_ )
language = language_;
PFile dir( "lingua/" + language );
- if( !dir.stat() ) {
+ if( dir.type() == File::MISSING ) {
OZ_ERROR( "Invalid locale '%s', does not match any subdirectory in lingua/", language.cstr() );
}
@@ -156,15 +155,14 @@ bool Lingua::init( const char* language_ )
continue;
}
- if( !file->map() ) {
+ Buffer buffer = file->read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "Cannot read catalogue '%s'", file->path().cstr() );
}
- InputStream is = file->inputStream();
+ InputStream is = buffer.inputStream();
nMessages += is.readInt();
-
- file->unmap();
}
if( nMessages == 0 ) {
@@ -180,11 +178,12 @@ bool Lingua::init( const char* language_ )
continue;
}
- if( !file->map() ) {
+ Buffer buffer = file->read();
+ if( buffer.isEmpty() ) {
OZ_ERROR( "Cannot read catalogue '%s'", file->path().cstr() );
}
- InputStream is = file->inputStream();
+ InputStream is = buffer.inputStream();
int length = is.readInt();
@@ -199,8 +198,6 @@ bool Lingua::init( const char* language_ )
messages[index] = msg;
}
-
- file->unmap();
}
return true;
View
22 src/matrix/BSP.cc
@@ -37,14 +37,14 @@ void BSP::load()
{
Log::print( "Loading BSP structure '%s' ...", name.cstr() );
- String sPath = "bsp/" + name + ".ozBSP";
+ PFile file( "bsp/" + name + ".ozBSP" );
+ Buffer buffer = file.read();
- PFile file( sPath );
- if( !file.map() ) {
- OZ_ERROR( "BSP file mmap failed" );
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "BSP file '%s' read failed", file.path().cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
// bounds
is.readPoint();
@@ -213,8 +213,6 @@ void BSP::load()
hard_assert( !is.isAvailable() );
- file.unmap();
-
Log::printEnd( " OK" );
}
@@ -255,11 +253,13 @@ BSP::BSP( const char* name_, int id_ ) :
name( name_ ), id( id_ ), nUsers( 0 )
{
PFile file( String::str( "bsp/%s.ozBSP", name_ ) );
- if( !file.map() ) {
- OZ_ERROR( "BSP file mmap failed" );
+
+ if( file.type() != File::REGULAR ) {
+ OZ_ERROR( "BSP file '%s' read failed", file.path().cstr() );
}
- InputStream is = file.inputStream();
+ Buffer buffer = file.read();
+ InputStream is = buffer.inputStream();
mins = is.readPoint();
maxs = is.readPoint();
@@ -286,8 +286,6 @@ BSP::BSP( const char* name_, int id_ ) :
String sDemolishSound = is.readString();
demolishSound = sDemolishSound.isEmpty() ? -1 : liber.soundIndex( sDemolishSound );
-
- file.unmap();
}
}
View
43 src/matrix/Liber.cc
@@ -222,8 +222,6 @@ void Liber::initTextures()
DArray<PFile> dirList = dir.ls();
foreach( subDir, dirList.iter() ) {
- subDir->stat();
-
if( subDir->type() != File::DIRECTORY ) {
continue;
}
@@ -262,8 +260,6 @@ void Liber::initSounds()
DArray<PFile> dirList = dir.ls();
foreach( subDir, dirList.iter() ) {
- subDir->stat();
-
if( subDir->type() != File::DIRECTORY ) {
continue;
}
@@ -388,22 +384,20 @@ void Liber::initModels()
DArray<PFile> dirList = dir.ls();
foreach( file, dirList.iter() ) {
- file->stat();
-
if( file->type() != File::DIRECTORY ) {
continue;
}
String name = file->name();
String path;
- if( PFile( file->path() + "/data.ozcSMM" ).stat() ) {
+ if( PFile( file->path() + "/data.ozcSMM" ).type() == File::REGULAR ) {
path = file->path() + "/data.ozcSMM";
}
- else if( PFile( file->path() + "/data.ozcMD2" ).stat() ) {
+ else if( PFile( file->path() + "/data.ozcMD2" ).type() == File::REGULAR ) {
path = file->path() + "/data.ozcMD2";
}
- else if( PFile( file->path() + "/data.ozcMD3" ).stat() ) {
+ else if( PFile( file->path() + "/data.ozcMD3" ).type() == File::REGULAR ) {
path = file->path() + "/data.ozcMD3";
}
else {
@@ -463,8 +457,6 @@ void Liber::initMusicRecurse( const char* path, List<Resource>* musicTracksList
DArray<PFile> dirList = dir.ls();
foreach( file, dirList.iter() ) {
- file->stat();
-
if( file->type() == File::DIRECTORY ) {
initMusicRecurse( file->path(), musicTracksList );
}
@@ -520,11 +512,12 @@ void Liber::initFragPools()
continue;
}
- if( !file->map() ) {
- OZ_ERROR( "Failed to map '%s'", file->path().cstr() );
+ Buffer buffer = file->read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Failed to read '%s'", file->path().cstr() );
}
- InputStream is = file->inputStream();
+ InputStream is = buffer.inputStream();
while( is.isAvailable() ) {
const char* name = is.readString();
@@ -533,8 +526,6 @@ void Liber::initFragPools()
fragPools.add( name, FragPool( &is, name, fragPools.length() ) );
}
-
- file->unmap();
}
nFragPools = fragPools.length();
@@ -562,11 +553,12 @@ void Liber::initClasses()
continue;
}
- if( !file->map() ) {
- OZ_ERROR( "Failed to map '%s'", file->path().cstr() );
+ Buffer buffer = file->read();
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Failed to read '%s'", file->path().cstr() );
}
- InputStream is = file->inputStream();
+ InputStream is = buffer.inputStream();
int nClasses = is.readInt();
int nDevices = is.readInt();
@@ -619,8 +611,6 @@ void Liber::initClasses()
audioIndices.include( sAudio, audioIndices.length() );
}
}
-
- file->unmap();
}
nDeviceClasses = deviceIndices.length();
@@ -629,15 +619,12 @@ void Liber::initClasses()
// Initialise all classes.
foreach( file, dirList.iter() ) {
- if( !file->hasExtension( "ozClasses" ) ) {
+ if( file->type() != File::REGULAR || !file->hasExtension( "ozClasses" ) ) {
continue;
}
- if( !file->map() ) {
- OZ_ERROR( "Failed to map '%s'", file->path().cstr() );
- }
-
- InputStream is = file->inputStream();
+ Buffer buffer = file->read();
+ InputStream is = buffer.inputStream();
int nClasses = is.readInt();
int nDevices = is.readInt();
@@ -664,8 +651,6 @@ void Liber::initClasses()
( *clazz )->init( &is, name );
}
-
- file->unmap();
}
foreach( classIter, objClasses.citer() ) {
View
12 src/matrix/Lua.cc
@@ -425,18 +425,12 @@ void Lua::init()
DArray<PFile> luaFiles = luaDir.ls();
foreach( file, luaFiles.iter() ) {
- if( file->hasExtension( "lua" ) ) {
- if( !file->map() ) {
- OZ_ERROR( "Failed to read Lua script '%s'", file->path().cstr() );
- }
-
- InputStream istream = file->inputStream();
+ if( file->type() != File::REGULAR || file->hasExtension( "lua" ) ) {
+ Buffer buffer = file->read();
- if( l_dobuffer( istream.begin(), istream.capacity(), file->path() ) != 0 ) {
+ if( !buffer.isEmpty() && l_dobuffer( buffer.begin(), buffer.length(), file->path() ) != 0 ) {
OZ_ERROR( "Matrix Lua script error" );
}
-
- file->unmap();
}
}
View
7 src/matrix/NamePool.cc
@@ -56,11 +56,12 @@ void NamePool::init()
for( int i = 0; i < liber.nameLists.length(); ++i ) {
PFile file( liber.nameLists[i].path );
- if( !file.map() ) {
+ if( file.type() != File::REGULAR ) {
OZ_ERROR( "Reading '%s' failed", liber.nameLists[i].path.cstr() );
}
- InputStream is = file.inputStream();
+ Buffer buffer = file.read();
+ InputStream is = buffer.inputStream();
const char* wordBegin = is.pos();
@@ -79,8 +80,6 @@ void NamePool::init()
}
}
- file.unmap();
-
listPositions.add( names.length() );
}
View
10 src/matrix/Terra.cc
@@ -56,11 +56,13 @@ void Terra::load( int id_ )
Log::print( "Loading terrain '%s' ...", name.cstr() );
PFile file( path );
- if( !file.map() ) {
- OZ_ERROR( "Cannot map terra file" );
+ Buffer buffer = file.read();
+
+ if( buffer.isEmpty() ) {
+ OZ_ERROR( "Cannot read terra file '%s'", file.path().cstr() );
}
- InputStream is = file.inputStream();
+ InputStream is = buffer.inputStream();
int max = is.readInt();
if( max != VERTS ) {
@@ -89,8 +91,6 @@ void Terra::load( int id_ )
liquid = is.readInt();
- file.unmap();
-
Log::printEnd( " OK" );
}
}
View
12 src/nirvana/Lua.cc
@@ -480,18 +480,12 @@ void Lua::init()
DArray<PFile> luaFiles = luaDir.ls();
foreach( file, luaFiles.iter() ) {
- if( file->hasExtension( "lua" ) ) {
- if( !file->map() ) {
- OZ_ERROR( "Failed to read Lua script '%s'", file->path().cstr() );
- }
-
- InputStream istream = file->inputStream();
+ if( file->type() != File::REGULAR || file->hasExtension( "lua" ) ) {
+ Buffer buffer = file->read();
- if( l_dobuffer( istream.begin(), istream.capacity(), file->path() ) != 0 ) {
+ if( !buffer.isEmpty() && l_dobuffer( buffer.begin(), buffer.length(), file->path() ) != 0 ) {
OZ_ERROR( "Nirvana Lua script error" );
}
-
- file->unmap();
}
}
View
2 src/ozCore/Buffer.cc
@@ -109,7 +109,7 @@ InputStream Buffer::inputStream( Endian::Order order ) const
return InputStream( data, data + size, order );
}
-OutputStream Buffer::outputStream( Endian::Order order ) const
+OutputStream Buffer::outputStream( Endian::Order order )
{
hard_assert( data != nullptr );
View
2 src/ozCore/Buffer.hh
@@ -173,7 +173,7 @@ class Buffer
/**
* Create an `OutputStream` object for writing binary data into the buffer.
*/
- OutputStream outputStream( Endian::Order order = Endian::NATIVE ) const;
+ OutputStream outputStream( Endian::Order order = Endian::NATIVE );
};
View
191 src/ozCore/File.cc
@@ -135,97 +135,10 @@ File::File( const char* path ) :
#ifdef __native_client__
descriptor = new Descriptor( this );
#endif
-}
-
-File::~File()
-{
- unmap();
-
-#ifdef __native_client__
- delete descriptor;
-#endif
-}
-
-File::File( const File& file ) :
- filePath( file.filePath ), fileType( file.fileType ), fileSize( file.fileSize ),
- fileTime( file.fileTime ), data( nullptr )
-{
-#ifdef __native_client__
- descriptor = new Descriptor( this );
-#endif
-}
-
-File::File( File&& file ) :
- filePath( static_cast<String&&>( file.filePath ) ), fileType( file.fileType ),
- fileSize( file.fileSize ), fileTime( file.fileTime ), data( file.data )
-{
-#ifdef __native_client__
- descriptor = new Descriptor( this );
-#endif
-
- file.filePath = "";
- file.fileType = MISSING;
- file.fileSize = -1;
- file.fileTime = 0;
- file.data = nullptr;
-}
-
-File& File::operator = ( const File& file )
-{
- if( &file == this ) {
- return *this;
- }
-
- unmap();
-
- filePath = file.filePath;
- fileType = file.fileType;
- fileSize = file.fileSize;
- fileTime = file.fileTime;
- data = nullptr;
-
- return *this;
-}
-
-File& File::operator = ( File&& file )
-{
- if( &file == this ) {
- return *this;
- }
-
- unmap();
-
- filePath = static_cast<String&&>( file.filePath );
- fileType = file.fileType;
- fileSize = file.fileSize;
- fileTime = file.fileTime;
- data = file.data;
-
- file.filePath = "";
- file.fileType = MISSING;
- file.fileSize = -1;
- file.fileTime = 0;
- file.data = nullptr;
-
- return *this;
-}
-
-void File::setPath( const char* path )
-{
- unmap();
- filePath = path;
- fileType = MISSING;
- fileSize = -1;
- fileTime = 0;
-}
-
-bool File::stat()
-{
- // If file is mapped it had to be successfuly stat'd before. Futhermore fileSize must not change
- // while file is mapped as it is needed by read() function if mapped and unmap() on POSIX systems.
- if( data != nullptr ) {
- return true;
+ // Avoid stat'ing obviously non-existent files.
+ if( filePath.isEmpty() ) {
+ return;
}
#if defined( __native_client__ )
@@ -363,53 +276,78 @@ bool File::stat()
}
#endif
-
- return fileType != MISSING;
}
-File::Type File::type() const
+File::~File()
{
- return fileType;
-}
+ unmap();