Bool HawkScriptManager::StateRegisterApi(HawkScriptState* pState,const AString& sLibName) { ApiMap::iterator lib_it = m_mApi.find(sLibName); if (lib_it == m_mApi.end()) return false; if (lib_it->second.size()) { Int32 iCnt = lib_it->second.size(); luaL_reg * luapkg = new luaL_reg[iCnt +1]; Int32 iIdx = 0; ScriptApiLib::iterator api_lib = lib_it->second.begin(); for (;api_lib!=lib_it->second.end();api_lib++) { luapkg[iIdx].name = api_lib->second.FuncName.c_str(); luapkg[iIdx].func = api_lib->second.FuncPtr; iIdx++; } luapkg[iCnt].name = 0; luapkg[iCnt].func = 0; luaL_register(pState->GetLuaState(), sLibName.c_str(), luapkg); lua_pop(pState->GetLuaState(), 1); HAWK_DELETE_ARRAY(luapkg); return true; } return false; }
Bool CExcelExporterDlg::ExportExcel(IllusionExcelFile& sExcel, const AString& sSheetName) { int iRow = sExcel.GetRowCount(); int iCol = sExcel.GetColumnCount(); //第一行: 数据类型(int, float, uchar[n]) //第二行: 字段名 //第三行: 字段注释 HawkAssert(iRow >= 3 && iCol > 0); if (iRow >= 3 && iCol > 0) { CString sVariable; AStringVector vTypes; AString sSheet = sSheetName; HawkAssert(sSheet.size()); if (!sSheet.size()) return false; //计算导出的数据格式 for (int i=1;i<=iCol;i++) { if (!sExcel.GetCellString(1, i).GetLength() || !sExcel.GetCellString(2, i).GetLength()) return false; AString sTypeName = sExcel.GetCellString(1, i).GetBuffer(0); AString sVarName = sExcel.GetCellString(2, i).GetBuffer(0); AString sVarDesc = sExcel.GetCellString(3, i).GetBuffer(0); vTypes.push_back(sTypeName); if (sTypeName != "int" && sTypeName != "float" && sTypeName.find("uchar") == AString::npos) return false; //字符数组转换-> unsigned char ***[n] if ( sTypeName.find("uchar") != AString::npos) { int iCap = HawkStringUtil::StringToInt<AString>(sTypeName.c_str() + strlen("uchar[")); sVariable.Format("%s\t//%s\r\n\tunsigned char %s[%d];\r\n", CString(sVariable).GetBuffer(0), sVarDesc.c_str(), sVarName.c_str(), iCap); } else { sVariable.Format("%s\t//%s\r\n\t%s %s;\r\n", CString(sVariable).GetBuffer(0), sVarDesc.c_str(), sTypeName.c_str(), sVarName.c_str()); } } //保存原始名字 AString sSheetName = sSheet; HawkStringUtil::UpCase<AString>(sSheet); //格式化导出模式文件 CString sStructFmt; sStructFmt.Format(STRUCT_FORMAT, sSheet.c_str(), sSheet.c_str(), sSheetName.c_str(), sVariable.GetBuffer(0)); OutputDebugString(sStructFmt.GetBuffer(0)); //存储模式文件 HawkDiskFile struct_file; char szExportFile[PAGE_SIZE] = {0}; sprintf(szExportFile, "Pattern/C++/%s.h", sSheetName.c_str()); _chmod(szExportFile, _S_IREAD | _S_IWRITE); if (struct_file.Open(szExportFile, HawkFile::OPEN_WRITE)) { struct_file.Write(sStructFmt.GetBuffer(0), sStructFmt.GetLength()); struct_file.Close(); } else { return false; } //二进制excel数据 OctetsStream xOS; //记录项数目 Int32 iCount = iRow - 3; xOS.Push<Int32>(iCount); for (int i=4; i<=iRow; i++) { for (int j=1;j<=iCol;j++) { AString sCellText = sExcel.GetCellString(i, j).GetBuffer(0); if (vTypes[j-1] == "int") { if (!sCellText.size()) sCellText = "0"; Int32 iVal = HawkStringUtil::StringToInt<AString>(sCellText); xOS.Push<Int32>(iVal); } else if (vTypes[j-1] == "float") { if (!sCellText.size()) sCellText = "0"; Float fVal = HawkStringUtil::StringToFloat<AString>(sCellText); xOS.Push<Float>(fVal); } else if (vTypes[j-1].find("uchar") != AString::npos) { UString sVal = HawkStringUtil::ToUtf8(sCellText); int iCap = HawkStringUtil::StringToInt<AString>(vTypes[j-1].c_str() + strlen("uchar[")); UChar* pBuf = new UChar[iCap]; memset(pBuf, 0, iCap); memcpy(pBuf, sVal.c_str(), sVal.size()); xOS.Push(pBuf, iCap); HAWK_DELETE_ARRAY(pBuf); } } } //数据压缩 UInt32 iSrcSize = xOS.Size(); ULong lComSize = HawkZip::GetRequiredSize(iSrcSize); Char* pComBuf = new Char[lComSize]; memset(pComBuf, 0, lComSize); HawkScope::DataArrayPtr scope(pComBuf); if (!HawkZip::Compress(pComBuf, lComSize, xOS.Begin(), iSrcSize)) return false; //压缩后做位反运算 for (ULong i=0;i<lComSize;i++) pComBuf[i] = (~pComBuf[i]); //压缩后的CRC校验 UInt32 iCrc = HawkStringUtil::CalcHash(pComBuf, lComSize); HawkDiskFile bin_file; sprintf(szExportFile, "BinCfg/%s.bin", sSheetName.c_str()); _chmod(szExportFile, _S_IREAD | _S_IWRITE); if (bin_file.Open(szExportFile, HawkFile::OPEN_WRITE)) { bin_file.Write(&iSrcSize, sizeof(iSrcSize)); bin_file.Write(&iCrc, sizeof(iCrc)); bin_file.Write(pComBuf, lComSize, true); bin_file.Close(); } else { return false; } return true; } return false; }