예제 #1
0
luabridge::LuaRef CLuaBinary::getStruct(luabridge::LuaRef objAttr)
{
    assert(objAttr.isTable());

    DataType emType;
    int iSize = Q_INIT_NUMBER;
    std::string strName;
    luabridge::LuaRef objVal = luabridge::newTable(m_pLua);

    for (int i = 1; i <= objAttr.length(); i++)
    {
        luabridge::LuaRef objTmp = objAttr[i];
        if (!objTmp.isTable()
            || STATTRTAB_MINLENS > objTmp.length())
        {
            break;
        }

        luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize);
        switch(emType)
        {
        case DTYPE_SINT8:
            {
                objVal[strName] = getSint8();
            }
            break;
        case DTYPE_UINT8:
            {
                objVal[strName] = getUint8();
            }
            break;
        case DTYPE_BOOL:
            {
                objVal[strName] = getBool();
            }
            break;
        case DTYPE_SINT16:
            {
                objVal[strName] = getSint16();
            }
            break;
        case DTYPE_UINT16:
            {
                objVal[strName] = getUint16();
            }
            break;
        case DTYPE_SINT32:
            {
                objVal[strName] = getSint32();
            }
            break;
        case DTYPE_UINT32:
            {
                objVal[strName] = getUint32();
            }
            break;
        case DTYPE_SINT64:
            {
                objVal[strName] = getSint64();
            }
            break;
        case DTYPE_UINT64:
            {
                objVal[strName] = getUint64();
            }
            break;
        case DTYPE_FLOAT:
            {
                objVal[strName] = getFloat();
            }
            break;
        case DTYPE_DOUBLE:
            {
                objVal[strName] = getDouble();
            }
            break;
        case DTYPE_STRING:
            {
                std::string strVal = getString();
                objVal[strName] = strVal;
                skipRead((unsigned int)(iSize - (strVal.size() + 1)));
            }
            break;
        case DTYPE_BYTE:
            {
                objVal[strName] = getByte(iSize);
            }
            break;
        case DTYPE_STRUCT:
            {
                objVal[strName] = getStruct(objChildStAttr);
            }
            break;
        case DTYPE_SKIP:
            {
                if (iSize > Q_INIT_NUMBER)
                {
                    skipRead(iSize);
                }
            }
            break;

        default:
            break;
        }
    }

    return objVal;
}
예제 #2
0
bool CLuaBinary::setStruct(luabridge::LuaRef objVal, luabridge::LuaRef objAttr)
{
    assert(objVal.isTable());
    assert(objAttr.isTable());

    bool bOk = false;
    DataType emType;
    int iSize = Q_INIT_NUMBER;
    std::string strName;

    for (int i = 1; i <= objAttr.length(); i++)
    {
        luabridge::LuaRef objTmp = objAttr[i];
        if (!objTmp.isTable()
            || STATTRTAB_MINLENS > objTmp.length())
        {
            return false;
        }

        luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize);
        luabridge::LuaRef objFieldVal = objVal[strName];

        switch(emType)
        {
        case DTYPE_SINT8:
            {
                bOk = setSint8(getLuaNumber<short>(objFieldVal));
            }
            break;
        case DTYPE_UINT8:
            {
                bOk = setUint8(getLuaNumber<unsigned short>(objFieldVal));
            }
            break;
        case DTYPE_BOOL:
            {
                bOk = setBool(((Q_INIT_NUMBER != getLuaNumber<unsigned short>(objFieldVal)) ? true : false));
            }
            break;
        case DTYPE_SINT16:
            {
                bOk = setSint16(getLuaNumber<short>(objFieldVal));
            }
            break;
        case DTYPE_UINT16:
            {
                bOk = setUint16(getLuaNumber<unsigned short>(objFieldVal));
            }
            break;
        case DTYPE_SINT32:
            {
                bOk = setSint32(getLuaNumber<int>(objFieldVal));
            }
            break;
        case DTYPE_UINT32:
            {
                bOk = setUint32(getLuaNumber<unsigned int>(objFieldVal));
            }
            break;
        case DTYPE_SINT64:
            {
                bOk = setSint64(Q_ToString(getLuaNumber<int64_t>(objFieldVal)).c_str());
            }
            break;
        case DTYPE_UINT64:
            {
                bOk = setUint64(Q_ToString(getLuaNumber<uint64_t>(objFieldVal)).c_str());
            }
            break;
        case DTYPE_FLOAT:
            {
                bOk = setFloat(getLuaNumber<float>(objFieldVal));
            }
            break;
        case DTYPE_DOUBLE:
            {
                bOk = setDouble(getLuaNumber<double>(objFieldVal));
            }
            break;
        case DTYPE_STRING:
            {
                if (!objFieldVal.isString())
                {
                    bOk = false;
                    break;
                }

                const char *pszVal = objFieldVal.cast<const char*>();
                if (objFieldVal.length() >= iSize)
                {
                    bOk = false;
                    Q_Printf("%s", "data len too large.");
                    break;
                }
               
                bOk = setString(pszVal);
                if (bOk)
                {
                    bOk = skipWrite(iSize - (objFieldVal.length() + 1));
                }
            }
            break;
        case DTYPE_BYTE:
            {
                if (!objFieldVal.isString())
                {
                    bOk = false;
                    break;
                }

                const char *pszVal = objFieldVal.cast<const char*>();
                if (objFieldVal.length() > iSize)
                {
                    bOk = false;
                    Q_Printf("%s", "data len too large.");
                    break;
                }

                bOk = setByte(pszVal, objFieldVal.length());
                if (bOk)
                {
                    bOk = skipWrite(iSize - objFieldVal.length());
                }
            }
            break;
        case DTYPE_STRUCT:
            {
                if (!objFieldVal.isTable()
                    || !objChildStAttr.isTable())
                {
                    bOk = false;
                    break;
                }

                bOk = setStruct(objFieldVal, objChildStAttr);
            }
            break;
        case DTYPE_SKIP:
            {
                if (iSize > Q_INIT_NUMBER)
                {
                    bOk = skipWrite(iSize);
                }
            }
            break;

        default:
            bOk = false;
            break;
        }

        if (!bOk)
        {
            return bOk;
        }
    }

    return true;
}