Skip to content

Commit 86ce21e

Browse files
committed
[FlashpointManager] Verify that component list is valid XML before overwriting backup
1 parent 91c45ed commit 86ce21e

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

FlashpointManager/src/Common.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static class FPM
144144
// Pointer to main form
145145
public static Main Main { get => (Main)Application.OpenForms["Main"]; }
146146
// The parsed component list XML
147-
public static XmlDocument XmlTree { get; set; }
147+
public static XmlDocument XmlTree { get; } = new XmlDocument();
148148

149149
// Name of configuration file
150150
public static string ConfigFile { get; set; } = "fpm.cfg";

FlashpointManager/src/Program.cs

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,66 +66,53 @@ static void Main(string[] args)
6666
}
6767
}
6868

69-
// Download and parse component list
70-
while (true)
69+
Stream listStream = null;
70+
71+
// Attempt to download and parse component list
72+
try
73+
{
74+
listStream = new MemoryStream(new WebClient().DownloadData(FPM.RepoXml));
75+
FPM.XmlTree.Load(listStream);
76+
}
77+
catch (Exception e)
7178
{
72-
byte[] listData = new byte[] { };
79+
string failedAction = e is XmlException ? "parsed" : "downloaded";
7380

74-
try
75-
{
76-
listData = new WebClient().DownloadData(FPM.RepoXml);
77-
}
78-
catch
79-
{
80-
FPM.GenericError(
81-
"The component list could not be downloaded! An offline backup will be used instead.\n\n" +
82-
"Verify that your internet connection is working and that your component source is not misconfigured."
83-
);
81+
FPM.GenericError(
82+
$"The component list could not be {failedAction}! An offline backup will be used instead.\n\n" +
83+
"Verify that your internet connection is working and that your component source is not misconfigured."
84+
);
8485

85-
FPM.OfflineMode = true;
86-
}
86+
FPM.OfflineMode = true;
87+
}
8788

88-
Stream listStream = null;
89-
string backupPath = Path.Combine(FPM.SourcePath, "Components", "components.bak");
89+
string backupPath = Path.Combine(FPM.SourcePath, "Components", "components.bak");
9090

91-
if (!FPM.OfflineMode)
92-
{
93-
try
94-
{
95-
listStream = File.Create(backupPath);
96-
listStream.Write(listData, 0, listData.Length);
97-
}
98-
catch
99-
{
100-
listStream = new MemoryStream(listData);
101-
}
102-
}
103-
else
91+
// Create backup if component list was successfully downloaded and parsed; otherwise, load from backup
92+
if (!FPM.OfflineMode)
93+
{
94+
try
10495
{
105-
try
106-
{
107-
listStream = new FileStream(backupPath, FileMode.Open, FileAccess.Read);
108-
}
109-
catch
96+
using (var fileStream = new FileStream(backupPath, FileMode.OpenOrCreate, FileAccess.Write))
11097
{
111-
FPM.GenericError("Failed to load component list from offline backup!");
112-
Environment.Exit(1);
98+
listStream.Position = 0;
99+
listStream.CopyTo(fileStream);
113100
}
114101
}
115-
116-
listStream.Position = 0;
117-
102+
catch { }
103+
}
104+
else
105+
{
118106
try
119107
{
120-
FPM.XmlTree = new XmlDocument();
108+
listStream = new FileStream(backupPath, FileMode.Open, FileAccess.Read);
121109
FPM.XmlTree.Load(listStream);
122110
}
123111
catch
124112
{
125-
FPM.ParseError("The XML markup is malformed.");
113+
FPM.GenericError("Failed to load component list from offline backup!");
114+
Environment.Exit(1);
126115
}
127-
128-
break;
129116
}
130117

131118
// Verify that the configured Flashpoint path is valid

0 commit comments

Comments
 (0)