Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FileUtilities.cs
Go to the documentation of this file.
1using System;
2using System.IO;
5using ReLogic.OS;
7
8namespace Terraria.Utilities;
9
10public static class FileUtilities
11{
12 private static Regex FileNameRegex = new Regex("^(?<path>.*[\\\\\\/])?(?:$|(?<fileName>.+?)(?:(?<extension>\\.[^.]*$)|$))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
13
14 public static bool Exists(string path, bool cloud)
15 {
16 if (cloud && SocialAPI.Cloud != null)
17 {
18 return SocialAPI.Cloud.HasFile(path);
19 }
20 return File.Exists(path);
21 }
22
23 public static void Delete(string path, bool cloud, bool forceDeleteFile = false)
24 {
25 if (cloud && SocialAPI.Cloud != null)
26 {
27 SocialAPI.Cloud.Delete(path);
28 }
29 else if (forceDeleteFile)
30 {
31 File.Delete(path);
32 }
33 else
34 {
35 Platform.Get<IPathService>().MoveToRecycleBin(path);
36 }
37 }
38
39 public static string GetFullPath(string path, bool cloud)
40 {
41 if (!cloud)
42 {
43 return Path.GetFullPath(path);
44 }
45 return path;
46 }
47
48 public static void Copy(string source, string destination, bool cloud, bool overwrite = true)
49 {
50 if (!cloud)
51 {
52 try
53 {
54 File.Copy(source, destination, overwrite);
55 return;
56 }
57 catch (IOException ex)
58 {
59 if (ex.GetType() != typeof(IOException))
60 {
61 throw;
62 }
63 using FileStream fileStream = File.OpenRead(source);
64 using FileStream destination2 = File.Create(destination);
65 fileStream.CopyTo(destination2);
66 return;
67 }
68 }
69 if (SocialAPI.Cloud != null && (overwrite || !SocialAPI.Cloud.HasFile(destination)))
70 {
72 }
73 }
74
75 public static void Move(string source, string destination, bool cloud, bool overwrite = true, bool forceDeleteSourceFile = false)
76 {
77 Copy(source, destination, cloud, overwrite);
78 Delete(source, cloud, forceDeleteSourceFile);
79 }
80
81 public static int GetFileSize(string path, bool cloud)
82 {
83 if (cloud && SocialAPI.Cloud != null)
84 {
85 return SocialAPI.Cloud.GetFileSize(path);
86 }
87 return (int)new FileInfo(path).Length;
88 }
89
90 public static void Read(string path, byte[] buffer, int length, bool cloud)
91 {
92 if (cloud && SocialAPI.Cloud != null)
93 {
94 SocialAPI.Cloud.Read(path, buffer, length);
95 return;
96 }
97 using FileStream fileStream = File.OpenRead(path);
98 fileStream.Read(buffer, 0, length);
99 }
100
101 public static byte[] ReadAllBytes(string path, bool cloud)
102 {
103 if (cloud && SocialAPI.Cloud != null)
104 {
105 return SocialAPI.Cloud.Read(path);
106 }
107 return File.ReadAllBytes(path);
108 }
109
110 public static void WriteAllBytes(string path, byte[] data, bool cloud)
111 {
112 Write(path, data, data.Length, cloud);
113 }
114
115 public static void Write(string path, byte[] data, int length, bool cloud)
116 {
117 if (cloud && SocialAPI.Cloud != null)
118 {
119 SocialAPI.Cloud.Write(path, data, length);
120 return;
121 }
122 string parentFolderPath = GetParentFolderPath(path);
123 if (parentFolderPath != "")
124 {
125 Utils.TryCreatingDirectory(parentFolderPath);
126 }
128 using FileStream fileStream = File.Open(path, FileMode.Create);
129 while (fileStream.Position < length)
130 {
131 fileStream.Write(data, (int)fileStream.Position, Math.Min(length - (int)fileStream.Position, 2048));
132 }
133 }
134
135 public static void RemoveReadOnlyAttribute(string path)
136 {
137 if (!File.Exists(path))
138 {
139 return;
140 }
141 try
142 {
143 FileAttributes attributes = File.GetAttributes(path);
144 if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
145 {
146 attributes &= ~FileAttributes.ReadOnly;
147 File.SetAttributes(path, attributes);
148 }
149 }
150 catch (Exception)
151 {
152 }
153 }
154
155 public static bool MoveToCloud(string localPath, string cloudPath)
156 {
157 if (SocialAPI.Cloud == null)
158 {
159 return false;
160 }
161 WriteAllBytes(cloudPath, ReadAllBytes(localPath, cloud: false), cloud: true);
162 Delete(localPath, cloud: false);
163 return true;
164 }
165
166 public static bool MoveToLocal(string cloudPath, string localPath)
167 {
168 if (SocialAPI.Cloud == null)
169 {
170 return false;
171 }
172 WriteAllBytes(localPath, ReadAllBytes(cloudPath, cloud: true), cloud: false);
173 Delete(cloudPath, cloud: true);
174 return true;
175 }
176
177 public static bool CopyToLocal(string cloudPath, string localPath)
178 {
179 if (SocialAPI.Cloud == null)
180 {
181 return false;
182 }
183 WriteAllBytes(localPath, ReadAllBytes(cloudPath, cloud: true), cloud: false);
184 return true;
185 }
186
187 public static string GetFileName(string path, bool includeExtension = true)
188 {
189 Match match = FileNameRegex.Match(path);
190 if (match == null || match.Groups["fileName"] == null)
191 {
192 return "";
193 }
194 includeExtension &= match.Groups["extension"] != null;
195 return match.Groups["fileName"].Value + (includeExtension ? match.Groups["extension"].Value : "");
196 }
197
198 public static string GetParentFolderPath(string path, bool includeExtension = true)
199 {
200 Match match = FileNameRegex.Match(path);
201 if (match == null || match.Groups["path"] == null)
202 {
203 return "";
204 }
205 return match.Groups["path"].Value;
206 }
207
208 public static void CopyFolder(string sourcePath, string destinationPath)
209 {
210 Directory.CreateDirectory(destinationPath);
211 string[] directories = Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories);
212 for (int i = 0; i < directories.Length; i++)
213 {
214 Directory.CreateDirectory(directories[i].Replace(sourcePath, destinationPath));
215 }
216 directories = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
217 foreach (string obj in directories)
218 {
219 File.Copy(obj, obj.Replace(sourcePath, destinationPath), overwrite: true);
220 }
221 }
222
223 public static void ProtectedInvoke(Action action)
224 {
225 bool isBackground = Thread.CurrentThread.IsBackground;
226 try
227 {
228 Thread.CurrentThread.IsBackground = false;
229 action();
230 }
231 finally
232 {
233 Thread.CurrentThread.IsBackground = isBackground;
234 }
235 }
236}
new Type GetType()
Definition Exception.cs:437
static string[] GetDirectories(string path)
Definition Directory.cs:156
static DirectoryInfo CreateDirectory(string path)
Definition Directory.cs:28
static string[] GetFiles(string path)
Definition Directory.cs:136
static void SetAttributes(string path, FileAttributes fileAttributes)
Definition File.cs:230
static FileStream Create(string path)
Definition File.cs:73
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static FileStream OpenRead(string path)
Definition File.cs:236
static FileStream Open(string path, FileMode mode)
Definition File.cs:128
static void Copy(string sourceFileName, string destFileName)
Definition File.cs:47
static byte[] ReadAllBytes(string path)
Definition File.cs:314
static void Delete(string path)
Definition File.cs:88
static FileAttributes GetAttributes(string path)
Definition File.cs:224
static string GetFullPath(string path)
Definition Path.cs:881
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static Thread CurrentThread
Definition Thread.cs:312
static Terraria.Social.Base.CloudSocialModule Cloud
Definition SocialAPI.cs:18
static bool Exists(string path, bool cloud)
static bool MoveToCloud(string localPath, string cloudPath)
static int GetFileSize(string path, bool cloud)
static void RemoveReadOnlyAttribute(string path)
static string GetFileName(string path, bool includeExtension=true)
static void ProtectedInvoke(Action action)
static void Write(string path, byte[] data, int length, bool cloud)
static byte[] ReadAllBytes(string path, bool cloud)
static void Move(string source, string destination, bool cloud, bool overwrite=true, bool forceDeleteSourceFile=false)
static void Copy(string source, string destination, bool cloud, bool overwrite=true)
static string GetParentFolderPath(string path, bool includeExtension=true)
static string GetFullPath(string path, bool cloud)
static void CopyFolder(string sourcePath, string destinationPath)
static bool MoveToLocal(string cloudPath, string localPath)
static void Read(string path, byte[] buffer, int length, bool cloud)
static void Delete(string path, bool cloud, bool forceDeleteFile=false)
static void WriteAllBytes(string path, byte[] data, bool cloud)
static bool CopyToLocal(string cloudPath, string localPath)
static bool TryCreatingDirectory(string folderPath)
Definition Utils.cs:754