102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
archive.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
7
8#include <string.h>
9
10// Original pcdogs.exe: 0x43ec30
11void *PKG_LoadEntryAlloc(int32_t toc_index, int32_t unused_zero)
12{
13 (void)unused_zero;
14 return PKG_LoadEntry(toc_index, NULL, 1);
15}
16
17// Original pcdogs.exe: 0x43ee00
18void *PKG_LoadEntry(int32_t toc_index, void *dest_buffer, int32_t unused_mode)
19{
20 (void)unused_mode;
21 uint32_t file_size = g_pkgTOC[toc_index].fileSize;
22 if ((int32_t)file_size <= 0)
23 return NULL;
24
25 /* Entries occupy whole 2048-byte sectors, including the final partial sector. */
26 uint32_t read_size = (file_size + PKG_SECTOR_OFFSET_MASK) & ~(uint32_t)PKG_SECTOR_OFFSET_MASK;
27 if (!dest_buffer)
28 {
29 dest_buffer = Pkg_AllocateResourceMemory(read_size);
30 if (!dest_buffer)
31 return NULL;
32 }
33
34 CRT_fseek(g_pkgFileHandle, g_pkgTOC[toc_index].fileOffset, SEEK_SET);
35 CRT_fread(dest_buffer, 1, read_size, g_pkgFileHandle);
36 return dest_buffer;
37}
38
39static bool package_exists(const char *path)
40{
41 struct File_Handle *stream = CRT_fopen(path, "rb");
42 if (!stream)
43 return false;
44
45 CRT_fclose(stream);
46 return true;
47}
48
49// Original pcdogs.exe: 0x43eea0
51{
52 GetCurrentDirectoryA(MAX_PATH, g_pkgBasePath);
53 char *end = g_pkgBasePath;
54 while (*end)
55 ++end;
56 if (end[-1] != '\\')
57 {
58 *end++ = '\\';
59 *end = '\0';
60 }
61
62 if (package_exists("pcdogs.pkg"))
63 return;
64
65 /* The original search stops at the first nonexistent drive and excludes Z. */
66 for (char drive = 'C'; drive < 'Z'; ++drive)
67 {
68 char root[4];
69 String_FormatCRTString(root, "%c:\\", (int)drive);
70 DWORD type = GetDriveTypeA(root);
71 if (type == DRIVE_UNKNOWN || type == DRIVE_NO_ROOT_DIR)
72 break;
73
74 char path[MAX_PATH];
75 /* "Dalmations" is the directory spelling in the shipped executable. */
76 String_FormatCRTString(g_pkgBasePath, "%sDalmations\\Setup\\102Dalms\\", root);
77 String_FormatCRTString(path, "%spcdogs.pkg", g_pkgBasePath);
78 if (package_exists(path))
79 return;
80
81 String_FormatCRTString(g_pkgBasePath, "%sSetup\\102Dalms\\", root);
82 String_FormatCRTString(path, "%spcdogs.pkg", g_pkgBasePath);
83 if (package_exists(path))
84 return;
85 }
86
87 MessageBoxA(NULL, "Cannot find pcdogs.pkg. Aborting.", "Error", MB_OK);
88}
89
90// Original pcdogs.exe: 0x43f010
92{
93 char path[MAX_PATH];
94 String_FormatCRTString(path, "%spcdogs.pkg", g_pkgBasePath);
95 g_pkgFileHandle = CRT_fopen(path, "rb");
96 if (!g_pkgFileHandle)
97 return;
98
99 struct Pkg_Header *header = Pkg_AllocateResourceMemory(sizeof(*header));
100 CRT_fread(header, 1, sizeof(*header), g_pkgFileHandle);
101 memcpy(g_pkgTOC, header->entries, sizeof(g_pkgTOC));
103}
104
105// Original pcdogs.exe: 0x43f090
106void PKG_Close(void)
107{
108 if (g_pkgFileHandle)
109 {
111 g_pkgFileHandle = NULL;
112 }
113}
void * PKG_LoadEntryAlloc(int32_t toc_index, int32_t unused_zero)
Definition archive.c:11
void PKG_Close(void)
Definition archive.c:106
void PKG_LocatePackagePath(void)
Definition archive.c:50
static bool package_exists(const char *path)
Definition archive.c:39
void PKG_OpenAndReadTOC(void)
Definition archive.c:91
void * PKG_LoadEntry(int32_t toc_index, void *dest_buffer, int32_t unused_mode)
Definition archive.c:18
#define PKG_SECTOR_OFFSET_MASK
Definition constants.h:26
void Pkg_FreeResourceMemory(void *user_ptr)
Definition resources.c:342
void * Pkg_AllocateResourceMemory(uint32_t size)
Definition resources.c:317
#define g_pkgFileHandle
Definition globals.h:87
#define g_pkgTOC
Definition globals.h:90
#define g_pkgBasePath
Definition globals.h:102
int32_t CRT_fclose(struct File_Handle *stream)
Definition stdio.c:80
int32_t CRT_fseek(struct File_Handle *stream, int32_t offset, int32_t origin)
Definition stdio.c:235
uint32_t CRT_fread(void *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
Definition stdio.c:283
struct File_Handle * CRT_fopen(char const *filename, char const *mode)
Definition stdio.c:134
int32_t String_FormatCRTString(char *buffer, char const *format,...)
Definition formatting.c:18
struct Pkg_TocEntry entries[0x8a]
Definition types.h:1196
#define MB_OK