a
    VŸ$c‚~  ã                   @   sZ  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlm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Zd dlZd dlZd dlZd dlZG dd„ de ƒZ!e!ƒ Z"G dd„ de ƒZ#G d	d
„ d
e#ƒZ$G dd„ dej%j&ƒZ'e(ƒ a)dd„ Z*dd„ Z+e ¡ Z,d a-ej% .¡ Z/G dd„ dej j0ƒZ1G dd„ dej j0ƒZ2dS )é    )ÚdivisionÚabsolute_importÚwith_statementÚprint_functionÚunicode_literals)ÚPY2Ú
basestringÚbchrÚbordÚchrÚopenÚpystrÚrangeÚroundÚstrÚtobytesÚunicode)ÚOptionalNc                   @   s   e Zd Zdd„ ZdS )ÚStoreDeletedc                 C   s   t rdS dS d S )Ns   deletedÚdeleted)r   ©Úself© r   úrenpy/rollback.pyÚ
__reduce__1   s    zStoreDeleted.__reduce__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   /   s   r   c                   @   s   e Zd ZdZdZdS )ÚSlottedNoRollbackaº  
    :doc: norollback class

    Instances of classes inheriting from this class do not participate
    in rollback. The difference between this and :class:`NoRollback` is that
    this class does not have an associated dictionary, hence can be used
    with ``__slots__`` to reduce memory usage.

    Objects reachable through an instance of a NoRollback class only participate
    in rollback if they are reachable through other paths.
    r   N)r   r   r   Ú__doc__Ú	__slots__r   r   r   r   r   ?   s   r   c                   @   s   e Zd ZdZdS )Ú
NoRollbacka  
    :doc: norollback class

    Instances of this class, and classes inheriting from this class,
    do not participate in rollback. Objects reachable through an instance
    of a NoRollback class only participate in rollback if they are
    reachable through other paths.
    N)r   r   r   r   r   r   r   r   r!   N   s   r!   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚAlwaysRollbackz¿
    This is a revertible object that always participates in rollback.
    It's used when a revertable object is created by an object that
    doesn't participate in the rollback system.
    c                    s0   t t| ƒ | ¡}tjj}|d ur,|jt|ƒ= |S ©N)Úsuperr"   Ú__new__ÚrenpyÚgameÚlogÚmutatedÚid)ÚclsÚargsÚkwargsr   r(   ©Ú	__class__r   r   r%   a   s
    zAlwaysRollback.__new__)r   r   r   r   r%   Ú__classcell__r   r   r.   r   r"   Z   s   r"   c                 C   sJ  |r
|ƒ  t | ƒ}||v rdS | ||< t| tƒr4dS zbt| ddƒ}|durvt| ƒ ¡ D ]\}}||vrVt|||ƒ qVnt| ƒ ¡ D ]}t|||ƒ q‚W n ty¨   Y n0 zt	| ƒr¾t| t
ƒrÄW dS W n tyÚ   Y dS 0 z|  ¡ D ]}t|||ƒ qæW n ty   Y n0 z |  ¡ D ]}t|||ƒ qW n tyD   Y n0 dS )zÖ
    @param obj: The object that was reached.

    `reachable`
        A map from id(obj) to int. The int is 1 if the object was reached
        normally, and 0 if it was reached, but inherits from NoRollback.
    NÚnosave)r*   Ú
isinstanceÚNOROLLBACK_TYPESÚgetattrÚvarsÚitemsÚreachedÚvaluesÚ	ExceptionÚlenr   Ú__iter__)ÚobjÚ	reachableÚwaitÚidobjr1   ÚkÚvr   r   r   r7   n   sB    	

r7   c                 C   sn   |   ¡ D ]}t|||ƒ qtjjD ]F}t|j||ƒ t|j||ƒ |jD ]}|  ¡ D ]}t|||ƒ qTqHq"dS )a  
    Marks everything reachable from the variables in the store
    or from the context info objects as reachable.

    `store`
        A map from variable name to variable value.

    `reachable`
        A dictionary that will be filled in with a map from id(obj) to obj.
    N)r8   r7   r&   r'   ÚcontextsÚinfoÚmusicÚdynamic_stack)Ústorer=   r>   rA   ÚcÚdr   r   r   Úreached_vars¬   s    
