Beispiel #1
0
void TestBinaryStream::TestArray()
{
	Registry R;
	R.AddClass<Array>("Array");
	R.AddClass<int>("int");

	Pointer<Array> A = R.New<Array>();
	A->Append(R.New(0));
	A->Append(R.New(1));
	A->Append(R.New(2));
	A->Append(R.New(3));

	BinaryStream S;
	S.SetRegistry(&R);

	S << A;
	Object Q;
	S >> Q;

	KAI_TEST_FALSE(S.CanRead(1));
	KAI_TEST_EQUIV(Q.GetTypeNumber(), Type::Traits<Array>::Number);
	const Array &B = ConstDeref<Array>(Q);
	KAI_TEST_EQUIV(B.Size(), 4);
	KAI_TEST_EQUIV(ConstDeref<int>(B.At(0)), 0);
	KAI_TEST_EQUIV(ConstDeref<int>(B.At(1)), 1);
	KAI_TEST_EQUIV(ConstDeref<int>(B.At(2)), 2);
	KAI_TEST_EQUIV(ConstDeref<int>(B.At(3)), 3);
}
    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;
    }
Beispiel #3
0
    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;
    }
Beispiel #4
0
Message FromMsgExecReply(const MsgExecReply& result){
	BinaryStream stream;
	stream<<result.exitStatus;
	stream.Write(result.type);
	stream<<result.output<<result.error<<result.memory<<result.time;
	Message msg;
	msg.type=EXEC_REPLY;
	msg.size=stream.Length();
	msg.body=stream.buffer;
	return msg;
}
Beispiel #5
0
typename std::enable_if<std::is_trivially_copyable<T>::value, BinaryStream&>::type operator>>(BinaryStream& stream,
                                                                                              T& value)
{
	std::streamsize r = stream.readHard(&value, sizeof(value));
	if (r != static_cast<std::streamsize>(sizeof(value)))
	{
		stream.setState(std::ios_base::failbit);
	}

	return stream;
}
Beispiel #6
0
  bool operator()(BinaryStream const& rBinStrm, TOffset Offset, Operand* pOprd)
  {
    u16 Seg;
    OffType Off;

    rBinStrm.Read(Offset + sizeof(OffType), Seg);
    rBinStrm.Read(Offset, Off);
    pOprd->SetValue(Off);
    pOprd->SetSegValue(Seg);
    pOprd->SetType(OpType | O_SEG_VAL);
    return true;
  }
Beispiel #7
0
bool GameBoyLoader::IsCompatible(BinaryStream const& rBinStrm)
{
  if (rBinStrm.GetSize() < GAMEBOY_HEADER_OFFSET + sizeof m_GameBoyRom)
    return false;

  if (!rBinStrm.Read(GAMEBOY_HEADER_OFFSET, reinterpret_cast<u8*>(&m_GameBoyRom), sizeof m_GameBoyRom))
    return false;

  if (memcmp(m_GameBoyRom.NintendoLogo, s_NintendoLogo, NINTENDO_LOGO_LEN))
    return false;

  return true;
}
 O3DGCErrorCode    LoadIntData(Vector<long> & data,
                               const BinaryStream & bstream,
                               unsigned long & iterator) 
 {
     bstream.ReadUInt32ASCII(iterator);
     const unsigned long size = bstream.ReadUInt32ASCII(iterator);
     data.Allocate(size);
     data.Clear();
     for(size_t i = 0; i < size; ++i)
     {
         data.PushBack(bstream.ReadIntASCII(iterator));
     }
     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;
    }
Beispiel #10
0
  bool operator()(BinaryStream const& rBinStrm, TOffset Offset, Instruction& rInsn, Operand* pOprd)
  {
    ConstType ct;

    rBinStrm.Read(Offset, ct);
    pOprd->SetValue(ct);
    pOprd->SetType(OpType);
    pOprd->SetOffset(static_cast<u8>(rInsn.GetLength()));
    rInsn.Length() += sizeof(ct);
    return true;
  }
