- SafeStorageService glow up.
- ILuaScriptLoader now inherits the ISafeStorageValidation interface. - LuaScriptLoader now uses the SafeStorageService.
This commit is contained in:
@@ -15,99 +15,77 @@ namespace Barotrauma.LuaCs.Services.Safe
|
||||
{
|
||||
public class LuaScriptLoader : ScriptLoaderBase, ILuaScriptLoader
|
||||
{
|
||||
public LuaScriptLoader(IStorageService storageService, Lazy<ILoggerService> loggerService, ILuaScriptServicesConfig luaScriptServicesConfig)
|
||||
public LuaScriptLoader(ISafeStorageService storageService, Lazy<ILoggerService> loggerService)
|
||||
{
|
||||
this._storageService = storageService;
|
||||
this._loggerService = loggerService;
|
||||
this._luaScriptServicesConfig = luaScriptServicesConfig;
|
||||
_storageService.UseCaching = _luaScriptServicesConfig.UseCaching;
|
||||
if (_luaScriptServicesConfig.SafeLuaIOEnabled)
|
||||
{
|
||||
//_storageService.EnableWhitelistOnly();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly IStorageService _storageService;
|
||||
private readonly ISafeStorageService _storageService;
|
||||
private readonly Lazy<ILoggerService> _loggerService;
|
||||
private readonly ILuaScriptServicesConfig _luaScriptServicesConfig;
|
||||
|
||||
public override object LoadFile(string file, Table globalContext)
|
||||
{
|
||||
((IService)this).CheckDisposed();
|
||||
|
||||
if (!CanReadFromPath(file))
|
||||
IService.CheckDisposed(this);
|
||||
if (file.IsNullOrWhiteSpace())
|
||||
{
|
||||
LogErrors<string>($"File access to \"{file}\" is not allowed.");
|
||||
return null;
|
||||
}
|
||||
|
||||
var res = _storageService.TryLoadText(file);
|
||||
|
||||
if (res.IsFailed || res is not { Value: { } script})
|
||||
{
|
||||
UnsafeLogErrors($"Failed to load file '{file}'.", res.ToResult());
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_storageService.TryLoadText(file) is not { IsSuccess: true, Value: not null } script)
|
||||
if (script.IsNullOrWhiteSpace())
|
||||
{
|
||||
LogErrors<string>($"Failed to load file \"{file}\".");
|
||||
UnsafeLogErrors($"The file '{file}' is empty. ", res.ToResult());
|
||||
return null;
|
||||
}
|
||||
|
||||
if (script.Value.IsNullOrWhiteSpace())
|
||||
{
|
||||
LogErrors<string>($"The file \"{file}\" was empty.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return script.Value;
|
||||
return script;
|
||||
}
|
||||
|
||||
public void ClearCaches()
|
||||
{
|
||||
((IService)this).CheckDisposed();
|
||||
IService.CheckDisposed(this);
|
||||
_storageService?.PurgeCache();
|
||||
}
|
||||
|
||||
public async Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos)
|
||||
{
|
||||
// TODO: Needs an async lock?
|
||||
IService.CheckDisposed(this);
|
||||
if (!_storageService.UseCaching)
|
||||
{
|
||||
return FluentResults.Result.Fail($"Caching is not enabled.");
|
||||
}
|
||||
|
||||
return await this._storageService.LoadPackageTextFilesAsync([..resourceInfos.SelectMany(ri => ri.FilePaths)]);
|
||||
}
|
||||
|
||||
public override bool ScriptFileExists(string file)
|
||||
{
|
||||
((IService)this).CheckDisposed();
|
||||
|
||||
if (!CanReadFromPath(file))
|
||||
{
|
||||
LogErrors<string>($"File access to \"{file}\" is not allowed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
IService.CheckDisposed(this);
|
||||
var result = _storageService.FileExists(file);
|
||||
|
||||
if (result is { IsFailed: true })
|
||||
{
|
||||
LogErrors<string>($"Unable to find and load file \"{file}\".");
|
||||
UnsafeLogErrors($"Unable to find and load file \"{file}\".", result.ToResult());
|
||||
return false;
|
||||
}
|
||||
|
||||
return result.IsSuccess;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool CanReadFromPath(string file)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private bool CanWriteToPath(string file)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void LogErrors<T>(string message, FluentResults.Result<T> result = null)
|
||||
private void UnsafeLogErrors(string message, FluentResults.Result result = null)
|
||||
{
|
||||
_loggerService.Value.LogError($"{nameof(LuaScriptLoader)}: {message}");
|
||||
|
||||
if (result is null || result.Errors.Count <= 0)
|
||||
if (result is null || result.Errors.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
@@ -117,14 +95,58 @@ namespace Barotrauma.LuaCs.Services.Safe
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (IsDisposed)
|
||||
if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed))
|
||||
{
|
||||
return;
|
||||
IsDisposed = true;
|
||||
}
|
||||
|
||||
_storageService?.Dispose();
|
||||
_loggerService?.Value.Dispose();
|
||||
}
|
||||
|
||||
public bool IsDisposed { get; private set; }
|
||||
private int _isDisposed = 0;
|
||||
public bool IsDisposed => ModUtils.Threading.GetBool(ref _isDisposed);
|
||||
|
||||
public bool IsFileAccessible(string path, bool readOnly, bool checkWhitelistOnly = true)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
return _storageService.IsFileAccessible(path, readOnly, checkWhitelistOnly);
|
||||
}
|
||||
|
||||
public void AddFileToWhitelist(string path, bool readOnly = true)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
_storageService.AddFileToWhitelist(path, readOnly);
|
||||
}
|
||||
|
||||
public void AddFilesToWhitelist(ImmutableArray<string> paths, bool readOnly = true)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
_storageService.AddFilesToWhitelist(paths, readOnly);
|
||||
}
|
||||
|
||||
public void RemoveFileFromAllWhitelists(string path)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
_storageService.RemoveFileFromAllWhitelists(path);
|
||||
}
|
||||
|
||||
public FluentResults.Result SetReadOnlyWhitelist(ImmutableArray<string> filePaths)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
return _storageService.SetReadOnlyWhitelist(filePaths);
|
||||
}
|
||||
|
||||
public FluentResults.Result SetReadWriteWhitelist(ImmutableArray<string> filePaths)
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
return _storageService.SetReadWriteWhitelist(filePaths);
|
||||
}
|
||||
|
||||
public void ClearAllWhitelists()
|
||||
{
|
||||
IService.CheckDisposed(this);
|
||||
_storageService.ClearAllWhitelists();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user