pystardust / ani-cli

A cli tool to browse and play anime
GNU General Public License v3.0
7.44k stars 530 forks source link

WSL2 dep for mvp.exe should be removed #1221

Closed Jammrock closed 8 months ago

Jammrock commented 9 months ago

Metadata (please complete the following information) Version: 4.0.4-1 OS: Ubuntu WSL2 on Windows 11 Shell: bash Anime: N/A

Describe the bug ani-cli tries to launch mpv.exe when launched in a WSL2 distro on Windows. With WSLg (WSL graphics) there is no longer a need to launch an external player in Windows. You can launch mpv in WSL2 and the video will play on the desktop.

Change:

    *MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS

To:

    *MINGW*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS

Result:

mpv icon on Windows taskbar. image

Video playing on Windows desktop. image

port19x commented 9 months ago

Do I understand correctly that mpv.exe continues to work, but in addition to it a WSL2 mpv works as well? In that case it would be more desireable to support both configurations.

The solution in that case would be to leave the matching alone and instead add a case during the dependency checks that adds a fallback mpv check if mpv.exe checks unsuccessfully.

The order of preference between wsl mpv and mpv.exe should also be determined.

Jammrock commented 9 months ago

Yes, they should both work... when defaults are used.

WSL2 interop with Windows can be disabled. Which would block using mpv.exe from within WSL2.

https://learn.microsoft.com/en-us/windows/wsl/filesystems#disable-interoperability

You can also disable Windows volume mounting in Linux. Which would block the use of mpv.exe.

https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configuration-setting-for-wslconfig

Section label: [automount]

key value   default notes
enabled boolean true    true causes fixed drives (i.e C:/ or D:/) to be automatically mounted with DrvFs under /mnt. false means drives won't be mounted automatically, but you could still mount them manually or via fstab.

So first check if C:\ is mounted, then if interop is enabled. Look for mpv.exe if true. Look for mpv if anything fails.

I came up with something like this...

# check if mpv.exe is discoverable by WSL2 interop
# check if /mnt/c exists.
# Searching for mpv.exe is irrelevant if Windows volume mounting is disabled.
DIR=/mnt/c
if [ ! -d "$DIR" ];
then
    echo "Windows volume mounting is disabled. Looking for mpv in WSL2."
else

    # WSL interop file
    wsli=/proc/sys/fs/binfmt_misc/WSLInterop

    # search for mpv.exe if interop enabled
    if ! grep -q enabled "$wsli"; 
    then 
        echo WSL interop disabled, looking for mpv in Linux; 
    else 
        echo WSL interop enabled, look for mpv.exe in Windows

        # the WSL user's Windows path
        wUserpath="$(wslvar USERPROFILE)"
        # converted to Linux path
        lUserpath=$(wslpath "$wUserpath")

        # get the Windows PATHs that are in the user path and convert them to WSL2 Linux paths
        # split the Windows paths into an array
        IFS=';' read -ra wPATH <<< "$(wslvar path)"
        # linux paths are saved to lPATH
        lPATH=()

        # loop through Windows paths
        for t in ${wPATH[@]}; do
            # convert to Linux path
            tl="$(wslpath "$t")"
            # search for a match to the user profile path, in LInux path format
            if [[ $tl == *"$lUserpath"* ]]; then
                # add to lPATH
                lPATH+=($tl);
            fi
        done

        # WSL will not run en EXE that does not exist in a system or user path
        # search for mpv.exe in the Windows paths within the user profile
        # this speeds up the search and will catch mpv.exe installed by scoop
        # and installed in the local user appdata.
        for m in ${lPATH[@]}; do
            # use -L because scoop uses a symlink from current to the newest app version
            mpvpath=$(find -L "$m" -type f -name "mpv.exe") 
            if [ -n "$mpvpath" ]; 
            then 
                echo "Found mpv.exe at $mpvpath"
                break;
            fi
        done

        # was mpv.exe found?
        if [ -z "$mpvpath" ]; 
        then 
            echo "mpv.exe was not found. Fall back to mpv in Linux."
        fi
    fi;
fi