[Milestone] AssemblyLoader completed.

Details:
- Assembly Mgmt Service for loading now a separate interface, not intended for normal use.
- Assembly Loader work; implemented custom dictionary key and table.
- Assembly loading work.
- EventService completed.
- Moved assembly extensions to ModUtils.cs
- Work to event service.
NetworkService work
- Added ImpromptuInterfaces package.
- Networking Service work to support NetVars
- Event Service
- Added assemblies references package for script compilation. Updated Roslyn version for compatibility.
- Package Loading work.
Swap Harmony to HarmonyX
- More refactor conversion to FluentResults.
- Updated StylesService to return Results.
- Refactor of PackageService partially complete.
- Made IService.Reset() required to return a Result.
- Moved plugin/assembly related code to their own folder (same namespace).
- Updated interfaces to reflect the use of Result<T>.
- Partial refactor, incomplete.
- Added 'FluentResults' so we can stop using cursed Exception-based flow control in loading code.
- Added 'OneOf' nuget package: https://github.com/mcintyre321/OneOf
for the implementation of the Optional<T> pattern and complex discrete return types instead of cursed enums (see current AssemblyManager.cs).
- Reapplied old branch changes.
This commit is contained in:
MapleWheels
2024-11-04 02:33:31 -05:00
committed by Maplewheels
parent 01cc1d331b
commit 6880e5e9ee
97 changed files with 4100 additions and 1512 deletions
@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Barotrauma.LuaCs.Data;
@@ -29,6 +31,7 @@ public partial record ModConfigInfo : IModConfigInfo
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; }
@@ -44,16 +47,109 @@ public record AssemblyResourceInfo : IAssemblyResourceInfo
public record DependencyInfo : IPackageDependencyInfo
{
public string InternalName { get; init; }
public ContentPackage OwnerPackage { get; init; }
public string FolderPath { get; init; }
public string PackageName { 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 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;
}
/// <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;
int ApplyHashString(int currentValue, string str)
{
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;
}
}
return hashCode;
}
private static readonly int Seed = new Random().Next(436457, int.MaxValue-900);
}
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; }
@@ -67,6 +163,7 @@ public record LocalizationResourceInfo : ILocalizationResourceInfo
public readonly struct LuaScriptResourceInfo : ILuaResourceInfo
{
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; }
@@ -1,4 +1,5 @@
using System;
// ReSharper disable InconsistentNaming
namespace Barotrauma.LuaCs.Data;
@@ -6,7 +7,7 @@ namespace Barotrauma.LuaCs.Data;
public enum Platform
{
Linux=0x1,
OSX=0x2,
MacOS=0x2,
Windows=0x4
}
@@ -21,14 +21,10 @@ public interface IPlatformInfo
Target SupportedTargets { get; }
}
/// <summary>
/// Which package does the following data belong to?
/// All info we should have on a package for a given resource.
/// </summary>
public interface IPackageInfo
{
ContentPackage OwnerPackage { get; }
}
public interface IPackageInfo : IDataInfo { }
/// <summary>
@@ -65,13 +61,3 @@ public interface IResourceCultureInfo
/// </summary>
ImmutableArray<CultureInfo> SupportedCultures { get; }
}
public interface ILoadableResourceInfo
{
/// <summary>
/// [UNIQUE] The name that will be used when trying to reference this resource for execution or loading.
/// </summary>
[Required]
public string InternalName { get; }
}
@@ -1,26 +1,23 @@
using System;
using Barotrauma.LuaCs.Networking;
using Barotrauma.Networking;
namespace Barotrauma.LuaCs.Data;
// TODO: Finish
public partial interface IConfigInfo
public partial interface IConfigInfo : IDataInfo
{
string Name { get; }
string PackageName { get; }
ConfigDataType Type { get; }
/// <summary>
/// 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; }
string DefaultValue { get; }
string StoredValue { get; }
ClientPermissions RequiredPermissions { get; }
}
public enum ConfigDataType
{
Boolean, Int32, Int64, Single, Double, String,
Color, Vector2, Vector3, List,
RangeInt32, RangeSingle, ControlInput
}
public enum NetSync
{
None, TwoWay, ServerAuthority, ClientOneWay
/// <summary>
/// Whether a value can be changed at runtime.
/// </summary>
bool IsReadOnly { get; }
NetSync NetSync { get; }
}
@@ -0,0 +1,20 @@
namespace Barotrauma.LuaCs.Data;
/// <summary>
/// Serves as a compound-key to refer to all resources and information that comes from a specific source.
/// </summary>
public interface IDataInfo
{
/// <summary>
/// Package-Unique name to be used internally for all representations of, and references to, this information.
/// </summary>
string InternalName { get; }
/// <summary>
/// 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; }
}
@@ -1,6 +1,12 @@
namespace Barotrauma.LuaCs.Data;
using System.Collections.Generic;
using System.Globalization;
public interface ILocalizationInfo
namespace Barotrauma.LuaCs.Data;
public interface ILocalizationInfo : IDataInfo
{
string Symbol { get; }
IReadOnlyDictionary<CultureInfo, RawLString> LocalizedValues { get; }
RawLString GetLocalizedString(CultureInfo locale);
RawLString GetLocalizedString(string cultureCode);
}
@@ -1,18 +1,17 @@
using System.Collections.Immutable;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Barotrauma.LuaCs.Data;
public interface IPackageDependencyInfo : IPackageInfo
public interface IPackageDependencyInfo : IPackageInfo,
IEqualityComparer<IPackageDependencyInfo>
{
/// <summary>
/// Root folder of the content package.
/// </summary>
public string FolderPath { get; }
/// <summary>
/// Name of the package.
/// </summary>
public string PackageName { get; }
/// <summary>
/// Steam ID of the package.
/// </summary>
public ulong SteamWorkshopId { get; }
@@ -20,6 +19,16 @@ public interface IPackageDependencyInfo : IPackageInfo
/// 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
@@ -10,8 +10,8 @@ public interface ILocalizationResourceInfo : IResourceInfo, IResourceCultureInfo
/// <summary>
/// Represents loadable Lua files.
/// </summary>
public interface ILuaResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, ILoadableResourceInfo, IPackageInfo { }
public interface IAssemblyResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, ILoadableResourceInfo, IPackageInfo
public interface ILuaResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo { }
public interface IAssemblyResourceInfo : IResourceInfo, IResourceCultureInfo, IPackageDependenciesInfo, IPackageInfo
{
/// <summary>
/// The friendly name of the assembly. Script files belonging to the same assembly should all have the same name.