v0.14.9.1
This commit is contained in:
@@ -1,21 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma.IO
|
||||
{
|
||||
static class Validation
|
||||
{
|
||||
static readonly string[] unwritableDirs = new string[] { "Content", "Data/ContentPackages" };
|
||||
private static readonly string[] unwritableDirs = new string[] { "Content", "Data/ContentPackages" };
|
||||
private static readonly string[] unwritableExtensions = new string[]
|
||||
{
|
||||
".pdb", ".com", ".scr", ".dylib", ".so", ".a", ".app", //executables and libraries (.exe and .dll handled separately in CanWrite)
|
||||
".bat", ".sh", //shell scripts
|
||||
".json" //deps.json
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// When set to true, the game is allowed to modify the vanilla content in debug builds. Has no effect in non-debug builds.
|
||||
/// </summary>
|
||||
public static bool SkipValidationInDebugBuilds;
|
||||
|
||||
public static bool CanWrite(string path)
|
||||
public static bool CanWrite(string path, bool isDirectory)
|
||||
{
|
||||
path = System.IO.Path.GetFullPath(path).CleanUpPath();
|
||||
|
||||
string extension = System.IO.Path.GetExtension(path).Replace(" ", "");
|
||||
if (unwritableExtensions.Any(e => e.Equals(extension, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!path.StartsWith(System.IO.Path.GetFullPath("Mods/").CleanUpPath(), StringComparison.OrdinalIgnoreCase)
|
||||
&& (extension.Equals(".dll", StringComparison.OrdinalIgnoreCase)
|
||||
|| extension.Equals(".exe", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (string unwritableDir in unwritableDirs)
|
||||
{
|
||||
string dir = System.IO.Path.GetFullPath(unwritableDir).CleanUpPath();
|
||||
@@ -38,9 +58,9 @@ namespace Barotrauma.IO
|
||||
{
|
||||
public static void SaveSafe(this System.Xml.Linq.XDocument doc, string path)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot save XML document to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot save XML document to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
doc.Save(path);
|
||||
@@ -48,9 +68,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void SaveSafe(this System.Xml.Linq.XElement element, string path)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot save XML element to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot save XML element to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
element.Save(path);
|
||||
@@ -73,9 +93,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public XmlWriter(string path, System.Xml.XmlWriterSettings settings)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot write XML document to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot write XML document to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
Writer = null;
|
||||
return;
|
||||
}
|
||||
@@ -223,9 +243,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static System.IO.DirectoryInfo CreateDirectory(string path)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, true))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot create directory \"{path}\": modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot create directory \"{path}\": modifying the contents of this folder/using this extension is not allowed.");
|
||||
return null;
|
||||
}
|
||||
return System.IO.Directory.CreateDirectory(path);
|
||||
@@ -233,9 +253,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void Delete(string path, bool recursive=true)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, true))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot delete directory \"{path}\": modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot delete directory \"{path}\": modifying the contents of this folder/using this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
//TODO: validate recursion?
|
||||
@@ -252,9 +272,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void Copy(string src, string dest, bool overwrite=false)
|
||||
{
|
||||
if (!Validation.CanWrite(dest))
|
||||
if (!Validation.CanWrite(dest, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot copy \"{src}\" to \"{dest}\": modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot copy \"{src}\" to \"{dest}\": modifying the contents of this folder/using this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
System.IO.File.Copy(src, dest, overwrite);
|
||||
@@ -262,12 +282,12 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void Move(string src, string dest)
|
||||
{
|
||||
if (!Validation.CanWrite(src))
|
||||
if (!Validation.CanWrite(src, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot move \"{src}\" to \"{dest}\": modifying the contents of the source folder is not allowed.");
|
||||
return;
|
||||
}
|
||||
if (!Validation.CanWrite(dest))
|
||||
if (!Validation.CanWrite(dest, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot move \"{src}\" to \"{dest}\": modifying the contents of the destination folder is not allowed");
|
||||
return;
|
||||
@@ -277,9 +297,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void Delete(string path)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot delete file \"{path}\": modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot delete file \"{path}\": modifying the contents of this folder/using this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
System.IO.File.Delete(path);
|
||||
@@ -299,15 +319,15 @@ namespace Barotrauma.IO
|
||||
case System.IO.FileMode.OpenOrCreate:
|
||||
case System.IO.FileMode.Append:
|
||||
case System.IO.FileMode.Truncate:
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot open \"{path}\" in {mode} mode: modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot open \"{path}\" in {mode} mode: modifying the contents of this folder/using this extension is not allowed.");
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return new FileStream(path, System.IO.File.Open(path, mode,
|
||||
!Validation.CanWrite(path) ?
|
||||
!Validation.CanWrite(path, false) ?
|
||||
System.IO.FileAccess.Read :
|
||||
access));
|
||||
}
|
||||
@@ -329,9 +349,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void WriteAllBytes(string path, byte[] contents)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot write all bytes to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot write all bytes to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
System.IO.File.WriteAllBytes(path, contents);
|
||||
@@ -339,9 +359,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void WriteAllText(string path, string contents, System.Text.Encoding? encoding = null)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot write all text to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot write all text to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
System.IO.File.WriteAllText(path, contents, encoding ?? System.Text.Encoding.UTF8);
|
||||
@@ -349,9 +369,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public static void WriteAllLines(string path, IEnumerable<string> contents, System.Text.Encoding? encoding = null)
|
||||
{
|
||||
if (!Validation.CanWrite(path))
|
||||
if (!Validation.CanWrite(path, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot write all lines to \"{path}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot write all lines to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
System.IO.File.WriteAllLines(path, contents, encoding ?? System.Text.Encoding.UTF8);
|
||||
@@ -391,7 +411,7 @@ namespace Barotrauma.IO
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!Validation.CanWrite(fileName)) { return false; }
|
||||
if (!Validation.CanWrite(fileName, false)) { return false; }
|
||||
return innerStream.CanWrite;
|
||||
}
|
||||
}
|
||||
@@ -417,13 +437,13 @@ namespace Barotrauma.IO
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (Validation.CanWrite(fileName))
|
||||
if (Validation.CanWrite(fileName, false))
|
||||
{
|
||||
innerStream.Write(buffer, offset, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot write to file \"{fileName}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot write to file \"{fileName}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,9 +508,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
if (!Validation.CanWrite(innerInfo.FullName))
|
||||
if (!Validation.CanWrite(innerInfo.FullName, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot delete directory \"{Name}\": modifying the contents of the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot delete directory \"{Name}\": modifying the contents of this folder/using this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
innerInfo.Delete();
|
||||
@@ -524,9 +544,9 @@ namespace Barotrauma.IO
|
||||
}
|
||||
set
|
||||
{
|
||||
if (!Validation.CanWrite(innerInfo.FullName))
|
||||
if (!Validation.CanWrite(innerInfo.FullName, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot set read-only to {value} for \"{Name}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot set read-only to {value} for \"{Name}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
innerInfo.IsReadOnly = value;
|
||||
@@ -535,7 +555,7 @@ namespace Barotrauma.IO
|
||||
|
||||
public void CopyTo(string dest, bool overwriteExisting = false)
|
||||
{
|
||||
if (!Validation.CanWrite(dest))
|
||||
if (!Validation.CanWrite(dest, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot copy \"{Name}\" to \"{dest}\": modifying the contents of the destination folder is not allowed.");
|
||||
return;
|
||||
@@ -545,9 +565,9 @@ namespace Barotrauma.IO
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
if (!Validation.CanWrite(innerInfo.FullName))
|
||||
if (!Validation.CanWrite(innerInfo.FullName, false))
|
||||
{
|
||||
DebugConsole.ThrowError($"Cannot delete file \"{Name}\": modifying the files in the folder is not allowed.");
|
||||
DebugConsole.ThrowError($"Cannot delete file \"{Name}\": modifying the files in this folder/with this extension is not allowed.");
|
||||
return;
|
||||
}
|
||||
innerInfo.Delete();
|
||||
|
||||
Reference in New Issue
Block a user