O3DGCErrorCode LoadBinAC(Vector<long> & data, const BinaryStream & bstream, unsigned long & iterator) { size_t sizeSize = bstream.ReadUInt32Bin(iterator) - 8; size_t size = bstream.ReadUInt32Bin(iterator); if (size == 0) { return O3DGC_OK; } unsigned char * buffer = 0; bstream.GetBuffer(iterator, buffer); iterator += sizeSize; data.Allocate(size); Arithmetic_Codec acd; acd.set_buffer(sizeSize, buffer); acd.start_decoder(); Adaptive_Bit_Model bModel; #ifdef DEBUG_VERBOSE printf("-----------\nsize %i\n", size); fprintf(g_fileDebugTF, "size %i\n", size); #endif //DEBUG_VERBOSE for(size_t i = 0; i < size; ++i) { data.PushBack(acd.decode(bModel)); #ifdef DEBUG_VERBOSE printf("%i\t%i\n", i, data[i]); fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]); #endif //DEBUG_VERBOSE } return O3DGC_OK; }
O3DGCErrorCode LoadUIntAC(Vector<long> & data, const unsigned long M, const BinaryStream & bstream, unsigned long & iterator) { unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12; unsigned long size = bstream.ReadUInt32Bin(iterator); if (size == 0) { return O3DGC_OK; } long minValue = bstream.ReadUInt32Bin(iterator); unsigned char * buffer = 0; bstream.GetBuffer(iterator, buffer); iterator += sizeSize; data.Allocate(size); Arithmetic_Codec acd; acd.set_buffer(sizeSize, buffer); acd.start_decoder(); Adaptive_Data_Model mModelValues(M+1); #ifdef DEBUG_VERBOSE printf("-----------\nsize %i\n", size); fprintf(g_fileDebugTF, "size %i\n", size); #endif //DEBUG_VERBOSE for(unsigned long i = 0; i < size; ++i) { data.PushBack(acd.decode(mModelValues)+minValue); #ifdef DEBUG_VERBOSE printf("%i\t%i\n", i, data[i]); fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]); #endif //DEBUG_VERBOSE } return O3DGC_OK; }
O3DGCErrorCode LoadIntACEGC(Vector<long> & data, const unsigned long M, const BinaryStream & bstream, unsigned long & iterator) { size_t sizeSize = bstream.ReadUInt32Bin(iterator) - 12; size_t size = bstream.ReadUInt32Bin(iterator); if (size == 0) { return O3DGC_OK; } long minValue = bstream.ReadUInt32Bin(iterator) - O3DGC_MAX_LONG; unsigned char * buffer = 0; bstream.GetBuffer(iterator, buffer); iterator += sizeSize; data.Allocate(size); Arithmetic_Codec acd; acd.set_buffer(sizeSize, buffer); acd.start_decoder(); Adaptive_Data_Model mModelValues(M+2); Static_Bit_Model bModel0; Adaptive_Bit_Model bModel1; unsigned long value; #ifdef DEBUG_VERBOSE printf("-----------\nsize %i\n", size); fprintf(g_fileDebugTF, "size %i\n", size); #endif //DEBUG_VERBOSE for(size_t i = 0; i < size; ++i) { value = acd.decode(mModelValues); if ( value == M) { value += acd.ExpGolombDecode(0, bModel0, bModel1); } data.PushBack(value + minValue); #ifdef DEBUG_VERBOSE printf("%i\t%i\n", i, data[i]); fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]); #endif //DEBUG_VERBOSE } #ifdef DEBUG_VERBOSE fflush(g_fileDebugTF); #endif //DEBUG_VERBOSE return O3DGC_OK; }
bool SV_ReturnCharacter(ServerConnection* conn, Packet& pack) { uint8_t packet_id; pack >> packet_id; if(packet_id != 0xCF) return false; uint32_t p_size; uint32_t p_id1, p_id2, p_loglen, p_chrlen; pack >> p_size >> p_id1 >> p_id2 >> p_loglen >> p_chrlen; if(!p_chrlen) { Printf(LOG_Error, "[SV] Received NULL character from server ID %u!\n", conn->ID); return true; } char* p_logname_c = new char[p_loglen + 1]; p_logname_c[p_loglen] = 0; char* p_chrdata = new char[p_chrlen]; pack.GetData((uint8_t*)p_chrdata, p_chrlen); pack.GetData((uint8_t*)p_logname_c, p_loglen); std::string p_logname(p_logname_c); delete[] p_logname_c; CCharacter chr; BinaryStream bs; std::vector<uint8_t>& bsb = bs.GetBuffer(); for(uint32_t i = 0; i < p_chrlen; i++) bsb.push_back(p_chrdata[i]); delete[] p_chrdata; if(!chr.LoadFromStream(bs)) { Printf(LOG_Error, "[SV] Received bad character from server ID %u!\n", conn->ID); return true; } if(!Login_Exists(p_logname)) { Printf(LOG_Error, "[SV] Received character \"%s\" for non-existent login \"%s\" from server ID %u!\n", chr.Nick.c_str(), p_logname.c_str(), conn->ID); delete[] p_chrdata; return true; } bool sent_s = true; if(!Login_CharExists(p_logname, chr.Id1, chr.Id2, true)) { if (conn->Parent->Info.GameMode == GAMEMODE_Softcore) chr.HatId = Config::HatIDSoftcore; else if (conn->Parent->Info.GameMode == GAMEMODE_Sandbox) chr.HatId = Config::HatIDSandbox; else chr.HatId = Config::HatID; } bool should_unlock = true; bool should_save = true; if(should_save && ((chr.Id2 & 0x3F000000) != 0x3F000000)) { int srvHatId; if (conn->Parent->Info.GameMode == GAMEMODE_Softcore) srvHatId = Config::HatIDSoftcore; else if (conn->Parent->Info.GameMode == GAMEMODE_Sandbox) srvHatId = Config::HatIDSandbox; else srvHatId = Config::HatID; bool server_nosaving = ((conn->Parent->Info.ServerMode & SVF_NOSAVING) == SVF_NOSAVING); if(server_nosaving) { should_save = false; Printf(LOG_Trivial, "[SV] Not saving login \"%s\" (SVG_NOSAVING is set, flags %08X).\n", p_logname.c_str(), conn->Parent->Info.ServerMode); } else if(conn->Parent->Info.GameMode == GAMEMODE_Arena) { should_save = false; Printf(LOG_Trivial, "[SV] Not saving login \"%s\" (Arena).\n", p_logname.c_str()); } else if(chr.HatId != srvHatId) { //should_save = false; //Printf(LOG_Trivial, "[SV] Not saving login \"%s\" (chr.HatId==%d != srvHatId==%d; GameMode=%d)\n", chr.HatId, srvHatId, conn->Parent->Info.GameMode); Printf(LOG_Error, "[SV] Server ID %u sent login \"%s\" with bad HatID (chr.HatId==%d != srvHatId==%d; GameMode==%d); saving with srvHatId.\n", conn->ID, p_logname.c_str(), chr.HatId, srvHatId, conn->Parent->Info.GameMode); chr.HatId = srvHatId; } } BinaryStream obs; if(!chr.SaveToStream(obs)) { Printf(LOG_Error, "[SV] Internal error: unable to save A2C for character \"%s\", login \"%s\" from server ID %u!\n", chr.Nick.c_str(), p_logname.c_str(), conn->ID); return true; } std::vector<uint8_t>& obsb = obs.GetBuffer(); p_chrdata = new char[obsb.size()]; for(uint32_t i = 0; i < obsb.size(); i++) p_chrdata[i] = obsb[i]; p_chrlen = obsb.size(); bool p__locked_hat, p__locked; unsigned long p__id1, p__id2, p__srvid; if(!Login_GetLocked(p_logname, p__locked_hat, p__locked, p__id1, p__id2, p__srvid)) { Printf(LOG_Error, "[DB] Error: Login_GetLocked(\"%s\", ..., ..., ..., ...).\n", p_logname.c_str()); return true; } if(p__locked_hat) { Printf(LOG_Error, "[SV] Warning: server tried to return character for hat-locked login \"%s\".\n", p_logname.c_str()); should_save = false; should_unlock = false; } else if(!p__locked) { Printf(LOG_Error, "[SV] Warning: server tried to return character for unlocked login \"%s\".\n", p_logname.c_str()); should_save = false; should_unlock = false; } else if(p__id1 != p_id1 || p__id2 != p_id2) { Printf(LOG_Error, "[SV] Warning: server tried to return different character (%u:%u as opposed to locked %u:%u).\n", p_id1, p_id2, p__id1, p__id2); should_save = false; should_unlock = false; } else if(p__srvid != conn->ID) { Printf(LOG_Error, "[SV] Warning: server tried to return character while not owning it!\n"); should_save = false; should_unlock = false; } if(should_save && !Login_SetCharacter(p_logname, p_id1, p_id2, p_chrlen, p_chrdata, chr.Nick)) Printf(LOG_Error, "[DB] Error: Login_SetCharacter(\"%s\", %u, %u, %u, %08X, \"%s\").\n", p_logname.c_str(), p_id1, p_id2, p_chrlen, p_chrdata, chr.Nick.c_str()); else { Printf(LOG_Info, "[SV] Received character \"%s\" for login \"%s\" from server ID %u.\n", chr.Nick.c_str(), p_logname.c_str(), conn->ID); sent_s = SVCMD_ReceivedCharacter(conn, p_logname); should_unlock = true; } if(should_unlock) Login_SetLocked(p_logname, false, false, 0, 0, 0); // character left the server, so unlock it //conn->Parent->Layer-> if(conn->Parent->Info.ServerCaps & SERVER_CAP_DETAILED_INFO) { for(size_t i = 0; i < conn->Parent->Info.Locked.size(); i++) { if(conn->Parent->Info.Locked[i] == p_logname) { conn->Parent->Info.Locked.erase(conn->Parent->Info.Locked.begin()+i); i--; } } } delete[] p_chrdata; return sent_s; }
int testDecode(std::string & fileName) { std::string folder; long found = (long)fileName.find_last_of(PATH_SEP); if (found != -1) { folder = fileName.substr(0,found); } if (folder == "") { folder = "."; } std::string file(fileName.substr(found+1)); std::string outFileName = folder + PATH_SEP + file.substr(0, file.find_last_of(".")) + "_dec.obj"; std::vector< Vec3<Real> > points; std::vector< Vec3<Real> > normals; std::vector< Vec2<Real> > colors; std::vector< Vec2<Real> > texCoords; std::vector< Vec3<Index> > triangles; std::vector< unsigned long > matIDs; std::vector< Material > materials; std::string materialLib; std::string matFileName = folder + PATH_SEP + file.substr(0, file.find_last_of(".")) + ".mat"; bool ret = LoadMaterials(matFileName.c_str(), materials, materialLib); if (ret) { const size_t numMaterials = materials.size(); unsigned long n, shift = 0; for(size_t i = 0; i < numMaterials; ++i) { n = materials[i].m_numTriangles + shift; matIDs.resize(n, materials[i].m_id); shift = n; } } BinaryStream bstream; IndexedFaceSet<Index> ifs; FILE * fin = fopen(fileName.c_str(), "rb"); if (!fin) { return -1; } fseek(fin, 0, SEEK_END); unsigned long size = ftell(fin); bstream.Allocate(size); rewind(fin); unsigned long nread = (unsigned long)fread((void *) bstream.GetBuffer(), 1, size, fin); bstream.SetSize(size); if (nread != size) { return -1; } fclose(fin); std::cout << "Bitstream size (bytes) " << bstream.GetSize() << std::endl; SC3DMCDecoder<Index> decoder; // load header Timer timer; timer.Tic(); decoder.DecodeHeader(ifs, bstream); timer.Toc(); std::cout << "DecodeHeader time (ms) " << timer.GetElapsedTime() << std::endl; // allocate memory triangles.resize(ifs.GetNCoordIndex()); ifs.SetCoordIndex((Index * const ) &(triangles[0])); points.resize(ifs.GetNCoord()); ifs.SetCoord((Real * const ) &(points[0])); if (ifs.GetNNormal() > 0) { normals.resize(ifs.GetNNormal()); ifs.SetNormal((Real * const ) &(normals[0])); } if (ifs.GetNColor() > 0) { colors.resize(ifs.GetNColor()); ifs.SetColor((Real * const ) &(colors[0])); } if (ifs.GetNTexCoord() > 0) { texCoords.resize(ifs.GetNTexCoord()); ifs.SetTexCoord((Real * const ) &(texCoords[0])); } std::cout << "Mesh info "<< std::endl; std::cout << "\t# coords " << ifs.GetNCoord() << std::endl; std::cout << "\t# normals " << ifs.GetNNormal() << std::endl; std::cout << "\t# texcoords " << ifs.GetNTexCoord() << std::endl; std::cout << "\t# triangles " << ifs.GetNCoordIndex() << std::endl; // decode mesh timer.Tic(); decoder.DecodePlayload(ifs, bstream); timer.Toc(); std::cout << "DecodePlayload time (ms) " << timer.GetElapsedTime() << std::endl; std::cout << "Details" << std::endl; const SC3DMCStats & stats = decoder.GetStats(); std::cout << "\t CoordIndex " << stats.m_timeCoordIndex << " ms, " << stats.m_streamSizeCoordIndex <<" bytes (" << 8.0*stats.m_streamSizeCoordIndex / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Coord " << stats.m_timeCoord << " ms, " << stats.m_streamSizeCoord <<" bytes (" << 8.0*stats.m_streamSizeCoord / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Normal " << stats.m_timeNormal << " ms, " << stats.m_streamSizeNormal <<" bytes (" << 8.0*stats.m_streamSizeNormal / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t TexCoord " << stats.m_timeTexCoord << " ms, " << stats.m_streamSizeTexCoord <<" bytes (" << 8.0*stats.m_streamSizeTexCoord / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Color " << stats.m_timeColor << " ms, " << stats.m_streamSizeColor <<" bytes (" << 8.0*stats.m_streamSizeColor / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Float Attributes " << stats.m_timeFloatAttribute << " ms, " << stats.m_streamSizeFloatAttribute <<" bytes (" << 8.0*stats.m_streamSizeFloatAttribute / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Integer Attributes " << stats.m_timeFloatAttribute << " ms, " << stats.m_streamSizeFloatAttribute <<" bytes (" << 8.0*stats.m_streamSizeFloatAttribute / ifs.GetNCoord() <<" bpv)" <<std::endl; std::cout << "\t Reorder " << stats.m_timeReorder << " ms, " << 0 <<" bytes (" << 0.0 <<" bpv)" <<std::endl; std::cout << "Saving " << outFileName << " ..." << std::endl; ret = SaveOBJ(outFileName.c_str(), points, texCoords, normals, triangles, materials, matIDs, materialLib); if (!ret) { std::cout << "Error: SaveOBJ()\n" << std::endl; return -1; } std::cout << "Done." << std::endl; return 0; }