A game about forced loneliness, made by TACStudios
1using System;
2
3using Codice.Client.Common.Threading;
4
5namespace Unity.PlasticSCM.Editor.UI
6{
7 internal class UnityThreadWaiterBuilder : IThreadWaiterBuilder
8 {
9 IThreadWaiter IThreadWaiterBuilder.GetWaiter()
10 {
11 return new UnityThreadWaiter(mPlasticTimerBuilder, false);
12 }
13
14 IThreadWaiter IThreadWaiterBuilder.GetWaiter(int timerIntervalMilliseconds)
15 {
16 return new UnityThreadWaiter(mPlasticTimerBuilder, false, timerIntervalMilliseconds);
17 }
18
19 IThreadWaiter IThreadWaiterBuilder.GetModalWaiter()
20 {
21 return new UnityThreadWaiter(mPlasticTimerBuilder, true);
22 }
23
24 IThreadWaiter IThreadWaiterBuilder.GetModalWaiter(int timerIntervalMilliseconds)
25 {
26 return new UnityThreadWaiter(mPlasticTimerBuilder, true, timerIntervalMilliseconds);
27 }
28
29 IPlasticTimer IThreadWaiterBuilder.GetTimer(
30 int timerIntervalMilliseconds, ThreadWaiter.TimerTick timerTickDelegate)
31 {
32 return mPlasticTimerBuilder.Get(false, timerIntervalMilliseconds, timerTickDelegate);
33 }
34
35 static IPlasticTimerBuilder mPlasticTimerBuilder = new UnityPlasticTimerBuilder();
36 }
37
38 internal class UnityThreadWaiter : IThreadWaiter
39 {
40 Exception IThreadWaiter.Exception { get { return mThreadOperation.Exception; } }
41
42 internal UnityThreadWaiter(
43 IPlasticTimerBuilder timerBuilder, bool bModalMode)
44 {
45 mPlasticTimer = timerBuilder.Get(bModalMode, OnTimerTick);
46 }
47
48 internal UnityThreadWaiter(
49 IPlasticTimerBuilder timerBuilder,
50 bool bModalMode,
51 int timerIntervalMilliseconds)
52 {
53 mPlasticTimer = timerBuilder.Get(bModalMode, timerIntervalMilliseconds, OnTimerTick);
54 }
55
56 void IThreadWaiter.Execute(
57 PlasticThread.Operation threadOperationDelegate,
58 PlasticThread.Operation afterOperationDelegate)
59 {
60 ((IThreadWaiter)(this)).Execute(threadOperationDelegate, afterOperationDelegate, null);
61 }
62
63 void IThreadWaiter.Execute(
64 PlasticThread.Operation threadOperationDelegate,
65 PlasticThread.Operation afterOperationDelegate,
66 PlasticThread.Operation timerTickDelegate)
67 {
68 mThreadOperation = new PlasticThread(threadOperationDelegate);
69 mAfterOperationDelegate = afterOperationDelegate;
70 mTimerTickDelegate = timerTickDelegate;
71
72 mPlasticTimer.Start();
73
74 mThreadOperation.Execute();
75 }
76
77 void IThreadWaiter.Cancel()
78 {
79 mbCancelled = true;
80 }
81
82 void OnTimerTick()
83 {
84 if (mThreadOperation.IsRunning)
85 {
86 if (mTimerTickDelegate != null)
87 EditorDispatcher.Dispatch(() => mTimerTickDelegate());
88
89 return;
90 }
91
92 mPlasticTimer.Stop();
93
94 if (mbCancelled)
95 return;
96
97 EditorDispatcher.Dispatch(() => mAfterOperationDelegate());
98 }
99
100 bool mbCancelled = false;
101
102 IPlasticTimer mPlasticTimer;
103 PlasticThread mThreadOperation;
104 PlasticThread.Operation mTimerTickDelegate;
105 PlasticThread.Operation mAfterOperationDelegate;
106 }
107}