/[pcsx2_0.9.7]/trunk/pcsx2/GameDatabase.h
ViewVC logotype

Annotation of /trunk/pcsx2/GameDatabase.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 6158 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 william 62 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #pragma once
17    
18     //#include "Common.h"
19     #include "AppConfig.h"
20     #include "Utilities/HashMap.h"
21     #include "Utilities/SafeArray.h"
22    
23     #include <wx/wfstream.h>
24    
25     struct key_pair;
26     struct Game_Data;
27    
28     class StringHashNoCase
29     {
30     public:
31     StringHashNoCase() {}
32    
33     HashTools::hash_key_t operator()( const wxString& src ) const
34     {
35     return HashTools::Hash( (const char *)src.Lower().data(), src.length() * sizeof( wxChar ) );
36     }
37     };
38    
39    
40     typedef std::vector<key_pair> KeyPairArray;
41    
42     struct key_pair {
43     wxString key;
44     wxString value;
45    
46     key_pair() {}
47     key_pair(const wxString& _key, const wxString& _value)
48     : key(_key) , value(_value) {}
49    
50     void Clear() {
51     key.clear();
52     value.clear();
53     }
54    
55     // Performs case-insensitive compare against the key value.
56     bool CompareKey( const wxString& cmpto ) const {
57     return key.CmpNoCase(cmpto) == 0;
58     }
59    
60     bool IsOk() const {
61     return !key.IsEmpty();
62     }
63    
64     wxString toString() const {
65     if (key[0] == '[') {
66     pxAssertDev( key.EndsWith(L"]"), "Malformed multiline key detected: missing end bracket!" );
67    
68     // Terminating tag must be written without the "rvalue" -- in the form of:
69     // [/patches]
70     // Use Mid() to strip off the left and right side brackets.
71     wxString midLine(key.Mid(1, key.Length()-2));
72     wxString keyLvalue(midLine.BeforeFirst(L'=').Trim(true).Trim(false));
73    
74     return wxsFormat( L"%s\n%s[/%s]\n",
75     key.c_str(), value.c_str(), keyLvalue.c_str()
76     );
77     }
78     else {
79     // Note: 6 char padding on the l-value makes things look nicer.
80     return wxsFormat(L"%-6s = %s\n", key.c_str(), value.c_str() );
81     }
82    
83     }
84     };
85    
86     // --------------------------------------------------------------------------------------
87     // Game_Data
88     // --------------------------------------------------------------------------------------
89     // This container is more or less required to be a simple struct (POD classification) --
90     // no virtuals and no inheritance. This is because it is used in a std::vector, so POD
91     // makes things... smoother.
92     struct Game_Data
93     {
94     wxString id; // Serial Identification Code
95     KeyPairArray kList; // List of all (key, value) pairs for game data
96    
97     Game_Data(const wxString& _id = wxEmptyString)
98     : id(_id) {}
99    
100     // Performs a case-insensitive compare of two IDs, returns TRUE if the IDs match
101     // or FALSE if the ids differ in a case-insensitive way.
102     bool CompareId( const wxString& _id ) const {
103     return id.CmpNoCase(_id) == 0;
104     }
105    
106     void clear() {
107     id.clear();
108     kList.clear();
109     }
110    
111     bool keyExists(const wxChar* key) const;
112     void deleteKey(const wxChar* key);
113     wxString getString(const wxChar* key) const;
114     void writeString(const wxString& key, const wxString& value);
115     void writeBool(const wxString& key, bool value);
116    
117     bool IsOk() const {
118     return !id.IsEmpty();
119     }
120    
121     bool sectionExists(const wxChar* key, const wxString& value) const {
122     return keyExists(wxsFormat(L"[%s%s%s]", key, value.IsEmpty() ? L"" : L" = ", value.c_str()));
123     }
124    
125     wxString getSection(const wxChar* key, const wxString& value) const {
126     return getString(wxsFormat(L"[%s%s%s]", key, value.IsEmpty() ? L"" : L" = ", value.c_str()));
127     }
128    
129     // Gets an integer representation of the 'value' for the given key
130     int getInt(const wxChar* key) const {
131     return wxStrtoul(getString(key), NULL, 0);
132     }
133    
134     // Gets a u8 representation of the 'value' for the given key
135     u8 getU8(const wxChar* key) const {
136     return (u8)wxAtoi(getString(key));
137     }
138    
139     // Gets a bool representation of the 'value' for the given key
140     bool getBool(const wxChar* key) const {
141     return !!wxAtoi(getString(key));
142     }
143    
144     bool keyExists(const char* key) const {
145     return keyExists(fromUTF8(key));
146     }
147    
148     wxString getString(const char* key) const {
149     return getString(fromUTF8(key));
150     }
151    
152     int getInt(const char* key) const {
153     return getInt(fromUTF8(key));
154     }
155    
156     u8 getU8(const char* key) const {
157     return getU8(fromUTF8(key));
158     }
159    
160     bool getBool(const char* key) const {
161     return getBool(fromUTF8(key));
162     }
163     };
164    
165     // --------------------------------------------------------------------------------------
166     // IGameDatabase
167     // --------------------------------------------------------------------------------------
168     class IGameDatabase
169     {
170     public:
171     virtual ~IGameDatabase() throw() {}
172    
173     virtual wxString getBaseKey() const=0;
174     virtual bool findGame(Game_Data& dest, const wxString& id)=0;
175     virtual Game_Data* createNewGame( const wxString& id )=0;
176     virtual void updateGame(const Game_Data& game)=0;
177     };
178    
179     typedef pxDictionary<Game_Data*,StringHashNoCase> GameDataHash;
180    
181     // --------------------------------------------------------------------------------------
182     // BaseGameDatabaseImpl
183     // --------------------------------------------------------------------------------------
184     // [TODO] Create a version of this that uses google hashsets; should be several magnitudes
185     // faster that way.
186     class BaseGameDatabaseImpl : public IGameDatabase
187     {
188     protected:
189     GameDataHash gHash; // hash table of game serials matched to their gList indexes!
190     wxString m_baseKey;
191    
192     std::vector<Game_Data*> m_BlockTable;
193     uint m_BlockTableWritePos;
194     int m_CurBlockWritePos;
195     int m_GamesPerBlock;
196    
197     public:
198     BaseGameDatabaseImpl();
199     virtual ~BaseGameDatabaseImpl() throw();
200    
201     wxString getBaseKey() const { return m_baseKey; }
202     void setBaseKey( const wxString& key ) { m_baseKey = key; }
203    
204     bool findGame(Game_Data& dest, const wxString& id);
205     Game_Data* createNewGame( const wxString& id );
206     void updateGame(const Game_Data& game);
207     };
208    
209     extern IGameDatabase* AppHost_GetGameDatabase();

  ViewVC Help
Powered by ViewVC 1.1.22