Skip to content

Commit

Permalink
Implement Usermessages (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin authored Aug 26, 2024
1 parent 28ce183 commit 10f472e
Show file tree
Hide file tree
Showing 62 changed files with 477,965 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
cmake-build-*/
.kdev4/
generated/
!**/protobuf/generated/

# configure_file auto generated.
configs/addons/metamod/counterstrikesharp.vdf
Expand Down
45 changes: 25 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ project(counterstrikesharp C CXX ASM)
include("makefiles/shared.cmake")

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
#set(protobuf_BUILD_TESTS OFF)
#set(protobuf_BUILD_SHARED_LIBS)

add_subdirectory(libraries/spdlog)
add_subdirectory(libraries/dyncall)
add_subdirectory(libraries/funchook)
add_subdirectory(libraries/DynoHook)
# add_subdirectory(libraries/protobuf-lib)

set_property(TARGET dynohook PROPERTY DYNO_ARCH_X86 64)
set_property(TARGET funchook-static PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down Expand Up @@ -88,42 +91,44 @@ set(SOURCE_FILES
src/core/managers/server_manager.cpp
src/core/managers/server_manager.h
src/scripting/natives/natives_server.cpp
src/scripting/natives/natives_usermessages.cpp
libraries/nlohmann/json.hpp
src/core/managers/voice_manager.cpp
src/core/managers/voice_manager.h
src/core/managers/usermessage_manager.cpp
src/core/managers/usermessage_manager.h
src/scripting/natives/natives_dynamichooks.cpp
src/core/game_system.h
src/core/game_system.cpp
src/core/UserMessage.h
src/core/UserMessage.cpp
src/core/recipientfilters.h
)

set(PROTO_DIRS -I${CMAKE_CURRENT_SOURCE_DIR}/libraries/Protobufs/csgo)
file(GLOB PROTOS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/Protobufs/csgo/*.proto")

## Generate protobuf source & headers
if(LINUX)
set(PROTOC_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/libraries/hl2sdk-cs2/devtools/bin/linux/protoc)
elseif(WIN32)
set(PROTOC_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/libraries/hl2sdk-cs2/devtools/bin/protoc.exe)
endif()

add_custom_command(
OUTPUT protobuf_output_stamp
COMMAND ${PROTOC_EXECUTABLE} --proto_path=thirdparty/protobuf-3.21.8/src --proto_path=common --cpp_out=common common/network_connection.proto
COMMENT "Generating protobuf file"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/libraries/hl2sdk-cs2
VERBATIM
set(PROTO_GEN_CPP
src/protobuf/generated/network_connection.pb.cc
src/protobuf/generated/networkbasetypes.pb.cc
src/protobuf/generated/cs_gameevents.pb.cc
src/protobuf/generated/engine_gcmessages.pb.cc
src/protobuf/generated/gcsdk_gcmessages.pb.cc
src/protobuf/generated/cstrike15_gcmessages.pb.cc
src/protobuf/generated/cstrike15_usermessages.pb.cc
src/protobuf/generated/netmessages.pb.cc
src/protobuf/generated/steammessages.pb.cc
src/protobuf/generated/usermessages.pb.cc
src/protobuf/generated/gameevents.pb.cc
src/protobuf/generated/clientmessages.pb.cc
src/protobuf/generated/te.pb.cc
)

set(SOURCE_FILES ${SOURCE_FILES} protobuf_output_stamp)

# Sources
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${NATIVES_SOURCES} ${CONVERSIONS_SOURCES} ${CONVERSIONS_HEADERS})
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${PROTO_GEN_CPP} ${NATIVES_SOURCES} ${CONVERSIONS_SOURCES} ${CONVERSIONS_HEADERS})

target_include_directories(
${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src/core/cs2_sdk
${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/generated
)

if(LINUX)
Expand Down
5 changes: 5 additions & 0 deletions docfx/examples/WithUserMessages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[!INCLUDE [WithUserMessages](../../examples/WithUserMessages/README.md)]

<a href="https://github.com/roflmuffin/CounterStrikeSharp/tree/main/examples/WithUserMessages" class="btn btn-secondary">View project on Github <i class="bi bi-github"></i></a>

[!code-csharp[](../../examples/WithUserMessages/WithUserMessagesPlugin.cs)]
2 changes: 2 additions & 0 deletions docfx/examples/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ items:
href: WithSharedTypes.md
- name: Translations
href: WithTranslations.md
- name: User Messages
href: WithUserMessages.md
- name: Voice Overrides
href: WithVoiceOverrides.md
- name: Warcraft Plugin
Expand Down
2 changes: 2 additions & 0 deletions examples/WithUserMessages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# With User Messages
This is example shows how you can hook, interrupt and send User Messages.
12 changes: 12 additions & 0 deletions examples/WithUserMessages/WithUserMessages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.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>
84 changes: 84 additions & 0 deletions examples/WithUserMessages/WithUserMessagesPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.UserMessages;
using Microsoft.Extensions.Logging;

namespace WithUserMessages;

[MinimumApiVersion(80)]
public class WithUserMessagesPlugin : BasePlugin
{
public override string ModuleName => "Example: With User Messages";
public override string ModuleVersion => "1.0.0";
public override string ModuleAuthor => "CounterStrikeSharp & Contributors";
public override string ModuleDescription => "A simple plugin that hooks and sends User Messages";

public override void Load(bool hotReload)
{
// Hooks can be added using the user message ID. In this case it's the ID for `CMsgTEFireBullets`.
HookUserMessage(452, um =>
{
// Sets all weapon sounds to the sound of a silenced usp.
um.SetUInt("weapon_id", 0);
um.SetInt("sound_type", 9);
um.SetUInt("item_def_index", 61);

return HookResult.Continue;
}, HookMode.Pre);

HookUserMessage(118, um =>
{
var author = um.ReadString("param1");
var message = um.ReadString("param2");
Logger.LogInformation("Chat message from {Author}: {Message}", author, message);

for (var i = 0; i < um.Recipients.Count; i++)
{
Logger.LogInformation("Recipient {Index}: {Name}", i, um.Recipients[i].PlayerName);
}

if (message.Contains("stop"))
{
return HookResult.Stop;
}

if (message.Contains("skip"))
{
um.Recipients.Clear();
}

return HookResult.Continue;
});
}

[ConsoleCommand("css_shake")]
public void OnCommandShake(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;

// UserMessage.FromPartialName is a helper method that creates a UserMessage object from a partial network name.
// In this case, it will resolve to `CUserMessageShake`.
var message = UserMessage.FromPartialName("Shake");

message.SetFloat("duration", 2);
message.SetFloat("amplitude", 5);
message.SetFloat("frequency", 10f);
message.SetInt("command", 0);

if (command.GetArg(1) == "all")
{
message.Recipients.AddAllPlayers();
}
else
{
message.Recipients.Add(player);
}

message.Send();

// You can also use an overload of `Send` to send the message to a specific player without manually creating a recipient filter.
// message.Send(player);
}
}
5 changes: 3 additions & 2 deletions makefiles/linux.base.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#set(_ITERATOR_DEBUG_LEVEL 2)
add_definitions(-D_LINUX -DPOSIX -DLINUX -DGNUC -DCOMPILER_GCC -DPLATFORM_64BITS -D_FILE_OFFSET_BITS=64)
add_definitions(-D_LINUX -DPOSIX -DLINUX -DGNUC -DCOMPILER_GCC -DPLATFORM_64BITS -D_FILE_OFFSET_BITS=64 -D_GLIBCXX_USE_CXX11_ABI=0)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dstricmp=strcasecmp -D_stricmp=strcasecmp -D_strnicmp=strncasecmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dstrnicmp=strncasecmp -D_snprintf=snprintf")
Expand All @@ -16,9 +15,11 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse -msse -fno-strict-aliasing"
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-threadsafe-statics -v -fvisibility=default")

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs=libprotobuf.a")

set(
COUNTER_STRIKE_SHARP_LINK_LIBRARIES
${SOURCESDK_LIB}/linux64/release/libprotobuf.a
${SOURCESDK_LIB}/linux64/libtier0.so
${SOURCESDK_LIB}/linux64/tier1.a
${SOURCESDK_LIB}/linux64/interfaces.a
Expand Down
11 changes: 10 additions & 1 deletion makefiles/shared.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ endif()

set(CMAKE_STATIC_LIBRARY_PREFIX "")

set(LIBRARIES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libraries)
set(SOURCESDK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libraries/hl2sdk-cs2)
set(METAMOD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libraries/metamod-source)

set(SOURCESDK ${SOURCESDK_DIR}/${BRANCH})
set(SOURCESDK_LIB ${SOURCESDK}/lib)

add_definitions(-DMETA_IS_SOURCE2)
add_definitions(-DMETA_IS_SOURCE2 -D_ITERATOR_DEBUG_LEVEL=0)

if(DEFINED ENV{GITHUB_SHA_SHORT})
add_definitions(-DGITHUB_SHA="$ENV{GITHUB_SHA_SHORT}")
Expand All @@ -40,6 +41,10 @@ else()
add_definitions(-DBUILD_NUMBER="0")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
endif()

include_directories(
${SOURCESDK}
${SOURCESDK}/thirdparty/protobuf-3.21.8/src
Expand All @@ -57,9 +62,11 @@ include_directories(
${SOURCESDK}/public/schemasystem
${METAMOD_DIR}/core
${METAMOD_DIR}/core/sourcehook
libraries/Protobufs/csgo
libraries/dyncall/dynload
libraries/dyncall/dyncall
libraries/spdlog/include
# libraries/protobuf
libraries/tl
libraries/funchook/include
libraries/DynoHook/src
Expand All @@ -68,3 +75,5 @@ include_directories(
)

include(${CMAKE_CURRENT_LIST_DIR}/metamod/configure_metamod.cmake)


1 change: 1 addition & 0 deletions makefiles/windows.base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /OPT
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /NODEFAULTLIB:libcmt")

set(COUNTER_STRIKE_SHARP_LINK_LIBRARIES
${SOURCESDK_LIB}/public/win64/2015/libprotobuf.lib
${SOURCESDK_LIB}/public/win64/tier0.lib
${SOURCESDK_LIB}/public/win64/tier1.lib
${SOURCESDK_LIB}/public/win64/interfaces.lib
Expand Down
Loading

0 comments on commit 10f472e

Please sign in to comment.