]>
Commit | Line | Data |
---|---|---|
63cafd27 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: scorefil.cpp | |
3 | // Purpose: Forty Thieves patience game | |
4 | // Author: Chris Breeze | |
5 | // Modified by: | |
6 | // Created: 21/07/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1993-1998 Chris Breeze | |
9 | // Licence: wxWindows licence | |
10 | //--------------------------------------------------------------------------- | |
11 | // Last modified: 14th May 1998 - ported to wxWindows 2.0 | |
12 | ///////////////////////////////////////////////////////////////////////////// | |
13 | ||
14 | #ifdef __GNUG__ | |
15 | #pragma implementation | |
16 | #pragma interface | |
17 | #endif | |
18 | ||
19 | // For compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/wx.h" | |
28 | #endif | |
29 | ||
30 | #ifdef __WXGTK__ | |
31 | #include <unistd.h> | |
32 | #include <sys/stat.h> | |
33 | #include <fcntl.h> | |
34 | #endif | |
35 | #include "wx/textfile.h" | |
36 | #include "wx/config.h" | |
37 | #include "wx/fileconf.h" | |
38 | ||
39 | #include "scorefil.h" | |
40 | ||
41 | ||
42 | ScoreFile::ScoreFile(const char* appName) | |
43 | { | |
44 | wxString filename; | |
45 | #ifdef __WXGTK__ | |
46 | m_configFilename << "/usr/local/share/" << appName << ".scores"; | |
47 | if (access(m_configFilename, F_OK) == 0) | |
48 | { | |
49 | if (access(m_configFilename, R_OK | W_OK) != 0) | |
50 | { | |
51 | // file is not r/w - use local file instead | |
52 | m_configFilename = wxFileConfig::GetLocalFileName(appName); | |
53 | } | |
54 | } | |
55 | else | |
56 | { | |
57 | int fd = creat(m_configFilename, 0666); | |
58 | ||
59 | if (fd < 0) | |
60 | { | |
61 | // failed to create file - use local file instead | |
62 | m_configFilename = wxFileConfig::GetLocalFileName(appName); | |
63 | } | |
64 | else | |
65 | { | |
66 | // ensure created file has rw-rw-rw permissions | |
67 | close(fd); | |
68 | } | |
69 | } | |
70 | #else | |
71 | // Windows | |
72 | m_configFilename = wxFileConfig::GetGlobalFileName(appName); | |
73 | #endif | |
74 | m_config = new wxFileConfig(m_configFilename); | |
75 | } | |
76 | ||
77 | ScoreFile::~ScoreFile() | |
78 | { | |
79 | delete m_config; | |
80 | #ifdef __WXGTK__ | |
81 | // ensure score file has rw-rw-rw permissions | |
82 | // (wxFileConfig sets them to rw-------) | |
83 | chmod(m_configFilename, 0666); | |
84 | #endif | |
85 | } | |
86 | ||
87 | ||
88 | void ScoreFile::GetPlayerList(wxString** list, int& length) | |
89 | { | |
90 | m_config->SetPath("/Players"); | |
91 | length = m_config->GetNumberOfGroups(); | |
92 | ||
93 | if (length <= 0) | |
94 | { | |
95 | *list = 0; | |
96 | return; | |
97 | } | |
98 | ||
99 | *list = new wxString[length]; | |
100 | ||
101 | wxString player; | |
102 | long index, i = 0; | |
103 | if (m_config->GetFirstGroup(player, index)) | |
104 | { | |
105 | (*list)[i++] = player; | |
106 | while (m_config->GetNextGroup(player, index)) | |
107 | { | |
108 | (*list)[i++] = player; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | ||
114 | // Calculate an encrypted check number to prevent tampering with | |
115 | // score file | |
116 | long ScoreFile::CalcCheck(const char* name, int p1, int p2, int p3) | |
117 | { | |
118 | long check = 0; | |
119 | while (*name) | |
120 | { | |
121 | check = (check << 1) ^ (long)*name++; | |
122 | check = ((check >> 23) ^ check) & 0xFFFFFF; | |
123 | } | |
124 | check = (check << 1) ^ (long)p1; | |
125 | check = ((check >> 23) ^ check) & 0xFFFFFF; | |
126 | check = (check << 1) ^ (long)p2; | |
127 | check = ((check >> 23) ^ check) & 0xFFFFFF; | |
128 | check = (check << 1) ^ (long)p3; | |
129 | check = ((check >> 23) ^ check) & 0xFFFFFF; | |
130 | return check; | |
131 | } | |
132 | ||
133 | wxString ScoreFile::GetPreviousPlayer() const | |
134 | { | |
135 | wxString result; | |
136 | m_config->SetPath("/General"); | |
137 | m_config->Read(&result, "LastPlayer"); | |
138 | return result; | |
139 | } | |
140 | ||
141 | void ScoreFile::ReadPlayersScore( | |
142 | const char* player, | |
143 | int& wins, | |
144 | int& games, | |
145 | int& score) | |
146 | { | |
147 | long check; | |
148 | long myWins, myGames, myScore; | |
149 | ||
150 | games = wins = score = 0; | |
151 | ||
152 | m_config->SetPath("/Players"); | |
153 | m_config->SetPath(player); | |
154 | if (m_config->Read(&myScore, "Score") && | |
155 | m_config->Read(&myGames, "Games") && | |
156 | m_config->Read(&myWins, "Wins") && | |
157 | m_config->Read(&check, "Check")) | |
158 | { | |
159 | if (check != CalcCheck(player, myGames, myWins, myScore)) | |
160 | { | |
161 | wxMessageBox("Score file corrupted", "Warning", | |
162 | wxOK | wxICON_EXCLAMATION); | |
163 | } | |
164 | else | |
165 | { | |
166 | games = myGames; | |
167 | wins = myWins; | |
168 | score = myScore; | |
169 | } | |
170 | } | |
171 | WritePlayersScore(player, wins, games, score); | |
172 | } | |
173 | ||
174 | ||
175 | void ScoreFile::WritePlayersScore(const char* player, int wins, int games, int score) | |
176 | { | |
177 | if (player) | |
178 | { | |
179 | m_config->SetPath("/General"); | |
180 | m_config->Write("LastPlayer", player); | |
181 | ||
182 | m_config->SetPath("/Players"); | |
183 | m_config->SetPath(player); | |
184 | m_config->Write("Score", score); | |
185 | m_config->Write("Games", games); | |
186 | m_config->Write("Wins", wins); | |
187 | m_config->Write("Check", CalcCheck(player, games, wins, score)); | |
188 | } | |
189 | } |