1 /*---------------------------------------------------------------------------*
2 Project: RevolutionDWC Demos
3 File: ./userdata/src/main.c
4
5 Copyright 2005-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 *---------------------------------------------------------------------------*/
14 /**
15 *
16 *
17 * Brief comment: User data handling demo
18 *
19 * This is a demo related to handling user data.
20 * The user data and the friend data is stored in the NAND flash memory along with the game save data.
21 * This demo will present simple, sample code that writes created user data to NAND flash memory and deletes stored user data.
22 * The code that actually writes to NAND flash memory is in /demos/common/src/main.c. Refer to that file, as well.
23 *
24 *
25 *
26 */
27 #include "../../common/include/common.h"
28
29
30 /// Structure that contains the player information
31 static struct tagPlayerInfo
32 {
33
34 /// User data
35 DWCUserData userdata;
36
37 /// Friend data
38 DWCFriendData friendlist[FRIEND_LIST_LEN];
39
40 }
41 s_playerinfo ATTRIBUTE_ALIGN(32);
42
43
44 // Function Prototype
45 BOOL Init ( void );
46 void SaveUserData ( void );
47 void DeleteUserData ( void );
48 DWCUserData* GetUserData ( void );
49 void SaveFriendList ( void );
50 void DeleteFriendList ( void );
51 DWCFriendData* GetFriendList ( void );
52 int GetAvailableFriendListIndex ( void );
53 void DispFriendList ( void );
54
55 //=============================================================================
56 /*!
57 * @brief Main
58 */
59 //=============================================================================
DWCDemoMain()60 void DWCDemoMain()
61 {
62
63 // Initialization
64 //
65 // Load user data from NAND flash memory. Create new (data) if none exists.
66 //
67 Init();
68
69 // [UserData] Save to NAND flash memory
70 //
71 SaveUserData();
72 DWCDemoUpdate();
73
74 // [UserData] Remove from NAND flash memory
75 //
76 DeleteUserData();
77 DWCDemoUpdate();
78
79 // [FriendData] Save to NAND flash memory
80 //
81 SaveFriendList();
82 DWCDemoUpdate();
83
84 // [FriendData] Remove from NAND flash memory
85 //
86 DeleteFriendList();
87 DWCDemoUpdate();
88
89 }
90
91 /**
92 * Start processing
93 *
94 * If there is user data in NAND flash memory, it is used; otherwise, new user data is created.
95 *
96 *
97 * Return value: TRUE: There is user data in NAND flash memory.
98 * Return value: FALSE: There is no user data in NAND flash memory.
99 */
Init(void)100 BOOL Init( void )
101 {
102
103 // Load from NAND
104 //
105 DWCDemoPrintf("Loading from NAND...\n");
106
107 DWCDemoLoadNAND( "playerinfo",
108 0,
109 (u8*)&s_playerinfo,
110 sizeof( s_playerinfo ) );
111
112 // Check if user data is valid
113 //
114 if ( DWC_CheckUserData( &s_playerinfo.userdata ) )
115 {
116
117 // It was valid, so output the user data and return TRUE
118 DWC_ReportUserData( &s_playerinfo.userdata );
119 return TRUE;
120
121 }
122
123 // If valid user data had not been saved
124 //
125
126 // Create user data.
127 DWCDemoPrintf("Creating new UserData\n");
128
129 DWC_CreateUserData( &s_playerinfo.userdata );
130
131 // Initialize the friend roster
132 memset( &s_playerinfo.friendlist, 0,
133 sizeof( s_playerinfo.friendlist ) );
134
135 DWC_ReportUserData( &s_playerinfo.userdata );
136
137 DWCDemoPrintf("Initialized\n");
138
139 return FALSE;
140 }
141
142
143 //-------------------------------------------------------------------
144 // User data-related
145 //-------------------------------------------------------------------
146
147 /**
148 * Save user data to NAND flash memory.
149 */
SaveUserData(void)150 void SaveUserData( void )
151 {
152
153 // Clear the change flag
154 DWC_ClearDirtyFlag( &s_playerinfo.userdata );
155
156 // Save to NAND
157 DWCDemoSaveNAND( "playerinfo",
158 0,
159 (u8*)&s_playerinfo.userdata,
160 sizeof( DWCUserData ) );
161
162 }
163
164 /**
165 * Delete user data from NAND flash memory.
166 */
DeleteUserData(void)167 void DeleteUserData( void )
168 {
169
170 // Clear the user data variables
171 memset( &s_playerinfo.userdata, 0, sizeof( DWCUserData ) );
172
173 // Write the cleared data to NAND flash memory
174 DWCDemoSaveNAND( "playerinfo",
175 0,
176 (u8*)&s_playerinfo.userdata,
177 sizeof( DWCUserData ) );
178
179 }
180
181 /**
182 * Get user data.
183 *
184 * Return value: User data pointer
185 */
GetUserData(void)186 DWCUserData* GetUserData( void )
187 {
188 return &s_playerinfo.userdata;
189 }
190
191
192 //-------------------------------------------------------------------
193 // Friend data-related
194 //-------------------------------------------------------------------
195
196 /**
197 * Saves the friend roster to NAND flash memory.
198 */
SaveFriendList(void)199 void SaveFriendList( void )
200 {
201 // Writes the friend roster to NAND flash memory.
202 //
203 // Because the user data is written in the beginning of the file, it will be recorded after that.
204 // Here, the seek position will be calculated from the pointer of the structure member with the impact of compiler padding considered.
205 //
206 //
207 DWCDemoSaveNAND( "playerinfo",
208 (s32)((u32)s_playerinfo.friendlist - (u32)&s_playerinfo),
209 (u8*)&s_playerinfo.friendlist,
210 (u32)sizeof( s_playerinfo.friendlist ) );
211
212 }
213
214 /**
215 * Delete the friend roster from NAND flash memory.
216 */
DeleteFriendList(void)217 void DeleteFriendList( void )
218 {
219
220 // Clear the friend roster variables
221 memset( &s_playerinfo.friendlist, 0,
222 sizeof( s_playerinfo.friendlist ) );
223
224 // Writes the cleared friend roster to NAND flash memory
225 //
226 // Because the user data is written in the beginning of the file, it will be recorded after that.
227 // Here, the seek position will be calculated from the pointer of the structure member with the impact of compiler padding considered.
228 //
229 //
230 DWCDemoSaveNAND( "playerinfo",
231 (s32)((u32)s_playerinfo.friendlist - (u32)&s_playerinfo),
232 (u8*)&s_playerinfo.friendlist,
233 (u32)sizeof( s_playerinfo.friendlist ) );
234
235 }
236
237 /**
238 * Gets the friend roster.
239 *
240 * Return value: Returns the friend roster pointer.
241 */
GetFriendList(void)242 DWCFriendData* GetFriendList( void )
243 {
244 return s_playerinfo.friendlist;
245 }
246
247 /**
248 * Gets an empty index on the friend roster.
249 *
250 * Return value: Available index in the friend roster.
251 * Returns -1 if there is no available index.
252 */
GetAvailableFriendListIndex(void)253 int GetAvailableFriendListIndex( void )
254 {
255 int i;
256
257 for ( i = 0; i < FRIEND_LIST_LEN; i++ )
258 {
259
260 if ( !DWC_IsValidFriendData( &s_playerinfo.friendlist[i] ) )
261 return i;
262
263 }
264
265 return -1;
266 }
267
268 /**
269 * Display the friend roster.
270 */
DispFriendList(void)271 void DispFriendList( void )
272 {
273 int i;
274
275 for ( i = 0; i < FRIEND_LIST_LEN; i++ )
276 {
277
278 if ( !DWC_IsValidFriendData( &s_playerinfo.friendlist[i] ) )
279 continue;
280
281 DWC_ReportFriendData( &s_playerinfo.userdata,
282 &s_playerinfo.friendlist[i] );
283 }
284
285 DWCDemoPrintf( "\n");
286
287 }
288