using System; using System.Collections.Generic; using System.Reflection; using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs; using Barotrauma.Networking; using Dynamitey; using ImpromptuInterface; namespace Barotrauma.LuaCs.Events; /* * The following is a collection of interfaces that types can implement to be registered events. * Note: Internally-marked interfaces should be consumed using a publicizer. This is due to the Barotrauma source * types being internal by default. */ public interface IEvent { bool IsLuaRunner() => false; } public interface IEvent : IEvent where T : IEvent { static virtual T GetLuaRunner(IDictionary luaFunc) { throw new InvalidOperationException($"Lua runners forbidden for {typeof(T).Name}"); } } #region RuntimeServiceEvents /// /// Called when the current (game state) changes. Upstream Type 'Screen' is internal. /// internal interface IEventScreenSelected : IEvent { void OnScreenSelected(Screen screen); } /// /// Called whenever the list of all (enabled and disabled) on disk has changed. /// internal interface IEventAllPackageListChanged : IEvent { void OnAllPackageListChanged(IEnumerable corePackages, IEnumerable regularPackages); } /// /// Called whenever the list of enabled has changed. /// internal interface IEventEnabledPackageListChanged : IEvent { void OnEnabledPackageListChanged(CorePackage package, IEnumerable regularPackages); } internal interface IEventReloadAllPackages : IEvent { void OnReloadAllPackages(); } internal interface IEventSettingInstanceLifetime : IEvent { void OnSettingInstanceCreated(T configInstance) where T : ISettingBase; void OnSettingInstanceDisposed(T configInstance) where T : ISettingBase; } #endregion #region GameEvents /// /// Called as soon as round begins to load before any loading takes place. /// public interface IEventRoundStarting : IEvent { void OnRoundStarting(); static IEventRoundStarting IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnRoundStarting = ReturnVoid.Arguments(() => luaFunc[nameof(OnRoundStarting)]()) }.ActLike(); } /// /// Called when a round has started and fully loaded. /// public interface IEventRoundStarted : IEvent { void OnRoundStart(); static IEventRoundStarted IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnRoundStart = ReturnVoid.Arguments(() => luaFunc[nameof(OnRoundStart)]()) }.ActLike(); } /// /// Called on game loop normal update. /// public interface IEventUpdate : IEvent { void OnUpdate(double fixedDeltaTime); static IEventUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnUpdate = ReturnVoid.Arguments((fixedDeltaTime) => luaFunc[nameof(OnUpdate)](fixedDeltaTime)) }.ActLike(); } /// /// Called on game loop draw update. /// public interface IEventDrawUpdate : IEvent { void OnDrawUpdate(double deltaTime); static IEventDrawUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnDrawUpdate = ReturnVoid.Arguments((deltaTime) => luaFunc[nameof(OnDrawUpdate)](deltaTime)) }.ActLike(); } #if CLIENT public interface IEventServerRawNetMessageReceived : IEvent { void OnReceivedServerNetMessage(IReadMessage netMessage, ServerPacketHeader serverPacketHeader); } public interface IEventConnectedToServer : IEvent { void OnConnectedToServer(); } #elif SERVER public interface IEventClientRawNetMessageReceived : IEvent { void OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader serverPacketHeader, NetworkConnection sender); } #endif #endregion #region Networking #region Networking-Server #if SERVER /// /// Called when a client connects to the server and has loaded into the lobby. /// interface IEventClientConnected : IEvent { /// /// Called when a client connects to the server. /// /// The connecting client. void OnClientConnected(Client client); static IEventClientConnected IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnClientConnected = ReturnVoid.Arguments((client) => luaFunc[nameof(OnClientConnected)](client)) }.ActLike(); } #endif #endregion #region Networking-Client #if CLIENT /// /// Called when the client has connected to the server and loaded to the lobby. /// public interface IEventServerConnected : IEvent { void OnServerConnected(); static IEventServerConnected IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnServerConnected = ReturnVoid.Arguments(() => luaFunc[nameof(OnServerConnected)]()) }.ActLike(); } #endif #endregion #endregion #region Assembly_PluginEvents /// /// Called on plugin normal, use this for basic/core loading that does not rely on any other modded content. /// public interface IEventPluginInitialize : IEvent { void Initialize(); static IEventPluginInitialize IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), Initialize = ReturnVoid.Arguments(() => luaFunc[nameof(Initialize)]()) }.ActLike(); } /// /// Called once all plugins have been loaded. if you have integrations with any other mod, put that code here. /// public interface IEventPluginLoadCompleted : IEvent { void OnLoadCompleted(); static IEventPluginLoadCompleted IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnLoadCompleted = ReturnVoid.Arguments(() => luaFunc[nameof(OnLoadCompleted)]()) }.ActLike(); } /// /// Called before Barotrauma initializes plugins. Use if you want to patch another plugin's behaviour 'unofficially'. /// WARNING: This method is called before Initialize()! /// public interface IEventPluginPreInitialize : IEvent { void PreInitPatching(); static IEventPluginPreInitialize IEvent.GetLuaRunner(IDictionary luaFunc) => new { IsLuaRunner = Return.Arguments(() => true), OnPreInitialize = ReturnVoid.Arguments(() => luaFunc[nameof(PreInitPatching)]()) }.ActLike(); } /// /// Called whenever a new assembly is loaded. /// public interface IEventAssemblyLoaded : IEvent { void OnAssemblyLoaded(Assembly assembly); } /// /// Called whenever an is instanced. /// public interface IEventAssemblyContextCreated : IEvent { void OnAssemblyCreated(IAssemblyLoaderService loaderService); } /// /// Called whenever an begins unloading. /// public interface IEventAssemblyContextUnloading : IEvent { void OnAssemblyUnloading(WeakReference loaderService); } public interface IEventAssemblyUnloading : IEvent { void OnAssemblyUnloading(Assembly assembly); } #endregion