Skip to content

Commit

Permalink
Few changes:
Browse files Browse the repository at this point in the history
- Removed the assert for 32-bit code and actually designed this extension to work in 32-bit srcds as well. This was a stupid idea from the beginning. Sorry.
- Added two new stocks - GetPointerSize() and GetArchitecture().
- Tweaked AMBuildScript so that the parameters actually make sense. (I am genuinely not a proper programmer.)
- I now provide x86-32 and x86-64 builds (which will be uploaded shortly).
  • Loading branch information
NotnHeavy committed Jul 1, 2024
1 parent fb3acb3 commit e36023c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 35 deletions.
30 changes: 13 additions & 17 deletions AMBuildScript
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class ExtensionConfig(object):
# Platform-specifics
if builder.target_platform == 'linux':
self.configure_linux(cxx)
elif builder.target_platform == 'mac':
self.configure_mac(cxx)
elif builder.target_platform == 'windows':
self.configure_windows(cxx)

Expand All @@ -95,6 +93,9 @@ class ExtensionConfig(object):
]

def configure_gcc(self, cxx):
arch = '-m32';
if builder.options.target == 'x86_64':
arch = '-m64';
cxx.defines += [
'stricmp=strcasecmp',
'_stricmp=strcasecmp',
Expand All @@ -112,7 +113,7 @@ class ExtensionConfig(object):
'-Wno-switch',
'-Wno-array-bounds',
'-msse',
'-m64',
arch,
'-fvisibility=hidden',
]
cxx.cxxflags += [
Expand All @@ -123,7 +124,7 @@ class ExtensionConfig(object):
'-Wno-overloaded-virtual',
'-fvisibility-inlines-hidden',
]
cxx.linkflags += ['-m64']
cxx.linkflags += [arch]

have_gcc = cxx.vendor == 'gcc'
have_clang = cxx.vendor == 'clang'
Expand Down Expand Up @@ -151,6 +152,9 @@ class ExtensionConfig(object):
cxx.cflags += ['-O3']

def configure_msvc(self, cxx):
arch = '/MACHINE:X86';
if builder.options.target == 'x86_64':
arch = '/MACHINE:X64';
if builder.options.debug == '1':
cxx.cflags += ['/MTd']
cxx.linkflags += ['/NODEFAULTLIB:libcmt']
Expand All @@ -171,7 +175,7 @@ class ExtensionConfig(object):
'/TP',
]
cxx.linkflags += [
'/MACHINE:X64',
arch,
'kernel32.lib',
'user32.lib',
'gdi32.lib',
Expand Down Expand Up @@ -205,19 +209,11 @@ class ExtensionConfig(object):
elif cxx.vendor == 'clang':
cxx.linkflags += ['-lgcc_eh']

def configure_mac(self, cxx):
cxx.defines += ['OSX', '_OSX', 'POSIX']
cxx.cflags += ['-mmacosx-version-min=10.5']
cxx.linkflags += [
'-mmacosx-version-min=10.5',
'-arch', 'i386',
'-lstdc++',
'-stdlib=libstdc++',
]
cxx.cxxflags += ['-stdlib=libstdc++']

def configure_windows(self, cxx):
cxx.defines += ['WIN64', '_WINDOWS']
arch = 'WIN32';
if builder.options.target == 'x86_64':
arch = 'WIN64';
cxx.defines += [arch, '_WINDOWS']

def ConfigureForExtension(self, context, compiler):
compiler.cxxincludes += [
Expand Down
17 changes: 7 additions & 10 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
builder = run.PrepareBuild(sourcePath = sys.path[0])

builder.options.add_option('--hl2sdk-root', type=str, dest='hl2sdk_root', default=None,
help='Root search folder for HL2SDKs')
help='Root search folder for HL2SDKs')
builder.options.add_option('--mms-path', type=str, dest='mms_path', default=None,
help='Path to Metamod:Source')
help='Path to Metamod:Source')
builder.options.add_option('--sm-path', type=str, dest='sm_path', default=None,
help='Path to SourceMod')
help='Path to SourceMod')
builder.options.add_option('--enable-debug', action='store_const', const='1', dest='debug',
help='Enable debugging symbols')
help='Enable debugging symbols')
builder.options.add_option('--enable-optimize', action='store_const', const='1', dest='opt',
help='Enable optimization')
builder.options.add_option('-s', '--sdks', default='all', dest='sdks',
help='Build against specified SDKs; valid args are "all", "present", or '
'comma-delimited list of engine names (default: %default)')
builder.options.add_option('--targets', type=str, dest='targets', default=None,
help="Override the target architecture (use commas to separate multiple targets).")
help='Enable optimization')
builder.options.add_option('--target', type=str, dest='target', default=None,
help="Override the target architecture. (Defaults to x86-32, specify x86-64 to use x86-64.)")