rI   c                       sL   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Z‡  ZS )ÚRollbacka<  
    Allows the state of the game to be rolled back to the point just
    before a node began executing.

    @ivar context: A shallow copy of the context we were in before
    we started executing the node. (Shallow copy also includes
    a copy of the associated SceneList.)

    @ivar objects: A list of tuples, each containing an object and a
    token of information that, when passed to the rollback method on
    that object, causes that object to rollback.

    @ivar store: A list of updates to store that will cause the state
    of the store to be rolled back to the start of node
    execution. This is a list of tuples, either (key, value) tuples
    representing a value that needs to be assigned to a key, or (key,)
    tuples that mean the key should be deleted.

    @ivar checkpoint: True if this is a user-visible checkpoint,
    false otherwise.

    @ivar purged: True if purge_unreachable has already been called on
    this Rollback, False otherwise.

    @ivar random: A list of random numbers that were generated during the
    execution of this element.
    é   NFc                    sp   t t| ƒ ¡  tj ¡  ¡ | _g | _d| _g | _	d | _
i | _i | _d| _d| _d| _d| _ttf| _td7 ad S )NFé   )r$   rJ   Ú__init__r&   r'   ÚcontextÚrollback_copyÚobjectsÚpurgedÚrandomÚforwardÚstoresÚ	delta_ebcÚretain_after_loadÚ
checkpointÚhard_checkpointÚ
not_greedyÚ
generationÚserialÚ
identifierr   r.   r   r   rM   í   s    
zRollback.__init__c                 C   s†   |dk rVdi i| _ | jD ]<}t|ƒdkr@|\}}|| j d |< q|\}t| j d |< q|dk rdd| _|dk rt| j| _|dk r‚i | _d S )Né   rF   é   Fé   rK   )rT   rF   r:   r   rV   rW   rX   rU   )r   ÚversionÚir@   rA   r   r   r   Úafter_upgrade  s    

zRollback.after_upgradec                 C   s&  | j r
dS d| _ | j ¡ D ]*}| ¡ D ]\}}|tur&t|||ƒ q&qt| jj||ƒ | jjD ]}| ¡ D ]}t|||ƒ qjq^t| jj	 
¡ ||ƒ g }d}tƒ }	|r
d}| jD ]V\}
}t|
ƒ}||	v s°||vrÒq°|	 |¡ t|
tƒrèq°d}| |
|f¡ t|||ƒ q°q | jdd…= | j |¡ dS )aT  
        Adds objects that are reachable from the store of this
        rollback to the set of reachable objects, and purges
        information that is stored about totally unreachable objects.

        Returns True if this is the first time this method has been
        called, or False if it has already been called once before.
        FTN)rQ   rT   r8   r6   r   r7   rN   rC   rE   Úscene_listsÚget_all_displayablesÚsetrP   r*   Úaddr2   r3   ÚappendÚextend)r   r=   r>   ÚchangesÚ_krA   rH   Únew_objectsÚobjects_changedÚseenÚoÚrbÚid_or   r   r   Úpurge_unreachable)  s<    


zRollback.purge_unreachablec                 C   sî   t jj}t| jƒD ]\}}|dur| |¡ q| j ¡ D ]P\}}| |d¡}|du rVq8| ¡ D ](\}}|t	u r~||v r†||= q^|||< q^q8| j
 ¡ D ],\}}| |d¡}|du r²q”| j|8  _q”t | j¡ t jj ¡  t jj | j¡ dS )zo
        Reverts the state of the game to what it was at the start of the
        previous checkpoint.
        N)r&   ÚpythonÚstore_dictsÚreversedrP   Ú	_rollbackrT   r6   Úgetr   rU   Úever_been_changedÚrngÚpushbackrR   r'   rB   Úpoprg   rN   )r   rs   r<   ÚrollÚnameri   rF   Úvaluer   r   r   Úrollbackh  s*    zRollback.rollbackc                 C   s    t jj ¡  t jj | j¡ dS )zr
        This rolls back only the control information, while leaving
        the data information intact.
        N)r&   r'   rB   rz   rg   rN   r   r   r   r   Úrollback_controlŽ  s    zRollback.rollback_control)r   r   r   r   Ú__version__r\   rY   rM   rb   rq   r~   r   r0   r   r   r.   r   rJ   Ë   s   &?&rJ   c                       sü   e Zd ZdZdZg d¢ZdZdZ‡ fdd„Zdd	„ Z	d
