IT BUILDS!!!

- Removed LocalizationServices and other sus things.
- Rewrote AssemblyLoader
[In Progress] SafeStorageService
[In Progress] LuaScriptLoader
This commit is contained in:
MapleWheels
2025-03-30 06:20:45 -04:00
committed by Maplewheels
parent 52d920d969
commit c6713f37bb
67 changed files with 3336 additions and 1283 deletions
@@ -5,7 +5,10 @@ using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
using Barotrauma.LuaCs.Services;
using Barotrauma.Steam;
using OneOf;
namespace Barotrauma.LuaCs.Data;
@@ -16,7 +19,6 @@ public partial record ModConfigInfo : IModConfigInfo
public ContentPackage Package { get; init; }
public string PackageName { get; init; }
public ImmutableArray<IAssemblyResourceInfo> Assemblies { get; init; }
public ImmutableArray<ILocalizationResourceInfo> Localizations { get; init; }
public ImmutableArray<ILuaScriptResourceInfo> LuaScripts { get; init; }
public ImmutableArray<IConfigResourceInfo> Configs { get; init; }
public ImmutableArray<IConfigProfileResourceInfo> ConfigProfiles { get; init; }
@@ -24,10 +26,9 @@ public partial record ModConfigInfo : IModConfigInfo
#endregion
#region DataContracts
#region DataContracts_Resources
public record AssemblyResourcesInfo(ImmutableArray<IAssemblyResourceInfo> Assemblies) : IAssembliesResourcesInfo;
public record LocalizationResourcesInfo(ImmutableArray<ILocalizationResourceInfo> Localizations) : ILocalizationsResourcesInfo;
public record LuaScriptsResourcesInfo(ImmutableArray<ILuaScriptResourceInfo> LuaScripts) : ILuaScriptsResourcesInfo;
public record ConfigResourcesInfo(ImmutableArray<IConfigResourceInfo> Configs) : IConfigsResourcesInfo;
public record ConfigProfilesResourcesInfo(ImmutableArray<IConfigProfileResourceInfo> ConfigProfiles) : IConfigProfilesResourcesInfo;
@@ -161,20 +162,7 @@ public record ConfigProfileResourceInfo : IConfigProfileResourceInfo
public ContentPackage OwnerPackage { get; init; }
}
public record LocalizationResourceInfo : ILocalizationResourceInfo
{
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public Platform SupportedPlatforms { get; init; }
public Target SupportedTargets { get; init; }
public int LoadPriority { get; init; }
public ImmutableArray<string> FilePaths { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public bool Optional { get; init; }
}
public readonly struct LuaScriptScriptResourceInfo : ILuaScriptResourceInfo
public readonly struct LuaScriptsResourceInfo : ILuaScriptResourceInfo
{
public ContentPackage OwnerPackage { get; init; }
public Platform SupportedPlatforms { get; init; }
@@ -189,3 +177,34 @@ public readonly struct LuaScriptScriptResourceInfo : ILuaScriptResourceInfo
}
#endregion
#region DataContracts_ParsedInfo
public record ConfigInfo : IConfigInfo
{
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public Type DataType { get; init; }
public OneOf<string, XElement> DefaultValue { get; init; }
public OneOf<string, XElement> Value { get; init; }
public RunState EditableStates { get; init; }
public NetSync NetSync { get; init; }
#if CLIENT // IConfigDisplayInfo
public string DisplayName { get; init; }
public string Description { get; init; }
public string DisplayCategory { get; init; }
public bool ShowInMenus { get; init; }
public string Tooltip { get; init; }
public string ImageIconPath { get; init; }
#endif
}
public record ConfigProfileInfo : IConfigProfileInfo
{
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public IReadOnlyList<(string ConfigName, OneOf<string, XElement> Value)> ProfileValues { get; init; }
}
#endregion
@@ -1,51 +1,43 @@
using System;
using System.Xml.Linq;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs.Data;
// TODO: Finish
/// <summary>
/// Parsed data from a configuration xml.
/// </summary>
public partial interface IConfigInfo : IDataInfo
{
/// <summary>
/// Specifies the data type this should be initialized to (ie. string, int, vector, etc.)
/// Custom types can be registered by mods.
/// Specifies the type initializer that will be used to instantiate the config var.
/// </summary>
Type DataType { get; }
/// <summary>
/// String version of the default value.
/// The default value.
/// </summary>
string DefaultValue { get; }
OneOf.OneOf<string, XElement> DefaultValue { get; }
/// <summary>
/// The value the last time this config was saved, if found in /data/.
/// The value the last time this config was saved. If not found, returns the default value.
/// <br/><b>[If(Type='<see cref="string"/>')]</b><br/>
/// The value is from the 'Value' Attribute. Typically used for types with single/simple values, such as primitives.
/// <br/><b>[If(Type='<see cref="XElement"/>')]</b><br/>
/// The value is from the first 'Value' child element. Typically used with complex config types, such as range and list.
/// </summary>
string StoredValue { get; }
OneOf.OneOf<string, XElement> Value { get; }
/// <summary>
/// Custom data storage for other type-specific information needed. IE. Used to store the min,
/// max and step values for the <b>IConfigRangeEntry(T)</b>.
/// In what <see cref="RunState"/>(s) is this config editable. Will be editable in the selected state, and lower value states.
/// <br/><br/>
/// <b>[Important]</b><br/> Setting this to value lower than 'Configuration` will render this config read-only.
/// <br/><br/><b>Expected Behaviour</b>:
/// <br/><b>[<see cref="RunState.Unloaded"/>|<see cref="RunState.Parsed"/>]</b>: Read-Only.
/// <br/><b>[<see cref="RunState.Configuration"/>]</b>: Can only be changed at the Main Menu (not in a lobby).
/// <br/><b>[<see cref="RunState.Running"/>]</b>: Can be changed at the Main Menu and while a lobby is active.
/// </summary>
string CustomParameters { get; }
/// <summary>
/// <b>[Multiplayer]</b><br/>
/// What permissions do clients require to change this setting.
/// </summary>
ClientPermissions RequiredPermissions { get; }
/// <summary>
/// In what <see cref="RunState"/>s is this config editable.
/// <br/>
/// Note: Setting this to value lower than 'Configuration` will render this config read-only.
/// </summary>
RunState CanEditStates { get; }
RunState EditableStates { get; }
/// <summary>
/// Network synchronization rules for this config.
/// </summary>
NetSync NetSync { get; }
/// <summary>
/// User friendly name or Localization Token.
/// </summary>
string DisplayName { get; }
/// <summary>
/// User friendly description or Localization Token.
/// </summary>
string Description { get; }
}
@@ -1,6 +1,9 @@
namespace Barotrauma.LuaCs.Data;
using System.Collections.Generic;
using System.Xml.Linq;
public interface IConfigProfileInfo
namespace Barotrauma.LuaCs.Data;
public interface IConfigProfileInfo : IDataInfo
{
IReadOnlyList<(string ConfigName, OneOf.OneOf<string, XElement> Value)> ProfileValues { get; }
}
@@ -1,12 +0,0 @@
using System.Collections.Generic;
using System.Globalization;
namespace Barotrauma.LuaCs.Data;
public interface ILocalizationInfo : IDataInfo
{
string Symbol { get; }
IReadOnlyDictionary<CultureInfo, RawLString> LocalizedValues { get; }
RawLString GetLocalizedString(CultureInfo locale);
RawLString GetLocalizedString(string cultureCode);
}
@@ -3,7 +3,7 @@
namespace Barotrauma.LuaCs.Data;
public partial interface IModConfigInfo : IAssembliesResourcesInfo,
ILocalizationsResourcesInfo, ILuaScriptsResourcesInfo, IConfigsResourcesInfo,
ILuaScriptsResourcesInfo, IConfigsResourcesInfo,
IConfigProfilesResourcesInfo
{
// package info
@@ -6,7 +6,6 @@ namespace Barotrauma.LuaCs.Data;
public interface IConfigResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IDataInfo { }
public interface IConfigProfileResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IDataInfo { }
public interface ILocalizationResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IDataInfo { }
/// <summary>
/// Represents loadable Lua files.
@@ -40,11 +39,6 @@ public interface IAssembliesResourcesInfo
ImmutableArray<IAssemblyResourceInfo> Assemblies { get; }
}
public interface ILocalizationsResourcesInfo
{
ImmutableArray<ILocalizationResourceInfo> Localizations { get; }
}
public interface ILuaScriptsResourcesInfo
{
ImmutableArray<ILuaScriptResourceInfo> LuaScripts { get; }
@@ -0,0 +1,206 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using Barotrauma.Networking;
using FluentResults;
namespace Barotrauma.LuaCs.Data;
// --- Storage Service
public interface IStorageServiceConfig
{
string LocalModsDirectory { get; }
string WorkshopModsDirectory { get; }
string GameSettingsConfigPath { get; }
#if CLIENT
string TempDownloadsDirectory { get; }
#endif
//ReadOnlyCollection<string> SafeIOReadDirectories { get; }
//ReadOnlyCollection<string> SafeIOWriteDirectories { get; }
IEnumerable<string> GlobalIOReadWhitelist();
IEnumerable<string> GlobalIOWriteWhitelist();
bool IOReadWhiteListContains(string filePath);
bool IOWriteWhiteListContains(string filePath);
string LocalDataSavePath { get; }
string LocalDataPathRegex { get; }
string LocalPackageDataPath { get; }
public string RunLocation { get; }
bool GlobalSafeIOEnabled { get; }
}
internal interface IStorageServiceConfigUpdate
{
public FluentResults.Result SetSafeReadFilePaths(string[] filePaths);
public FluentResults.Result SetSafeWriteFilePaths(string[] filePaths);
}
public record StorageServiceConfig : IStorageServiceConfig, IStorageServiceConfigUpdate
{
private static readonly string ExecutionLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.CleanUpPath());
public string LocalModsDirectory { get; init; } = System.IO.Path.GetFullPath(ContentPackage.LocalModsDir).CleanUpPath();
public string WorkshopModsDirectory { get; init; } = System.IO.Path.GetFullPath(ContentPackage.WorkshopModsDir).CleanUpPath();
public string GameSettingsConfigPath { get; init; } = System.IO.Path.GetFullPath(
string.IsNullOrEmpty(GameSettings.CurrentConfig.SavePath)
? SaveUtil.DefaultSaveFolder
: GameSettings.CurrentConfig.SavePath).CleanUpPath();
#if CLIENT
public string TempDownloadsDirectory { get; init; } = System.IO.Path.GetFullPath(ModReceiver.DownloadFolder).CleanUpPath();
#endif
private readonly AsyncReaderWriterLock _safeIOReadLock = new();
private readonly AsyncReaderWriterLock _safeIOWriteLock = new();
private readonly ConcurrentDictionary<string,byte> _safeIOReadFilePaths = new();
private readonly ConcurrentDictionary<string,byte> _safeIOWriteFilePaths = new();
public IEnumerable<string> GlobalIOReadWhitelist()
{
using var lck = _safeIOReadLock.AcquireReaderLock().GetAwaiter().GetResult();
if (_safeIOReadFilePaths.Count == 0)
{
yield break;
}
foreach (var path in _safeIOReadFilePaths)
{
yield return path.Key;
}
}
public IEnumerable<string> GlobalIOWriteWhitelist()
{
using var lck = _safeIOWriteLock.AcquireReaderLock().GetAwaiter().GetResult();
if (_safeIOWriteFilePaths.Count == 0)
{
yield break;
}
foreach (var path in _safeIOWriteFilePaths)
{
yield return path.Key;
}
}
public bool IOReadWhiteListContains(string filePath)
{
if (filePath.IsNullOrWhiteSpace())
return false;
return _safeIOReadFilePaths.ContainsKey(filePath);
}
public bool IOWriteWhiteListContains(string filePath)
{
if (filePath.IsNullOrWhiteSpace())
return false;
return _safeIOWriteFilePaths.ContainsKey(filePath);
}
public string LocalDataSavePath => Path.Combine(ExecutionLocation, "/Data/Mods/");
public string LocalDataPathRegex => "<PACKAGENAME>";
public string RunLocation => ExecutionLocation;
public bool GlobalSafeIOEnabled => false;
public string LocalPackageDataPath
{
get
{
return ContainsIllegalPaths(LocalDataSavePath) ? $"/Data/Mods/{LocalDataPathRegex}"
: Path.Combine(LocalDataSavePath, LocalDataPathRegex);
bool ContainsIllegalPaths(string path)
{
throw new NotImplementedException();
}
}
}
public FluentResults.Result SetSafeReadFilePaths(string[] filePaths)
{
using var lck = _safeIOReadLock.AcquireWriterLock().GetAwaiter().GetResult();
return SetSafeDirectory(_safeIOReadFilePaths, filePaths);
}
public FluentResults.Result SetSafeWriteFilePaths(string[] filePaths)
{
using var lck = _safeIOWriteLock.AcquireWriterLock().GetAwaiter().GetResult();
return SetSafeDirectory(_safeIOWriteFilePaths, filePaths);
}
private FluentResults.Result SetSafeDirectory(ConcurrentDictionary<string,byte> target, string[] filePaths)
{
if (filePaths is null || filePaths.Length < 1)
{
target.Clear();
return FluentResults.Result.Ok();
}
FluentResults.Result result = new();
target.Clear();
foreach (string path in filePaths)
{
if (path.IsNullOrWhiteSpace())
{
result = result.WithError($"ServicesConfigData: A supplied whitelist path was null.");
continue;
}
try
{
var path2 = Path.GetFullPath(path);
target.TryAdd(path2, 0);
}
catch (Exception e)
{
result = result.WithError(
new ExceptionalError(e).WithMetadata(FluentResults.LuaCs.MetadataType.ExceptionObject, this));
continue;
}
}
return result.WithSuccess($"Whitelist updated.");
}
}
// --- Config Service
public interface IConfigServiceConfig
{
string LocalConfigPathPartial { get; }
string FileNamePattern { get; }
}
public record ConfigServiceConfig : IConfigServiceConfig
{
public string LocalConfigPathPartial => $"/Config/{FileNamePattern}.xml";
public string FileNamePattern => "<ConfigName>";
}
// --- Lua Scripts Service
public interface ILuaScriptServicesConfig
{
bool SafeLuaIOEnabled { get; }
bool UseCaching { get; }
}
public record LuaScriptServicesConfig : ILuaScriptServicesConfig
{
public bool SafeLuaIOEnabled => true;
public bool UseCaching => true;
}