/*---------------------------------------------------------------------------* Project: RevolutionDWC Demos File: ./userdata/src/main.c Copyright 2005-2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ /** * * * Brief comment: User data handling demo * * This is a demo related to handling user data. * The user data and the friend data is stored in the NAND flash memory along with the game save data. * This demo will present simple, sample code that writes created user data to NAND flash memory and deletes stored user data. * The code that actually writes to NAND flash memory is in /demos/common/src/main.c. Refer to that file, as well. * * * */ #include "../../common/include/common.h" /// Structure that contains the player information static struct tagPlayerInfo { /// User data DWCUserData userdata; /// Friend data DWCFriendData friendlist[FRIEND_LIST_LEN]; } s_playerinfo ATTRIBUTE_ALIGN(32); // Function Prototype BOOL Init ( void ); void SaveUserData ( void ); void DeleteUserData ( void ); DWCUserData* GetUserData ( void ); void SaveFriendList ( void ); void DeleteFriendList ( void ); DWCFriendData* GetFriendList ( void ); int GetAvailableFriendListIndex ( void ); void DispFriendList ( void ); //============================================================================= /*! * @brief Main */ //============================================================================= void DWCDemoMain() { // Initialization // // Load user data from NAND flash memory. Create new (data) if none exists. // Init(); // [UserData] Save to NAND flash memory // SaveUserData(); DWCDemoUpdate(); // [UserData] Remove from NAND flash memory // DeleteUserData(); DWCDemoUpdate(); // [FriendData] Save to NAND flash memory // SaveFriendList(); DWCDemoUpdate(); // [FriendData] Remove from NAND flash memory // DeleteFriendList(); DWCDemoUpdate(); } /** * Start processing * * If there is user data in NAND flash memory, it is used; otherwise, new user data is created. * * * Return value: TRUE: There is user data in NAND flash memory. * Return value: FALSE: There is no user data in NAND flash memory. */ BOOL Init( void ) { // Load from NAND // DWCDemoPrintf("Loading from NAND...\n"); DWCDemoLoadNAND( "playerinfo", 0, (u8*)&s_playerinfo, sizeof( s_playerinfo ) ); // Check if user data is valid // if ( DWC_CheckUserData( &s_playerinfo.userdata ) ) { // It was valid, so output the user data and return TRUE DWC_ReportUserData( &s_playerinfo.userdata ); return TRUE; } // If valid user data had not been saved // // Create user data. DWCDemoPrintf("Creating new UserData\n"); DWC_CreateUserData( &s_playerinfo.userdata ); // Initialize the friend roster memset( &s_playerinfo.friendlist, 0, sizeof( s_playerinfo.friendlist ) ); DWC_ReportUserData( &s_playerinfo.userdata ); DWCDemoPrintf("Initialized\n"); return FALSE; } //------------------------------------------------------------------- // User data-related //------------------------------------------------------------------- /** * Save user data to NAND flash memory. */ void SaveUserData( void ) { // Clear the change flag DWC_ClearDirtyFlag( &s_playerinfo.userdata ); // Save to NAND DWCDemoSaveNAND( "playerinfo", 0, (u8*)&s_playerinfo.userdata, sizeof( DWCUserData ) ); } /** * Delete user data from NAND flash memory. */ void DeleteUserData( void ) { // Clear the user data variables memset( &s_playerinfo.userdata, 0, sizeof( DWCUserData ) ); // Write the cleared data to NAND flash memory DWCDemoSaveNAND( "playerinfo", 0, (u8*)&s_playerinfo.userdata, sizeof( DWCUserData ) ); } /** * Get user data. * * Return value: User data pointer */ DWCUserData* GetUserData( void ) { return &s_playerinfo.userdata; } //------------------------------------------------------------------- // Friend data-related //------------------------------------------------------------------- /** * Saves the friend roster to NAND flash memory. */ void SaveFriendList( void ) { // Writes the friend roster to NAND flash memory. // // Because the user data is written in the beginning of the file, it will be recorded after that. // Here, the seek position will be calculated from the pointer of the structure member with the impact of compiler padding considered. // // DWCDemoSaveNAND( "playerinfo", (s32)((u32)s_playerinfo.friendlist - (u32)&s_playerinfo), (u8*)&s_playerinfo.friendlist, (u32)sizeof( s_playerinfo.friendlist ) ); } /** * Delete the friend roster from NAND flash memory. */ void DeleteFriendList( void ) { // Clear the friend roster variables memset( &s_playerinfo.friendlist, 0, sizeof( s_playerinfo.friendlist ) ); // Writes the cleared friend roster to NAND flash memory // // Because the user data is written in the beginning of the file, it will be recorded after that. // Here, the seek position will be calculated from the pointer of the structure member with the impact of compiler padding considered. // // DWCDemoSaveNAND( "playerinfo", (s32)((u32)s_playerinfo.friendlist - (u32)&s_playerinfo), (u8*)&s_playerinfo.friendlist, (u32)sizeof( s_playerinfo.friendlist ) ); } /** * Gets the friend roster. * * Return value: Returns the friend roster pointer. */ DWCFriendData* GetFriendList( void ) { return s_playerinfo.friendlist; } /** * Gets an empty index on the friend roster. * * Return value: Available index in the friend roster. * Returns -1 if there is no available index. */ int GetAvailableFriendListIndex( void ) { int i; for ( i = 0; i < FRIEND_LIST_LEN; i++ ) { if ( !DWC_IsValidFriendData( &s_playerinfo.friendlist[i] ) ) return i; } return -1; } /** * Display the friend roster. */ void DispFriendList( void ) { int i; for ( i = 0; i < FRIEND_LIST_LEN; i++ ) { if ( !DWC_IsValidFriendData( &s_playerinfo.friendlist[i] ) ) continue; DWC_ReportFriendData( &s_playerinfo.userdata, &s_playerinfo.friendlist[i] ); } DWCDemoPrintf( "\n"); }