usbloadergx/source/settings/newtitles.cpp

210 lines
4.7 KiB
C++
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include "CSettings.h"
#include "settings/CGameStatistics.h"
#include "newtitles.h"
#define NEW_SECONDS 24 * 60 * 60
#define GAMETITLES "gametitles.txt"
NewTitles *NewTitles::instance = NULL;
NewTitles* NewTitles::Instance()
{
2010-09-24 02:48:03 +02:00
if (instance == NULL)
{
instance = new NewTitles();
}
return instance;
}
void NewTitles::DestroyInstance()
{
2010-09-24 02:48:03 +02:00
if (instance != NULL)
{
delete instance;
instance = NULL;
}
}
NewTitles::NewTitles()
{
firstTitle = lastTitle = NULL;
isDirty = isNewFile = false;
// Read the text file
char path[255];
2010-09-24 02:48:03 +02:00
strcpy(path, Settings.titlestxt_path);
path[strlen(Settings.titlestxt_path) - 1] = '/';
strcat(path, GAMETITLES);
char line[20];
2010-09-24 02:48:03 +02:00
FILE *fp = fopen(path, "r");
if (fp != NULL)
{
2010-09-24 02:48:03 +02:00
while (fgets(line, sizeof(line), fp))
{
// This is one line
2010-09-24 02:48:03 +02:00
if (line[0] != '#' || line[0] != ';')
{
Title *title = new Title();
2010-09-24 02:48:03 +02:00
if (sscanf(line, "%6c:%ld", (u8 *) &title->titleId, &title->timestamp) == 2)
{
2010-09-24 02:48:03 +02:00
if (firstTitle == NULL)
{
firstTitle = title;
lastTitle = title;
}
else
{
lastTitle->next = title;
lastTitle = title;
}
}
else
{
delete title; // Invalid title entry, ignore
}
}
}
2010-09-24 02:48:03 +02:00
fclose(fp);
}
else
{
isNewFile = true;
}
}
NewTitles::~NewTitles()
{
Save();
Title *t = firstTitle;
2010-09-24 02:48:03 +02:00
while (t != NULL)
{
2010-09-24 02:48:03 +02:00
Title *temp = (Title *) t->next;
delete t;
t = temp;
}
firstTitle = lastTitle = NULL;
}
2010-09-24 02:48:03 +02:00
void NewTitles::CheckGame(u8 *titleid)
{
2010-09-24 02:48:03 +02:00
if (titleid == NULL || strlen((char *) titleid) == 0)
{
return;
}
Title *t = firstTitle;
2010-09-24 02:48:03 +02:00
while (t != NULL)
{
// Loop all titles, search for the correct titleid
2010-09-24 02:48:03 +02:00
if (strcmp((const char *) titleid, (const char *) t->titleId) == 0)
{
return; // Game found, which is excellent
}
2010-09-24 02:48:03 +02:00
t = (Title *) t->next;
}
// Not found, add it
t = new Title();
2010-09-24 02:48:03 +02:00
strncpy((char *) t->titleId, (char *) titleid, 6);
t->timestamp = time(NULL);
if (isNewFile)
{
2010-09-24 02:48:03 +02:00
t->timestamp -= (NEW_SECONDS + 1); // Mark all games as not new if this is a new file
}
2010-09-24 02:48:03 +02:00
if (firstTitle == NULL)
{
firstTitle = t;
lastTitle = t;
}
else
{
lastTitle -> next = t;
lastTitle = t;
}
isDirty = true;
}
2010-09-24 02:48:03 +02:00
bool NewTitles::IsNew(u8 *titleid)
{
2010-09-24 02:48:03 +02:00
if (titleid == NULL || strlen((char *) titleid) == 0) return false;
Title *t = firstTitle;
2010-09-24 02:48:03 +02:00
while (t != NULL)
{
// Loop all titles, search for the correct titleid
2010-09-24 02:48:03 +02:00
if (strcmp((const char *) titleid, (const char *) t->titleId) == 0)
{
// This title is less than 24 hours old
2010-09-24 02:48:03 +02:00
if ((time(NULL) - t->timestamp) < NEW_SECONDS)
{
// Only count the game as new when it's never been played through GX
GameStatus *gnum = GameStatistics.GetGameStatus(titleid);
return gnum == NULL || gnum->PlayCount == 0;
}
return false;
}
2010-09-24 02:48:03 +02:00
t = (Title *) t->next;
}
// We should never get here, since all files should be added by now!
2010-09-24 02:48:03 +02:00
CheckGame(titleid);
return !isNewFile; // If this is a new file, return false
}
2010-09-24 02:48:03 +02:00
void NewTitles::Remove(u8 *titleid)
{
2010-09-24 02:48:03 +02:00
if (titleid == NULL || strlen((char *) titleid) == 0) return;
Title *t = firstTitle, *prev = NULL;
2010-09-24 02:48:03 +02:00
while (t != NULL)
{
2010-09-24 02:48:03 +02:00
if (strcmp((const char *) titleid, (const char *) t->titleId) == 0)
{
2010-09-24 02:48:03 +02:00
if (prev == NULL)
{
2010-09-24 02:48:03 +02:00
firstTitle = (Title *) t->next;
}
else
{
prev->next = t->next;
}
delete t;
isDirty = true;
return;
}
prev = t;
2010-09-24 02:48:03 +02:00
t = (Title *) t->next;
}
}
void NewTitles::Save()
{
2010-09-24 02:48:03 +02:00
if (!isDirty) return;
char path[255];
2010-09-24 02:48:03 +02:00
strcpy(path, Settings.titlestxt_path);
path[strlen(Settings.titlestxt_path) - 1] = '/';
strcat(path, GAMETITLES);
2010-09-24 02:48:03 +02:00
FILE *fp = fopen(path, "w");
if (fp != NULL)
{
Title *t = firstTitle;
2010-09-24 02:48:03 +02:00
while (t != NULL && strlen((char *) t->titleId) > 0)
{
2010-09-24 02:48:03 +02:00
fprintf(fp, "%s:%ld\n", t->titleId, t->timestamp);
t = (Title *) t->next;
}
2010-09-24 02:48:03 +02:00
fclose(fp);
}
}