1+ using System ;
2+ using Popstation ;
3+
4+ namespace PSXPackager
5+ {
6+ public class ConsoleNotifier : INotifier
7+ {
8+ private int _cursorYPos ;
9+ private long _total ;
10+ private long _lastTicks ;
11+ private int _charsToDelete ;
12+ private DateTime _startDateTime ;
13+ private readonly int _logLevel ;
14+
15+ public ConsoleNotifier ( int logLevel )
16+ {
17+ _logLevel = logLevel ;
18+ }
19+
20+ private int GetLogLevel ( PopstationEventEnum @event )
21+ {
22+ switch ( @event )
23+ {
24+ case PopstationEventEnum . ProcessingStart :
25+ case PopstationEventEnum . ProcessingComplete :
26+ return - 1 ;
27+
28+ case PopstationEventEnum . Error :
29+ return - 1 ;
30+
31+ case PopstationEventEnum . Warning :
32+ return 1 ;
33+
34+ case PopstationEventEnum . Info :
35+ return 3 ;
36+
37+
38+ case PopstationEventEnum . FileName :
39+ return 0 ;
40+
41+ case PopstationEventEnum . GetIsoSize :
42+ case PopstationEventEnum . ConvertSize :
43+ case PopstationEventEnum . ExtractSize :
44+ case PopstationEventEnum . WriteSize :
45+ return - 1 ;
46+
47+ case PopstationEventEnum . ConvertStart :
48+ case PopstationEventEnum . WriteStart :
49+ case PopstationEventEnum . ExtractStart :
50+ case PopstationEventEnum . DecompressStart :
51+
52+ case PopstationEventEnum . ConvertProgress :
53+ case PopstationEventEnum . ExtractProgress :
54+ case PopstationEventEnum . WriteProgress :
55+ case PopstationEventEnum . DecompressProgress :
56+
57+ case PopstationEventEnum . ConvertComplete :
58+ case PopstationEventEnum . ExtractComplete :
59+ case PopstationEventEnum . WriteComplete :
60+ case PopstationEventEnum . DecompressComplete :
61+ return 2 ;
62+ }
63+ return - 1 ;
64+ }
65+
66+ public void Notify ( PopstationEventEnum @event , object value )
67+ {
68+ if ( GetLogLevel ( @event ) > _logLevel ) return ;
69+
70+ switch ( @event )
71+ {
72+ case PopstationEventEnum . ProcessingStart :
73+ _startDateTime = DateTime . Now ;
74+ WriteLine ( @event , $ "Processing started: { _startDateTime . Hour : 00} :{ _startDateTime . Minute : 00} :{ _startDateTime . Second : 00} ") ;
75+ break ;
76+
77+ case PopstationEventEnum . ProcessingComplete :
78+ var elapsedSpan = DateTime . Now - _startDateTime ;
79+ WriteLine ( @event , $ "Processing completed: { elapsedSpan . TotalHours : 00} h { elapsedSpan . Minutes : 00} m { elapsedSpan . Seconds : 00} s") ;
80+ break ;
81+
82+ case PopstationEventEnum . Error :
83+ _charsToDelete = 0 ;
84+ Console . CursorVisible = true ;
85+ WriteLine ( @event , $ "\r \n { value } ") ;
86+ break ;
87+ case PopstationEventEnum . FileName :
88+ case PopstationEventEnum . Info :
89+ WriteLine ( @event , $ "{ value } ") ;
90+ break ;
91+ case PopstationEventEnum . Warning :
92+ var lastColor = Console . ForegroundColor ;
93+ Console . ForegroundColor = ConsoleColor . DarkYellow ;
94+ WriteLine ( @event , $ "WARNING: { value } ") ;
95+ Console . ForegroundColor = lastColor ;
96+ break ;
97+
98+ case PopstationEventEnum . GetIsoSize :
99+ _total = Convert . ToInt64 ( value ) ;
100+ break ;
101+ case PopstationEventEnum . ConvertSize :
102+ case PopstationEventEnum . ExtractSize :
103+ case PopstationEventEnum . WriteSize :
104+ _total = Convert . ToInt64 ( value ) ;
105+ break ;
106+
107+ case PopstationEventEnum . ConvertStart :
108+ Write ( @event , $ "Converting Disc { value } - ") ;
109+ _cursorYPos = Console . CursorTop ;
110+ Console . CursorVisible = false ;
111+ break ;
112+ case PopstationEventEnum . WriteStart :
113+ Write ( @event , $ "Writing Disc { value } - ") ;
114+ _cursorYPos = Console . CursorTop ;
115+ Console . CursorVisible = false ;
116+ break ;
117+ case PopstationEventEnum . ExtractStart :
118+ Write ( @event , $ "Extracting Disc { value } - ") ;
119+ _cursorYPos = Console . CursorTop ;
120+ Console . CursorVisible = false ;
121+ break ;
122+ case PopstationEventEnum . DecompressStart :
123+ Write ( @event , $ "Decompressing file { value } - ") ;
124+ _cursorYPos = Console . CursorTop ;
125+ Console . CursorVisible = false ;
126+ break ;
127+ case PopstationEventEnum . ConvertComplete :
128+ case PopstationEventEnum . ExtractComplete :
129+ case PopstationEventEnum . WriteComplete :
130+ case PopstationEventEnum . DecompressComplete :
131+ _charsToDelete = 0 ;
132+ Console . CursorVisible = true ;
133+ Console . WriteLine ( ) ;
134+ break ;
135+
136+ case PopstationEventEnum . ConvertProgress :
137+ case PopstationEventEnum . ExtractProgress :
138+ case PopstationEventEnum . WriteProgress :
139+ //Console.SetCursorPosition(0, _cursorYPos);
140+ if ( DateTime . Now . Ticks - _lastTicks > 100000 )
141+ {
142+ Overwrite ( $ "{ Math . Round ( Convert . ToInt32 ( value ) / ( double ) _total * 100 , 0 ) } %") ;
143+ _lastTicks = DateTime . Now . Ticks ;
144+ }
145+ break ;
146+ case PopstationEventEnum . DecompressProgress :
147+ //Console.SetCursorPosition(0, _cursorYPos);
148+ if ( DateTime . Now . Ticks - _lastTicks > 100000 )
149+ {
150+ Overwrite ( $ "{ value } %") ;
151+ _lastTicks = DateTime . Now . Ticks ;
152+ }
153+ break ;
154+ }
155+ }
156+
157+ private string TimeStamp ( PopstationEventEnum @event )
158+ {
159+ if ( _logLevel < 4 ) return string . Empty ;
160+ var currentTime = DateTime . Now ;
161+ switch ( @event )
162+ {
163+ case PopstationEventEnum . ConvertProgress :
164+ case PopstationEventEnum . ExtractProgress :
165+ case PopstationEventEnum . WriteProgress :
166+ case PopstationEventEnum . DecompressProgress :
167+ break ;
168+ default :
169+ return $ "[{ currentTime . Hour : 00} :{ currentTime . Minute : 00} :{ currentTime . Second : 00} ]: ";
170+ }
171+ return string . Empty ;
172+ }
173+
174+ private void WriteLine ( PopstationEventEnum @event , string text )
175+ {
176+ Console . WriteLine ( TimeStamp ( @event ) + text ) ;
177+ }
178+
179+ private void Write ( PopstationEventEnum @event , string text )
180+ {
181+ Console . Write ( TimeStamp ( @event ) + text ) ;
182+ }
183+
184+ private void Overwrite ( string text )
185+ {
186+ Console . Write ( new string ( '\b ' , _charsToDelete ) ) ;
187+ Console . Write ( text ) ;
188+ _charsToDelete = text . Length ;
189+ }
190+
191+ }
192+ }
0 commit comments