Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TitleContainer.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4
6
7public static class TitleContainer
8{
9 private static char[] badCharacters = new char[7] { ':', '*', '?', '"', '<', '>', '|' };
10
11 public static Stream OpenStream(string name)
12 {
13 if (string.IsNullOrEmpty(name))
14 {
15 throw new ArgumentNullException("name");
16 }
17 name = GetCleanPath(name);
18 if (IsCleanPathAbsolute(name))
19 {
21 }
22 try
23 {
24 string uriString = name.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
25 new Uri(uriString, UriKind.Relative);
26 }
27 catch (Exception innerException)
28 {
30 }
31 try
32 {
33 string path = Path.Combine(TitleLocation.Path, name);
34 return File.OpenRead(path);
35 }
36 catch (Exception ex)
37 {
39 {
41 }
43 }
44 }
45
46 internal static bool IsPathAbsolute(string path)
47 {
48 path = GetCleanPath(path);
49 return IsCleanPathAbsolute(path);
50 }
51
52 internal static string GetCleanPath(string path)
53 {
54 path = path.Replace('/', '\\');
55 path = path.Replace("\\.\\", "\\");
56 while (path.StartsWith(".\\"))
57 {
58 path = path.Substring(".\\".Length);
59 }
60 while (path.EndsWith("\\."))
61 {
62 path = ((path.Length <= "\\.".Length) ? "\\" : path.Substring(0, path.Length - "\\.".Length));
63 }
64 int num;
65 for (num = 1; num < path.Length; num = CollapseParentDirectory(ref path, num, "\\..\\".Length))
66 {
67 num = path.IndexOf("\\..\\", num);
68 if (num < 0)
69 {
70 break;
71 }
72 }
73 if (path.EndsWith("\\.."))
74 {
75 num = path.Length - "\\..".Length;
76 if (num > 0)
77 {
78 CollapseParentDirectory(ref path, num, "\\..".Length);
79 }
80 }
81 if (path == ".")
82 {
83 path = string.Empty;
84 }
85 return path;
86 }
87
88 private static int CollapseParentDirectory(ref string path, int position, int removeLength)
89 {
90 int num = path.LastIndexOf('\\', position - 1) + 1;
91 path = path.Remove(num, position - num + removeLength);
92 return Math.Max(num - 1, 1);
93 }
94
95 private static bool IsCleanPathAbsolute(string path)
96 {
97 if (path.IndexOfAny(badCharacters) >= 0)
98 {
99 return true;
100 }
101 if (path.StartsWith("\\"))
102 {
103 return true;
104 }
105 if (path.StartsWith("..\\") || path.Contains("\\..\\") || path.EndsWith("\\..") || path == "..")
106 {
107 return true;
108 }
109 return false;
110 }
111}
static Stream OpenStream(string name)
static string GetCleanPath(string path)
static bool IsPathAbsolute(string path)
static bool IsCleanPathAbsolute(string path)
static int CollapseParentDirectory(ref string path, int position, int removeLength)
static CultureInfo CurrentCulture
static FileStream OpenRead(string path)
Definition File.cs:236
static string Combine(string path1, string path2)
Definition Path.cs:304
static readonly char AltDirectorySeparatorChar
Definition Path.cs:73
static readonly char DirectorySeparatorChar
Definition Path.cs:71
static byte Max(byte val1, byte val2)
Definition Math.cs:738
UriKind
Definition UriKind.cs:4