2010-09-25 10:51:44 +02:00
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "CGameStatistics.h"
|
|
|
|
#include "FileOperations/fileops.h"
|
2011-01-06 19:59:45 +01:00
|
|
|
#include "svnrev.h"
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
#define VALID_CONFIG_REV 1031
|
2010-09-25 10:51:44 +02:00
|
|
|
|
|
|
|
CGameStatistics GameStatistics;
|
|
|
|
|
|
|
|
|
|
|
|
CGameStatistics::CGameStatistics()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CGameStatistics::~CGameStatistics()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-13 20:05:31 +01:00
|
|
|
GameStatus * CGameStatistics::GetGameStatus(const char * id) const
|
2010-09-25 10:51:44 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for(u32 i = 0; i < GameList.size(); ++i)
|
|
|
|
{
|
|
|
|
if(strncasecmp(id, GameList[i].id, 6) == 0)
|
|
|
|
{
|
|
|
|
return (GameStatus *) &GameList[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::AddGame(const GameStatus & NewGame)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
for(u32 i = 0; i < GameList.size(); ++i)
|
|
|
|
{
|
|
|
|
if(strncasecmp(NewGame.id, GameList[i].id, 6) == 0)
|
|
|
|
{
|
|
|
|
memcpy(&GameList[i], &NewGame, sizeof(GameStatus));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GameList.push_back(NewGame);
|
|
|
|
|
|
|
|
return true;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::RemoveAll()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
GameList.clear();
|
|
|
|
std::vector<GameStatus>().swap(GameList);
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return Save();
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::Remove(const char * id)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(u32 i = 0; i < GameList.size(); ++i)
|
|
|
|
{
|
|
|
|
if(strncasecmp(id, GameList[i].id, 6) == 0)
|
|
|
|
{
|
|
|
|
GameList.erase(GameList.begin()+i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::Load(const char * path)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
char line[1024];
|
|
|
|
char filepath[300];
|
|
|
|
snprintf(filepath, sizeof(filepath), "%sGXGameStatistics.cfg", path);
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ConfigPath = filepath;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
FILE *file = fopen(filepath, "r");
|
|
|
|
if (!file) return false;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!ValidVersion(file))
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-06 19:59:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
while (fgets(line, sizeof(line), file))
|
|
|
|
{
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
this->ParseLine(line);
|
|
|
|
}
|
|
|
|
fclose(file);
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
2011-01-06 19:59:45 +01:00
|
|
|
bool CGameStatistics::ValidVersion(FILE * file)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!file) return false;
|
2011-01-06 19:59:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char line[255];
|
|
|
|
int revision = 0;
|
2011-01-06 19:59:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
while (fgets(line, sizeof(line), file))
|
|
|
|
{
|
|
|
|
const char * ptr = strcasestr(line, "USB Loader GX R");
|
|
|
|
if(ptr)
|
|
|
|
{
|
|
|
|
ptr += strlen("USB Loader GX R");
|
|
|
|
revision = atoi(ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-06 19:59:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
rewind(file);
|
2011-01-06 19:59:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return revision >= VALID_CONFIG_REV;
|
2011-01-06 19:59:45 +01:00
|
|
|
}
|
|
|
|
|
2010-09-25 10:51:44 +02:00
|
|
|
bool CGameStatistics::Save()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
char filepath[300];
|
|
|
|
strcpy(filepath, ConfigPath.c_str());
|
|
|
|
|
|
|
|
char * ptr = strrchr(filepath, '/');
|
|
|
|
if(ptr)
|
|
|
|
ptr[0] = 0;
|
|
|
|
|
|
|
|
if(!CreateSubfolder(filepath))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FILE * f = fopen(ConfigPath.c_str(), "w");
|
|
|
|
if (!f) return false;
|
|
|
|
|
|
|
|
fprintf(f, "# USB Loader GX R%s - Game statistics file\n", GetRev());
|
|
|
|
fprintf(f, "# note: this file is automatically generated\n");
|
|
|
|
fprintf(f, "# Num Games: %d\n", GameList.size());
|
|
|
|
for (u32 i = 0; i < GameList.size(); i++)
|
|
|
|
{
|
|
|
|
fprintf(f, "game:%s = ", GameList[i].id);
|
|
|
|
fprintf(f, "FavoriteRank:%d; ", GameList[i].FavoriteRank);
|
|
|
|
fprintf(f, "PlayCount:%d;\n", GameList[i].PlayCount);
|
|
|
|
}
|
|
|
|
fprintf(f, "# END\n");
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return true;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::SetSetting(GameStatus & game, char *name, char *value)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if(strcmp(name, "FavoriteRank") == 0)
|
|
|
|
{
|
|
|
|
if (sscanf(value, "%d", &i) == 1)
|
|
|
|
{
|
|
|
|
game.FavoriteRank = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(strcmp(name, "PlayCount") == 0)
|
|
|
|
{
|
|
|
|
if (sscanf(value, "%d", &i) == 1)
|
|
|
|
{
|
|
|
|
game.PlayCount = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGameStatistics::ReadGameID(const char * src, char * GameID, int size)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(strncasecmp(src, "game:", 5) != 0)
|
|
|
|
return false;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * ptr = strchr(src, ':');
|
|
|
|
if(!ptr)
|
|
|
|
return false;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ptr++;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int i = 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
for(i = 0; i < size; i++, ptr++)
|
|
|
|
{
|
|
|
|
if(*ptr == ' ' || *ptr == '\0')
|
|
|
|
break;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameID[i] = *ptr;
|
|
|
|
}
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameID[i] = 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
2011-01-13 20:05:31 +01:00
|
|
|
|
2010-09-25 10:51:44 +02:00
|
|
|
void CGameStatistics::ParseLine(char *line)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
char name[1024], value[1024];
|
|
|
|
char GameID[8];
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!ReadGameID(line, GameID, 6))
|
|
|
|
return;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-11-12 19:14:09 +01:00
|
|
|
if(strlen(GameID) != 6 && strlen(GameID) != 4)
|
2011-07-26 00:28:22 +02:00
|
|
|
return;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus NewGame;
|
|
|
|
memset(&NewGame, 0, sizeof(GameStatus));
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
snprintf(NewGame.id, sizeof(NewGame.id), GameID);
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * LinePtr = strchr(line, '=');
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
while(LinePtr != NULL)
|
|
|
|
{
|
|
|
|
LinePtr++;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * eq = strchr(LinePtr, ':');
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!eq) break;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
this->TrimLine(name, LinePtr, sizeof(name));
|
|
|
|
this->TrimLine(value, eq + 1, sizeof(value));
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SetSetting(NewGame, name, value);
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
LinePtr = strchr(LinePtr, ';');
|
|
|
|
}
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
AddGame(NewGame);
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGameStatistics::TrimLine(char *dest, const char *src, int size)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
while (*src == ' ')
|
|
|
|
src++;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int i = 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
for(i = 0; i < size; i++, src++)
|
|
|
|
{
|
|
|
|
if(*src == ';' || *src == ':' || *src == '\n' ||
|
|
|
|
*src == '\r' || *src == '\0')
|
|
|
|
break;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
dest[i] = *src;
|
|
|
|
}
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
dest[i] = '\0';
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGameStatistics::SetPlayCount(const char * id, int count)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus NewStatus;
|
|
|
|
snprintf(NewStatus.id, sizeof(NewStatus.id), id);
|
|
|
|
NewStatus.FavoriteRank = 0;
|
|
|
|
NewStatus.PlayCount = count;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus * game = GetGameStatus(id);
|
|
|
|
if(game)
|
|
|
|
{
|
|
|
|
NewStatus.FavoriteRank = game->FavoriteRank;
|
|
|
|
}
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
AddGame(NewStatus);
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGameStatistics::SetFavoriteRank(const char * id, int rank)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus NewStatus;
|
|
|
|
snprintf(NewStatus.id, sizeof(NewStatus.id), id);
|
|
|
|
NewStatus.FavoriteRank = rank;
|
|
|
|
NewStatus.PlayCount = 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus * game = GetGameStatus(id);
|
|
|
|
if(game)
|
|
|
|
{
|
|
|
|
NewStatus.PlayCount = game->PlayCount;
|
|
|
|
}
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
AddGame(NewStatus);
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
2011-01-13 20:05:31 +01:00
|
|
|
int CGameStatistics::GetPlayCount(const char * id) const
|
2010-09-25 10:51:44 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus * game = GetGameStatus(id);
|
|
|
|
if(game)
|
|
|
|
return game->PlayCount;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
2011-01-13 20:05:31 +01:00
|
|
|
int CGameStatistics::GetFavoriteRank(const char * id) const
|
2010-09-25 10:51:44 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!id)
|
|
|
|
return 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
GameStatus * game = GetGameStatus(id);
|
|
|
|
if(game)
|
|
|
|
return game->FavoriteRank;
|
2010-09-25 10:51:44 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return 0;
|
2010-09-25 10:51:44 +02:00
|
|
|
}
|