Beispiel #11
0
 O3DGCErrorCode    LoadBinData(Vector<long> & data,
                               const BinaryStream & bstream,
                               unsigned long & iterator) 
 {
     bstream.ReadUInt32ASCII(iterator);
     const unsigned long size = bstream.ReadUInt32ASCII(iterator);
     long symbol;
     data.Allocate(size * O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
     data.Clear();
     for(size_t i = 0; i < size;)
     {
         symbol = bstream.ReadUCharASCII(iterator);
         for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0; ++h)
         {
             data.PushBack(symbol & 1);
             symbol >>= 1;
             ++i;
         }
     }
     return O3DGC_OK;
 }
Beispiel #12
0
void TestBinaryStream::TestObject()
{
	Registry R;
	R.AddClass<int>("int");

	BinaryStream S;
	S.SetRegistry(&R);

	Pointer<int> N = R.New(42);
	S << N;
	Object Q;
	S >> Q;
	KAI_TEST_FALSE(S.CanRead(1));
	KAI_TEST_EQUIV(Q.GetTypeNumber(), Type::Traits<int>::Number);
	KAI_TEST_EQUIV(ConstDeref<int>(Q), 42);

	S.Clear();
	KAI_TEST_TRUE(S.Empty());

	N.Set("child0", R.New(123));
	S << N;
	Object M;
	S >> M;
	KAI_TEST_FALSE(S.CanRead(1));
	KAI_TEST_EQUIV(ConstDeref<int>(M), 42);
	KAI_TEST_TRUE(M.Has("child0"));
	KAI_TEST_EQUIV(ConstDeref<int>(M.Get("child0")), 123);
}
Beispiel #13
0
    O3DGCErrorCode    CompressedTriangleFans::SaveUIntAC(const Vector<long> & data,
                                                         const unsigned long M,
                                                         BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();     
        const unsigned int NMAX = data.GetSize() * 8 + 100;
        const size_t size       = data.GetSize();
        long minValue = O3DGC_MAX_LONG;
        bstream.WriteUInt32Bin(0);
        bstream.WriteUInt32Bin(size);
        if (size > 0)
        {
    #ifdef DEBUG_VERBOSE
            printf("-----------\nsize %i, start %i\n", size, start);
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
    #endif //DEBUG_VERBOSE

            for(size_t i = 0; i < size; ++i)
            {
                if (minValue > data[i]) 
                {
                    minValue = data[i];
                }
    #ifdef DEBUG_VERBOSE
                printf("%i\t%i\n", i, data[i]);
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
    #endif //DEBUG_VERBOSE
            }
            bstream.WriteUInt32Bin(minValue);
            if ( m_sizeBufferAC < NMAX )
            {
                delete [] m_bufferAC;
                m_sizeBufferAC = NMAX;
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
            }
            Arithmetic_Codec ace;
            ace.set_buffer(NMAX, m_bufferAC);
            ace.start_encoder();
            Adaptive_Data_Model mModelValues(M+1);
            for(size_t i = 0; i < size; ++i)
            {
                ace.encode(data[i]-minValue, mModelValues);
            }
            unsigned long encodedBytes = ace.stop_encoder();
            for(size_t i = 0; i < encodedBytes; ++i)
            {
                bstream.WriteUChar8Bin(m_bufferAC[i]);
            }
        }
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
Beispiel #14
0
  bool operator()(BinaryStream const& rBinStrm, X86_Bit Bit, TOffset Offset, Instruction& rInsn, Operand* pOprd)
  {
    ConstType ct;

    rBinStrm.Read(Offset, ct);
    pOprd->SetValue(SignExtend<ConstType, Pos>(ct));
    switch (Bit)
    {
    case X86_Bit_16: pOprd->SetType(O_REL16); break;
    case X86_Bit_32: pOprd->SetType(O_REL32); break;
    case X86_Bit_64: pOprd->SetType(O_REL64); break;
    default:         pOprd->SetType(O_NONE);  break;
    }
    pOprd->SetOffset(static_cast<u8>(rInsn.GetLength()));
    rInsn.Length() += sizeof(ct);
    return true;
  }
Beispiel #15
0
 O3DGCErrorCode    SaveIntData(const Vector<long> & data,
                               BinaryStream & bstream) 
 {
     unsigned long start = bstream.GetSize();
     bstream.WriteUInt32ASCII(0);
     const size_t size       = data.GetSize();
     bstream.WriteUInt32ASCII(size);
     for(size_t i = 0; i < size; ++i)
     {
         bstream.WriteIntASCII(data[i]);
     }
     bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
     return O3DGC_OK;
 }
Beispiel #16
0
bool Architecture::FormatCharacter(
  Document      const& rDoc,
  BinaryStream  const& rBinStrm,
  Address       const& rAddr,
  Character     const& rChar,
  std::string        & rStrCell,
  Cell::Mark::List   & rMarks) const
{
  std::ostringstream oss;
  TOffset Off;

  if (!rDoc.ConvertAddressToFileOffset(rAddr, Off))
    return false;

  switch (rChar.GetSubType())
  {
  case Character::AsciiCharacterType: default:
    {
      s8 Char;
      if (!rBinStrm.Read(Off, Char))
        return false;

      switch (Char)
      {
      case '\0': oss << "\\0"; break;
      case '\a': oss << "\\a"; break;
      case '\b': oss << "\\b"; break;
      case '\t': oss << "\\t"; break;
      case '\n': oss << "\\n"; break;
      case '\v': oss << "\\v"; break;
      case '\f': oss << "\\f"; break;
      case '\r': oss << "\\r"; break;
      default:   oss << Char;  break;
      }
    }
  }
  rMarks.push_back(Cell::Mark(Cell::Mark::StringType, 1));
  rStrCell = oss.str();
  return true;
}
Beispiel #17
0
KAI_TESTS_BEGIN

void TestBinaryStream::TestBuiltins()
{
	BinaryStream S;

	S << 42;
	KAI_TEST_TRUE(S.Size() == sizeof(int));
	KAI_TEST_TRUE(S.CanRead(sizeof(int)));
	int N;
	S >> N;
	KAI_TEST_EQUIV(N, 42);
	KAI_TEST_FALSE(S.CanRead(1));

	S.Clear();
	KAI_TEST_TRUE(S.Size() == 0);
	KAI_TEST_FALSE(S.CanRead(1));

	S << String("Hello, world");
	String T;
	S >> T;
	KAI_TEST_FALSE(S.CanRead(1));
	KAI_TEST_EQUIV(T, "Hello, world");
}
Beispiel #18
0
 O3DGCErrorCode    SaveBinData(const Vector<long> & data,
                               BinaryStream & bstream) 
 {
     unsigned long start = bstream.GetSize();
     bstream.WriteUInt32ASCII(0);
     const size_t size = data.GetSize();
     long symbol;
     bstream.WriteUInt32ASCII(size);
     for(size_t i = 0; i < size; )
     {
         symbol = 0;
         for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 && i < size; ++h)
         {
             symbol += (data[i] << h);
             ++i;
         }
         bstream.WriteUCharASCII((unsigned char) symbol);
     }
     bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
     return O3DGC_OK;
 }
