From 8c9400a3093bf353ea5a09b188f7c385352188ac Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Thu, 15 Jul 2021 10:26:35 +0100 Subject: [PATCH 1/2] New: Support URL based textures https://github.com/leezer3/OpenBVE/pull/670 --- source/Plugins/Object.CsvB3d/Plugin.Parser.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs index 4c8e9f0fd6..05a02893c0 100644 --- a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs +++ b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Net; using System.Text; using System.Text.RegularExpressions; using OpenBveApi; @@ -1161,6 +1162,63 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) { Builder.Materials[j].NighttimeTexture = tnight; } } break; + case "url": + string tUday = null, tUnight = null; + if (Arguments.Length >= 1 && !string.IsNullOrEmpty(Arguments[0])) + { + string tDayURL = Arguments[0]; + if (!Regex.IsMatch(tDayURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + { + //If no http, add it + tDayURL = "http://" + tDayURL; + } + + Uri textureUri; + if (Uri.TryCreate(tDayURL, UriKind.Absolute, out textureUri)) + { + using (MyWebClient downloadClient = new MyWebClient()) + { + string tempFile = System.IO.Path.GetTempFileName(); + downloadClient.DownloadFile(tDayURL, tempFile); + tUday = tempFile; + } + } + else + { + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + + } + if (Arguments.Length >= 2 && !string.IsNullOrEmpty(Arguments[1])) + { + string tNightURL = Arguments[1]; + if (!Regex.IsMatch(tNightURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + { + //If no http, add it + tNightURL = "http://" + tNightURL; + } + + Uri textureUri; + if (Uri.TryCreate(tNightURL, UriKind.Absolute, out textureUri)) + { + using (MyWebClient downloadClient = new MyWebClient()) + { + string tempFile = System.IO.Path.GetTempFileName(); + downloadClient.DownloadFile(tNightURL, tempFile); + tUnight = tempFile; + } + } + else + { + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + + } + for (int j = 0; j < Builder.Materials.Length; j++) { + Builder.Materials[j].DaytimeTexture = tUday; + Builder.Materials[j].NighttimeTexture = tUnight; + } + break; case "settext": case "text": { @@ -1569,5 +1627,18 @@ private static bool IsUtf(Encoding Encoding) } return false; } + + private class MyWebClient : WebClient + { + /* + * Minor helper class in order to set a 5s timeout, not the default 30s + */ + protected override WebRequest GetWebRequest(Uri uri) + { + WebRequest w = base.GetWebRequest(uri); + w.Timeout = 5000; + return w; + } + } } } From f21ef3d7f18d9fd5db7ca77026406d4fd95cf87a Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Fri, 16 Jul 2021 12:04:13 +0100 Subject: [PATCH 2/2] Change: Improve safety of URL based textures --- source/Plugins/Object.CsvB3d/Plugin.Parser.cs | 91 +++++++++++++------ 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs index 05a02893c0..cda4d0b0c0 100644 --- a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs +++ b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs @@ -1,16 +1,17 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; -using OpenBveApi; using OpenBveApi.Colors; using OpenBveApi.Interface; using OpenBveApi.Math; using OpenBveApi.Objects; using OpenBveApi.Textures; +using Path = OpenBveApi.Path; namespace Plugin { @@ -1167,50 +1168,82 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) { if (Arguments.Length >= 1 && !string.IsNullOrEmpty(Arguments[0])) { string tDayURL = Arguments[0]; - if (!Regex.IsMatch(tDayURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + try { - //If no http, add it - tDayURL = "http://" + tDayURL; - } - - Uri textureUri; - if (Uri.TryCreate(tDayURL, UriKind.Absolute, out textureUri)) - { - using (MyWebClient downloadClient = new MyWebClient()) + if (!Regex.IsMatch(tDayURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + { + //If no http, add it + tDayURL = "http://" + tDayURL; + } + + Uri textureUri; + if (Uri.TryCreate(tDayURL, UriKind.Absolute, out textureUri)) + { + using (MyWebClient downloadClient = new MyWebClient()) + { + string tempFile = System.IO.Path.GetTempFileName(); + downloadClient.DownloadFile(tDayURL, tempFile); + FileInfo fi = new FileInfo(tempFile); + if (fi.Length < 100) + { + //Zero byte / 404 text probably + currentHost.AddMessage(MessageType.Error, false, "Invalid texture downloaded in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + else + { + tUday = tempFile; + } + } + } + else { - string tempFile = System.IO.Path.GetTempFileName(); - downloadClient.DownloadFile(tDayURL, tempFile); - tUday = tempFile; + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); } } - else + catch { - currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + currentHost.AddMessage(MessageType.Error, false, "An unexpected error occured whilst attempting to download " + Arguments[0] + " in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); } } if (Arguments.Length >= 2 && !string.IsNullOrEmpty(Arguments[1])) { string tNightURL = Arguments[1]; - if (!Regex.IsMatch(tNightURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + try { - //If no http, add it - tNightURL = "http://" + tNightURL; - } - - Uri textureUri; - if (Uri.TryCreate(tNightURL, UriKind.Absolute, out textureUri)) - { - using (MyWebClient downloadClient = new MyWebClient()) + if (!Regex.IsMatch(tNightURL, @"^https?:\/\/", RegexOptions.IgnoreCase)) + { + //If no http, add it + tNightURL = "http://" + tNightURL; + } + + Uri textureUri; + if (Uri.TryCreate(tNightURL, UriKind.Absolute, out textureUri)) + { + using (MyWebClient downloadClient = new MyWebClient()) + { + string tempFile = System.IO.Path.GetTempFileName(); + downloadClient.DownloadFile(tNightURL, tempFile); + FileInfo fi = new FileInfo(tempFile); + if (fi.Length < 100) + { + //Zero byte / 404 text probably + currentHost.AddMessage(MessageType.Error, false, "Invalid texture downloaded in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + else + { + tUnight = tempFile; + } + } + } + else { - string tempFile = System.IO.Path.GetTempFileName(); - downloadClient.DownloadFile(tNightURL, tempFile); - tUnight = tempFile; + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); } } - else + catch { - currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + currentHost.AddMessage(MessageType.Error, false, "An unexpected error occured whilst attempting to download " + Arguments[0] + " in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); } }