<commit_before>﻿using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public struct RecordState
{
    private readonly Vector3 _position;
    private readonly Quaternion _rotation;

    public RecordState(Vector3 position, Quaternion rotation)
    {
        _position = position;
        _rotation = rotation;
    }

    public Vector3 Position
    {
        get { return _position; }
    }

    public Quaternion Rotation
    {
        get { return _rotation; }
    }
}

public class Recorder : MonoBehaviour
{
    private readonly Dictionary<int, RecordState> _states = new Dictionary<int, RecordState>();
    private RecordState _previousState;
    private PhotonView _photonView;

    //Animator m_Animator;
    private Rigidbody _rigidbody;


    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
        _photonView = GetComponent<PhotonView>();
        Register();
        DoOnTick(0);
    }

    private void Register()
    {
        var mainRecorder = FindObjectOfType<MainRecorder>();
        if (!mainRecorder) return;
        mainRecorder.OnTick.Suscribe(DoOnTick);
        mainRecorder.OnRewind.Suscribe(DoOnRewind);
    }

    private RecordState FindClosestState(int key)
    {
        var keys = new List<int>(_states.Keys);
        var index = keys.BinarySearch(key);
        //~ = Bitwise NOT
        index = ~index - 1;
        if (index < 0) index = 0;
        if (!_states.ContainsKey(index))
            Debug.Log("Using previous state, Dictionnary did not contain proper key : " + index +
                      " Dictionnary count : " + _states.Count);
        return !_states.ContainsKey(index) ? _previousState : _states[index];
    }

    private int DoOnTick(int time)
    {
        var curState = new RecordState(transform.position, transform.rotation);
        if (curState.Equals(_previousState)) return 0;
        _previousState = curState;
        _states[time] = new RecordState(transform.position, transform.rotation);
        return 0;
    }

    private int DoOnRewind(int time)
    {
        _photonView.RPC("DoRewind", PhotonTargets.All, time);
        return 0;
    }

    [PunRPC]
    private void DoRewind(int time)
    {
        if (_states.ContainsKey(time))
        {
            PlayState(_states[time]);<commit_msg>Prevent executing stuff that doesn't exists anymore.<commit_after>﻿using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public struct RecordState
{
    private readonly Vector3 _position;
    private readonly Quaternion _rotation;

    public RecordState(Vector3 position, Quaternion rotation)
    {
        _position = position;
        _rotation = rotation;
    }

    public Vector3 Position
    {
        get { return _position; }
    }

    public Quaternion Rotation
    {
        get { return _rotation; }
    }
}

public class Recorder : MonoBehaviour
{
    private readonly Dictionary<int, RecordState> _states = new Dictionary<int, RecordState>();
    private RecordState _previousState;
    private PhotonView _photonView;

    //Animator m_Animator;
    private Rigidbody _rigidbody;


    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
        _photonView = GetComponent<PhotonView>();
        Register();
        DoOnTick(0);
    }

    private void Register()
    {
        var mainRecorder = FindObjectOfType<MainRecorder>();
        if (!mainRecorder) return;
        mainRecorder.OnTick.Suscribe(DoOnTick);
        mainRecorder.OnRewind.Suscribe(DoOnRewind);
    }

    private RecordState FindClosestState(int key)
    {
        var keys = new List<int>(_states.Keys);
        var index = keys.BinarySearch(key);
        //~ = Bitwise NOT
        index = ~index - 1;
        if (index < 0) index = 0;
        if (!_states.ContainsKey(index))
            Debug.Log("Using previous state, Dictionnary did not contain proper key : " + index +
                      " Dictionnary count : " + _states.Count);
        return !_states.ContainsKey(index) ? _previousState : _states[index];
    }

    private int DoOnTick(int time)
    {
        if (this == null) return 0;
        var curState = new RecordState(transform.position, transform.rotation);
        if (curState.Equals(_previousState)) return 0;
        _previousState = curState;
        _states[time] = new RecordState(transform.position, transform.rotation);
        return 0;
    }

    private int DoOnRewind(int time)
    {
        _photonView.RPC("DoRewind", PhotonTargets.All, time);
        return 0;
    }

    [PunRPC]
    private void DoRewind(int time)
    {
        if (this == null) return;
        if (_states.ContainsKey(time))
        {
            PlayState(_states[time]);
{'fraction_non_alphanumeric': 0.09366391184573003, 'fraction_numerical': 0.004450095359186269, 'mean_word_length': 2.504083147735709, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4661509', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1341, 'n_words': 332}