-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDLGameList.c
executable file
·173 lines (142 loc) · 4.65 KB
/
HDLGameList.c
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
#include <errno.h>
#include <kernel.h>
#include <libcdvd.h>
#include <libpad.h>
#include <loadfile.h>
#include <malloc.h>
#include <sifrpc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fileXio_rpc.h>
#include <sys/fcntl.h>
#include "main.h"
#ifdef ENABLE_NETWORK_SUPPORT
#include <ps2ip.h>
#include <netman.h>
#endif
#include "HDLGameList.h"
#include "system.h"
#include "hdlfs/hdlfs.h"
struct HDLGameLinkedListNode
{
struct HDLGameLinkedListNode *next;
struct HDLGameEntry HDLGameEntry;
};
static int LoadHDLGameListIntoBuffer(struct HDLGameEntry **GameList);
static void FreeHDLGameListInBuffer(struct HDLGameEntry *GameList);
static unsigned int CurrentHDLGameListGeneration = 0;
static struct HDLGameEntry *CentralGameList = NULL;
static int NumGamesInCentralGameList = 0;
static int CentralGameListLockSemaID;
extern struct RuntimeData RuntimeData;
void LockCentralHDLGameList(void)
{
WaitSema(CentralGameListLockSemaID);
}
void UnlockCentralHDLGameList(void)
{
SignalSema(CentralGameListLockSemaID);
}
void InitCentralHDLGameList(void)
{
ee_sema_t sema;
sema.init_count = 1;
sema.max_count = 1;
sema.attr = 0;
sema.option = (u32) "GameListLock";
CentralGameListLockSemaID = CreateSema(&sema);
}
void DeinitCentralHDLGameList(void)
{
WaitSema(CentralGameListLockSemaID);
DeleteSema(CentralGameListLockSemaID);
FreeHDLGameList();
}
static int GameEntryComparator(const void *e1, const void *e2)
{
return (strcmp(((struct HDLGameEntry *)e1)->GameTitle, ((struct HDLGameEntry *)e2)->GameTitle));
}
int LoadHDLGameList(struct HDLGameEntry **GameList)
{
LockCentralHDLGameList();
FreeHDLGameListInBuffer(CentralGameList);
NumGamesInCentralGameList = LoadHDLGameListIntoBuffer(&CentralGameList);
if (RuntimeData.SortTitles)
qsort(CentralGameList, NumGamesInCentralGameList, sizeof(struct HDLGameEntry), &GameEntryComparator);
CurrentHDLGameListGeneration++;
UnlockCentralHDLGameList();
if (GameList != NULL)
*GameList = CentralGameList;
return NumGamesInCentralGameList;
}
unsigned int GetHDLGameListGeneration(void)
{
return CurrentHDLGameListGeneration;
}
int GetHDLGameList(struct HDLGameEntry **GameList)
{
if (GameList != NULL)
*GameList = CentralGameList;
return NumGamesInCentralGameList;
}
void FreeHDLGameList(void)
{
FreeHDLGameListInBuffer(CentralGameList);
NumGamesInCentralGameList = 0;
CentralGameList = NULL;
}
static int LoadHDLGameListIntoBuffer(struct HDLGameEntry **GameList)
{
struct HDLGameLinkedListNode *LinkedList, *first, *prev;
struct HDLGameEntry GameEntry;
iox_dirent_t dirent;
int result, fd;
unsigned int NumGames, i;
DEBUG_PRINTF("Loading game list...\n");
NumGames = 0;
first = NULL;
LinkedList = NULL;
*GameList = NULL;
result = 0;
if ((fd = fileXioDopen("hdd0:")) >= 0) {
while (fileXioDread(fd, &dirent) > 0) {
/* The APA driver stores the partition type in the mode field. */
if (dirent.stat.mode == HDL_FS_MAGIC && !(dirent.stat.attr & APA_FLAG_SUB)) {
if (RetrieveGameInstallationSector(dirent.stat.private_5, dirent.name, &GameEntry) == 0) {
/* The head of the list needs to be formed first. */
if (first == NULL) {
LinkedList = malloc(sizeof(struct HDLGameLinkedListNode));
first = LinkedList;
} else {
LinkedList->next = malloc(sizeof(struct HDLGameLinkedListNode));
LinkedList = LinkedList->next;
}
NumGames++;
memcpy(&LinkedList->HDLGameEntry, &GameEntry, sizeof(LinkedList->HDLGameEntry));
LinkedList->next = NULL;
}
}
}
fileXioDclose(fd);
}
/* Now, if no errors occurred, begin consolidating the game list. */
if (result >= 0 && NumGames > 0) {
*GameList = malloc(sizeof(struct HDLGameEntry) * NumGames);
LinkedList = first;
for (i = 0; LinkedList != NULL; i++) {
memcpy(&(*GameList)[i], &LinkedList->HDLGameEntry, sizeof(struct HDLGameEntry));
prev = LinkedList;
LinkedList = LinkedList->next;
free(prev);
}
result = NumGames;
}
DEBUG_PRINTF("Game list loaded. result: %d\n", result);
return (result < 0 ? 0 : result);
}
static void FreeHDLGameListInBuffer(struct HDLGameEntry *GameList)
{
if (GameList != NULL)
free(GameList);
}