Ë
    Í—Äj±"  ã                   óŠ   — d dl Z d dlmZ d dlmZmZ ddlmZmZ dZ	 e
dd«      Zd Zd	Zd
ZdZdZdZdZdZdZdZ G d„ d«      Zy)é    N)ÚPath)ÚListÚUnioné   )Úread_cstring_atÚread_fixed_strings   IDSTé,   é2   é   é   é   éL   éÌ   éÐ   éÔ   éØ   é@   c                   ó|   — e Zd ZdZddeeef   defd„Zdd„Z	ddœd	e
e   d
eddfd„Zddœde
e   d
eddfd„Zdd„Zy)ÚMDLFilea(  A (tiny) parser for Source engine MDL (model) files.

    Currently scoped to reading the materials[] and materialDirectories[]
    blocks, and rewriting materialDirectories[] in place using a
    append, the new strings are appended at EOF, the int32
    offset table is repointed at them, and dataLength is updated.

    Args:
        mdl_path: Path to the MDL file. Accepts str or Path objects.
        auto_parse: Whether to automatically parse on initialization.
                   Defaults to True for convenience.

    Example:
        >>> mdl = MDLFile("path/to/model.mdl")
        >>> print(mdl.materials)         # ['my_texture', 'my_texture_blue']
        >>> print(mdl.material_dirs)     # ['models\\player\\demo\\']
        >>> mdl.rewrite_material_dirs(['console\\models\\player\\demo\\'])
    TÚmdl_pathÚ
auto_parsec                 óÖ   — t        |«      | _        d| _        d| _        d| _        d| _        d| _        g | _        g | _        d| _	        d| _
        d| _        |r| j                  «        y y )Nr   Ú F)Ústrr   ÚversionÚchecksumÚnameÚdata_lengthÚ	file_sizeÚ	materialsÚmaterial_dirsÚ_material_offsetÚ_material_dir_offsetÚ_parsedÚparse)Úselfr   r   s      úVC:\Users\pivot\Documents\cueki\casual-preloader\Lib\site-packages\valve_parsers/mdl.pyÚ__init__zMDLFile.__init__1   sh   € Ü˜H›ˆŒØˆŒØˆŒØˆŒ	Ø !ˆÔØˆŒØ$&ˆŒØ(*ˆÔØ%&ˆÔØ)*ˆÔ!ØˆŒÙØ�J‰J�Lð ó    Úreturnc                 óˆ  — t        | j                  «      j                  «       }|t        t        dz    t        k7  rt        d| j                  › �«      ‚t        j                  d|t        «      \  }|t        vrt        d|› d| j                  › �«      ‚t        j                  d|t        «      \  }t        |t        d«      }t        j                  d|t        «      \  }t        j                  d|t        «      \  }t        j                  d|t        «      \  }t        j                  d|t         «      \  }t        j                  d|t"        «      \  }	g }
t%        |«      D ]E  }||t&        z  z   }t        j                  d||«      \  }|
j)                  t+        |||z   «      «       ŒG g }t%        |«      D ]<  }t        j                  d||	|dz  z   «      \  }|j)                  t+        ||«      «       Œ> || _        || _        || _        || _        t5        |«      | _        |
| _        || _        || _        |	| _        d| _         | S )zÞParse the MDL header and populate materials / material_dirs.

        Returns:
            Self for method chaining.

        Raises:
            ValueError: If the file is not an MDL or is an unsupported version.
        r   únot an MDL file: ú<izunsupported MDL version z in r   T)!r   r   Ú
