a
    6i=                  	   @   s  U d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZmZ ddlmZ ddlmZ d	Zdaeedf ed
< edddZd?eeedddZdZdZdZdZd@eedddZdAeedddZdBeedddZ dCeedddZ!eeeee"e"dd d!Z#eeee"e"d"d#d$Z$dDd%d&eeeee"e"d'd(d)Z%eeeee	e	gdf ee"d*d+d,Z&dd-ee	eedf dd.d/d0Z'ee	e	dd1d2d3Z(ee	e	dd1d4d5Z)dEeeeedd6d7d8Z*dFeeeeeee"d9d:d;Z+e,eeedd<d=d>Z-dS )Gz4Download and setup DLLs to upgrade various upscalers    N)Path)CallableUnion)unquoteurlparse   )log)configzihttps://raw.githubusercontent.com/beeradmoore/dlss-swapper-manifest-builder/refs/heads/main/manifest.json__manifest_json)returnc               
   C   s  t d urt S tjjd} | jddd | d}i a zBtjjt	dd}t
| a W d    n1 sl0    Y  W nD ty } z,tdt	 d tt| W Y d }~nHd }~0 0 |jd	d
d }|t
t  W d    n1 s0    Y  zPt sJ| rJ|jd
d}t
| a W d    n1 s@0    Y  W nJ ty } z0tdt| d tt| W Y d }~n
d }~0 0 t S )N	upscalersTparentsexist_okzmanifest.json
   timeoutzFailed to download ""wutf-8encodingz Failed to read cached manifest ")r
   r	   path	cache_dirjoinpathmkdirurllibrequesturlopen__manifest_urljsonloadsread	Exceptionr   critrepropenwritedumpsexistsstr)r   Zcached_manifesturl_fdeZmanifest_fd r-   b/home/james-whalen/.local/share/Steam/compatibilitytools.d/GE-Proton10-32/protonfixes/upscalers.py__get_manifest   s,    
