Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Helper.cs
Go to the documentation of this file.
7
9
10internal static class Helper
11{
12 private static string s_machineRootDirectory;
13
14 private static string s_roamingUserRootDirectory;
15
16 private static string s_userRootDirectory;
17
18 internal static string GetRootDirectory(IsolatedStorageScope scope)
19 {
20 if (IsRoaming(scope))
21 {
22 if (string.IsNullOrEmpty(s_roamingUserRootDirectory))
23 {
25 }
27 }
28 if (IsMachine(scope))
29 {
30 if (string.IsNullOrEmpty(s_machineRootDirectory))
31 {
33 }
35 }
36 if (string.IsNullOrEmpty(s_userRootDirectory))
37 {
39 }
41 }
42
43 internal static bool IsMachine(IsolatedStorageScope scope)
44 {
45 return (scope & IsolatedStorageScope.Machine) != 0;
46 }
47
48 internal static bool IsAssembly(IsolatedStorageScope scope)
49 {
50 return (scope & IsolatedStorageScope.Assembly) != 0;
51 }
52
53 internal static bool IsApplication(IsolatedStorageScope scope)
54 {
55 return (scope & IsolatedStorageScope.Application) != 0;
56 }
57
58 internal static bool IsRoaming(IsolatedStorageScope scope)
59 {
60 return (scope & IsolatedStorageScope.Roaming) != 0;
61 }
62
63 internal static bool IsDomain(IsolatedStorageScope scope)
64 {
65 return (scope & IsolatedStorageScope.Domain) != 0;
66 }
67
68 internal static string GetDataDirectory(IsolatedStorageScope scope)
69 {
70 Environment.SpecialFolder folder = (IsMachine(scope) ? Environment.SpecialFolder.CommonApplicationData : (IsRoaming(scope) ? Environment.SpecialFolder.ApplicationData : Environment.SpecialFolder.LocalApplicationData));
71 string folderPath = Environment.GetFolderPath(folder, Environment.SpecialFolderOption.Create);
72 return Path.Combine(folderPath, "IsolatedStorage");
73 }
74
75 [UnconditionalSuppressMessage("SingleFile", "IL3000:Avoid accessing Assembly file path when publishing as a single file", Justification = "Code handles single-file deployment by using the information of the .exe file")]
76 internal static void GetDefaultIdentityAndHash(out object identity, out string hash, char separator)
77 {
78 Assembly entryAssembly = Assembly.GetEntryAssembly();
79 string text = null;
80 if (entryAssembly != null)
81 {
82 AssemblyName name = entryAssembly.GetName();
84 if (hash != null)
85 {
86 hash = "StrongName" + separator + hash;
87 identity = name;
88 return;
89 }
90 text = entryAssembly.Location;
91 }
92 if (string.IsNullOrEmpty(text))
93 {
95 }
96 if (string.IsNullOrEmpty(text))
97 {
99 }
100 Uri uri = new Uri(text);
101 hash = "Url" + separator + IdentityHelper.GetNormalizedUriHash(uri);
102 identity = uri;
103 }
104
105 internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
106 {
107 string text = GetExistingRandomDirectory(rootDirectory);
108 if (string.IsNullOrEmpty(text))
109 {
110 using Mutex mutex = CreateMutexNotOwned(rootDirectory);
111 if (!mutex.WaitOne())
112 {
114 }
115 try
116 {
117 text = GetExistingRandomDirectory(rootDirectory);
118 if (string.IsNullOrEmpty(text))
119 {
121 CreateDirectory(text, scope);
122 }
123 }
124 finally
125 {
126 mutex.ReleaseMutex();
127 }
128 }
129 return text;
130 }
131
132 internal static string GetExistingRandomDirectory(string rootDirectory)
133 {
134 if (!Directory.Exists(rootDirectory))
135 {
136 return null;
137 }
138 string[] directories = Directory.GetDirectories(rootDirectory);
139 foreach (string path in directories)
140 {
141 string? fileName = Path.GetFileName(path);
142 if (fileName == null || fileName.Length != 12)
143 {
144 continue;
145 }
146 string[] directories2 = Directory.GetDirectories(path);
147 foreach (string text in directories2)
148 {
149 string? fileName2 = Path.GetFileName(text);
150 if (fileName2 != null && fileName2.Length == 12)
151 {
152 return text;
153 }
154 }
155 }
156 return null;
157 }
158
159 private static Mutex CreateMutexNotOwned(string pathName)
160 {
161 return new Mutex(initiallyOwned: false, "Global\\" + IdentityHelper.GetStrongHashSuitableForObjectName(pathName));
162 }
163
164 internal static void CreateDirectory(string path, IsolatedStorageScope scope)
165 {
166 if (!Directory.Exists(path))
167 {
168 DirectoryInfo directoryInfo = Directory.CreateDirectory(path);
169 if (IsMachine(scope))
170 {
171 DirectorySecurity directorySecurity = new DirectorySecurity();
172 directorySecurity.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
173 directorySecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.Read | FileSystemRights.Write, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
174 directorySecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null), FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
175 directorySecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, null), FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
176 directoryInfo.SetAccessControl(directorySecurity);
177 }
178 }
179 }
180}
static string GetFolderPath(SpecialFolder folder)
static ? string ProcessPath
static string[] GetDirectories(string path)
Definition Directory.cs:156
static DirectoryInfo CreateDirectory(string path)
Definition Directory.cs:28
static bool Exists([NotNullWhen(true)] string? path)
Definition Directory.cs:43
static string s_machineRootDirectory
Definition Helper.cs:12
static bool IsAssembly(IsolatedStorageScope scope)
Definition Helper.cs:48
static bool IsRoaming(IsolatedStorageScope scope)
Definition Helper.cs:58
static bool IsMachine(IsolatedStorageScope scope)
Definition Helper.cs:43
static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
Definition Helper.cs:105
static bool IsDomain(IsolatedStorageScope scope)
Definition Helper.cs:63
static Mutex CreateMutexNotOwned(string pathName)
Definition Helper.cs:159
static string GetRootDirectory(IsolatedStorageScope scope)
Definition Helper.cs:18
static void CreateDirectory(string path, IsolatedStorageScope scope)
Definition Helper.cs:164
static string s_roamingUserRootDirectory
Definition Helper.cs:14
static bool IsApplication(IsolatedStorageScope scope)
Definition Helper.cs:53
static string GetExistingRandomDirectory(string rootDirectory)
Definition Helper.cs:132
static string s_userRootDirectory
Definition Helper.cs:16
static string GetDataDirectory(IsolatedStorageScope scope)
Definition Helper.cs:68
static void GetDefaultIdentityAndHash(out object identity, out string hash, char separator)
Definition Helper.cs:76
static string Combine(string path1, string path2)
Definition Path.cs:304
static ? string GetFileName(string? path)
Definition Path.cs:200
static unsafe string GetRandomFileName()
Definition Path.cs:254
virtual AssemblyName GetName()
Definition Assembly.cs:294
static ? Assembly GetEntryAssembly()
Definition Assembly.cs:486
virtual string Location
Definition Assembly.cs:92
static string IsolatedStorage_Init
Definition SR.cs:32
Definition SR.cs:7
void SetAccessRuleProtection(bool isProtected, bool preserveInheritance)
static string GetStrongHashSuitableForObjectName(string name)
static string GetNormalizedUriHash(Uri uri)
static string GetNormalizedStrongNameHash(AssemblyName name)