Skip to content

Commit

Permalink
- Added some of the service interfaces.
Browse files Browse the repository at this point in the history
- Added LightInject dependency
- Added refactor notes to existing code.
  • Loading branch information
TBN-MapleWheels committed Sep 19, 2024
1 parent 01207c8 commit e3bb44f
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 1 deletion.
1 change: 1 addition & 0 deletions Barotrauma/BarotraumaShared/Luatrauma.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.1.0" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="Sigil" Version="5.0.0" />
<PackageReference Include="LightInject" Version="6.6.4" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Libraries\moonsharp\MoonSharp.Interpreter\MoonSharp.Interpreter.csproj" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Libraries\moonsharp\MoonSharp.VsCodeDebugger\MoonSharp.VsCodeDebugger.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ partial class LuaCsLogger

#if SERVER
private const string LogPrefix = "SV";
private const int NetMaxLength = 1024;
private const int NetMaxLength = 1024; // character limit of vanilla Barotrauma's chat system.
private const int NetMaxMessages = 60;

// This is used so its possible to call logging functions inside the serverLog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public void Update()
#endif
}


public void Stop()
{
PluginPackageManager.UnloadPlugins();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IAssemblyMgmtService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IConfigService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IHookMgmtService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IHookService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Barotrauma.Networking;
using Microsoft.Xna.Framework;

namespace Barotrauma.LuaCs.Services;

/// <summary>
/// Provides console and debug logging services
/// </summary>
public interface ILoggerService : IService
{
void HandleException(Exception exception, LuaCsMessageOrigin origin);
void LogError(string message, LuaCsMessageOrigin origin);
void LogError(string message);
void LogMessage(string message, Color? serverColor = null, Color? clientColor = null);
void Log(string message, Color? serverColor = null, ServerLog.MessageType messageType = ServerLog.MessageType.ServerMessage);
}

public enum LuaCsMessageOrigin
{
LuaCs,
Unknown,
LuaMod,
CSharpMod,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Barotrauma.LuaCs.Services;

public interface ILuaScriptService
{
#region Type_Registration

void RegisterSafeType(Type type);
void UnregisterSafeType(Type type);
void UnregisterAllTypes();

#endregion

#region Script_File_Runner

void AddScriptFiles(string[] filePaths);
void RemoveScriptFiles(string[] filePaths);
void RunLoadedScripts();

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface INetworkingService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IPluginMgmtService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Barotrauma.LuaCs.Services;

public interface IPluginService : IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Barotrauma.LuaCs.Services;

/// <summary>
/// Base interface inherited by all services
/// </summary>
public interface IService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;

namespace Barotrauma.LuaCs.Services;

/// <summary>
/// Provides instancing and management of IServices.
/// </summary>
public interface IServicesProvider
{
#region Type_Registration

/// <summary>
/// Registers a type as a service for a given interface.
/// </summary>
/// <param name="lifetime"></param>
/// <typeparam name="TSvcInterface"></typeparam>
/// <typeparam name="TService"></typeparam>
void RegisterServiceType<TSvcInterface, TService>(ServiceLifetime lifetime) where TSvcInterface : class, IService where TService : class, IService;

/// <summary>
/// Removes a type's registration from being available for the given interface.
/// </summary>
/// <typeparam name="TSvcInterface"></typeparam>
/// <typeparam name="TService"></typeparam>
void UnregisterServiceType<TSvcInterface, TService>() where TSvcInterface : class, IService where TService : class, IService;

/// <summary>
/// Called whenever a new service type for a given interface is implemented.
/// Args[0]: Interface type
/// Args[1]: Implementing type
/// </summary>
event System.Action<Type, Type> OnServiceRegistered;

#endregion

#region Services_Instancing_Injection

/// <summary>
/// Injects services into the properties of already instanced objects.
/// </summary>
/// <param name="inst"></param>
/// <typeparam name="T"></typeparam>
void InjectServices<T>(T inst) where T : class;

/// <summary>
/// Tries to get a service for the given interface, returns success/failure.
/// </summary>
/// <param name="service"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TSvcInterface"></typeparam>
/// <returns></returns>
bool TryGetService<TSvcInterface>(out IService service, out ServiceLifetime lifetime) where TSvcInterface : class, IService;

/// <summary>
/// Called whenever a new service is created/instanced.
/// Args[0]: The interface type of the service.
/// Args[1]: The instance of the service.
/// </summary>
event System.Action<Type, IService> OnServiceInstanced;

#endregion

#region ActiveServices

/// <summary>
/// Returns all services for the given interface.
/// </summary>
/// <typeparam name="TSvc"></typeparam>
/// <returns></returns>
List<TSvc> GetAllServices<TSvc>() where TSvc : class, IService;

#endregion

#region Internal_Use

/// <summary>
/// Disposes of all services for a type. Warning: unable to dispose of services held by other objects.
/// </summary>
/// <typeparam name="TSvc"></typeparam>
internal void DisposeServicesOfType<TSvc>() where TSvc : class, IService;

/// <summary>
/// Disposes of all services and resets DI container. Warning: unable to dispose of services held by other objects.
/// </summary>
internal void DisposeAllServices();

#endregion
}

public enum ServiceLifetime
{
Transient, Singleton, PerInstance, PerThread, Invalid, Custom
}

0 comments on commit e3bb44f

Please sign in to comment.