-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGDSaveData.cpp
More file actions
104 lines (95 loc) · 2.24 KB
/
GDSaveData.cpp
File metadata and controls
104 lines (95 loc) · 2.24 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// GDSaveData.cpp
// BasketWorldCup2
//
// Created by ksoo k on 13. 1. 4..
//
//
#include "GDSaveData.h"
#include "JsonBox.h"
#include <sstream>
#include "StringCodec.h"
#include "cocos2d.h"
GDSaveData* gdSaveData = GDSaveData::sharedObject();
GDSaveData* GDSaveData::sharedObject()
{
static GDSaveData* _ins = 0;
if(_ins == 0)
{
_ins = new GDSaveData();
}
return _ins;
}
void GDSaveData::createJSON()
{
ablePath = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath();
string rawData = gdreadF(ablePath);
if(rawData == "")
jsonData.loadFromString("{}");
else
jsonData.loadFromString(rawData);
isInit = true;
}
void GDSaveData::setKeyValue(string _key, string _value)
{
CCAssert(isInit == true, "call createJSON");
pthread_mutex_lock(&t_functionMutex);
string key = stringEnc(_key);
string value = stringEnc(_value);
jsonData[key] = value;
ostringstream oss;
jsonData.writeToStream(oss);
gdwriteF(ablePath, oss.str());
pthread_mutex_unlock(&t_functionMutex);
}
void GDSaveData::setKeyValue(string _key, int _value)
{
CCAssert(isInit == true, "call createJSON");
pthread_mutex_lock(&t_functionMutex);
string key = stringEnc(_key);
ostringstream valueoss;
valueoss << _value;
string value = stringEnc(valueoss.str());
jsonData[key] = value;
ostringstream oss;
jsonData.writeToStream(oss);
gdwriteF(ablePath, oss.str());
pthread_mutex_unlock(&t_functionMutex);
}
string GDSaveData::getValue(string _key, string defaultValue)
{
CCAssert(isInit == true, "call createJSON");
pthread_mutex_lock(&t_functionMutex);
string key = stringEnc(_key);
string v = jsonData[key].getString();
string v2 = stringDecode(v);
if(v2 == "")
{
pthread_mutex_unlock(&t_functionMutex);
return defaultValue;
}
else
{
pthread_mutex_unlock(&t_functionMutex);
return v2;
}
}
int GDSaveData::getValue(string _key, int defaultValue)
{
CCAssert(isInit == true, "call createJSON");
pthread_mutex_lock(&t_functionMutex);
string key = stringEnc(_key);
string v = jsonData[key].getString();
string v2 = stringDecode(v);
int _v2 = atoi(v2.c_str());
if(v2 == "")
{
pthread_mutex_unlock(&t_functionMutex);
return defaultValue;
}
else
{
pthread_mutex_unlock(&t_functionMutex);
return _v2;
}
}