summaryrefslogtreecommitdiffstats
path: root/game/code/data/gamedatamanager.h
blob: f82524246052d637b5e6f26cb84594c96881d029 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===========================================================================
// Copyright (C) 2000 Radical Entertainment Ltd.  All rights reserved.
//
// Component:   GameDataManager
//
// Description: Interface for the GameDataManager class.
//
// Authors:     Tony Chu
//
// Revisions		Date			Author	    Revision
//                  2002/09/16      TChu        Created for SRR2
//
//===========================================================================

#ifndef GAMEDATAMANAGER_H
#define GAMEDATAMANAGER_H

#ifndef NULL
    #define NULL 0
#endif

//===========================================================================
// Nested Includes
//===========================================================================

#include <data/gamedata.h>
#include <radfile.hpp>

//===========================================================================
// Forward References
//===========================================================================

const unsigned int MAX_NUM_GAME_DATA = 16; // maximum number of
                                           // client-registered game data

const unsigned int KB = 1024;
const unsigned int MB = 1024 * KB;

struct GameDataLoadCallback
{
    virtual void OnLoadGameComplete( radFileError errorCode ) = 0;
};

struct GameDataSaveCallback
{
    virtual void OnSaveGameComplete( radFileError errorCode ) = 0;
};

struct GameDataDeleteCallback
{
    virtual void OnDeleteGameComplete( radFileError errorCode ) = 0;
};

struct IRadFile;
struct radMemcardInfo;
struct radDate;

class SaveGameInfo;

//===========================================================================
// Interface Definitions
//===========================================================================

class GameDataManager : public IRadFileCompletionCallback,
                        public IRadDriveCompletionCallback,
                        public IRadDriveErrorCallback
{
public:
    // Static Methods for accessing this singleton.
    static GameDataManager* CreateInstance();
    static void DestroyInstance();
    static GameDataManager* GetInstance();

	GameDataManager();
    virtual ~GameDataManager();

    // Initialization
    //
    void Init();

    // Update
    //
    void Update( unsigned int elapsedTime );

    // Registration of client game data
    //
    void RegisterGameData( GameDataHandler* gdHandler,
                           unsigned int numBytes,
                           const char* name = NULL );

    unsigned int GetGameDataSize() const { return m_gameDataSize; }

    // Load/Save Game (Asynchronous)
    void LoadGame( unsigned int slot, GameDataLoadCallback* callback = NULL, const char *filename = NULL );
    void SaveGame( unsigned int slot, GameDataSaveCallback* callback = NULL );

	// Delete Game
	radFileError DeleteGame( const char *fileName, bool async = false, GameDataDeleteCallback* callback = NULL );

    // Reset All Game Data (for New Games)
    //
    void ResetGame();

    void SetMinimumLoadSaveTime( unsigned int minimumTime );
    void RestoreDefaultMinimumLoadSaveTime();

    bool IsGameLoaded() const { return m_isGameLoaded; }
    void SetGameLoaded() { m_isGameLoaded = true; }

    bool GetSaveGameInfo( IRadDrive* pDrive,
                          unsigned int slot,
                          SaveGameInfo* saveGameInfo,
                          bool  *file_corrupt_flag = NULL);

    bool DoesSaveGameExist( IRadDrive* pDrive,
                            bool check_valid = true,
                            bool forAllSlots = false );

    bool FindMostRecentSaveGame( IRadDrive* pDrive,
                                 unsigned int& slot,
                                 radDate& timeStamp );

    // saved game filename formatting
    //
    static void FormatSavedGameFilename( char* filename,
                                         unsigned int filenameLength,
                                         unsigned int slot );

    // Implements IRadFileCompletionCallback
    //
    virtual void AddRef() {;}
    virtual void Release() {;}
    virtual void OnFileOperationsComplete( void* pUserData );

    virtual void OnDriveOperationsComplete( void* pUserData );

    // Implements IRadDriveErrorCallback
    //
    virtual bool OnDriveError( radFileError error, const char* pDriveName, void* pUserData );

    bool IsUsingDrive() const;

protected:

    enum eFileOperation
    {
        FILE_OP_NONE,

        FILE_OP_OPEN_FOR_READING,
        FILE_OP_OPEN_FOR_WRITING,
        FILE_OP_READ,
        FILE_OP_WRITE,
        FILE_OP_COMMIT,
        FILE_OP_FILE_CHECK,

        FILE_OP_LOAD_COMPLETED,
        FILE_OP_SAVE_COMPLETED,

		FILE_OP_DELETE,
        FILE_OP_DELETE_COMPLETED,

        NUM_FILE_OPERATIONS
    };

private:
    //---------------------------------------------------------------------
    // Private Functions
    //---------------------------------------------------------------------

    // No copying or assignment. Declare but don't define.
    //
    GameDataManager( const GameDataManager& );
    GameDataManager& operator= ( const GameDataManager& );

    bool LoadAllData();
    bool SaveAllData();

    static void PrintRawData( GameDataByte* dataBuffer,
                              unsigned int numBytes );

    //---------------------------------------------------------------------
    // Private Data
    //---------------------------------------------------------------------

    // Pointer to the one and only instance of this singleton.
    static GameDataManager* spInstance;

    GameData* m_registeredGameData[ MAX_NUM_GAME_DATA ];
    unsigned int m_numRegisteredGameData;

    GameDataByte* m_gameDataBuffer;
    unsigned int m_gameDataSize;

    GameDataLoadCallback* m_gameDataLoadCallback;
    GameDataSaveCallback* m_gameDataSaveCallback;
    GameDataDeleteCallback* m_gameDataDeleteCallback;

    unsigned int m_minimumLoadSaveTime;
    unsigned int m_elapsedOperationTime;

    bool m_isGameLoaded;

    SaveGameInfo* m_saveGameInfoHandler;

    // RadFile Stuff
    //
    IRadFile* m_radFile;
    eFileOperation m_currentFileOperation;

	radFileError m_lastError;

};

// A little syntactic sugar for getting at this singleton.
inline GameDataManager* GetGameDataManager() { return( GameDataManager::GetInstance() ); }

#endif // GAMEDATAMANAGER_H