-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Poggu <[email protected]> Co-authored-by: KillStr3aK <[email protected]> Co-authored-by: Poggu <[email protected]>
- Loading branch information
1 parent
1d6bee0
commit 2a15a8d
Showing
24 changed files
with
630 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[!INCLUDE [WithEntityOutputHooks](../../examples/WithEntityOutputHooks/README.md)] | ||
|
||
<a href="https://github.com/roflmuffin/CounterStrikeSharp/tree/main/examples/WithEntityOutputHooks" class="btn btn-secondary">View project on Github <i class="bi bi-github"></i></a> | ||
|
||
[!code-csharp[](../../examples/WithEntityOutputHooks/WithEntityOutputHooksPlugin.cs)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# With Entity Output Hooks | ||
This example shows how to implement hooks for entity output, such as StartTouch, OnPickup etc. |
12 changes: 12 additions & 0 deletions
12
examples/WithEntityOutputHooks/WithEntityOutputHooks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\managed\CounterStrikeSharp.API\CounterStrikeSharp.API.csproj" /> | ||
</ItemGroup> | ||
</Project> |
43 changes: 43 additions & 0 deletions
43
examples/WithEntityOutputHooks/WithEntityOutputHooksPlugin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using CounterStrikeSharp.API.Core; | ||
using CounterStrikeSharp.API.Core.Attributes; | ||
using CounterStrikeSharp.API.Core.Attributes.Registration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WithEntityOutputHooks; | ||
|
||
[MinimumApiVersion(80)] | ||
public class WithEntityOutputHooksPlugin : BasePlugin | ||
{ | ||
public override string ModuleName => "Example: With Entity Output Hooks"; | ||
public override string ModuleVersion => "1.0.0"; | ||
public override string ModuleAuthor => "CounterStrikeSharp & Contributors"; | ||
public override string ModuleDescription => "A simple plugin that showcases entity output hooks"; | ||
|
||
public override void Load(bool hotReload) | ||
{ | ||
HookEntityOutput("weapon_knife", "OnPlayerPickup", (CEntityIOOutput output, string name, CEntityInstance activator, CEntityInstance caller, CVariant value, float delay) => | ||
{ | ||
Logger.LogInformation("weapon_knife called OnPlayerPickup ({name}, {activator}, {caller}, {delay})", name, activator.DesignerName, caller.DesignerName, delay); | ||
|
||
return HookResult.Continue; | ||
}); | ||
} | ||
|
||
// Output hooks can use wildcards to match multiple entities | ||
[EntityOutputHook("*", "OnPlayerPickup")] | ||
public HookResult OnPickup(CEntityIOOutput output, string name, CEntityInstance activator, CEntityInstance caller, CVariant value, float delay) | ||
{ | ||
Logger.LogInformation("[EntityOutputHook Attribute] Called OnPlayerPickup ({name}, {activator}, {caller}, {delay})", name, activator.DesignerName, caller.DesignerName, delay); | ||
|
||
return HookResult.Continue; | ||
} | ||
|
||
// Output hooks can use wildcards to match multiple output names | ||
[EntityOutputHook("func_buyzone", "*")] | ||
public HookResult OnTouchStart(CEntityIOOutput output, string name, CEntityInstance activator, CEntityInstance caller, CVariant value, float delay) | ||
{ | ||
Logger.LogInformation("[EntityOutputHook Attribute] Buyzone called output ({name}, {activator}, {caller}, {delay})", name, activator.DesignerName, caller.DesignerName, delay); | ||
|
||
return HookResult.Continue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
managed/CounterStrikeSharp.API/Core/Attributes/Registration/EntityOutputHookAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace CounterStrikeSharp.API.Core.Attributes.Registration; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public class EntityOutputHookAttribute : Attribute | ||
{ | ||
public string Classname { get; } | ||
public string OutputName { get; } | ||
|
||
public EntityOutputHookAttribute(string classname, string outputName) | ||
{ | ||
Classname = classname; | ||
OutputName = outputName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
managed/CounterStrikeSharp.API/Core/Model/CEntityIOOutput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using CounterStrikeSharp.API.Modules.Utils; | ||
|
||
namespace CounterStrikeSharp.API.Core; | ||
|
||
public partial class CEntityIOOutput | ||
{ | ||
public EntityIOConnection_t? Connections => Utilities.GetPointer<EntityIOConnection_t>(Handle + 8); | ||
|
||
public EntityIOOutputDesc_t Description => new(Marshal.ReadIntPtr(Handle + 16)); | ||
} | ||
|
||
public class EntityIOOutputDesc_t : NativeObject | ||
{ | ||
public EntityIOOutputDesc_t(IntPtr pointer) : base(pointer) | ||
{ | ||
} | ||
|
||
public string Name => Utilities.ReadStringUtf8(Handle + 0); | ||
public unsafe ref uint Flags => ref Unsafe.AsRef<uint>((void*)(Handle + 8)); | ||
public unsafe ref uint OutputOffset => ref Unsafe.AsRef<uint>((void*)(Handle + 16)); | ||
} | ||
|
||
public class EntityIOConnection_t : EntityIOConnectionDesc_t | ||
{ | ||
public EntityIOConnection_t(IntPtr pointer) : base(pointer) | ||
{ | ||
} | ||
|
||
public unsafe ref bool MarkedForRemoval => ref Unsafe.AsRef<bool>((void*)(Handle + 40)); | ||
|
||
public EntityIOConnection_t? Next => Utilities.GetPointer<EntityIOConnection_t>(Handle + 48); | ||
} | ||
|
||
public class EntityIOConnectionDesc_t : NativeObject | ||
{ | ||
public EntityIOConnectionDesc_t(IntPtr pointer) : base(pointer) | ||
{ | ||
} | ||
|
||
public string TargetDesc => Utilities.ReadStringUtf8(Handle + 0); | ||
public string TargetInput => Utilities.ReadStringUtf8(Handle + 8); | ||
public string ValueOverride => Utilities.ReadStringUtf8(Handle + 16); | ||
public CEntityHandle Target => new(Handle + 24); | ||
public unsafe ref EntityIOTargetType_t TargetType => ref Unsafe.AsRef<EntityIOTargetType_t>((void*)(Handle + 28)); | ||
public unsafe ref int TimesToFire => ref Unsafe.AsRef<int>((void*)(Handle + 32)); | ||
public unsafe ref float Delay => ref Unsafe.AsRef<float>((void*)(Handle + 36)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace CounterStrikeSharp.API.Core; | ||
|
||
/// <summary> | ||
/// Placeholder for CVariant | ||
/// <see href="https://github.com/alliedmodders/hl2sdk/blob/cs2/public/variant.h"/> | ||
/// <remarks>A lot of entity outputs do not use this value</remarks> | ||
/// </summary> | ||
public class CVariant : NativeObject | ||
{ | ||
public CVariant(IntPtr pointer) : base(pointer) | ||
{ | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
managed/CounterStrikeSharp.API/Modules/Entities/EntityIO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This file is part of CounterStrikeSharp. | ||
* CounterStrikeSharp is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* CounterStrikeSharp is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. * | ||
*/ | ||
|
||
namespace CounterStrikeSharp.API.Modules.Entities | ||
{ | ||
public class EntityIO | ||
{ | ||
public delegate HookResult EntityOutputHandler(CEntityIOOutput output, string name, CEntityInstance activator, CEntityInstance caller, CVariant value, float delay); | ||
|
||
internal class EntityOutputCallback | ||
{ | ||
public string Classname; | ||
|
||
public string Output; | ||
|
||
public EntityOutputHandler Handler; | ||
|
||
public EntityOutputCallback(string classname, string output, EntityOutputHandler handler) | ||
{ | ||
Classname = classname; | ||
Output = output; | ||
Handler = handler; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.