11using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Threading ;
26
37namespace InEngine . Core . IO
48{
59 public class Write : IWrite
610 {
11+ static Mutex consoleOutputLock = new Mutex ( ) ;
12+ static Mutex fileOutputLock = new Mutex ( ) ;
13+
714 public ConsoleColor InfoColor { get ; set ; } = ConsoleColor . Green ;
815 public ConsoleColor WarningColor { get ; set ; } = ConsoleColor . Yellow ;
916 public ConsoleColor ErrorColor { get ; set ; } = ConsoleColor . Red ;
1017 public ConsoleColor LineColor { get ; set ; } = ConsoleColor . White ;
18+ public List < string > Buffer { get ; set ; } = new List < string > ( ) ;
1119
1220 public IWrite Newline ( int count = 1 )
1321 {
@@ -38,9 +46,12 @@ public IWrite Line(string val)
3846
3947 public IWrite ColoredLine ( string val , ConsoleColor consoleColor )
4048 {
49+ consoleOutputLock . WaitOne ( ) ;
4150 Console . ForegroundColor = consoleColor ;
4251 Console . WriteLine ( val ) ;
4352 Console . ResetColor ( ) ;
53+ consoleOutputLock . ReleaseMutex ( ) ;
54+ Buffer . Add ( val ) ;
4455 return this ;
4556 }
4657
@@ -66,10 +77,32 @@ public IWrite Text(string val)
6677
6778 public IWrite ColoredText ( string val , ConsoleColor consoleColor )
6879 {
80+ consoleOutputLock . WaitOne ( ) ;
6981 Console . ForegroundColor = consoleColor ;
7082 Console . Write ( val ) ;
7183 Console . ResetColor ( ) ;
84+ consoleOutputLock . ReleaseMutex ( ) ;
85+ Buffer . Add ( val ) ;
7286 return this ;
7387 }
88+
89+ public string FlushBuffer ( )
90+ {
91+ var str = string . Join ( "\n " , Buffer ) ;
92+ Buffer . Clear ( ) ;
93+ return str ;
94+ }
95+
96+ public void ToFile ( string path , string text , bool shouldAppend = false )
97+ {
98+ fileOutputLock . WaitOne ( ) ;
99+ if ( ! File . Exists ( path ) )
100+ File . Create ( path ) ;
101+ if ( shouldAppend )
102+ File . AppendAllText ( path , text ) ;
103+ else
104+ File . WriteAllText ( path , text ) ;
105+ fileOutputLock . ReleaseMutex ( ) ;
106+ }
74107 }
75108}
0 commit comments