Beispiel #19
0
void WFObjLoader::loadWeight(BinaryStream& stream, Figure *fig)
{
	int weightIndexSize = stream.readInt();
	int weightIndex[weightIndexSize];
	for (int i = 0; i < weightIndexSize; i++) {
		weightIndex[i] = stream.readInt();
	}
	
	int weightsSize = stream.readInt();
	float weights[weightsSize];
	for (int i = 0; i < weightIndexSize; i++) {
		weights[i] = stream.readFloat();
	}
	
	int vcount = stream.readInt();
	GLushort joint1Array;
	GLushort joint2Array;
	GLfloat weight1Array;
	GLfloat weight2Array;
	
	int jidx = 0;
	for (int i = 0; i < vcount; i++) {
		short v = stream.readInt();
		joint1Array = weightIndex[jidx];
		weight1Array = weights[jidx];
		if (v > 1) {
			joint2Array = weightIndex[jidx + 1];
			weight2Array = weights[jidx + 1];
		} else {
			joint2Array = 0;
			weight2Array = 0;
		}
		jidx += v;
		
		// FIXEDME: Figureが2つのウェイトしか持てないので、それ以上のデータが入っていても無視する
		
		fig->addJoints(&joint1Array, &weight1Array, &joint2Array, &weight2Array, 1);
	}
}
Beispiel #20
0
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;
}
Beispiel #21
0
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;
}
Beispiel #22
0
static x86::ModRM GetModRm(BinaryStream const& rBinStrm, TOffset Offset)
{
  u8 ModRmByte;
  rBinStrm.Read(Offset, ModRmByte);
  return ModRmByte;
}