// Copy file contents bool FileSpecifier::CopyContents(FileSpecifier &source_name) { err = 0; OpenedFile src, dst; if (source_name.Open(src)) { Delete(); if (Open(dst, true)) { const int BUFFER_SIZE = 1024; uint8 buffer[BUFFER_SIZE]; int32 length = 0; src.GetLength(length); while (length && err == 0) { int32 count = length > BUFFER_SIZE ? BUFFER_SIZE : length; if (src.Read(count, buffer)) { if (!dst.Write(count, buffer)) err = dst.GetError(); } else err = src.GetError(); length -= count; } } } else err = source_name.GetError(); if (err) Delete(); return err == 0; }
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; }
bool physics_file_is_m1(void) { bool m1_physics = false; // check for M1 physics OpenedFile PhysicsFile; short SavedType, SavedError = get_game_error(&SavedType); if (PhysicsFileSpec.Open(PhysicsFile)) { uint32 tag = SDL_ReadBE32(PhysicsFile.GetRWops()); switch (tag) { case M1_MONSTER_PHYSICS_TAG: case M1_EFFECTS_PHYSICS_TAG: case M1_PROJECTILE_PHYSICS_TAG: case M1_PHYSICS_PHYSICS_TAG: case M1_WEAPONS_PHYSICS_TAG: m1_physics = true; break; default: break; } PhysicsFile.Close(); } set_game_error(SavedType, SavedError); return m1_physics; }
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; } }
bool LoadModel_Studio(FileSpecifier& Spec, Model3D& Model) { ModelPtr = &Model; Model.Clear(); if (DBOut) { // Name buffer const int BufferSize = 256; char Buffer[BufferSize]; Spec.GetName(Buffer); fprintf(DBOut,"Loading 3D Studio Max model file %s\n",Buffer); } OpenedFile OFile; if (!Spec.Open(OFile)) { if (DBOut) fprintf(DBOut,"ERROR opening the file\n"); return false; } ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; if (ChunkHeader.ID != MASTER) { if (DBOut) fprintf(DBOut,"ERROR: not a 3DS Max model file\n"); return false; } if (!ReadContainer(OFile,ChunkHeader,ReadMaster)) return false; return (!Model.Positions.empty() && !Model.VertIndices.empty()); }
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 SndfileDecoder::Open(FileSpecifier& File) { Close(); sfinfo.format = 0; OpenedFile openedFile; if (File.Open(openedFile)) { rwops = openedFile.TakeRWops(); sndfile = sf_open_virtual(&sf_virtual, SFM_READ, &sfinfo, rwops); } return sndfile; }
/* -------------- Entry Point ----------- */ uint32 calculate_crc_for_file(FileSpecifier& File) { uint32 crc = 0; OpenedFile OFile; if (File.Open(OFile)) { crc= calculate_crc_for_opened_file(OFile); OFile.Close(); } return crc; }
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); } }
SDL_Surface *WadImageCache::image_from_name(std::string& name) const { FileSpecifier file; file.SetToImageCacheDir(); file.AddPart(name); OpenedFile of; if (!file.Open(of)) return NULL; #ifdef HAVE_SDL_IMAGE SDL_Surface *img = IMG_Load_RW(of.GetRWops(), 0); #else SDL_Surface *img = SDL_LoadBMP_RW(of.GetRWops(), 0); #endif return img; }
bool QuickSaveLoader::ParseQuickSave(FileSpecifier& file_name) { struct wad_header header; struct wad_data *wad; OpenedFile file; if (file_name.Open(file)) { if (read_wad_header(file, &header)) { wad = read_indexed_wad_from_file(file, &header, SAVE_GAME_METADATA_INDEX, true); if (wad) { size_t data_length; char *raw_metadata = (char *)extract_type_from_wad(wad, SAVE_META_TAG, &data_length); std::string metadata = std::string(raw_metadata, data_length); boost::property_tree::ptree pt; std::istringstream strm(metadata); try { boost::property_tree::ini_parser::read_ini(strm, pt); } catch (...) { return false; } QuickSave Data = QuickSave(); Data.save_file = file_name; Data.name = pt.get("name", Data.name); Data.level_name = pt.get("level_name", Data.level_name); Data.ticks = pt.get("ticks", Data.ticks); Data.formatted_ticks = pt.get("ticks_formatted", Data.formatted_ticks); Data.save_time = pt.get("time", Data.save_time); Data.formatted_time = pt.get("time_formatted", Data.formatted_time); Data.players = pt.get("players", Data.players); QuickSaves::instance()->add(Data); free_wad(wad); } } return true; } return false; }
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; }
bool MADDecoder::Open(FileSpecifier &File) { if (!File.Open(file)) return false; file_done = false; if (DecodeFrame()) { stereo = (MAD_NCHANNELS(&Frame.header) == 2); bytes_per_frame = 2 * (stereo ? 2 : 1); rate = Frame.header.samplerate; sample = 0; return true; } else { return false; } }
/* Note that we _must_ set the header information before we start recording!! */ void start_recording( void) { assert(!replay.valid); replay.valid= true; if(get_recording_filedesc(FilmFileSpec)) FilmFileSpec.Delete(); if (FilmFileSpec.Create(_typecode_film)) { /* I debate the validity of fsCurPerm here, but Alain had it, and it has been working */ if (FilmFileSpec.Open(FilmFile,true)) { replay.game_is_being_recorded= true; // save a header containing information about the game. byte Header[SIZEOF_recording_header]; pack_recording_header(Header,&replay.header,1); FilmFile.Write(SIZEOF_recording_header,Header); } } }
std::string WadImageCache::image_to_new_name(SDL_Surface *image, int32 *filesize) const { // create name boost::uuids::random_generator gen; boost::uuids::uuid u = gen(); std::string ustr = boost::uuids::to_string(u); FileSpecifier File; File.SetToImageCacheDir(); File.AddPart(ustr); FileSpecifier TempFile; TempFile.SetTempName(File); int ret; //#if defined(HAVE_PNG) && defined(HAVE_SDL_IMAGE) // ret = aoIMG_SavePNG(TempFile.GetPath(), image, IMG_COMPRESS_DEFAULT, NULL, 0); #ifdef HAVE_SDL_IMAGE ret = IMG_SavePNG(image, TempFile.GetPath()); #else ret = SDL_SaveBMP(image, TempFile.GetPath()); #endif if (ret == 0 && TempFile.Rename(File)) { if (filesize) { OpenedFile of; if (File.Open(of)) of.GetLength(*filesize); } return ustr; } if (filesize) *filesize = 0; return ""; }
bool LoadModel_Studio(FileSpecifier& Spec, Model3D& Model) { ModelPtr = &Model; Model.Clear(); Path = Spec.GetPath(); logNote1("Loading 3D Studio Max model file %s",Path); OpenedFile OFile; if (!Spec.Open(OFile)) { logError1("ERROR opening %s",Path); return false; } ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; if (ChunkHeader.ID != MASTER) { logError1("ERROR: not a 3DS Max model file: %s",Path); return false; } if (!ReadContainer(OFile,ChunkHeader,ReadMaster)) return false; if (Model.Positions.empty()) { logError1("ERROR: no vertices found in %s",Path); return false; } if (Model.VertIndices.empty()) { logError1("ERROR: no faces found in %s",Path); return false; } return true; }