Files
SPT-Server-Build/Libraries/SPTarkov.Server.Core/Utils/FileUtil.cs
T
DrakiaXYZ d2e2f04c93 Merge 4.0.3 changes to main (#674)
* Fix exception sometimes thrown on save
- Switch back from File.Rename to File.Move, as Rename is throwing exceptions on some users systems

* Change BTR skin to tarcola during Christmas event

* Added comment

* Remove unused using

* Add wipe Response model

* formatting and add Wipe Endpoint to V2

* Format Style Fixes

* Merge pull request #669 from sp-tarkov/Assembly-ref-validation

Validate core assembly reference when loading mods

* removed zombies from customs and interchange + increased infection across other maps that have zombie kill quests

* Don't apply hostility changes to maps without zombies during halloween

`ReplaceBotHostiltiy` has optional map whitelist param

* Updated hostility values for maps with infection:
bosses = hostile to player not to pmc bots
followers = hostile to player not to pmc bots
pmcs = hostile to player + always hostile to scavs
scavs = hostile to player and pmc bots
raiders = hostile to player and pmc bots

Adjusted infection rates to just maps with zombie kill quests

* Format Style Fixes

* Added missing values for event bosses

* Format Style Fixes

* Added missing values for `ravangezryachiyevent`
Fixed preset typo `bossTagillaAgro`

* Format Style Fixes

* Flagged `Night of The Cult` as halloween quest

* Fixed incorrect logic

* Enabled `Night of The Cult` bosses to spawn

* Format Style Fixes

* Addd a new ReleaseCheckService to notify users of updates (#670)

* Addd a new ReleaseCheckService to notify users of updates
- Pulls the latest release from GitHub API to compare the tag against the users current SPT version
- Runs at the very end of the startup process to avoid being pushed off screen by mod logging
- Only notifies of patch version increments, not major or minor increments
- Links the release notes so users can Ctrl+Click to open directly to the upgrade page
- Is run on its own thread, and discards all errors, so as to not impact users without an internet connection or ability to access GitHub

* Formatting

* Use record for the ReleaseInformation class

---------

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>

* ProfileDataService changes:

Added `ClearProfileData()`
Replaced filepath access with `Path.Combine`
Reduced various sources of duplication

* Adjusted `Goons` spawn chance to 20% across `Customs/Lighthouse/Woods/Shoreline`

* Account for compound items in DialogHelper.GetMessageItemContents

* Generate weapon/armor price based on the child item price total

* Added halloween event bosses to april event

* Flagged infected spawns as `ForceSpawn` and ``

* Add migration for invalid pockets

* Default assign IEnumerable

* Post raid effect fixes:
When exiting raid with severe muscle pain, prevent client instructing server to add mild muscle pain
When exiting a raid with effect that has a timer, decrease timer value by amount of time spent in raid

* Updated nuget packages

* Fixed player scav not having correct HP values on limbs #642

* Remove unused record

* Revert "Updated nuget packages"

This reverts commit f6d9d461a6.

* Added `IMP mine detector` to reward and flea blacklist

* Fixed weapon builds not overwriting existing #654

Cleaned up `SaveWeaponBuild` and `SaveEquipmentBuild`

---------

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>
Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
Co-authored-by: Chomp <dev@dev.sp-tarkov.com>
Co-authored-by: CWX <CWXDEV@outlook.com>
Co-authored-by: sp-tarkov-bot <singleplayertarkov@gmail.com>
Co-authored-by: Cj <161484149+CJ-SPT@users.noreply.github.com>
Co-authored-by: Tyfon <29051038+tyfon7@users.noreply.github.com>
Co-authored-by: Archangel <jesse@archangel.wtf>
2025-10-31 14:55:07 -07:00

208 lines
5.3 KiB
C#

using System.Text;
using SPTarkov.DI.Annotations;
namespace SPTarkov.Server.Core.Utils;
[Injectable]
public class FileUtil
{
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)
{
return File.ReadAllText(path);
}
public async Task<string> ReadFileAsync(string path)
{
return await File.ReadAllTextAsync(path);
}
public async Task<byte[]> ReadFileAsBytesAsync(string path)
{
return await File.ReadAllBytesAsync(path);
}
public void WriteFile(string filePath, string fileContent)
{
if (!DirectoryExists(Path.GetDirectoryName(filePath)))
{
CreateDirectory(Path.GetDirectoryName(filePath));
}
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);
}
public async Task WriteFileAsync(string filePath, string fileContent)
{
var bytes = Encoding.UTF8.GetBytes(fileContent);
await WriteFileAsync(filePath, bytes);
}
/// <summary>
/// Writes a file atomically by first writing to a temporary file, then replacing the original.
/// This prevents corruption if the write operation fails or is interrupted.
/// </summary>
public async Task WriteFileAsync(string filePath, byte[] fileContent)
{
var directoryPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
var tempFilePath = filePath + ".bak";
try
{
await using (
var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)
)
{
await fs.WriteAsync(fileContent);
// We flush here so we can be sure it's immediately committed to disk
await fs.FlushAsync();
fs.Flush(true);
}
// Overwrite over the old file
File.Move(tempFilePath, filePath, overwrite: true);
}
catch
{
if (File.Exists(tempFilePath))
{
try
{
File.Delete(tempFilePath);
}
catch { }
}
throw;
}
}
private void CreateFile(string filePath)
{
var stream = File.Create(filePath);
stream.Close();
}
public bool DeleteFile(string filePath)
{
if (!FileExists(filePath))
{
return false;
}
File.Delete(filePath);
return true;
}
/// <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 bool CopyFile(string copyFromPath, string destinationFilePath, bool overwrite = false)
{
// Check it exists first
if (!FileExists(copyFromPath))
{
return false;
}
// Ensure dir exists
Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
// Copy the file
File.Copy(copyFromPath, destinationFilePath, overwrite);
return true;
}
/// <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);
}
}