2626using System . Linq ;
2727using System . Diagnostics ;
2828using System . Threading ;
29+ using System . ComponentModel ;
2930
3031namespace ORTS
3132{
3233 public partial class DownloadContentForm : Form
3334 {
34- private readonly GettextResourceManager Catalog = new GettextResourceManager ( "Menu" ) ;
35+ private readonly GettextResourceManager Catalog ;
3536 private readonly UserSettings Settings ;
3637 private readonly IDictionary < string , RouteSettings . Route > Routes ;
3738
@@ -41,35 +42,23 @@ public DownloadContentForm(UserSettings settings)
4142 {
4243 InitializeComponent ( ) ;
4344
45+ Catalog = new GettextResourceManager ( "Menu" ) ;
4446 Settings = settings ;
45- Routes = settings . Routes . Routes ;
4647
48+ Settings . Routes . LoadContentAndInstalled ( ) ;
49+ Routes = settings . Routes . Routes ;
4750 for ( int index = 0 ; index < Routes . Count ; index ++ )
4851 {
4952 string routeName = Routes . ElementAt ( index ) . Key ;
5053 RouteSettings . Route route = Routes . ElementAt ( index ) . Value ;
5154 dataGridViewDownloadContent . Rows . Add ( new string [ ] { routeName , route . DateInstalled , route . Url } ) ;
5255 }
5356
54- Add ( "OR CPV" , "https://github.com/cpvirtual/OR_CPV.git" , "" ) ;
55- Add ( "NewForest Route V3" , "https://github.com/rickloader/NewForestRouteV3.git" , "" ) ;
56- Add ( "MidEast Coast" , "https://github.com/MECoast/MECoast.git" , "" ) ;
57- Add ( "Demo Model 1" , "https://github.com/cjakeman/Demo-Model-1.git" , "Demo Model 1" ) ;
58- Add ( "Chiltern Route V2" , "https://github.com/DocMartin7644/Chiltern-Route-v2.git" , "" ) ;
57+ dataGridViewDownloadContent . Sort ( dataGridViewDownloadContent . Columns [ 0 ] , ListSortDirection . Ascending ) ;
5958
6059 InstallPathTextBox . Text = settings . Content . InstallPath ;
6160 }
6261
63- public void Add ( string routeName , string url , string subDirectory )
64- {
65- RouteSettings . Route route = Routes . ContainsKey ( routeName ) ? Routes [ routeName ] : null ;
66- if ( route == null )
67- {
68- dataGridViewDownloadContent . Rows . Add ( new string [ ] { routeName , "" , url } ) ;
69- Routes . Add ( routeName , new RouteSettings . Route ( "" , url , subDirectory ) ) ;
70- }
71- }
72-
7362 void dataGridViewDownloadContent_SelectionChanged ( object sender , EventArgs e )
7463 {
7564 RouteName = dataGridViewDownloadContent . CurrentRow . Cells [ 0 ] . Value . ToString ( ) ;
@@ -82,7 +71,7 @@ private void InstallPathButton_Click(object sender, EventArgs e)
8271 using ( FolderBrowserDialog folderBrowser = new FolderBrowserDialog ( ) )
8372 {
8473 folderBrowser . SelectedPath = InstallPathTextBox . Text ;
85- folderBrowser . Description = "Main Path where route is to be installed" ;
74+ folderBrowser . Description = Catalog . GetString ( "Main Path where route is to be installed" ) ;
8675 folderBrowser . ShowNewFolderButton = true ;
8776 if ( folderBrowser . ShowDialog ( this ) == DialogResult . OK )
8877 {
@@ -141,10 +130,6 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
141130
142131 Settings . Content . InstallPath = installPath ;
143132
144- // check if filesystem is case sensitive
145- // ok, this check will be wrong if both upper- and lowercase named route directories exist
146- bool directoryCaseInsensitive = Directory . Exists ( installPathRoute . ToUpper ( ) ) && Directory . Exists ( installPathRoute . ToLower ( ) ) ;
147-
148133 // set json route filename
149134
150135 Settings . Content . RouteJsonName = Path . Combine ( installPath , "ORRoute.json" ) ;
@@ -156,58 +141,16 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
156141 dataGridViewDownloadContent . CurrentRow . Cells [ 1 ] . Value = Catalog . GetString ( "Installing..." ) ;
157142 Refresh ( ) ;
158143
159- // actual download
160-
161144 if ( ! downloadRoute ( installPathRoute ) )
162145 {
163146 return ;
164147 }
165148
166149 // insert row in Options, tab Content
167150
168- if ( ! string . IsNullOrWhiteSpace ( Routes [ RouteName ] . SubDirectory ) )
169- {
170- installPathRoute = Path . Combine ( installPathRoute , Routes [ RouteName ] . SubDirectory ) ;
171- }
172-
173- bool updated = false ;
174- int index = 0 ;
175- while ( ! updated )
151+ if ( ! insertRowInOptions ( installPathRoute ) )
176152 {
177- string routeName = "" ;
178- bool routeNameFound = false ;
179- foreach ( KeyValuePair < string , string > folderSetting in Settings . Folders . Folders )
180- {
181- if ( index == 0 ) {
182- routeName = RouteName ;
183- } else
184- {
185- routeName = string . Format ( "{0} ({1})" , RouteName , index ) ;
186- }
187- if ( folderSetting . Key == routeName ) {
188- if ( folderSetting . Value . Equals ( installPathRoute ,
189- directoryCaseInsensitive ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ) )
190- {
191- updated = true ;
192- }
193- else
194- {
195- routeNameFound = true ;
196- }
197- }
198- }
199- if ( ! updated )
200- {
201- if ( routeNameFound )
202- {
203- index ++ ;
204- }
205- else
206- {
207- Settings . Folders . Folders [ routeName ] = installPathRoute ;
208- updated = true ;
209- }
210- }
153+ return ;
211154 }
212155
213156 string dateTimeNowStr = DateTime . Now . ToString ( CultureInfo . CurrentCulture . DateTimeFormat ) ;
@@ -275,14 +218,92 @@ private void sumMB(string path)
275218 {
276219 foreach ( string fileName in Directory . GetFiles ( path ) )
277220 {
278- TotalBytes += new System . IO . FileInfo ( fileName ) . Length ;
221+ TotalBytes += new FileInfo ( fileName ) . Length ;
279222 }
280223
281224 foreach ( string directoryName in Directory . GetDirectories ( path ) )
282225 {
283226 sumMB ( directoryName ) ;
284227 }
285228 }
229+
230+ private bool insertRowInOptions ( string installPathRoute )
231+ {
232+ // check if filesystem is case sensitive
233+ // ok, this check will be wrong if both upper- and lowercase named route directories exist
234+ bool directoryCaseInsensitive = Directory . Exists ( installPathRoute . ToUpper ( ) ) && Directory . Exists ( installPathRoute . ToLower ( ) ) ;
235+
236+ // sometimes the route is located one directory level deeper, determine real installPathRoute
237+ string installPathRouteReal ;
238+
239+ if ( Directory . Exists ( Path . Combine ( installPathRoute , "routes" ) ) ) {
240+ installPathRouteReal = installPathRoute ;
241+ }
242+ else
243+ {
244+ string [ ] directories = Directory . GetDirectories ( installPathRoute ) ;
245+ int indexDirectories = 0 ;
246+ while ( ( indexDirectories < directories . Length ) &&
247+ ! Directory . Exists ( Path . Combine ( directories [ indexDirectories ] , "routes" ) ) )
248+ {
249+ indexDirectories ++ ;
250+ }
251+ if ( indexDirectories < directories . Length )
252+ {
253+ installPathRouteReal = Path . Combine ( installPathRoute , directories [ indexDirectories ] ) ;
254+ }
255+ else
256+ {
257+ string message = Catalog . GetString ( "Incorrect route, directory \" routes\" not found" ) ;
258+ MessageBox . Show ( message , Catalog . GetString ( "Attention" ) , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
259+ return false ;
260+ }
261+ }
262+
263+ bool updated = false ;
264+ int index = 0 ;
265+ while ( ! updated )
266+ {
267+ string routeName = "" ;
268+ bool routeNameFound = false ;
269+ foreach ( KeyValuePair < string , string > folderSetting in Settings . Folders . Folders )
270+ {
271+ if ( index == 0 )
272+ {
273+ routeName = RouteName ;
274+ }
275+ else
276+ {
277+ routeName = string . Format ( "{0} ({1})" , RouteName , index ) ;
278+ }
279+ if ( folderSetting . Key == routeName )
280+ {
281+ if ( folderSetting . Value . Equals ( installPathRouteReal ,
282+ directoryCaseInsensitive ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ) )
283+ {
284+ updated = true ;
285+ }
286+ else
287+ {
288+ routeNameFound = true ;
289+ }
290+ }
291+ }
292+ if ( ! updated )
293+ {
294+ if ( routeNameFound )
295+ {
296+ index ++ ;
297+ }
298+ else
299+ {
300+ Settings . Folders . Folders [ routeName ] = installPathRouteReal ;
301+ updated = true ;
302+ }
303+ }
304+ }
305+ return true ;
306+ }
286307 }
287308
288309}
0 commit comments