+static void InitLevelSetupDirectory(char *level_subdir)
+{
+ createDirectory(getUserDataDir(), "user data");
+ createDirectory(getLevelSetupDir(""), "main level setup");
+ createDirectory(getLevelSetupDir(level_subdir), "level setup");
+}
+
+static void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fgetc(file);
+}
+
+static void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fputc(0, file);
+}
+
+static void ReadChunk_VERS(FILE *file, int *file_version, int *game_version)
+{
+ int file_version_major, file_version_minor, file_version_patch;
+ int game_version_major, game_version_minor, game_version_patch;
+
+ file_version_major = fgetc(file);
+ file_version_minor = fgetc(file);
+ file_version_patch = fgetc(file);
+ fgetc(file); /* not used */
+
+ game_version_major = fgetc(file);
+ game_version_minor = fgetc(file);
+ game_version_patch = fgetc(file);
+ fgetc(file); /* not used */
+
+ *file_version = VERSION_IDENT(file_version_major,
+ file_version_minor,
+ file_version_patch);
+
+ *game_version = VERSION_IDENT(game_version_major,
+ game_version_minor,
+ game_version_patch);
+}
+
+static void WriteChunk_VERS(FILE *file, int file_version, int game_version)
+{
+ int file_version_major = VERSION_MAJOR(file_version);
+ int file_version_minor = VERSION_MINOR(file_version);
+ int file_version_patch = VERSION_PATCH(file_version);
+ int game_version_major = VERSION_MAJOR(game_version);
+ int game_version_minor = VERSION_MINOR(game_version);
+ int game_version_patch = VERSION_PATCH(game_version);
+
+ fputc(file_version_major, file);
+ fputc(file_version_minor, file);
+ fputc(file_version_patch, file);
+ fputc(0, file); /* not used */
+
+ fputc(game_version_major, file);
+ fputc(game_version_minor, file);
+ fputc(game_version_patch, file);
+ fputc(0, file); /* not used */
+}
+
+static int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
+static void setLevelInfoToDefaults()
+{
+ int i, x, y;
+
+ level.file_version = FILE_VERSION_ACTUAL;
+ level.game_version = GAME_VERSION_ACTUAL;
+
+ level.encoding_16bit_field = FALSE; /* default: only 8-bit elements */
+ level.encoding_16bit_yamyam = FALSE; /* default: only 8-bit elements */
+ level.encoding_16bit_amoeba = FALSE; /* default: only 8-bit elements */
+
+ lev_fieldx = level.fieldx = STD_LEV_FIELDX;
+ lev_fieldy = level.fieldy = STD_LEV_FIELDY;
+
+ for(x=0; x<MAX_LEV_FIELDX; x++)
+ for(y=0; y<MAX_LEV_FIELDY; y++)
+ Feld[x][y] = Ur[x][y] = EL_ERDREICH;
+
+ level.time = 100;
+ level.gems_needed = 0;
+ level.amoeba_speed = 10;
+ level.time_magic_wall = 10;
+ level.time_wheel = 10;
+ level.time_light = 10;
+ level.time_timegate = 10;
+ level.amoeba_content = EL_DIAMANT;
+ level.double_speed = FALSE;
+ level.gravity = FALSE;
+
+ for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
+ level.name[i] = '\0';
+ for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
+ level.author[i] = '\0';
+
+ strcpy(level.name, NAMELESS_LEVEL_NAME);
+ strcpy(level.author, ANONYMOUS_NAME);
+
+ for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
+ level.score[i] = 10;
+
+ level.num_yam_contents = STD_ELEMENT_CONTENTS;
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(x=0; x<3; x++)
+ for(y=0; y<3; y++)
+ level.yam_content[i][x][y] =
+ (i < STD_ELEMENT_CONTENTS ? EL_FELSBROCKEN : EL_LEERRAUM);
+
+ Feld[0][0] = Ur[0][0] = EL_SPIELFIGUR;
+ Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
+ Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_AUSGANG_ZU;
+
+ BorderElement = EL_BETON;
+
+ /* try to determine better author name than 'anonymous' */
+ if (strcmp(leveldir_current->author, ANONYMOUS_NAME) != 0)