1 #pragma warning disable 0659, 0661
2 
3 namespace OpenBveApi.Textures
4 {
5 	/// <summary>Represents a file or directory where the texture can be loaded from.</summary>
6 	public class PathOrigin : TextureOrigin
7 	{
8 		// --- members ---
9 		/// <summary>The absolute on-disk path to where the texture may be loaded</summary>
10 		public readonly string Path;
11 		/// <summary>The texture parameters to be applied when loading this texture from disk</summary>
12 		/// <remarks>Multiple textures with different parameters may be loaded from the same on-disk file</remarks>
13 		public readonly TextureParameters Parameters;
14 
15 		private readonly Hosts.HostInterface currentHost;
16 
17 		// --- constructors ---
18 		/// <summary>Creates a new path origin.</summary>
19 		/// <param name="path">The path to the texture.</param>
20 		/// <param name="parameters">The parameters that specify how to process the texture.</param>
21 		/// <param name="Host">The callback function to the host application</param>
PathOrigin(string path, TextureParameters parameters, Hosts.HostInterface Host)22 		public PathOrigin(string path, TextureParameters parameters, Hosts.HostInterface Host)
23 		{
24 			this.Path = path;
25 			this.Parameters = parameters;
26 			this.currentHost = Host;
27 		}
28 
29 		// --- functions ---
30 		/// <summary>Gets the texture from this origin.</summary>
31 		/// <param name="texture">Receives the texture.</param>
32 		/// <returns>Whether the texture could be obtained successfully.</returns>
GetTexture(out Texture texture)33 		public override bool GetTexture(out Texture texture)
34 		{
35 			if (!currentHost.LoadTexture(this.Path, this.Parameters, out texture))
36 			{
37 				texture = null;
38 				return false;
39 			}
40 
41 			return true;
42 		}
43 
44 		// --- operators ---
45 		/// <summary>Checks whether two origins are equal.</summary>
46 		/// <param name="a">The first origin.</param>
47 		/// <param name="b">The second origin.</param>
48 		/// <returns>Whether the two origins are equal.</returns>
operator ==(PathOrigin a, PathOrigin b)49 		public static bool operator ==(PathOrigin a, PathOrigin b)
50 		{
51 			if (object.ReferenceEquals(a, b)) return true;
52 			if (object.ReferenceEquals(a, null)) return false;
53 			if (object.ReferenceEquals(b, null)) return false;
54 			return a.Path == b.Path;
55 		}
56 
57 		/// <summary>Checks whether two origins are unequal.</summary>
58 		/// <param name="a">The first origin.</param>
59 		/// <param name="b">The second origin.</param>
60 		/// <returns>Whether the two origins are unequal.</returns>
operator !=(PathOrigin a, PathOrigin b)61 		public static bool operator !=(PathOrigin a, PathOrigin b)
62 		{
63 			if (object.ReferenceEquals(a, b)) return false;
64 			if (object.ReferenceEquals(a, null)) return true;
65 			if (object.ReferenceEquals(b, null)) return true;
66 			return a.Path != b.Path;
67 		}
68 
69 		/// <summary>Checks whether this instance is equal to the specified object.</summary>
70 		/// <param name="obj">The object.</param>
71 		/// <returns>Whether this instance is equal to the specified object.</returns>
Equals(object obj)72 		public override bool Equals(object obj)
73 		{
74 			if (object.ReferenceEquals(this, obj)) return true;
75 			if (object.ReferenceEquals(this, null)) return false;
76 			if (object.ReferenceEquals(obj, null)) return false;
77 			if (!(obj is PathOrigin)) return false;
78 			return this.Path == ((PathOrigin) obj).Path;
79 		}
80 
81 		/// <summary>Returns a string representing the absolute on-disk path of this texture</summary>
ToString()82 		public override string ToString()
83 		{
84 			return this.Path;
85 		}
86 	}
87 }
88