Skip to content

Commit

Permalink
- Cleaned up inclusions.
Browse files Browse the repository at this point in the history
- Removed unneeded regions.
- PluginManagementService implementation work.
- Changed Reference Assemblies package to .NET 6.0 exclusive.
  • Loading branch information
TBN-MapleWheels committed Dec 20, 2024
1 parent e0e127b commit 1470793
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 215 deletions.
2 changes: 1 addition & 1 deletion Barotrauma/BarotraumaShared/Luatrauma.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="QuikGraph" Version="2.5.0" />
<PackageReference Include="OneOf" Version="3.0.271" />
<PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="Basic.Reference.Assemblies" Version="1.7.9" />
<PackageReference Include="Basic.Reference.Assemblies.Net60" Version="1.7.9" />
<PackageReference Include="ImpromptuInterface " Version="8.0.4" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Libraries\moonsharp\MoonSharp.Interpreter\MoonSharp.Interpreter.csproj" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Libraries\moonsharp\MoonSharp.VsCodeDebugger\MoonSharp.VsCodeDebugger.csproj" />
Expand Down
1 change: 1 addition & 0 deletions Barotrauma/BarotraumaShared/SharedSource/LuaCs/ModUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using OneOf;
using Platform = Barotrauma.LuaCs.Data.Platform;

// This file is cursed, we put everything in it, and I'm not sorry about it.
namespace Barotrauma.LuaCs
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using System.Collections.Immutable;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using Barotrauma.LuaCs.Data;
using FluentResults;
using Microsoft.CodeAnalysis;
Expand All @@ -7,5 +14,93 @@ namespace Barotrauma.LuaCs.Services;

