fix: Cleans up file system paths and archiving (#815)
* Makes EnsureDirectory properly work for relative and absolute paths * Adds a `PathUtility.GetFullPath` which returns full paths for relative paths to `Core.BaseDirectory`. If the path is absolute, it will return as-is. * Moves EnsureDirectory to `PathUtility`. So `ScriptsHandler.EnsureDirectory` and `AssemblyHandler.EnsureDirectory` are now `PathUtility.EnsureDirectory` * Fixes crash guard so that it copies accounts properly. * Changes world save and auto archive to use a random folder name inside of the temp folder.
This commit is contained in:
parent
da31153b20
commit
a55e271a69
17 changed files with 447 additions and 341 deletions
67
Projects/Server/Utilities/PathUtility.cs
Normal file
67
Projects/Server/Utilities/PathUtility.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: PathUtility.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public static class PathUtility
|
||||
{
|
||||
public static string EnsureDirectory(string dir)
|
||||
{
|
||||
var path = GetFullPath(dir, Core.BaseDirectory);
|
||||
Directory.CreateDirectory(path);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void EnsureDirectory(this FileInfo fi)
|
||||
{
|
||||
var dir = GetFullPath(fi.DirectoryName, Core.BaseDirectory);
|
||||
if (dir != null)
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EnsureDirectory(this DirectoryInfo di)
|
||||
{
|
||||
var file = GetFullPath(di.FullName, Core.BaseDirectory);
|
||||
Directory.CreateDirectory(file);
|
||||
}
|
||||
|
||||
public static string GetFullPath(string relativeOrAbsolutePath) =>
|
||||
GetFullPath(relativeOrAbsolutePath, Core.BaseDirectory);
|
||||
|
||||
public static string GetFullPath(string relativeOrAbsolutePath, string basePath) =>
|
||||
relativeOrAbsolutePath switch
|
||||
{
|
||||
null => null,
|
||||
"" => basePath,
|
||||
_ => Path.IsPathRooted(relativeOrAbsolutePath)
|
||||
? relativeOrAbsolutePath
|
||||
: Path.GetFullPath(relativeOrAbsolutePath, basePath)
|
||||
};
|
||||
|
||||
public static string EnsureRandomPath(string basePath)
|
||||
{
|
||||
Span<byte> bytes = stackalloc byte[8];
|
||||
Utility.RandomBytes(bytes);
|
||||
return EnsureDirectory(Path.Combine(basePath, bytes.ToHexString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1331,28 +1331,6 @@ namespace Server
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T Max<T>(T val, T max) where T : IComparable<T> => val.CompareTo(max) > 0 ? val : max;
|
||||
|
||||
public static string TrimMultiline(this string str, string lineSeparator = "\n")
|
||||
{
|
||||
var parts = str.Split(lineSeparator);
|
||||
for (var i = 0; i < parts.Length; i++)
|
||||
{
|
||||
parts[i] = parts[i].Trim();
|
||||
}
|
||||
|
||||
return string.Join(lineSeparator, parts);
|
||||
}
|
||||
|
||||
public static string IndentMultiline(this string str, string indent = "\t", string lineSeparator = "\n")
|
||||
{
|
||||
var parts = str.Split(lineSeparator);
|
||||
for (var i = 0; i < parts.Length; i++)
|
||||
{
|
||||
parts[i] = $"{indent}{parts[i]}";
|
||||
}
|
||||
|
||||
return string.Join(lineSeparator, parts);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Tidy<T>(this List<T> list) where T : ISerializable
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue