关闭

ByteBuffer之二进制数据的存取

556人阅读 评论(0) 收藏 举报
分类:
function LocalData:readFromBuffer( buffer )
    local v = buffer:readInt()
    if v > self.version then
        self.version = v
    end
    self.localResourceVersion = v

    self.curLanguage = buffer:readInt( )
    self.userId = buffer:readInt( )
    self.password = buffer:readUTF8( )
    self.loginWay = buffer:readUTF8( )
end

function LocalData:writeToBuffer( buffer )
    buffer:writeInt( self.version )
    buffer:writeInt( self.curLanguage )
    buffer:writeInt( self.userId )
    buffer:writeUTF8( self.password )
    buffer:writeUTF8( self.loginWay)
end

function LocalData:loadLocalData()

    local buffer = dyt.ByteBuffer:new()
    dyt.Cocos2dUtils:readBufferFromFile( buffer, self:getLocalDataPath() )

    if buffer:getBufferSize() >= 8 then
        self:init(  )
        self:readFromBuffer( buffer )
    else
        self:init(  )
    end
end

function LocalData:cleanLocalData()
    local buffer = dyt.ByteBuffer:new()
    buffer:writeInt( self.version )
    buffer:writeInt( self.curLanguage )
    buffer:writeInt( -1 )
    buffer:writeUTF8( -1 )
    dyt.Cocos2dUtils:writeBufferToFile( buffer, self:getLocalDataPath() )
end

function LocalData:saveLocalData()
    local buffer = dyt.ByteBuffer:new()

    self:writeToBuffer( buffer )
    dyt.Cocos2dUtils:writeBufferToFile( buffer, self:getLocalDataPath() )
end

function LocalData:getLocalDataPath()
    local writeDir = cc.FileUtils:getInstance():getWritablePath()
    local fullpath = writeDir .. "game.bin"
    return fullpath
end

--写入propChangeList到gamePropChange.bin
function LocalData:savePropChangeList(propChangeList)
    local buffer = dyt.ByteBuffer:new()
    self:writePropChangeList(buffer, propChangeList )
    dyt.Cocos2dUtils:writeBufferToFile( buffer, self:getLocalPropChangeDataPath() ) 
end

function LocalData:clearPropChangeList()  --清空本地PropChangeList
    local buffer = dyt.ByteBuffer:new()
    buffer:writeInt(0)
    dyt.Cocos2dUtils:writeBufferToFile( buffer, self:getLocalPropChangeDataPath() ) 
end

function LocalData:writePropChangeList(buffer, propChangeList)
    local size = #propChangeList
    buffer:writeInt(size)
    for i=1, size do
        buffer:writeInt(propChangeList[i].dictId)
        buffer:writeInt(propChangeList[i].count)
    end
end

function LocalData:getLocalPropChangeDataPath()
    local writeDir = cc.FileUtils:getInstance():getWritablePath()
    local fullpath = writeDir .. "gamePropChange.bin"
    return fullpath
end

function LocalData:readPropChangeList()
    local propChangeList = {}
    local buffer = dyt.ByteBuffer:new()
    dyt.Cocos2dUtils:readBufferFromFile( buffer, self:getLocalPropChangeDataPath() )
    if buffer:getBufferSize() >= 8 then
        local size = buffer:readInt()
        for i = 1, size do
            local dictId = buffer:readInt()
            local count = buffer:readInt()
            local propChange = PropChange:create()
            propChange.dictId = dictId
            propChange.count = count
            table.insert(propChangeList, propChange)
        end
    end
    return propChangeList
end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:555730次
    • 积分:15759
    • 等级:
    • 排名:第668名
    • 原创:1060篇
    • 转载:0篇
    • 译文:0篇
    • 评论:47条
    文章分类
    最新评论