d„ Z
d7dd„Zdd„ Zd8dd„Zdd„ Zd9dd„Zdd„ Zdd„ Zdd„ Zd:dd„Zdd „ Zd;d!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd<d+d,„Zd=d-d.„Zd/d0„ Zd>d1d2„Zd3d4„ Zd5d6„ Z‡  Z S )?ÚRollbackLoga­  
    This class manages the list of Rollback objects.

    @ivar log: The log of rollback objects.

    @ivar current: The current rollback object. (Equivalent to
    log[-1])

    @ivar rollback_limit: The number of steps left that we can
    interactively rollback.

    Not serialized:

    @ivar mutated: A dictionary that maps object ids to a tuple of
    (weakref to object, information needed to rollback that object)
    rK   )Ú	old_storer)   Úidentifier_cacheNFc                    sh   t t| ƒ ¡  g | _d | _i | _d| _d| _d| _d | _	g | _
i | _d| _t ¡  d| _d| _d| _d S )Nr   FT)r$   r   rM   r(   Úcurrentr)   Úrollback_limitÚrollback_is_fixedÚcheckpointing_suspendedÚfixed_rollback_boundaryrS   r‚   Úrolled_forwardrx   ÚresetÚretain_after_load_flagÚdid_interactionÚforce_checkpointr   r.   r   r   rM   °  s    zRollbackLog.__init__c                 C   s   i | _ d| _d S )NF)r)   r‰   r   r   r   r   Úafter_setstateÐ  s    zRollbackLog.after_setstatec                 C   s|   |dk rdt | jƒi| _|dk r,d| _d | _|dk r:d| _|dk rx| jrxd}| j| j d … D ]}|jr^|d7 }q^|| _d S )	Nr]   rF   r^   Fr_   rK   r   rL   )re   rw   r†   rˆ   r‹   r…   r(   rX   )r   r`   Únrblro   r   r   r   rb   Ô  s    
zRollbackLog.after_upgradec                 C   s(  d| _ tj ¡ }|jsdS d}|r(d}n0| jr4d}n$| jdurX| jjrLd}n| jjrXd}|r`dS d| _| jdur||  	d¡ n
tj
 ¡  t| jƒtjjkr¦| j d¡ q†t| jƒdkrÎ| jd jj| jkrÎd| _| jrì| jsì| jjj| _d| _tƒ | _| j| j_| j | j¡ | j ¡  dtj_d| _dS )z~
        Called before a node begins executing, to indicate that the
        state needs to be saved for rollbacking.
        NTFr   r]   éþÿÿÿ)rƒ   r&   r'   rN   r~   rŒ   r„   rW   rV   Úcompleterr   Úbegin_storesr:   r(   ÚconfigÚrollback_lengthrz   rˆ   r†   rS   rJ   r‹   rg   r)   ÚclearÚ
revertableÚmutate_flagr‰   )r   ÚforcerN   Úignorer   r   r   Úbeginê  sD    





zRollbackLog.beginc                 C   s   | j D ]}|j ||¡ qdS )zk
        Replaces references to the `old` ast node with a reference to the
        `new` ast node.
        N)r(   rN   Úreplace_node)r   ÚoldÚnewra   r   r   r   r›   *  s    
zRollbackLog.replace_nodec              	   C   sä   | j r| jdd d| _ tjj ¡ D ].\}}| |¡}|r$|\| jj|< | jj	|< q$t
dƒD ]‚}| jjdd…= z\| j ¡ D ]F\}}|du rŒqz|\}}	|ƒ }
|
du r¤qz|
 |	¡}| jj |
