int KGFileManager::CompareFileByDataStream(const char cszFileName_1[], const char cszFileName_2[]) { int nResult = false; int nRetCode = false; IFile* pFile_1 = NULL; IFile* pFile_2 = NULL; BYTE byData_1[SIZE_READ_BUFFER] = {0}; BYTE byData_2[SIZE_READ_BUFFER] = {0}; char szResult[2 * MAX_PATH] = {0}; pFile_1 = g_OpenFile(cszFileName_1); KGLOG_PROCESS_ERROR(pFile_1); pFile_2 = g_OpenFile(cszFileName_2); KGLOG_PROCESS_ERROR(pFile_2); if (pFile_1->Size() != pFile_2->Size()) { nRetCode = _snprintf_s( szResult, sizeof(szResult), sizeof(szResult) - 1, "Size Differ. File_1=%s File_2=%s", cszFileName_1, cszFileName_2 ); KGLOG_PROCESS_ERROR(nRetCode > 0); KGLogPrintf(KGLOG_ERR, szResult); KGLOG_PROCESS_ERROR(false); } for (ULONG i = 0; i < pFile_1->Size(); i += SIZE_READ_BUFFER) { pFile_1->Read(byData_1, SIZE_READ_BUFFER); pFile_2->Read(byData_2, SIZE_READ_BUFFER); nRetCode = memcmp(byData_1, byData_2, SIZE_READ_BUFFER); if (nRetCode != 0) { nRetCode = _snprintf_s( szResult, sizeof(szResult), sizeof(szResult) - 1, "File Data Differ. File_1=%s File_2=%s", cszFileName_1, cszFileName_2 ); KGLOG_PROCESS_ERROR(nRetCode > 0); KGLogPrintf(KGLOG_ERR, szResult); KGLOG_PROCESS_ERROR(false); } } nResult = true; Exit0: KG_COM_RELEASE(pFile_1); KG_COM_RELEASE(pFile_2); return nResult; }
bool TiXmlDocument::LoadFile( const char* filename, TiXmlEncoding encoding ) { // There was a really terrifying little bug here. The code: // value = filename // in the STL case, cause the assignment method of the std::string to // be called. What is strange, is that the std::string had the same // address as it's c_str() method, and so bad things happen. Looks // like a bug in the Microsoft STL implementation. // See STL_STRING_BUG above. // Fixed with the StringToBuffer class. value = filename; // reading in binary mode so that tinyxml can normalize the EOL IFile* file = g_OpenFile( value.c_str () ); if ( file ) { bool result = LoadBuffer( (LPCSTR)file->GetBuffer(), file->Size(), encoding ); file->Release(); return result; } else { SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN ); return false; } }
void KG3DFontTexture::LoadAlphaAdjustTable(LPCSTR szFontName, INT nFontSize) { BOOL bAdjust = FALSE; char szFileName[MAX_PATH]; LPSTR szSep = NULL; int nLen = _countof(szFileName) - 16; strncpy(szFileName, szFontName, nLen); szFileName[nLen - 1] = '\0'; szSep = strrchr(szFileName, '.'); if (szSep) { sprintf(szSep, "%d.prf", nFontSize); if (g_IsFileExist(szFileName)) { IFile* pFile = g_OpenFile(szFileName); if (pFile) { unsigned int nCount = pFile->Read(m_uchAdjustBuffer, sizeof(m_uchAdjustBuffer)); if (nCount == sizeof(m_uchAdjustBuffer)) bAdjust = TRUE; KG_COM_RELEASE(pFile); } } } if (!bAdjust) { for (size_t nIndex = 0; nIndex < _countof(m_uchAdjustBuffer); ++nIndex) { m_uchAdjustBuffer[nIndex] = (UCHAR)nIndex; } } }
IIniFile* g_OpenIniFile(const char* FileName, int ForceUnpakFile/* = false*/, int ForWrite/* = false*/) { QIniFileImpl* pIni = NULL; IFile* pFile = g_OpenFile(FileName, ForceUnpakFile, ForWrite); if (pFile) { pIni = QIniFileImpl::New(); if (pIni) { if (!pIni->LoadData(pFile)) { pIni->Release(); pIni = NULL; } } pFile->Release(); pFile = NULL; } if (pIni == NULL && ForWrite) pIni = QIniFileImpl::New(); return pIni; }
BOOL KSkillManager::LoadSkillBinaryData(KMAP_SKILL_DATA& allBinarySkillData) { BOOL bResult = false; BOOL bRetCode = false; IFile* piFile = NULL; BYTE* pBuffer = NULL; size_t uBufferSize = 0; uint64_t uSkillKey = 0; KSkillBinaryData* pBinaryData = NULL; KAnchor* pAnchor = NULL; KSkillSegmentInfo* pSegmentInfo = NULL; KSkillLogicInfo* pLogicData = NULL; unsigned int uEnd = 0; BYTE* pBufferEnd = NULL; piFile = g_OpenFile(SETTING_DIR"/SkillAction.skilllogic"); KGLOG_PROCESS_ERROR(piFile); pBuffer = (BYTE*)piFile->GetBuffer(); KGLOG_PROCESS_ERROR(pBuffer); uBufferSize = piFile->Size(); KGLOG_PROCESS_ERROR(uBufferSize); bRetCode = g_DoBufferSkip(pBuffer, uBufferSize, sizeof(KSkillLogicFileHeader)); KG_PROCESS_ERROR(bRetCode); while (uBufferSize) { bRetCode = g_ReadFromBufferTo(pBuffer, uBufferSize, uEnd); KGLOG_PROCESS_ERROR(bRetCode); pBufferEnd = pBuffer + uEnd; bRetCode = g_ReadFromBufferAs(pBuffer, uBufferSize, pLogicData); KGLOG_PROCESS_ERROR(bRetCode); uSkillKey = MAKE_INT64(pLogicData->skillID, pLogicData->skillStep); pBinaryData = &allBinarySkillData[uSkillKey]; pBinaryData->dwID = pLogicData->skillID; pBinaryData->dwStep = pLogicData->skillStep; pBinaryData->nTotalFrame = pLogicData->frameCount; bRetCode = LoadAnchors(pBuffer, uBufferSize, pBinaryData->Anchors); KGLOG_PROCESS_ERROR(bRetCode); bRetCode = LoadSegments(pBuffer, uBufferSize, pBinaryData->Segments); KGLOG_PROCESS_ERROR(bRetCode); KGLOG_PROCESS_ERROR(pBufferEnd == pBuffer); } bResult = true; Exit0: KG_COM_RELEASE(piFile); return bResult; }
bool KLuaWrap::DoScriptFile(const char *ScriptFile, const char *EnvName /*= NULL*/) { bool ret = false; assert(m_L); if (!(ScriptFile && ScriptFile[0])) return false; IFile* pFile = NULL; char* pBuffer = NULL; unsigned int size; if ((pFile = g_OpenFile(ScriptFile)) == NULL) { printf("%s is not exist!\n", ScriptFile); KGLogPrintf(KGLOG_WARNING, "[Lua] %s is not exist!\n", ScriptFile); return false; } size = pFile->Size(); pBuffer = (char*)malloc(size + 1024); if (pBuffer) { char tmp[128]; int pos = 0; int tmpLen = sprintf(tmp, "package.path = \"%s?.lua\"", m_ScriptPackagePath); #ifdef WIN32 for (int i = 0; i < tmpLen; ++i) { pBuffer[pos++] = tmp[i]; if (tmp[i] == '\\') { pBuffer[pos++] = '\\'; } } #endif if (EnvName && EnvName[0]) pos += sprintf(pBuffer + pos, "%s={} setmetatable(%s,{__index=_G,__newindex=function(t,i,v)for _,r in pairs(_RESERVE_G_VAR_)do if i==r then _G[i] = v return end end rawset(t,i,v)end}) setfenv(1,%s)", EnvName, EnvName, EnvName); if (pFile->Read(pBuffer + pos, size) == size) { ret = lua_tinker::dobuffer(m_L, pBuffer, size + pos, ScriptFile); if (ret && EnvName && EnvName[0]) { lua_getglobal(m_L, EnvName); lua_pushstring(m_L, "CURRENT_SCRIPT_FILE"); lua_pushstring(m_L, ScriptFile); lua_settable(m_L, -3); lua_pop(m_L, 1); } } free(pBuffer); pBuffer = NULL; } pFile->Release(); pFile = NULL; return ret; }
void KLuaWrap::LoadJitOpt() { IFile* pFile = NULL; if ((pFile = g_OpenFile("\\script\\lib\\jit\\opt.lua")) == NULL) { lua_tinker::print_error(m_L, "\\script\\lib\\jit\\opt.lua is not exist!\n"); return; } if (luaL_loadbuffer(m_L, (const char*)pFile->GetBuffer(), pFile->Size(), "load jit opt")) { lua_tinker::print_error(m_L, "%s", lua_tostring(m_L, -1)); lua_pop(m_L, 1); return; } pFile->Release(); if ((pFile = g_OpenFile("\\script\\lib\\jit\\opt_inline.lua")) == NULL) { lua_tinker::print_error(m_L, "\\script\\lib\\jit\\opt_inline.lua is not exist!\n"); return; } if (luaL_loadbuffer(m_L, (const char*)pFile->GetBuffer(), pFile->Size(), "load jit opt_inline")) { lua_tinker::print_error(m_L, "%s", lua_tostring(m_L, -1)); lua_pop(m_L, 1); return; } pFile->Release(); lua_getglobal(m_L, "package"); lua_getfield(m_L, -1, "preload"); lua_insert(m_L, 1); lua_pop(m_L, 1); lua_setfield(m_L, 1, "jit.opt_inline"); lua_setfield(m_L, 1, "jit.opt"); lua_pop(m_L, 1); }
int KSystemScriptTable::LuaLoadDataFromFile(Lua_State* L) { int nRetCode = false; IFile *piFile = NULL; const char *pcszFile = NULL; const char *pcsBuff = NULL; size_t uBuffSize = 0; int nParamCount = 0; char szFilePath[MAX_PATH]; KGLOG_PROCESS_ERROR(L); nRetCode = lua_gettop(L); KGLOG_PROCESS_ERROR(nRetCode == 1); pcszFile = lua_tostring(L, 1); KGLOG_PROCESS_ERROR(pcszFile); nRetCode = snprintf( szFilePath, sizeof(szFilePath), "%s\\%s", F_UI_USER_DATA_FOLDER, pcszFile ); KGLOG_PROCESS_ERROR(nRetCode > 0); szFilePath[sizeof(szFilePath) - 1] = '\0'; FormatFilePath(szFilePath); nRetCode = KUiConfig::IsFilePathExist(szFilePath); KG_PROCESS_SUCCESS(!nRetCode); piFile = g_OpenFile(szFilePath, true); KGLOG_PROCESS_ERROR(piFile); uBuffSize = piFile->Size(); KGLOG_PROCESS_ERROR(uBuffSize > 0); pcsBuff = (const char *)piFile->GetBuffer(); KGLOG_PROCESS_ERROR(pcsBuff); lua_pushlstring(L, pcsBuff, uBuffSize); nParamCount++; Exit1: Exit0: SAFE_RELEASE(piFile); return nParamCount; }
//--------------------------------------------------------------------------- // 函数: Mp3FileOpen // 功能: // 参数: FileName // 返回: BOOL //--------------------------------------------------------------------------- BOOL KMp3Music::Mp3FileOpen(LPSTR FileName) { ASSERT(m_pMp3File == NULL); m_pMp3File = g_OpenFile(FileName, false, false); if (m_pMp3File==NULL) { m_szMp3FilePath[0] = '\0'; return FALSE; } else { strcpy(m_szMp3FilePath, FileName); return TRUE; } }
BOOL File::Open(CPCCHAR cpcFile, CPCCHAR cpcMode) { CHECK_C_STRING_RETURN_BOOL(cpcFile); CHECK_C_STRING_RETURN_BOOL(cpcMode); INT nReCode = 0; CHECK_RETURN_BOOL(m_hFile == NULL); nReCode = g_OpenFile(m_hFile, cpcFile, cpcMode); CHECK_RETURN_BOOL(nReCode); if (::strchr(cpcMode, 'b')) m_bBinaryFile = TRUE; else m_bBinaryFile = FALSE; return TRUE; }
C_ENGINE_API ITabFile* g_OpenTabFile(const char* FileName, int ForceUnpakFile/* = false*/, int ForWrite/* = false*/) { ITabFile* pTab = NULL; IFile* pFile = g_OpenFile(FileName, ForceUnpakFile, ForWrite); int nResult = false; if (pFile) { if (!ForWrite) { if ((pTab = KTabFile::New()) != NULL) nResult = ((KTabFile*)pTab)->LoadData(pFile); } else if ((pTab = KTabFileWritable::New()) != NULL) { nResult = ((KTabFileWritable*)pTab)->LoadData(pFile); } } if (!nResult) SAFE_RELEASE(pTab); SAFE_RELEASE(pFile); return pTab; }
int TestLoadAllBSP() { int nResult = false; int nRetCode = false; HRESULT hrRetCode = E_FAIL; unsigned uTotalCount = 0; unsigned uFailedCount = 0; unsigned uFailedCountSave = 0; BYTE *pbyTestFileData = NULL; BYTE *pbySourceFileData = NULL; IIniFile *pFile = NULL; IFile *piTestFile = NULL; IKG3DModel *piModel = NULL; IKG3DResourceManager *piModelTable = NULL; char szMessage[MAX_PATH] = ""; char szDirPath[MAX_PATH] = ""; TCHAR strBSPPathName[MAX_PATH] = ""; //vector<string> vecModelList; //vector<string> vecMeshList; MSG msg; nRetCode = GetCurrentDirectory(MAX_PATH, szDirPath); KG_PROCESS_ERROR(nRetCode > 0 && nRetCode < MAX_PATH + 1); strcat(szDirPath, "\\data\\source\\*.*"); szDirPath[MAX_PATH - 1] = '\0'; //strcpy(szDirPath, "D:\\Kingsoft\\Game\\sword3-products\\trunk\\client\\data\\source\\NPC_source\\*.*"); piModelTable = g_cEngineManager.Get3DModelTable(); KGLOG_PROCESS_ERROR(piModelTable); pFile = g_OpenIniFile(g_cszErrorFileName[EXT_MESH],false,true); KGLOG_PROCESS_ERROR(pFile); nRetCode = GetFileList(szDirPath, &g_vecSourceList/*vecMeshList*/, EXT_MESH); KGLOG_PROCESS_ERROR(nRetCode); //g_vecSourceList.push_back("D:\\Kingsoft\\Game\\trunk\\data\\source\\maps_source\\亭塔\\T_erg风车塔001A.Mesh"); uTotalCount = (unsigned)/*vecMeshList*/g_vecSourceList.size(); for (unsigned i = 0; i < uTotalCount; ++i) { char szKeyName[32] = ""; snprintf( szMessage, sizeof(szMessage), "Mesh模型的BSP文件检查:%s", /*vecMeshList*/g_vecSourceList[i].c_str() ); szMessage[sizeof(szMessage) - 1] = '\0'; SetWindowText(g_hWnd, szMessage); TCHAR strDriver[MAX_PATH]; TCHAR strPath[MAX_PATH]; TCHAR strFile[MAX_PATH]; _splitpath_s(g_vecSourceList[i].c_str(), strDriver, MAX_PATH, strPath, MAX_PATH, strFile, MAX_PATH, NULL, 0); //sprintf_s(strBSPPathName, // MAX_PATH, // "%s%s%s.bsp", // strDriver, // strPath, // strFile); //nRetCode = g_IsFileExist(strBSPPathName); //if (!nRetCode) //{ // /*itoa(uFailedCount, szKeyName, 10); // pFile->WriteString("BSP_NotExited",szKeyName, g_vecSourceList[i].c_str()); // ++uFailedCount;*/ // continue; //} try { g_bForceGenerateBspForMesh = /*FALSE*/TRUE; hrRetCode = piModelTable->LoadResourceFromFile(/*vecMeshList*/g_vecSourceList[i].c_str(), 0, 0, (IKG3DResourceBase**)&piModel); if (FAILED(hrRetCode)) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("ErrorMesh",szKeyName, /*vecMeshList*/g_vecSourceList[i].c_str()); ++uFailedCount; } KG_COM_RELEASE(piModel); g_cEngineManager.FrameMove(); g_bForceGenerateBspForMesh = FALSE; if (FAILED(hrRetCode)) { continue; } //cmp unsigned uTestFileDataSize = 0; unsigned uSourceFileDataSize = 0; unsigned uSizeRead = 0; sprintf_s(strBSPPathName, MAX_PATH, "%s%s%s.bsp", strDriver, strPath, strFile); nRetCode = g_IsFileExist(strBSPPathName); if (!nRetCode) { sprintf_s(strBSPPathName, MAX_PATH, "%s%s%s_test.bsp", strDriver, strPath, strFile); nRetCode = g_IsFileExist(strBSPPathName); if (nRetCode) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("BSP_NotGenerate",szKeyName, g_vecSourceList[i].c_str()); ++uFailedCount; DeleteFile(strBSPPathName); } continue; } piTestFile = g_OpenFile(strBSPPathName); KGLOG_PROCESS_ERROR(piTestFile); uSourceFileDataSize = piTestFile->Size(); KGLOG_PROCESS_ERROR(uSourceFileDataSize > sizeof(DWORD)); pbySourceFileData = new BYTE[uSourceFileDataSize]; KGLOG_PROCESS_ERROR(pbySourceFileData); uSizeRead = piTestFile->Read(pbySourceFileData, uSourceFileDataSize); KGLOG_PROCESS_ERROR(uSizeRead == uSourceFileDataSize); KG_COM_RELEASE(piTestFile); sprintf_s(strBSPPathName, MAX_PATH, "%s%s%s_test.bsp", strDriver, strPath, strFile); piTestFile = g_OpenFile(strBSPPathName); if (!piTestFile) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("BSP_Don't Exited",szKeyName, g_vecSourceList[i].c_str()); ++uFailedCount; SAFE_DELETE_ARRAY(pbySourceFileData); continue; } uTestFileDataSize = piTestFile->Size(); KGLOG_PROCESS_ERROR(uTestFileDataSize > sizeof(DWORD)); pbyTestFileData = new BYTE[uTestFileDataSize]; KGLOG_PROCESS_ERROR(pbyTestFileData); uSizeRead = piTestFile->Read(pbyTestFileData, uTestFileDataSize); KGLOG_PROCESS_ERROR(uSizeRead == uTestFileDataSize); KG_COM_RELEASE(piTestFile); if (uTestFileDataSize != uSourceFileDataSize) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("BSP_NotMatchedSize",szKeyName, g_vecSourceList[i].c_str()); ++uFailedCount; DeleteFile(strBSPPathName); SAFE_DELETE_ARRAY(pbyTestFileData); SAFE_DELETE_ARRAY(pbySourceFileData); continue; } nRetCode = memcmp(pbyTestFileData, pbySourceFileData, uSourceFileDataSize); if (nRetCode) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("BSP_NotMatched",szKeyName, g_vecSourceList[i].c_str()); ++uFailedCount; DeleteFile(strBSPPathName); SAFE_DELETE_ARRAY(pbyTestFileData); SAFE_DELETE_ARRAY(pbySourceFileData); continue; } DeleteFile(strBSPPathName); SAFE_DELETE_ARRAY(pbyTestFileData); SAFE_DELETE_ARRAY(pbySourceFileData); } catch (...) { itoa(uFailedCount, szKeyName, 10); pFile->WriteString("Mesh_Exception",szKeyName, /*vecMeshList*/g_vecSourceList[i].c_str()); ++uFailedCount; } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { switch (msg.message) { case WM_QUIT: break; default: TranslateMessage(&msg); DispatchMessage(&msg); } } if (msg.message == WM_QUIT) break; } //uFailedCount += uFailedCountSave; //uTotalCount = (unsigned)(vecModelList.size() + vecMeshList.size()); // snprintf( // szMessage, sizeof(szMessage), // "模型加载检查完成:共检查模型 %u 个 已发现错误 %u 个,请检查文件%s", // uTotalCount, uFailedCount, g_cszErrorFileName[TYPE_MDL] //); //szMessage[sizeof(szMessage) - 1] = '\0'; //::MessageBox(g_hWnd, szMessage, "检查报告", MB_OK); nResult = true; Exit0: if (pFile) { pFile->WriteInteger("ErrorMesh", "ErrorNum", uFailedCount); pFile->Save(g_cszErrorFileName[EXT_MESH]); } SAFE_DELETE_ARRAY(pbyTestFileData); SAFE_DELETE_ARRAY(pbySourceFileData); if (strstr(strBSPPathName, "_test.bsp")) { DeleteFile(strBSPPathName); } KG_COM_RELEASE(piModel); KG_COM_RELEASE(pFile); KG_COM_RELEASE(piTestFile); g_bForceGenerateBspForMesh = FALSE; g_vecSourceList.clear(); return nResult; }
int KGTestMapDisuseResource::FindResInPVS(const char cszResourceName[], set<string>& setResList) { //参照KG3DRepresentObjectPVS::LoadPvsFile int nRetCode = false; int nResult = false; HRESULT hrRetCode = E_FAIL; IFile* pFile = NULL; DWORD dwVersion = PVS_FILE_VERSION; char szSourcePathName[MAX_PATH] = {0}; char szResName[MAX_PATH] = {0}; long lOffset = 0; size_t objNum = 0; KG_ASSERT_EXIT(cszResourceName); KGLOG_PROCESS_ERROR(cszResourceName[0] != '\0'); nRetCode = _snprintf_s(szSourcePathName, sizeof(szSourcePathName), sizeof(szSourcePathName) - 1, "%s%s", m_szClientPath, cszResourceName); KGLOG_PROCESS_ERROR(nRetCode > 0); pFile = g_OpenFile(cszResourceName, false, false); KGLOG_PROCESS_ERROR(pFile); /* read version */ pFile->Read(&dwVersion, sizeof(dwVersion)); /* load border, the frist model of the pvs object is it's ouside border and the second model is it's inside border */ pFile->Read(szResName, sizeof(szResName)); FindResource(szResName, setResList); pFile->Read(szResName, sizeof(szResName)); FindResource(szResName, setResList); /* load inside objects, the object may be is pvs object too */ pFile->Read(&objNum, sizeof(objNum)); for (size_t i = 0; i < objNum; i++) { DWORD dwObjType = 0; pFile->Read(&dwObjType, sizeof(dwObjType)); switch (dwObjType) { case REPRESENTOBJECT_PVS : case REPRESENTOBJECT_SET : { pFile->Read(szResName, sizeof(szResName)); FindResource(szResName, setResList); } break; case REPRESENTOBJECT_DEFAULT : { /* is normal object */ DWORD dwCount = 0; DWORD dwColor = 0; DWORD dwType = 0; char szAnins[MAX_PATH] = {0}; pFile->Read(&dwCount, sizeof(dwCount)); for (DWORD i = 0; i < dwCount; i++) { pFile->Read(szResName, sizeof(szResName)); FindResource(szResName, setResList); if (dwVersion > PVS_FILE_VERSION_1) { pFile->Read(szAnins, sizeof(szAnins)); FindResource(szAnins, setResList); } TypeInfo* pInfo = NULL; IEKG3DEngineManager* pIEEngineMgr = NULL; IEKG3DModelTable* pIEModelTable = NULL; KG3DModelTable* pModelTable = NULL; pIEEngineMgr = (IEKG3DEngineManager*)m_pEngineMgr; hrRetCode = pIEEngineMgr->GetIEKG3DModelTable(&pIEModelTable); KGLOG_COM_PROCESS_ERROR(hrRetCode); pModelTable= (KG3DModelTable*)pIEModelTable; hrRetCode = pModelTable->GetTypeInfoByFileName(&pInfo, szResName); if (pInfo) dwType = pInfo->dwType; else dwType = MESHTYPE_DEFAULT; switch (dwType) { case MESHTYPE_POINTLIGHT : { pFile->Read(&dwColor, sizeof(dwColor)); } break; default : break; } } } break; default : _ASSERTE(false); break; } lOffset = sizeof(D3DXVECTOR3) + //ScalingCenter sizeof(D3DXQUATERNION) + //ScalingRotation sizeof(D3DXVECTOR3) + //Scaling sizeof(D3DXVECTOR3) + //RotationCenter sizeof(D3DXQUATERNION) + //Rotation sizeof(D3DXVECTOR3) + //Translation sizeof(AABBOX); //aabox pFile->Seek(lOffset, SEEK_CUR); lOffset = 0; } pFile->Close(); SAFE_RELEASE(pFile); nResult = true; Exit0: if (pFile) { pFile->Close(); SAFE_RELEASE(pFile); } return nResult; }
int KGTestMapDisuseResource::FindResInSFX(const char cszResourceName[], set<string>& setResList) { //参照KG3DSFX::_LoadFromFile int nResult = false; HRESULT hRetCode = E_FAIL; DWORD dwSize = 0; SFX_FILE_HEADER* pHeader = NULL; DWORD fileLen = 0; BYTE* pBuffer = NULL; DWORD dwCurPos = 0; DWORD i = 0; IFile* pFile = NULL; KG_ASSERT_EXIT(cszResourceName); KGLOG_PROCESS_ERROR(cszResourceName[0] != '\0'); pFile = g_OpenFile(cszResourceName); KG_PROCESS_ERROR(pFile); fileLen = pFile->Size(); ASSERT(fileLen > 0); KG_PROCESS_ERROR(fileLen); pBuffer = new BYTE[fileLen]; KG_PROCESS_ERROR(pBuffer); dwSize = pFile->Read(pBuffer, fileLen); KG_PROCESS_ERROR(dwSize == fileLen); pHeader = reinterpret_cast<SFX_FILE_HEADER*>(pBuffer); KG_PROCESS_ERROR(pHeader->dwID == SFX_ID); KG_PROCESS_ERROR(pHeader->dwFileLength == fileLen); KG_PROCESS_ERROR(LOWORD(pHeader->dwVersion) >= 2); dwCurPos += sizeof(SFX_FILE_HEADER); for (i = 0; i < pHeader->dwElementNum; ++i, dwCurPos += sizeof(SFX_ELEMENT)) { SFX_ELEMENT *pElem = reinterpret_cast<SFX_ELEMENT*>(&pBuffer[dwCurPos]); ASSERT(dwCurPos < fileLen); KG_PROCESS_ERROR( (pElem->dwElementID >= SFX_FEID_GENERAL_INFORMATION) && (pElem->dwElementID < SFX_FEID_SIZE) ); ASSERT(pElem->dwElementOffset <= fileLen); /* pElem->dwElementID对应于 KG3DSFX::ProcessBlockFunction KG3DSFX::ms_pfnProcessBlock[SFX_FEID_SIZE] 中的ReadModelBlock函数指针的索引7,此函数读取mesh,mtl,ani的信息 */ if (pElem->dwElementID == 7) { //参照KG3DSFX::ReadModelBlock SFX_MODEL_BLOCK* pBlock = (SFX_MODEL_BLOCK*)&pBuffer[pElem->dwElementOffset]; FindResource((const char*)pBlock->byMeshFileName, setResList); FindResource((const char*)pBlock->byMaterialFileName, setResList); FindResource((const char*)pBlock->byAnimationFileName, setResList); } } nResult = true; Exit0: return nResult; }
int KGTestMapDisuseResource::FindResInMDL(const char cszResourceName[], set<string>& setResList) { //参照KG3DModelST::LoadMDLContent int nResult = false; char szFilePath[MAX_PATH] = {0}; IFile* pFile = NULL; unsigned long uSize = 0; unsigned long uFileSize = 0; char* pBuffer = NULL; std::stringstream ss; std::string strBuffer; std::string strFilePath; MDLFileContent Content; KG_ASSERT_EXIT(cszResourceName); KGLOG_PROCESS_ERROR(cszResourceName[0] != '\0'); Content.dwNumModels = 0; pFile = g_OpenFile(cszResourceName); KG_PROCESS_ERROR(pFile); uFileSize = pFile->Size(); pBuffer = (char*)malloc(uFileSize + 1); KG_PROCESS_ERROR(pBuffer); uSize = pFile->Read(pBuffer, uFileSize); KG_PROCESS_ERROR(uSize == uFileSize); pBuffer[uSize] = '\0'; // TODO : text 文件没有使用'\0'作结束,不能作为字符串处理,特别麻烦,建议使用binary ss << pBuffer; std::getline(ss, strBuffer); strBuffer.erase(strBuffer.find_last_not_of("\n\r") + 1); KG_PROCESS_ERROR(!strBuffer.empty()); g_ExtractFilePath(szFilePath, cszResourceName); strFilePath = szFilePath; strFilePath += "\\"; if (strBuffer[0] == '\\') { Content.strBipFile = strBuffer; } else { Content.strBipFile = std::string(strFilePath + strBuffer); } FindResource(Content.strBipFile.c_str(), setResList); while (std::getline(ss, strBuffer)) { strBuffer.erase(strBuffer.find_last_not_of("\n\r") + 1); if (strBuffer.empty()) break; std::stringstream ssLine(strBuffer); std::string strMesh, strMtl; ssLine >> strMesh >> strMtl; if (strMtl.size()) { Content.strMaterialFile[Content.dwNumModels] = strMtl; } FindResource(Content.strMaterialFile[Content.dwNumModels].c_str(), setResList); if (strMesh.size()) { if (strMesh[0] == '\\') { Content.strMeshFile[Content.dwNumModels] = strMesh; } else { Content.strMeshFile[Content.dwNumModels] = strFilePath + strMesh; } FindResource(Content.strMeshFile[Content.dwNumModels].c_str(), setResList); Content.dwNumModels++; } } nResult = true; Exit0: SAFE_FREE(pBuffer); KG_COM_RELEASE(pFile); if (!nResult && cszResourceName) { KGLogPrintf(KGLOG_ERR, "Find Res In MDL %s failed.\n", cszResourceName); } return nResult; }
int KGTestMapDisuseResource::FindResInSTree(const char cszResourceName[], set<string>& setResList) { //参照KG3DModelSpeedTree::_LoadFromFile int nRetCode = false; int nResult = false; IFile* pFile = NULL; DWORD dwMark = 0; char szSourcePathName[MAX_PATH] = {0}; char szTreeMeshName[MAX_PATH] = {0}; char szTreeMtlName [MAX_PATH] = {0}; char szPathName[MAX_PATH] = {0}; char szDriver[MAX_PATH] = {0}; char szPath[MAX_PATH] = {0}; char szFileName[MAX_PATH] = {0}; char szExt[MAX_PATH] = {0}; KG_ASSERT_EXIT(cszResourceName); KGLOG_PROCESS_ERROR(cszResourceName[0] != '\0'); nRetCode = _snprintf_s(szSourcePathName, sizeof(szSourcePathName), sizeof(szSourcePathName) - 1, "%s%s", m_szClientPath, cszResourceName); KGLOG_PROCESS_ERROR(nRetCode > 0); pFile = g_OpenFile(szSourcePathName); KGLOG_PROCESS_ERROR(pFile); pFile->Read(&dwMark, sizeof(DWORD)); //Mesh Name pFile->Read(szTreeMeshName, sizeof(char) * MAX_PATH); nRetCode = _splitpath_s( cszResourceName, szDriver, sizeof(szDriver), szPath, sizeof(szPath), NULL, 0, NULL, 0 ); KGLOG_PROCESS_ERROR(nRetCode == 0); nRetCode = _splitpath_s( szTreeMeshName, NULL, 0, NULL, 0, szFileName, sizeof(szFileName), szExt, sizeof(szExt) ); KGLOG_PROCESS_ERROR(nRetCode == 0); nRetCode = _snprintf_s( szPathName, sizeof(szPathName), sizeof(szPathName) - 1, "%s%s", szDriver, szPath ); KGLOG_PROCESS_ERROR(nRetCode > 0); nRetCode = _snprintf_s( szTreeMeshName, sizeof(szTreeMeshName), sizeof(szTreeMeshName) - 1, "%s%s%s", szPathName, szFileName, szExt ); KGLOG_PROCESS_ERROR(nRetCode > 0); FindResource(szTreeMeshName, setResList); //Mtl Name pFile->Read(szTreeMtlName, sizeof(char) * MAX_PATH); nRetCode = _splitpath_s( szTreeMtlName, NULL, 0, NULL, 0, szFileName, sizeof(szFileName), szExt, sizeof(szExt) ); KGLOG_PROCESS_ERROR(nRetCode == 0); nRetCode = _snprintf_s( szTreeMtlName, sizeof(szTreeMtlName), sizeof(szTreeMtlName) - 1, "%s%s%s", szPathName, szFileName, szExt ); KGLOG_PROCESS_ERROR(nRetCode > 0); FindResource(szTreeMtlName, setResList); //Mtl Name nRetCode = _splitpath_s( cszResourceName, NULL, 0, NULL, 0, szFileName, sizeof(szFileName), NULL, 0 ); KGLOG_PROCESS_ERROR(nRetCode == 0); nRetCode = _snprintf_s( szTreeMtlName, sizeof(szTreeMtlName), sizeof(szTreeMtlName) - 1, "%s%s.Mtl", szPathName, szFileName ); KGLOG_PROCESS_ERROR(nRetCode > 0); FindResource(szTreeMtlName, setResList); nResult = true; Exit0: if (pFile) { pFile->Close(); SAFE_RELEASE(pFile); } return nResult; }
HRESULT KG3DAnimationWarper::Load(const TCHAR *strFileName) { HRESULT hr = E_FAIL; IFile *pFile = NULL; unsigned long uSize = 0; SerializeData Data; float *pWeights = NULL; KG3DAnimationUpdateExtraInfoCollector** ppCollectors = NULL; DWORD dwStartTime = timeGetTime(); KGLOG_PROCESS_ERROR(strFileName); KG_PROCESS_ERROR(m_pModel); Clear(); pFile = g_OpenFile(strFileName); if (!pFile) { KGLogPrintf(KGLOG_ERR, "KG3DAnimationWarper open file %s failed.", strFileName); goto Exit0; } uSize = pFile->Read(&Data, sizeof(SerializeData)); KGLOG_PROCESS_ERROR(uSize == sizeof(SerializeData)); KG_PROCESS_ERROR(Data.cdwMask == SerializeData::cdwMask); switch(Data.dwVersion) { case SerializeData::cdwVersion: { //read bone name information here hr = Helper_LoadBipExtraInfo(pFile); KGLOG_COM_PROCESS_ERROR(hr); m_Data.resize(Data.dwNumAnimationComposer); ppCollectors = new KG3DAnimationUpdateExtraInfoCollector*[Data.dwNumAnimation]; KGLOG_PROCESS_ERROR(ppCollectors); for (DWORD i = 0; i < Data.dwNumAnimation; i++) { AnimationInfo New; New.pClip = NULL; New.pCollector = new KG3DRTSInfoCollector; KGLOG_PROCESS_ERROR(New.pCollector); ppCollectors[i] = New.pCollector; New.pCollector->CreateStore(m_pModel); New.pCollector->Clear(); m_vecAnimations.push_back(New); m_vecTagContainers.push_back(NULL); } for (DWORD i = 0; i < Data.dwNumAnimationComposer; i++) { m_Data[i].Composer = new KG3DAnimationComposer; KGLOG_PROCESS_ERROR(m_Data[i].Composer); m_Data[i].Composer->Init(&m_Skeleton, m_pModel); m_Data[i].Composer->ReadBlock(pFile, ppCollectors, Data.dwNumAnimation); } break; } default: assert(0); } //Load composer weight information here size_t uIndex = 0; size_t uNumWeight = Data.dwNumBone * Data.dwNumAnimationComposer; pWeights = new float[uNumWeight]; KGLOG_PROCESS_ERROR(pWeights); uSize = pFile->Read(pWeights, sizeof(float) * (unsigned long)uNumWeight); KGLOG_PROCESS_ERROR(uSize == sizeof(float) * uNumWeight); for (size_t i = 0; i < m_Data.size(); i++) { m_Data[i].BoneWeight.resize(Data.dwNumBone); for (size_t j = 0; j < m_Data[i].BoneWeight.size(); j++) { assert(m_BoneMatchTable[j] != -1 && m_BoneMatchTable[j] < static_cast<int>(m_Data[i].BoneWeight.size())); BOOL bWeight = (pWeights[uIndex] == 1.0f) ? TRUE : FALSE; m_Data[i].BoneWeight[m_BoneMatchTable[j]] = bWeight; uIndex++; } } hr = S_OK; Exit0: SAFE_DELETE_ARRAY(pWeights); SAFE_DELETE_ARRAY(ppCollectors); KG_COM_RELEASE(pFile); DWORD dwCost = timeGetTime() - dwStartTime; if(g_cEngineOption.bEnableTimeOptimizeLog && dwCost > 10) { KGLogPrintf(KGLOG_INFO,"TimeOptimize KG3DAnimationWarper::Load cost %d %s",dwCost,strFileName); } return hr; }
HRESULT KTgaButton::LoadTgaFile(const char* strFileName,BOOL bAddToTerrian) { if (!strFileName) return E_FAIL; bool isload = false; std::string dir = strFileName; std::list<std::string>::iterator its,listend; std::string *psting = NULL; //FILE* fpFile = fopen(strFileName, "rb"); IFile* fpFile = g_OpenFile(strFileName); if (!fpFile)//打开默认的贴图文件 { TCHAR NameDefault[MAX_PATH]; wsprintf(NameDefault,"%s\\Data\\Editor\\Texture\\replace.tga",g_szDefWorkDirectory); fpFile = g_OpenFile(NameDefault);//fpFile = fopen(NameDefault, "rb");// if(!fpFile) return E_FAIL; } fpFile->Read(&m_TgaInfo.Header, sizeof(TGAHeader));//fread(&m_TgaInfo.Header, sizeof(TGAHeader), 1, fpFile);// const int byteperpixel = m_TgaInfo.Header.bitsperpixel / 8; if(byteperpixel != 4) { MessageBox("Tga格式不正确,必须为32位"); SAFE_RELEASE(fpFile);//fclose(fpFile);// return E_FAIL; } m_TextureDir = dir; //TCHAR drive[256]; //TCHAR dirc[256]; //TCHAR fname[256]; //TCHAR ext[256]; std::string filename; if(m_pTerrainEx && m_nButtonType == GROUNDTEXTURE) { int nCoord =0; if(SUCCEEDED(m_pTerrainEx->GetGroundTextureCoordIndex(m_nLayer,m_nTextureIndex,&nCoord))) { SetTexCoordIndex(nCoord); } } size_t Size = m_TgaInfo.Header.height * m_TgaInfo.Header.width * byteperpixel; SAFE_DELETE_ARRAY(m_TgaInfo.pBuffer); m_TgaInfo.pBuffer = new BYTE[Size]; fpFile->Seek(m_TgaInfo.Header.Id + sizeof(TGAHeader), SEEK_SET);//fseek(fpFile, m_TgaInfo.Header.Id + sizeof(TGAHeader), SEEK_SET);// fpFile->Read(m_TgaInfo.pBuffer, (unsigned long)Size);//fread(m_TgaInfo.pBuffer, Size, 1, fpFile);// RECT rect; GetClientRect(&rect); int height = abs(rect.bottom - rect.top); int width = abs(rect.left - rect.right); BYTE *NewData = SacleData(rect,m_TgaInfo.pBuffer,byteperpixel); CDC* pDc = this->GetDC(); m_Bitmap.CreateCompatibleBitmap(pDc, width, height); m_Bitmap.SetBitmapBits(width * height * byteperpixel,NewData); SetBitmap(m_Bitmap); ReleaseDC(pDc); SAFE_DELETE_ARRAY(NewData); //fclose(fpFile); SAFE_RELEASE(fpFile); m_bIsHaveTexture = true; return S_OK; }
//往打包文件中添加一个子文件 bool KPackFileManager::AddElemToPak() { unsigned int uElemIndex = 0; unsigned int uHashId = 0; unsigned int uCRC = 0; if (!GenerateElemIndexAndHashId(uElemIndex, uHashId)) return false; KSmartFile SrcFile; SrcFile = g_OpenFile(m_FullFolderFileName, true, false); if (!SrcFile) return false; int nSrcSize = SrcFile->Size(); if (nSrcSize == 0) return true; unsigned int uCompressType = XPACK_METHOD_UCL; unsigned int uCompressSize; unsigned char * pSrcBuffer = (unsigned char*)SrcFile->GetBuffer(); /* 直接读取数据,没有压缩 */ if (pSrcBuffer == NULL) return false; const char* pExt = strrchr(m_FullFolderFileName, '.'); if (pExt && !stricmp(pExt + 1, "spr")) // 判断是否为SPR文件 { if ((unsigned int)nSrcSize >= m_uPackFileShellOptionSprSplitFrameBalance) //spr超过大小则分块压缩 { SPRHEAD* pSpr = (SPRHEAD*)pSrcBuffer; if (*(int*)(&(pSpr->Comment)) == SPR_COMMENT_FLAG && pSpr->Frames > 1) uCompressType = XPACK_FLAG_FRAGMENT; } } PACK_ITEM & item = m_PackItemList[m_nCurrentPakIndex]; item.pIOFile->Seek(item.nDataEndOffset, SEEK_SET); bool bOk = false; if (uCompressType == XPACK_FLAG_FRAGMENT) //分块存的spr bOk = AddElemToPakFragmentSPR(pSrcBuffer, nSrcSize, uCompressSize); else bOk = AddElemToPakCommon(pSrcBuffer, nSrcSize, uCompressType, uCompressSize); uCRC = Misc_CRC32(0, pSrcBuffer, nSrcSize); /// 原始数据,上面两个AddElemToPakXxx函数没有对pSrcBuffer进行操作 SrcFile.Release(); if (bOk) { for (unsigned int i = item.Header.uCount; i > uElemIndex; i--) item.pIndexList[i] = item.pIndexList[i - 1]; item.Header.uCount++; item.pIndexList[uElemIndex].uCompressSizeFlag = uCompressSize | uCompressType; item.pIndexList[uElemIndex].uSize = nSrcSize; item.pIndexList[uElemIndex].uId = uHashId; item.pIndexList[uElemIndex].uOffset = item.nDataEndOffset; item.nDataEndOffset += uCompressSize; item.bModified = true; KPackFilePartner::PACKPARTNER_ELEM_INFO info; info.nElemIndex = uElemIndex; strcpy(info.szFileName, m_FullFolderFileName + m_nElemFileRootPathNotEnderLen); info.uCRC = uCRC; info.uId = uHashId; info.uSize = nSrcSize; info.uStoreSizeAndCompressFlag = uCompressSize | uCompressType; time_t t; struct _stat s; if (!_stat(m_FullFolderFileName, &s)) info.uTime = (unsigned int)s.st_mtime; else info.uTime = (unsigned int)time(&t); bOk = m_PackPartnerList[m_nCurrentPakIndex].AddElem(info); assert(bOk); } return bOk; }
//创建/打开的一个打包文件,返回打包文件索引,返回0值表示操作失败。 int KPackFileManager::CreatePack(const char* pszFile, int bOpenExist, int bExcludeOfCheckId) { int nPakIndex; for (nPakIndex = 0; nPakIndex < PACK_FILE_SHELL_MAX_SUPPORT_PAK_NUM; nPakIndex++) { if (m_PackItemList[nPakIndex].pIOFile == NULL) break; } if (nPakIndex == PACK_FILE_SHELL_MAX_SUPPORT_PAK_NUM) return -1; PACK_ITEM& item = m_PackItemList[nPakIndex]; bool bOk = false; g_GetFullPath(item.PackFileName, pszFile); while(true) { item.pIndexList = (XPackIndexInfo*)malloc(sizeof(XPackIndexInfo) * PACK_FILE_SHELL_MAX_SUPPORT_ELEM_FILE_NUM); if (item.pIndexList == NULL) break; memset(item.pIndexList, 0, sizeof(XPackIndexInfo) * PACK_FILE_SHELL_MAX_SUPPORT_ELEM_FILE_NUM); if (bOpenExist) { if (!g_IsFileExist(pszFile)) //FilePath.cpp:426 --> pszFile = 路径名+文件名 break; item.pIOFile = g_OpenFile(pszFile, true, true); if (item.pIOFile == NULL) break; if (item.pIOFile->Read(&item.Header, sizeof(item.Header)) != sizeof(item.Header) || (*(int*)(&(item.Header.cSignature)) != IPACK_FILE_SIGNATURE_FLAG)) { break; } item.pIOFile->Seek(item.Header.uIndexTableOffset, SEEK_SET); if (item.pIOFile->Read(item.pIndexList, sizeof(XPackIndexInfo) * item.Header.uCount) != sizeof(XPackIndexInfo) * item.Header.uCount)//读入文件尾部,插入新的文件数据之后再插回来 break; item.nDataEndOffset = item.Header.uIndexTableOffset; item.pIOFile->Seek(item.nDataEndOffset, SEEK_SET); LoadPackPartner(nPakIndex); } else { item.pIOFile = g_CreateFile(pszFile); if (item.pIOFile == NULL) break; memset(&item.Header, 0, sizeof(item.Header)); *(int*)(&(item.Header.cSignature)) = IPACK_FILE_SIGNATURE_FLAG; /* 前提条件为:Little-Endian */ if (item.pIOFile->Write(&item.Header, sizeof(item.Header)) != sizeof(item.Header)) break; item.Header.uDataOffset = sizeof(item.Header); item.nDataEndOffset = sizeof(item.Header); item.bModified = true; CreatePackPartner(nPakIndex); } item.bExcludeOfCheckId = (bExcludeOfCheckId != false); bOk = true; break; } if (!bOk) { SAFE_FREE(item.pIndexList); SAFE_RELEASE(item.pIOFile); item.PackFileName[0] = 0; return -1; } return nPakIndex; }
//加载打包文件信息 bool KPackFilePartner::Load(const char* pFileName) { if (!Init()) return false; KSmartFile file; file = g_OpenFile(pFileName, true, false); if (!file) return false; char* pFileBuffer = (char*)file->GetBuffer(); if (pFileBuffer == NULL) return false; int nSize = file->Size(); char* pString = pFileBuffer; int nPos = 0, nLineCount = 0, nTotalCount = 0; struct tm FormatTime; int nTempFlag = 0; while(nPos < nSize) { int nLen, nRet; char* pEnd = (char*)memchr(pString, '\n', nSize - nPos); if (pEnd == NULL) pEnd = &pFileBuffer[nSize - 1]; *pEnd = 0; nLen = pEnd - pString; if (nLineCount == 0) { nRet = sscanf(pString, LINE_FORMAT_FIRST, &nTotalCount, &FormatTime.tm_year, &FormatTime.tm_mon, &FormatTime.tm_mday, &FormatTime.tm_hour, &FormatTime.tm_min, &FormatTime.tm_sec, &m_uPackTime, &m_uCRC); if (nRet != VALUE_COUNT_IN_LINE_FIRST) //上面读了9个数据 break; } nLineCount++; if (nLineCount <= 2) { pString = pEnd + 1; nPos+= nLen + 1; continue; } PACKPARTNER_ELEM_INFO& info = m_pElemInfoList[nLineCount - 3]; //以为文件名中可能包含空格,所以不能直接用LINE_FORMAT_OTHERS作为格式化字符串来读取! //读取文件名之前的数据 nRet = sscanf(pString, LINE_FORMAT_OTHERS_HEAD, &info.nElemIndex, &info.uId, &FormatTime.tm_year, &FormatTime.tm_mon, &FormatTime.tm_mday, &FormatTime.tm_hour, &FormatTime.tm_min, &FormatTime.tm_sec); if (nRet != VALUE_COUNT_IN_LINE_OTHERS_HEAD || info.nElemIndex != nLineCount - 3) break; char* pElemName = strchr(pString, '\\'); if (pElemName) { char* pElemEnd = strchr(pElemName, '\t'); if (!pElemEnd) break; //读取文件名 memcpy(info.szFileName, pElemName, pElemEnd - pElemName); info.szFileName[pElemEnd - pElemName] = 0; pString = pElemEnd + 1; } else { info.szFileName[0] = 0; pString = strrchr(pString, ':'); if (!pString) break; pString = strchr(pString + 1, '\t'); if (!pString) break; pString = strchr(pString + 1, '\t'); if (!pString) break; pString++; } //读取文件名之后的部分 nRet = sscanf(pString, LINE_FORMAT_OTHERS_TAIL, &info.uSize, &info.uStoreSizeAndCompressFlag, &nTempFlag, &info.uCRC); if (nRet != VALUE_COUNT_IN_LINE_OTHERS_TAIL) break; info.uStoreSizeAndCompressFlag |= (nTempFlag << XPACK_COMPRESS_SIZE_BIT); FormatTime.tm_year -= 1900; FormatTime.tm_mon--; info.uTime = (unsigned int)mktime(&FormatTime); if (info.uTime == (unsigned int)(-1)) info.uTime = 0; pString = pEnd + 1; nPos+= nLen + 1; m_nElemCount++; } return (m_nElemCount == nTotalCount); }
BOOL KRegion::LoadTerrainData(const char cszFileName[], KObjAlloctor& rObjectAlloctor) { BOOL bResult = false; BOOL bRetCode = false; IFile* piFile = NULL; IKG_Buffer* piBuffer = NULL; BYTE* pbyBuffer = NULL; size_t uFileSize = 0; size_t uReadBytes = 0; size_t uLeftBytes = 0; BYTE* pbyOffset = NULL; KRegionHeader* pFileHeader = NULL; size_t uBaseCellInfoSize = sizeof(KCell::KBaseInfo) + sizeof(WORD); KCell* pAllocCell = NULL; int nExtCellCount = 0; size_t uExtCellInfoSize = sizeof(int) * 2 + sizeof(KCell::KBaseInfo) + sizeof(WORD) * 2; piFile = g_OpenFile(cszFileName); KGLOG_PROCESS_ERROR(piFile); uFileSize = piFile->Size(); KGLOG_PROCESS_ERROR(uFileSize > 0); piBuffer = KG_MemoryCreateBuffer((unsigned)uFileSize); KGLOG_PROCESS_ERROR(piBuffer); pbyBuffer = (BYTE*)piBuffer->GetData(); KGLOG_PROCESS_ERROR(pbyBuffer); uReadBytes = piFile->Read(pbyBuffer, (unsigned long)uFileSize); KGLOG_PROCESS_ERROR(uReadBytes == uFileSize); KG_COM_RELEASE(piFile); pbyOffset = pbyBuffer; uLeftBytes = uReadBytes; KGLOG_PROCESS_ERROR(uLeftBytes >= sizeof(KRegionHeader)); pFileHeader = (KRegionHeader*)pbyOffset; pbyOffset += sizeof(KRegionHeader); uLeftBytes -= sizeof(KRegionHeader); KGLOG_PROCESS_ERROR(pFileHeader->nRegionX == m_nRegionX); KGLOG_PROCESS_ERROR(pFileHeader->nRegionY == m_nRegionY); KGLOG_PROCESS_ERROR(uLeftBytes >= uBaseCellInfoSize * REGION_GRID_WIDTH * REGION_GRID_HEIGHT); uLeftBytes -= uBaseCellInfoSize * REGION_GRID_WIDTH * REGION_GRID_HEIGHT; for (int nCellY = 0; nCellY < REGION_GRID_HEIGHT; nCellY++) { for (int nCellX = 0; nCellX < REGION_GRID_WIDTH; nCellX++) { KCell* pCell = &m_Cells[nCellX][nCellY]; KCell::KBaseInfo* pBaseInfo = (KCell::KBaseInfo*)pbyOffset; pCell->m_BaseInfo = *pBaseInfo; pCell->m_wLowLayer = 0; pbyOffset += sizeof(KCell::KBaseInfo); pCell->m_wHighLayer = *(WORD*)pbyOffset; pbyOffset += sizeof(WORD); } } KGLOG_PROCESS_ERROR(uLeftBytes >= sizeof(int)); uLeftBytes -= sizeof(int); nExtCellCount = *(int*)pbyOffset; pbyOffset += sizeof(int); KGLOG_PROCESS_ERROR(nExtCellCount >= 0); KGLOG_PROCESS_ERROR(uLeftBytes >= uExtCellInfoSize * nExtCellCount); uLeftBytes -= uExtCellInfoSize * nExtCellCount; for (int nIndex = 0; nIndex < nExtCellCount; nIndex++) { int nCellX = 0; int nCellY = 0; KCell::KBaseInfo* pBaseInfo = NULL; pAllocCell = rObjectAlloctor.NewCell(); KGLOG_PROCESS_ERROR(pAllocCell); nCellX = *(int*)pbyOffset; pbyOffset += sizeof(int); nCellY = *(int*)pbyOffset; pbyOffset += sizeof(int); pBaseInfo = (KCell::KBaseInfo*)pbyOffset; pbyOffset += sizeof(KCell::KBaseInfo); pAllocCell->m_BaseInfo = *pBaseInfo; pAllocCell->m_wHighLayer = *(WORD*)pbyOffset; pbyOffset += sizeof(WORD); pAllocCell->m_wLowLayer = *(WORD*)pbyOffset; pbyOffset += sizeof(WORD); bRetCode = AddObstacle(nCellX, nCellY, pAllocCell); KGLOG_PROCESS_ERROR(bRetCode); pAllocCell = NULL; } if (uLeftBytes >= sizeof(m_dwScriptList)) { memcpy(m_dwScriptList, pbyOffset, sizeof(m_dwScriptList)); pbyOffset += sizeof(m_dwScriptList); uLeftBytes -= sizeof(m_dwScriptList); } KGLOG_PROCESS_ERROR(uLeftBytes == 0); bResult = true; Exit0: if (pAllocCell) { rObjectAlloctor.DeleteCell(pAllocCell); pAllocCell = NULL; } KG_COM_RELEASE(piBuffer); KG_COM_RELEASE(piFile); return bResult; }
HRESULT KG3DTerrainRoad::LoadFromFile(const char cszFileName[]) { //return E_FAIL; HRESULT hr = E_FAIL; int nRetCode = false; TCHAR FileName[MAX_PATH]; _RoadHead RoadHead; DWORD nSize; D3DXVECTOR3* pNodePos = NULL; IFile* pFile = NULL; pFile = g_OpenFile(cszFileName); KG_PROCESS_ERROR(pFile); nSize = pFile->Read(&RoadHead,sizeof(_RoadHead));//fread(&RoadData,sizeof(_RoadData),1,pFile); m_scName = RoadHead.scName; m_scTextureName = RoadHead.scTextureName; m_fBendModulus = RoadHead.fBendModulus; m_fEdgeModulus = RoadHead.fEdgeModulus; m_fTexDensity = RoadHead.fTexDensity; m_fBlendLength = RoadHead.fBlendLength; m_fWidth = RoadHead.fWidth; m_nID = RoadHead.nID; m_fNodeSize = RoadHead.fNodeSize; m_nSegmentLength= RoadHead.dwSegmentLength; wsprintf(FileName,"%s", m_scTextureName.c_str()); LoadRoadTexture(FileName); if (!g_bClient) { nRetCode = pFile->Seek(RoadHead.dwParentTerrainBlock, SEEK_SET); KGLOG_PROCESS_ERROR(nRetCode != -1); for (int nT= 0; nT < (int)RoadHead.dwNumParentBlock; nT++) { TerrainBlockInfo TBI; pFile->Read(&TBI,sizeof(TerrainBlockInfo)); m_vecParentTerrainBlock.push_back(TBI); } nRetCode = pFile->Seek(RoadHead.dwNodePosBlock, SEEK_SET);//fseek(pFile,RoadData.dwNodePosBlock, SEEK_SET); KGLOG_PROCESS_ERROR(nRetCode != -1); pNodePos = new D3DXVECTOR3[RoadHead.dwNumOfNode]; KGLOG_PROCESS_ERROR(pNodePos); nSize = pFile->Read(pNodePos,sizeof(D3DXVECTOR3)*RoadHead.dwNumOfNode);//fread(RoadData.pNodePos,sizeof(D3DXVECTOR3),RoadData.dwNumOfNode,pFile); LoadNode(RoadHead.dwNumOfNode,pNodePos); } nRetCode = pFile->Seek(RoadHead.dwPassageBlock, SEEK_SET); KGLOG_PROCESS_ERROR(nRetCode != -1); { list<KG3DRepresentObjectNode*>::iterator iNode = m_listNode.begin(); float fLengthSum = 0; m_BBox.Clear(); for (DWORD i = 0;i < RoadHead.dwNumOfPassage;i++) { KG3DTerrainRoadPassage* pPassage = new KG3DTerrainRoadPassage(); if(pPassage) { _PassageData ReadData; if(SUCCEEDED(pPassage->ReadDataFromFile(pFile,ReadData))) { pPassage->CreateBuffersFromData(ReadData); pPassage->BulidPassageData(fLengthSum,m_fWidth,m_fBendModulus,m_nSegmentLength,m_fExtendWidth); m_listPassage.push_back(pPassage); m_BBox.AddPosition(pPassage->m_Bbox.A); m_BBox.AddPosition(pPassage->m_Bbox.B); m_ExtendBBox.AddPosition(pPassage->m_ExtendBBox.A); m_ExtendBBox.AddPosition(pPassage->m_ExtendBBox.B); if(iNode!=m_listNode.end()) { pPassage->m_pNodeA = *iNode; ++iNode; if(iNode!=m_listNode.end()) pPassage->m_pNodeB = *iNode; } } else { SAFE_DELETE(pPassage); } } } this->m_fLength = fLengthSum; } hr = S_OK; Exit0: KG_COM_RELEASE(pFile); SAFE_DELETE_ARRAY(pNodePos); return hr; }
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; }
HRESULT KG3DAnimationTagContainer::_Load(LPCSTR strFileName) { HRESULT hrResult = E_FAIL; HRESULT hrRetCode = E_FAIL; IFile* pFile = NULL; FileHeader Header; AnimationTagBlockHeader BlockHeader; unsigned long uRetCode = 0; IKG3DAnimationTag* pNewTag = NULL; Clear(); KG_PROCESS_ERROR(strFileName); pFile = g_OpenFile(strFileName); KGLOG_PROCESS_ERROR(pFile); uRetCode = pFile->Read(&Header, sizeof(FileHeader)); KGLOG_PROCESS_ERROR(uRetCode == sizeof(FileHeader)); KG_PROCESS_ERROR(Header.dwMask == FileHeader::s_dwMask); strncpy(m_szAnimationName, Header.strAnimationFileName, _countof(m_szAnimationName) - 1); m_szAnimationName[_countof(m_szAnimationName) - 1] = '\0'; hrRetCode = g_cClipTable.LoadResourceFromFile(m_szAnimationName, 0, 0, &m_pClip); KGLOG_COM_PROCESS_ERROR(hrRetCode); switch(Header.dwVersion) { case FileHeader::s_dwCurrentVersion: { for (DWORD i = 0; i < Header.dwNumBlock; i++) { Item NewItem; uRetCode = pFile->Read(&BlockHeader, sizeof(AnimationTagBlockHeader)); KGLOG_PROCESS_ERROR(uRetCode == sizeof(AnimationTagBlockHeader)); if (BlockHeader.dwNumKeyFrames == 0) { hrRetCode = SkipBlock(&BlockHeader, pFile); KGLOG_COM_PROCESS_ERROR(hrRetCode); continue; } pNewTag = GetNewInstance(static_cast<enuTagType>(BlockHeader.dwType)); KG_PROCESS_ERROR(pNewTag); hrRetCode = pNewTag->LoadFromFile(pFile, BlockHeader.dwVersion, BlockHeader.dwNumKeyFrames); KG_COM_PROCESS_ERROR(hrRetCode); pNewTag->SetParentModel(m_pModel); NewItem.pTag = pNewTag; NewItem.Type = static_cast<enuTagType>(BlockHeader.dwType); m_vecTags.push_back(NewItem); pNewTag = NULL; } } break; default: _ASSERTE(0); } m_IsLoaded = TRUE; hrResult = S_OK; Exit0: SAFE_DELETE(pNewTag); KG_COM_RELEASE(pFile); return hrResult; }
BOOL KScriptManager::Reload(const char* pszFileName) { BOOL bResult = false; BOOL bRetCode = false; IFile* piFile = NULL; char* pszBuffer = NULL; DWORD dwScriptID = 0; DWORD dwFileSize = 0; DWORD dwReadSize = 0; KLuaScriptData* pScriptData = NULL; char szPathName[MAX_PATH]; char szFileName[MAX_PATH]; assert(pszFileName); piFile = g_OpenFile(pszFileName); KG_PROCESS_ERROR(piFile); dwFileSize = piFile->Size(); pszBuffer = new char[dwFileSize]; KG_PROCESS_ERROR(pszBuffer); dwReadSize = piFile->Read(pszBuffer, dwFileSize); KG_PROCESS_ERROR(dwReadSize == dwFileSize); g_GetFullPath(szPathName, pszFileName); bRetCode = g_GetFilePathFromFullPath(szFileName, szPathName); KG_PROCESS_ERROR(bRetCode); dwScriptID = g_FileNameHash(szFileName); bRetCode = IsScriptExist(dwScriptID); KG_PROCESS_ERROR(bRetCode); bRetCode = m_piScript->LoadFromBuffer(dwScriptID, szPathName, pszBuffer, dwFileSize); KG_PROCESS_ERROR(bRetCode); pScriptData = m_piScript->GetScriptData(dwScriptID); if (pScriptData) { std::vector<DWORD>::iterator it = pScriptData->vecIncludeScriptID.begin(); std::vector<DWORD>::iterator itEnd = pScriptData->vecIncludeScriptID.end(); for (NULL; it != itEnd; ++it) { KLuaScriptData* pNewScriptData = NULL; pNewScriptData = m_piScript->GetScriptData(*it); if (pNewScriptData) Reload(pNewScriptData->szName); } } bResult = true; Exit0: if (!bResult) { KGLogPrintf(KGLOG_ERR, "[Lua] Failed to reload file \"%s\" !\n", pszFileName); } KG_DELETE_ARRAY(pszBuffer); KG_COM_RELEASE(piFile); return bResult; }