| 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 |
#include "files.h" |
| 50 |
|
| 51 |
#include <SDL.h> |
| 52 |
#include <errno.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <string.h> |
| 57 |
#include <sys/stat.h> |
| 58 |
#include <sys/types.h> |
| 59 |
|
| 60 |
#include "door.h" |
| 61 |
#include "log.h" |
| 62 |
#include "map_new.h" |
| 63 |
#include "palette.h" |
| 64 |
#include "sys_specifics.h" |
| 65 |
#include "utils.h" |
| 66 |
|
| 67 |
#define MAX_STRING_LEN 1000 |
| 68 |
|
| 69 |
#define CAMPAIGN_MAGIC 690304 |
| 70 |
#define CAMPAIGN_VERSION 6 |
| 71 |
|
| 72 |
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
| 73 |
static const int bendian = 1; |
| 74 |
#else |
| 75 |
static const int bendian = 0; |
| 76 |
#endif |
| 77 |
|
| 78 |
void swap32(void *d) |
| 79 |
{ |
| 80 |
if (bendian) |
| 81 |
{ |
| 82 |
*((int32_t *)d) = SDL_Swap32(*((int32_t *)d)); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
size_t f_read(FILE *f, void *buf, size_t size) |
| 87 |
{ |
| 88 |
return fread(buf, size, 1, f); |
| 89 |
} |
| 90 |
|
| 91 |
size_t f_read32(FILE *f, void *buf, size_t size) |
| 92 |
{ |
| 93 |
size_t ret = 0; |
| 94 |
if (buf) |
| 95 |
{ |
| 96 |
ret = f_read(f, buf, size); |
| 97 |
swap32((int *)buf); |
| 98 |
} |
| 99 |
return ret; |
| 100 |
} |
| 101 |
|
| 102 |
void swap16(void *d) |
| 103 |
{ |
| 104 |
if (bendian) |
| 105 |
{ |
| 106 |
*((int16_t *)d) = SDL_Swap16(*((int16_t *)d)); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
size_t f_read16(FILE *f, void *buf, size_t size) |
| 111 |
{ |
| 112 |
size_t ret = 0; |
| 113 |
if (buf) |
| 114 |
{ |
| 115 |
ret = f_read(f, buf, size); |
| 116 |
swap16((int16_t *)buf); |
| 117 |
} |
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
int fwrite32(FILE *f, void *buf) |
| 122 |
{ |
| 123 |
size_t ret = fwrite(buf, 4, 1, f); |
| 124 |
if (ret != 1) |
| 125 |
{ |
| 126 |
return 0; |
| 127 |
} |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
int IsCampaignOldFile(const char *filename) |
| 132 |
{ |
| 133 |
int32_t i; |
| 134 |
FILE *f = fopen(filename, "rb"); |
| 135 |
if (f == NULL) |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
f_read32(f, &i, sizeof i); |
| 140 |
fclose(f); |
| 141 |
return i == CAMPAIGN_MAGIC; |
| 142 |
} |
| 143 |
|
| 144 |
int ScanCampaignOld(const char *filename, char **title, int *missions) |
| 145 |
{ |
| 146 |
FILE *f; |
| 147 |
int i; |
| 148 |
CampaignSettingOld setting; |
| 149 |
|
| 150 |
f = fopen(filename, "rb"); |
| 151 |
if (f != NULL) |
| 152 |
{ |
| 153 |
f_read32(f, &i, sizeof(i)); |
| 154 |
|
| 155 |
if (i != CAMPAIGN_MAGIC) |
| 156 |
{ |
| 157 |
fclose(f); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
f_read32(f, &i, sizeof(i)); |
| 162 |
if (i != CAMPAIGN_VERSION) |
| 163 |
{ |
| 164 |
fclose(f); |
| 165 |
return -1; |
| 166 |
} |
| 167 |
|
| 168 |
f_read(f, setting.title, sizeof(setting.title)); |
| 169 |
f_read(f, setting.author, sizeof(setting.author)); |
| 170 |
f_read(f, setting.description, sizeof(setting.description)); |
| 171 |
f_read32(f, &setting.missionCount, sizeof(setting.missionCount)); |
| 172 |
if (title) |
| 173 |
{ |
| 174 |
CSTRDUP(*title, setting.title); |
| 175 |
} |
| 176 |
if (missions) |
| 177 |
{ |
| 178 |
*missions = setting.missionCount; |
| 179 |
} |
| 180 |
|
| 181 |
fclose(f); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
perror("ScanCampaignOld - couldn't read file:"); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
#define R32(v) \ |
| 190 |
{ \ |
| 191 |
int32_t _n; \ |
| 192 |
f_read32(f, &_n, sizeof _n); \ |
| 193 |
(v) = _n; \ |
| 194 |
} |
| 195 |
|
| 196 |
static void load_mission_objective(FILE *f, struct MissionObjectiveOld *o) |
| 197 |
{ |
| 198 |
f_read(f, o->description, sizeof(o->description)); |
| 199 |
f_read32(f, &o->type, sizeof(o->type)); |
| 200 |
f_read32(f, &o->index, sizeof(o->index)); |
| 201 |
f_read32(f, &o->count, sizeof(o->count)); |
| 202 |
f_read32(f, &o->required, sizeof(o->required)); |
| 203 |
f_read32(f, &o->flags, sizeof(o->flags)); |
| 204 |
} |
| 205 |
|
| 206 |
static void load_mission(FILE *f, struct MissionOld *m) |
| 207 |
{ |
| 208 |
int i; |
| 209 |
|
| 210 |
f_read(f, m->title, sizeof(m->title)); |
| 211 |
f_read(f, m->description, sizeof(m->description)); |
| 212 |
|
| 213 |
R32(m->wallStyle); |
| 214 |
R32(m->floorStyle); |
| 215 |
R32(m->roomStyle); |
| 216 |
R32(m->exitStyle); |
| 217 |
R32(m->keyStyle); |
| 218 |
R32(m->doorStyle); |
| 219 |
|
| 220 |
R32(m->mapWidth); |
| 221 |
R32(m->mapHeight); |
| 222 |
R32(m->wallCount); |
| 223 |
R32(m->wallLength); |
| 224 |
R32(m->roomCount); |
| 225 |
R32(m->squareCount); |
| 226 |
|
| 227 |
R32(m->exitLeft); |
| 228 |
R32(m->exitTop); |
| 229 |
R32(m->exitRight); |
| 230 |
R32(m->exitBottom); |
| 231 |
|
| 232 |
R32(m->objectiveCount); |
| 233 |
|
| 234 |
for (i = 0; i < OBJECTIVE_MAX; i++) |
| 235 |
{ |
| 236 |
load_mission_objective(f, &m->objectives[i]); |
| 237 |
} |
| 238 |
|
| 239 |
R32(m->baddieCount); |
| 240 |
for (i = 0; i < BADDIE_MAX; i++) |
| 241 |
{ |
| 242 |
f_read32(f, &m->baddies[i], sizeof(int)); |
| 243 |
} |
| 244 |
|
| 245 |
R32(m->specialCount); |
| 246 |
for (i = 0; i < SPECIAL_MAX; i++) |
| 247 |
{ |
| 248 |
f_read32(f, &m->specials[i], sizeof(int)); |
| 249 |
} |
| 250 |
|
| 251 |
R32(m->itemCount); |
| 252 |
for (i = 0; i < ITEMS_MAX; i++) |
| 253 |
{ |
| 254 |
f_read32(f, &m->items[i], sizeof(int)); |
| 255 |
} |
| 256 |
for (i = 0; i < ITEMS_MAX; i++) |
| 257 |
{ |
| 258 |
f_read32(f, &m->itemDensity[i], sizeof(int)); |
| 259 |
} |
| 260 |
|
| 261 |
R32(m->baddieDensity); |
| 262 |
R32(m->weaponSelection); |
| 263 |
|
| 264 |
f_read(f, m->song, sizeof(m->song)); |
| 265 |
f_read(f, m->map, sizeof(m->map)); |
| 266 |
|
| 267 |
R32(m->wallRange); |
| 268 |
R32(m->floorRange); |
| 269 |
R32(m->roomRange); |
| 270 |
R32(m->altRange); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static unsigned char cShadePalettes[] = {56, 6, 72, 88, 104, 120, 136, |
| 276 |
36, 40, 45, 148, 8, 1, 20}; |
| 277 |
void ConvertCharacterColors( |
| 278 |
const int skin, const int arm, const int body, const int leg, |
| 279 |
const int hair, CharColors *c) |
| 280 |
{ |
| 281 |
|
| 282 |
const HSV tint = {-1.0, 1.0, 1.7}; |
| 283 |
c->Skin = ColorTint(PaletteToColor(cShadePalettes[skin]), tint); |
| 284 |
c->Arms = ColorTint(PaletteToColor(cShadePalettes[arm]), tint); |
| 285 |
c->Body = ColorTint(PaletteToColor(cShadePalettes[body]), tint); |
| 286 |
c->Legs = ColorTint(PaletteToColor(cShadePalettes[leg]), tint); |
| 287 |
c->Hair = ColorTint(PaletteToColor(cShadePalettes[hair]), tint); |
| 288 |
c->Feet = c->Legs; |
| 289 |
c->Facehair = c->Hat = c->Glasses = c->Hair; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
void ConvertHairColors(Character *ch, const char *face) |
| 296 |
{ |
| 297 |
const color_t darkRed = {0xC0, 0, 0, 0xFF}; |
| 298 |
if (strcmp(face, "Cyborg") == 0) |
| 299 |
{ |
| 300 |
|
| 301 |
ch->Colors.Hair = colorRed; |
| 302 |
} |
| 303 |
else if (strcmp(face, "Ice") == 0) |
| 304 |
{ |
| 305 |
|
| 306 |
ch->Colors.Hair = colorBlack; |
| 307 |
} |
| 308 |
else if (strcmp(face, "Ogre") == 0) |
| 309 |
{ |
| 310 |
|
| 311 |
ch->Colors.Hair = darkRed; |
| 312 |
} |
| 313 |
else if (strcmp(face, "Snake") == 0) |
| 314 |
{ |
| 315 |
|
| 316 |
ch->Colors.Hair = colorBlack; |
| 317 |
} |
| 318 |
else if (strcmp(face, "WarBaby") == 0) |
| 319 |
{ |
| 320 |
|
| 321 |
ch->Colors.Hair = colorRed; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
void load_character(FILE *f, TBadGuy *b) |
| 326 |
{ |
| 327 |
R32(b->armedBodyPic); |
| 328 |
R32(b->unarmedBodyPic); |
| 329 |
R32(b->facePic); |
| 330 |
R32(b->speed); |
| 331 |
R32(b->probabilityToMove); |
| 332 |
R32(b->probabilityToTrack); |
| 333 |
R32(b->probabilityToShoot); |
| 334 |
R32(b->actionDelay); |
| 335 |
R32(b->gun); |
| 336 |
R32(b->skinColor); |
| 337 |
R32(b->armColor); |
| 338 |
R32(b->bodyColor); |
| 339 |
R32(b->legColor); |
| 340 |
R32(b->hairColor); |
| 341 |
R32(b->health); |
| 342 |
R32(b->flags); |
| 343 |
} |
| 344 |
void ConvertCharacter(Character *c, TBadGuy *b) |
| 345 |
{ |
| 346 |
const char *face = IntCharacterFace(b->facePic); |
| 347 |
char *newFace = NULL; |
| 348 |
CharacterOldFaceToHeadParts(face, &newFace, c->HeadParts); |
| 349 |
c->Class = StrCharacterClass(newFace); |
| 350 |
CFREE(newFace); |
| 351 |
c->speed = b->speed / 256.0f; |
| 352 |
c->bot->probabilityToMove = b->probabilityToMove; |
| 353 |
c->bot->probabilityToTrack = b->probabilityToTrack; |
| 354 |
c->bot->probabilityToShoot = b->probabilityToShoot; |
| 355 |
c->bot->actionDelay = b->actionDelay; |
| 356 |
c->Gun = CArrayGet(&gWeaponClasses.Guns, b->gun); |
| 357 |
ConvertCharacterColors( |
| 358 |
b->skinColor, b->armColor, b->bodyColor, b->legColor, b->hairColor, |
| 359 |
&c->Colors); |
| 360 |
ConvertHairColors(c, face); |
| 361 |
c->maxHealth = b->health; |
| 362 |
c->excessHealth = c->maxHealth * 2; |
| 363 |
c->flags = b->flags; |
| 364 |
} |
| 365 |
static void ConvertObjective(Objective *dest, struct MissionObjectiveOld *src) |
| 366 |
{ |
| 367 |
CSTRDUP(dest->Description, src->description); |
| 368 |
dest->Type = src->type; |
| 369 |
|
| 370 |
dest->color = ObjectiveTypeColor(dest->Type); |
| 371 |
|
| 372 |
|
| 373 |
switch (dest->Type) |
| 374 |
{ |
| 375 |
case OBJECTIVE_COLLECT: |
| 376 |
ObjectiveSetPickup(dest, IntPickupClass(src->index)); |
| 377 |
break; |
| 378 |
case OBJECTIVE_DESTROY: |
| 379 |
ObjectiveSetDestroy(dest, IntMapObject(src->index)); |
| 380 |
break; |
| 381 |
default: |
| 382 |
dest->u.Index = src->index; |
| 383 |
break; |
| 384 |
} |
| 385 |
dest->Count = src->count; |
| 386 |
dest->Required = src->required; |
| 387 |
if (dest->Required < 0) |
| 388 |
{ |
| 389 |
dest->Required = 0; |
| 390 |
} |
| 391 |
dest->Flags = src->flags; |
| 392 |
} |
| 393 |
static void ConvertMission( |
| 394 |
Mission *dest, struct MissionOld *src, const int charCount) |
| 395 |
{ |
| 396 |
CFREE(dest->Title); |
| 397 |
CSTRDUP(dest->Title, src->title); |
| 398 |
CFREE(dest->Description); |
| 399 |
CSTRDUP(dest->Description, src->description); |
| 400 |
dest->Type = MAPTYPE_CLASSIC; |
| 401 |
dest->Size = svec2i(src->mapWidth, src->mapHeight); |
| 402 |
strcpy(dest->ExitStyle, IntExitStyle(src->exitStyle)); |
| 403 |
strcpy(dest->KeyStyle, IntKeyStyle(src->keyStyle)); |
| 404 |
for (int i = 0; i < src->objectiveCount; i++) |
| 405 |
{ |
| 406 |
Objective o; |
| 407 |
memset(&o, 0, sizeof o); |
| 408 |
ConvertObjective(&o, &src->objectives[i]); |
| 409 |
CArrayPushBack(&dest->Objectives, &o); |
| 410 |
} |
| 411 |
|
| 412 |
for (int i = 0; i < src->baddieCount; i++) |
| 413 |
{ |
| 414 |
int n = src->baddies[i] % charCount; |
| 415 |
CArrayPushBack(&dest->Enemies, &n); |
| 416 |
} |
| 417 |
for (int i = 0; i < src->specialCount; i++) |
| 418 |
{ |
| 419 |
int n = src->specials[i] % charCount; |
| 420 |
CArrayPushBack(&dest->SpecialChars, &n); |
| 421 |
} |
| 422 |
for (int i = 0; i < src->itemCount; i++) |
| 423 |
{ |
| 424 |
MapObjectDensity mod; |
| 425 |
mod.M = IntMapObject(src->items[i]); |
| 426 |
mod.Density = src->itemDensity[i]; |
| 427 |
CArrayPushBack(&dest->MapObjectDensities, &mod); |
| 428 |
} |
| 429 |
dest->EnemyDensity = src->baddieDensity; |
| 430 |
CArrayClear(&dest->Weapons); |
| 431 |
for (int i = 0; i < WEAPON_MAX; i++) |
| 432 |
{ |
| 433 |
if ((src->weaponSelection & (1 << i)) || !src->weaponSelection) |
| 434 |
{ |
| 435 |
WeaponClass *wc = CArrayGet(&gWeaponClasses.Guns, i); |
| 436 |
CArrayPushBack(&dest->Weapons, &wc); |
| 437 |
} |
| 438 |
} |
| 439 |
if (strlen(src->song) > 0) |
| 440 |
{ |
| 441 |
dest->Music.Type = MUSIC_SRC_DYNAMIC; |
| 442 |
CSTRDUP(dest->Music.Data.Filename, src->song); |
| 443 |
} |
| 444 |
const color_t maskAlt = |
| 445 |
RangeToColor(abs(src->altRange) % COLORRANGE_COUNT); |
| 446 |
|
| 447 |
TileClassInit( |
| 448 |
&dest->u.Classic.TileClasses.Wall, &gPicManager, &gTileWall, |
| 449 |
IntWallStyle(src->wallStyle), TileClassBaseStyleType(TILE_CLASS_WALL), |
| 450 |
RangeToColor(abs(src->wallRange) % COLORRANGE_COUNT), maskAlt); |
| 451 |
TileClassInit( |
| 452 |
&dest->u.Classic.TileClasses.Floor, &gPicManager, &gTileFloor, |
| 453 |
IntFloorStyle(src->floorStyle), |
| 454 |
TileClassBaseStyleType(TILE_CLASS_FLOOR), |
| 455 |
RangeToColor(abs(src->floorRange) % COLORRANGE_COUNT), maskAlt); |
| 456 |
TileClassInit( |
| 457 |
&dest->u.Classic.TileClasses.Room, &gPicManager, &gTileRoom, |
| 458 |
IntRoomStyle(src->roomStyle), TileClassBaseStyleType(TILE_CLASS_FLOOR), |
| 459 |
RangeToColor(abs(src->roomRange) % COLORRANGE_COUNT), maskAlt); |
| 460 |
TileClassInit( |
| 461 |
&dest->u.Classic.TileClasses.Door, &gPicManager, &gTileDoor, |
| 462 |
IntDoorStyle(src->doorStyle), TileClassBaseStyleType(TILE_CLASS_DOOR), |
| 463 |
colorWhite, colorWhite); |
| 464 |
dest->u.Classic.Walls = src->wallCount; |
| 465 |
dest->u.Classic.WallLength = src->wallLength; |
| 466 |
dest->u.Classic.CorridorWidth = 1; |
| 467 |
dest->u.Classic.Rooms.Count = src->roomCount; |
| 468 |
dest->u.Classic.Rooms.Min = 6; |
| 469 |
dest->u.Classic.Rooms.Max = 10; |
| 470 |
dest->u.Classic.Rooms.Edge = 0; |
| 471 |
dest->u.Classic.Rooms.Walls = 0; |
| 472 |
dest->u.Classic.Squares = src->squareCount; |
| 473 |
dest->u.Classic.Doors.Enabled = 1; |
| 474 |
dest->u.Classic.Doors.Min = dest->u.Classic.Doors.Max = 1; |
| 475 |
dest->u.Classic.Pillars.Count = 0; |
| 476 |
} |
| 477 |
|
| 478 |
void ConvertCampaignSetting(CampaignSetting *dest, CampaignSettingOld *src) |
| 479 |
{ |
| 480 |
int i; |
| 481 |
CFREE(dest->Title); |
| 482 |
CSTRDUP(dest->Title, src->title); |
| 483 |
CFREE(dest->Author); |
| 484 |
CSTRDUP(dest->Author, src->author); |
| 485 |
CFREE(dest->Description); |
| 486 |
CSTRDUP(dest->Description, src->description); |
| 487 |
for (i = 0; i < src->missionCount; i++) |
| 488 |
{ |
| 489 |
Mission m; |
| 490 |
MissionInit(&m); |
| 491 |
ConvertMission(&m, &src->missions[i], src->characterCount); |
| 492 |
CArrayPushBack(&dest->Missions, &m); |
| 493 |
} |
| 494 |
CharacterStoreTerminate(&dest->characters); |
| 495 |
CharacterStoreInit(&dest->characters); |
| 496 |
for (i = 0; i < src->characterCount; i++) |
| 497 |
{ |
| 498 |
Character *ch = CharacterStoreAddOther(&dest->characters); |
| 499 |
ConvertCharacter(ch, &src->characters[i]); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
int LoadCampaignOld(const char *filename, CampaignSettingOld *setting) |
| 504 |
{ |
| 505 |
FILE *f = NULL; |
| 506 |
int32_t i; |
| 507 |
int err = 0; |
| 508 |
|
| 509 |
f = fopen(filename, "rb"); |
| 510 |
if (f == NULL) |
| 511 |
{ |
| 512 |
err = -1; |
| 513 |
goto bail; |
| 514 |
} |
| 515 |
|
| 516 |
f_read32(f, &i, sizeof(i)); |
| 517 |
if (i != CAMPAIGN_MAGIC) |
| 518 |
{ |
| 519 |
err = -1; |
| 520 |
goto bail; |
| 521 |
} |
| 522 |
|
| 523 |
f_read32(f, &i, sizeof(i)); |
| 524 |
if (i != CAMPAIGN_VERSION) |
| 525 |
{ |
| 526 |
err = -1; |
| 527 |
goto bail; |
| 528 |
} |
| 529 |
|
| 530 |
f_read(f, setting->title, sizeof(setting->title)); |
| 531 |
f_read(f, setting->author, sizeof(setting->author)); |
| 532 |
f_read(f, setting->description, sizeof(setting->description)); |
| 533 |
|
| 534 |
f_read32(f, &setting->missionCount, sizeof(int32_t)); |
| 535 |
CCALLOC( |
| 536 |
setting->missions, setting->missionCount * sizeof *setting->missions); |
| 537 |
for (i = 0; i < setting->missionCount; i++) |
| 538 |
{ |
| 539 |
load_mission(f, &setting->missions[i]); |
| 540 |
} |
| 541 |
|
| 542 |
f_read32(f, &setting->characterCount, sizeof(int32_t)); |
| 543 |
CCALLOC( |
| 544 |
setting->characters, |
| 545 |
setting->characterCount * sizeof *setting->characters); |
| 546 |
for (i = 0; i < setting->characterCount; i++) |
| 547 |
{ |
| 548 |
load_character(f, &setting->characters[i]); |
| 549 |
} |
| 550 |
|
| 551 |
bail: |
| 552 |
if (f != NULL) |
| 553 |
{ |
| 554 |
fclose(f); |
| 555 |
} |
| 556 |
return err; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
char *cdogs_homepath = NULL; |
| 567 |
const char *GetHomeDirectory(void) |
| 568 |
{ |
| 569 |
#ifdef __EMSCRIPTEN__ |
| 570 |
return "/persistent_data/"; |
| 571 |
#endif |
| 572 |
|
| 573 |
const char *p; |
| 574 |
|
| 575 |
if (cdogs_homepath != NULL) |
| 576 |
{ |
| 577 |
return cdogs_homepath; |
| 578 |
} |
| 579 |
#ifdef _WIN32 |
| 580 |
#define SEP '\\' |
| 581 |
#else |
| 582 |
#define SEP '/' |
| 583 |
#endif |
| 584 |
p = getenv("CDOGS_CONFIG_DIR"); |
| 585 |
if (p != NULL && strlen(p) != 0) |
| 586 |
{ |
| 587 |
CCALLOC(cdogs_homepath, strlen(p) + 2); |
| 588 |
strcpy(cdogs_homepath, p); |
| 589 |
cdogs_homepath[strlen(p)] = SEP; |
| 590 |
return cdogs_homepath; |
| 591 |
} |
| 592 |
|
| 593 |
p = getenv(HOME_DIR_ENV); |
| 594 |
if (p != NULL && strlen(p) != 0) |
| 595 |
{ |
| 596 |
CCALLOC(cdogs_homepath, strlen(p) + 2); |
| 597 |
strcpy(cdogs_homepath, p); |
| 598 |
cdogs_homepath[strlen(p)] = SEP; |
| 599 |
return cdogs_homepath; |
| 600 |
} |
| 601 |
#undef SEP |
| 602 |
|
| 603 |
fprintf( |
| 604 |
stderr, "%s%s%s%s", |
| 605 |
"##############################################################\n", |
| 606 |
"# You don't have the environment variables HOME or USER set. #\n", |
| 607 |
"# It is suggested you get a better shell. :D #\n", |
| 608 |
"##############################################################\n"); |
| 609 |
return ""; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
char cfpath[CDOGS_PATH_MAX]; |
| 617 |
const char *GetConfigFilePath(const char *name) |
| 618 |
{ |
| 619 |
const char *xdgConfigDir = getenv("XDG_CONFIG_HOME"); |
| 620 |
if (xdgConfigDir != NULL) |
| 621 |
{ |
| 622 |
sprintf(cfpath, "%s/cdogs-sdl/", xdgConfigDir); |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
const char *homedir = GetHomeDirectory(); |
| 627 |
strcpy(cfpath, homedir); |
| 628 |
#ifndef __EMSCRIPTEN__ |
| 629 |
strcat(cfpath, CDOGS_CFG_DIR); |
| 630 |
#endif |
| 631 |
} |
| 632 |
strcat(cfpath, name); |
| 633 |
|
| 634 |
return cfpath; |
| 635 |
} |
| 636 |
|
| 637 |
static bool doMkdir(const char *path) |
| 638 |
{ |
| 639 |
if (mkdir(path, MKDIR_MODE) != -1) |
| 640 |
{ |
| 641 |
return true; |
| 642 |
} |
| 643 |
int e = errno; |
| 644 |
(void)e; |
| 645 |
|
| 646 |
|
| 647 |
return errno == EEXIST || errno == EISDIR || errno == EACCES; |
| 648 |
} |
| 649 |
bool mkdir_deep(const char *path) |
| 650 |
{ |
| 651 |
for (int i = 0; i < (int)strlen(path); i++) |
| 652 |
{ |
| 653 |
if (path[i] == '\0') |
| 654 |
break; |
| 655 |
if (path[i] == '/') |
| 656 |
{ |
| 657 |
char buf[CDOGS_PATH_MAX]; |
| 658 |
strncpy(buf, path, i + 1); |
| 659 |
buf[i + 1] = '\0'; |
| 660 |
if (!doMkdir(buf)) |
| 661 |
{ |
| 662 |
return false; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
if (path[strlen(path) - 1] != '/' && !doMkdir(path)) |
| 667 |
{ |
| 668 |
return false; |
| 669 |
} |
| 670 |
return true; |
| 671 |
} |
| 672 |
|
| 673 |
void SetupConfigDir(void) |
| 674 |
{ |
| 675 |
const char *cfg_p = GetConfigFilePath(""); |
| 676 |
|
| 677 |
LOG(LM_MAIN, LL_INFO, "Creating config dir %s... ", cfg_p); |
| 678 |
|
| 679 |
if (mkdir_deep(cfg_p)) |
| 680 |
{ |
| 681 |
if (errno != EEXIST) |
| 682 |
LOG(LM_MAIN, LL_INFO, "Config dir created."); |
| 683 |
else |
| 684 |
LOG(LM_MAIN, LL_INFO, "Config dir already exists."); |
| 685 |
} |
| 686 |
else |
| 687 |
{ |
| 688 |
switch (errno) |
| 689 |
{ |
| 690 |
case EACCES: |
| 691 |
LOG(LM_MAIN, LL_WARN, "Permission denied"); |
| 692 |
break; |
| 693 |
default: |
| 694 |
perror("Error creating config directory:"); |
| 695 |
break; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
color_t RangeToColor(const int range) |
| 703 |
{ |
| 704 |
switch (range) |
| 705 |
{ |
| 706 |
case 0: |
| 707 |
return colorMaroon; |
| 708 |
case 1: |
| 709 |
return colorLonestar; |
| 710 |
case 2: |
| 711 |
return colorRusticRed; |
| 712 |
case 3: |
| 713 |
return colorOfficeGreen; |
| 714 |
case 4: |
| 715 |
return colorPakistanGreen; |
| 716 |
case 5: |
| 717 |
return colorDarkFern; |
| 718 |
case 6: |
| 719 |
return colorNavyBlue; |
| 720 |
case 7: |
| 721 |
return colorArapawa; |
| 722 |
case 8: |
| 723 |
return colorStratos; |
| 724 |
case 9: |
| 725 |
return colorPatriarch; |
| 726 |
case 10: |
| 727 |
return colorPompadour; |
| 728 |
case 11: |
| 729 |
return colorLoulou; |
| 730 |
case 12: |
| 731 |
return colorBattleshipGrey; |
| 732 |
case 13: |
| 733 |
return colorDoveGray; |
| 734 |
case 14: |
| 735 |
return colorGravel; |
| 736 |
case 15: |
| 737 |
return colorComet; |
| 738 |
case 16: |
| 739 |
return colorFiord; |
| 740 |
case 17: |
| 741 |
return colorTuna; |
| 742 |
case 18: |
| 743 |
return colorHacienda; |
| 744 |
case 19: |
| 745 |
return colorKumera; |
| 746 |
case 20: |
| 747 |
return colorHimalaya; |
| 748 |
case 21: |
| 749 |
return colorChocolate; |
| 750 |
case 22: |
| 751 |
return colorNutmeg; |
| 752 |
case 23: |
| 753 |
return colorBracken; |
| 754 |
case 24: |
| 755 |
return colorTeal; |
| 756 |
case 25: |
| 757 |
return colorSkobeloff; |
| 758 |
case 26: |
| 759 |
return colorDeepJungleGreen; |
| 760 |
default: |
| 761 |
CASSERT(false, "unknown colour range"); |
| 762 |
return colorBlack; |
| 763 |
}; |
| 764 |
} |