Skip to content

Commit dde21ac

Browse files
committed
[FlashpointManager] Attempt to fix configuration file if invalid
1 parent 58168f6 commit dde21ac

3 files changed

Lines changed: 70 additions & 27 deletions

File tree

FlashpointManager/src/Common.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ public static void SyncManager(bool init = false)
330330
{
331331
string localHash = File.ReadLines(component.InfoFile).First().Split(' ')[0];
332332
outdated = localHash != component.Hash && component.ID != "core-database";
333-
334-
if (outdated) MessageBox.Show(localHash + " " + component.Hash);
335333
}
336334
else if (component.Required)
337335
{
@@ -468,7 +466,7 @@ public static void DeleteFileAndDirectories(string file)
468466
}
469467

470468
// Checks if specified Flashpoint source path is valid
471-
public static void VerifySourcePath()
469+
public static bool VerifySourcePath()
472470
{
473471
bool isFlashpoint = false;
474472

@@ -482,15 +480,28 @@ public static void VerifySourcePath()
482480
}
483481
});
484482

485-
if (!isFlashpoint)
483+
return isFlashpoint;
484+
}
485+
486+
// Writes new values to the configuration file
487+
public static bool WriteConfig(string path, string source)
488+
{
489+
try
490+
{
491+
File.WriteAllLines("fpm.cfg", new[] { path, source });
492+
}
493+
catch
486494
{
487495
MessageBox.Show(
488-
"The Flashpoint directory specified in fpm.cfg is invalid!",
496+
"Could not write to configuration file (fpm.cfg)." +
497+
"Make sure it is not being used by another program.",
489498
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
490499
);
491500

492-
Environment.Exit(1);
501+
return false;
493502
}
503+
504+
return true;
494505
}
495506

496507
// Checks if any dependencies were not marked for download by the user, and marks them accordingly

FlashpointManager/src/Forms/Settings.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,7 @@ private void BrowseButton_Click(object sender, EventArgs e)
4646

4747
private void SaveButton_Click(object sender, EventArgs e)
4848
{
49-
try
50-
{
51-
File.WriteAllLines("fpm.cfg", new[] { LocationBox.Text, RepositoryBox.Text });
52-
}
53-
catch
54-
{
55-
MessageBox.Show(
56-
"Could not write to configuration file (fpm.cfg)." +
57-
"Make sure it is not being used by another program.",
58-
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
59-
);
60-
61-
return;
62-
}
49+
if (!FPM.WriteConfig(LocationBox.Text, RepositoryBox.Text)) return;
6350

6451
Application.Restart();
6552
}

FlashpointManager/src/Program.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Xml;
88

99
using FlashpointManager.Common;
10+
using Microsoft.WindowsAPICodePack.Dialogs;
1011

1112
namespace FlashpointManager
1213
{
@@ -63,26 +64,70 @@ static void Main(string[] args)
6364
}
6465
}
6566

67+
bool updateConfig = false;
68+
6669
// Download and parse component list
67-
try
70+
while (true)
6871
{
69-
var listStream = new MemoryStream(new WebClient().DownloadData(FPM.RepoXml)) { Position = 0 };
72+
MemoryStream listStream = null;
73+
74+
try
75+
{
76+
listStream = new MemoryStream(new WebClient().DownloadData(FPM.RepoXml)) { Position = 0 };
77+
}
78+
catch
79+
{
80+
bool suggestReset = FPM.RepoXml != FPM.RepoXmlTemplates.Stable;
81+
82+
var errorDialog = MessageBox.Show(
83+
"The component list could not be downloaded!\n\n" +
84+
"Verify that your internet connection is working. " + (suggestReset ?
85+
"If it is, the component source may be misconfigured. Click OK to switch to the default component source."
86+
: ""), "Error", suggestReset ? MessageBoxButtons.OKCancel : MessageBoxButtons.OK, MessageBoxIcon.Error
87+
);
88+
89+
if (suggestReset && errorDialog == DialogResult.OK)
90+
{
91+
FPM.RepoXml = FPM.RepoXmlTemplates.Stable;
92+
updateConfig = true;
93+
94+
continue;
95+
}
96+
97+
Environment.Exit(1);
98+
}
7099

71100
FPM.XmlTree = new XmlDocument();
72101
FPM.XmlTree.Load(listStream);
102+
103+
break;
73104
}
74-
catch
105+
106+
// Verify that the configured Flashpoint path is valid
107+
while (!FPM.VerifySourcePath())
75108
{
76109
MessageBox.Show(
77-
"The component list could not be downloaded! Do you have an internet connection?",
110+
"The Flashpoint directory specified in fpm.cfg is invalid!\n\n" +
111+
"Please choose a valid directory.",
78112
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
79113
);
80114

81-
Environment.Exit(1);
115+
var pathDialog = new CommonOpenFileDialog() { IsFolderPicker = true };
116+
117+
if (pathDialog.ShowDialog() == CommonFileDialogResult.Cancel)
118+
{
119+
Environment.Exit(1);
120+
}
121+
122+
FPM.SourcePath = pathDialog.FileName;
123+
updateConfig = true;
82124
}
83125

84-
// Verify that the configured Flashpoint path is valid
85-
FPM.VerifySourcePath();
126+
// Write new values to configuration file if needed
127+
if (updateConfig && !FPM.WriteConfig(FPM.SourcePath, FPM.RepoXml))
128+
{
129+
Environment.Exit(1);
130+
}
86131

87132
if (args.Length > 0)
88133
{

0 commit comments

Comments
 (0)