Files
SPT-Server-Build/Libraries/Core/Utils/FileUtil.cs
T
clodanSPT 2464246fd7 Pre spt mod loader (#102)
* partial commit

* pre spt mod loader refactored

---------

Co-authored-by: Alex <alex@dm-me-for-questions.com>
Co-authored-by: clodan <clodan@clodan.com>
2025-02-25 16:34:51 +00:00

145 lines
3.6 KiB
C#

using Core.Models.Utils;
using SptCommon.Annotations;
namespace Core.Utils;
[Injectable]
public class FileUtil(
ISptLogger<FileUtil> _logger)
{
protected const string _modBasePath = "user/mods/";
public List<string> GetFiles(string path, bool recursive = false, string searchPattern = "*")
{
var files = new List<string>(Directory.GetFiles(path, searchPattern));
if (recursive)
{
files.AddRange(Directory.GetDirectories(path).SelectMany(d => GetFiles(d, recursive, searchPattern)));
}
return files;
}
public string[] GetDirectories(string path)
{
return Directory.GetDirectories(path);
}
public string GetFileExtension(string path)
{
return Path.GetExtension(path).Replace(".", "");
}
public string GetFileNameAndExtension(string path)
{
return Path.GetFileName(path);
}
public string StripExtension(string path, bool keepPath = false)
{
if (keepPath)
{
return path.StartsWith(".") ? path.Split('.')[1] : path.Split('.').First();
}
return Path.GetFileNameWithoutExtension(path);
}
public bool DirectoryExists(string path)
{
return Directory.Exists(path);
}
public DirectoryInfo CreateDirectory(string path)
{
return Directory.CreateDirectory(path);
}
public bool FileExists(string path)
{
return File.Exists(path);
}
public string ReadFile(string path)
{
using var reader = new StreamReader(path);
return reader.ReadToEnd();
}
public void WriteFile(string filePath, string fileContent)
{
if (!FileExists(filePath))
{
CreateFile(filePath);
}
File.WriteAllText(filePath, fileContent);
}
public void WriteFile(string filePath, byte[] fileContent)
{
if (!FileExists(filePath))
{
CreateFile(filePath);
}
File.WriteAllBytes(filePath, fileContent);
}
private void CreateFile(string filePath)
{
var stream = File.Create(filePath);
stream.Close();
}
public void DeleteFile(string filePath)
{
if (!FileExists(filePath))
{
_logger.Error($"Unable to delete file, not found: {filePath}");
return;
}
File.Delete(filePath);
}
/// <summary>
/// Copy a file from one path to another
/// </summary>
/// <param name="copyFromPath">Source file to copy from</param>
/// <param name="destinationFilePath"></param>
/// <param name="overwrite">Should destination file be overwritten</param>
public void CopyFile(string copyFromPath, string destinationFilePath, bool overwrite = false)
{
// Check it exists first
if (!FileExists(copyFromPath))
{
_logger.Error($"Source file not found: {copyFromPath}. Cannot copy to: {destinationFilePath}");
}
// Ensure dir exists
Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
// Copy the file
File.Copy(copyFromPath, destinationFilePath, overwrite);
}
/// <summary>
/// Delete a directory, must be empty unless 'deleteContent' is set to 'true'
/// </summary>
/// <param name="directory"></param>
/// <param name="deleteContent"></param>
public void DeleteDirectory(string directory, bool deleteContent = false)
{
Directory.Delete(directory, deleteContent);
}
public string GetModPath(string modName)
{
return Path.Combine(_modBasePath, modName);
}
}