Unstable 0.1500.0.0

This commit is contained in:
Markus Isberg
2021-08-26 21:08:21 +09:00
parent 265a2e7ab3
commit 501e02c026
245 changed files with 9775 additions and 2034 deletions
@@ -12,7 +12,7 @@ namespace Barotrauma
private int maxId;
private readonly List<Point> srcRanges;
private readonly List<Range<int>> srcRanges;
private readonly int destOffset;
public IdRemap(XElement parentElement, int offset)
@@ -20,13 +20,13 @@ namespace Barotrauma
destOffset = offset;
if (parentElement != null && parentElement.HasElements)
{
srcRanges = new List<Point>();
srcRanges = new List<Range<int>>();
foreach (XElement subElement in parentElement.Elements())
{
int id = subElement.GetAttributeInt("ID", -1);
if (id > 0) { InsertId(id); }
}
maxId = GetOffsetId(srcRanges.Last().Y) + 1;
maxId = GetOffsetId(srcRanges.Last().End) + 1;
}
else
{
@@ -44,38 +44,38 @@ namespace Barotrauma
{
for (int i = 0; i < srcRanges.Count; i++)
{
if (srcRanges[i].X > id)
if (srcRanges[i].Start > id)
{
if (srcRanges[i].X == (id + 1))
if (srcRanges[i].Start == (id + 1))
{
srcRanges[i] = new Point(id, srcRanges[i].Y);
if (i > 0 && srcRanges[i].X == srcRanges[i - 1].Y)
srcRanges[i] = new Range<int>(id, srcRanges[i].End);
if (i > 0 && srcRanges[i].Start == srcRanges[i - 1].End)
{
srcRanges[i - 1] = new Point(srcRanges[i - 1].X, srcRanges[i].Y);
srcRanges[i - 1] = new Range<int>(srcRanges[i - 1].Start, srcRanges[i].End);
srcRanges.RemoveAt(i);
}
}
else
{
srcRanges.Insert(i, new Point(id, id));
srcRanges.Insert(i, new Range<int>(id, id));
}
return;
}
else if (srcRanges[i].Y < id)
else if (srcRanges[i].End < id)
{
if (srcRanges[i].Y == (id - 1))
if (srcRanges[i].End == (id - 1))
{
srcRanges[i] = new Point(srcRanges[i].X, id);
if (i < (srcRanges.Count - 1) && srcRanges[i].Y == srcRanges[i + 1].X)
srcRanges[i] = new Range<int>(srcRanges[i].Start, id);
if (i < (srcRanges.Count - 1) && srcRanges[i].End == srcRanges[i + 1].Start)
{
srcRanges[i] = new Point(srcRanges[i].X, srcRanges[i + 1].Y);
srcRanges[i] = new Range<int>(srcRanges[i].Start, srcRanges[i + 1].End);
srcRanges.RemoveAt(i + 1);
}
return;
}
}
}
srcRanges.Add(new Point(id, id));
srcRanges.Add(new Range<int>(id, id));
}
public ushort GetOffsetId(XElement element)
@@ -92,11 +92,11 @@ namespace Barotrauma
int currOffset = destOffset;
for (int i = 0; i < srcRanges.Count; i++)
{
if (id >= srcRanges[i].X && id <= srcRanges[i].Y)
if (id >= srcRanges[i].Start && id <= srcRanges[i].End)
{
return (ushort)(id - srcRanges[i].X + 1 + currOffset);
return (ushort)(id - srcRanges[i].Start + 1 + currOffset);
}
currOffset += srcRanges[i].Y - srcRanges[i].X + 1;
currOffset += srcRanges[i].End - srcRanges[i].Start + 1;
}
return 0;
}
@@ -29,6 +29,11 @@ namespace Barotrauma
return (i % n + n) % n;
}
public static float PositiveModulo(float i, float n)
{
return (i % n + n) % n;
}
public static double Distance(double x1, double y1, double x2, double y2)
{
double dX = x1 - x2;
@@ -0,0 +1,44 @@
using System;
namespace Barotrauma
{
public struct Range<T> where T : IComparable
{
private T start; private T end;
public T Start
{
get { return start; }
set
{
start = value;
VerifyStartLessThanEnd();
}
}
public T End
{
get { return end; }
set
{
end = value;
VerifyEndGreaterThanStart();
}
}
private void VerifyStartLessThanEnd()
{
if (start.CompareTo(end) > 0) { throw new InvalidOperationException($"Range<{typeof(T).Name}>.Start set to a value greater than End ({start} > {end})"); }
}
private void VerifyEndGreaterThanStart()
{
if (end.CompareTo(start) < 0) { throw new InvalidOperationException($"Range<{typeof(T).Name}>.End set to a value less than Start ({end} < {start})"); }
}
public Range(T start, T end)
{
this.start = start; this.end = end;
VerifyEndGreaterThanStart();
}
}
}
@@ -138,6 +138,11 @@ namespace Barotrauma.IO
return System.IO.Path.GetPathRoot(path);
}
public static string GetRelativePath(string relativeTo, string path)
{
return System.IO.Path.GetRelativePath(relativeTo, path);
}
public static string GetDirectoryName(string path)
{
return System.IO.Path.GetDirectoryName(path);
@@ -296,20 +296,10 @@ namespace Barotrauma
public static void CompressStringToFile(string fileName, string value)
{
// A.
// Write string to temporary file.
string temp = Path.GetTempFileName();
File.WriteAllText(temp, value);
// Convert the string to its byte representation.
byte[] b = Encoding.UTF8.GetBytes(value);
// B.
// Read file into byte array buffer.
byte[] b;
using (FileStream f = File.Open(temp, System.IO.FileMode.Open))
{
b = new byte[f.Length];
f.Read(b, 0, (int)f.Length);
}
// C.
// Use GZipStream to write compressed bytes to target file.
using (FileStream f2 = File.Open(fileName, System.IO.FileMode.Create))
using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
@@ -12,6 +12,7 @@ using System.Text;
namespace Barotrauma
{
[Obsolete("Use named tuples instead.")]
public class Pair<T1, T2>
{
public T1 First { get; set; }
@@ -24,20 +25,6 @@ namespace Barotrauma
}
}
public class Triplet<T1, T2, T3>
{
public T1 First { get; set; }
public T2 Second { get; set; }
public T3 Third { get; set; }
public Triplet(T1 first, T2 second, T3 third)
{
First = first;
Second = second;
Third = third;
}
}
public static partial class ToolBox
{
static internal class Epoch
@@ -555,15 +542,6 @@ namespace Barotrauma
return hex.ToString();
}
public static string ConvertAbsoluteToRelativePath(string path)
{
string[] splitted = path.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
string currentFolder = Environment.CurrentDirectory.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }).Last();
// Filter out the current folder -> result is "Content/blaahblaah" or "Mods/blaahblaah" etc.
IEnumerable<string> filtered = splitted.SkipWhile(part => part != currentFolder).Skip(1);
return string.Join("/", filtered);
}
public static string EscapeCharacters(string str)
{
return str.Replace("\\", "\\\\").Replace("\"", "\\\"");
@@ -640,6 +618,17 @@ namespace Barotrauma
Process.Start(startInfo);
}
/// <summary>
/// Cleans up a path by replacing backslashes with forward slashes, and
/// optionally corrects the casing of the path. Recommended when serializing
/// paths to a human-readable file to force case correction on all platforms.
/// Also useful when working with paths to files that currently don't exist,
/// i.e. case cannot be corrected.
/// </summary>
/// <param name="path">Path to clean up</param>
/// <param name="correctFilenameCase">Should the case be corrected to match the filesystem?</param>
/// <param name="directory">Directories that the path should be found in, not returned.</param>
/// <returns>Path with corrected slashes, and corrected case if requested.</returns>
public static string CleanUpPathCrossPlatform(this string path, bool correctFilenameCase = true, string directory = "")
{
if (string.IsNullOrEmpty(path)) { return ""; }
@@ -659,21 +648,24 @@ namespace Barotrauma
return path;
}
/// <summary>
/// Cleans up a path by replacing backslashes with forward slashes, and
/// corrects the casing of the path on non-Windows platforms. Recommended
/// when loading a path from a file, to make sure that it is found on all
/// platforms when attempting to open it.
/// </summary>
/// <param name="path">Path to clean up</param>
/// <returns>Path with corrected slashes, and corrected case if required by the platform.</returns>
public static string CleanUpPath(this string path)
{
if (string.IsNullOrEmpty(path)) { return ""; }
path = path.Replace('\\', '/');
while (path.IndexOf("//") >= 0)
{
path = path.Replace("//", "/");
}
#if LINUX || OSX
//required on *nix platforms to load in mods made on Windows
string correctedPath = CorrectFilenameCase(path, out _);
if (!string.IsNullOrEmpty(correctedPath)) { path = correctedPath; }
return path.CleanUpPathCrossPlatform(
correctFilenameCase:
#if WINDOWS
false
#else
true
#endif
return path;
);
}
public static float GetEasing(TransitionMode easing, float t)
@@ -1,223 +0,0 @@
using System;
using System.Collections.Generic;
using Barotrauma.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;
namespace Barotrauma
{
public static class UpdaterUtil
{
public const string Version = "1.1";
public static void SaveFileList(string filePath)
{
XDocument doc = new XDocument(CreateFileList());
doc.SaveSafe(filePath);
}
public static XElement CreateFileList()
{
XElement root = new XElement("filelist");
string currentDir = Directory.GetCurrentDirectory();
IEnumerable<string> files = Directory.GetFiles(currentDir, "*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
XElement fileElement = new XElement("file");
fileElement.Add(new XAttribute("path", GetRelativePath(file, currentDir)));
fileElement.Add(new XAttribute("md5", GetFileMd5Hash(file)));
root.Add(fileElement);
}
return root;
}
public static List<string> GetFileList(XDocument fileListDoc)
{
List<string> fileList = new List<string>();
XElement fileListElement = fileListDoc.Root;
if (fileListElement == null)
{
throw new Exception("Received list of new files was corrupted");
}
foreach (XElement file in fileListElement.Elements())
{
string filePath = file.GetAttributeString("path", "");
fileList.Add(filePath);
}
return fileList;
}
public static List<string> GetRequiredFiles(XDocument fileListDoc)
{
List<string> requiredFiles = new List<string>();
XElement fileList = fileListDoc.Root;
if (fileList==null)
{
throw new Exception("Received list of new files was corrupted");
}
foreach (XElement file in fileList.Elements())
{
string filePath = file.GetAttributeString("path", "");
if (!File.Exists(filePath))
{
requiredFiles.Add(filePath);
continue;
}
string md5 = file.GetAttributeString("md5", "");
if (GetFileMd5Hash(filePath) != md5)
{
requiredFiles.Add(filePath);
}
}
return requiredFiles;
}
private static string GetFileMd5Hash(string filePath)
{
Md5Hash md5Hash = null;
var md5 = MD5.Create();
using (var stream = File.OpenRead(filePath))
{
md5Hash = new Md5Hash(md5.ComputeHash(stream));
}
return md5Hash.Hash;
}
public static string GetRelativePath(string filespec, string folder)
{
Uri pathUri = new Uri(filespec);
// Folders must end in a slash
if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
folder += Path.DirectorySeparatorChar;
}
Uri folderUri = new Uri(folder);
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
/// <summary>
/// moves the files in the updatefolder to the install folder
/// if there's an existing file with the same name in the install folder and it can't be removed,
/// it will be renamed as "OLD_[filename]"
/// </summary>
/// <param name="updateFileFolder"></param>
public static void InstallUpdatedFiles(string updateFileFolder)
{
IEnumerable<string> files = Directory.GetFiles(updateFileFolder, "*", System.IO.SearchOption.AllDirectories);
string currentDir = Directory.GetCurrentDirectory();
foreach (string file in files)
{
string fileRelPath = GetRelativePath(file, updateFileFolder);
if (File.Exists(fileRelPath))
{
try
{
File.Delete(fileRelPath);
}
//couldn't delete file, probably because it's already in use
catch
{
string oldFileName = Path.Combine(currentDir, Path.GetDirectoryName(fileRelPath), "OLD_"+Path.GetFileName(fileRelPath));
if (File.Exists(oldFileName)) File.Delete(oldFileName);
File.Move(fileRelPath, oldFileName);
}
}
string directoryName = Path.GetDirectoryName(fileRelPath);
if (!string.IsNullOrWhiteSpace(directoryName))
{
Directory.CreateDirectory(directoryName);
}
System.Diagnostics.Debug.WriteLine("moving: "+file+" -> "+fileRelPath);
File.Move(file, fileRelPath);
}
Directory.Delete(updateFileFolder, true);
}
public static void CleanUnnecessaryFiles(List<string> filesToKeep)
{
string currentDir = Directory.GetCurrentDirectory();
IEnumerable<string> files = Directory.GetFiles(currentDir, "*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
string relativePath = GetRelativePath(file, currentDir);
string dirRoot = relativePath.Split(Path.DirectorySeparatorChar).First();
if (dirRoot != "Content") continue;
if (filesToKeep.Contains(relativePath)) continue;
if (Path.GetFileName(file).Split('_').First() == "OLD") continue;
System.Diagnostics.Debug.WriteLine("deleting file "+file);
try
{
File.Delete(file);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Could not delete file \"" + file + "\" (" + e.Message + ")");
continue;
}
}
}
public static void CleanOldFiles()
{
string currentDir = Directory.GetCurrentDirectory();
IEnumerable<string> files = Directory.GetFiles(currentDir, "*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
if (Path.GetFileName(file).Split('_').First() != "OLD") continue;
System.Diagnostics.Debug.WriteLine("deleting file " + file);
try
{
File.Delete(file);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Could not delete file \"" + file + "\" (" + e.Message + ")");
continue;
}
}
}
}
}