2 using System.Collections.Generic;
3 using System.Diagnostics;
7 using System.Xml.Serialization;
11 using Microsoft.CodeAnalysis;
12 using Microsoft.Xna.Framework;
16 public static class ModUtils
20 public static class Logging
22 public static void PrintMessage(
string s)
31 public static void PrintWarning(
string s)
40 public static void PrintError(
string s)
55 public static class IO
57 public static IEnumerable<string> FindAllFilesInDirectory(
string folder,
string pattern,
62 return Directory.GetFiles(folder, pattern, option);
64 catch (DirectoryNotFoundException e)
66 return new string[] { };
70 public static string PrepareFilePathString(
string filePath) =>
71 PrepareFilePathString(Path.GetDirectoryName(filePath)!, Path.GetFileName(filePath));
73 public static string PrepareFilePathString(
string path,
string fileName) =>
74 Path.Combine(SanitizePath(path), SanitizeFileName(fileName));
76 public static string SanitizeFileName(
string fileName)
78 foreach (
char c
in Barotrauma.
IO.
Path.GetInvalidFileNameCharsCrossPlatform())
79 fileName = fileName.Replace(c,
'_');
90 return SanitizePath(Path.GetFullPath(package.
Dir));
93 public static string SanitizePath(
string path)
95 foreach (
char c
in Path.GetInvalidPathChars())
96 path = path.Replace(c.ToString(),
"_");
97 return path.CleanUpPath();
100 public static IOActionResultState GetOrCreateFileText(
string filePath, out
string fileText, Func<string> fileDataFactory =
null,
bool createFile =
true)
103 string fp = Path.GetFullPath(SanitizePath(filePath));
105 IOActionResultState ioActionResultState = IOActionResultState.Success;
108 ioActionResultState = CreateFilePath(SanitizePath(filePath), out fp, fileDataFactory);
110 else if (!File.Exists(fp))
112 return IOActionResultState.FileNotFound;
115 if (ioActionResultState == IOActionResultState.Success)
119 fileText = File.ReadAllText(fp!);
120 return IOActionResultState.Success;
122 catch (ArgumentNullException ane)
124 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: An argument is null. path: {fp ?? "null"} | Exception Details: {ane.Message}");
125 return IOActionResultState.FilePathNull;
127 catch (ArgumentException ae)
129 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: An argument is invalid. path: {fp ?? "null"} | Exception Details: {ae.Message}");
130 return IOActionResultState.FilePathInvalid;
132 catch (DirectoryNotFoundException dnfe)
134 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Cannot find directory. path: {fp ?? "null"} | Exception Details: {dnfe.Message}");
135 return IOActionResultState.DirectoryMissing;
137 catch (PathTooLongException ptle)
139 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: path length is over 200 characters. path: {fp ?? "null"} | Exception Details: {ptle.Message}");
140 return IOActionResultState.PathTooLong;
142 catch (NotSupportedException nse)
144 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Operation not supported on your platform/environment (permissions?). path: {fp ?? "null"} | Exception Details: {nse.Message}");
145 return IOActionResultState.InvalidOperation;
147 catch (IOException ioe)
149 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: IO tasks failed (Operation not supported). path: {fp ?? "null"} | Exception Details: {ioe.Message}");
150 return IOActionResultState.IOFailure;
154 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Unknown/Other Exception. path: {fp ?? "null"} | ExceptionMessage: {e.Message}");
155 return IOActionResultState.UnknownError;
159 return ioActionResultState;
162 public static IOActionResultState CreateFilePath(
string filePath, out
string formattedFilePath, Func<string> fileDataFactory =
null)
164 string file = Path.GetFileName(filePath);
165 string path = Path.GetDirectoryName(filePath)!;
167 formattedFilePath = IO.PrepareFilePathString(path, file);
170 if (!Directory.Exists(path))
171 Directory.CreateDirectory(path);
172 if (!File.Exists(formattedFilePath))
173 File.WriteAllText(formattedFilePath, fileDataFactory is
null ?
"" : fileDataFactory.Invoke());
174 return IOActionResultState.Success;
176 catch (ArgumentNullException ane)
178 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: An argument is null. path: {formattedFilePath ?? "null"} | Exception Details: {ane.Message}");
179 return IOActionResultState.FilePathNull;
181 catch (ArgumentException ae)
183 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: An argument is invalid. path: {formattedFilePath ?? "null"} | Exception Details: {ae.Message}");
184 return IOActionResultState.FilePathInvalid;
186 catch (DirectoryNotFoundException dnfe)
188 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Cannot find directory. path: {path ?? "null"} | Exception Details: {dnfe.Message}");
189 return IOActionResultState.DirectoryMissing;
191 catch (PathTooLongException ptle)
193 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: path length is over 200 characters. path: {formattedFilePath ?? "null"} | Exception Details: {ptle.Message}");
194 return IOActionResultState.PathTooLong;
196 catch (NotSupportedException nse)
198 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Operation not supported on your platform/environment (permissions?). path: {formattedFilePath ?? "null"} | Exception Details: {nse.Message}");
199 return IOActionResultState.InvalidOperation;
201 catch (IOException ioe)
203 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: IO tasks failed (Operation not supported). path: {formattedFilePath ?? "null"} | Exception Details: {ioe.Message}");
204 return IOActionResultState.IOFailure;
208 ModUtils.Logging.PrintError($
"ModUtils::CreateFilePath() | Exception: Unknown/Other Exception. path: {path ?? "null"} | Exception Details: {e.Message}");
209 return IOActionResultState.UnknownError;
213 public static IOActionResultState WriteFileText(
string filePath,
string fileText)
215 IOActionResultState ioActionResultState = CreateFilePath(filePath, out var fp);
216 if (ioActionResultState == IOActionResultState.Success)
220 File.WriteAllText(fp!, fileText);
221 return IOActionResultState.Success;
223 catch (ArgumentNullException ane)
225 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: An argument is null. path: {fp ?? "null"} | Exception Details: {ane.Message}");
226 return IOActionResultState.FilePathNull;
228 catch (ArgumentException ae)
230 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: An argument is invalid. path: {fp ?? "null"} | Exception Details: {ae.Message}");
231 return IOActionResultState.FilePathInvalid;
233 catch (DirectoryNotFoundException dnfe)
235 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: Cannot find directory. path: {fp ?? "null"} | Exception Details: {dnfe.Message}");
236 return IOActionResultState.DirectoryMissing;
238 catch (PathTooLongException ptle)
240 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: path length is over 200 characters. path: {fp ?? "null"} | Exception Details: {ptle.Message}");
241 return IOActionResultState.PathTooLong;
243 catch (NotSupportedException nse)
245 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: Operation not supported on your platform/environment (permissions?). path: {fp ?? "null"} | Exception Details: {nse.Message}");
246 return IOActionResultState.InvalidOperation;
248 catch (IOException ioe)
250 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: IO tasks failed (Operation not supported). path: {fp ?? "null"} | Exception Details: {ioe.Message}");
251 return IOActionResultState.IOFailure;
255 ModUtils.Logging.PrintError($
"ModUtils::WriteFileText() | Exception: Unknown/Other Exception. path: {fp ?? "null"} | ExceptionMessage: {e.Message}");
256 return IOActionResultState.UnknownError;
260 return ioActionResultState;
272 public static bool LoadOrCreateTypeXml<T>(out T instance,
273 string filepath, Func<T> typeFactory =
null,
bool createFile =
true) where T : class, new()
276 filepath = filepath.CleanUpPath();
277 if (IOActionResultState.Success == GetOrCreateFileText(
278 filepath, out
string fileText, typeFactory is not
null ? () =>
280 using StringWriter sw =
new StringWriter();
281 T t = typeFactory?.Invoke();
284 XmlSerializer s =
new XmlSerializer(typeof(T));
286 return sw.ToString();
289 } :
null, createFile))
291 XmlSerializer s =
new XmlSerializer(typeof(T));
294 using TextReader tr =
new StringReader(fileText);
295 instance = (T)s.Deserialize(tr);
298 catch(InvalidOperationException ioe)
300 ModUtils.Logging.PrintError($
"Error while parsing type data for {typeof(T)}.");
302 ModUtils.Logging.PrintError($
"Exception: {ioe.Message}. Details: {ioe.InnerException?.Message}");
312 public enum IOActionResultState
314 Success, FileNotFound, FilePathNull, FilePathInvalid, DirectoryMissing, PathTooLong, InvalidOperation, IOFailure, UnknownError
322 public static class Game
328 public static bool IsRoundInProgress()
static GameSession?? GameSession
static void LogError(string message, LuaCsMessageOrigin origin)
static void LogMessage(string message, Color? serverColor=null, Color? clientColor=null)
static void Log(string message, Color? color=null, ServerLog.MessageType messageType=ServerLog.MessageType.ServerMessage)