Unstable 0.1500.2.0 (Rokvach's dog edition)
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;
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Barotrauma.IO;
|
||||
using System.IO.Compression;
|
||||
@@ -352,8 +353,10 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
public static bool DecompressFile(string sDir, GZipStream zipStream, ProgressDelegate progress)
|
||||
private static bool DecompressFile(bool writeFile, string sDir, GZipStream zipStream, ProgressDelegate progress, out string fileName)
|
||||
{
|
||||
fileName = null;
|
||||
|
||||
//Decompress file name
|
||||
byte[] bytes = new byte[sizeof(int)];
|
||||
int Readed = zipStream.Read(bytes, 0, sizeof(int));
|
||||
@@ -375,6 +378,8 @@ namespace Barotrauma
|
||||
sb.Append(c);
|
||||
}
|
||||
string sFileName = sb.ToString();
|
||||
|
||||
fileName = sFileName;
|
||||
progress?.Invoke(sFileName);
|
||||
|
||||
//Decompress file content
|
||||
@@ -387,6 +392,17 @@ namespace Barotrauma
|
||||
|
||||
string sFilePath = Path.Combine(sDir, sFileName);
|
||||
string sFinalDir = Path.GetDirectoryName(sFilePath);
|
||||
|
||||
string sDirFull = (string.IsNullOrEmpty(sDir) ? Directory.GetCurrentDirectory() : Path.GetFullPath(sDir)).CleanUpPathCrossPlatform(correctFilenameCase: false);
|
||||
string sFinalDirFull = (string.IsNullOrEmpty(sFinalDir) ? Directory.GetCurrentDirectory() : Path.GetFullPath(sFinalDir)).CleanUpPathCrossPlatform(correctFilenameCase: false);
|
||||
|
||||
if (!sFinalDirFull.StartsWith(sDirFull, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Error extracting \"{sFileName}\": cannot be extracted to parent directory");
|
||||
}
|
||||
|
||||
if (!writeFile) { return true; }
|
||||
if (!Directory.Exists(sFinalDir))
|
||||
Directory.CreateDirectory(sFinalDir);
|
||||
|
||||
@@ -421,7 +437,7 @@ namespace Barotrauma
|
||||
{
|
||||
using (FileStream inFile = File.Open(sCompressedFile, System.IO.FileMode.Open, System.IO.FileAccess.Read))
|
||||
using (GZipStream zipStream = new GZipStream(inFile, CompressionMode.Decompress, true))
|
||||
while (DecompressFile(sDir, zipStream, progress)) { };
|
||||
while (DecompressFile(true, sDir, zipStream, progress, out _)) { };
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -434,6 +450,35 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> EnumerateContainedFiles(string sCompressedFile)
|
||||
{
|
||||
int maxRetries = 4;
|
||||
HashSet<string> paths = new HashSet<string>();
|
||||
for (int i = 0; i <= maxRetries; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using FileStream inFile = File.Open(sCompressedFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
|
||||
using GZipStream zipStream = new GZipStream(inFile, CompressionMode.Decompress, true);
|
||||
while (DecompressFile(false, "", zipStream, null, out string fileName))
|
||||
{
|
||||
paths.Add(fileName);
|
||||
}
|
||||
}
|
||||
catch (System.IO.IOException e)
|
||||
{
|
||||
if (i >= maxRetries || !File.Exists(sCompressedFile)) { throw; }
|
||||
|
||||
DebugConsole.NewMessage(
|
||||
$"Failed to decompress file \"{sCompressedFile}\" for enumeration {{{e.Message}}}, retrying in 250 ms...",
|
||||
Color.Red);
|
||||
Thread.Sleep(250);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
public static void CopyFolder(string sourceDirName, string destDirName, bool copySubDirs, bool overwriteExisting = false)
|
||||
{
|
||||
// Get the subdirectories for the specified directory.
|
||||
|
||||
Reference in New Issue
Block a user