read_bytesÚ_OFF_IDÚ_MDL_IDÚ
ValueErrorÚstructÚunpack_fromÚ_OFF_VERSIONÚ_SUPPORTED_VERSIONSÚ_OFF_CHECKSUMr   Ú	_OFF_NAMEÚ_OFF_DATA_LENGTHÚ_OFF_MATERIAL_COUNTÚ_OFF_MATERIAL_OFFSETÚ_OFF_MATERIAL_DIR_COUNTÚ_OFF_MATERIAL_DIR_OFFSETÚrangeÚ_MATERIAL_ENTRY_SIZEÚappendr   r   r   r   r   Úlenr   r    r!   r"   r#   r$   )r&   Úbufr   r   r   r   Ú	mat_countÚ
mat_offsetÚ	dir_countÚ
dir_offsetr    ÚiÚ	entry_posÚname_relr!   Ústr_offs                   r'   r%   zMDLFile.parse@   s  € ô �4—=‘=Ó!×,Ñ,Ó.ˆàŒw”w ‘{Ð#¤wÒ.ÜÐ0°·±°Ð@ÓAÐAä×'Ñ'¨¨c´<Ó@‰
ˆØÔ-Ñ-ÜÐ7¸°yÀÀTÇ]Á]ÀOÐTÓUÐUä×(Ñ(¨¨s´MÓB‰ˆÜ  ¤i°Ó4ˆÜ×+Ñ+¨D°#Ô7GÓH‰ˆä×)Ñ)¨$°Ô5HÓI‰ˆÜ×*Ñ*¨4°Ô6JÓK‰ˆÜ×)Ñ)¨$°Ô5LÓM‰ˆÜ×*Ñ*¨4°Ô6NÓO‰ˆð  "ˆ	Ü�yÖ!ˆAØ" QÔ)=Ñ%=Ñ=ˆIÜ ×,Ñ,¨T°3¸	ÓB‰KˆXØ×Ñœ_¨S°)¸hÑ2FÓGÕHð "ð $&ˆÜ�yÖ!ˆAÜ×+Ñ+¨D°#°zÀAÈÁEÑ7IÓJ‰JˆWØ× Ñ ¤°°gÓ!>Õ?ð "ð ˆŒØ ˆŒØˆŒ	Ø&ˆÔÜ˜S›ˆŒØ"ˆŒØ*ˆÔØ *ˆÔØ$.ˆÔ!ØˆŒØˆr)   )Úupdate_data_lengthÚnew_dirsrJ   Nc                óX  — | j                  «        t        t        | j                  «      j	                  «       «      }|t
        t
        dz    t        k7  rt        d| j                  › �«      ‚t        j                  d|t        «      \  }t        j                  d|t        «      \  }g }|D ]?  }|j                  t        |«      «       |j                  |j                  d«      dz   «       ŒA t        |«      |k(  r4t!        |«      D ]#  \  }}	t        j"                  d|||dz  z   |	«       Œ% |}
nxt        |«      }
|D ]'  }	|j                  t        j$                  d|	«      «       Œ) t        j"                  d|t        t        |«      «       t        j"                  d|t        |
«       |r%t        j"                  d|t&        t        |«      «       t        | j                  «      j)                  |«       t+        |«      | _        |
| _        t        |«      | _        |rt        |«      | _        yy)a  Replace materialDirectories[] with new_dirs.

        Appends each new string at EOF. When `len(new_dirs) == dir_count`, the
        existing int32 offset table is overwritten in place. When the lengths
        differ (growing or shrinking), a fresh int32 table is appended at EOF
        and the header's count + offset fields are updated to point at it; the
        old table bytes become orphaned.

        Args:
            new_dirs: Replacement material directories. Length may differ from
                the existing materialDirectories[].
            update_data_length: Whether to update the dataLength header field
                to match the new file size. Defaults to True.

        Raises:
            ValueError: If the file is no longer a valid MDL.
        r   r,   r-   Úasciió    N)Ú_ensure_parsedÚ	bytearrayr   r   r.   r/   r0   r1   r2   r3   r;   r<   r?   r@   ÚextendÚencodeÚ	enumerateÚ	pack_intoÚpackr8   Úwrite_bytesÚlistr!   r#   r   r   )r&   rK   rJ   rA   rD   rE   Únew_offsetsÚsrF   Únew_offÚnew_dir_offsets              r'   Úrewrite_material_dirszMDLFile.rewrite_material_dirst   sÀ  € ð. 	×ÑÔÜœ˜TŸ]™]Ó+×6Ñ6Ó8Ó9ˆàŒw”w ‘{Ð#¤wÒ.ÜÐ0°·±°Ð@ÓAÐAä×)Ñ)¨$°Ô5LÓM‰ˆÜ×*Ñ*¨4°Ô6NÓO‰ˆà!#ˆÛˆAØ×Ñœs 3›xÔ(Ø�J‰J�q—x‘x Ó(¨7Ñ2Õ3ð ô ˆx‹=˜IÒ%Ü'¨Ö4‘
