a
    6i                     @   s"  d 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 ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ eedddZeedddZeedddZee
e dddZd$eeeedddZd%eeedddZd&eeeedddZeddd d!Zddd"d#ZdS )'z+Gets the game id and applies a fix if found    N)	lru_cache)import_module)Optional   )ProtonVersion)config)
run_checks)log)returnc                   C   sp   dt jv rt jd S dt jv r(t jd S dt jv r<t jd S dt jv r\tdt jd d S td t  dS )	z5Trys to return the game id from environment variablesUMU_ID
SteamAppIdSteamGameIdSTEAM_COMPAT_DATA_PATHz\d+z*Game ID not found in environment variablesN)osenvironrefindallr	   Zcritexit r   r   \/home/james-whalen/.local/share/Steam/compatibilitytools.d/GE-Proton10-32/protonfixes/fix.pyget_game_id   s    







r   )databaser
   c              
   C   s  t jd }t jdpd}d}zt| dddd}t|}|D ]D}t|dkr>|d |kr>|d	 |kr>|d
   W  d   W S q>W d   n1 s0    Y  W nT ty   t	d|   Y n4 t
y } ztd|  W Y d}~n
d}~0 0 t	d |S )z-Get the game name from the local umu databaser   STOREnoneUNKNOWN utf-8)newlineencoding   r   r   NzCSV file not found: zError reading CSV file: zGame title not found in CSV)r   r   getopencsvreaderlenFileNotFoundErrorr	   warn	Exceptiondebug)r   Zumu_idstoretitleZcsvfileZ	csvreaderrowexr   r   r   get_game_title"   s    

$@&
r.   c               
   C   s   t jdr.t jt jt d} t| S zt	d t
dt jd t
jd }t j|dt  d}t|d	d
V}| D ]<}d| v rt
d|t
jd }|  W  d   W S qW d   n1 s0    Y  W n tttfy   Y n0 dS )z8Tries to return the game name from environment variablesr   z/umu-database.csvzUMU_ID is not in environmentz.*/steamappsZPWDr   Zappmanifest_z.acfr   r   namez"[^"]+"Nr   )r   r   r!   pathdirnameabspath__file__r.   r	   r)   r   r   
IGNORECASEjoinr   r"   	readlinesstripUNICODEOSError
IndexErrorUnicodeDecodeError)r   Zgame_libraryZgame_manifestZappmanifestZxliner0   r   r   r   get_game_name9   s    
<r=   )r*   r
   c                 C   s"   ddddddddd	d
d
 | dS )z*Mapping for store identifier to store nameZAmazonz
Battle.netZEAZEGSZGOGZHumblezItch.ioZSteamZUbisoftzZOOM Platform)
Zamazon	battleneteaZegsZgogZhumbleZitchiosteamubisoftZzoomplatformN)r!   )r*   r   r   r   get_store_nameP   s    rB   F)game_iddefaultlocalr
   c                 C   s   d}|   rd}ntjdr0tjdd }|dkrtdt  d|  d t|}|rvt| d| d	 ntd
 d}|sd| dnd|s| nd S )z;Creates the name of a gamefix module, which can be importedZumur@   r   r   zNon-steam game  ()z store specified, using z	 databasez&No store specified, using UMU databasezprotonfixes.gamefixes-.zlocalfixes.rD   )		isnumericr   r   r!   lowerr	   infor=   rB   )rC   rD   rE   r*   Z
store_namer   r   r   get_module_name`   s    

rL   )rC   rD   r
   c                 C   s   t jd}|s| nd}t jt j||d s6dS tt j|dddd$ tjt jd	 W d
   n1 sx0    Y  t| |dS )z6Check if a local gamefix is available first and run itz ~/.config/protonfixes/localfixesrD   z.pyFz__init__.pyar   r/   z~/.config/protonfixesNT)	r   r1   
expanduserisfiler6   r"   sysappend_run_fix)rC   rD   Z	localpathmodule_namer   r   r   _run_fix_localw   s    2rT   c                 C   s   |sdnd}|sdnd}z\t | ||}t|}td| d| dt  d|  d		 t|d
rj||  n|  W n: ty   td| d| dt  d|  d		 Y dS 0 dS )z3Private function, which actually executes gamefixesZ	protonfixdefaultsglobalrE   zUsing  z for rF   rG   main_with_idzNo z found for FT)	rL   r   r	   rK   r=   hasattrrX   mainImportError)rC   rD   rE   Zfix_typeZscoperS   Zgame_moduler   r   r   rR      s    &
&rR   )rC   r
   c                 C   sV   | du rdS t jjrt  t| ds6t jjr6t| d t| dsRt jjrRt| d dS )zdLoads a gamefix module by it's gameid

    local fixes prevent global fixes from being executed
    NTF)r   rZ   Zenable_checksr   rT   Zenable_global_fixesrR   )rC   r   r   r   run_fix   s    
r\   c                  C   s   dt jd v dt jd v dt jd v g} t| rNttt j td dS t }td|j	 d	|j
 d
 tt  dS )zRuns the gamefixziscriptevaluator.exe   getcompatpathr   getnativepathz&Not running protonfixes for setup runsNzRunning protonfixes on "z", build at rH   )rP   argvanyr	   r)   strr   Zfrom_version_filerK   Zversion_nameZ
build_dater\   r   )Z
check_argsversionr   r   r   rZ      s    
rZ   )FF)F)FF)__doc__r   r   rP   r#   	functoolsr   	importlibr   typingr   Zutilr   r   Zchecksr   loggerr	   rb   r   r.   r=   rB   boolrL   rT   rR   r\   rZ   r   r   r   r   <module>   s,   