-}
-
-int getLevelSeriesNrFromLevelSeriesName(char *level_series_name)
-{
- int i;
-
- if (!level_series_name)
- return 0;
-
- for (i=0; i<num_leveldirs; i++)
- if (strcmp(level_series_name, leveldir[i].filename) == 0)
- return i;
-
- return 0;
-}
-
-static int compareLevelDirInfoEntries(const void *object1, const void *object2)
-{
- const struct LevelDirInfo *entry1 = object1;
- const struct LevelDirInfo *entry2 = object2;
- int compare_result;
-
- if (entry1->sort_priority == entry2->sort_priority)
- {
- char *name1 = getStringToLower(entry1->name_sorting);
- char *name2 = getStringToLower(entry2->name_sorting);
-
- compare_result = strcmp(name1, name2);
-
- free(name1);
- free(name2);
- }
- else if (LEVELSORTING(entry1) == LEVELSORTING(entry2))
- compare_result = entry1->sort_priority - entry2->sort_priority;
- else
- compare_result = LEVELSORTING(entry1) - LEVELSORTING(entry2);
-
- return compare_result;
-}
-
-static int LoadLevelInfoFromLevelDir(char *level_directory, int start_entry)
-{
- DIR *dir;
- struct stat file_status;
- char *directory = NULL;
- char *filename = NULL;
- struct SetupFileList *setup_file_list = NULL;
- struct dirent *dir_entry;
- int i, current_entry = start_entry;
-
- if ((dir = opendir(level_directory)) == NULL)
- {
- Error(ERR_WARN, "cannot read level directory '%s'", level_directory);
- return current_entry;
- }
-
- while (current_entry < MAX_LEVDIR_ENTRIES)
- {
- if ((dir_entry = readdir(dir)) == NULL) /* last directory entry */
- break;
-
- /* skip entries for current and parent directory */
- if (strcmp(dir_entry->d_name, ".") == 0 ||
- strcmp(dir_entry->d_name, "..") == 0)
- continue;
-
- /* find out if directory entry is itself a directory */
- directory = getPath2(level_directory, dir_entry->d_name);
- if (stat(directory, &file_status) != 0 || /* cannot stat file */
- (file_status.st_mode & S_IFMT) != S_IFDIR) /* not a directory */
- {
- free(directory);
- continue;
- }
-
- filename = getPath2(directory, LEVELINFO_FILENAME);
- setup_file_list = loadSetupFileList(filename);
-
- if (setup_file_list)
- {
-#if 0
- DrawInitText(dir_entry->d_name, 150, FC_YELLOW);
-#endif
-
- checkSetupFileListIdentifier(setup_file_list, LEVELINFO_COOKIE);
- setLevelDirInfoToDefaults(&leveldir[current_entry]);
-
- ldi = leveldir[current_entry];
- for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++)
- setSetupInfo(i, getTokenValue(setup_file_list, token_info[i].text));
- leveldir[current_entry] = ldi;
-
-#if 1
- DrawInitText(leveldir[current_entry].name, 150, FC_YELLOW);
-#endif
-
- if (leveldir[current_entry].name_short == NULL)
- leveldir[current_entry].name_short =
- getStringCopy(leveldir[current_entry].name);
-
- if (leveldir[current_entry].name_sorting == NULL)
- leveldir[current_entry].name_sorting =
- getStringCopy(leveldir[current_entry].name);
-
- leveldir[current_entry].filename = getStringCopy(dir_entry->d_name);
- leveldir[current_entry].last_level =
- leveldir[current_entry].first_level +
- leveldir[current_entry].levels - 1;
- leveldir[current_entry].user_defined =
- (level_directory == options.level_directory ? FALSE : TRUE);
- leveldir[current_entry].color = LEVELCOLOR(&leveldir[current_entry]);
- leveldir[current_entry].class_desc =
- getLevelClassDescription(&leveldir[current_entry]);
-#if 0
- leveldir[current_entry].handicap_level =
- leveldir[current_entry].first_level; /* default value */
-#else
- leveldir[current_entry].handicap_level =
- (leveldir[current_entry].user_defined ?
- leveldir[current_entry].last_level :
- leveldir[current_entry].first_level);
-#endif
-
- freeSetupFileList(setup_file_list);
- current_entry++;
- }
- else
- Error(ERR_WARN, "ignoring level directory '%s'", directory);
-
- free(directory);
- free(filename);
- }
-
- closedir(dir);
-
- if (current_entry == MAX_LEVDIR_ENTRIES)
- {
- Error(ERR_WARN, "maximum of %d level directories reached", current_entry);
- Error(ERR_WARN, "remaining level directories ignored in directory '%s'",
- level_directory);
- }
- else if (current_entry == start_entry)
- Error(ERR_WARN, "cannot find any valid level series in directory '%s'",
- level_directory);
-
- return current_entry;
-}
-
-void LoadLevelInfo()
-{
- InitUserLevelDirectory(getLoginName());
-
- num_leveldirs = 0;
- leveldir_nr = 0;
-
- DrawInitText("Loading level series:", 120, FC_GREEN);
-
- num_leveldirs = LoadLevelInfoFromLevelDir(options.level_directory,
- num_leveldirs);
- num_leveldirs = LoadLevelInfoFromLevelDir(getUserLevelDir(""),
- num_leveldirs);
-
- if (num_leveldirs == 0)
- Error(ERR_EXIT, "cannot find any valid level series in any directory");
-
- if (num_leveldirs > 1)
- qsort(leveldir, num_leveldirs, sizeof(struct LevelDirInfo),
- compareLevelDirInfoEntries);
-}
-
-static void SaveUserLevelInfo()
-{
- char *filename;
- FILE *file;
- int i;
-
- filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME);
-
- if (!(file = fopen(filename, "w")))
- {
- Error(ERR_WARN, "cannot write level info file '%s'", filename);
- free(filename);
- return;
- }