BaseData.lua
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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