|f¡ qzW  qàW q\ tyÜ   Y q\0 q\dS )ap  
        Called after a node is finished executing, before a save
        begins, or right before a rollback is attempted. This may be
        called more than once between calls to begin, and should always
        be called after an update to the store but before a rollback
        occurs.

        `begin`
            Should be true if called from begin().
        F)Úhardr_   N)r   rW   r&   rr   rs   r6   Úget_changesr„   rT   rU   r   rP   r)   Ú	_compressrg   ÚRuntimeError)r   rš   r|   ÚsdÚdeltaÚ_irj   rA   ÚrefÚcleanr<   Ú
compressedr   r   r   r‘   3  s,    


zRollbackLog.completec                 C   s|   i }t jj ¡ D ]B\}}|jD ]2}||v r@|| ||d | < qt||d | < qqtt jjdd… ƒD ]}| 	|¡ qh|S )a  
        Return a map giving the current roots of the store. This is a
        map from a variable name in the store to the value of that
        variable. A variable is only in this map if it has ever been
        changed since the init phase finished.
        Ú.rL   N)
r&   rr   rs   r6   rw   r   rt   r'   rB   Úpop_dynamic_roots)r   ÚrvÚ
store_namer¢   r|   ra   r   r   r   Ú	get_rootsg  s    
zRollbackLog.get_rootsc                 C   sb   t jtjjttjtfa	i }t
|||ƒ | jdd… }| ¡  |D ]}| ||¡s@ qVq@| ¡  dS )a  
        This is called to purge objects that are unreachable from the
        roots from the object rollback lists inside the Rollback entries.

        This should be called immediately after complete(), so that there
        are no changes queued up.
        N)ÚtypesÚ
ModuleTyper&   rr   ÚStoreModuler   ÚioÚIOBaseÚtyper3   rI   r(   Úreverserq   r•   )r   Úrootsr>   r=   Úrevlogra   r   r   r   rq   }  s    zRollbackLog.purge_unreachablec                 C   s   | j r
dS dS d S )NTF)rS   r   r   r   r   Úin_rollback™  s    zRollbackLog.in_rollbackc                 C   s   | j S r#   )r†   r   r   r   r   Úin_fixed_rollbackŸ  s    zRollbackLog.in_fixed_rollbackc                 C   s*   | j r&| j d \}}| jjj|kr&|S dS )z;
        Returns the current forward info, if any.
        r   N)rS   r„   rN   )r   r|   Údatar   r   r   Úforward_info¢  s
    zRollbackLog.forward_infoTc                 C   s>  | j r
