1+ namespace OpenSWF2EXE
2+ {
3+ public static class Config
4+ {
5+ private static string configPath ;
6+
7+ static Config ( )
8+ {
9+ configPath = Path . Combine ( new string [ ] { Environment . CurrentDirectory , "s2e_config.txt" } ) ;
10+ if ( ! File . Exists ( configPath ) )
11+ {
12+ FileStream fileStream = File . Create ( configPath ) ;
13+ fileStream . Close ( ) ;
14+ }
15+ }
16+
17+ public static int GetLanguage ( )
18+ {
19+ string value = GetString ( "language" , "2" ) ;
20+ return int . Parse ( value ) ;
21+ }
22+
23+ public static void SetLanguage ( int value )
24+ {
25+ SetString ( "language" , value . ToString ( ) ) ;
26+ }
27+
28+ public static string GetString ( string key , string defaultVal )
29+ {
30+ string [ ] lines = File . ReadAllLines ( configPath ) ;
31+
32+ foreach ( string line in lines )
33+ {
34+ int signIndex = line . IndexOf ( '=' ) ;
35+ if ( signIndex == - 1 ) continue ;
36+
37+ string keyStr = line . Substring ( 0 , signIndex ) ;
38+ string keyStrWithoutSpace = keyStr . Trim ( ) ;
39+
40+ //return first key
41+ if ( keyStrWithoutSpace . Equals ( key ) )
42+ {
43+ string valueStr = line . Substring ( signIndex + 1 , line . Length - 1 - keyStr . Length ) ;
44+ string valueStrWithoutSpace = valueStr . Trim ( ) ;
45+ return valueStrWithoutSpace ;
46+ }
47+ }
48+
49+ return defaultVal ;
50+ }
51+
52+ public static void SetString ( string key , string value )
53+ {
54+ string [ ] lines = File . ReadAllLines ( configPath ) ;
55+
56+ //If the file is empty, just write!
57+ if ( lines . Length == 0 || ( lines . Length == 1 && string . IsNullOrWhiteSpace ( lines [ 0 ] ) ) )
58+ {
59+ string content = key + " = " + value ;
60+ File . WriteAllText ( configPath , content ) ;
61+ return ;
62+ }
63+
64+ bool keyExists = false ;
65+ bool needToWrite = false ;
66+
67+ for ( int i = 0 ; i < lines . Length ; i ++ )
68+ {
69+ string line = lines [ i ] ;
70+
71+ int signIndex = line . IndexOf ( '=' ) ;
72+ if ( signIndex == - 1 ) continue ;
73+
74+ string keyStr = line . Substring ( 0 , signIndex ) ;
75+ string keyStrWithoutSpace = keyStr . Trim ( ) ;
76+ //Find first key
77+ if ( keyStrWithoutSpace . Equals ( key ) )
78+ {
79+ keyExists = true ;
80+ string valueStr = line . Substring ( signIndex + 1 , line . Length - 1 - keyStr . Length ) ;
81+ string valueStrWithoutSpace = valueStr . Trim ( ) ;
82+ //If value changed
83+ if ( valueStrWithoutSpace != value )
84+ {
85+ needToWrite = true ;
86+ lines [ i ] = keyStrWithoutSpace + " = " + value ;
87+ }
88+ break ;
89+ }
90+ }
91+
92+ if ( needToWrite )
93+ {
94+ File . WriteAllLines ( configPath , lines ) ;
95+ }
96+ else if ( ! keyExists )
97+ {
98+ string newLine = "\n " + key + " = " + value ;
99+ List < string > newLines = new List < string > ( ) ;
100+ newLines . AddRange ( lines ) ;
101+ newLines . Add ( newLine ) ;
102+ File . WriteAllLines ( configPath , newLines . ToArray ( ) ) ;
103+ }
104+ }
105+ }
106+ }
0 commit comments