lua class.cpp
下定义
int WriteDataToFile(lua_State* pArgs)
{
string sPath = Lua_Tool::GetString(pArgs, 1);
void* pUserData = Lua_Tool::GetUserData(pArgs, 2);
int nSize = Lua_Tool::GetInteger(pArgs, 3);
Data data;
data.copy((unsigned char*)pUserData, nSize);
FileUtils::getInstance()->removeFile(sPath);
FileUtils::getInstance()->writeDataToFile(data, sPath);
return 0;
}
int ReadDataFromFile(lua_State* pArgs)
{
string sPath = Lua_Tool::GetString(pArgs, 1);
Data data = FileUtils::getInstance()->getDataFromFile(sPath);
int nSize = data.getSize();
unsigned char* pUserData[10240] = { 0 };
memcpy(pUserData, data.getBytes(), nSize);
Lua_Tool::PushUserData(pArgs, pUserData);
Lua_Tool::PushInteger(pArgs, nSize);
return 2;
}
用来存储和读取录像的二进制存储数据
战绩读取成为Table后转化为json文件进行文档存储
local NewtbRecondData={};
if fileUtils:isFileExist(recondDirPath..tostring(uUserId)..".json") == false then
print(" buffer:ReadInt();error", recondDirPath..tostring(uUserId)..".json")
NewtbRecondData = {};
table.insert(NewtbRecondData,tbRecondData)
local json=json.encode(NewtbRecondData)
SaveRecond(json,size)
else
local file = fileUtils:getStringFromFile(recondDirPath..tostring(uUserId)..".json");
NewtbRecondData = json.decode(file)
local i=#NewtbRecondData
--判断是否为同一大局
if(tbRecondData.bigTime==NewtbRecondData[i].bigTime) then
PrintTable(NewtbRecondData[i].tbUserPoint["0"])
table.insert(NewtbRecondData[i].tbReplayTime,table.nums(NewtbRecondData[i].tbReplayTime),tbRecondData.tbReplayTime[0])
NewtbRecondData[i].nGroupNumber_Now=tbRecondData.nGroupNumber_Now
for j = 0, tbRecondData.nPersonNumber-1 do
table.insert(NewtbRecondData[i].tbUserPoint[tostring(j)].tbPoint,table.nums(NewtbRecondData[i].tbUserPoint[tostring(j)].tbPoint),tbRecondData.tbUserPoint[j].tbPoint[0])
NewtbRecondData[i].tbUserPoint[tostring(j)].sumPoint=tbRecondData.tbUserPoint[j].sumPoint
local json=json.encode(NewtbRecondData)
print("RecondData=",json);
SaveRecond(json,size)
end
--如果战绩过多则删除
if(#NewtbRecondData>50) then
print(#NewtbRecondData)
table.remove(NewtbRecondData,"1")
end
else
table.insert(NewtbRecondData,tbRecondData)
local json=json.encode(NewtbRecondData)
print("RecondData=",json);
SaveRecond(json,size)
end
注意战绩经过json.decode转化为Table后数组下标需字符串化(tostring)
function SaveRecond(buf,size)
local fileUtils = cc.FileUtils:getInstance();
local strWritablePath = fileUtils:getWritablePath();
local recondDirPath = strWritablePath.."recond/";
if fileUtils:isDirectoryExist(strWritablePath.."recond/") == false then
--不存在文件夹
if fileUtils:createDirectory(recondDirPath) == false then
--创建文件夹失败
return false;
end
end
local uUserId1 = tostring(g_tableUserData.nUserId1)
local uUserId2 = tostring(g_tableUserData.nUserId2)
local uUserId = uUserId1 .. uUserId2
local file = io.open(recondDirPath..uUserId..".json" ,"w+");
if file then
if file:write(tostring(buf).."\n") == nil then
return false;
end
io.close(file);
--WriteDataToFile(recondDirPath..tostring(nUserId) ,buf,size);
--DeleteRecond(30);
return true;
else
return false;
end
end