0$.2$r/   default)upscalerversionr   c              	   C   s   t  |  }ttdd |}t|D ]4}||d v r$td|   d| d |  S q$td| d|   d	|d
 d   |d
 S )Nc                 S   s
   | d  S )NZis_dev_filer-   )dllr-   r-   r.   <lambda>6       z$__get_dll_manifest.<locals>.<lambda>r2   zFound "z" dll version "r   z	Version "z" for "z" not found, using )r/   tuplefilterreversedr   debugupper)r1   r2   dllsr3   r-   r-   r.   __get_dll_manifest4   s    

 r=   Zdlss_versionZxess_versionZfsr3_versionZfsr4_version)r2   r   c                 C   s   t d| t d| t d| dS )NdlssZdlss_dZdlss_g)z'drive_c/windows/system32/nvngx_dlss.dllz(drive_c/windows/system32/nvngx_dlssd.dllz(drive_c/windows/system32/nvngx_dlssg.dllr=   r2   r-   r-   r.   __get_dlss_dllsG   s    rA   c                 C   s&   t d| t d| t d| t d| dS )NxessZ	xess_dx11ZxellZxess_fg)z$drive_c/windows/system32/libxess.dllz)drive_c/windows/system32/libxess_dx11.dllz$drive_c/windows/system32/libxell.dllz'drive_c/windows/system32/libxess_fg.dllr?   r@   r-   r-   r.   __get_xess_dllsS   s    rC   c                 C   s   t d| t d| dS )NZ	fsr_31_vkZfsr_31_dx12)z.drive_c/windows/system32/amd_fidelityfx_vk.dllz0drive_c/windows/system32/amd_fidelityfx_dx12.dllr?   r@   r-   r-   r.   __get_fsr3_dlls`   s    rD   c                 C   sN   ddd d dddd d dddd d dd}| d	ks>| |  vrBd
} d||  iS )Nz4.0.0_67A4D2BC10ad000zNhttps://download.amd.com/dir/bin/amdxcffx64.dll/67A4D2BC10ad000/amdxcffx64.dll)r2   download_urlmd5_hashzip_md5_hashz4.0.1_67D435F7d97000zMhttps://download.amd.com/dir/bin/amdxcffx64.dll/67D435F7d97000/amdxcffx64.dllz4.0.2_68840348eb8000zMhttps://download.amd.com/dir/bin/amdxcffx64.dll/68840348eb8000/amdxcffx64.dll)4.0.0z4.0.14.0.2r0   rI   z'drive_c/windows/system32/amdxcffx64.dll)keys)r2   Z__fsr4_dllsr-   r-   r.   __get_fsr4_dllsk   s(    rK   )
prefix_dirdstfiler2   ignore_versionr   c           
      C   s  t j| |}t j|r6td| d t | t j|rnt |j	dk rntd| d t | t j|st
d| d dS t|d&}t|   }W d    n1 s0    Y  |d }|d ur|| krt
d	| d dS |s|d
 |d
 kr4t
d| d dS |dd }	|	d urn||	 krnt
d| d dS td| d dS )NzRemoving stale symlink "r      zRemoving stale file "zMissing file from prefix "FrbrF   z2MD5 checksum mismatch between version and prefix "r2   z3Version mismatch between configuration and prefix "z3MD5 checksum mismatch between manifest and prefix "zFound matching file in prefix "T)osr   joinislinkr   r:   unlinkr)   statst_sizewarnr&   hashlibmd5r"   	hexdigestlowerget)
rL   rM   rN   r2   rO   targetdst_fddst_md5Zversion_md5file_md5r-   r-   r.   __check_upscaler_file   s4    

4rb   )rL   filesversion_filerO   r   c           	   
      s   t j|s"td| d dS z^t|dd}| W d    n1 sN0    Y  t 	 D ]}| 
d}qjW nF ty } z.td| d tt| W Y d }~dS d }~0 0 t fdd	 	 D }t|S )
NzMissing version file "r   Fr   r   rF   z"Error while reading version file "c                 3   s&   | ]}t | | | V  qd S )N)rb   ).0rM   rc   rO   rL   r2   r-   r.   	<genexpr>   s   z)__check_upscaler_files.<locals>.<genexpr>)rR   r   isfiler   rX   r&   r"   r    r!   rJ   r]   r#   r%   r7   all)	rL   rc   rd   rO   
version_fdrM   _r,   Zvalid_filesr-   rf   r.   __check_upscaler_files   s"    &
rl   FrO   )name
compat_dirrL   r2   rO   r   c          
   
   C   s   t tfttfttfttfd}||  \}}z||}W n> tyt }	 z&t	
d t	
t|	 W Y d}	~	dS d}	~	0 0 t||tj|||S )a	  Check for upscaler files and their versions

    name: the name of the upscaler, possible values dlss, xess, fsr3, fsr4
    version: the version of the upscaler dll to download
    ignore_version: ignore version mismatch but still check if the dlls are present
    r>   rB   fsr3fsr4z)Failed to get file versions from manifestNF)rA   __dlss_version_filerC   __xess_version_filerD   __fsr3_version_filerK   __fsr4_version_filer#   r   r$   r%   rl   rR   r   rS   )
rn   ro   rL   r2   rO   r   	get_filesrd   rc   r,   r-   r-   r.   check_upscaler   s$    
rx   )rL   rc   dlfuncrd   r   c                 C   sd  t jjd}i }| D ]}tdtj| d t	| |}t	| |d }z:|
 sd| rn|| ||| || |jdd W nr t y }	 zXtd|j d tt|	 |jdd |
 s| r|| W Y d}	~	 d	S d}	~	0 0 || d
 || d d||< qt|ddd }
|
t| W d   n1 sV0    Y  dS )a  Download and install the required dlls.

    This function takes care of backing up, downloading, and installing the required dlls
    If the download fails, it will uses the backups to revert to the previous files, otherwise
    the backups are removed.

    The downloading, caching and installation of the dlls is facilitated in the callable passed through
    the `dlfunc` argument.
    r   zDownloading upscaler file "r   z.oldT
missing_okzError while downloading file "NFr2   rF   )r2   rF   r   r   r   )r	   r   r   r   rJ   r   inforR   basenamer   r)   
is_symlinkrenamerU   r#   r$   rn   r%   r&   r'   r    r(   )rL   rc   ry   rd   r   r2   rM   rN   tempr,   rj   r-   r-   r.   __download_upscaler_files   s0    




0r   checksum)urlrM   r   r   c          	   
   C   s,  |j jddd tjj| ddid}z|dN}tjj|dd}||  W d	   n1 sf0    Y  W d	   n1 s0    Y  t	
|d
   }| r| jnd}|d	ur|| ks|dk rtdt| W n6 ty& } z|jdd |W Y d	}~n
d	}~0 0 d	S )zDownloads a file and checks against a checksum.

    If the download fails or the checksums do not match, the file is removed and the exception is
    propagated to the caller.
    Tr   z
User-AgentzIMozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0)headerswbr   r   NrQ   r   rP   zMalformed download rz   )parentr   r   r   Requestr&   r   r'   r"   rY   rZ   r[   r\   r)   rV   rW   RuntimeErrorr*   r#   rU   )	r   rM   r   r   r_   r+   r`   Zdst_sizer,   r-   r-   r.   __download_file  s"    Jr   )rN   cacherM   r   c                 C   s   t tt| d j}||j}| dd }| rt	|
d   }|d ur|| krtd|j d |jdd | st| d ||d |jjddd	 t|}||j W d    n1 s0    Y  d S )
NrE   rG   rQ   z3MD5 checksum mismatch between manifest and cached "r   Trz   r   r   )r   r   r   r   r   rn   r]   r)   rY   rZ   r&   r"   r[   r\   r   r$   rU   r   r   r   zipfileZipFile
extractall)rN   r   rM   url_pathcached_filera   Z
cached_md5Zzip_fdr-   r-   r.   __download_extract_zip6  s    r   c                 C   s   t tt| d j}||jd| d   |j }| dd }| r`|	 j
dk r`|  | szt| d ||d |jjddd t|| d S )	NrE   Z_vr2   rG   rP   r   Tr   )r   r   r   r   r   stemsuffixr]   r)   rV   rW   rU   r   r   r   shutilcopy)rN   r   rM   r   r   ra   r-   r-   r.   __download_fsr4H  s    r   )rn   ro   rL   r2   r   c           
   
   C   s   t | |||ddrdS td|   d tttftttft	tt
