Unstable 0.16.0.0

This commit is contained in:
Markus Isberg
2022-01-14 01:28:24 +09:00
parent d9baeaa2e1
commit 7d6421a548
237 changed files with 6430 additions and 2205 deletions
@@ -7,7 +7,7 @@ using System.Xml.Linq;
namespace Barotrauma
{
public class IdRemap
public sealed class IdRemap
{
public static readonly IdRemap DiscardId = new IdRemap(null, -1);
@@ -58,21 +58,37 @@ namespace Barotrauma.IO
public static class SafeXML
{
public static void SaveSafe(this System.Xml.Linq.XDocument doc, string path)
public static void SaveSafe(this System.Xml.Linq.XDocument doc, string path, bool throwExceptions = false)
{
if (!Validation.CanWrite(path, false))
{
DebugConsole.ThrowError($"Cannot save XML document to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
string errorMsg = $"Cannot save XML document to \"{path}\": modifying the files in this folder/with this extension is not allowed.";
if (throwExceptions)
{
throw new InvalidOperationException(errorMsg);
}
else
{
DebugConsole.ThrowError(errorMsg);
}
return;
}
doc.Save(path);
}
public static void SaveSafe(this System.Xml.Linq.XElement element, string path)
public static void SaveSafe(this System.Xml.Linq.XElement element, string path, bool throwExceptions = false)
{
if (!Validation.CanWrite(path, false))
{
DebugConsole.ThrowError($"Cannot save XML element to \"{path}\": modifying the files in this folder/with this extension is not allowed.");
string errorMsg = $"Cannot save XML element to \"{path}\": modifying the files in this folder/with this extension is not allowed.";
if (throwExceptions)
{
throw new InvalidOperationException(errorMsg);
}
else
{
DebugConsole.ThrowError(errorMsg);
}
return;
}
element.Save(path);
@@ -7,7 +7,8 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Steamworks.Data;
using Color = Microsoft.Xna.Framework.Color;
namespace Barotrauma
{
@@ -66,6 +67,7 @@ namespace Barotrauma
catch (Exception e)
{
DebugConsole.ThrowError("Failed to clear folder", e);
return;
}
try
@@ -75,6 +77,7 @@ namespace Barotrauma
catch (Exception e)
{
DebugConsole.ThrowError("Error saving gamesession", e);
return;
}
try
@@ -107,6 +110,7 @@ namespace Barotrauma
catch (Exception e)
{
DebugConsole.ThrowError("Error saving submarine", e);
return;
}
try
@@ -356,10 +360,10 @@ namespace Barotrauma
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));
int Readed = Read(zipStream, bytes, sizeof(int));
if (Readed < sizeof(int))
return false;
@@ -373,29 +377,29 @@ namespace Barotrauma
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iNameLen; i++)
{
zipStream.Read(bytes, 0, sizeof(char));
Read(zipStream, bytes, sizeof(char));
char c = BitConverter.ToChar(bytes, 0);
sb.Append(c);
}
string sFileName = sb.ToString();
fileName = sFileName;
progress?.Invoke(sFileName);
//Decompress file content
bytes = new byte[sizeof(int)];
zipStream.Read(bytes, 0, sizeof(int));
Read(zipStream, bytes, sizeof(int));
int iFileLen = BitConverter.ToInt32(bytes, 0);
bytes = new byte[iFileLen];
zipStream.Read(bytes, 0, bytes.Length);
Read(zipStream, bytes, bytes.Length);
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(
@@ -427,6 +431,26 @@ namespace Barotrauma
return true;
}
private static int Read(GZipStream zipStream, byte[] bytes, int amount)
{
int read = 0;
// FIXME workaround for .NET6 causing save decompression to fail
#if NET6_0 && LINUX
for (int i = 0; i < amount; i++)
{
int result = zipStream.ReadByte();
if (result < 0) { break; }
bytes[i] = (byte) result;
read++;
}
#else
read = zipStream.Read(bytes, 0, amount);
#endif
return read;
}
public static void DecompressToDirectory(string sCompressedFile, string sDir, ProgressDelegate progress)
{
DebugConsole.Log("Decompressing " + sCompressedFile + " to " + sDir + "...");
@@ -0,0 +1,18 @@
using System.Threading.Tasks;
namespace Barotrauma
{
static class TaskExtensions
{
public static bool TryGetResult<T>(this Task task, out T result)
{
if (task is Task<T> { IsCompletedSuccessfully: true } castTask)
{
result = castTask.Result;
return true;
}
result = default;
return false;
}
}
}
@@ -535,16 +535,6 @@ namespace Barotrauma
}
}
// Enum.HasFlag() sucks
public static bool IsBitSet<T>(this T self, T bit) where T : struct, Enum
{
// This uses Unsafe.As for performance reasons, as
// C# will otherwise not allow a T -> int cast
// without first casting to object, which would make
// this not any better than Enum.HasFlag
return (Unsafe.As<T, int>(ref self) & Unsafe.As<T, int>(ref bit)) != 0;
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);