Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Support URL based textures #671

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 105 additions & 1 deletion source/Plugins/Object.CsvB3d/Plugin.Parser.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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":
{
Expand Down Expand Up @@ -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;
}
}
}
}