[Milestone] PackageManagementService completed.

- ContentPackageInfoLookup Service completed.
- Implemented ModConfigService.cs
- Implemented some of the resource processors.
This commit is contained in:
MapleWheels
2025-02-26 12:48:34 -05:00
committed by Maplewheels
parent cb88d215fa
commit 52d920d969
78 changed files with 2331 additions and 422 deletions
@@ -5,6 +5,7 @@ using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Barotrauma.Steam;
namespace Barotrauma.LuaCs.Data;
@@ -14,9 +15,6 @@ public partial record ModConfigInfo : IModConfigInfo
{
public ContentPackage Package { get; init; }
public string PackageName { get; init; }
public TargetRunMode RunModes { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IAssemblyResourceInfo> Assemblies { get; init; }
public ImmutableArray<ILocalizationResourceInfo> Localizations { get; init; }
public ImmutableArray<ILuaScriptResourceInfo> LuaScripts { get; init; }
@@ -28,10 +26,15 @@ public partial record ModConfigInfo : IModConfigInfo
#region DataContracts
public record AssemblyResourceInfo : IAssemblyResourceInfo
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;
public record AssemblyResourceInfo : IAssemblyResourceInfo
{
public ContentPackage OwnerPackage { get; init; }
public string FallbackPackageName { get; init; }
public string FriendlyName { get; init; }
public bool IsScript { get; init; }
public string InternalName { get; init; }
@@ -41,138 +44,148 @@ public record AssemblyResourceInfo : IAssemblyResourceInfo
public int LoadPriority { get; init; }
public ImmutableArray<string> FilePaths { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IPackageDependencyInfo> Dependencies { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public bool Optional { get; init; }
}
public record DependencyInfo : IPackageDependencyInfo
public record PackageDependency : IPackageDependency
{
public PackageDependency(ContentPackage package, IPackageInfo dependencyInfo, string internalName)
{
Dependency = dependencyInfo ?? throw new ArgumentNullException(nameof(dependencyInfo));
OwnerPackage = package ?? throw new ArgumentNullException(nameof(package));
InternalName = internalName ?? throw new ArgumentNullException(nameof(internalName));
}
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public string FolderPath { get; init; }
public string FallbackPackageName { get; init; }
public ulong SteamWorkshopId { get; init; }
public ContentPackage DependencyPackage { get; init; }
public bool IsMissing { get; init; }
public bool IsWorkshopInstallation { get; init; }
public virtual bool Equals(DependencyInfo other) => Equals(this, other);
public override int GetHashCode()
{
if (DependencyPackage is not null)
return DependencyPackage.GetHashCode();
if (SteamWorkshopId != 0)
return SteamWorkshopId.GetHashCode();
if (!FallbackPackageName.IsNullOrWhiteSpace() && !FolderPath.IsNullOrWhiteSpace())
return string.Concat(FallbackPackageName, FolderPath).GetHashCode();
if (!InternalName.IsNullOrWhiteSpace() && !FolderPath.IsNullOrWhiteSpace())
return string.Concat(InternalName, FolderPath).GetHashCode();
return base.GetHashCode();
}
bool IEqualityComparer<IPackageDependencyInfo>.Equals(IPackageDependencyInfo x, IPackageDependencyInfo y) => DependencyInfo.Equals(x, y);
public IPackageInfo Dependency { get; init; }
public override int GetHashCode() => Dependency.GetHashCode();
public static bool operator ==(IPackageDependencyInfo x, DependencyInfo y) => y?.Equals(x) ?? false;
public static bool operator !=(IPackageDependencyInfo x, DependencyInfo y) => y?.Equals(x) ?? false;
public static bool Equals(IPackageDependencyInfo x, IPackageDependencyInfo y)
{
if (x is null)
return false;
if (y is null)
return false;
if (x == y)
return true;
if (x.DependencyPackage is not null && y.DependencyPackage is not null)
return y.DependencyPackage == x.DependencyPackage;
if (!x.FolderPath.IsNullOrWhiteSpace()
&& !y.FolderPath.IsNullOrWhiteSpace()
&& y.FolderPath == x.FolderPath)
return true;
if (!x.FolderPath.IsNullOrWhiteSpace() != !y.FolderPath.IsNullOrWhiteSpace())
return false;
if (!x.FallbackPackageName.IsNullOrWhiteSpace()
&& !y.FallbackPackageName.IsNullOrWhiteSpace()
&& y.FallbackPackageName == x.FallbackPackageName)
return true;
if (x.SteamWorkshopId != 0 && y.SteamWorkshopId == x.SteamWorkshopId)
return true;
}
return false;
}
public record PackageInfo : IPackageInfo
{
public string Name { get; private set; }
public ulong SteamWorkshopId { get; private set; }
public uint Id { get; private set; }
/// <summary>
/// Returns the hash code unique for the package reference.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
/// <remarks>The hash should only be collision-free when referring to different packages.</remarks>
public int GetHashCode(IPackageDependencyInfo obj)
{
int hashCode = Seed;
hashCode = ApplyHashString(hashCode, obj.FallbackPackageName);
hashCode = ApplyHashString(hashCode, obj.InternalName);
if (obj.SteamWorkshopId > 0)
hashCode ^= (int)obj.SteamWorkshopId;
private readonly Func<IPackageInfo, ContentPackage> _getPackage;
int ApplyHashString(int currentValue, string str)
public ContentPackage GetPackage() => _getPackage?.Invoke(this) ?? null;
public void UpdateInfo(string name, ulong steamId, uint packageId)
{
if (name.IsNullOrWhiteSpace() || steamId == 0 || packageId == 0)
{
try
{
if (str is null || str.Length < 1)
return currentValue;
byte[] b = Encoding.UTF8.GetBytes(str);
for (int i = 0; i < Math.Min(24, b.Length-1); i++)
currentValue ^= b[i];
return currentValue;
}
catch
{
return currentValue;
}
throw new ArgumentException(
$"{nameof(PackageInfo)}: You cannot update a package with an invalid name or steam id with a valid id, or vice-versa.");
}
return hashCode;
Name = name;
SteamWorkshopId = steamId;
Id = packageId;
}
public PackageInfo(ContentPackage package, uint id, Func<IPackageInfo, ContentPackage> getPackage)
{
if (package is null)
throw new ArgumentNullException($"{nameof(PackageInfo)}: package is null");
if (id == 0)
throw new ArgumentNullException($"{nameof(PackageInfo)}: id is zero.");
this.Name = package.Name;
this.SteamWorkshopId = package.TryExtractSteamWorkshopId(out var sId) ? sId.Value : 0;
this.Id = id;
this._getPackage = getPackage;
}
private static readonly int Seed = new Random().Next(436457, int.MaxValue-900);
public PackageInfo(string name, ulong steamWorkshopId, uint id, Func<IPackageInfo, ContentPackage> getPackage)
{
Name = !name.IsNullOrWhiteSpace() ? name : throw new ArgumentNullException($"{nameof(PackageInfo)}: name cannot be null or empty.");
SteamWorkshopId = steamWorkshopId != 0 ? steamWorkshopId : throw new ArgumentNullException($"{nameof(PackageInfo)}: steam id cannot be 0.");
this.Id = id;
this._getPackage = getPackage;
}
public PackageInfo(string name, uint id, Func<IPackageInfo, ContentPackage> getPackage)
{
Name = name ?? throw new ArgumentNullException($"{nameof(PackageInfo)}: name cannot be null or empty.");
this.SteamWorkshopId = 0;
this.Id = id;
this._getPackage = getPackage;
}
public PackageInfo(ulong steamWorkshopId, uint id, Func<IPackageInfo, ContentPackage> getPackage)
{
SteamWorkshopId = steamWorkshopId != 0 ? steamWorkshopId : throw new ArgumentNullException($"{nameof(PackageInfo)}: steamid cannot be 0.");
this.Id = id;
this._getPackage = getPackage;
}
public override int GetHashCode()
{
return (int)Id;
}
public virtual bool Equals(PackageInfo other)
{
return ((IEquatable<IPackageInfo>)this).Equals(other);
}
}
public record ConfigResourceInfo : IConfigResourceInfo
{
public Platform SupportedPlatforms { get; init; }
public Target SupportedTargets { get; init; }
public int LoadPriority { get; init; }
public ImmutableArray<string> FilePaths { get; init; }
public bool Optional { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
}
public record ConfigProfileResourceInfo : IConfigProfileResourceInfo
{
public Platform SupportedPlatforms { get; init; }
public Target SupportedTargets { get; init; }
public int LoadPriority { get; init; }
public ImmutableArray<string> FilePaths { get; init; }
public bool Optional { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
}
public record LocalizationResourceInfo : ILocalizationResourceInfo
{
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public string FallbackPackageName { get; init; }
public CultureInfo TargetCulture { 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<IPackageDependencyInfo> Dependencies { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public bool Optional { get; init; }
}
public readonly struct LuaScriptScriptResourceInfo : ILuaScriptResourceInfo
{
public ContentPackage OwnerPackage { get; init; }
public string FallbackPackageName { 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<IPackageDependencyInfo> Dependencies { get; init; }
public ImmutableArray<IPackageDependency> Dependencies { get; init; }
public bool Optional { get; init; }
public string InternalName { get; init; }
public bool LazyLoad { get; init; }
public bool IsAutorun { get; init; }
}
#endregion
@@ -7,7 +7,7 @@ namespace Barotrauma.LuaCs.Data;
public enum Platform
{
Linux=0x1,
MacOS=0x2,
OSX=0x2,
Windows=0x4
}
@@ -17,12 +17,3 @@ public enum Target
Client=0x1,
Server=0x2
}
[Flags]
public enum TargetRunMode
{
ClientEnabled = 0x1,
ClientAlways = 0x2,
ServerEnabled = 0x4,
ServerAlways = 0x8
}
@@ -21,11 +21,6 @@ public interface IPlatformInfo
Target SupportedTargets { get; }
}
/// <summary>
/// All info we should have on a package for a given resource.
/// </summary>
public interface IPackageInfo : IDataInfo { }
/// <summary>
/// ResourceInfos contain metadata about a resource.
@@ -1,5 +1,5 @@
using System;
using Barotrauma.LuaCs.Networking;
using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs.Data;
@@ -11,13 +11,41 @@ public partial interface IConfigInfo : IDataInfo
/// Specifies the data type this should be initialized to (ie. string, int, vector, etc.)
/// Custom types can be registered by mods.
/// </summary>
string DataType { get; }
Type DataType { get; }
/// <summary>
/// String version of the default value.
/// </summary>
string DefaultValue { get; }
/// <summary>
/// The value the last time this config was saved, if found in /data/.
/// </summary>
string StoredValue { 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>.
/// </summary>
string CustomParameters { get; }
/// <summary>
/// <b>[Multiplayer]</b><br/>
/// What permissions do clients require to change this setting.
/// </summary>
ClientPermissions RequiredPermissions { get; }
/// <summary>
/// Whether a value can be changed at runtime.
/// 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; }
/// <summary>
/// Network synchronization rules for this config.
/// </summary>
bool IsReadOnly { get; }
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; }
}
@@ -16,10 +16,6 @@ public interface IDataInfo : IEqualityComparer<IDataInfo>, IEquatable<IDataInfo>
/// The package this information belongs to.
/// </summary>
ContentPackage OwnerPackage { get; }
/// <summary>
/// Used in place of the package data when the OwnerPackage is missing.
/// </summary>
string FallbackPackageName { get; }
bool IEqualityComparer<IDataInfo>.Equals(IDataInfo x, IDataInfo y)
{
@@ -2,13 +2,11 @@
namespace Barotrauma.LuaCs.Data;
public partial interface IModConfigInfo : IResourceCultureInfo, IAssembliesResourcesInfo,
public partial interface IModConfigInfo : IAssembliesResourcesInfo,
ILocalizationsResourcesInfo, ILuaScriptsResourcesInfo, IConfigsResourcesInfo,
IConfigProfilesResourcesInfo
{
// package info
ContentPackage Package { get; }
string PackageName { get; }
// configuration
TargetRunMode RunModes { get; }
}
@@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using Barotrauma;
using Barotrauma.LuaCs.Data;
namespace Barotrauma.LuaCs.Data;
public interface IPackageDependency : IDataInfo, IEquatable<IPackageDependency>
{
public IPackageInfo Dependency { get; }
bool IEquatable<IPackageDependency>.Equals(IPackageDependency other)
{
return other is not null && Dependency.Equals(other.Dependency);
}
}
public interface IPackageInfo : IEquatable<IPackageInfo>
{
/// <summary>
/// Name of the content package.
/// </summary>
public string Name { get; }
/// <summary>
/// Steam ID of the package.
/// </summary>
public ulong SteamWorkshopId { get; }
/// <summary>
/// The Guid for the runtime instance of the package.
/// </summary>
public uint Id { get; }
/// <summary>
/// Gets the reference to the best-match target ContentPackage that meets the requirement.
/// </summary>
/// <returns>The <see cref="ContentPackage"/> reference, or null if none was found.</returns>
public ContentPackage GetPackage();
/// <summary>
/// Tries to retrieve the current best <see cref="ContentPackage"/> and returns true if none was found.
/// </summary>
public bool IsMissing => GetPackage() is null;
bool IEquatable<IPackageInfo>.Equals(IPackageInfo other)
{
if (other is null)
return false;
if (ReferenceEquals(other, this))
return true;
if (!this.IsMissing && !other.IsMissing && ReferenceEquals(other.GetPackage, this.GetPackage))
return true;
if (this.SteamWorkshopId != 0 && other.SteamWorkshopId == this.SteamWorkshopId)
return true;
return this.Name == other.Name;
}
}
public interface IPackageDependenciesInfo
{
/// <summary>
/// List of required packages.
/// </summary>
ImmutableArray<IPackageDependency> Dependencies { get; }
}
@@ -1,40 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Barotrauma.LuaCs.Data;
public interface IPackageDependencyInfo : IPackageInfo,
IEqualityComparer<IPackageDependencyInfo>
{
/// <summary>
/// Root folder of the content package.
/// </summary>
public string FolderPath { get; }
/// <summary>
/// Steam ID of the package.
/// </summary>
public ulong SteamWorkshopId { get; }
/// <summary>
/// The dependency package, if found in the ALL Packages List.
/// </summary>
public ContentPackage DependencyPackage { get; }
/// <summary>
/// This dependency was not found.
/// </summary>
public bool IsMissing { get; }
/// <summary>
/// Whether the package is installed from the workshop. False means installation is from local mods.
/// </summary>
public bool IsWorkshopInstallation { get; }
}
public interface IPackageDependenciesInfo
{
/// <summary>
/// List of required packages.
/// </summary>
ImmutableArray<IPackageDependencyInfo> Dependencies { get; }
}
@@ -4,14 +4,22 @@ using System.Globalization;
namespace Barotrauma.LuaCs.Data;
public interface IConfigResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo { }
public interface IConfigProfileResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo { }
public interface ILocalizationResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo { }
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.
/// </summary>
public interface ILuaScriptResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo { }
public interface IAssemblyResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo
public interface ILuaScriptResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IDataInfo
{
/// <summary>
/// Should this script be run automatically.
/// </summary>
public bool IsAutorun { get; }
}
public interface IAssemblyResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IDataInfo
{
/// <summary>
/// The friendly name of the assembly. Script files belonging to the same assembly should all have the same name.