diff --git a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs index 4c8e9f0fd6..cda4d0b0c0 100644 --- a/source/Plugins/Object.CsvB3d/Plugin.Parser.cs +++ b/source/Plugins/Object.CsvB3d/Plugin.Parser.cs @@ -1,15 +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 { @@ -1161,6 +1163,95 @@ 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]; + try + { + 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 + { + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + } + catch + { + 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]; + try + { + 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 + { + currentHost.AddMessage(MessageType.Error, false, "Not a valid URL in " + Command + " at line " + (i + 1).ToString(Culture) + " in file " + FileName); + } + } + catch + { + 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); + } + + } + 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 +1660,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; + } + } } }