1
+ // See https://aka.ms/new-console-template for more information
2
+
3
+ using System ;
4
+ using System . IO ;
5
+ using System . IO . Compression ;
6
+ using System . Linq ;
7
+ using System . Net . Http ;
8
+
9
+
10
+ namespace LordFanger ;
11
+ public class RimCzechGitDownloader
12
+ {
13
+ private class Expansion
14
+ {
15
+ private readonly string _rimBase ;
16
+ private readonly string _name ;
17
+ private readonly string _checkPart ;
18
+ private readonly string _baseDir ;
19
+ private readonly string _languageDir ;
20
+ private readonly bool _exists ;
21
+
22
+ public Expansion ( string rimBase , string name ) {
23
+ _rimBase = rimBase ;
24
+ _name = name ;
25
+ _checkPart = $ "/{ name } /";
26
+ _baseDir = Path . Combine ( _rimBase , "Data" , name , "Languages" ) ;
27
+ _languageDir = Path . Combine ( _baseDir , "Czech - git" ) ;
28
+ _exists = Directory . Exists ( _baseDir ) ;
29
+ }
30
+
31
+ public bool Exists => _exists ;
32
+
33
+ public string LanguageDir => _languageDir ;
34
+
35
+ public bool IsForExpansion ( string fullName ) => fullName . Contains ( _checkPart , StringComparison . OrdinalIgnoreCase ) ;
36
+
37
+ public string GetPathFor ( string [ ] parts ) {
38
+ return Path . Combine ( new [ ] { _languageDir } . Concat ( parts . SkipWhile ( s => ! s . Equals ( _name , StringComparison . OrdinalIgnoreCase ) ) . Skip ( 1 ) ) . ToArray ( ) ) ;
39
+ }
40
+
41
+ public static Expansion [ ] GetExpansions ( string rimBase , out Expansion core ) {
42
+ core = new Expansion ( rimBase , "Core" ) ;
43
+ return new Expansion [ ] {
44
+ core ,
45
+ new Expansion ( rimBase , "Royalty" ) ,
46
+ new Expansion ( rimBase , "Ideology" ) ,
47
+ new Expansion ( rimBase , "Biotech" ) ,
48
+ } ;
49
+ }
50
+ }
51
+
52
+ private static string Version => "1.4" ;
53
+
54
+ private static void Main ( string [ ] args ) {
55
+ Console . WriteLine ( $ "Rim Czech Git Downloader - v { Version } ") ;
56
+ Console . WriteLine ( "Hledám instalaci RimWorldu..." ) ;
57
+ if ( args ? . Length > 0 )
58
+ {
59
+ SafeExecute ( ( ) => CheckForRimworldFolder ( args [ 0 ] ) ) ;
60
+ }
61
+ SafeExecute ( ( ) => CheckForRimworldFolder ( @".\" ) ) ;
62
+ SafeExecute ( ( ) => CheckForSteamFolder ( @"C:\Program Files\Steam\steamapps" ) ) ;
63
+ SafeExecute ( ( ) => CheckForSteamFolder ( @"C:\Program Files (x86)\Steam\steamapps" ) ) ;
64
+
65
+ var drives = DriveInfo . GetDrives ( ) ;
66
+ foreach ( var drive in drives )
67
+ {
68
+ Console . WriteLine ( drive . RootDirectory ) ;
69
+ CheckFolder ( drive . RootDirectory . FullName , 0 ) ;
70
+ }
71
+
72
+ void CheckFolder ( string path , int level ) {
73
+ var directoryName = Path . GetFileName ( path ) ;
74
+ if ( directoryName == "steamapps" )
75
+ {
76
+ CheckForSteamFolder ( path ) ;
77
+ }
78
+
79
+ if ( level > 3 ) return ;
80
+
81
+ SafeExecute (
82
+ ( ) => {
83
+ var subDirectories = Directory . GetDirectories ( path ) ;
84
+ foreach ( var subDirectory in subDirectories )
85
+ {
86
+ SafeExecute ( ( ) => CheckFolder ( subDirectory , level + 1 ) ) ;
87
+ }
88
+ } ) ;
89
+ }
90
+
91
+ void CheckForSteamFolder ( string path ) {
92
+ SafeExecute (
93
+ ( ) => {
94
+ var rimFolder = Path . Combine ( path , "common" , "RimWorld" ) ;
95
+ CheckForRimworldFolder ( rimFolder ) ;
96
+ } ) ;
97
+ }
98
+
99
+ void SafeExecute ( Action action ) {
100
+ try
101
+ {
102
+ action ( ) ;
103
+ } catch { }
104
+ }
105
+
106
+ void CheckForRimworldFolder ( string rimFolder ) {
107
+ if ( ! CheckRimworldFolder ( rimFolder , out var expansions ) ) return ;
108
+
109
+ Console . WriteLine ( "Nalezena instalace RimWorldu." ) ;
110
+ Console . WriteLine ( rimFolder ) ;
111
+ ProcessRimworld ( expansions ) ;
112
+ }
113
+
114
+ bool CheckRimworldFolder ( string rimFolder , out Expansion [ ] expansions ) {
115
+ expansions = null ;
116
+ if ( ! Directory . Exists ( rimFolder ) ) return false ;
117
+
118
+ expansions = Expansion . GetExpansions ( rimFolder , out var core ) ;
119
+ if ( ! core . Exists ) return false ;
120
+ return true ;
121
+ }
122
+
123
+ void ProcessRimworld ( Expansion [ ] expansions ) {
124
+ try
125
+ {
126
+ Console . WriteLine ( "Stahují aktuální překlad češtiny." ) ;
127
+ var zipStream = GetGitZip ( ) ;
128
+ using var zipArchive = new ZipArchive ( zipStream ) ;
129
+
130
+ Console . WriteLine ( "Mažu původní překlad." ) ;
131
+ foreach ( var expansion in expansions )
132
+ {
133
+ if ( ! expansion . Exists ) continue ;
134
+ if ( Directory . Exists ( expansion . LanguageDir ) ) Directory . Delete ( expansion . LanguageDir ) ;
135
+ }
136
+
137
+ Console . WriteLine ( "Kopíruji aktuální překlad češtiny do hry." ) ;
138
+ foreach ( var entry in zipArchive . Entries )
139
+ {
140
+ if ( string . IsNullOrEmpty ( entry . Name ) ) continue ;
141
+ var fullName = entry . FullName ;
142
+ var parts = fullName . Split ( '/' , StringSplitOptions . RemoveEmptyEntries ) ;
143
+
144
+ foreach ( var expansion in expansions )
145
+ {
146
+ if ( ! expansion . IsForExpansion ( fullName ) ) continue ;
147
+ var path = expansion . GetPathFor ( parts ) ;
148
+ CopyToFile ( entry , path ) ;
149
+ Console . WriteLine ( path ) ;
150
+ break ;
151
+ }
152
+ }
153
+
154
+ Console . WriteLine ( "Aktuální překlad do češtiny nakopírován." ) ;
155
+ Environment . Exit ( 0 ) ;
156
+
157
+ } catch ( Exception ex )
158
+ {
159
+ Console . WriteLine ( $ "{ ex . GetType ( ) } : { ex . Message } ") ;
160
+ Console . WriteLine ( ex . StackTrace ) ;
161
+ Environment . Exit ( 1 ) ;
162
+ }
163
+
164
+ void CopyToFile ( ZipArchiveEntry entry , string path ) {
165
+ var directory = Path . GetDirectoryName ( path ) ;
166
+ if ( directory != null && ! Directory . Exists ( directory ) ) Directory . CreateDirectory ( directory ) ;
167
+ entry . ExtractToFile ( path ) ;
168
+ }
169
+ }
170
+
171
+ Stream GetGitZip ( ) {
172
+ using var httpClient = new HttpClient ( ) ;
173
+ var response = httpClient . Send ( new HttpRequestMessage ( HttpMethod . Get , @"https://github.com/Ludeon/RimWorld-Czech/archive/refs/heads/master.zip" ) ) ;
174
+ using var data = response . Content . ReadAsStream ( ) ;
175
+ var ms = new MemoryStream ( ) ;
176
+ data . CopyTo ( ms ) ;
177
+ ms . Position = 0 ;
178
+ return ms ;
179
+ }
180
+ }
181
+ }
0 commit comments