/*读取一个Unicode文件,使用fgetws函数,速度较快*/ extern "C" int GetAllFileWS(lua_State *L) { /*传递第一个参数,文件名*/ const char* filename = lua_tostring(L, -1); /*获取文件大小*/ FILE* input=fopen( filename, "rb"); int len = _GetFileSize(filename); /*初始化变量*/ wchar_t *all ; all = (wchar_t *)malloc(sizeof(wchar_t)*(len/CHAR_SCALE+1)); memset(all, 0, sizeof(wchar_t) * (len/CHAR_SCALE+1) ); int i=0; while(!feof(input)) { wchar_t * buf; buf = (wchar_t *)malloc(sizeof(wchar_t)*BUFF_SIZE) ; memset(buf, 0, sizeof(wchar_t) * BUFF_SIZE ); int j=0; fgetws(buf, BUFF_SIZE, input); /*连接缓存空间,原先使用wcscat连接,但是很慢且有问题,使用指针后速度很快*/ while(buf[j] != L'\0') { all[i++]=buf[j++]; } free(buf); } all[len/CHAR_SCALE]=L'\0'; lua_pushlstring(L, (char*)all,wcslen(all)*CHAR_SCALE); return 1; }
static int ReadEntireFileSafely( char *filename , char *mode , char **pbuf , long *pbufsize ) { long filesize ; int nret ; filesize = _GetFileSize( filename ); (*pbuf) = (char*)malloc( filesize + 1 ) ; if( (*pbuf) == NULL ) return -1; memset( (*pbuf) , 0x00 , filesize + 1 ); nret = ReadEntireFile( filename , mode , (*pbuf) , & filesize ) ; if( nret ) { free( (*pbuf) ); (*pbuf) = NULL ; return nret; } else { if( pbufsize ) (*pbufsize) = filesize ; return 0; } }
/*Lua 获取 文件大小*/ extern "C" int GetFileSizeW(lua_State *L) { /*传递第一个参数,文件名*/ const char* filename = lua_tostring(L, -1); lua_pushinteger(L, _GetFileSize(filename)); return 1; }
static int ReadEntireFile( char *filename , char *mode , char *buf , long *bufsize ) { FILE *fp = NULL ; long filesize ; long lret ; if( filename == NULL ) return -1; if( strcmp( filename , "" ) == 0 ) return -1; filesize = _GetFileSize( filename ) ; if( filesize < 0 ) return -2; fp = fopen( filename , mode ) ; if( fp == NULL ) return -3; if( filesize <= (*bufsize) ) { lret = fread( buf , sizeof(char) , filesize , fp ) ; if( lret < filesize ) { fclose( fp ); return -4; } (*bufsize) = filesize ; fclose( fp ); return 0; } else { lret = fread( buf , sizeof(char) , (*bufsize) , fp ) ; if( lret < (*bufsize) ) { fclose( fp ); return -4; } fclose( fp ); return 1; } }
/*读取一个Unicode文件,使用fgetwc函数,IO导致速度较慢*/ extern "C" int GetAllFileWC(lua_State *L) { /*传递第一个参数,文件名*/ const char* filename = lua_tostring(L, -1); /*获取文件大小*/ int len; len = _GetFileSize(filename); /*设置缓存大小*/ wchar_t * buf; /*由于wchar_t长度为char长度一倍,所以buf空间大小为文件长度一半,再加末尾的 '\0'*/ buf = (wchar_t *) malloc (sizeof(wchar_t) * (len/CHAR_SCALE+1)); int i = 0 ; FILE* input=fopen( filename, "rb"); while(!feof(input)) { buf[i++]=fgetwc(input); } /*字符串末尾置零*/ buf[i-1]=L'\0'; lua_pushlstring(L, (char*)buf,wcslen(buf)*CHAR_SCALE); return 1; }
BOOL CPostEffectConverter::ExportVSProgram() { std::vector<SVSInfo>::const_iterator it = m_CompiledVSList.begin(); std::vector<izanagi::E_POSTEFFECT_VTX_SHADER> tvExportedVSType; tvExportedVSType.reserve(m_CompiledVSList.size()); std::vector<izanagi::E_POSTEFFECT_VTX_SHADER>::const_iterator itType; for (; it != m_CompiledVSList.end(); it++) { const SVSInfo& sVSInfo = *it; // Check whether vertex program is exported. itType = std::find( tvExportedVSType.begin(), tvExportedVSType.end(), sVSInfo.type); if (itType == tvExportedVSType.end()) { // If not exported. izanagi::S_PES_VS_HEADER sVSHeader; sVSHeader.type = sVSInfo.type; sVSHeader.sizeProgram = _GetFileSize(sVSInfo.file); IZ_OUTPUT_WRITE_VRETURN(&m_Out, &sVSHeader, 0, sizeof(sVSHeader)); VRETURN(_ExportFile(sVSInfo.file, &m_Out)); tvExportedVSType.push_back(sVSInfo.type); m_PesHeader.numVtxProgram++; m_PesHeader.maxProgamSize = IZ_MAX(m_PesHeader.maxProgamSize, sVSHeader.sizeProgram); } } return TRUE; }
// パス解析 BOOL CPostEffectConverter::ExportPass() { IZ_UINT nTechIdx = 0; IZ_UINT nPassIdx = 0; IZ_UINT nConstNum = 0; IZ_UINT nSamplerNum = 0; izanagi::tool::CString strPrevPassFunctoName; CGtechnique tech = ::cgGetFirstTechnique(m_pCgEffect); while (tech != NULL) { CGpass pass = ::cgGetFirstPass(tech); while (pass != NULL) { izanagi::S_PES_PASS sPass; { FILL_ZERO(&sPass, sizeof(sPass)); IZ_PCSTR name = ::cgGetPassName(pass); IZ_UINT pos = CStringChunk::GetInstance().Register(name); //sPass.name = *(IZ_PCSTR*)(&pos); sPass.posName = pos; sPass.keyName = izanagi::CKey::GenerateValue(name); sPass.TechniqueIdx = nTechIdx; sPass.numConst = _GetUsedParamNum(m_ParamList, pass); sPass.numSampler = _GetUsedParamNum(m_SamplerList, pass); VRETURN(CPassUtil::SetVSType(sPass, pass)); sPass.sizeProgram = _GetFileSize(m_CompiledPSList[nPassIdx]); sPass.ann.RenderTargetIdx = _GetRenderTargetIdx(m_TexList, pass); sPass.ann.TexOffsetParamIdx = _GetTexOffsetParamIdx(m_ParamList, pass); for (IZ_UINT i = 0; i < izanagi::PES_FUNCTOR_ARGS_NUM; i++) { sPass.ann.FunctorArgs[i] = -1; } nConstNum += sPass.numConst; nSamplerNum += sPass.numSampler; } VRETURN( CPassUtil::SetAnnValue( sPass.ann, pass)); IZ_PCSTR pszFunctorName = CPassUtil::GetFunctorName(pass); if (pszFunctorName != IZ_NULL) { if (strPrevPassFunctoName == pszFunctorName) { sPass.ann.isSubPass = IZ_TRUE; } strPrevPassFunctoName = pszFunctorName; } // Stringで設定されているファンクタ引数 std::vector<izanagi::tool::CString> tvFunctorArgSList; CPassUtil::GetFunctorArgsString( pass, tvFunctorArgSList); if (tvFunctorArgSList.size() > 0) { ConvertFunctorArgsStrToIndex( tvFunctorArgSList, sPass.ann); } VRETURN( CPassUtil::SetStateValue( sPass.state, pass)); // 出力 VRETURN(ExportData(sPass)); pass = ::cgGetNextPass(pass); nPassIdx++; m_PesHeader.maxProgamSize = IZ_MAX(m_PesHeader.maxProgamSize, sPass.sizeProgram); m_PesHeader.numPass++; } tech = ::cgGetNextTechnique(tech); nTechIdx++; } // NOTE // 全体でのシェーダ定数、サンプラの総数を知りたい m_PesHeader.numParam = nConstNum; m_PesHeader.numSampler = nSamplerNum; return TRUE; }
// パス解析 BOOL CShaderConverter::ExportPass(const SShaderConfig& config) { _ExportChunkHeader(m_Out, izanagi::shader::SHD_CHUNK_MAGIC_NUMBER_PASS); izanagi::shader::S_SHD_PASS_HEADER sPassHeader; FILL_ZERO(&sPassHeader, sizeof(sPassHeader)); // Blank for pass's header. izanagi::tool::CIoStreamSeekHelper cSeekHelper(&m_Out); VRETURN(_BeginExportChunk(sPassHeader, cSeekHelper)); IZ_UINT nTechIdx = 0; IZ_UINT nPassIdx = 0; IZ_UINT nConstNum = 0; IZ_UINT nSamplerNum = 0; CGtechnique tech = ::cgGetFirstTechnique(m_pCgEffect); while (tech != NULL) { CGpass pass = ::cgGetFirstPass(tech); while (pass != NULL) { izanagi::shader::S_SHD_PASS sPass; { FILL_ZERO(&sPass, sizeof(sPass)); IZ_PCSTR name = ::cgGetPassName(pass); IZ_UINT pos = CStringChunk::GetInstance().Register(name); //sPass.name = *(IZ_PCSTR*)(&pos); sPass.posName = pos; sPass.keyName = izanagi::CKey::GenerateValue(name); sPass.TechniqueIdx = nTechIdx; sPass.numConst = _GetUsedParamNum(m_ParamList, pass); sPass.numSampler = _GetUsedParamNum(m_SamplerList, pass); sPass.sizeVS = _GetFileSize(config, m_CompiledVSList[nPassIdx]); sPass.sizePS = _GetFileSize(config, m_CompiledPSList[nPassIdx]); nConstNum += sPass.numConst; nSamplerNum += sPass.numSampler; } VRETURN( CPassUtil::SetStateValue( sPass.state, pass)); // 出力 VRETURN(ExportData(sPass)); pass = ::cgGetNextPass(pass); nPassIdx++; IZ_UINT nMax = IZ_MAX(sPass.sizeVS, sPass.sizePS); m_ShdHeader.maxProgamSize = IZ_MAX(m_ShdHeader.maxProgamSize, nMax); sPassHeader.numPass++; } tech = ::cgGetNextTechnique(tech); nTechIdx++; } m_ShdHeader.numPass = sPassHeader.numPass; // NOTE // 全体でのシェーダ定数、サンプラの総数を知りたい m_ShdHeader.numParam = nConstNum; m_ShdHeader.numSmpl = nSamplerNum; VRETURN(ExportUsedParamAndSamplerIdxByPass()); VRETURN(_EndExportChunk(sPassHeader, cSeekHelper)); return TRUE; }
BOOL CGuideDoc::OnOpenDocument(LPCTSTR lpszPathName) { // create backup if required if (theApp.GetAutoBackup()) BackupFile(lpszPathName); // == [0-byte file handling] ============================================ // // Treat specially if file is 0 bytes. This is required so that // when the app is asked to open a 0 byte file, it acts as though // it is doing a file->new, but with the associated file name. // This is required when you do New -> Guide from the explorer // RMB. Explorer creates a 0 byte file and calls Guide.exe with // the filename as argument. DWORD fileSize = 0; if (_GetFileSize(lpszPathName, fileSize) && fileSize == 0) { // delete current contents DeleteContents(); // load an empty guide m_pGuide = guide_create(); // return success return TRUE; } // ====================================================================== // load the file as a new document unsigned os_errcode; uint32 gde_format = 0; struct guide_t *pNewDocument = guide_load(lpszPathName, &os_errcode, &gde_format); // could we open it? if (pNewDocument == NULL) { // could not open file: display an error message // get only the filename out CString fullName(lpszPathName); fullName.Replace(_T('/'), _T('\\')); int slashPos = fullName.ReverseFind(_T('\\')); LPCTSTR fileName = static_cast<LPCTSTR>(fullName) + slashPos + 1; // note: if \ is not present, slashPos == -1. // we form the error message in 'msg' CString msg; if (os_errcode == 0) { msg.FormatMessage(IDS_NOGDE, fileName); } else { if (os_errcode == 32) { // show a specific message for file locked msg.FormatMessage(IDS_FILELOCKED, fileName); } else { // show a generic message otherwise msg.FormatMessage(IDS_FILEOPENERR, fileName, getMessage(os_errcode)); } } // display the message AfxMessageBox(msg, MB_OK|MB_ICONSTOP); // return error return FALSE; } // now we can delete the old document DeleteContents(); ASSERT(m_pGuide == NULL); // use the new one hereafter m_pGuide = pNewDocument; // opened: "lock" the .gde file m_Locker.lock(lpszPathName); // migrate old (v1.x gde file format) links to new ones if (gde_format == 1) migrateLinks(m_pGuide); // success! return TRUE; }