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
@@ -0,0 +1,26 @@
using System;
using System.Collections.Immutable;
using Barotrauma.LuaCs.Configuration;
using Barotrauma.LuaCs.Data;
using Barotrauma.Networking;
using FluentResults;
namespace Barotrauma.LuaCs.Services;
public partial class ConfigService
{
public ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigs()
{
throw new NotImplementedException();
}
public ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigsForPackage(ContentPackage package)
{
throw new NotImplementedException();
}
public Result<IConfigControl> AddConfigControl(IConfigInfo configInfo)
{
throw new NotImplementedException();
}
}
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Barotrauma.LuaCs.Configuration;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
@@ -8,28 +10,8 @@ namespace Barotrauma.LuaCs.Services;
public partial interface IConfigService
{
/*
* Immediate mode
*/
FluentResults.Result<IConfigEntry<T>> AddConfigEntry<T>(IDisplayableData data,
T defaultValue,
NetSync syncMode = NetSync.None,
ClientPermissions permissions = ClientPermissions.None,
Func<T, bool> valueChangePredicate = null,
Action<IConfigEntry<T>> onValueChanged = null) where T : IConvertible, IEquatable<T>;
FluentResults.Result<IConfigList> AddConfigList(IDisplayableData data,
int defaultIndex, IReadOnlyList<string> values,
NetSync syncMode = NetSync.None,
ClientPermissions permissions = ClientPermissions.None,
Func<IConfigList, int, bool> valueChangePredicate = null,
Action<IConfigList, int> onValueChanged = null);
ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigs();
ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigsForPackage(ContentPackage package);
FluentResults.Result<IConfigRangeEntry<T>> AddConfigRangeEntry<T>(IDisplayableData data,
T defaultValue, T minValue, T maxValue,
Func<IConfigRangeEntry<T>, int> getStepCount,
NetSync syncMode = NetSync.None,
ClientPermissions permissions = ClientPermissions.None,
Func<T, bool> valueChangePredicate = null,
Action<IConfigEntry<T>> onValueChanged = null) where T : IConvertible, IEquatable<T>;
FluentResults.Result<IConfigControl> AddConfigControl(IConfigInfo configInfo);
}
@@ -1,13 +0,0 @@
using System.Collections.Immutable;
using System.Threading.Tasks;
using Barotrauma.LuaCs.Data;
namespace Barotrauma.LuaCs.Services;
public interface IStylesManagementService : IReusableService
{
Task<FluentResults.Result> LoadStylesAsync(ImmutableArray<IStylesResourceInfo> styles);
FluentResults.Result<IStylesService> GetStylesService(ContentPackage package);
Task<FluentResults.Result> DisposeAllStyles();
Task<FluentResults.Result> DisposeStylesForPackage(ContentPackage package);
}
@@ -1,58 +0,0 @@
using System.Threading.Tasks;
namespace Barotrauma.LuaCs.Services;
// TODO: Rework interface to support resource infos.
/// <summary>
/// Loads XML Style assets from the given content package.
/// </summary>
public interface IStylesService : IService
{
/// <summary>
/// Tries to load the styles file for the given <see cref="ContentPackage"/> and path into a new <see cref="UIStyleProcessor"/> instance.
/// </summary>
/// <param name="package"></param>
/// <param name="path"></param>
/// <returns></returns>
Task<FluentResults.Result> LoadStylesFileAsync(ContentPackage package, ContentPath path);
/// <summary>
/// Unloads all styles assets and <see cref="UIStyleProcessor"/> instances.
/// </summary>
FluentResults.Result UnloadAllStyles();
/// <summary>
/// Tries to the get the <see cref="GUIFont"/> asset by xml asset name, returns null on failure.
/// </summary>
/// <param name="fontName">XML Name of the asset.</param>
/// <returns>The asset or null if none are found.</returns>
GUIFont GetFont(string fontName);
/// <summary>
/// Tries to the get the <see cref="GUISprite"/> asset by xml asset name, returns null on failure.
/// </summary>
/// <param name="spriteName">XML Name of the asset.</param>
/// <returns>The asset or null if none are found.</returns>
GUISprite GetSprite(string spriteName);
/// <summary>
/// Tries to the get the <see cref="GUISpriteSheet"/> asset by xml asset name, returns null on failure.
/// </summary>
/// <param name="spriteSheetName">XML Name of the asset.</param>
/// <returns>The asset or null if none are found.</returns>
GUISpriteSheet GetSpriteSheet(string spriteSheetName);
/// <summary>
/// Tries to the get the <see cref="GUICursor"/> asset by xml asset name, returns null on failure.
/// </summary>
/// <param name="cursorName">XML Name of the asset.</param>
/// <returns>The asset or null if none are found.</returns>
GUICursor GetCursor(string cursorName);
/// <summary>
/// Tries to the get the <see cref="GUIColor"/> asset by xml asset name, returns null on failure.
/// </summary>
/// <param name="colorName">XML Name of the asset.</param>
/// <returns>The asset or null if none are found.</returns>
GUIColor GetColor(string colorName);
}
@@ -1,13 +1,14 @@
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Barotrauma.LuaCs.Services;
partial class NetworkingService : INetworkingService
{
private Dictionary<ushort, Queue<IReadMessage>> receiveQueue = new Dictionary<ushort, Queue<IReadMessage>>();
private ConcurrentDictionary<ushort, ConcurrentQueue<IReadMessage>> receiveQueue = new();
public void SendSyncMessage()
{
@@ -99,7 +100,7 @@ partial class NetworkingService : INetworkingService
}
else
{
if (!receiveQueue.ContainsKey(id)) { receiveQueue[id] = new Queue<IReadMessage>(); }
if (!receiveQueue.ContainsKey(id)) { receiveQueue[id] = new ConcurrentQueue<IReadMessage>(); }
receiveQueue[id].Enqueue(netMessage);
if (GameSettings.CurrentConfig.VerboseLogging)
@@ -1,81 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services.Processing;
using FluentResults;
// ReSharper disable UseCollectionExpression
namespace Barotrauma.LuaCs.Services;
public partial class PackageManagementService : IPackageManagementService
{
public PackageManagementService(
IConverterServiceAsync<ContentPackage, IModConfigInfo> modConfigParserService,
IProcessorService<IReadOnlyList<IAssemblyResourceInfo>, IAssembliesResourcesInfo> assemblyInfoConverter,
IProcessorService<IReadOnlyList<IConfigResourceInfo>, IConfigsResourcesInfo> configsInfoConverter,
IProcessorService<IReadOnlyList<IConfigProfileResourceInfo>, IConfigProfilesResourcesInfo> configProfilesConverter,
IProcessorService<IReadOnlyList<ILocalizationResourceInfo>, ILocalizationsResourcesInfo> localizationsConverter,
IProcessorService<IReadOnlyList<ILuaScriptResourceInfo>, ILuaScriptsResourcesInfo> luaScriptsConverter,
IPackageInfoLookupService packageInfoLookupService, Func<IReadOnlyList<IStylesResourceInfo>, IStylesResourcesInfo> stylesInfoConverter)
{
_stylesInfoConverter = stylesInfoConverter;
_modConfigParserService = modConfigParserService;
_assemblyInfoConverter = assemblyInfoConverter;
_configsInfoConverter = configsInfoConverter;
_configProfilesConverter = configProfilesConverter;
_localizationsConverter = localizationsConverter;
_luaScriptsConverter = luaScriptsConverter;
_packageInfoLookupService = packageInfoLookupService;
}
private readonly Func<IReadOnlyList<IStylesResourceInfo>, IStylesResourcesInfo> _stylesInfoConverter;
public ImmutableArray<IStylesResourceInfo> Styles => _modInfos.IsEmpty ? ImmutableArray<IStylesResourceInfo>.Empty
: _modInfos.SelectMany(kvp => kvp.Value.Styles).ToImmutableArray();
public Result<IStylesResourcesInfo> GetStylesInfos(ContentPackage package, bool onlySupportedResources = true)
{
((IService)this).CheckDisposed();
if (package is null)
return FluentResults.Result.Fail($"{nameof(GetStylesInfos)}: ContentPackage is null.");
if (_modInfos.TryGetValue(package, out var result))
return FluentResults.Result.Ok<IStylesResourcesInfo>(_stylesInfoConverter(onlySupportedResources?
result.Styles.Where(r =>
(r.SupportedPlatforms & ModUtils.Environment.CurrentPlatform) > 0
&& (r.SupportedTargets & ModUtils.Environment.CurrentTarget) > 0).ToImmutableArray()
: result.Styles
));
return FluentResults.Result.Fail(
$"{nameof(GetStylesInfos)}: ContentPackage {package.Name} is not registered.");
}
public Result<IStylesResourcesInfo> GetStylesInfos(IReadOnlyList<ContentPackage> packages, bool onlySupportedResources = true)
{
((IService)this).CheckDisposed();
if (packages is null || packages.Count == 0)
return FluentResults.Result.Fail($"{nameof(GetStylesInfos)}: ContentPackage list is null or empty.");
var builder = ImmutableArray.CreateBuilder<IStylesResourceInfo>();
foreach (var package in packages)
{
if (_modInfos.TryGetValue(package, out var result) && result.Styles is { IsEmpty: false })
{
builder.AddRange(onlySupportedResources?
result.Styles.Where(r =>
(r.SupportedPlatforms & ModUtils.Environment.CurrentPlatform) > 0
&& (r.SupportedTargets & ModUtils.Environment.CurrentTarget) > 0).ToImmutableArray()
: result.Styles);
}
}
return FluentResults.Result.Ok(_stylesInfoConverter(builder.MoveToImmutable()));
}
public async Task<Result<IStylesResourcesInfo>> GetStylesInfosAsync(IReadOnlyList<ContentPackage> packages, bool onlySupportedResources = true)
{
return await Task.Run(() => GetStylesInfos(packages, onlySupportedResources));
}
}
@@ -14,26 +14,17 @@ public partial class ModConfigService
private partial async Task<Result<IModConfigInfo>> GetModConfigInfoAsync(ContentPackage package, XElement root)
{
var asm = root.GetChildElements("Assembly").ToImmutableArray();
var loc = root.GetChildElements("Localization").ToImmutableArray();
var cfg = root.GetChildElements("Config").ToImmutableArray();
var lua = root.GetChildElements("Lua").ToImmutableArray();
var stl = root.GetChildElements("Style").ToImmutableArray();
return FluentResults.Result.Ok<IModConfigInfo>(new ModConfigInfo()
{
Package = package,
PackageName = package.Name,
Assemblies = asm.Any() ? GetAssemblies(package, asm) : ImmutableArray<IAssemblyResourceInfo>.Empty,
Localizations = loc.Any() ? GetLocalizations(package, loc) : ImmutableArray<ILocalizationResourceInfo>.Empty,
Configs = cfg.Any() ? GetConfigs(package, cfg) : ImmutableArray<IConfigResourceInfo>.Empty,
ConfigProfiles = cfg.Any() ? GetConfigProfiles(package, cfg) : ImmutableArray<IConfigProfileResourceInfo>.Empty,
LuaScripts = lua.Any() ? GetLuaScripts(package, lua) : ImmutableArray<ILuaScriptResourceInfo>.Empty,
Styles = stl.Any() ? GetStyles(package, stl) : ImmutableArray<IStylesResourceInfo>.Empty
LuaScripts = lua.Any() ? GetLuaScripts(package, lua) : ImmutableArray<ILuaScriptResourceInfo>.Empty
});
}
private ImmutableArray<IStylesResourceInfo> GetStyles(ContentPackage src, IEnumerable<XElement> elements)
{
throw new NotImplementedException();
}
}
@@ -1,6 +0,0 @@
namespace Barotrauma.LuaCs.Services.Processing;
public class StylesManagementService : IStylesManagementService
{
}
@@ -1,120 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using FluentResults;
using FluentResults.LuaCs;
namespace Barotrauma.LuaCs.Services;
// TODO: Complete rewrite
public class StylesService : IStylesService
{
private readonly ConcurrentDictionary<string, UIStyleProcessor> _loadedProcessors = new();
private readonly IStorageService _storageService;
private readonly ILoggerService _loggerService;
public StylesService(IStorageService storageService, ILoggerService loggerService)
{
_storageService = storageService;
_loggerService = loggerService;
}
public async Task<FluentResults.Result> LoadStylesFileAsync(ContentPackage package, ContentPath path)
{
throw new NotImplementedException();
}
public FluentResults.Result UnloadAllStyles()
{
if (NoProcessorsLoaded)
return FluentResults.Result.Fail(new Error($"{nameof(StylesService)}.{nameof(UnloadAllStyles)}: No processors have been loaded.")
.WithMetadata(MetadataType.ExceptionObject, this));
foreach (var processor in _loadedProcessors)
{
processor.Value.UnloadFile();
}
_loadedProcessors.Clear();
return FluentResults.Result.Ok();
}
public GUIFont GetFont(string fontName)
{
if (NoProcessorsLoaded)
return null;
foreach (var processor in _loadedProcessors.Values)
{
if (processor.Fonts.TryGetValue(fontName, out var asset))
return asset;
}
return null;
}
public GUISprite GetSprite(string spriteName)
{
if (NoProcessorsLoaded)
return null;
foreach (var processor in _loadedProcessors.Values)
{
if (processor.Sprites.TryGetValue(spriteName, out var asset))
return asset;
}
return null;
}
public GUISpriteSheet GetSpriteSheet(string spriteSheetName)
{
if (NoProcessorsLoaded)
return null;
foreach (var processor in _loadedProcessors.Values)
{
if (processor.SpriteSheets.TryGetValue(spriteSheetName, out var asset))
return asset;
}
return null;
}
public GUICursor GetCursor(string cursorName)
{
if (NoProcessorsLoaded)
return null;
foreach (var processor in _loadedProcessors.Values)
{
if (processor.Cursors.TryGetValue(cursorName, out var asset))
return asset;
}
return null;
}
public GUIColor GetColor(string colorName)
{
if (NoProcessorsLoaded)
return null;
foreach (var processor in _loadedProcessors.Values)
{
if (processor.Colors.TryGetValue(colorName, out var asset))
return asset;
}
return null;
}
private bool NoProcessorsLoaded => _loadedProcessors.IsEmpty;
public void Dispose()
{
UnloadAllStyles();
GC.SuppressFinalize(this);
}
public bool IsDisposed { get; private set; }
}
@@ -1,93 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using Barotrauma.Extensions;
namespace Barotrauma.LuaCs.Services;
public class UIStyleProcessor : HashlessFile
{
private readonly UIStyleFile _fake;
public readonly Dictionary<string, GUIFont> Fonts = new();
public readonly Dictionary<string, GUISprite> Sprites = new();
public readonly Dictionary<string, GUISpriteSheet> SpriteSheets = new();
public readonly Dictionary<string, GUICursor> Cursors = new();
public readonly Dictionary<string, GUIColor> Colors = new();
public UIStyleProcessor(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path)
{
_fake = new UIStyleFile(contentPackage, path);
}
public override void LoadFile()
{
var element = XMLExtensions.TryLoadXml(path: Path)?.Root?.FromPackage(ContentPackage);
if (element is null)
throw new InvalidDataException($"UIStyleProcessor: Failed to load UI style file: {Path}");
var styleElement = element.Name.LocalName.ToLowerInvariant() == "style" ? element : element.GetChildElement("style");
if (styleElement is null)
throw new InvalidDataException($"UIStyleProcessor: no 'style' XmlElement found in file: {Path}");
var childElements = styleElement.GetChildElements("Font");
if (childElements is not null)
AddToList<GUIFont, GUIFontPrefab>(Fonts, childElements, _fake);
childElements = styleElement.GetChildElements("Sprite");
if (childElements is not null)
AddToList<GUISprite, GUISpritePrefab>(Sprites, childElements, _fake);
childElements = styleElement.GetChildElements("Spritesheet");
if (childElements is not null)
AddToList<GUISpriteSheet, GUISpriteSheetPrefab>(SpriteSheets, childElements, _fake);
childElements = styleElement.GetChildElements("Cursor");
if (childElements is not null)
AddToList<GUICursor, GUICursorPrefab>(Cursors, childElements, _fake);
childElements = styleElement.GetChildElements("Color");
if (childElements is not null)
AddToList<GUIColor, GUIColorPrefab>(Colors, childElements, _fake);
void AddToList<T1, T2>(Dictionary<string, T1> dict, IEnumerable<ContentXElement> ele, UIStyleFile file) where T1 : GUISelector<T2> where T2 : GUIPrefab
{
foreach (ContentXElement prefabElement in ele)
{
string name = prefabElement.GetAttributeString("name", string.Empty);
if (name != string.Empty)
{
var prefab = (T2)Activator.CreateInstance(typeof(T2), new object[]{ prefabElement, file })!;
if (!dict.ContainsKey(name))
dict[name] = (T1)Activator.CreateInstance(typeof(T1), new object[] { name })!;
dict[name].Prefabs.Add(prefab, false);
}
}
}
}
public override void UnloadFile()
{
Fonts.Values.ForEach(p => p.Prefabs.RemoveByFile(_fake));
Sprites.Values.ForEach(p => p.Prefabs.RemoveByFile(_fake));
SpriteSheets.Values.ForEach(p => p.Prefabs.RemoveByFile(_fake));
Cursors.Values.ForEach(p => p.Prefabs.RemoveByFile(_fake));
Colors.Values.ForEach(p => p.Prefabs.RemoveByFile(_fake));
Fonts.Clear();
Sprites.Clear();
SpriteSheets.Clear();
Cursors.Clear();
Colors.Clear();
}
public override void Sort()
{
Fonts.Values.ForEach(p => p.Prefabs.Sort());
Sprites.Values.ForEach(p => p.Prefabs.Sort());
SpriteSheets.Values.ForEach(p => p.Prefabs.Sort());
Cursors.Values.ForEach(p => p.Prefabs.Sort());
Colors.Values.ForEach(p => p.Prefabs.Sort());
}
}