BaseData.lua 1.53 KB
local BaseData = class("BaseData")

local password = "zhjianctkj0571";

function BaseData:ctor( fileName )
    self.fileName = fileName
	self.data = {}
end

function BaseData:saveToFile(  )
	local str = json.encode(self.data)
	local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform ~= cc.PLATFORM_OS_WINDOWS then
--        str = MCCrypto:encryptXXTEA(str, string.len(str), password, string.len(password));
    end
    local fullFilename = cc.FileUtils:getInstance():getWritablePath()..self.fileName
	local file = io.open(fullFilename, "w")
    file:write(str)
    file:close()
end

function BaseData:readFromFile(  )	
	local result = false
    local fullFilename = cc.FileUtils:getInstance():getWritablePath()..self.fileName
    local file = io.open(fullFilename, "r")
    
    if file then
        local str= file:read("*all")
--        print(str)
        file:close()
        if pcall(function()
            local targetPlatform = cc.Application:getInstance():getTargetPlatform()
            if targetPlatform ~= cc.PLATFORM_OS_WINDOWS then
--                str = MCCrypto:decryptXXTEA(str, string.len(str), password, string.len(password))
            end
            self.data = json.decode(str)
            result = true
        end) then
            --todo
        else
            self.data = {}
            print("invalid json data: "..self.fileName)
        end
    else

        self.data = {}
    end

    return result
end

function BaseData:resetData(  )
    self.data = {}
    self:saveToFile()
end

return BaseData