Example #1
0
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;
	}
}
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;
}
Example #3
0
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;
}
Example #4
0
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);
}
Example #5
0
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;
}
Example #6
0
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;
}
Example #7
0
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;
}
Example #8
0
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;
}
Example #9
0
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;
}
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;
}