-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProfile.cpp
More file actions
107 lines (77 loc) · 1.94 KB
/
Profile.cpp
File metadata and controls
107 lines (77 loc) · 1.94 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
105
106
107
//================================================//
#include "Profile.hpp"
//================================================//
Profile::Profile(void)
{
m_pData = new PROFILE_DATA();
memset(m_pData, 0, sizeof(PROFILE_DATA));
this->setDefaults();
}
//================================================//
Profile::~Profile(void)
{
delete m_pData;
m_pData = 0;
}
//================================================//
void Profile::setDefaults(void)
{
m_pData->magic = BINARY_MAGIC;
m_pData->name = "Default";
m_pData->difficulty = NORMAL;
m_pData->stage = 0;
m_pData->inventory.setDefaults();
}
//================================================//
bool Profile::create(Ogre::String name)
{
this->setDefaults();
m_pData->name = name;
if(save()){
return true;
}
return false;
}
//================================================//
bool Profile::load(Ogre::String name)
{
// Add encryption
this->setDefaults();
std::ifstream fp("Saves/" + name, std::ifstream::binary);
if(fp.is_open()){
// Compare sizes
fp.seekg(0, std::ios::end);
int size = (int)fp.tellg();
fp.seekg(0, std::ios::beg);
if(size == sizeof(PROFILE_DATA)){
fp.read(reinterpret_cast<char*>(m_pData), sizeof(PROFILE_DATA));
fp.close();
Ogre::StringUtil strUtil;
if(strUtil.match(m_pData->magic, BINARY_MAGIC, true)){
printf("Matched!!\n");
return true;
}
}
else{
printf("Invalid size! (%d)\n", size);
}
}
return false;
}
//================================================//
bool Profile::save(void)
{
// Create Saves directory if not already there
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
(void)CreateDirectory("Saves", NULL);
#endif
std::ofstream fp("Saves/" + m_pData->name + ".save", std::ofstream::binary);
if(fp.is_open()){
fp.write(reinterpret_cast<const char*>(m_pData), sizeof(PROFILE_DATA));
fp.close();
//! More error handling
return true;
}
return false;
}
//================================================//