示例#1
0
def show_user_scripts() -> None:
    """Open or nicely print the location of the user-scripts directory."""
    from ba import _lang
    from ba._enums import Permission
    app = _ba.app

    # First off, if we need permission for this, ask for it.
    if not _ba.have_permission(Permission.STORAGE):
        _ba.playsound(_ba.getsound('error'))
        _ba.screenmessage(_lang.Lstr(resource='storagePermissionAccessText'),
                          color=(1, 0, 0))
        _ba.request_permission(Permission.STORAGE)
        return

    # Secondly, if the dir doesn't exist, attempt to make it.
    if not os.path.exists(app.python_directory_user):
        os.makedirs(app.python_directory_user)

    # On android, attempt to write a file in their user-scripts dir telling
    # them about modding. This also has the side-effect of allowing us to
    # media-scan that dir so it shows up in android-file-transfer, since it
    # doesn't seem like there's a way to inform the media scanner of an empty
    # directory, which means they would have to reboot their device before
    # they can see it.
    if app.platform == 'android':
        try:
            usd: Optional[str] = app.python_directory_user
            if usd is not None and os.path.isdir(usd):
                file_name = usd + '/about_this_folder.txt'
                with open(file_name, 'w') as outfile:
                    outfile.write('You can drop files in here to mod the game.'
                                  '  See settings/advanced'
                                  ' in the game for more info.')
                _ba.android_media_scan_file(file_name)
        except Exception:
            from ba import _error
            _error.print_exception('error writing about_this_folder stuff')

    # On a few platforms we try to open the dir in the UI.
    if app.platform in ['mac', 'windows']:
        _ba.open_dir_externally(app.python_directory_user)

    # Otherwise we just print a pretty version of it.
    else:
        _ba.screenmessage(get_human_readable_user_scripts_path())
示例#2
0
def _request_storage_permission() -> bool:
    """If needed, requests storage permission from the user (& return true)."""
    from ba._language import Lstr
    from ba._enums import Permission
    if not _ba.have_permission(Permission.STORAGE):
        _ba.playsound(_ba.getsound('error'))
        _ba.screenmessage(Lstr(resource='storagePermissionAccessText'),
                          color=(1, 0, 0))
        _ba.timer(1.0, lambda: _ba.request_permission(Permission.STORAGE))
        return True
    return False