Пример #1
0
def available_languages():
    return [default_language] + \
           [file_name[5:] for file_dir in dirs.get_read_dirs("i18n")
                          if os.path.isdir(file_dir)
                          for file_name in os.listdir(file_dir)
                          if os.path.isdir(os.path.join(file_dir, file_name))
                             and file_name.startswith("lang_")]
Пример #2
0
def load_sounds():
    """ 
        load_sounds() loads all of the sounds in the data/sounds/ directory 
    """
    global sounds
    sounds = {}

    if not init:
        # Sound is not initialized. Warn if user wanted sound
        if not nosound:
            sys.stderr.write(
                "WARNING: Sound is requested, but mixer is not initialized!\n")
        return

    # Build the set of paths we'll check for sounds.
    sounds_paths = dirs.get_read_dirs("sounds")

    # Main loop for sounds_paths
    for sounds_path in sounds_paths:
        if not os.path.isdir(sounds_path): continue

        # Loop through the files in sounds_path and add them
        for entry in os.walk(sounds_path):
            root = entry[0]
            files = entry[2]
            (head, tail) = os.path.split(root)

            # Avoid hidden file
            if tail.startswith("."): continue

            for file_name in files:
                # Only wav file supported now.
                if (len(file_name) < 6 or file_name[-3:] != "wav"): continue

                real_file = os.path.join(head, tail, file_name)
                sound_class = tail

                # Load it via the mixer ...
                sound = pygame.mixer.Sound(real_file)

                # And shove it into the sounds dictionary.
                if sound_class not in sounds:
                    sounds[sound_class] = []

                sounds[sound_class].append({
                    "filename": real_file,
                    "sound": sound
                })

                if g.debug:
                    sys.stderr.write("DEBUG: Loaded soundfile: %s\n" %
                                     real_file)
Пример #3
0
def load_themes():
    themes = theme.themes = {}
    themes_dirs = dirs.get_read_dirs("themes")

    for themes_dir in themes_dirs:
        themes_list = [name for name in os.listdir(themes_dir)
                            if os.path.isdir(os.path.join(themes_dir, name))]

        for theme_id in themes_list:
            if (theme_id in themes):
                continue

            th = load_theme(theme_id, os.path.join(themes_dir, theme_id))
            th.find_files()
            themes[theme_id] = th
Пример #4
0
def get_savegames():
    all_dirs = dirs.get_read_dirs("saves")

    all_savegames = []
    for saves_dir in all_dirs:
        try:
            all_files = os.listdir(saves_dir)
        except Exception:
            continue

        for file_name in all_files:
            if file_name[0] == ".":
                continue

            if file_name.endswith('.sav'):
                name = file_name[:-4]
                parse_headers = parse_pickle_savegame_headers
                load_file = load_savegame_by_pickle
            elif file_name.endswith('.s2'):
                name = file_name[:-3]
                parse_headers = parse_json_savegame_headers
                load_file = load_savegame_by_json
            else:
                # Unknown extension; ignore
                continue

            filepath = os.path.join(saves_dir, file_name)
            version_name = None  # None == Unknown version

            try:
                with open(filepath, 'rb') as loadfile:
                    version_line, headers = parse_headers(loadfile)

                    if version_line in savefile_translation:
                        version_name = savefile_translation[version_line][0]
            except Exception:
                version_name = None  # To be sure.

            savegame = Savegame(convert_path_name_to_str(name), filepath,
                                version_name, headers, load_file)
            all_savegames.append(savegame)

    return all_savegames
Пример #5
0
def load_music():
    """
load_music() loads music for the game.  It looks in multiple locations:

* music/ in the install directory for E:S.
* music/ in user's XDG_DATA_HOME/singularity folder.
"""

    global music_dict
    music_dict = {}

    # Build the set of paths we'll check for music.
    music_paths = dirs.get_read_dirs("music")

    # Main loop for music_paths
    for music_path in music_paths:
        if os.path.isdir(music_path):

            # Loop through the files in music_path and add the ones
            # that are .mp3s and .oggs.
            for entry in os.walk(music_path):
                root = entry[0]
                files = entry[2]
                (head, tail) = os.path.split(root)
                if (tail.lower() != ".svn"):
                    if tail not in music_dict:
                        music_dict[tail] = []
                    for file_name in files:
                        if (len(file_name) > 5
                                and (file_name[-3:] == "ogg"
                                     or file_name[-3:] == "mp3")):
                            music_dict[tail].append(
                                os.path.join(head, tail, file_name))
                            if g.debug:
                                sys.stderr.write("D: Loaded musicfile %s\n" %
                                                 music_dict[tail][-1])
Пример #6
0
def generic_load(filename, load_dirs="data", mandatory=True):
    """
generic_load() loads a data file.  Data files are all in Python-standard
ConfigParser format.  The 'id' of any object is the section of that object.
Fields that need to be lists are postpended with _list; this is stripped
from the actual name, and the internal entries are broken up by the pipe
("|") character.

On errors, if file is mandatory then quit, else raise exception. For syntax
parsing-related errors, always print error message. For IOErrors silently ignore
non-mandatory missing or otherwise unreadable files
"""

    # Get directories to find the file
    if load_dirs is not None:
        load_dirs = dirs.get_read_dirs(load_dirs)

    # For each directories, create a file, otherwise use filename
    if load_dirs is not None:
        files = [os.path.join(load_dir, filename) for load_dir in load_dirs]
    else:
        files = [filename]

    # Find the first readable file.
    found = False
    errors = []
    config = RawConfigParser()

    for filepath in files:
        try:
            with open(filepath, encoding='utf-8') as fd:
                config.read_file(fd)
            found = True
            break

        except IOError as reason:
            # Silently ignore non-mandatory missing files
            if mandatory:
                errors.append("Cannot read '%s': %s\nExiting\n" %  (filename, reason))

        except Exception as reason:
            # Always print parsing errors, even for non-mandatory files
            errors.append("Error parsing '%s': %s\n" %  (filename, reason))

    for err in errors:
        sys.stderr.write(err)

    if not found:
        if mandatory:
            sys.exit(1)
        else:
            return

    return_list = []

    # Get the list of items (IDs) in the file and loop through them.
    for item_id in config.sections():
        item_dict = {}
        item_dict["id"] = item_id

        # Get the list of settings for this particular item.
        for option in config.options(item_id):

            # If this is a list ...
            if len(option) > 6 and option[-5:] == "_list":

                # Break it into elements separated by |.
                item_dict[option[:-5]] = [x.strip() for x in config.get(item_id, option).split("|")]
            else:

                # Otherwise, just grab the data.
                item_dict[option] = config.get(item_id, option).strip()

        # Add this to the list of all objects we are returning.
        return_list.append(item_dict)

    return return_list