d}|rd| _| jjr dS tj ¡ js0dS d| j_|rz| jjsz| j	tj
jk r`|  j	d7  _	|dkrrd| j_n|| j_|  ¡ rÐ| jrÐ| jd \}}| jjj|krº|| j_| j d¡ n|| j_| jdd…= nj|dur:| jr2| jd \}}| jjj|kr&||kr&|s| jr&| j d¡ n| jdd…= || j_dS )z˜
        Called to indicate that this is a checkpoint, which means
        that the user may want to rollback to just before this
        node.
        FNTrL   rY   r   )r‡   r‹   r„   rW   r&   r'   rN   r~   rX   r…   r“   Úhard_rollback_limitrY   r·   rS   rz   r‰   )r   r¸   Úkeep_rollbackrž   Úfwd_nameÚfwd_datar   r   r   rW   °  sF    

ÿþþzRollbackLog.checkpointc                 C   s
   || _ dS )z{
        Called to temporarily suspend checkpointing, so any rollback
        will jump to prior to this statement
        N)r‡   )r   Úflagr   r   r   Úsuspend_checkpointingé  s    z!RollbackLog.suspend_checkpointingc                 C   s&   d| _ dtj ¡ _|r"| jdd…= dS )zr
        Called to indicate that the user should not be able to rollback
        through this checkpoint.
        r   TN)r…   r&   r'   rN   r   r(   )r   Úpurger   r   r   Úblockñ  s    zRollbackLog.blockc                 C   sL   t jjjrdS d| _d| j_t| jƒD ]}|j	r4 q<d|_q&dt j
 ¡ _dS )zv
        Called to return data from this statement until the next checkpoint
        when the game is loaded.
        NT)r&   ÚdisplayÚpredictÚ
predictingr‹   r„   rV   rt   r(   rX   r'   rN   r   )r   ro   r   r   r   rV   ý  s    
zRollbackLog.retain_after_loadc                 C   s4   | j s$t| jƒdkr$| jd jj| _dtj ¡ _d S )NrL   r   T)	r†   r:   r(   rN   r„   rˆ   r&   r'   r   r   r   r   r   Úfix_rollback  s    zRollbackLog.fix_rollbackc                 C   s
   | j dkS )z2
        Returns True if we can rollback.
        r   )r…   r   r   r   r   Úcan_rollback  s    zRollbackLog.can_rollbackc                 C   sx   t jj}t|ƒr|ƒ }|s"tdƒ‚| j ¡ }| ¡  t j 	¡ rJt j 
¡  q4dt jjd _t jjd  |¡ t j ¡ ‚dS )zG
        This is called to try to recover when rollback fails.
        z^Couldn't find a place to stop rolling back. Perhaps the script changed in an incompatible way?Tr   N)r&   r“   Úload_failed_labelÚcallabler9   r(   rz   r~   ÚexportsÚcall_stack_depthÚpop_callr'   rB   r   Ú
goto_labelÚRestartTopContext)r   Úlflro   r   r   r   Úload_failed  s    

zRollbackLog.load_failedc                 C   s”  |r| j dkr|sdS |  d¡ |  |  ¡ ¡ g }| jr–| j ¡ }	| |	¡ |	jr`|  j d8  _ |	jsp|rx|	jrx|d8 }|dkr2t	j
j |	jj¡r2qÄq2| ¡  | j |¡ |r¸|  ¡  ntdƒ dS d}
|r.| jr.| j dkr.| jd }	t	j
j |	jj¡sq.|	jrq.|	jrq.| | j ¡ ¡ qÈt	j
 ¡ jrFd}g }n(d}t	j
jdd… }t	j
jdd… t	j
_|r|d jr| ¡ }d| _nd}d}|dur²t	j
 ¡ j}|}|D ]R}	|	 ¡  |	jj| jkrâ|	jjrâd| _|	jdur¶| j d|	jj|	jf¡ q¶|dur(| ¡  | j |¡ |durH|du rHt	j
 ¡ j}|durdt	j
 ¡  ||¡ |t	j
j_|rˆt  !¡  | jdd…= |t	j
_"t	j#j# ¡  t	j
jD ]}|j$ %¡  q¤t	j
j |¡ t	j& 'd¡ | j( )¡  t	j* +¡  |r@|
rþdt	j
jd _,t-ƒ | _t	j
jd  .¡ | j_| jdur4| j | j¡ t	j
 /¡ ‚nPt-ƒ | _t	j
 ¡  .¡ | j_| jdurt| j | j¡ |
r†dt	j
 ¡ _,t	j
 0¡ ‚dS )aÑ  
        This rolls the system back to the first valid rollback point
        after having rolled back past the specified number of checkpoints.

        If we're currently executing code, it's expected that complete()
        will be called before a rollback is attempted.

        force makes us throw an exception if we can't find a place to stop
        rolling back, otherwise if we run out of log this call has no
        effect.

        `label`
            A label that is called after rollback has finished, if the
            label exists.

        `greedy`
            If true, rollback will keep going until just after the last
            checkpoint. If False, it will stop immediately before the
            current statement.

        `on_load`
            Should be true if this rollback is being called in response to a
            load. Used to implement .retain_after_load()

        `abnormal`
            If true, treats this as an abnormal event, suppressing transitions
            and so on.

        `current_label`
            A lable that is called when control returns to the current statement,
            after rollback. (At most one of `current_label` and `label` can be
            provided.)
        r   NFrL   z4Can't find a place to rollback to. Not rolling back.éÿÿÿÿT)1r…   r¿   rq   r¬   r(   rz   rg   rX   rW   r&   r'   ÚscriptÚ	has_labelrN   r„   r³   rh   rÏ   ÚprintrY   r~   rB   rV   r‹   rˆ   r†   rS   Úinsertr   Ú	come_fromÚ	interfaceÚsuppress_transitionrx   rŠ   Úafter_rollbackÚaudiorc   Úremove_all_hiddenrÉ   Úexecute_default_statementr)   r•   rr   r’   r   rJ   rO   rÍ   ÚRestartContext)r   Úcheckpointsr˜   ÚlabelÚgreedyÚon_loadÚabnormalÚcurrent_labelrµ   ro   r   Úreplace_contextÚother_contextsÚretainedrÕ   ra   r   r   r   r~   4  sª    %










