// For reading the face-data chunk static bool ReadFaceData(OpenedFile& OFile, int32 ParentChunkEnd) { uint8 NFBuffer[2]; uint16 NumFaces; if (!OFile.Read(2,NFBuffer)) { logError1("ERROR reading number of faces in %s",Path); return false; } uint8 *S = NFBuffer; StreamToValue(S,NumFaces); int32 DataSize = 4*sizeof(uint16)*int(NumFaces); SetChunkBufferSize(DataSize); if (!OFile.Read(DataSize,ChunkBufferBase())) { logError1("ERROR reading face-chunk contents in %s",Path); return false; } S = ChunkBufferBase(); ModelPtr->VertIndices.resize(3*NumFaces); for (int k=0; k<NumFaces; k++) { uint16 *CurrPoly = ModelPtr->VIBase() + 3*k; uint16 Flags; StreamToList(S,CurrPoly,3); StreamToValue(S,Flags); } int32 Location = 0; OFile.GetPosition(Location); while(Location < ParentChunkEnd) { ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; switch(ChunkHeader.ID) { /* case OBJECT: if (!ReadContainer(OFile,ChunkHeader,ReadObject)) return false; break; */ default: if (!SkipChunk(OFile,ChunkHeader)) return false; } // Where are we now? OFile.GetPosition(Location); } if (Location > ParentChunkEnd) { logError3("ERROR: Overran parent chunk: %d > %d in %s",Location,ParentChunkEnd,Path); return false; } return true; }
static void import_m1_physics_data() { OpenedFile PhysicsFile; if (!PhysicsFileSpec.Open(PhysicsFile)) { return; } int32 position = 0; int32 length; PhysicsFile.GetLength(length); while (position < length) { std::vector<uint8> header(12); PhysicsFile.Read(header.size(), &header[0]); AIStreamBE header_stream(&header[0], header.size()); uint32 tag; uint16 count; uint16 size; header_stream >> tag; header_stream.ignore(4); // unused header_stream >> count; header_stream >> size; std::vector<uint8> data(count * size); PhysicsFile.Read(data.size(), &data[0]); switch (tag) { case M1_MONSTER_PHYSICS_TAG: unpack_m1_monster_definition(&data[0], count); break; case M1_EFFECTS_PHYSICS_TAG: unpack_m1_effect_definition(&data[0], count); break; case M1_PROJECTILE_PHYSICS_TAG: unpack_m1_projectile_definition(&data[0], count); break; case M1_PHYSICS_PHYSICS_TAG: unpack_m1_physics_constants(&data[0], count); break; case M1_WEAPONS_PHYSICS_TAG: unpack_m1_weapon_definition(&data[0], count); break; } PhysicsFile.GetPosition(position); } }
bool XML_Loader_SDL::ParseFile(FileSpecifier &file_name) { // Open file OpenedFile file; if (file_name.Open(file)) { printf ( "Parsing: %s\n", file_name.GetPath() ); // Get file size and allocate buffer file.GetLength(data_size); data = new char[data_size]; // In case there were errors... file_name.GetName(FileName); // Read and parse file if (file.Read(data_size, data)) { if (!DoParse()) { fprintf(stderr, "There were parsing errors in configuration file %s\n", FileName); } } // Delete buffer delete[] data; data = NULL; return true; } else { printf ( "Couldn't open: %s\n", file_name.GetPath() ); } return false; }
bool PluginLoader::ParsePlugin(FileSpecifier& file_name) { OpenedFile file; if (file_name.Open(file)) { int32 data_size; file.GetLength(data_size); m_data.resize(data_size); if (file.Read(data_size, &m_data[0])) { file_name.ToDirectory(current_plugin_directory); char name[256]; current_plugin_directory.GetName(name); m_name = name; if (!DoParse()) { logError1("There were parsing errors in %s Plugin.xml\n", m_name.c_str()); } } m_data.clear(); return true; } return false; }
void rewind_recording( void) { if(replay.game_is_being_recorded) { /* This is unnecessary, because it is called from reset_player_queues, */ /* which is always called from revert_game */ /* FilmFile.SetLength(sizeof(recording_header)); FilmFile.SetPosition(sizeof(recording_header)); */ // Alternative that does not use "SetLength", but instead creates and re-creates the file. FilmFile.SetPosition(0); byte Header[SIZEOF_recording_header]; FilmFile.Read(SIZEOF_recording_header,Header); FilmFile.Close(); FilmFileSpec.Delete(); FilmFileSpec.Create(_typecode_film); FilmFileSpec.Open(FilmFile,true); FilmFile.Write(SIZEOF_recording_header,Header); // Use the packed length here!!! replay.header.length= SIZEOF_recording_header; } }
// For reading the object-data chunk static bool ReadObject(OpenedFile& OFile, int32 ParentChunkEnd) { // Read the name if (DBOut) fprintf(DBOut,"Object Name: "); while(true) { char c; if (!OFile.Read(1,&c)) { if (DBOut) fprintf(DBOut,"ERROR in reading name"); return false; } if (c == 0) { if (DBOut) fprintf(DBOut,"\n"); break; } else { if (DBOut) fprintf(DBOut,"%c",c); } } int32 Location = 0; OFile.GetPosition(Location); while(Location < ParentChunkEnd) { ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; switch(ChunkHeader.ID) { case TRIMESH: if (!ReadContainer(OFile,ChunkHeader,ReadTrimesh)) return false; break; default: if (!SkipChunk(OFile,ChunkHeader)) return false; } // Where are we now? OFile.GetPosition(Location); } if (Location > ParentChunkEnd) { if (DBOut) fprintf(DBOut,"ERROR: Overran parent chunk: %ld > %ld\n",Location,ParentChunkEnd); return false; } return true; }
bool setup_for_replay_from_file( FileSpecifier& File, uint32 map_checksum, bool prompt_to_export) { bool successful= false; (void)(map_checksum); FilmFileSpec = File; if (FilmFileSpec.Open(FilmFile)) { replay.valid= true; replay.have_read_last_chunk = false; replay.game_is_being_replayed = true; assert(!replay.resource_data); replay.resource_data= NULL; replay.resource_data_size= 0l; replay.film_resource_offset= NONE; byte Header[SIZEOF_recording_header]; FilmFile.Read(SIZEOF_recording_header,Header); unpack_recording_header(Header,&replay.header,1); replay.header.game_information.cheat_flags = _allow_crosshair | _allow_tunnel_vision | _allow_behindview | _allow_overlay_map; /* Set to the mapfile this replay came from.. */ if(use_map_file(replay.header.map_checksum)) { replay.fsread_buffer= new char[DISK_CACHE_SIZE]; assert(replay.fsread_buffer); replay.location_in_cache= NULL; replay.bytes_in_cache= 0; replay.replay_speed= 1; #ifdef DEBUG_REPLAY open_stream_file(); #endif if (prompt_to_export) Movie::instance()->PromptForRecording(); successful= true; } else { /* Tell them that this map wasn't found. They lose. */ alert_user(infoError, strERRORS, cantFindReplayMap, 0); replay.valid= false; replay.game_is_being_replayed= false; FilmFile.Close(); } } return successful; }
bool LoadChunk(OpenedFile& OFile, ChunkHeaderData& ChunkHeader) { logTrace2("Loading chunk 0x%04hx size %u",ChunkHeader.ID,ChunkHeader.Size); int32 DataSize = ChunkHeader.Size - SIZEOF_ChunkHeaderData; SetChunkBufferSize(DataSize); if (!OFile.Read(DataSize,ChunkBufferBase())) { logError1("ERROR reading chunk contents in %s",Path); return false; } return true; }
bool ReadChunkHeader(OpenedFile& OFile, ChunkHeaderData& ChunkHeader) { uint8 Buffer[SIZEOF_ChunkHeaderData]; if (!OFile.Read(SIZEOF_ChunkHeaderData,Buffer)) { logError1("ERROR reading chunk header in %s",Path); return false; } uint8 *S = Buffer; StreamToValue(S,ChunkHeader.ID); StreamToValue(S,ChunkHeader.Size); return true; }
bool ReadChunkHeader(OpenedFile& OFile, ChunkHeaderData& ChunkHeader) { uint8 Buffer[SIZEOF_ChunkHeaderData]; if (!OFile.Read(SIZEOF_ChunkHeaderData,Buffer)) { if (DBOut) fprintf(DBOut,"ERROR reading chunk header\n"); return false; } uint8 *S = Buffer; StreamToValue(S,ChunkHeader.ID); StreamToValue(S,ChunkHeader.Size); return true; }
bool LoadChunk(OpenedFile& OFile, ChunkHeaderData& ChunkHeader) { if (DBOut) fprintf(DBOut,"Loading chunk 0x%04hx size %lu\n",ChunkHeader.ID,ChunkHeader.Size); int32 DataSize = ChunkHeader.Size - SIZEOF_ChunkHeaderData; SetChunkBufferSize(DataSize); if (!OFile.Read(DataSize,ChunkBufferBase())) { if (DBOut) fprintf(DBOut,"ERROR reading chunk contents\n"); return false; } return true; }
void *get_network_physics_buffer( int32 *physics_length) { if (physics_file_is_m1()) { bool success = false; uint8 *data = NULL; OpenedFile PhysicsFile; if (PhysicsFileSpec.Open(PhysicsFile) && PhysicsFile.GetLength(*physics_length)) { data = (uint8 *)malloc(*physics_length + 4); SDL_RWops *ops = SDL_RWFromMem(data, *physics_length + 4); success = SDL_WriteBE32(ops, uint32(M1_PHYSICS_MAGIC_COOKIE)); if (success) success = SDL_WriteBE32(ops, uint32(*physics_length)); SDL_RWclose(ops); if (success) success = PhysicsFile.Read(*physics_length, &data[8]); if (!success) free(data); } if (!success) { *physics_length = 0; return NULL; } return data; } short SavedType, SavedError = get_game_error(&SavedType); void *data= get_flat_data(PhysicsFileSpec, false, 0); set_game_error(SavedType, SavedError); if(data) { *physics_length= get_flat_data_length(data); } else { *physics_length= 0; } return data; }
// For reading the object-data chunk static bool ReadObject(OpenedFile& OFile, int32 ParentChunkEnd) { // Read the name char c; do { if (!OFile.Read(1,&c)) { logError1("ERROR when reading name in %s",Path); return false; } } while(c != 0); int32 Location = 0; OFile.GetPosition(Location); while(Location < ParentChunkEnd) { ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; switch(ChunkHeader.ID) { case TRIMESH: if (!ReadContainer(OFile,ChunkHeader,ReadTrimesh)) return false; break; default: if (!SkipChunk(OFile,ChunkHeader)) return false; } // Where are we now? OFile.GetPosition(Location); } if (Location > ParentChunkEnd) { logError3("ERROR: Overran parent chunk: %d > %d in %s",Location,ParentChunkEnd,Path); return false; } return true; }
// DJB OpenGL char* parseFile(FileSpecifier& fileSpec) { if (fileSpec == FileSpecifier() || !fileSpec.Exists()) { return NULL; } OpenedFile file; if (!fileSpec.Open(file)) { fprintf(stderr, "%s not found\n", fileSpec.GetPath()); return NULL; } int32 length; file.GetLength(length); char* str = new char[length + 1]; file.Read(length, str); str[length] = 0; return str; }
/* This is gross, (Alain wrote it, not me!) but I don't have time to clean it up */ static bool vblFSRead( OpenedFile& File, int32 *count, void *dest, bool& HitEOF) { int32 fsread_count; bool status = true; assert(replay.fsread_buffer); // LP: way for testing whether hitting end-of-file; // doing that by testing for whether a read was complete. HitEOF = false; if (replay.bytes_in_cache < *count) { assert(replay.bytes_in_cache + *count < int(DISK_CACHE_SIZE)); if (replay.bytes_in_cache) { memcpy(replay.fsread_buffer, replay.location_in_cache, replay.bytes_in_cache); } replay.location_in_cache = replay.fsread_buffer; fsread_count= DISK_CACHE_SIZE - replay.bytes_in_cache; int32 PrevPos; File.GetPosition(PrevPos); int32 replay_left= replay.header.length - PrevPos; if(replay_left < fsread_count) fsread_count= replay_left; if(fsread_count > 0) { assert(fsread_count > 0); // LP: wrapped the routines with some for finding out the file positions; // this finds out how much is read indirectly status = File.Read(fsread_count,replay.fsread_buffer+replay.bytes_in_cache); int32 CurrPos; File.GetPosition(CurrPos); int32 new_fsread_count = CurrPos - PrevPos; int32 FileLen; File.GetLength(FileLen); HitEOF = (new_fsread_count < fsread_count) && (CurrPos == FileLen); fsread_count = new_fsread_count; if(status) replay.bytes_in_cache += fsread_count; } } // If we're still low, then we've consumed the disk cache if(replay.bytes_in_cache < *count) { HitEOF = true; } // Ignore EOF if we still have cache if (HitEOF && replay.bytes_in_cache < *count) { *count= replay.bytes_in_cache; } else { status = true; HitEOF = false; } memcpy(dest, replay.location_in_cache, *count); replay.bytes_in_cache -= *count; replay.location_in_cache += *count; return status; }