Unstable 0.1500.2.0 (Rokvach's dog edition)

This commit is contained in:
Markus Isberg
2021-09-10 04:52:34 +09:00
parent e7b7c1a748
commit 1231170fce
126 changed files with 2424 additions and 1083 deletions
@@ -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;
}
@@ -228,9 +248,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);
@@ -238,9 +258,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?
@@ -257,9 +277,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);
@@ -267,12 +287,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;
@@ -282,9 +302,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);
@@ -304,15 +324,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));
}
@@ -334,9 +354,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);
@@ -344,9 +364,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);
@@ -354,9 +374,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);
@@ -396,7 +416,7 @@ namespace Barotrauma.IO
{
get
{
if (!Validation.CanWrite(fileName)) { return false; }
if (!Validation.CanWrite(fileName, false)) { return false; }
return innerStream.CanWrite;
}
}
@@ -422,13 +442,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.");
}
}
@@ -493,9 +513,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();
@@ -529,9 +549,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;
@@ -540,7 +560,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;
@@ -550,9 +570,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();