ftttfd}||  \}}}z(||}t|||tj||stW nH ty }	 z0td|   d tt|	 W Y d}	~	n
d}	~	0 0 dS )	zCheck for upscaler files and their versions

    name: the name of the upscaler, possible values dlss, xess, fsr3, fsr4
    version: the version of the upscaler dll to download
    Frm   NzFailed to validate "z" files.rp   zFailed to download z dlls.)rx   r   r|   r;   rA   r   rs   rC   rt   rD   ru   rK   r   rv   r   rR   r   rS   r   r#   r$   r%   )
rn   ro   rL   r2   r   rw   Zdownload_funcrd   rc   r,   r-   r-   r.   download_upscalerW  s*    r   )envkeyrn   ro   rL   r2   r   c                 C   sZ   |  |ddvr| | n|}t|||| t||||dd}|rVtd|  d |S )N0>   1r   Trm   z
Automatic z upgrade enabled.)r]   r   rx   r   r|   r;   )r   r   rn   ro   rL   r2   enabledr-   r-   r.   __setup_upscalerx  s    r   )compat_configr   ro   rL   r   c                 C   s(  t  }d| v r(t|dd||r(|d d| v rJt|dd||rJ|d d| v rlt|dd||rl|d d| v rt|dd	||d
r|d	 n"d	| v rt|dd	||r|d	 d	|v rd|d< d| v rd|d< d| v rd|d< d|v r|dtd d|v rd|v r|r$d||d< dS )zSetup configured upscalers

    usage: setup_upscalers(g_session.compat_config, g_session.env, g_compatdata.base_dir, g_compatdata.prefix_dir)
    r>   PROTON_DLSS_UPGRADErB   PROTON_XESS_UPGRADErq   PROTON_FSR3_UPGRADE	fsr4rdna3PROTON_FSR4_RDNA3_UPGRADErr   rH   PROTON_FSR4_UPGRADEr   ZFSR4_UPGRADEZmlfgZMLFG_UPGRADEZwmma_rdna3_workaroundZDXIL_SPIRV_CONFIGZDXVK_NVAPI_DRS_SETTINGSzngx_dlss_sr_override=on,ngx_dlss_rr_override=on,ngx_dlss_fg_override=on,ngx_dlss_sr_override_render_preset_selection=default,ngx_dlss_rr_override_render_preset_selection=default,,ZWINE_LOADDLL_REPLACEN)setr   add
setdefaultr*   rS   )r   r   ro   rL   Zloaddll_replacer-   r-   r.   setup_upscalers  sF    





r   )r0   )r0   )r0   )r0   )r0   )r0   )r0   )r0   ).__doc__rY   r    rR   r   urllib.requestr   r   pathlibr   typingr   r   urllib.parser   r   loggerr   r	   r   r
   dict__annotations__r/   r*   r=   rs   rt   ru   rv   rA   rC   rD   rK   boolrb   rl   rx   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   s   
, $*" ' 