From f26ea45ec195f8bc796206e4a543adfacd532dc5 Mon Sep 17 00:00:00 2001 From: nosoop Date: Thu, 11 Jul 2024 05:27:03 -0700 Subject: [PATCH] Get TF2Items building on 64-bit - Pull alliedmodders/sourcemod#2107 wholesale to modernize the build script (multiplat, hl2sdk-manifests, safetyhook) and make some minor adjustments to support CDetour. - Stub out plugin compilation for now; it will be added in a separate commit to preserve readability. - Hoist reading 'tf2.items.nosoop' and add it as a fallback for GiveNamedItem. SM's gamedata updater will stomp all over any changes we make in asherkin's. - Fix sizing for reverse-engineered TF2 classes to match the 64-bit release. - Rename smsdk_ext.hpp since CDetour is picky about the filename. --- .gitignore | 3 + AMBuildScript | 519 +++++++++--------- buildbot/PackageScript | 12 +- configure.py | 39 +- extension/AMBuilder | 42 +- extension/extension.cpp | 25 +- extension/extension.h | 10 + .../sdk/{smsdk_config.hpp => smsdk_config.h} | 0 extension/sdk/smsdk_ext.hpp | 2 +- tf2.items.nosoop.txt | 11 + tf2.items.txt | 2 + 11 files changed, 350 insertions(+), 315 deletions(-) rename extension/sdk/{smsdk_config.hpp => smsdk_config.h} (100%) diff --git a/.gitignore b/.gitignore index fa52adf..708ef23 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ /extension/msvc10/tf2items.sdf /extension/msvc10/tf2items.sln /extension/msvc10/tf2items.suo + +hl2sdk-manifests/ +safetyhook/ diff --git a/AMBuildScript b/AMBuildScript index f72223b..1b45ecd 100644 --- a/AMBuildScript +++ b/AMBuildScript @@ -1,47 +1,5 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: -import os, sys -import shutil - -# Simple extensions do not need to modify this file. - -class SDK(object): - def __init__(self, sdk, ext, aDef, name, platform, dir): - self.folder = 'hl2sdk-' + dir - self.envvar = sdk - self.ext = ext - self.code = aDef - self.define = name - self.platform = platform - self.name = dir - self.path = None # Actual path - -WinOnly = ['windows'] -WinLinux = ['windows', 'linux'] -WinLinuxMac = ['windows', 'linux', 'mac'] - -PossibleSDKs = { - 'episode1': SDK('HL2SDK', '1.ep1', '1', 'EPISODEONE', WinLinux, 'episode1'), - 'ep2': SDK('HL2SDKOB', '2.ep2', '3', 'ORANGEBOX', WinLinux, 'orangebox'), - 'css': SDK('HL2SDKCSS', '2.css', '6', 'CSS', WinLinuxMac, 'css'), - 'hl2dm': SDK('HL2SDKHL2DM', '2.hl2dm', '7', 'HL2DM', WinLinuxMac, 'hl2dm'), - 'dods': SDK('HL2SDKDODS', '2.dods', '8', 'DODS', WinLinuxMac, 'dods'), - 'sdk2013': SDK('HL2SDK2013', '2.sdk2013', '9', 'SDK2013', WinLinuxMac, 'sdk2013'), - 'tf2': SDK('HL2SDKTF2', '2.ep2v', '11', 'TF2', WinLinuxMac, 'tf2'), - 'l4d': SDK('HL2SDKL4D', '2.l4d', '12', 'LEFT4DEAD', WinLinuxMac, 'l4d'), - 'nucleardawn': SDK('HL2SDKND', '2.nd', '13', 'NUCLEARDAWN', WinLinuxMac, 'nucleardawn'), - 'l4d2': SDK('HL2SDKL4D2', '2.l4d2', '15', 'LEFT4DEAD2', WinLinuxMac, 'l4d2'), - 'darkm': SDK('HL2SDK-DARKM', '2.darkm', '2', 'DARKMESSIAH', WinOnly, 'darkm'), - 'swarm': SDK('HL2SDK-SWARM', '2.swarm', '16', 'ALIENSWARM', WinOnly, 'swarm'), - 'bgt': SDK('HL2SDK-BGT', '2.bgt', '4', 'BLOODYGOODTIME', WinOnly, 'bgt'), - 'eye': SDK('HL2SDK-EYE', '2.eye', '5', 'EYE', WinOnly, 'eye'), - 'csgo': SDK('HL2SDKCSGO', '2.csgo', '21', 'CSGO', WinLinuxMac, 'csgo'), - 'portal2': SDK('HL2SDKPORTAL2', '2.portal2', '17', 'PORTAL2', [], 'portal2'), - 'blade': SDK('HL2SDKBLADE', '2.blade', '18', 'BLADE', WinLinux, 'blade'), - 'insurgency': SDK('HL2SDKINSURGENCY', '2.insurgency', '19', 'INSURGENCY', WinLinuxMac, 'insurgency'), - 'contagion': SDK('HL2SDKCONTAGION', '2.contagion', '14', 'CONTAGION', WinOnly, 'contagion'), - 'bms': SDK('HL2SDKBMS', '2.bms', '10', 'BMS', WinLinux, 'bms'), - 'doi': SDK('HL2SDKDOI', '2.doi', '20', 'DOI', WinLinuxMac, 'doi'), -} +import os, shutil def ResolveEnvPath(env, folder): if env in os.environ: @@ -63,17 +21,73 @@ def ResolveEnvPath(env, folder): def Normalize(path): return os.path.abspath(os.path.normpath(path)) + +def SetArchFlags(compiler): + if compiler.behavior == 'gcc': + if compiler.target.arch == 'x86_64': + compiler.cflags += ['-fPIC'] + elif compiler.like('msvc'): + if compiler.target.arch == 'x86_64': + compiler.defines += ['WIN64'] + +hl2sdk_manifests_root = None +hl2sdk_manifests_dest = Normalize(builder.sourcePath + '/hl2sdk-manifests/') + +if not builder.options.hl2sdk_manifest: + if not os.path.exists(hl2sdk_manifests_dest): # if no root was specified and an hl2sdk-manifests folder does not exists in the source folder + raise Exception('HL2SDK Manifests directory not found! Set --hl2sdk-manifest-path or manually copy the hl2sdk manifests folder to the current source folder.') +else: + # The HL2SDK Manifest looks for the manifest file based on the current sourcePath, so we need to copy it to our sourcePath first + hl2sdk_manifests_root = Normalize(builder.options.hl2sdk_manifest + '/') + + if not os.path.exists(hl2sdk_manifests_dest): + shutil.copytree(hl2sdk_manifests_root, hl2sdk_manifests_dest) + else: + print('HL2SDK Manifests folder already exists, not copying.') + +SdkHelpers = builder.Eval('hl2sdk-manifests/SdkHelpers.ambuild', { + 'Project': 'sm-extension' +}) class ExtensionConfig(object): def __init__(self): + self.sdk_manifests = [] self.sdks = {} - self.binaries = [] + self.sdk_targets = [] self.extensions = [] - self.plugins = {} self.generated_headers = None self.mms_root = None self.sm_root = None - self.spcomp_bin = None + self.all_targets = [] + self.target_archs = set() + self.libsafetyhook = {} + + if builder.options.targets: + target_archs = builder.options.targets.split(',') + else: + target_archs = ['x86'] + if builder.backend != 'amb2': + target_archs.append('x86_64') + + for arch in target_archs: + try: + cxx = builder.DetectCxx(target_arch = arch) + self.target_archs.add(cxx.target.arch) + except Exception as e: + # Error if archs were manually overridden. + if builder.options.targets: + raise + print('Skipping target {}: {}'.format(arch, e)) + continue + self.all_targets.append(cxx) + + if not self.all_targets: + raise Exception('No suitable C/C++ compiler was found.') + + def use_auto_versioning(self): + if builder.backend != 'amb2': + return False + return not getattr(builder.options, 'disable_auto_versioning', False) @property def tag(self): @@ -81,48 +95,33 @@ class ExtensionConfig(object): return 'Debug' return 'Release' - def detectSDKs(self): - sdk_list = builder.options.sdks.split(',') - use_all = sdk_list[0] == 'all' - use_present = sdk_list[0] == 'present' - - for sdk_name in PossibleSDKs: - sdk = PossibleSDKs[sdk_name] - if builder.target_platform in sdk.platform: - if builder.options.hl2sdk_root: - sdk_path = os.path.join(builder.options.hl2sdk_root, sdk.folder) - else: - sdk_path = ResolveEnvPath(sdk.envvar, sdk.folder) - if sdk_path is None or not os.path.isdir(sdk_path): - if use_all or sdk_name in sdk_list: - raise Exception('Could not find a valid path for {0}'.format(sdk.envvar)) - continue - if use_all or use_present or sdk_name in sdk_list: - sdk.path = Normalize(sdk_path) - self.sdks[sdk_name] = sdk - - if len(self.sdks) < 1: - raise Exception('At least one SDK must be available.') + def findSdkPath(self, sdk_name): + dir_name = 'hl2sdk-{}'.format(sdk_name) + if builder.options.hl2sdk_root: + sdk_path = os.path.join(builder.options.hl2sdk_root, dir_name) + if os.path.exists(sdk_path): + return sdk_path + return ResolveEnvPath('HL2SDK{}'.format(sdk_name.upper()), dir_name) - if builder.options.sm_path: - self.sm_root = builder.options.sm_path - else: - self.sm_root = ResolveEnvPath('SOURCEMOD18', 'sourcemod-1.8') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD', 'sourcemod') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod-central') + def shouldIncludeSdk(self, sdk): + return not sdk.get('source2', False) - if not self.sm_root or not os.path.isdir(self.sm_root): - raise Exception('Could not find a source copy of SourceMod') - self.sm_root = Normalize(self.sm_root) + def detectSDKs(self): + sdk_list = [s for s in builder.options.sdks.split(',') if s] + SdkHelpers.sdk_filter = self.shouldIncludeSdk + SdkHelpers.find_sdk_path = self.findSdkPath + SdkHelpers.findSdks(builder, self.all_targets, sdk_list) + + self.sdks = SdkHelpers.sdks + self.sdk_manifests = SdkHelpers.sdk_manifests + self.sdk_targets = SdkHelpers.sdk_targets if builder.options.mms_path: self.mms_root = builder.options.mms_path else: - self.mms_root = ResolveEnvPath('MMSOURCE110', 'mmsource-1.10') + self.mms_root = ResolveEnvPath('MMSOURCE112', 'mmsource-1.12') if not self.mms_root: - self.mms_root = ResolveEnvPath('MMSOURCE', 'metamod-source') + self.mms_root = ResolveEnvPath('MMSOURCE_DEV', 'metamod-source') if not self.mms_root: self.mms_root = ResolveEnvPath('MMSOURCE_DEV', 'mmsource-central') @@ -130,27 +129,46 @@ class ExtensionConfig(object): raise Exception('Could not find a source copy of Metamod:Source') self.mms_root = Normalize(self.mms_root) - def detectSPCompiler(self): - if builder.options.no_plugins: - return - spcomp_path = builder.options.spcomp_path - if not spcomp_path or not os.path.isdir(spcomp_path): - spcomp_found = shutil.which("spcomp") - if not spcomp_found: - raise Exception('Could not find a path for spcomp') - self.spcomp_bin = shutil.which('spcomp', path = spcomp_path) - if not self.spcomp_bin: - raise Exception('Could not find spcomp') + if builder.options.sm_path: + self.sm_root = builder.options.sm_path + else: + self.sm_root = ResolveEnvPath('SOURCEMOD112', 'sourcemod-1.12') + if not self.sm_root: + self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod') + if not self.sm_root: + self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod-central') + + if not self.sm_root or not os.path.isdir(self.sm_root): + raise Exception('Could not find a source copy of SourceMod') + self.sm_root = Normalize(self.sm_root) def configure(self): - cxx = builder.DetectCompilers() + + allowed_archs = ['x86','x86_64'] + + if not set(self.target_archs).issubset(allowed_archs): + raise Exception('Unknown target architecture: {0}'.format(self.target_archs)) + + for cxx in self.all_targets: + self.configure_cxx(cxx) + + def configure_cxx(self, cxx): + if cxx.family == 'msvc': + if cxx.version < 1914 and builder.options.generator != 'vs': + raise Exception(f'Only MSVC 2017 15.7 and later are supported, full C++17 support is required. ({str(cxx.version)} < 1914)') + elif cxx.family == 'gcc': + if cxx.version < 'gcc-9': + raise Exception('Only GCC versions 9 or later are supported, full C++17 support is required.') + elif cxx.family == 'clang': + if cxx.version < 'clang-5': + raise Exception('Only clang versions 5 or later are supported, full C++17 support is required.') if cxx.like('gcc'): self.configure_gcc(cxx) - elif cxx.vendor == 'msvc': + elif cxx.family == 'msvc': self.configure_msvc(cxx) - # Optimization + # Optimizaiton if builder.options.opt == '1': cxx.defines += ['NDEBUG'] @@ -159,16 +177,15 @@ class ExtensionConfig(object): cxx.defines += ['DEBUG', '_DEBUG'] # Platform-specifics - if builder.target_platform == 'linux': + if cxx.target.platform == 'linux': self.configure_linux(cxx) - elif builder.target_platform == 'mac': + elif cxx.target.platform == 'mac': self.configure_mac(cxx) - elif builder.target_platform == 'windows': + elif cxx.target.platform == 'windows': self.configure_windows(cxx) - # Finish up. cxx.includes += [ - os.path.join(self.sm_root, 'public'), + os.path.join(builder.sourcePath, 'public'), ] def configure_gcc(self, cxx): @@ -188,31 +205,39 @@ class ExtensionConfig(object): '-Wno-unused', '-Wno-switch', '-Wno-array-bounds', - '-msse', - '-m32', '-fvisibility=hidden', ] + if cxx.target.arch in ['x86', 'x86_64']: + cxx.cflags += ['-msse'] + + cxx.cxxflags += ['-std=c++17'] + cxx.cxxflags += [ - '-std=c++14', - '-fno-exceptions', '-fno-threadsafe-statics', '-Wno-non-virtual-dtor', '-Wno-overloaded-virtual', + '-Wno-register', '-fvisibility-inlines-hidden', ] - cxx.linkflags += ['-m32'] - have_gcc = cxx.vendor == 'gcc' - have_clang = cxx.vendor == 'clang' - if cxx.version >= 'clang-3.6': + have_gcc = cxx.family == 'gcc' + have_clang = cxx.family == 'clang' + if cxx.version >= 'clang-3.9' or cxx.version == 'clang-3.4' or cxx.version > 'apple-clang-6.0': + cxx.cxxflags += ['-Wno-expansion-to-defined'] + if cxx.version == 'clang-3.9' or cxx.version == 'apple-clang-8.0': + cxx.cflags += ['-Wno-varargs'] + if cxx.version >= 'clang-3.4' or cxx.version >= 'apple-clang-7.0': cxx.cxxflags += ['-Wno-inconsistent-missing-override'] + if cxx.version >= 'clang-2.9' or cxx.version >= 'apple-clang-3.0': + cxx.cxxflags += ['-Wno-null-dereference'] if have_clang or (cxx.version >= 'gcc-4.6'): cxx.cflags += ['-Wno-narrowing'] if have_clang or (cxx.version >= 'gcc-4.7'): cxx.cxxflags += ['-Wno-delete-non-virtual-dtor'] if cxx.version >= 'gcc-4.8': cxx.cflags += ['-Wno-unused-result'] - + if cxx.version >= 'gcc-9.0': + cxx.cxxflags += ['-Wno-class-memaccess', '-Wno-packed-not-aligned'] if have_clang: cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch'] if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4': @@ -221,13 +246,25 @@ class ExtensionConfig(object): cxx.cxxflags += ['-Wno-deprecated'] cxx.cflags += ['-Wno-sometimes-uninitialized'] + # Work around SDK warnings. + if cxx.version >= 'clang-10.0' or cxx.version >= 'apple-clang-12.0': + cxx.cflags += [ + '-Wno-implicit-int-float-conversion', + '-Wno-tautological-overlap-compare', + ] + if have_gcc: cxx.cflags += ['-mfpmath=sse'] + cxx.cflags += ['-Wno-maybe-uninitialized'] if builder.options.opt == '1': cxx.cflags += ['-O3'] + # Don't omit the frame pointer. + cxx.cflags += ['-fno-omit-frame-pointer'] + def configure_msvc(self, cxx): + if builder.options.debug == '1': cxx.cflags += ['/MTd'] cxx.linkflags += ['/NODEFAULTLIB:libcmt'] @@ -246,9 +283,9 @@ class ExtensionConfig(object): '/EHsc', '/GR-', '/TP', + '/std:c++17', ] cxx.linkflags += [ - '/MACHINE:X86', 'kernel32.lib', 'user32.lib', 'gdi32.lib', @@ -275,27 +312,66 @@ class ExtensionConfig(object): cxx.cflags += ['/Oy-'] def configure_linux(self, cxx): - cxx.defines += ['_LINUX', 'POSIX'] - cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm'] - if cxx.vendor == 'gcc': + cxx.defines += ['LINUX', '_LINUX', 'POSIX', '_FILE_OFFSET_BITS=64'] + cxx.linkflags += ['-lm'] + if cxx.family == 'gcc': cxx.linkflags += ['-static-libgcc'] - elif cxx.vendor == 'clang': + elif cxx.family == 'clang': cxx.linkflags += ['-lgcc_eh'] + cxx.linkflags += ['-static-libstdc++'] def configure_mac(self, cxx): - cxx.defines += ['OSX', '_OSX', 'POSIX'] - cxx.cflags += ['-mmacosx-version-min=10.5'] + cxx.defines += ['OSX', '_OSX', 'POSIX', 'KE_ABSOLUTELY_NO_STL'] + cxx.cflags += ['-mmacosx-version-min=10.15'] cxx.linkflags += [ - '-mmacosx-version-min=10.5', - '-arch', 'i386', - '-lstdc++', - '-stdlib=libstdc++', + '-mmacosx-version-min=10.15', + '-stdlib=libc++', + '-lc++', ] - cxx.cxxflags += ['-stdlib=libstdc++'] + cxx.cxxflags += ['-stdlib=libc++'] def configure_windows(self, cxx): cxx.defines += ['WIN32', '_WINDOWS'] - + + def LibraryBuilder(self, compiler, name): + binary = compiler.Library(name) + self.AddVersioning(binary) + if binary.compiler.like('msvc'): + binary.compiler.linkflags += ['/SUBSYSTEM:WINDOWS'] + self.AddCxxCompat(binary) + return binary + + def ProgramBuilder(self, compiler, name): + binary = compiler.Program(name) + self.AddVersioning(binary) + if '-static-libgcc' in binary.compiler.linkflags: + binary.compiler.linkflags.remove('-static-libgcc') + if '-lgcc_eh' in binary.compiler.linkflags: + binary.compiler.linkflags.remove('-lgcc_eh') + if binary.compiler.like('gcc'): + binary.compiler.linkflags += ['-lstdc++', '-lpthread'] + if binary.compiler.like('msvc'): + binary.compiler.linkflags += ['/SUBSYSTEM:CONSOLE'] + return binary + + def StaticLibraryBuilder(self, compiler, name): + return compiler.StaticLibrary(name) + + def Library(self, context, compiler, name): + compiler = compiler.clone() + SetArchFlags(compiler) + return self.LibraryBuilder(compiler, name) + + def Program(self, context, compiler, name): + compiler = compiler.clone() + SetArchFlags(compiler) + return self.ProgramBuilder(compiler, name) + + def StaticLibrary(self, context, compiler, name): + compiler = compiler.clone() + SetArchFlags(compiler) + return self.StaticLibraryBuilder(compiler, name) + def ConfigureForExtension(self, context, compiler): compiler.cxxincludes += [ os.path.join(context.currentSourcePath), @@ -308,164 +384,93 @@ class ExtensionConfig(object): ] return compiler - def ConfigureForHL2(self, binary, sdk): - compiler = binary.compiler - - if sdk.name == 'episode1': - mms_path = os.path.join(self.mms_root, 'core-legacy') - else: - mms_path = os.path.join(self.mms_root, 'core') + def ExtLibrary(self, context, compiler, name): + binary = self.Library(context, compiler, name) + SetArchFlags(compiler) + self.ConfigureForExtension(context, binary.compiler) + return binary - compiler.cxxincludes += [ - os.path.join(mms_path), - os.path.join(mms_path, 'sourcehook'), - ] + def AddCxxCompat(self, binary): + if binary.compiler.target.platform == 'linux': + binary.sources += [ + os.path.join(self.sm_root, 'public', 'amtl', 'compat', 'stdcxx.cpp'), + ] - defines = ['SE_' + PossibleSDKs[i].define + '=' + PossibleSDKs[i].code for i in PossibleSDKs] - compiler.defines += defines + def ConfigureForHL2(self, context, binary, sdk): + compiler = binary.compiler + SetArchFlags(compiler) - paths = [ - ['public'], - ['public', 'engine'], - ['public', 'mathlib'], - ['public', 'vstdlib'], - ['public', 'tier0'], - ['public', 'tier1'] + compiler.cxxincludes += [ + os.path.join(self.mms_root, 'core'), + os.path.join(self.mms_root, 'core', 'sourcehook'), ] - if sdk.name == 'episode1' or sdk.name == 'darkm': - paths.append(['public', 'dlls']) - paths.append(['game_shared']) - else: - paths.append(['public', 'game', 'server']) - paths.append(['public', 'toolframework']) - paths.append(['game', 'shared']) - paths.append(['common']) - - compiler.defines += ['SOURCE_ENGINE=' + sdk.code] - - if sdk.name in ['sdk2013', 'bms'] and compiler.like('gcc'): - # The 2013 SDK already has these in public/tier0/basetypes.h - compiler.defines.remove('stricmp=strcasecmp') - compiler.defines.remove('_stricmp=strcasecmp') - compiler.defines.remove('_snprintf=snprintf') - compiler.defines.remove('_vsnprintf=vsnprintf') - - if compiler.like('msvc'): - compiler.defines += ['COMPILER_MSVC', 'COMPILER_MSVC32'] - compiler.linkflags += ['legacy_stdio_definitions.lib'] - else: - compiler.defines += ['COMPILER_GCC'] - - # For everything after Swarm, this needs to be defined for entity networking - # to work properly with sendprop value changes. - if sdk.name in ['blade', 'insurgency', 'doi', 'csgo']: - compiler.defines += ['NETWORK_VARS_ENABLED'] - if sdk.name in ['css', 'hl2dm', 'dods', 'sdk2013', 'bms', 'tf2', 'l4d', 'nucleardawn', 'l4d2']: - if builder.target_platform in ['linux', 'mac']: - compiler.defines += ['NO_HOOK_MALLOC', 'NO_MALLOC_OVERRIDE'] + for other_sdk in self.sdk_manifests: + compiler.defines += ['SE_{}={}'.format(other_sdk['define'], other_sdk['code'])] - if sdk.name == 'csgo' and builder.target_platform == 'linux': - compiler.linkflags += ['-lstdc++'] - - for path in paths: - compiler.cxxincludes += [os.path.join(sdk.path, *path)] - - if builder.target_platform == 'linux': - if sdk.name == 'episode1': - lib_folder = os.path.join(sdk.path, 'linux_sdk') - elif sdk.name in ['sdk2013', 'bms']: - lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32') - else: - lib_folder = os.path.join(sdk.path, 'lib', 'linux') - elif builder.target_platform == 'mac': - if sdk.name in ['sdk2013', 'bms']: - lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32') - else: - lib_folder = os.path.join(sdk.path, 'lib', 'mac') + SdkHelpers.configureCxx(context, binary, sdk) - if builder.target_platform in ['linux', 'mac']: - if sdk.name in ['sdk2013', 'bms']: - compiler.postlink += [ - compiler.Dep(os.path.join(lib_folder, 'tier1.a')), - compiler.Dep(os.path.join(lib_folder, 'mathlib.a')) - ] - else: - compiler.postlink += [ - compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a')), - compiler.Dep(os.path.join(lib_folder, 'mathlib_i486.a')) - ] + return binary - if sdk.name in ['blade', 'insurgency', 'doi', 'csgo']: - compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))] + def AddCDetour(self, binary): + public_path = os.path.join(self.sm_root, 'public') + binary.sources += [ os.path.join(public_path, 'CDetour', 'detours.cpp') ] + binary.compiler.cxxincludes += [ os.path.join(builder.sourcePath, 'safetyhook', 'include') ] - dynamic_libs = [] - if builder.target_platform == 'linux': - if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'bms', 'nucleardawn', 'l4d2', 'insurgency', 'doi']: - dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so'] - elif sdk.name in ['l4d', 'blade', 'insurgency', 'doi', 'csgo']: - dynamic_libs = ['libtier0.so', 'libvstdlib.so'] - else: - dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so'] - elif builder.target_platform == 'mac': - compiler.linkflags.append('-liconv') - dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib'] - elif builder.target_platform == 'windows': - libs = ['tier0', 'tier1', 'vstdlib', 'mathlib'] - if sdk.name in ['swarm', 'blade', 'insurgency', 'doi', 'csgo']: - libs.append('interfaces') - for lib in libs: - lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib' - compiler.linkflags.append(compiler.Dep(lib_path)) - - for library in dynamic_libs: - source_path = os.path.join(lib_folder, library) - output_path = os.path.join(binary.localFolder, library) - - def make_linker(source_path, output_path): - def link(context, binary): - cmd_node, (output,) = context.AddSymlink(source_path, output_path) - return output - return link - - linker = make_linker(source_path, output_path) - compiler.linkflags[0:0] = [compiler.Dep(library, linker)] + for task in self.libsafetyhook: + if task.target.arch == binary.compiler.target.arch: + binary.compiler.linkflags += [task.binary] + return + raise Exception('No suitable build of safetyhook was found.') - return binary - - def HL2Library(self, context, name, sdk): - binary = context.compiler.Library(name) + def HL2Library(self, context, compiler, name, sdk): + binary = self.Library(context, compiler, name) self.ConfigureForExtension(context, binary.compiler) - return self.ConfigureForHL2(binary, sdk) - - def HL2Project(self, context, name): - project = context.compiler.LibraryProject(name) - self.ConfigureForExtension(context, project.compiler) - return project + return self.ConfigureForHL2(context, binary, sdk) + + def HL2Config(self, project, context, compiler, name, sdk): + binary = project.Configure(compiler, name, + '{0} - {1} {2}'.format(self.tag, sdk['name'], compiler.target.arch)) + self.AddCxxCompat(binary) + return self.ConfigureForHL2(context, binary, sdk) + + def HL2ExtConfig(self, project, context, compiler, name, sdk): + binary = project.Configure(compiler, name, + '{0} - {1} {2}'.format(self.tag, sdk['name'], compiler.target.arch)) + self.AddCxxCompat(binary) + self.ConfigureForHL2(context, binary, sdk) + self.ConfigureForExtension(context, binary.compiler) + return binary - def HL2Config(self, project, name, sdk): - binary = project.Configure(name, '{0} - {1}'.format(self.tag, sdk.name)) - return self.ConfigureForHL2(binary, sdk) +class SafetyHookShim(object): + def __init__(self): + self.all_targets = {} + self.libsafetyhook = {} Extension = ExtensionConfig() Extension.detectSDKs() -Extension.detectSPCompiler() Extension.configure() -# Add additional buildscripts here +# we need to pull safetyhook in locally because ambuild does not take kindly to outside relpaths +safetyhook_dest = Normalize(builder.sourcePath + '/safetyhook/') +shutil.copytree(os.path.join(Extension.sm_root, 'public', 'safetyhook'), safetyhook_dest, dirs_exist_ok=True) + +SafetyHook = SafetyHookShim() +SafetyHook.all_targets = Extension.all_targets +builder.Build('safetyhook/AMBuilder', {'SafetyHook': SafetyHook }) +Extension.libsafetyhook = SafetyHook.libsafetyhook + +# This will clone the list and each cxx object as we recurse, preventing child +# scripts from messing up global state. +builder.targets = builder.CloneableList(Extension.all_targets) + BuildScripts = [ 'extension/AMBuilder', ] -if not builder.options.no_plugins: - BuildScripts += [ - 'pawn/AMBuilder', - ] - if builder.backend == 'amb2': BuildScripts += [ 'buildbot/PackageScript', ] - # TODO implement breakpad-symbols job -builder.RunBuildScripts(BuildScripts, { 'Extension': Extension }) +builder.Build(BuildScripts, { 'Extension': Extension }) diff --git a/buildbot/PackageScript b/buildbot/PackageScript index 26a1d8b..7a4e009 100644 --- a/buildbot/PackageScript +++ b/buildbot/PackageScript @@ -14,10 +14,10 @@ folder_list = [ 'addons/sourcemod/scripting/include', ] -if Extension.plugins: - folder_list += [ - 'addons/sourcemod/plugins/optional', - ] +# if Extension.plugins: +# folder_list += [ +# 'addons/sourcemod/plugins/optional', +# ] # Create the distribution folder hierarchy. folder_map = {} @@ -60,5 +60,5 @@ for cxx_task in Extension.extensions: builder.AddCopy(cxx_task.binary, folder_map['addons/sourcemod/extensions']) # Copy plugins -for smx_file, smx_entry in Extension.plugins.items(): - builder.AddCopy(smx_entry, folder_map['addons/sourcemod/plugins/optional']) +# for smx_file, smx_entry in Extension.plugins.items(): +# builder.AddCopy(smx_entry, folder_map['addons/sourcemod/plugins/optional']) diff --git a/configure.py b/configure.py index 3406415..79e2fe3 100644 --- a/configure.py +++ b/configure.py @@ -4,24 +4,27 @@ # Simple extensions do not need to modify this file. -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') -builder.options.add_option('--mms-path', type=str, dest='mms_path', default=None, - help='Path to Metamod:Source') -builder.options.add_option('--sm-path', type=str, dest='sm_path', default=None, +parser = run.BuildParser(sourcePath=sys.path[0], api='2.2') +parser.options.add_argument('--hl2sdk-root', type=str, dest='hl2sdk_root', default=None, + help='Root search folder for HL2SDKs') +parser.options.add_argument('--hl2sdk-manifest-path', type=str, dest='hl2sdk_manifest', default=None, + help='Path to HL2SDK Manifests') +parser.options.add_argument('--sm-path', type=str, dest='sm_path', default=None, help='Path to SourceMod') -builder.options.add_option('--enable-debug', action='store_const', const='1', dest='debug', +parser.options.add_argument('--mms-path', type=str, dest='mms_path', default=None, + help='Path to Metamod:Source') + +parser.options.add_argument('--enable-debug', action='store_const', const='1', dest='debug', help='Enable debugging symbols') -builder.options.add_option('--enable-optimize', action='store_const', const='1', dest='opt', +parser.options.add_argument('--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('--spcomp-path', type=str, dest='spcomp_path', default=None, - help='Path to directory containing spcomp') -builder.options.add_option('--no-plugins', action='store_true', - help='Do not compile plugins') - -builder.Configure() +parser.options.add_argument('-s', '--sdks', default='present', dest='sdks', + help='Build against specified SDKs; valid args are "none", "all", "present",' + ' or comma-delimited list of engine names') +parser.options.add_argument('--targets', type=str, dest='targets', default=None, + help="Override the target architecture (use commas to separate multiple targets).") +# builder.options.add_option('--spcomp-path', type=str, dest='spcomp_path', default=None, +# help='Path to directory containing spcomp') +# builder.options.add_option('--no-plugins', action='store_true', +# help='Do not compile plugins') +parser.Configure() diff --git a/extension/AMBuilder b/extension/AMBuilder index d755fe0..2a84402 100644 --- a/extension/AMBuilder +++ b/extension/AMBuilder @@ -1,27 +1,15 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: -import os, sys +import os projectName = 'tf2items' +extName = projectName + '.ext.' +# smsdk_ext.cpp will be automatically added later sourceFiles = [ 'extension.cpp', - - os.path.join(Extension.sm_root, 'public', 'CDetour', 'detours.cpp'), - os.path.join(Extension.sm_root, 'public', 'asm', 'asm.c'), - - os.path.join(Extension.sm_root, 'public', 'libudis86', 'decode.c'), - os.path.join(Extension.sm_root, 'public', 'libudis86', 'itab.c'), - os.path.join(Extension.sm_root, 'public', 'libudis86', 'syn-att.c'), - os.path.join(Extension.sm_root, 'public', 'libudis86', 'syn-intel.c'), - os.path.join(Extension.sm_root, 'public', 'libudis86', 'syn.c'), - os.path.join(Extension.sm_root, 'public', 'libudis86', 'udis86.c') ] -############### -# Make sure to edit PackageScript, which copies your files to their appropriate locations -# Simple extensions do not need to modify past this point. - -project = Extension.HL2Project(builder, projectName + '.ext') +project = builder.LibraryProject(extName) if os.path.isfile(os.path.join(builder.currentSourcePath, 'sdk', 'smsdk_ext.cpp')): # Use the copy included in the project @@ -31,13 +19,21 @@ else: project.sources += [os.path.join(Extension.sm_root, 'public', 'smsdk_ext.cpp')] project.sources += sourceFiles - + for sdk_name in Extension.sdks: sdk = Extension.sdks[sdk_name] - - binary = Extension.HL2Config(project, projectName + '.ext.' + sdk.ext, sdk) - - # udis86 will raise a warning on memset without this set - binary.compiler.defines += ['HAVE_STRING_H'] + if sdk['name'] in ['mock']: + continue + + for cxx in builder.targets: + if not cxx.target.arch in sdk['platforms'][cxx.target.platform]: + continue + + # hack to use bcompat filenames + # I don't want to have to deal with complaints of people forgetting to delete the original + ext_or_tf = '2.ep2v' if sdk_name == 'tf2' else sdk['extension'] + + binary = Extension.HL2ExtConfig(project, builder, cxx, extName + ext_or_tf, sdk) + Extension.AddCDetour(binary) -Extension.extensions = builder.Add(project) +Extension.extensions += builder.Add(project) diff --git a/extension/extension.cpp b/extension/extension.cpp index 6fccb62..2373bd6 100644 --- a/extension/extension.cpp +++ b/extension/extension.cpp @@ -47,6 +47,11 @@ TF2Items g_TF2Items; SMEXT_LINK(&g_TF2Items); SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *); + +// FIXME: error C4789: buffer 'mfp' of size 8 bytes will be overrun; 8 bytes will be written starting at offset 8 +#if defined COMPILER_MSVC +#pragma warning( disable: 4789 ) +#endif SH_DECL_MANUALHOOK4(MHook_GiveNamedItem, 0, 0, 0, CBaseEntity *, char const *, int, CEconItemView *, bool); CDetour *g_pDetourGetLoadoutItem; @@ -510,16 +515,6 @@ bool TF2Items::SDK_OnLoad(char *error, size_t maxlen, bool late) { return false; } - int iOffset; - if (!g_pGameConf->GetOffset("GiveNamedItem", &iOffset)) - { - snprintf(error, maxlen, "Could not find offset for GiveNamedItem"); - return false; - } else { - SH_MANUALHOOK_RECONFIGURE(MHook_GiveNamedItem, iOffset, 0, 0); - g_pSM->LogMessage(myself, "\"GiveNamedItem\" offset = %d", iOffset); - } - if (!gameconfs->LoadGameConfigFile("tf2.items.nosoop", &g_pSecondaryGameConf, conf_error, sizeof(conf_error))) { if (conf_error[0]) @@ -528,6 +523,16 @@ bool TF2Items::SDK_OnLoad(char *error, size_t maxlen, bool late) { } return false; } + + int iOffset; + if (!g_pGameConf->GetOffset("GiveNamedItem", &iOffset) && !g_pSecondaryGameConf->GetOffset("GiveNamedItem", &iOffset)) + { + snprintf(error, maxlen, "Could not find offset for GiveNamedItem"); + return false; + } else { + SH_MANUALHOOK_RECONFIGURE(MHook_GiveNamedItem, iOffset, 0, 0); + g_pSM->LogMessage(myself, "\"GiveNamedItem\" offset = %d", iOffset); + } CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pSecondaryGameConf); g_pDetourGetLoadoutItem = DETOUR_CREATE_MEMBER(CTFPlayer_GetLoadoutItem, "GetLoadoutItem"); diff --git a/extension/extension.h b/extension/extension.h index a0b6ef7..829a739 100644 --- a/extension/extension.h +++ b/extension/extension.h @@ -84,7 +84,9 @@ class CEconItemAttribute int32 m_nRefundableCurrency; //12 }; +#ifndef PLATFORM_64BITS #pragma pack(push, 4) +#endif class CEconItemHandle @@ -159,11 +161,19 @@ class CEconItemView bool m_bDoNotIterateStaticAttributes; //144 }; +#ifndef PLATFORM_64BITS #pragma pack(pop) +#endif +#ifdef PLATFORM_64BITS +static_assert(sizeof(CEconItemView) == 208, "CEconItemView - incorrect size on this compiler"); +static_assert(sizeof(CEconItemHandle) == 32, "CEconItemHandle - incorrect size on this compiler"); +static_assert(sizeof(CAttributeList) == 48, "CAttributeList - incorrect size on this compiler"); +#else static_assert(sizeof(CEconItemView) == 148, "CEconItemView - incorrect size on this compiler"); static_assert(sizeof(CEconItemHandle) == 24, "CEconItemHandle - incorrect size on this compiler"); static_assert(sizeof(CAttributeList) == 28, "CAttributeList - incorrect size on this compiler"); +#endif // enable to debug memory layout issues #if 0 diff --git a/extension/sdk/smsdk_config.hpp b/extension/sdk/smsdk_config.h similarity index 100% rename from extension/sdk/smsdk_config.hpp rename to extension/sdk/smsdk_config.h diff --git a/extension/sdk/smsdk_ext.hpp b/extension/sdk/smsdk_ext.hpp index a3de9b3..520a9b3 100644 --- a/extension/sdk/smsdk_ext.hpp +++ b/extension/sdk/smsdk_ext.hpp @@ -37,7 +37,7 @@ * @brief Contains wrappers for making Extensions easier to write. */ -#include "smsdk_config.hpp" +#include "smsdk_config.h" #include #include #include diff --git a/tf2.items.nosoop.txt b/tf2.items.nosoop.txt index bbd094c..f6cca76 100644 --- a/tf2.items.nosoop.txt +++ b/tf2.items.nosoop.txt @@ -2,12 +2,23 @@ { "tf" { + "Offsets" + { + "GiveNamedItem" + { + // offset copy here just so it doesn't get stomped by SM autoupdate + "windows64" "486" + "linux64" "493" + } + } "Signatures" { "GetLoadoutItem" { "windows" "\x55\x8B\xEC\x83\xEC\x10\x53\x56\x8B\xF1\x8B\x0D\x2A\x2A\x2A\x2A" + "windows64" "\x48\x89\x5C\x24\x08\x48\x89\x6C\x24\x10\x48\x89\x74\x24\x18\x44\x88\x4C\x24\x20\x57\x41\x54" "linux" "@_ZN9CTFPlayer14GetLoadoutItemEiib" + "linux64" "@_ZN9CTFPlayer14GetLoadoutItemEiib" } } } diff --git a/tf2.items.txt b/tf2.items.txt index 907a9dd..f395e67 100644 --- a/tf2.items.txt +++ b/tf2.items.txt @@ -9,6 +9,8 @@ "windows" "486" "linux" "493" "mac" "493" + "windows64" "486" + "linux64" "493" } } }