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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
/* file.c */
/* Copyright Hugh Robinson 2006-2008.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include "asylum.h"
#define STOREAREALEN (16*0x40000)
char storage[STOREAREALEN];
#define storageend (storage+STOREAREALEN)
//const char idpermitpath[]="<PsychoResource$Path>Idpermit";
static char resource_path[240];
static char score_path[240];
char configname[] = "/.asylum"; //"<PsychoResource$Path>Config";
char savegamename[] = "/.asylum_game";
static const char* score_name[4] = {
"/EgoHighScores", "/PsycheHighScores", "/IdHighScores", "/ExtendedHighScores"
};
FILE* score_file[4];
FILE* find_game(int op)
{
char fullname[240] = "";
char* home = getenv("HOME");
if (home)
strcat(fullname, home);
else
return NULL;
strcat(fullname, savegamename);
switch (op)
{
case 0x40: return fopen(fullname, "rb");
case 0x80: return fopen(fullname, "wb");
case 0xc0: return fopen(fullname, "ab");
default: return NULL;
}
}
FILE* find_config(int op)
{
char fullname[240] = "";
char* home = getenv("HOME");
if (home)
strcat(fullname, home);
else
strcat(fullname, resource_path);
strcat(fullname, configname);
switch (op)
{
case 0x40: return fopen(fullname, "rb");
case 0x80: return fopen(fullname, "wb");
case 0xc0: return fopen(fullname, "ab");
default: return NULL;
}
}
void dropprivs()
{
#ifndef _WIN32
if (setregid(getgid(), getgid()) != 0)
{
fprintf(stderr, "setregid failed\n");
exit(1);
}
if (setreuid(getuid(), getuid()) != 0)
{
fprintf(stderr, "setreuid failed\n");
exit(1);
}
#endif
}
uint32_t read_littleendian(uint8_t* bytes)
{
return (*bytes)|(bytes[1]<<8)|(bytes[2]<<16)|(bytes[3]<<24);
}
uint32_t read_littleendian(uint32_t* word)
{
return read_littleendian((uint8_t*)word);
}
void write_littleendian(uint8_t* bytes, uint32_t word)
{
*bytes = word & 0xff;
bytes[1] = (word>>8) & 0xff;
bytes[2] = (word>>16) & 0xff;
bytes[3] = (word>>24) & 0xff;
}
int loadhammered_game(char** spaceptr, char* r1, char* path)
{
int reload;
do
{
reload = loadhammered(spaceptr, r1, path);
if (reload == -1)
{
badload(); return 0;
}
}
while (reload == 1);
return reload;
}
int loadhammered_level(char** spaceptr, char* r1, char* path)
{
int reload;
do
{
reload = loadhammered(spaceptr, r1, path);
if (reload == -1) reload = badlevelload();
}
while (reload == 1);
return reload;
}
int loadvitalfile(char** spaceptr, char* r1, char* path)
{
// if VS or if r0==1
char fullname[240] = "";
strcat(fullname, path);
strcat(fullname, r1);
int r4 = swi_osfile(15, fullname, 0, 0);
if (r4 <= 0) fatalfile();
*spaceptr = (char*)malloc(r4);
if (swi_osfile(14, fullname, *spaceptr, 0)) fatalfile();
return r4;
}
int loadhammered(char** spaceptr, char* r1, char* path)
{
int r3 = swi_blitz_hammerop(0, r1, path, NULL);
if (r3 == -1)
{
filenotthere(); return -1;
}
if (r3 == 0) return loadfile(spaceptr, r1, path);
*spaceptr = (char*)malloc(r3);
if (*spaceptr == NULL)
{
nomemory(); return 1;
}
int k;
if ((k = swi_blitz_hammerop(1, r1, path, *spaceptr)) < 0)
{
printf("Error %i\n", -k); filesyserror(); return 1;
}
return k;
}
int loadfile(char** spaceptr, char* r1, char* path)
{
int r4 = filelength(r1, path);
char fullname[240] = "";
// hack: +4 as feof doesn't trigger until we've passed the end
*spaceptr = (char*)malloc(r4+4);
strcat(fullname, path);
strcat(fullname, r1);
if (r4 == -1)
{
filenotthere(); return 1;
}
if (*spaceptr == NULL)
{
nomemory(); return 1;
}
if (swi_osfile(14, fullname, *spaceptr, 0))
{
filesyserror(); return 1;
}
return r4;
}
void set_paths()
{
#ifdef RESOURCEPATH
if (chdir(RESOURCEPATH) == 0)
{
strcpy(resource_path, RESOURCEPATH);
strcpy(score_path, SCOREPATH);
/* We could fall back to ~/.asylum/ if SCOREPATH is not writable.
However just assuming the current directory is ok is not cool. */
return;
}
#endif
fprintf(stderr, "Running as uninstalled, looking for files in local directory.\n");
#ifdef HAVE_GET_EXE_PATH
char exe_path[240];
if (get_exe_path(exe_path, sizeof(exe_path)))
{
strcpy(resource_path, exe_path);
strncat(resource_path, "/data");
strcpy(score_path, exe_path);
strcat(score_path, "/hiscores");
return;
}
#endif
strcpy(resource_path, "data");
strcpy(score_path, "../hiscores"); /* relative to resource_path */
}
void open_scores()
{
char filename[PATH_MAX];
for (int i = 0; i < 4; ++i)
{
strcpy(filename, score_path);
strcat(filename, score_name[i]);
score_file[i] = fopen(filename, "r+b");
if (score_file[i] == NULL)
{
// Perhaps the file didn't exist yet
score_file[i] = fopen(filename, "w+b");
if (score_file[i] == NULL)
{
// Perhaps we don't have write permissions :(
score_file[i] = fopen(filename, "rb");
if (score_file[i] == NULL)
fprintf(stderr, "Couldn't open %s, check if the directory exists\n", filename);
else
fprintf(stderr, "Opening %s read-only, high scores will not be saved\n", filename);
}
}
}
}
void find_resources()
{
set_paths();
if (chdir(resource_path) != 0)
{
fprintf(stderr, "Couldn't find resources directory %s\n", resource_path);
exit(1);
}
}
void savescores(char* highscorearea, int mentalzone)
{
highscorearea[13*5] = swi_oscrc(0, highscorearea, highscorearea+13*5, 1);
if (mentalzone >= 1 && mentalzone <= 4 && score_file[mentalzone - 1] != NULL)
{
FILE * f = score_file[mentalzone - 1];
fseek(f, 0, SEEK_SET);
fwrite(highscorearea, 1, 13*5+1, f);
fflush(f);
}
}
void loadscores(char* highscorearea, int mentalzone)
{
if (mentalzone >= 1 && mentalzone <= 4 && score_file[mentalzone - 1] != NULL)
{
FILE * f = score_file[mentalzone - 1];
fseek(f, 0, SEEK_SET);
if (fread(highscorearea, 1, 13*5+1, f) == 13*5+1 &&
swi_oscrc(0, highscorearea, highscorearea+13*5, 1) == highscorearea[13*5])
{
return;
}
}
setdefaultscores();
}
int filelength(char* name, char* path)
{
char fullname[240] = "";
strcat(fullname, path);
strcat(fullname, name);
int r4 = swi_osfile(15, fullname, NULL, NULL);
if (r4 == -1)
{
filesyserror(); return 0;
}
//if (r0!=1) {fileerror: return -1;}
return r4;
}
void swi_osgbpb(int op, FILE* f, char* start, char* end, int b)
{
switch (op)
{
case 3: for (char* i = start; (i < end) && !feof(f); i++) *i = fgetc(f);
break;
case 1: for (char* i = start; (i < end) && !feof(f); i++) fputc(*i, f);
break;
}
}
int swi_osfile(int op, const char* name, char* start, char* end)
{
FILE* f;
int x;
//printf("Looking for %s\n",name);
switch (op)
{
case 10: // save file
f = fopen(name, "wb");
for (char* i = start; i < end; i++) fputc(*i, f);
fclose(f);
return 0;
case 5: // test file existence
f = fopen(name, "rb");
if (f == NULL) return 0;
fclose(f);
return 1;
case 15: // file length
f = fopen(name, "rb");
if (f == NULL) return -1;
fseek(f, 0, SEEK_END);
x = ftell(f);
fclose(f);
return x;
case 0xff:
case 14: // load file
f = fopen(name, "rb");
if (f == NULL) return -1;
while ((x = fgetc(f)) != EOF) *start++ = x;
fclose(f);
return 0;
}
}
int swi_blitz_hammerop(int op, char* name, char* path, char* space)
{
char fullname[240] = "";
strcat(fullname, path);
strcat(fullname, name);
FILE* f = fopen(fullname, "rb");
if (f == NULL) return -1; // file does not exist
if ((getc(f) != 'H') || (getc(f) != 'm') || (getc(f) != 'r'))
{
fclose(f); return op;
} // file is not Hammered
if (op == 0)
{
fclose(f); return 0x24000; // hack: should return length
}
char a[524288];
int p = 0;
char c;
int fmt = getc(f);
int len = 0;
for (int i = 0; i < 4; i++) len = ((len>>8)&0xffffff)|((getc(f)<<24)&0xff000000);
while (!feof(f))
{
c = getc(f);
if (c == 0xff) break; // end flag
else if (c < 0x10)
{
// type 1
int n = (c == 15) ? 256 : c+2;
char d = getc(f);
for (int i = 0; i < n; i++) a[p++] = d;
}
else if (c < 0x20)
{
// type 2
int n = (c&0xf)+1;
for (int i = 0; i < n; i++) a[p++] = getc(f);
}
else if (c < 0x40)
{
// type 3
char d = getc(f);
int P = p-1-((c&3)<<8)-d;
if (P < 0)
{
fclose(f); return -3;
}
int n = ((c&0x1c)>>2)+2;
for (int i = 0; i < n; i++) a[p++] = a[P--];
}
else if (c < 0x80)
{
// type 4
char d = getc(f);
int D = 1<<(((c&4) ? ((c&6)+2) : (c&6))>>1);
int P = p-1-((c&1)<<8)-d;
if (P < 0)
{
fclose(f); return -4;
}
int n = ((c&0x38)>>3)+2;
for (int i = 0; i < n; i++) a[p++] = a[P+(i*D)/4];
}
else
{
// type 5
char d = getc(f);
int P = p-1-((c&7)<<8)-d;
if (P < 0)
{
fclose(f); return -5;
}
int n = ((c&0x78)>>3)+2;
for (int i = 0; i < n; i++) a[p++] = a[P++];
}
}
if (fmt != 0)
{
int wlen = (len+3)>>2;
for (int i = 0; i < p; i++)
space[i] = a[(i%4)*wlen+(i>>2)];
}
else
for (int i = 0; i < p; i++)
space[i] = a[i];
fclose(f);
return p;
}
|