builder.Configure()
1 change: 1 addition & 0 deletions gamedata/smaddress64-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"i_honestly_dont_know_what_function_this_is_i_just_picked_a_random_one_from_ida"
{
"library" "server"
"windows" "\x55\x8B\xEC\x83\xEC\x10\x53\x56\x8B\xF1\x8B\x0D\x2A\x2A\x2A\x2A"
"windows64" "\x48\x89\x5C\x24\x08\x57\x48\x83\xEC\x60\x48\x8B\xD9\x8B\xFA"
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/smsdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/* Basic information exposed publicly */
#define SMEXT_CONF_NAME "SM-Address64"
#define SMEXT_CONF_DESCRIPTION "A temporary solution for 64-bit addressing"
#define SMEXT_CONF_VERSION "1.1.0.0"
#define SMEXT_CONF_VERSION "1.2.0.0"
#define SMEXT_CONF_AUTHOR "NotnHeavy"
#define SMEXT_CONF_URL "http://www.github.com/notnheavy"
#define SMEXT_CONF_LOGTAG "smaddress64"
Expand Down
29 changes: 29 additions & 0 deletions scripting/include/smaddress64.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,38 @@

const NumberType NumberType_Int64 = view_as<NumberType>(3);

enum Architecture
{
Architecture_x86,
Architecture_x64
};

//////////////////////////////////////////////////////////////////////////////
// ADDRESS FUNCTIONS //
//////////////////////////////////////////////////////////////////////////////

/**
* Get this architecture's pointer size. (4 = 32-bit, 8 = 64-bit)
*
* @return Pointer size of the architecture used for this program.
*/
stock int GetPointerSize()
{
return Native_GetPointerSize();
}

/**
* Returns whether the architecture used currently is x86-32 or x86-64.
*
* @return The architecture used.
*/
stock Architecture GetArchitecture()
{
if (GetPointerSize() == 4)
return Architecture_x86;
return Architecture_x64;
}

/**
* Dereferences a 64-bit address.
*
Expand Down Expand Up @@ -518,6 +546,7 @@ enum struct int64_t
// NATIVES //
//////////////////////////////////////////////////////////////////////////////

native int Native_GetPointerSize();
native void Native_LoadFromAddress64(any address[2], NumberType size, any buffer[2]);
native void Native_StoreToAddress64(any address[2], NumberType size, any buffer[2], bool setMemAccess = true);
native void Native_FromPseudoAddress(Address pseudoAddress, any address64[2]);
Expand Down
5 changes: 4 additions & 1 deletion scripting/smaddress64-test.sp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Plugin myinfo =
name = PLUGIN_NAME,
author = "NotnHeavy",
description = "Testing plugin for SM-Address64.",
version = "1.1",
version = "1.2",
url = "none"
};

Expand All @@ -32,6 +32,9 @@ public void OnPluginStart()
LoadTranslations("common.phrases");
PrintToServer("--------------------------------------------------------");

// get the pointer size
PrintToServer("pointer size: %u\n", GetPointerSize());

// nt info
int64_t KUSER_SHARED_DATA = { 0x7FFE0000, 0x00 };
KUSER_SHARED_DATA.low += 0x26C;
Expand Down
18 changes: 16 additions & 2 deletions src/addressing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ enum struct NumberType
// ADDRESS NATIVES //
//////////////////////////////////////////////////////////////////////////////

static cell_t Native_GetPointerSize(IPluginContext* pContext, const cell_t* params)
{
return sizeof(void*);
}

static cell_t Native_LoadFromAddress64(IPluginContext* pContext, const cell_t* params)
{
// Obtain parameters.
Expand All @@ -54,7 +59,7 @@ static cell_t Native_LoadFromAddress64(IPluginContext* pContext, const cell_t* p
*buffer = *(int32_t*)address;
break;
case NumberType::NumberType_Int64:
*buffer = *address;
*buffer = *(int64_t*)address;
break;
default:
pContext->ReportError("Invalid NumberType value %d", type);
Expand Down Expand Up @@ -99,7 +104,7 @@ static cell_t Native_StoreToAddress64(IPluginContext* pContext, const cell_t* pa
case NumberType::NumberType_Int64:
if (updateMemAccess)
SourceHook::SetMemAccess(address, sizeof(int64_t), SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC);
*address = *buffer;
*(int64_t*)address = *(int64_t*)buffer;
break;
default:
pContext->ReportError("Invalid NumberType value %d", type);
Expand All @@ -119,7 +124,11 @@ static cell_t Native_FromPseudoAddress(IPluginContext* pContext, const cell_t* p
pContext->LocalToPhysAddr(params[2], reinterpret_cast<cell_t**>(&buffer));

// Convert the pseudo-address to an absolute address.
#ifdef PLATFORM_X86
*buffer = pseudoAddress;
#else
*buffer = (uintptr_t)smutils->FromPseudoAddress(pseudoAddress);
#endif
return 0;
}

Expand All @@ -132,7 +141,11 @@ static cell_t Native_ToPseudoAddress(IPluginContext* pContext, const cell_t* par
pContext->LocalToPhysAddr(params[1], reinterpret_cast<cell_t**>(&address));

// Convert the absolute address to a pseudo-address.
#ifdef PLATFORM_X86
return *address; // Just return the original address.
#else
return smutils->ToPseudoAddress(reinterpret_cast<void*>(*address)); // no uintptr_t?
#endif
}

static cell_t Native_GetEntityAddress64(IPluginContext* pContext, const cell_t* params)
Expand Down Expand Up @@ -269,6 +282,7 @@ static cell_t Native_RtsInt64(IPluginContext* pContext, const cell_t* params)
//////////////////////////////////////////////////////////////////////////////

sp_nativeinfo_t g_AddressNatives[] = {
{ "Native_GetPointerSize", Native_GetPointerSize },
{ "Native_LoadFromAddress64", Native_LoadFromAddress64 },
{ "Native_StoreToAddress64", Native_StoreToAddress64 },
{ "Native_FromPseudoAddress", Native_FromPseudoAddress },
Expand Down
4 changes: 0 additions & 4 deletions src/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ SMEXT_LINK(&g_SMAddress64);

void SMAddress64::SDK_OnAllLoaded()
{
#ifdef PLATFORM_X86
assert(false);
#endif

sharesys->RegisterLibrary(myself, "SM-Address64");
sharesys->AddNatives(myself, g_AddressNatives);
}

0 comments on commit e36023c

Please sign in to comment.