int main(int argc, char** argv) { _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); IFile::OpenMode mode = IFile::O_ReadOnly; assert(argc>1); IFile *pFile = OpenDiskFile(argv[1], mode); BlockManager* pMgr = new BlockManager(pFile, mode==IFile::O_Truncate, offset_type(1024)); BlockFS *pFS = new BlockFS(pMgr, mode); std::vector<std::string> names; pFS->ExportFileNames(names); for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) { IFile* pUnpackedFile = pFS->OpenFileInPackage(it->c_str()); IFile* pTemp = OpenDiskFile(it->c_str(), IFile::O_Truncate); offset_type length = pUnpackedFile->GetSize(); char* buff = new char[(size_t)length.offset]; printf("Unpacking %s\n", it->c_str()); pUnpackedFile->Read(buff, length); pTemp->Write(buff, length); delete []buff; delete pTemp; delete pUnpackedFile; } delete pFS; delete pMgr; delete pFile; }
bool CConfigFile::Save(const char* filename, IFileSystem& fs) const { IFile* file = fs.Open(filename, IFileSystem::write); if (file == NULL) { return false; } // find the section without a name and write that first std::map<std::string, Section>::const_iterator i; for (i = m_sections.begin(); i != m_sections.end(); i++) { if (i->first == "") { const Section& s = i->second; std::map<std::string, std::string>::const_iterator j; for (j = s.entries.begin(); j != s.entries.end(); j++) { write_string(file, j->first); file->Write("=", 1); write_string(file, j->second); file->Write("\n", 1); } file->Write("\n", 1); } } // write the rest of the sections for (i = m_sections.begin(); i != m_sections.end(); i++) { if (i->first != "") { file->Write("[", 1); write_string(file, i->first); file->Write("]\n", 2); const Section& s = i->second; std::map<std::string, std::string>::const_iterator j; for (j = s.entries.begin(); j != s.entries.end(); j++) { write_string(file, j->first); file->Write("=", 1); write_string(file, j->second); file->Write("\n", 1); } file->Write("\n", 1); } } file->Close(); return true; }
//------------------------------------------------------------- // exports car model. Car models are typical MODEL structures //------------------------------------------------------------- void ExportCarModel(MODEL* model, int size, int index, const char* name_suffix) { EqString model_name(varargs("%s/CARMODEL_%d_%s", g_levname_moddir.c_str(), index, name_suffix)); // export model ExportDMODELToOBJ(model, model_name.c_str(), index, true); // save original dmodel2 IFile* dFile = GetFileSystem()->Open(varargs("%s.dmodel", model_name.c_str()), "wb", SP_ROOT); if(dFile) { dFile->Write(model, size, 1); GetFileSystem()->Close(dFile); } }
//--------------------------------------------------------------------------- // 函数: Save // 功能: 保存当前的INI文件 // 返回: 1 成功, 0失败 //--------------------------------------------------------------------------- int QIniFileImpl::Save(const char* FileName) { int nResult = false; IFile* piFile = NULL; SECNODE* SecNode = m_Header.pNextNode; KEYNODE* KeyNode = NULL; DWORD dwStringLen = 0; DWORD dwWriteSize = 0; const char cszNewLine[3] = "\r\n"; piFile = g_CreateFile(FileName); PROCESS_ERROR(piFile); while (SecNode != NULL) { dwStringLen = (DWORD)strlen(SecNode->pSection); dwWriteSize = piFile->Write(SecNode->pSection, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); KeyNode = SecNode->RootNode.pNextNode; while (KeyNode != NULL) { dwStringLen = (DWORD)strlen(KeyNode->pKey); dwWriteSize = piFile->Write(KeyNode->pKey, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write("=", 1); PROCESS_ERROR(dwWriteSize == 1); dwStringLen = (DWORD)strlen(KeyNode->pValue); dwWriteSize = piFile->Write(KeyNode->pValue, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); KeyNode = KeyNode->pNextNode; } dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); SecNode = SecNode->pNextNode; } nResult = true; EXIT0: SAFE_RELEASE(piFile); return nResult; }
VFS_BOOL VFS_File_Write( VFS_Handle hFile, const VFS_BYTE* pBuffer, VFS_DWORD dwToWrite, VFS_DWORD* pWritten ) { // Not initialized yet? if( !IsInit() ) { SetLastError( VFS_ERROR_NOT_INITIALIZED_YET ); return VFS_FALSE; } // Invalid Handle Value? if( hFile == VFS_INVALID_HANDLE_VALUE ) { SetLastError( VFS_ERROR_INVALID_PARAMETER ); return VFS_FALSE; } // Get the File Pointer. IFile* pFile = ( IFile* )( VFS_DWORD )hFile; return pFile->Write( pBuffer, dwToWrite, pWritten ); }
void SuiteFile::TestFunctionality(IFile& aFile, TUint32 aBytes) { Print("Testing File implementation functionality on %d byte file\n", aBytes); // We should be able to set the file cursor to all // bytes, from 0, up to /and including/ aBytes. // i.e. it is valid for the cursor to be pointing off // the end, but not to read data from here. // Test absolute seeking - ensure cursor is changed for // each test. aFile.Seek(0, eSeekFromStart); TEST(aFile.Tell() == 0); aFile.Seek(aBytes, eSeekFromStart); TEST(aFile.Tell() == aBytes); aFile.Seek(-(TInt) aBytes, eSeekFromEnd); TEST(aFile.Tell() == 0); aFile.Seek(0, eSeekFromEnd); TEST(aFile.Tell() == aBytes); // Test boundaries // Backwards at start. TEST_THROWS(aFile.Seek(-1, eSeekFromStart), FileSeekError); // Forwards at end //TEST_THROWS(aFile.Seek(+1, eSeekFromEnd), FileSeekError); // Forwards from start //TEST_THROWS(aFile.Seek(aBytes+1, eSeekFromStart), FileSeekError); // Backwards from end TEST_THROWS(aFile.Seek(-(TInt) (aBytes+1), eSeekFromEnd), FileSeekError); // Reading Bwh buffer(aBytes + 20); aFile.Seek(0, eSeekFromStart); aFile.Read(buffer); TEST(aFile.Tell() == aBytes); TEST(buffer.Bytes() == aBytes); const TUint readBytes = 10; aFile.Seek(0, eSeekFromStart); buffer.SetBytes(0); aFile.Read(buffer, readBytes); TEST(aFile.Tell() == readBytes); TEST(buffer.Bytes() == readBytes); aFile.Seek(-(TInt) (readBytes/2), eSeekFromEnd); buffer.SetBytes(0); aFile.Read(buffer); TEST(aFile.Tell() == aBytes); TEST(buffer.Bytes() == readBytes/2); aFile.Seek(0, eSeekFromEnd); buffer.SetBytes(0); TEST_THROWS(aFile.Read(buffer), FileReadError); // Writing buffer.SetBytes(aBytes/2); TEST_THROWS(aFile.Write(buffer), FileWriteError); Print("\n"); }
int KSystemScriptTable::LuaSaveDataToFile(Lua_State* L) { int nRetCode = false; IFile *piFile = NULL; const char *pcszFile = NULL; const char *pcsBuff = NULL; unsigned uFilePathLength = 0; size_t uBuffSize = 0; int i = 0; char szFile[MAX_PATH]; char szPath[MAX_PATH]; KGLOG_PROCESS_ERROR(L); nRetCode = lua_gettop(L); KGLOG_PROCESS_ERROR(nRetCode == 2); pcsBuff = lua_tolstring(L, 1, &uBuffSize); KGLOG_PROCESS_ERROR(pcsBuff); KGLOG_PROCESS_ERROR(uBuffSize > 0); pcszFile = lua_tostring(L, 2); KGLOG_PROCESS_ERROR(pcszFile); KGLOG_PROCESS_ERROR(pcszFile[0]); uFilePathLength = snprintf( szFile, sizeof(szFile), "%s\\%s", F_UI_USER_DATA_FOLDER, pcszFile ); KGLOG_PROCESS_ERROR(uFilePathLength > 0); szFile[sizeof(szFile) - 1] = '\0'; FormatFilePath(szFile); szPath[0] = '\0'; for (i = uFilePathLength; i >= 0; i--) { if (szFile[i] != '/' && szFile[i] != '\\') continue; ASSERT(i < sizeof(szPath)); strncpy(szPath, szFile, i); szPath[i] = '\0'; break; } nRetCode = KUiConfig::IsFilePathExist(szPath); if (!nRetCode) { nRetCode = KUiConfig::CreatePath(szPath); KGLOG_PROCESS_ERROR(nRetCode); } piFile = g_CreateFile(szFile); KGLOG_PROCESS_ERROR(piFile); nRetCode = piFile->Write(pcsBuff, (unsigned long)uBuffSize); KGLOG_PROCESS_ERROR(nRetCode == uBuffSize); Exit0: SAFE_RELEASE(piFile); return 0; }
void ExportLevelData() { Msg("-------------\nExporting level data\n-------------\n"); // export models if(g_export_models.GetBool()) { Msg("exporting models\n"); for(int i = 0; i < g_levelModels.numElem(); i++) { if(!g_levelModels[i].model) continue; EqString modelFileName = varargs("%s/ZMOD_%d", g_levname_moddir.c_str(), i); if(g_model_names[i].GetLength()) modelFileName = varargs("%s/%d_%s", g_levname_moddir.c_str(), i, g_model_names[i]); // export model ExportDMODELToOBJ(g_levelModels[i].model, modelFileName.c_str(), i); // save original dmodel2 IFile* dFile = GetFileSystem()->Open(varargs("%s.dmodel", modelFileName.c_str()), "wb", SP_ROOT); if(dFile) { dFile->Write(g_levelModels[i].model, g_levelModels[i].size, 1); GetFileSystem()->Close(dFile); } } // create material file IFile* pMtlFile = GetFileSystem()->Open(varargs("%s/MODELPAGES.mtl", g_levname_moddir.c_str()), "wb", SP_ROOT); if(pMtlFile) { for(int i = 0; i < g_numTexPages; i++) { pMtlFile->Print("newmtl page_%d\r\n", i); pMtlFile->Print("map_Kd ../../%s/PAGE_%d.tga\r\n", g_levname_texdir.c_str(), i); } GetFileSystem()->Close(pMtlFile); } } // export car models if(g_export_carmodels.GetBool()) { for(int i = 0; i < MAX_CAR_MODELS; i++) { ExportCarModel(g_carModels[i].cleanmodel, g_carModels[i].cleanSize, i, "clean"); ExportCarModel(g_carModels[i].lowmodel, g_carModels[i].lowSize, i, "low"); // TODO: export damaged model } } // export world region if(g_export_world.GetBool()) { Msg("exporting cell points and world model\n"); ExportRegions(); } if(g_export_textures.GetBool()) { // preload region data if needed if(!g_export_world.GetBool()) { Msg("preloading region datas (%d)\n", g_numRegionDatas); for(int i = 0; i < g_numRegionDatas; i++) { LoadRegionData( g_levStream, NULL, &g_regionDataDescs[i], &g_regionPages[i] ); } } Msg("exporting texture data\n"); for(int i = 0; i < g_numTexPages; i++) { ExportTexturePage(i); } // create material file IFile* pMtlFile = GetFileSystem()->Open(varargs("%s_LEVELMODEL.mtl", g_levname.c_str()), "wb", SP_ROOT); if(pMtlFile) { for(int i = 0; i < g_numTexPages; i++) { pMtlFile->Print("newmtl page_%d\r\n", i); pMtlFile->Print("map_Kd %s_textures/PAGE_%d.tga\r\n", g_levname.Path_Extract_Name().c_str(), i); } GetFileSystem()->Close(pMtlFile); } } Msg("Export done\n"); // create material file IFile* pLevFile = GetFileSystem()->Open(varargs("%s.lev", g_levname.c_str()), "wb"); if(pLevFile) { g_levStream->Seek(g_levSize, VS_SEEK_SET); ((CMemoryStream*)g_levStream)->WriteToFileStream(pLevFile); GetFileSystem()->Close(pLevFile); } }
HRESULT KG3DTerrainRoad::SaveToFile(const char cszFileName[]) { // int nRetCode = false; HRESULT hr =E_FAIL; _RoadHead RoadHead; DWORD PosStart,PosEnd; IFile* pFile = NULL; RoadHead.dwNumOfNode = (DWORD)m_listNode.size(); RoadHead.dwNumOfPassage = (DWORD)m_listPassage.size(); D3DXVECTOR3* pNodePos = new D3DXVECTOR3[RoadHead.dwNumOfNode]; list<KG3DRepresentObjectNode*>::iterator iNode; DWORD k = 0; pFile = g_OpenFile(cszFileName,false,true); KG_PROCESS_ERROR(pFile); PosStart = pFile->Tell();//ftell(pFile); iNode = m_listNode.begin(); while(iNode != m_listNode.end()) { (*iNode)->GetTranslation(&pNodePos[k]);//pNodePos[i] = (*iNode)->vPosition; k++; ++iNode; } RoadHead.fBendModulus = m_fBendModulus; RoadHead.fBlendLength = m_fBlendLength; RoadHead.fEdgeModulus = m_fEdgeModulus; RoadHead.fWidth = m_fWidth; RoadHead.fTexDensity = m_fTexDensity; RoadHead.nID = m_nID; RoadHead.fNodeSize = m_fNodeSize; RoadHead.dwSegmentLength= m_nSegmentLength; RoadHead.dwNumParentBlock = (DWORD)m_vecParentTerrainBlock.size(); RoadHead.dwParentTerrainBlock = PosStart + sizeof(_RoadHead); RoadHead.dwNodePosBlock = PosStart + sizeof(_RoadHead) + sizeof(TerrainBlockInfo) * RoadHead.dwNumParentBlock ; RoadHead.dwPassageBlock = PosStart + sizeof(_RoadHead) + sizeof(TerrainBlockInfo) * RoadHead.dwNumParentBlock + sizeof(D3DXVECTOR3)*RoadHead.dwNumOfNode; strncpy(RoadHead.scName,m_scName.c_str(),sizeof(TCHAR)*32); strncpy(RoadHead.scTextureName,m_scTextureName.c_str(),sizeof(TCHAR)*MAX_PATH); pFile->Write(&RoadHead, sizeof(_RoadHead)); for (int nT= 0; nT < (int)RoadHead.dwNumParentBlock; nT++) { TerrainBlockInfo TBI = m_vecParentTerrainBlock[nT]; pFile->Write(&TBI,sizeof(TerrainBlockInfo)); } pFile->Write(pNodePos,sizeof(D3DXVECTOR3)*RoadHead.dwNumOfNode); { list<KG3DPassage*>::iterator i = m_listPassage.begin(); while ( i != m_listPassage.end()) { KG3DTerrainRoadPassage* pPassage = static_cast<KG3DTerrainRoadPassage*>(*i); pPassage->WriteDataToFile(pFile); ++i; } } PosEnd = pFile->Tell();//ftell(pFile); hr = S_OK; Exit0: KG_COM_RELEASE(pFile); SAFE_DELETE_ARRAY(pNodePos); return hr; }
bool sMap::Save(const char* filename, IFileSystem& fs) { // do some preliminary checking... // the start layer should not have parallax m_Layers[m_StartLayer].EnableParallax(false); IFile* file = fs.Open(filename, IFileSystem::write); if (file == NULL) return false; // write the map header MAP_HEADER header; memset(&header, 0, sizeof(header)); memcpy(header.signature, ".rmp", 4); header.version = 1; header.num_layers = m_Layers.size(); header.num_entities = m_Entities.size(); header.startx = m_StartX; header.starty = m_StartY; header.startlayer = m_StartLayer; header.startdirection = m_StartDirection; header.num_strings = 9; file->Write(&header, sizeof(header)); // write the strings WriteMapString(file, ""); // OBSOLETE WriteMapString(file, m_MusicFile.c_str()); WriteMapString(file, ""); // OBSOLETE WriteMapString(file, m_EntryScript.c_str()); WriteMapString(file, m_ExitScript.c_str()); WriteMapString(file, m_EdgeScripts[0].c_str()); WriteMapString(file, m_EdgeScripts[1].c_str()); WriteMapString(file, m_EdgeScripts[2].c_str()); WriteMapString(file, m_EdgeScripts[3].c_str()); // write layers for (int i = 0; i < m_Layers.size(); i++) { const sLayer& layer = m_Layers[i]; const sObstructionMap& obstructions = layer.GetObstructionMap(); // write the header LAYER_HEADER lh; memset(&lh, 0, sizeof(lh)); lh.width = m_Layers[i].GetWidth(); lh.height = m_Layers[i].GetHeight(); lh.flags = (m_Layers[i].IsVisible() ? 0 : 1) | (m_Layers[i].HasParallax() ? 2 : 0); lh.parallax_x = m_Layers[i].GetXParallax(); lh.parallax_y = m_Layers[i].GetYParallax(); lh.scrolling_x = m_Layers[i].GetXScrolling(); lh.scrolling_y = m_Layers[i].GetYScrolling(); lh.num_segments = obstructions.GetNumSegments(); lh.reflective = (m_Layers[i].IsReflective() ? 1 : 0); file->Write(&lh, sizeof(lh)); // write the layer name WriteMapString(file, m_Layers[i].GetName()); // write the layer data for (int iy = 0; iy < m_Layers[i].GetHeight(); iy++) for (int ix = 0; ix < m_Layers[i].GetWidth(); ix++) { word w = m_Layers[i].GetTile(ix, iy); file->Write(&w, 2); } // write the obstruction map for (int i = 0; i < obstructions.GetNumSegments(); i++) { const sObstructionMap::Segment& s = obstructions.GetSegment(i); dword x1 = s.x1; dword y1 = s.y1; dword x2 = s.x2; dword y2 = s.y2; file->Write(&x1, sizeof(dword)); file->Write(&y1, sizeof(dword)); file->Write(&x2, sizeof(dword)); file->Write(&y2, sizeof(dword)); } } // end for layer // write entities for (int i = 0; i < m_Entities.size(); i++) { // write the header ENTITY_HEADER eh; memset(&eh, 0, sizeof(eh)); eh.mapx = m_Entities[i]->x; eh.mapy = m_Entities[i]->y; eh.layer = m_Entities[i]->layer; switch (m_Entities[i]->GetEntityType()) { case sEntity::PERSON: eh.type = 1; break; case sEntity::TRIGGER: eh.type = 2; break; } file->Write(&eh, sizeof(eh)); // write the entity data switch (m_Entities[i]->GetEntityType()) { case sEntity::PERSON: { sPersonEntity* person = (sPersonEntity*)m_Entities[i]; WriteMapString(file, person->name.c_str()); WriteMapString(file, person->spriteset.c_str()); WriteMapWord(file, 5); // four scripts // scripts WriteMapString(file, person->script_create.c_str()); WriteMapString(file, person->script_destroy.c_str()); WriteMapString(file, person->script_activate_touch.c_str()); WriteMapString(file, person->script_activate_talk.c_str()); WriteMapString(file, person->script_generate_commands.c_str()); // reserved for (int i = 0; i < 16; i++) { WriteMapByte(file, 0); } break; } case sEntity::TRIGGER: { sTriggerEntity* trigger = (sTriggerEntity*)m_Entities[i]; WriteMapString(file, trigger->script.c_str()); break; } } // end switch entity type } // end for entity // write the zones for (int i = 0; i < m_Zones.size(); i++) { ZONE_HEADER zh; memset(&zh, 0, sizeof(zh)); zh.x1 = m_Zones[i].x1; zh.y1 = m_Zones[i].y1; zh.x2 = m_Zones[i].x2; zh.y2 = m_Zones[i].y2; zh.layer = m_Zones[i].layer; zh.reactivate_in_num_steps = m_Zones[i].reactivate_in_num_steps; file->Write(&zh, sizeof(zh)); WriteMapString(file, m_Zones[i].script.c_str()); } // end for zones // save the tileset if (!m_Tileset.SaveToFile(file)) { file->Close(); return false; } file->Close(); return true; }
int main(int argc, char* argv[]) { if (argc <= 2) { printf("Usage: resourcepacker.exe [-d] [-u] [resourceDirectoryPath] [archiveName]\n"); printf("Written by Borodovsky Vitaliy (C) 2006.\n"); printf("Example:\n"); printf("resourcepacker.exe -d data data.re\n\t- pack directory data to archive data.re\n"); printf("resourcepacker.exe -u data data.re\n\t- unpack archive data.re to directory data\n"); printf("Options:\n"); printf("\t -d don't include to archive relative pathnames\n"); printf("\t -u unpack archive with contents\n"); return 0; } bool packPaths = true; bool showMode = false; String directoryPath; String archiveName; String headerName; int32 pathIndex = 1; for (int flag = 0; flag < 2; ++flag) { if (String(argv[pathIndex]) == "-d") { pathIndex++; packPaths = false; } else if (String(argv[pathIndex]) == "-u") { pathIndex++; showMode = true; } } // IEngineSystem * es = CreateConsoleEngine(); new AnsiFileSystem(); IFileSystem * fs = Singleton<IO::IFileSystem>::Instance(); String programmPath = fs->GetCurrentDirectory(); if (showMode) // show mode of ResourcePacker tool { directoryPath = argv[pathIndex]; if (directoryPath.find(':') == -1) { directoryPath = programmPath + "/" + directoryPath; } if ((pathIndex + 1) > (argc - 1)) { String::size_type pos = directoryPath.rfind('/'); if (pos == -1) { archiveName = directoryPath + ".re"; }else { archiveName = directoryPath.substr(pos + 1) + ".re"; } }else { archiveName = argv[pathIndex + 1]; String::size_type pos = archiveName.rfind('.'); if (pos == -1) { headerName = archiveName; archiveName += ".re"; headerName += ".h"; } } printf("===================================================\n"); printf("=== Unpacker started\n"); printf("=== Unpack directory: %s\n", directoryPath.c_str()); printf("=== Unpack archiveName: %s\n", archiveName.c_str()); printf("===================================================\n"); fs->CreateDirectory(directoryPath.c_str()); ResourceArchive ra; ra.Open(programmPath + "/" + archiveName); for (int file = 0; file < ra.GetFileCount(); ++file) { String pathName = ra.GetResourcePathname(file); pathName = directoryPath + pathName; String::size_type pos = pathName.rfind('/'); if (pos == -1) { }else { String dirCreate = pathName.substr(0, pos); fs->CreateDirectory(dirCreate.c_str()); } int32 size = ra.LoadResource(file, 0); uint8 * data = new uint8[size]; ra.LoadResource(file, data); IFile * unpackFile = fs->CreateFile(pathName.c_str(), IO::EFA_CREATE | IO::EFA_WRITE); if (unpackFile) { unpackFile->Write(data, size); SafeRelease(unpackFile); } else { } delete [] data; } }else // archive mode of ResourcePacker tool { directoryPath = argv[pathIndex]; if (directoryPath.find(':') == -1) { directoryPath = programmPath + "/" + directoryPath; } if ((pathIndex + 1) > (argc - 1)) { String::size_type pos = directoryPath.rfind('/'); if (pos == -1) { archiveName = directoryPath + ".re"; headerName = directoryPath + ".h"; }else { archiveName = directoryPath.substr(pos + 1) + ".re"; headerName = directoryPath.substr(pos + 1) + ".h"; } }else { archiveName = argv[pathIndex + 1]; String::size_type pos = archiveName.rfind('.'); if (pos == -1) { headerName = archiveName; archiveName += ".re"; headerName += ".h"; }else { headerName = archiveName.substr(0, pos) + ".h"; } } printf("===================================================\n"); printf("=== Packer started\n"); printf("=== Include relative filepaths %s\n", (packPaths == true)? ("enabled"):("disabled")); printf("=== Pack directory: %s\n", directoryPath.c_str()); printf("=== Pack archiveName: %s\n", archiveName.c_str()); printf("=== Pack headerName: %s\n", headerName.c_str()); printf("===================================================\n"); Packer * packer = new Packer(programmPath + "/" + archiveName, directoryPath, programmPath + "/" + headerName); packer->Pack(packPaths); delete packer; packer = 0; } // SafeRelease(es); return 0; }