zRollbackLog.rollbackc                 C   s,   |   d¡ |  ¡ }| j||d d| j_|S )a	  
        This is called to freeze the store and the log, in preparation
        for serialization. The next call on log should either be
        unfreeze (called after a serialization reload) or discard_freeze()
        (called after the save is complete).
        F)r>   )r‘   r¬   rq   r„   rQ   )r   r>   r´   r   r   r   Úfreezeð  s
    	
zRollbackLog.freezec                 C   s   dS )za
        Called to indicate that we will not be restoring from the
        frozen state.
        Nr   r   r   r   r   Údiscard_freeze  s    zRollbackLog.discard_freezec           	      C   sÄ   t jj ¡  | t j_t j ¡  t j 	¡  t jj
}| ¡ D ]d\}}d|v rZ| dd¡\}}nd}||vrhq8|| }|j |¡ |tu r”||v rœ||= q8|||< q8t j dd¡}| jdd||dd d	S )
a}  
        Used to unfreeze the game state after a load of this log
        object. This call will always throw an exception. If we're
        lucky, it's the one that indicates load was successful.

        @param roots: The roots returned from freeze.

        @param label: The label that is jumped to in the game script
        after rollback has finished, if it exists.
        r¨   rL   rF   Ú_greedy_rollbackFr   T)r˜   rÞ   rß   rà   N)r&   rÂ   ÚscreenÚbefore_restartr'   r(   rr   Úclean_storesÚtranslationÚinit_translationrs   r6   Úrsplitrw   rf   r   Úsessionrz   r~   )	r   r´   rÞ   rs   r|   r}   r«   rF   rß   r   r   r   Úunfreeze	  s&    


zRollbackLog.unfreezec                 C   s~   | j d urd S | j}d}i | _ t| jƒD ]P}|jd urTtjj |j	j
¡rT|| j |j< |jrb|d7 }|jrp|d8 }|s( qzq(d S )NrL   )rƒ   r…   rt   r(   r\   r&   r'   rÑ   rÒ   rN   r„   rX   rW   )r   r…   rÝ   ra   r   r   r   Úbuild_identifier_cache:  s    

z"RollbackLog.build_identifier_cachec                 C   s   |   ¡  | j |d ¡S r#   )rñ   rƒ   rv   )r   r\   r   r   r   Úget_identifier_checkpointsS  s    z&RollbackLog.get_identifier_checkpoints)F)F)N)NFT)F)FNTFTN)N)N)!r   r   r   r   r€   r1   rƒ   r   rM   rŽ   rb   rš   r›   r‘   r¬   rq   r¶   r·   r¹   rW   r¿   rÁ   rV   rÅ   rÆ   rÏ   r~   ræ   rç   rð   rñ   rò   r0   r   r   r.   r   r   ˜  s<    
@	
4

9

 =

1r   )3Ú
__future__r   r   r   r   r   Úrenpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   Útypingr   ÚmarshalrR   ÚweakrefÚreÚsysÚtimer°   r­   ÚcopyregÚ	functoolsr&   Úobjectr   r   r   r!   r–   ÚRevertableObjectr"   Útupler3   r7   rI   rZ   r[   Ú	DetRandomrx   ÚObjectrJ   r   r   r   r   r   Ú<module>   s6   8	>
 N