Simple GMOD module template for MSVC++

This is a little project of mine which helps me keep organized when including the many different header files and cpp files in a gmod module project. Simply rename the folder and files and follow the instructions inside of main.cpp in order to configure your template to compile with certain libraries. All the required libraries can be added to the module by simply uncommenting one line.

The following libraries supported are:

  • Main GMOD header files
  • Detours 1.5 and 2.1
  • Sigscan
  • VFNHooks
  • A small library of function(s) I’ve been adding and will continue to update

Changelog:
[v1.3]

**Fixed some memory leaks and crashes for select circumstances

***[v1.2]

****Fixed crashes when loading onto multiple Lua states
*Added gLua_Server, gLua_Client, and gLua_Menu to denote each state the module is run on
*Fixed compile errors when including the base headers more than once
**Added an example of a basic print function using C++

[v1.1]
*Added the ability to turn off the info message when the module loads, set default to “No”

Download v1.3 here

If you have any suggestions or problems please comment below!

If you are missing interface.h, make sure you have added:
C:/path/to/sdk/public
C:/path/to/sdk/utils
C:/path/to/sdk/public/tier0
C:/path/to/sdk/public/tier1

to your Tools -> Options… -> Projects and Solutions -> VC++ Directories -> Include files

and

C:/path/to/sdk/lib/public

to your Tools -> Options… -> Projects and Solutions -> VC++ Directories -> Library files

This will be very helpful for people new to C++ who can’t figure out how to get MSVC++ configured properly. I had to have someone on TeamViewer to configure mine when I first started.

With Mac and Linux support coming to gmod in the near future, it would be wise to move away from Win32-only libraries like detours. In fact, looking at this base, it is too platform-specific to be useful for module development much longer (unless encouraging poorly-developed platform-dependent modules is, in your eyes, a good thing).

The included sigscan file includes a bug that will cause a crash on modern systems when the signature is not found successfully, as it will search out of the bounds of the searched module by the length of the signature it is searching for.

Your GmodModuleConfig.h header will create binaries that break when loaded in both the client and server states of a listen server (or even client and menu state of any client) as gLua will point to the state of what mostly recently loaded the module, and not what is actually calling a function. If you insist on doing it this way, generate gmsv_ and gmcl_ binaries so that the binaries are only loaded for a single state. Generating binaries with gm_ is just asking for trouble down the line if you code them like this.

LuaExt.h will break compilation when included in more than one file of a project. Don’t define functions like that in headers.

Why exactly did you add a bunch of nops to the gmod_open and gmod_close functions in the GMLuaModule.h header?

Just a heads up, distributing Detours 2.1 without the detoured.dll is against the EULA. Might want to package it regardless if you kept the code requiring it to be used or not.

I am very tempted to say who cares about mac and linux, but if I had to play on a mac or linux machine I would think otherwise, so I will refrain from that. Anyways, are there any platform independent ways to achieve the functionality of the detours library? It’s used in a lot of extremely unique things that can’t be done any way else.

Yes, the Metamod CDetours class is able to gracefully detour source engine functions on both Windows and Linux. I’m not sure of OSX, but I wrote my post mostly from the perspective of server modules where there is no need to limit yourself to only being able to detour functions on Windows when it’s a relatively trivial endeavor to do the same on Linux (especially since the Linux binaries export almost everything meaning you can simply look up the function symbol instead of sigscanning).

Well this will be redundant when I release my Function Detouring code which works on MacOSX, Linux and Windows. Plus a range of thread safe classes. Which is all setup in a CMake style project.

I’ve taken all the posts in this thread into consideration and will be modifying the
template to help fix these concerns. If someone will help me locate CDetour and the fixed
sigscan lib, I’ll be happy to include those. For the multistate gLua, I’m workin to modify the LUA_FUNCTION to be multistate compatible and avoid some crashes there. I find that creating
modules for OSX and Linux may require some extra setup, so I don’t plan on including OSX support, though if I get my hands on CDetour we can work towards a Linux compatible detour lib. As for
the NOPs, I had to include those for a program called VMProtect, which helps protect the code
of the module for paid versions. I appreciate all the feedback!

[editline]10:58AM[/editline]

As for LuaExt.h, I assumed that the file would only be included in the main.cpp file anyways, but Ill throw it into a class for multifile use.

Let’s assume Google works.

Ah Metamod, what can that be, second result on Google points me to the metamod project website. Oh look it’s open-source, great! Now we can just download the source and get this class.

Thank you for stating the obvious while also being a bitch about it. Your e-penis is now one inch longer.

Could you make one for MSVC++ 2010 please?

It works in 2010… Just convert it

who gives a shit?

You may should add a bigger buffer to the sigscan-code (line 37 of sigscan.cpp).

I once got an error when I detoured virtual functions where I sigscanned the address before. The solution was, increasing the buffer in line 37.

[cpp] sig_mask = new char[sig_len+2]; //Original line was “sig_mask = new char[sig_len+1];”, but made my module fail with “invalid access to memory” (translated error message into English) @aVoN[/cpp]

I know, it makes absolutely no sense why it should fail, but this fixed the error.

I did but somehow I have to re-organize everything so it includes the right things and such, only I dont know if it will compile right, well I will have to try! xD

Edit:

yay it worked! Now trying some simple things…

Add support for linux to :3

Then it wouldn’t be a “MSVC++ template” anymore.

And so far, since there is no official lin-bin release yet (to my knowledge, there are only testing binaries), it makes no sense to add it right now.