��7Ü× Ñ   s¨J¸¸Q¹Ñ,>ÀÕHð 5à'‰Nä  ›XˆNÛ&�Ø—
‘
œ6Ÿ;™; t¨WÓ5Õ6ð 'ä×Ñ˜T 3Ô(?ÄÀXÃÔOÜ×Ñ˜T 3Ô(@À.ÔQáÜ×Ñ˜T 3Ô(8¼#¸c»(ÔCäˆT�]‰]Ó×'Ñ'¨Ô,ä! (›^ˆÔØ$2ˆÔ!Ü˜S›ˆŒÙÜ" 3›xˆDÕð r)   Únew_materialsc                ój  — | j                  «        t        t        | j                  «      j	                  «       «      }|t
        t
        dz    t        k7  rt        d| j                  › �«      ‚t        j                  d|t        «      \  }t        j                  d|t        «      \  }t        |«      |k7  rt        dt        |«      › d|› �«      ‚t        |«      D ]Z  \  }}||t        z  z   }t        |«      }	|j                  |j!                  d«      dz   «       t        j"                  d|||	|z
  «       Œ\ |r%t        j"                  d|t$        t        |«      «       t        | j                  «      j'                  |«       t)        |«      | _        t        |«      | _        |rt        |«      | _        yy)	a.  Replace per-material name strings in place.

        Appends each new string at EOF and rewrites each material entry's
        nameIndex (int32, relative to that entry's own start). No existing
        bytes are shifted, so all other offsets in the file remain valid.

        Useful when relocating model materials on disk: some MDLs are
        compiled with `$cdmaterials` blank and the full path baked into the
        material name itself (yielding an empty material_dir entry paired
        with a path-rooted name like `models/foo/bar`). Moving the on-disk
        folder requires re-prefixing those names too.

        Args:
            new_materials: Replacement material name strings. Must have the
                same length as the existing materials[].
            update_data_length: Whether to update the dataLength header field
                to match the new file size. Defaults to True.

        Raises:
            ValueError: If new_materials length doesn't match the existing
                count, or if the file is no longer a valid MDL.
        r   r,   r-   znew_materials has z entries but MDL has rM   rN   N)rO   rP   r   r   r.   r/   r0   r1   r2   r3   r9   r:   r@   rS   r>   rQ   rR   rT   r8   rV   rW   r    r   r   )
r&   r]   rJ   rA   rB   rC   rF   r   rG   rZ   s
             r'   Úrewrite_materialszMDLFile.rewrite_materials¯   sv  € ð8 	×ÑÔÜœ˜TŸ]™]Ó+×6Ñ6Ó8Ó9ˆàŒw”w ‘{Ð#¤wÒ.ÜÐ0°·±°Ð@ÓAÐAä×)Ñ)¨$°Ô5HÓI‰ˆÜ×*Ñ*¨4°Ô6JÓK‰ˆäˆ}Ó Ò*ÜØ$¤S¨Ó%7Ð$8Ð8MÈiÈ[ÐYóð ô ! Ö/‰GˆAˆtØ" QÔ)=Ñ%=Ñ=ˆIÜ˜#“hˆGØ�J‰J�t—{‘{ 7Ó+¨gÑ5Ô6Ü×Ñ˜T 3¨	°7¸YÑ3FÕGð	 0ñ Ü×Ñ˜T 3Ô(8¼#¸c»(ÔCäˆT�]‰]Ó×'Ñ'¨Ô,ä˜mÓ,ˆŒÜ˜S›ˆŒÙÜ" 3›xˆDÕð r)   c                 ó>   — | j                   s| j                  «        y y )N)r$   r%   )r&   s    r'   rO   zMDLFile._ensure_parsedé   s   € Ø�|Š|Ø�J‰J�Lð r)   )T)r*   r   )r*   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úboolr(   r%   r   r\   r_   rO   © r)   r'   r   r      s„   „ ññ&  s¨D yÑ!1ð ¸tó ó2ðp $(ò	9(à�s‘)ð9(ð !ð	9(ð
 
ó9(ð~ $(ò	8(à˜C‘yð8(ð !ð	8(ð
 
ó8(ôtr)   r   )r2   Úpathlibr   Útypingr   r   Ú_ior   r   r0   r=   r5   r/   r4   r6   r7   r8   r9   r:   r;   r<   r>   r   rf   r)   r'   Ú<module>rj      sl   ðÛ Ý ß ç 3ð
 €Ù˜B “mÐ ð €Ø€Ø€Ø€	ØÐ ØÐ ØÐ ØÐ ØÐ ð Ð ÷Nò Nr)   