Exemple #1
0
MOUL::Creatable* MOUL::Factory::ReadCreatable(DS::Stream* stream)
{
    uint16_t type = stream->read<uint16_t>();
    Creatable* obj = Create(type);
    if (obj) {
        try {
            obj->read(stream);
        } catch (...) {
            obj->unref();
            throw;
        }
    }
    return obj;
}
void MOUL::CreatableList::read(DS::Stream* stream)
{
    clear();

    m_flags = stream->read<uint8_t>();

    uint32_t bufSz = stream->read<uint32_t>();
    uint8_t* buf = new uint8_t[bufSz];

    if (m_flags & e_Compressed) {
        uint32_t zBufSz = stream->read<uint32_t>();
        uint8_t* zBuf = new uint8_t[zBufSz];
        stream->readBytes(zBuf, zBufSz);
        uLongf zLen;
        int result = uncompress(buf, &zLen, zBuf, zBufSz);
        delete[] zBuf;
        if (result != Z_OK) {
            delete[] buf;
            DS_PASSERT(0);
        }
        DS_PASSERT(zLen == bufSz);
        m_flags &= ~e_Compressed;
    } else {
        stream->readBytes(buf, bufSz);
    }

    DS::BufferStream ram;
    ram.steal(buf, bufSz);
    uint16_t numItems = ram.read<uint16_t>();
    for (uint16_t i = 0; i < numItems; i++) {
        uint16_t id = ram.read<uint16_t>();
        uint16_t type = ram.read<uint16_t>();
        Creatable* cre = Factory::Create(type);
        DS_PASSERT(cre);
        cre->read(&ram);
        m_items[id] = cre;
    }
}