| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "scott.h" |
| 14 |
#include "scottdefines.h" |
| 15 |
#include "scottgameinfo.h" |
| 16 |
|
| 17 |
#include "c64decrunch.h" |
| 18 |
#include "c64diskimage.h" |
| 19 |
#include "detectgame.h" |
| 20 |
#include "sagadraw.h" |
| 21 |
#include "sagagraphics.h" |
| 22 |
|
| 23 |
#include "hulk.h" |
| 24 |
|
| 25 |
#include "unp64_interface.h" |
| 26 |
|
| 27 |
typedef enum { |
| 28 |
UNKNOWN_FILE_TYPE, |
| 29 |
TYPE_D64, |
| 30 |
TYPE_T64, |
| 31 |
TYPE_US |
| 32 |
} file_type; |
| 33 |
|
| 34 |
struct c64rec { |
| 35 |
GameIDType id; |
| 36 |
size_t length; |
| 37 |
uint16_t chk; |
| 38 |
file_type type; |
| 39 |
int decompress_iterations; |
| 40 |
const char *switches; |
| 41 |
const char *appendfile; |
| 42 |
int parameter; |
| 43 |
size_t copysource; |
| 44 |
size_t copydest; |
| 45 |
size_t copysize; |
| 46 |
size_t imgoffset; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
static const struct c64rec c64_registry[] = { |
| 51 |
{ BATON_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 52 |
{ TIME_MACHINE_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 53 |
{ ARROW1_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 54 |
{ ARROW2_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 55 |
{ PULSAR7_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 56 |
{ CIRCUS_C64, 0x2ab00, 0xc3fc, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 57 |
|
| 58 |
{ FEASIBILITY_C64, 0x2ab00, 0x9eaa, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 59 |
{ AKYRZ_C64, 0x2ab00, 0x9eaa, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 60 |
{ PERSEUS_C64, 0x2ab00, 0x9eaa, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 61 |
{ INDIANS_C64, 0x2ab00, 0x9eaa, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 62 |
{ WAXWORKS_C64, 0x2ab00, 0x9eaa, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 63 |
{ BATON_C64, 0x2ab00, 0x9dca, TYPE_D64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 64 |
|
| 65 |
{ BATON_C64, 0x5170, 0xb240, TYPE_T64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 66 |
{ BATON_C64, 0x2ab00, 0xbfbf, TYPE_D64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 67 |
{ FEASIBILITY_C64, 0x2ab00, 0x9c18, TYPE_D64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 68 |
{ TIME_MACHINE_C64, 0x5032, 0x5635, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 69 |
{ TIME_MACHINE_C64, 0x2ab00, 0xed21, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 70 |
{ ARROW1_C64, 0x5b46, 0x92db, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 71 |
{ ARROW1_C64, 0x2ab00, 0xe71d, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 72 |
{ ARROW1_C64, 0x2ab00, 0x7687, TYPE_D64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 73 |
{ ARROW2_C64, 0x5fe2, 0xe14f, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 74 |
{ PULSAR7_C64, 0x46bf, 0x1679, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 75 |
{ CIRCUS_C64, 0x4269, 0xa449, TYPE_T64, 2, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 76 |
{ FEASIBILITY_C64, 0x5a7b, 0x0f48, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 77 |
{ AKYRZ_C64, 0x2ab00, 0x6cca, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 78 |
{ AKYRZ_C64, 0x4be1, 0x5a00, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 79 |
{ PERSEUS_C64, 0x502b, 0x913b, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 80 |
{ PERSEUS_C64, 0x2ab00, 0xdc5e, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 81 |
{ INDIANS_C64, 0x4f9f, 0xe6c8, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 82 |
{ WAXWORKS_C64, 0x4a11, 0xa37a, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 83 |
{ WAXWORKS_C64, 0x2ab00, 0x0a78, TYPE_D64, 3, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 84 |
|
| 85 |
{ ADVENTURELAND_C64, 0x6a10, 0x1910, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 86 |
{ ADVENTURELAND_C64, 0x6a10, 0x1b10, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 87 |
{ ADVENTURELAND_C64, 0x2ab00, 0x6638, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 88 |
{ ADVENTURELAND_C64, 0x2adab, 0x751f, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 89 |
{ ADVENTURELAND_C64, 0x2adab, 0x64a4, TYPE_D64, 0, NULL, "SAG1PIC", -0xa53, 0, 0, 0, 0x65af }, |
| 90 |
{ ADVENTURELAND_C64, 0x2adab, 0x8847, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 91 |
|
| 92 |
{ PIRATE_US, 0x2adab, 0x04c5, TYPE_US, 0, NULL, "PIRATE", 0, 0, 0, 0, 0x06d30 }, |
| 93 |
|
| 94 |
{ SECRET_MISSION_C64, 0x88be, 0xa122, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 95 |
{ SECRET_MISSION_C64, 0x2ab00, 0x04d6, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, -0x1bff }, |
| 96 |
{ SECRET_MISSION_C64, 0x2adab, 0x3ca3, TYPE_D64, 0, NULL, "SAG3PIC", -0x83a, 0, 0, 0, 0x67c8 }, |
| 97 |
|
| 98 |
{ VOODOO_CASTLE_US, 0x2adab, 0xcb2b, TYPE_US, 0, NULL, "SAGA1", 0, 0, 0, 0, 0x06c30 }, |
| 99 |
{ VOODOO_CASTLE_US, 0x2ab00, 0x8969, TYPE_US, 1, NULL, "VOODOO CASTLE", 0, 0, 0, 0, 0x06c30 }, |
| 100 |
{ VOODOO_CASTLE_US, 0x2ab00, 0x2682, TYPE_US, 1, NULL, "VOODOO CASTLE", 0, 0, 0, 0, 0x06c30 }, |
| 101 |
{ VOODOO_CASTLE_US, 0x2ab00, 0xac79, TYPE_US, 0, NULL, "VOODOO CASTLE 2", 0, 0, 0, 0, 0x06c30 }, |
| 102 |
|
| 103 |
{ CLAYMORGUE_C64, 0x6ff7, 0xe4ed, TYPE_T64, 3, NULL, NULL, 0, 0x855, 0x7352, 0x20, 0 }, |
| 104 |
|
| 105 |
{ CLAYMORGUE_C64, 0x912f, 0xa69f, TYPE_T64, 1, NULL, NULL, 0, 0x855, 0x7352, 0x20, 0 }, |
| 106 |
|
| 107 |
{ CLAYMORGUE_C64, 0x2ab00, 0xf70c, TYPE_D64, 1, NULL, NULL, 0, 0x855, 0x7352, 0x20, 0 }, |
| 108 |
{ CLAYMORGUE_C64, 0xc0dd, 0x3701, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, -0x7fe }, |
| 109 |
{ CLAYMORGUE_C64, 0xbc5f, 0x492c, TYPE_T64, 1, NULL, NULL, 0, 0x855, 0x7352, 0x20, 0 }, |
| 110 |
{ CLAYMORGUE_C64, 0x2ab00, 0xfd67, TYPE_D64, 1, NULL, NULL, 0, 0x855, 0x7352, 0x20, 0 }, |
| 111 |
{ CLAYMORGUE_C64, 0x2ab00, 0x7ece, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, -0x7fe }, |
| 112 |
|
| 113 |
|
| 114 |
{ CLAYMORGUE_US, 0x2adab, 0x1fac, TYPE_US, 0, NULL, "SAGA.DB", 0, 0, 0, 0, 0 }, |
| 115 |
{ CLAYMORGUE_US, 0x2ab00, 0xa957, TYPE_US, 0, NULL, "SAGA.DB", 0, 0, 0, 0, 0 }, |
| 116 |
{ CLAYMORGUE_US, 0x2ab00, 0xfbb6, TYPE_US, 0, NULL, "SAGA.DB", 0, 0, 0, 0, 0 }, |
| 117 |
{ CLAYMORGUE_US, 0x2ab00, 0x0b54, TYPE_US, 0, NULL, "SAGA.DB", 0, 0, 0, 0, 0 }, |
| 118 |
|
| 119 |
{ HULK_C64, 0x2ab00, 0xcdd8, TYPE_D64, 0, NULL, NULL, 0, 0x1806, 0xb801, 0x307, 0 }, |
| 120 |
{ HULK_C64, 0x8534, 0x623a, TYPE_T64, 2, NULL, NULL, 0, 0x1806, 0xb801, 0x307, 0 }, |
| 121 |
{ HULK_US, 0x2ab00, 0x2918, TYPE_US, 0, NULL, "SHULK.DB", 0, 0, 0, 0, 0 }, |
| 122 |
{ SPIDERMAN_C64, 0x2ab00, 0xde56, TYPE_D64, 0, NULL, NULL, 0, 0x1801, 0xa801, 0x2000, 0 }, |
| 123 |
{ SPIDERMAN_C64, 0x2ab00, 0x2736, TYPE_D64, 0, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 124 |
{ SPIDERMAN_C64, 0x2ab00, 0x490a, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, -0x7ff }, |
| 125 |
{ SPIDERMAN_C64, 0x2ab00, 0xc4c4, TYPE_D64, 0, NULL, NULL, 0, 0x1801, 0xa801, 0x2000, 0 }, |
| 126 |
{ SPIDERMAN_C64, 0x08e72, 0xb2f4, TYPE_T64, 3, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 127 |
|
| 128 |
{ SAVAGE_ISLAND_C64, 0x2ab00, 0x8801, TYPE_D64, 1, "-f86 -d0x1793", "SAVAGEISLAND1+", 1, 0, 0, 0, 0 }, |
| 129 |
{ SAVAGE_ISLAND2_C64, 0x2ab00, 0x8801, TYPE_D64, 1, "-f86 -d0x178b", "SAVAGEISLAND2+", 1, 0, 0, 0, 0 }, |
| 130 |
{ SAVAGE_ISLAND_C64, 0x2ab00, 0xc361, TYPE_D64, 1, "-f86 -d0x1793", "SAVAGE ISLAND P1", 1, 0, 0, 0, 0 }, |
| 131 |
{ SAVAGE_ISLAND2_C64, 0x2ab00, 0xc361, TYPE_D64, 1, NULL, "SAVAGE ISLAND P2", 0, 0, 0, 0, 0 }, |
| 132 |
|
| 133 |
{ ROBIN_OF_SHERWOOD_C64, 0x2ab00, 0xcf9e, TYPE_D64, 1, NULL, NULL, 0, 0x1802, 0xbd27, 0x1f6c, 0 }, |
| 134 |
{ ROBIN_OF_SHERWOOD_C64, 0x2ab00, 0xc0c7, TYPE_D64, 1, NULL, NULL, 0, 0xd7fb, 0xbd20, 0x1f6c, 0 }, |
| 135 |
{ ROBIN_OF_SHERWOOD_C64, 0x2ab00, 0xc6e6, TYPE_D64, 1, NULL, NULL, 0, 0x9702, 0x9627, 0x1f6c, 0 }, |
| 136 |
{ ROBIN_OF_SHERWOOD_C64, 0xb2ef, 0x7c44, TYPE_T64, 1, NULL, NULL, 0, 0x9702, 0x9627, 0x1f6c, 0 }, |
| 137 |
{ ROBIN_OF_SHERWOOD_C64, 0xb690, 0x7b61, TYPE_T64, 1, NULL, NULL, 0, 0x9702, 0x9627, 0x1f6c, 0 }, |
| 138 |
{ ROBIN_OF_SHERWOOD_C64, 0x8db6, 0x7853, TYPE_T64, 1, NULL, NULL, 0, 0xd7fb, 0xbd20, 0x1f6c, 0 }, |
| 139 |
|
| 140 |
{ GREMLINS_C64, 0xdd94, 0x25a8, TYPE_T64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 141 |
{ GREMLINS_C64, 0x2ab00, 0xc402, TYPE_D64, 0, NULL, "G1", -0x8D, 0, 0, 0, 0 }, |
| 142 |
{ GREMLINS_C64, 0x2ab00, 0x3ccf, TYPE_D64, 0, NULL, "G1", -0x8D, 0, 0, 0, 0 }, |
| 143 |
{ GREMLINS_C64, 0x2ab00, 0xabf8, TYPE_D64, 2, "-e0x1255", NULL, 2, 0, 0, 0, 0 }, |
| 144 |
{ GREMLINS_C64, 0x2ab00, 0xa265, TYPE_D64, 2, "-e0x1255", NULL, 2, 0, 0, 0, 0 }, |
| 145 |
{ GREMLINS_C64, 0x2ab00, 0xa1dc, TYPE_D64, 2, "-e0x1255", NULL, 2, 0, 0, 0, 0 }, |
| 146 |
{ GREMLINS_SPANISH_C64,0x2ab00, 0x2ad9, TYPE_D64, 1, NULL, NULL, 0, 0xd002, 0xbf8a, 0x1f00, 0 }, |
| 147 |
{ GREMLINS_C64, 0x2ab00, 0x4be2, TYPE_D64, 0, NULL, "G1", -0x8D, 0, 0, 0, 0 }, |
| 148 |
{ GREMLINS_C64, 0x2ab00, 0x5c7a, TYPE_D64, 3, "-e0x2ec2", NULL, 2, 0xc801, 0xd774, 0xbeb, 0 }, |
| 149 |
{ GREMLINS_C64, 0x2ab00, 0x331a, TYPE_D64, 2, NULL, NULL, 0, 0xd80f, 0xc782, 0x1f00, 0 }, |
| 150 |
{ GREMLINS_GERMAN_C64, 0xc003, 0x558c, TYPE_T64, 1, NULL, NULL, 0, 0xd801, 0xc6c0, 0x1f00, 0 }, |
| 151 |
{ GREMLINS_GERMAN_C64, 0x2ab00, 0x6729, TYPE_D64, 2, NULL, NULL, 0, 0xdc02, 0xcac1, 0x1f00, 0 }, |
| 152 |
|
| 153 |
{ SUPERGRAN_C64, 0x726f, 0x0901, TYPE_T64, 1, NULL, NULL, 0, 0xd802, 0xc623, 0x1f00, 0 }, |
| 154 |
{ SUPERGRAN_C64, 0x2ab00, 0x538c, TYPE_D64, 1, NULL, NULL, 0, 0, 0, 0, 0 }, |
| 155 |
|
| 156 |
{ SEAS_OF_BLOOD_C64, 0xa209, 0xf115, TYPE_T64, 6, "-e0x1000", NULL, 3, 0xd802, 0xb07c, 0x1fff, 0 }, |
| 157 |
|
| 158 |
|
| 159 |
{ SEAS_OF_BLOOD_C64, 0x2ab00, 0x5c1d, TYPE_D64, 1, NULL, NULL, 0, 0xd802, 0xb07c, 0x1fff, 0 }, |
| 160 |
{ SEAS_OF_BLOOD_C64, 0x2ab00, 0xe308, TYPE_D64, 1, NULL, NULL, 0, 0xd802, 0xb07c, 0x1fff, 0 }, |
| 161 |
{ SEAS_OF_BLOOD_C64, 0x2ab00, 0x3df2, TYPE_D64, 6, "-e0x1000", NULL, 3, 0xd802, 0xb07c, 0x1fff, 0 }, |
| 162 |
|
| 163 |
{ UNKNOWN_GAME, 0, 0, UNKNOWN_FILE_TYPE, 0, NULL, NULL, 0, 0, 0, 0, 0 } |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
uint16_t checksum(uint8_t *sf, uint32_t extent) |
| 168 |
{ |
| 169 |
uint16_t c = 0; |
| 170 |
for (int i = 0; i < extent; i++) |
| 171 |
c += sf[i]; |
| 172 |
return c; |
| 173 |
} |
| 174 |
|
| 175 |
static GameIDType DecrunchC64(uint8_t **sf, size_t *extent, struct c64rec entry); |
| 176 |
|
| 177 |
size_t writeToFile(const char *name, uint8_t *data, size_t size) |
| 178 |
{ |
| 179 |
FILE *fptr = fopen(name, "w"); |
| 180 |
|
| 181 |
if (fptr == NULL) { |
| 182 |
Fatal("File open error!"); |
| 183 |
} |
| 184 |
|
| 185 |
size_t result = fwrite(data, 1, size, fptr); |
| 186 |
|
| 187 |
fclose(fptr); |
| 188 |
return result; |
| 189 |
} |
| 190 |
|
| 191 |
static uint8_t *get_largest_file(uint8_t *data, int length, int *newlength) |
| 192 |
{ |
| 193 |
uint8_t *file = NULL; |
| 194 |
*newlength = 0; |
| 195 |
DiskImage *d64 = di_create_from_data(data, length); |
| 196 |
if (d64) { |
| 197 |
RawDirEntry *largest = find_largest_file_entry(d64); |
| 198 |
if (largest) { |
| 199 |
ImageFile *c64file = di_open(d64, largest->rawname, largest->type, "rb"); |
| 200 |
if (c64file) { |
| 201 |
uint8_t buf[0xffff]; |
| 202 |
*newlength = di_read(c64file, buf, 0xffff); |
| 203 |
file = MemAlloc(*newlength); |
| 204 |
memcpy(file, buf, *newlength); |
| 205 |
free(c64file); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
return file; |
| 210 |
} |
| 211 |
|
| 212 |
static uint8_t *get_file_named(uint8_t *data, int length, int *newlength, |
| 213 |
const char *name) |
| 214 |
{ |
| 215 |
uint8_t *file = NULL; |
| 216 |
*newlength = 0; |
| 217 |
DiskImage *d64 = di_create_from_data(data, length); |
| 218 |
unsigned char rawname[100]; |
| 219 |
di_rawname_from_name(rawname, name); |
| 220 |
if (d64) { |
| 221 |
ImageFile *c64file = di_open(d64, rawname, 0xc2, "rb"); |
| 222 |
if (c64file) { |
| 223 |
uint8_t buf[0xffff]; |
| 224 |
*newlength = di_read(c64file, buf, 0xffff); |
| 225 |
file = MemAlloc(*newlength); |
| 226 |
memcpy(file, buf, *newlength); |
| 227 |
free(c64file); |
| 228 |
} |
| 229 |
} |
| 230 |
return file; |
| 231 |
} |
| 232 |
|
| 233 |
uint8_t *save_island_appendix_1 = NULL; |
| 234 |
int save_island_appendix_1_length = 0; |
| 235 |
uint8_t *save_island_appendix_2 = NULL; |
| 236 |
int save_island_appendix_2_length = 0; |
| 237 |
|
| 238 |
static GameIDType savage_island_menu(uint8_t **sf, size_t *extent, int recindex) |
| 239 |
{ |
| 240 |
Output("This disk image contains two games. Select one.\n\n1. Savage Island " |
| 241 |
"part I\n2. Savage Island part II"); |
| 242 |
|
| 243 |
glk_request_char_event(Bottom); |
| 244 |
|
| 245 |
event_t ev; |
| 246 |
int result = 0; |
| 247 |
do { |
| 248 |
glk_select(&ev); |
| 249 |
if (ev.type == evtype_CharInput) { |
| 250 |
if (ev.val1 == '1' || ev.val1 == '2') { |
| 251 |
result = ev.val1 - '0'; |
| 252 |
} else { |
| 253 |
glk_request_char_event(Bottom); |
| 254 |
} |
| 255 |
} |
| 256 |
} while (result == 0); |
| 257 |
|
| 258 |
glk_window_clear(Bottom); |
| 259 |
|
| 260 |
recindex += result - 1; |
| 261 |
|
| 262 |
struct c64rec rec = c64_registry[recindex]; |
| 263 |
int length; |
| 264 |
uint8_t *file = get_file_named(*sf, *extent, &length, rec.appendfile); |
| 265 |
|
| 266 |
if (file != NULL) { |
| 267 |
if (rec.chk == 0xc361) { |
| 268 |
if (rec.switches != NULL) { |
| 269 |
save_island_appendix_1 = get_file_named( |
| 270 |
*sf, *extent, &save_island_appendix_1_length, "SI1PC1"); |
| 271 |
save_island_appendix_2 = get_file_named( |
| 272 |
*sf, *extent, &save_island_appendix_2_length, "SI1PC2"); |
| 273 |
} else { |
| 274 |
save_island_appendix_1 = get_file_named( |
| 275 |
*sf, *extent, &save_island_appendix_1_length, "SI2PIC"); |
| 276 |
} |
| 277 |
} |
| 278 |
free(*sf); |
| 279 |
*sf = file; |
| 280 |
*extent = length; |
| 281 |
if (save_island_appendix_1_length > 2) |
| 282 |
save_island_appendix_1_length -= 2; |
| 283 |
if (save_island_appendix_2_length > 2) |
| 284 |
save_island_appendix_2_length -= 2; |
| 285 |
return DecrunchC64(sf, extent, rec); |
| 286 |
} else { |
| 287 |
fprintf(stderr, "SCOTT: DetectC64() Failed loading file %s\n", rec.appendfile); |
| 288 |
return UNKNOWN_GAME; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static void appendSIfiles(uint8_t **sf, size_t *extent) |
| 293 |
{ |
| 294 |
uint8_t megabuf[0xffff]; |
| 295 |
memcpy(megabuf, *sf, *extent); |
| 296 |
free(*sf); |
| 297 |
int offset = 0x6202; |
| 298 |
|
| 299 |
if (save_island_appendix_1) { |
| 300 |
memcpy(megabuf + offset, save_island_appendix_1 + 2, |
| 301 |
save_island_appendix_1_length); |
| 302 |
free(save_island_appendix_1); |
| 303 |
} |
| 304 |
if (save_island_appendix_2) { |
| 305 |
memcpy(megabuf + offset + save_island_appendix_1_length, |
| 306 |
save_island_appendix_2 + 2, save_island_appendix_2_length); |
| 307 |
free(save_island_appendix_2); |
| 308 |
} |
| 309 |
*extent = offset + save_island_appendix_1_length + save_island_appendix_2_length; |
| 310 |
*sf = malloc(*extent); |
| 311 |
memcpy(*sf, megabuf, *extent); |
| 312 |
} |
| 313 |
|
| 314 |
static GameIDType mysterious_menu(uint8_t **sf, size_t *extent, int recindex) |
| 315 |
{ |
| 316 |
recindex = 0; |
| 317 |
|
| 318 |
Output("This disk image contains six games. Select one.\n\n1. The Golden Baton\n2. The Time Machine\n3. Arrow of Death part 1\n4. Arrow of Death part 2\n5. Escape from Pulsar 7\n6. Circus"); |
| 319 |
|
| 320 |
glk_request_char_event(Bottom); |
| 321 |
|
| 322 |
event_t ev; |
| 323 |
int result = 0; |
| 324 |
do { |
| 325 |
glk_select(&ev); |
| 326 |
if (ev.type == evtype_CharInput) { |
| 327 |
if (ev.val1 >= '1' && ev.val1 <= '6') { |
| 328 |
result = ev.val1 - '0'; |
| 329 |
} else { |
| 330 |
glk_request_char_event(Bottom); |
| 331 |
} |
| 332 |
} |
| 333 |
} while (result == 0); |
| 334 |
|
| 335 |
glk_window_clear(Bottom); |
| 336 |
|
| 337 |
const char *filename = NULL; |
| 338 |
switch (result) { |
| 339 |
case 1: |
| 340 |
filename = "BATON"; |
| 341 |
break; |
| 342 |
case 2: |
| 343 |
filename = "TIME MACHINE"; |
| 344 |
break; |
| 345 |
case 3: |
| 346 |
filename = "ARROW I"; |
| 347 |
break; |
| 348 |
case 4: |
| 349 |
filename = "ARROW II"; |
| 350 |
break; |
| 351 |
case 5: |
| 352 |
filename = "PULSAR 7"; |
| 353 |
break; |
| 354 |
case 6: |
| 355 |
filename = "CIRCUS"; |
| 356 |
break; |
| 357 |
default: |
| 358 |
fprintf(stderr, "SCOTT: DetectC64() Error!\n"); |
| 359 |
break; |
| 360 |
} |
| 361 |
|
| 362 |
int length; |
| 363 |
uint8_t *file = get_file_named(*sf, *extent, &length, filename); |
| 364 |
|
| 365 |
if (file != NULL) { |
| 366 |
free(*sf); |
| 367 |
*sf = file; |
| 368 |
*extent = length; |
| 369 |
struct c64rec rec = c64_registry[recindex - 1 + result]; |
| 370 |
return DecrunchC64(sf, extent, rec); |
| 371 |
} else { |
| 372 |
fprintf(stderr, "SCOTT: DetectC64() Failed loading file %s\n", filename); |
| 373 |
return UNKNOWN_GAME; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
static GameIDType mysterious_menu2(uint8_t **sf, size_t *extent, int recindex) |
| 378 |
{ |
| 379 |
recindex = 6; |
| 380 |
|
| 381 |
Output("This disk image contains five games. Select one.\n\n1. Feasibility Experiment\n2. The Wizard of Akyrz\n3. Perseus and Andromeda\n4. Ten Little Indians\n5. Waxworks"); |
| 382 |
|
| 383 |
glk_request_char_event(Bottom); |
| 384 |
|
| 385 |
event_t ev; |
| 386 |
int result = 0; |
| 387 |
do { |
| 388 |
glk_select(&ev); |
| 389 |
if (ev.type == evtype_CharInput) { |
| 390 |
if (ev.val1 >= '1' && ev.val1 <= '5') { |
| 391 |
result = ev.val1 - '0'; |
| 392 |
} else { |
| 393 |
glk_request_char_event(Bottom); |
| 394 |
} |
| 395 |
} |
| 396 |
} while (result == 0); |
| 397 |
|
| 398 |
glk_window_clear(Bottom); |
| 399 |
|
| 400 |
const char *filename = NULL; |
| 401 |
switch (result) { |
| 402 |
case 1: |
| 403 |
filename = "EXPERIMENT"; |
| 404 |
break; |
| 405 |
case 2: |
| 406 |
filename = "WIZARD OF AKYRZ"; |
| 407 |
break; |
| 408 |
case 3: |
| 409 |
filename = "PERSEUS"; |
| 410 |
break; |
| 411 |
case 4: |
| 412 |
filename = "INDIANS"; |
| 413 |
break; |
| 414 |
case 5: |
| 415 |
filename = "WAXWORKS"; |
| 416 |
break; |
| 417 |
default: |
| 418 |
fprintf(stderr, "Error!\n"); |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
int length; |
| 423 |
uint8_t *file = get_file_named(*sf, *extent, &length, filename); |
| 424 |
|
| 425 |
if (file != NULL) { |
| 426 |
free(*sf); |
| 427 |
*sf = file; |
| 428 |
*extent = length; |
| 429 |
struct c64rec rec = c64_registry[recindex - 1 + result]; |
| 430 |
return DecrunchC64(sf, extent, rec); |
| 431 |
} else { |
| 432 |
fprintf(stderr, "Failed loading file %s\n", filename); |
| 433 |
return UNKNOWN_GAME; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
static size_t CopyData(size_t dest, size_t source, uint8_t **data, size_t datasize, |
| 439 |
size_t bytestomove) |
| 440 |
{ |
| 441 |
if (source > datasize || *data == NULL) |
| 442 |
return 0; |
| 443 |
|
| 444 |
size_t newsize = MAX(dest + bytestomove, datasize); |
| 445 |
uint8_t *megabuf = MemAlloc(newsize); |
| 446 |
memcpy(megabuf, *data, datasize); |
| 447 |
memcpy(megabuf + dest, *data + source, bytestomove); |
| 448 |
free(*data); |
| 449 |
*data = megabuf; |
| 450 |
return newsize; |
| 451 |
} |
| 452 |
|
| 453 |
void PrintFirstTenBytes(uint8_t *ptr, size_t offset) { |
| 454 |
fprintf(stderr, "First 10 bytes at 0x%04zx: ", offset); |
| 455 |
for (int i = 0; i < 10; i++) |
| 456 |
fprintf(stderr, "\\x%02x", ptr[offset + i]); |
| 457 |
fprintf(stderr, "\n"); |
| 458 |
} |
| 459 |
|
| 460 |
void LoadC64USImages(uint8_t *data, size_t length) { |
| 461 |
int numfiles; |
| 462 |
|
| 463 |
DiskImage *d64 = di_create_from_data(data, length); |
| 464 |
if (d64) { |
| 465 |
char **filenames = get_all_file_names(d64, &numfiles); |
| 466 |
unsigned char rawname[1024]; |
| 467 |
if (filenames) { |
| 468 |
int imgindex = 0; |
| 469 |
if (numfiles > 1024) |
| 470 |
numfiles = 1024; |
| 471 |
char *imagefiles[1024]; |
| 472 |
for (int i = 0; i < numfiles; i++) { |
| 473 |
if (issagaimg(filenames[i])) { |
| 474 |
imagefiles[imgindex++] = filenames[i]; |
| 475 |
} else { |
| 476 |
free(filenames[i]); |
| 477 |
} |
| 478 |
} |
| 479 |
free(filenames); |
| 480 |
|
| 481 |
if (imgindex) { |
| 482 |
USImages = new_image(); |
| 483 |
struct USImage *image = USImages; |
| 484 |
for (int i = 0; i < imgindex; i++) { |
| 485 |
const char *shortname = imagefiles[i]; |
| 486 |
di_rawname_from_name(rawname, shortname); |
| 487 |
ImageFile *c64file = di_open(d64, rawname, 0xc2, "rb"); |
| 488 |
if (c64file) { |
| 489 |
uint8_t buf[0xffff]; |
| 490 |
image->datasize = di_read(c64file, buf, 0xffff); |
| 491 |
image->imagedata = MemAlloc(image->datasize); |
| 492 |
memcpy(image->imagedata, buf, image->datasize); |
| 493 |
free(c64file); |
| 494 |
image->systype = SYS_C64; |
| 495 |
image->index = shortname[5] - '0' + (shortname[4] - '0') * 10 + (shortname[3] - '0') * 100; |
| 496 |
if (shortname[0] == 'R') { |
| 497 |
image->usage = IMG_ROOM; |
| 498 |
} else if (shortname[6] == 'R') { |
| 499 |
image->usage = IMG_ROOM_OBJ; |
| 500 |
} else if (shortname[6] == 'I') { |
| 501 |
image->usage = IMG_INV_OBJ; |
| 502 |
} |
| 503 |
|
| 504 |
image->next = new_image(); |
| 505 |
image = image->next; |
| 506 |
} |
| 507 |
free(imagefiles[i]); |
| 508 |
} |
| 509 |
|
| 510 |
if (USImages->imagedata == NULL && USImages->next == NULL) { |
| 511 |
free(USImages); |
| 512 |
USImages = NULL; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
GameIDType DetectC64(uint8_t **sf, size_t *extent) |
| 520 |
{ |
| 521 |
if (*extent > MAX_LENGTH || *extent < MIN_LENGTH) |
| 522 |
return UNKNOWN_GAME; |
| 523 |
|
| 524 |
uint16_t chksum = checksum(*sf, *extent); |
| 525 |
|
| 526 |
for (int i = 0; c64_registry[i].length != 0; i++) { |
| 527 |
if (*extent == c64_registry[i].length && chksum == c64_registry[i].chk) { |
| 528 |
if (c64_registry[i].id == SAVAGE_ISLAND_C64) { |
| 529 |
return savage_island_menu(sf, extent, i); |
| 530 |
} else if (c64_registry[i].id == BATON_C64 && (chksum == 0xc3fc || chksum == 0xbfbf)) { |
| 531 |
return mysterious_menu(sf, extent, i); |
| 532 |
} else if (c64_registry[i].id == FEASIBILITY_C64 && (chksum == 0x9eaa || chksum == 0x9c18)) { |
| 533 |
return mysterious_menu2(sf, extent, i); |
| 534 |
} |
| 535 |
if (c64_registry[i].type == TYPE_D64) { |
| 536 |
int newlength; |
| 537 |
uint8_t *largest_file = get_largest_file(*sf, *extent, &newlength); |
| 538 |
uint8_t *appendix = NULL; |
| 539 |
int appendixlen = 0; |
| 540 |
|
| 541 |
if (c64_registry[i].appendfile != NULL) { |
| 542 |
appendix = get_file_named(*sf, *extent, &appendixlen, |
| 543 |
c64_registry[i].appendfile); |
| 544 |
if (appendix == NULL) |
| 545 |
fprintf(stderr, "SCOTT: DetectC64() Appending file failed!\n"); |
| 546 |
appendixlen -= 2; |
| 547 |
} |
| 548 |
|
| 549 |
size_t buflen = newlength + appendixlen; |
| 550 |
if (buflen <= 0 || buflen > MAX_LENGTH) |
| 551 |
return UNKNOWN_GAME; |
| 552 |
|
| 553 |
uint8_t *megabuf = MemAlloc(buflen); |
| 554 |
memcpy(megabuf, largest_file, newlength); |
| 555 |
if (appendix != NULL) { |
| 556 |
memcpy(megabuf + newlength + c64_registry[i].parameter, appendix + 2, |
| 557 |
appendixlen); |
| 558 |
newlength = buflen; |
| 559 |
} |
| 560 |
|
| 561 |
if (largest_file) { |
| 562 |
free(*sf); |
| 563 |
*sf = MemAlloc(newlength); |
| 564 |
memcpy(*sf, megabuf, newlength); |
| 565 |
*extent = newlength; |
| 566 |
} |
| 567 |
free(megabuf); |
| 568 |
|
| 569 |
} else if (c64_registry[i].type == TYPE_T64) { |
| 570 |
uint8_t *file_records = *sf + 64; |
| 571 |
int number_of_records = (*sf)[36] + (*sf)[37] * 0x100; |
| 572 |
int offset = file_records[8] + file_records[9] * 0x100; |
| 573 |
int start_addr = file_records[2] + file_records[3] * 0x100; |
| 574 |
int end_addr = file_records[4] + file_records[5] * 0x100; |
| 575 |
int size; |
| 576 |
if (number_of_records == 1) |
| 577 |
size = *extent - offset; |
| 578 |
else |
| 579 |
size = end_addr - start_addr; |
| 580 |
uint8_t *first_file = MemAlloc(size + 2); |
| 581 |
memcpy(first_file + 2, *sf + offset, size); |
| 582 |
memcpy(first_file, file_records + 2, 2); |
| 583 |
free(*sf); |
| 584 |
*sf = first_file; |
| 585 |
*extent = size + 2; |
| 586 |
} else if (c64_registry[i].type == TYPE_US) { |
| 587 |
int newlength; |
| 588 |
uint8_t *database_file = get_file_named(*sf, *extent, &newlength, c64_registry[i].appendfile); |
| 589 |
if (database_file == NULL) { |
| 590 |
fprintf(stderr, "SCOTT: DetectC64() Could not find database in D64\n"); |
| 591 |
return UNKNOWN_GAME; |
| 592 |
} |
| 593 |
|
| 594 |
if (c64_registry[i].decompress_iterations) { |
| 595 |
size_t len = (size_t)newlength; |
| 596 |
DecrunchC64(&database_file, &len, c64_registry[i]); |
| 597 |
newlength = len; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
int cutoff = c64_registry[i].imgoffset; |
| 605 |
if (cutoff && cutoff < newlength) { |
| 606 |
newlength -= cutoff; |
| 607 |
uint8_t *shorter = MemAlloc(newlength); |
| 608 |
memcpy(shorter, database_file + cutoff, newlength); |
| 609 |
free(database_file); |
| 610 |
database_file = shorter; |
| 611 |
} |
| 612 |
|
| 613 |
int result = LoadBinaryDatabase(database_file, newlength, *Game, 0); |
| 614 |
if (result) { |
| 615 |
CurrentSys = SYS_C64; |
| 616 |
LoadC64USImages(*sf, *extent); |
| 617 |
free(*sf); |
| 618 |
*sf = MemAlloc(newlength); |
| 619 |
memcpy(*sf, database_file, newlength); |
| 620 |
*extent = newlength; |
| 621 |
return CurrentGame; |
| 622 |
} |
| 623 |
} |
| 624 |
return DecrunchC64(sf, extent, c64_registry[i]); |
| 625 |
} |
| 626 |
} |
| 627 |
return UNKNOWN_GAME; |
| 628 |
} |
| 629 |
|
| 630 |
static GameIDType DecrunchC64(uint8_t **sf, size_t *extent, struct c64rec record) |
| 631 |
{ |
| 632 |
size_t length = *extent; |
| 633 |
size_t decompressed_length = *extent; |
| 634 |
|
| 635 |
uint8_t *uncompressed = MemAlloc(0x10000); |
| 636 |
|
| 637 |
int result = 0; |
| 638 |
|
| 639 |
for (int i = 1; i <= record.decompress_iterations; i++) { |
| 640 |
|
| 641 |
if (i == record.parameter && record.switches != NULL) { |
| 642 |
result = unp64(*sf, length, uncompressed, |
| 643 |
&decompressed_length, record.switches); |
| 644 |
} else |
| 645 |
result = unp64(*sf, length, uncompressed, |
| 646 |
&decompressed_length, NULL); |
| 647 |
if (result) { |
| 648 |
if (*sf != NULL) |
| 649 |
free(*sf); |
| 650 |
*sf = MemAlloc(decompressed_length); |
| 651 |
memcpy(*sf, uncompressed, decompressed_length); |
| 652 |
length = decompressed_length; |
| 653 |
} else { |
| 654 |
free(uncompressed); |
| 655 |
uncompressed = NULL; |
| 656 |
break; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if (uncompressed != NULL) |
| 661 |
free(uncompressed); |
| 662 |
|
| 663 |
if (record.type == TYPE_US) { |
| 664 |
*extent = length; |
| 665 |
return record.id; |
| 666 |
} |
| 667 |
|
| 668 |
for (int i = 0; games[i].Title != NULL; i++) { |
| 669 |
if (games[i].gameID == record.id) { |
| 670 |
free(Game); |
| 671 |
Game = &games[i]; |
| 672 |
break; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
if (Game->Title == NULL) { |
| 677 |
Fatal("Game not found!"); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
if (entire_file != *sf && entire_file != NULL) { |
| 688 |
free(entire_file); |
| 689 |
entire_file = *sf; |
| 690 |
} |
| 691 |
file_length = length; |
| 692 |
|
| 693 |
size_t offset; |
| 694 |
|
| 695 |
DictionaryType dictype = GetId(&offset); |
| 696 |
if (dictype != Game->dictionary) { |
| 697 |
Fatal("Wrong game?"); |
| 698 |
} |
| 699 |
|
| 700 |
if (TryLoading(*Game, offset, 0) == UNKNOWN_GAME) { |
| 701 |
Fatal("Game could not be read!"); |
| 702 |
} |
| 703 |
|
| 704 |
if (save_island_appendix_1 != NULL) { |
| 705 |
appendSIfiles(sf, extent); |
| 706 |
} |
| 707 |
|
| 708 |
if (CurrentGame == GREMLINS_C64 && record.copysource == 0xc801) { |
| 709 |
uint8_t overlap[0x1000]; |
| 710 |
memcpy(overlap, *sf + 0xd801, 0x1000); |
| 711 |
result = CopyData(record.copydest, record.copysource, sf, *extent, record.copysize); |
| 712 |
*extent = result; |
| 713 |
memcpy(*sf + 0xc774, overlap, 0x1000); |
| 714 |
} else if (record.copysource != 0) { |
| 715 |
result = CopyData(record.copydest, record.copysource, sf, *extent, |
| 716 |
record.copysize); |
| 717 |
if (result) { |
| 718 |
*extent = result; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
if (CurrentGame == CLAYMORGUE_C64 && record.copysource == 0x855) { |
| 723 |
result = CopyData(0x1531a, 0x2002, sf, *extent, 0x2000); |
| 724 |
if (result) { |
| 725 |
*extent = result; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
if (!(Game->subtype & MYSTERIOUS)) |
| 730 |
SagaSetup(record.imgoffset); |
| 731 |
|
| 732 |
return CurrentGame; |
| 733 |
} |