public class PluginManagementService : IPluginManagementService, IAssemblyManagementService
{
public bool IsDisposed
{
get => ModUtils.Threading.GetBool(ref _isDisposed);
set => ModUtils.Threading.SetBool(ref _isDisposed, value);
}
private int _isDisposed;

private readonly ReaderWriterLockSlim _operationsLock = new(LockRecursionPolicy.SupportsRecursion);

private readonly ConcurrentDictionary<Guid, IAssemblyLoaderService> _assemblyServices = new();
private readonly ConcurrentDictionary<IAssemblyResourceInfo, Guid> _resourceData = new();
private readonly Lazy<IEventService> _eventService;
private readonly Func<IAssemblyLoaderService> _assemblyServiceFactory;


public bool IsResourceLoaded<T>(T resource) where T : IAssemblyResourceInfo
{
throw new NotImplementedException();
}

public Result<ImmutableArray<T>> GetTypes<T>(string namespacePrefix = null, bool includeInterfaces = false,
bool includeAbstractTypes = false, bool includeDefaultContext = true, bool includeExplicitAssembliesOnly = false)
{
throw new NotImplementedException();
}

public Result<ImmutableArray<IAssemblyResourceInfo>> LoadAssemblyResources(ImmutableArray<IAssemblyResourceInfo> resource)
{
throw new NotImplementedException();
}

public Result<Assembly> GetLoadedAssembly(string assemblyName, in Guid[] excludedContexts)
{
((IService)this).CheckDisposed();
_operationsLock.EnterReadLock();
try
{
foreach (var (guid, context) in _assemblyServices)
{
if (excludedContexts.Length > 0 && excludedContexts.Contains(guid))
continue;
if (context.GetAssemblyByName(assemblyName) is { IsSuccess: true, Value: not null } ret)
return ret.Value;
}
return FluentResults.Result.Fail($"Could not find assembly {assemblyName}");
}
finally
{
_operationsLock.ExitReadLock();
}
}

public Result<Assembly> GetLoadedAssembly(AssemblyName assemblyName, in Guid[] excludedContexts)
=> GetLoadedAssembly(assemblyName.FullName, excludedContexts);

public ImmutableArray<MetadataReference> GetDefaultMetadataReferences() =>
Basic.Reference.Assemblies.Net60.References.All.Select(Unsafe.As<MetadataReference>).ToImmutableArray();

public ImmutableArray<MetadataReference> GetAddInContextsMetadataReferences()
{
((IService)this).CheckDisposed();
_operationsLock.EnterReadLock();
try
{
if (_assemblyServices.IsEmpty)
return ImmutableArray<MetadataReference>.Empty;
var builder = ImmutableArray.CreateBuilder<MetadataReference>();
foreach (var context in _assemblyServices.Values)
builder.AddRange(context.AssemblyReferences);
return builder.ToImmutable();
}
finally
{
_operationsLock.ExitReadLock();
}
}

public ImmutableArray<IAssemblyLoaderService> AssemblyLoaderServices { get; }

public void Dispose()
{
// TODO release managed resources here
throw new NotImplementedException();
}

public FluentResults.Result Reset()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public interface IPluginManagementService : IReusableService
/// <summary>
///
/// </summary>
/// <param name="package"></param>
/// <param name="namespacePrefix"></param>
/// <param name="includeInterfaces"></param>
/// <param name="includeAbstractTypes"></param>
Expand All @@ -27,20 +26,12 @@ public interface IPluginManagementService : IReusableService
/// <typeparam name="T"></typeparam>
/// <returns></returns>
FluentResults.Result<ImmutableArray<T>> GetTypes<T>(
ContentPackage package = null,
string namespacePrefix = null,
bool includeInterfaces = false,
bool includeAbstractTypes = false,
bool includeDefaultContext = true,
bool includeExplicitAssembliesOnly = false);

/// <summary>
///
/// </summary>
/// <param name="package"></param>
/// <returns></returns>
FluentResults.Result<ImmutableArray<IAssemblyResourceInfo>> GetCachedAssembliesForPackage(ContentPackage package);

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace Barotrauma.LuaCs.Services;

public interface IStorageService : IService
{
#region LocalGameData

// -- local game folder storage
FluentResults.Result<XDocument> LoadLocalXml(ContentPackage package, string localFilePath);
FluentResults.Result<byte[]> LoadLocalBinary(ContentPackage package, string localFilePath);
FluentResults.Result<string> LoadLocalText(ContentPackage package, string localFilePath);
Expand All @@ -22,10 +21,8 @@ public interface IStorageService : IService
Task<FluentResults.Result> SaveLocalXmlAsync(ContentPackage package, string localFilePath, XDocument document);
Task<FluentResults.Result> SaveLocalBinaryAsync(ContentPackage package, string localFilePath, byte[] bytes);
Task<FluentResults.Result> SaveLocalTextAsync(ContentPackage package, string localFilePath, string text);

#endregion

#region ContentPackageData
// -- package directory
// singles
FluentResults.Result<XDocument> LoadPackageXml(ContentPackage package, string localFilePath);
FluentResults.Result<byte[]> LoadPackageBinary(ContentPackage package, string localFilePath);
Expand All @@ -45,9 +42,7 @@ public interface IStorageService : IService
Task<ImmutableArray<(string, FluentResults.Result<byte[]>)>> LoadPackageBinaryFilesAsync(ContentPackage package, ImmutableArray<string> localFilePaths);
Task<ImmutableArray<(string, FluentResults.Result<string>)>> LoadPackageTextFilesAsync(ContentPackage package, ImmutableArray<string> localFilePaths);

#endregion

#region AbsolutePaths
// -- absolute paths
FluentResults.Result<XDocument> TryLoadXml(string filePath, Encoding encoding = null);
FluentResults.Result<string> TryLoadText(string filePath, Encoding encoding = null);
FluentResults.Result<byte[]> TryLoadBinary(string filePath);
Expand All @@ -62,5 +57,4 @@ public interface IStorageService : IService
Task<FluentResults.Result> TrySaveXmlAsync(string filePath, XDocument document, Encoding encoding = null);
Task<FluentResults.Result> TrySaveTextAsync(string filePath, string text, Encoding encoding = null);
Task<FluentResults.Result> TrySaveBinaryAsync(string filePath, byte[] bytes);
#endregion
}
Loading

0 comments on commit 1470793

Please sign in to comment.