Skip to content

Commit 772f381

Browse files
Don't crash and burn when downloading a server resource fails.
1 parent e0b213a commit 772f381

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

fCraft/System/Server.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -461,35 +461,33 @@ public static bool StartServer() {
461461
}
462462

463463
static void DownloadResources() {
464-
if (!Directory.Exists("./Bot")) {
465-
Directory.CreateDirectory("./Bot");
466-
}
467-
if (!File.Exists("Bot/Funfacts.txt")) {
468-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Funfacts.txt", "Bot/Funfacts.txt");
469-
}
470-
if (!File.Exists("Bot/Jokes.txt")) {
471-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Jokes.txt", "Bot/Jokes.txt");
472-
}
473-
if (!File.Exists("Bot/Protips.txt")) {
474-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Protips.txt", "Bot/Protips.txt");
475-
}
476-
if (!File.Exists("Bot/Adjectives.txt")) {
477-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Adjectives.txt", "Bot/Adjectives.txt");
478-
}
479-
if (!File.Exists("Bot/Nouns.txt")) {
480-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Nouns.txt", "Bot/Nouns.txt");
481-
}
482-
483-
if (!File.Exists("./MOTDList.txt")) {
484-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/MOTDList.txt", "MOTDList.txt");
485-
}
486-
487-
if (!Directory.Exists("./fonts")) {
488-
Directory.CreateDirectory("./fonts");
489-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/comicsans.ttf", "fonts/comicsans.ttf");
490-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/mcclassic.ttf", "fonts/mcclassic.ttf");
491-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/microsoft.ttf", "fonts/microsoft.ttf");
492-
(new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/minecraft.ttf", "fonts/minecraft.ttf");
464+
if (!Directory.Exists("./Bot")) Directory.CreateDirectory("./Bot");
465+
if (!Directory.Exists("./fonts")) Directory.CreateDirectory("./fonts");
466+
467+
DownloadResource("http://123dmwm.tk/ProCraft/resources/Funfacts.txt", "./Bot/Funfacts.txt");
468+
DownloadResource("http://123dmwm.tk/ProCraft/resources/Jokes.txt", "./Bot/Jokes.txt");
469+
DownloadResource("http://123dmwm.tk/ProCraft/resources/Protips.txt", "./Bot/Protips.txt");
470+
DownloadResource("http://123dmwm.tk/ProCraft/resources/Adjectives.txt", "./Bot/Adjectives.txt");
471+
DownloadResource("http://123dmwm.tk/ProCraft/resources/Nouns.txt", "./Bot/Nouns.txt");
472+
473+
DownloadResource("http://123dmwm.tk/ProCraft/resources/MOTDList.txt", "./MOTDList.txt");
474+
DownloadResource("http://123dmwm.tk/ProCraft/resources/comicsans.ttf", "./fonts/comicsans.ttf");
475+
DownloadResource("http://123dmwm.tk/ProCraft/resources/mcclassic.ttf", "./fonts/mcclassic.ttf");
476+
DownloadResource("http://123dmwm.tk/ProCraft/resources/microsoft.ttf", "./fonts/microsoft.ttf");
477+
DownloadResource("http://123dmwm.tk/ProCraft/resources/minecraft.ttf", "./fonts/minecraft.ttf");
478+
}
479+
480+
static void DownloadResource(string url, string file) {
481+
if (File.Exists(file)) return;
482+
483+
try {
484+
new WebClient().DownloadFile(url, file);
485+
} catch (WebException ex) {
486+
WebResponse resp = ex.Response;
487+
if (resp != null) resp.Close();
488+
489+
Logger.Log(LogType.Warning, "Error downloading resource {0}: {1}",
490+
file, ex);
493491
}
494492
}
495493

@@ -1095,8 +1093,8 @@ public static string GetRandomString( int chars ) {
10951093
[CanBeNull]
10961094
static IPAddress CheckExternalIP()
10971095
{
1098-
HttpWebRequest request = HttpUtil.CreateRequest(IPCheckUri,
1099-
TimeSpan.FromMilliseconds(IPCheckTimeout));
1096+
HttpWebRequest request = HttpUtil.CreateRequest(IPCheckUri,
1097+
TimeSpan.FromMilliseconds(IPCheckTimeout));
11001098
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
11011099

11021100
try

0 commit comments

Comments
 (0)