/// <summary> /// コンストラクタ /// </summary> protected InfernoScript() { Interval = 16; //初期化をちょっと遅延させる Observable.Interval(TimeSpan.FromMilliseconds(10)) .Where(_ => InfernoCore.Instance != null) .First() .Subscribe(_ => { InfernoCore.Instance.PedsNearPlayer.Subscribe(x => _cachedPeds = x); InfernoCore.Instance.VehicleNearPlayer.Subscribe(x => _cachedVehicles = x); InfernoCore.Instance.PlayerPed.Subscribe(x => cahcedPlayerPed = x); InfernoCore.Instance.PlayerVehicle.Subscribe(x => PlayerVehicle.Value = x); }); OnTickAsObservable = Observable.FromEventPattern <EventHandler, EventArgs>(h => h.Invoke, h => Tick += h, h => Tick -= h) .Select(_ => Unit.Default).Share(); //Subscribeされたらイベントハンドラを登録する OnThinnedTickAsObservable = OnTickAsObservable.ThrottleFirst(TimeSpan.FromMilliseconds(100), InfernoScriptScheduler) .Share(); OnDrawingTickAsObservable = DrawingCore.OnDrawingTickAsObservable; OnAllOnCommandObservable = CreateInputKeywordAsObservable("allon"); //スケジューラ実行 OnTickAsObservable.Subscribe(_ => infernoScheduler?.Run()); // SynchronizationContextの実行 OnTickAsObservable .Subscribe(_ => { if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(InfernoSynchronizationContext); } InfernoSynchronizationContext.Update(); }); //タイマのカウント OnThinnedTickAsObservable .Where(_ => _counterList.Any()) .Subscribe(_ => { foreach (var c in _counterList) { c.Update(100); } //完了状態にあるタイマを全て削除 _counterList.RemoveAll(x => x.IsCompleted); }); _coroutinePool = new CoroutinePool(5); //コルーチンを実行する CreateTickAsObservable(TimeSpan.FromMilliseconds(_coroutinePool.ExpectExecutionInterbalMillSeconds)) .Subscribe(_ => _coroutinePool.Run()); OnAbortAsync.Subscribe(_ => { IsActive = false; foreach (var e in _autoReleaseEntities.Where(x => x.IsSafeExist())) { e.MarkAsNoLongerNeeded(); } _autoReleaseEntities.Clear(); }); try { Setup(); } catch (Exception e) { LogWrite(e.ToString()); } }
/// <summary> /// 任意のTickObservableを生成する /// <returns></returns> protected UniRx.IObservable <Unit> CreateTickAsObservable(TimeSpan timeSpan) { return(OnTickAsObservable.ThrottleFirst(timeSpan, InfernoScriptScheduler).Share()); }