bool trpgSupportStyleTable::Read(trpgReadBuffer &buf) { trpgSupportStyle style; trpgToken styleTok; int32 len; bool status; int numStyle; int i; Reset(); try { buf.Get(numStyle); if (numStyle < 0) throw 1; //styles.resize(numStyle); for (i=0;i<numStyle;i++) { buf.GetToken(styleTok,len); if (styleTok != TRPG_SUPPORT_STYLE) throw 1; buf.PushLimit(len); style.Reset(); status = style.Read(buf); buf.PopLimit(); if (!status) throw 1; AddStyle(style); } } catch (...) { return false; } return isValid(); }
bool trpgMatTable::Read(trpgReadBuffer &buf) { trpgMaterial mat; trpgToken matTok; int32 len; bool status; int i,j; int nMat,nTable; try { buf.Get(nTable); buf.Get(nMat); if (nTable <= 0 || nMat < 0) throw 1; // Read the materials for (i=0;i<nTable;i++) for (j=0;j<nMat;j++) { buf.GetToken(matTok,len); if (matTok != TRPGMATERIAL) throw 1; buf.PushLimit(len); mat.Reset(); status = mat.Read(buf); buf.PopLimit(); if (!status) throw 1; AddMaterial(mat,false); } numTable += nTable; numMat = materialMap.size(); } catch (...) { return false; } return isValid(); }
bool trpgRangeTable::Read(trpgReadBuffer &buf) { int32 numRange; trpgToken tok; int32 len; valid = false; try { buf.Get(numRange); if (numRange < 0) throw 1; for (int i=0;i<numRange;i++) { // Read in the individual range buf.GetToken(tok,len); if (tok != TRPG_RANGE) throw 1; buf.PushLimit(len); trpgRange range; bool status = range.Read(buf); buf.PopLimit(); if (!status) throw 1; AddRange(range); } valid = true; } catch (...) { return false; } return isValid(); }
bool trpgTexTable::Read(trpgReadBuffer &buf) { int32 numTex; trpgToken texTok; int32 len; try { buf.Get(numTex); for (int i=0;i<numTex;i++) { buf.GetToken(texTok,len); if (texTok != TRPGTEXTURE) throw 1; buf.PushLimit(len); trpgTexture tex; bool status = tex.Read(buf); //set the block for multi-archive archives (version>=2.3) if((currentRow!=-1)&&(currentCol!=-1)) { trpgwAppAddress taddr; tex.GetImageAddr(taddr); taddr.col = currentCol; taddr.row = currentRow; tex.SetImageAddr(taddr); } AddTexture(tex); buf.PopLimit(); if (!status) throw 1; } } catch (...) { return false; } return true; }
// Read the LOD info (separate token) bool trpgHeader::ReadLodInfo(trpgReadBuffer &buf) { float64 range; trpg2iPoint pt; trpg2dPoint sz; try { for (int i = 0; i < numLods; i++) { buf.Get(pt); buf.Get(range); buf.Get(sz); lodSizes.push_back(pt); lodRanges.push_back(range); tileSize.push_back(sz); } } catch (...) { return false; } return true; }
bool trpgModelTable::Read(trpgReadBuffer &buf) { int32 numModel; trpgToken tok; int32 len; bool status; try { buf.Get(numModel); for (int i=0;i<numModel;i++) { trpgModel model; buf.GetToken(tok,len); bool readHandle; if (tok == TRPGMODELREF) readHandle = false; else if (tok == TRPGMODELREF2) readHandle = true; else throw 1; buf.PushLimit(len); status = model.Read(buf,readHandle); buf.PopLimit(); if (!status) throw 1; AddModel(model); } } catch (...) { return false; } return isValid(); }
// Read in LOD bool trpgLod::Read(trpgReadBuffer &buf) { try { buf.Get(id); buf.Get(numRange); if (numRange < 0) throw 1; buf.Get(center); buf.Get(switchIn); buf.Get(switchOut); buf.Get(width); if ( !buf.isEmpty() ) { char nm[1024] = {0}; buf.Get(nm,1024); if (*nm) SetName(nm); // Look for a range index if (!buf.isEmpty()) buf.Get(rangeIndex); } } catch (...) { return false; } return isValid(); }
bool trpgLightTable::Read(trpgReadBuffer &buf) { int32 numLights; trpgToken lightTok; int32 len; try { buf.Get(numLights); for (int i=0;i<numLights;i++) { buf.GetToken(lightTok,len); if (lightTok != TRPGLIGHTATTR) throw 1; buf.PushLimit(len); trpgLightAttr light;// = lightList[i]; bool status = light.Read(buf); buf.PopLimit(); if (!status) throw 1; AddLightAttr(light); } } catch (...) { return false; } return true; }
bool trpgLabel::Read(trpgReadBuffer &buf) { int numSupport,i; trpg3dPoint support; int iVal; try { buf.Get(iVal); propertyId = iVal; buf.Get(text); buf.Get(iVal); alignment = (AlignmentType)iVal; buf.Get(tabSize); buf.Get(scale); buf.Get(thickness); buf.Get(desc); buf.Get(url); buf.Get(location); buf.Get(numSupport); if (numSupport < 0) throw 1; for (i=0;i<numSupport;i++) { buf.Get(support); supports.push_back(support); } } catch (...) { return false; } return isValid(); }
// Reads this class from a read buffer bool trpgLight::Read(trpgReadBuffer &buf) { Reset(); int numVertices; buf.Get(index); buf.Get(numVertices); for ( int i = 0; i < numVertices; i++ ) { trpg3dPoint vx; buf.Get(vx); lightPoints.push_back(vx); } return isValid(); }
bool trpgTexture1_0::Read(trpgReadBuffer &buf) { mode = External; try { char texName[1024]; buf.Get(texName,1023); SetName(texName); buf.Get(useCount); } catch (...) { return false; } return true; }
// Read model reference bool trpgModelRef::Read(trpgReadBuffer &buf) { try { buf.Get(modelRef); if (modelRef < 0) throw 1; for (int i=0;i<4;i++) for (int j=0;j<4;j++) buf.Get(m[i][j]); } catch (...) { return false; } valid = true; return isValid(); }
// Read layer bool trpgLayer::Read(trpgReadBuffer &buf) { try { buf.Get(numChild); if (numChild < 0) throw 1; buf.Get(id); if (id < 0) throw 1; if ( !buf.isEmpty() ) { char nm[1024] = {0}; buf.Get(nm,1024); SetName(nm); } } catch (...) { return false; } return isValid(); }
// Read transform bool trpgTransform::Read(trpgReadBuffer &buf) { try { buf.Get(numChild); buf.Get(id); if (numChild < 0) throw 1; for (int i=0;i<4;i++) for (int j=0;j<4;j++) buf.Get(m[i][j]); if ( !buf.isEmpty() ) { char nm[1024] = {0}; buf.Get(nm,1024); SetName(nm); } } catch (...) { return false; } return isValid(); }
// Read function bool trpgTexture::Read(trpgReadBuffer &buf) { char texName[1024]; try { buf.Get(texName,1023); SetName(texName); buf.Get(useCount); mode = External; // New in 2.0 from here down unsigned char bval; buf.Get(bval); mode = (trpgTexture::ImageMode)bval; buf.Get(bval); type = (trpgTexture::ImageType)bval; GetImageDepth(numLayer); // heh buf.Get(sizeX); buf.Get(sizeY); buf.Get(addr.file); buf.Get(addr.offset); int32 ival; buf.Get(ival); // Read the handle if we can.. try { int32 tempHandle; if(buf.Get(tempHandle)) { writeHandle = true; handle = tempHandle; } else { handle = -1; } } catch (...) { handle = -1; } isMipmap = (ival) ? true : false; } catch (...) { return false; } if (!isValid()) return false; // calculate the mip level sizes CalcMipLevelSizes(); return true; }
// Read Attach node bool trpgChildRef::Read(trpgReadBuffer &buf) { try { buf.Get(lod); if (lod < 0) throw 1; buf.Get(x); buf.Get(y); buf.Get(addr.file); buf.Get(addr.offset); buf.Get(zmin); buf.Get(zmax); } catch (...) { return false; } return true; }
bool trpgRange::Read(trpgReadBuffer &buf) { char catStr[1024],subStr[1024]; Reset(); valid = false; try { buf.Get(inLod); buf.Get(outLod); buf.Get(priority); buf.Get(catStr,1024); buf.Get(subStr,1024); SetCategory(catStr,subStr); // Read the handle if we can.. try { int32 tempHandle; if(buf.Get(tempHandle)) { handle = tempHandle; } else { handle = -1; } } catch (...) { handle = -1; } valid = true; } catch (...) { return false; } return isValid(); }
bool trpgLabelPropertyTable::Read(trpgReadBuffer &buf) { trpgLabelProperty property; trpgToken propertyTok; int32 len; bool status; int numProperty; int i; Reset(); try { buf.Get(numProperty); if (numProperty < 0) throw 1; //properties.resize(numProperty); for (i=0;i<numProperty;i++) { buf.GetToken(propertyTok,len); if (propertyTok != TRPG_LABEL_PROPERTY) throw 1; buf.PushLimit(len); property.Reset(); status = property.Read(buf); buf.PopLimit(); if (!status) throw 1; //properties[i] = property; AddProperty(property); } } catch (...) { return false; } return isValid(); }
bool trpgTexTable1_0::Read(trpgReadBuffer &buf) { int32 numTex; try { buf.Get(numTex); //texList.resize(numTex); for (int i=0;i<numTex;i++) { trpgTexture1_0 tex1_0; tex1_0.Read(buf); AddTexture(tex1_0); //texList[i] = tex1_0; } } catch (...) { return false; } valid = true; return true; }
// Read billboard bool trpgBillboard::Read(trpgReadBuffer &buf) { uint8 uChar; try { buf.Get(numChild); buf.Get(id); buf.Get(uChar); type = uChar; buf.Get(uChar); mode = uChar; buf.Get(center); buf.Get(axis); if ( !buf.isEmpty() ) { char nm[1024] = {0}; buf.Get(nm,1024); SetName(nm); } } catch (...) { return false; } return isValid(); }
// Read Attach node bool trpgAttach::Read(trpgReadBuffer &buf) { try { buf.Get(numChild); buf.Get(id); if (id < 0) throw 1; buf.Get(parentID); if (parentID < 0) throw 1; buf.Get(childPos); if (childPos < 0) throw 1; if ( !buf.isEmpty() ) { char nm[1024] = {0}; buf.Get(nm,1024); SetName(nm); } } catch (...) { return false; } return true; }
/* Parse Buffer This runs through the given buffer parsing token sets until it (1) runs out of buffer or (2) fails. Note: Good place to return an exception, but keep it simple for now. */ bool trpgr_Parser::Parse(trpgReadBuffer &buf) { bool ret = true; try { while (!buf.isEmpty()) { /* We're expecting the following Token (int32) Length (int32) Data (variable) */ trpgToken tok; int32 len; if (!buf.Get(tok)) throw 1; // Push and Pop are special - no data if (tok != TRPG_PUSH && tok != TRPG_POP) { if (!buf.Get(len)) throw 1; if (!TokenIsValid(tok)) throw 1; if (len < 0) throw 1; // Limit what we're reading to the length of this buf.PushLimit(len); } // Call our token handler for this one try { const trpgr_Token *tcb = NULL; tok_map::const_iterator p = tokenMap.find(tok); if (p != tokenMap.end()) tcb = &(*p).second; if (!tcb) // No such token, call the default tcb = &defCb; // Run the callback if (tcb->cb) { void *ret = tcb->cb->Parse(tok,buf); // Note: Do something with the return value lastObject = ret; } } catch (...) { // Don't want to screw up the limit stack } // No limit to worry about with push and pop if (tok != TRPG_PUSH && tok != TRPG_POP) { buf.SkipToLimit(); buf.PopLimit(); } } } catch (...) { // Failed to parse. ret = false; } return ret; }
bool trpgMatTable1_0::Read(trpgReadBuffer &buf) { trpgMaterial mat; trpgToken matTok; int32 len; bool status; unsigned int i,j,k; std::vector<trpgShortMaterial> shortTable; std::vector<trpgMaterial> baseMats; try { buf.Get(numTable); buf.Get(numMat); if (numTable <= 0 || numMat < 0) throw 1; // Short material tables are always full size shortTable.resize(numTable*numMat); // Look for short material table buf.GetToken(matTok,len); if (matTok == TRPGSHORTMATTABLE) { int32 numTex,texId; buf.PushLimit(len); for (i=0;i<(unsigned int)numTable;i++) { for (j=0;j<(unsigned int)numMat;j++) { trpgShortMaterial &smat = shortTable[i*numMat+j]; buf.Get(smat.baseMat); buf.Get(numTex); for (k=0;k<(unsigned int)numTex;k++) { buf.Get(texId); smat.texids.push_back(texId); } } } buf.PopLimit(); // Now read the base materials int32 numBaseMat; buf.Get(numBaseMat); if (numBaseMat < 0) throw 1; baseMats.resize(numBaseMat); for (i=0;i<(unsigned int)numBaseMat;i++) { buf.GetToken(matTok,len); if (matTok != TRPGMATERIAL) throw 1; buf.PushLimit(len); mat.Reset(); status = mat.Read(buf); buf.PopLimit(); if (!status) throw 1; baseMats[i] = mat; } } } catch (...) { return false; } // Now convert to the new style material table for (i=0;i<shortTable.size();i++) { trpgShortMaterial &shortMat = shortTable[i]; trpgMaterial &baseMat = baseMats[shortMat.baseMat]; AddMaterial(baseMat,false); trpgMaterial newMat = baseMat; newMat.SetNumTexture(shortMat.texids.size()); for (j=0;j<shortMat.texids.size();j++) { int texId; trpgTextureEnv texEnv; baseMat.GetTexture(j,texId,texEnv); newMat.SetTexture(j,shortMat.texids[j],texEnv); } } valid = true; return true; }
// Read in the header bool trpgHeader::Read(trpgReadBuffer &buf) { uint8 i8; trpgToken tok; bool status; int32 len; try { buf.Get(verMajor); buf.Get(verMinor); buf.Get(dbVerMajor); buf.Get(dbVerMinor); buf.Get(origin); buf.Get(sw); buf.Get(ne); buf.Get(i8); tileType = (trpgTileType)i8; buf.Get(numLods); if (numLods < 0) throw 1; // Read in the LOD range info buf.GetToken(tok,len); if (tok != TRPGHEAD_LODINFO) throw 1; buf.PushLimit(len); status = ReadLodInfo(buf); buf.PopLimit(); if (!status) throw 1; // Added after the first version (but still in 1.0) buf.Get(maxGroupID); if((verMajor >= TRPG_NOMERGE_VERSION_MAJOR) && (verMinor >=TRPG_NOMERGE_VERSION_MINOR)) { buf.Get(flags); buf.Get(rows); buf.Get(cols); } } catch (...) { return false; } return isValid(); }
bool trpgLocalMaterial::Read(trpgReadBuffer &buf) { try { buf.Get(baseMatTable); buf.Get(baseMat); buf.Get(sx); buf.Get(sy); buf.Get(ex); buf.Get(ey); buf.Get(destWidth); buf.Get(destHeight); buf.Get(addr[0].file); buf.Get(addr[0].offset); if ( !buf.isEmpty() ) { // there might be more int extraAddrs; buf.Get(extraAddrs); if (extraAddrs!=0) { addr.resize(extraAddrs+1); for (int i=1;i<=extraAddrs;i++) { buf.Get(addr[i].file); buf.Get(addr[i].offset); addr[i].col = -1; addr[i].row = -1; } } } } catch (...) { return false; } return isValid(); }
bool trpgModel::Read(trpgReadBuffer &buf, bool hasHandle) { // MD: added complexity here - written multiple ways by // mistake, unraveling the various cases. char tmpName[1024]; try { buf.Get(type); // TerraPage 2.2 will store the unique handle after the type // we use a different token, so this is backwards compatible. if (hasHandle) { int32 tempHandle; if (buf.Get(tempHandle)) { handle = tempHandle; } else { handle = -1; } } else handle = -1; if (type == Local) { // two possibilities: // name, diskRef, useCount // diskRef, useCount // diskRef + useCount = 12 bytes... if (buf.TestLimit(13)) { buf.Get(tmpName, 1023); SetName(tmpName); } buf.Get(diskRef); buf.Get(useCount); } else { // two possibilities: // name, diskRef, useCount // name, useCount buf.Get(tmpName, 1023); SetName(tmpName); // useCount = 4 bytes... if (buf.TestLimit(5)) buf.Get(diskRef); buf.Get(useCount); } } catch (...) { return false; } // going to make this fail if the buffer isn't empty. if (buf.TestLimit(1)) return false; return isValid(); }
bool trpgTileTable::Read(trpgReadBuffer &buf) { valid = false; try { int imode; buf.Get(imode); mode = (TileMode)imode; if (mode != External && mode != Local && mode != ExternalSaved) throw 1; if (mode == Local || mode == ExternalSaved) { int numLod; buf.Get(numLod); if (numLod <= 0) throw 1; lodInfo.resize(numLod); for (int i=0;i<numLod;i++) { LodInfo &li = lodInfo[i]; if(localBlock) { if(li.addr.size()==0) { li.addr.resize(1); li.elev_min.resize(1,0.0); li.elev_max.resize(1,0.0); } int32 x,y; buf.Get(x); buf.Get(y); int pos = (currentRow * li.numX) + currentCol; int32 file,offset; buf.Get(file); buf.Get(offset); trpgwAppAddress &ref = li.addr[pos]; ref.file = file; ref.offset = offset; ref.col = currentCol; ref.row = currentRow; float emin,emax; buf.Get(emin); buf.Get(emax); li.elev_max[pos] = emax; li.elev_min[pos] = emin; } else { buf.Get(li.numX); buf.Get(li.numY); if (li.numX <= 0 || li.numY <= 0) throw 1; int numTile = li.numX*li.numY; li.addr.resize(numTile); li.elev_min.resize(numTile); li.elev_max.resize(numTile); int j; for (j=0;j<numTile;j++) { int32 file,offset; buf.Get(file); buf.Get(offset); trpgwAppAddress &ref = li.addr[j]; ref.file = file; ref.offset = offset; } for (j=0;j<numTile;j++) { float emin,emax; buf.Get(emin); buf.Get(emax); li.elev_max[j] = emax; li.elev_min[j] = emin; } } } } valid = true; } catch (...) { printf("Caught an exception\n"); return false; } return isValid(); }