├── .gitignore ├── scripts ├── install.sh ├── version.sh └── package.sh ├── .github └── workflows │ ├── compile.yml │ └── package.yml ├── scripting ├── vscript │ ├── gamesystem.sp │ ├── memory.sp │ ├── entity.sp │ ├── util.sp │ ├── field.sp │ ├── list.sp │ ├── variant.sp │ ├── binding.sp │ ├── class.sp │ ├── execute.sp │ ├── hscript.sp │ ├── vtable.sp │ └── function.sp ├── vscript_test.sp ├── include │ └── vscript.inc └── vscript.sp ├── README.md ├── gamedata └── vscript.txt └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.smx 2 | -------------------------------------------------------------------------------- /scripts/install.sh: -------------------------------------------------------------------------------- 1 | # Create build folder 2 | mkdir build 3 | cd build 4 | 5 | # Install SourceMod 6 | wget --input-file=http://sourcemod.net/smdrop/$SM_VERSION/sourcemod-latest-linux 7 | tar -xzf $(cat sourcemod-latest-linux) 8 | 9 | # Copy sp and compiler to build dir 10 | cp -r ../scripting addons/sourcemod 11 | cd addons/sourcemod/scripting 12 | 13 | # Install Dependencies 14 | wget "https://raw.githubusercontent.com/nosoop/SMExt-SourceScramble/master/scripting/include/sourcescramble.inc" -O include/sourcescramble.inc -------------------------------------------------------------------------------- /scripts/version.sh: -------------------------------------------------------------------------------- 1 | # Go to build scripting folder with vscript.sp 2 | cd build/addons/sourcemod/scripting 3 | 4 | # Get plugin version 5 | export PLUGIN_VERSION=$(sed -En '/#define PLUGIN_VERSION\W/p' vscript.sp) 6 | echo "PLUGIN_VERSION<> $GITHUB_ENV 7 | echo $PLUGIN_VERSION | grep -o '[0-9]*\.[0-9]*\.[0-9]*' >> $GITHUB_ENV 8 | echo 'EOF' >> $GITHUB_ENV 9 | 10 | # Set revision to vscript.sp 11 | sed -i -e 's/#define PLUGIN_VERSION_REVISION.*".*"/#define PLUGIN_VERSION_REVISION "'$PLUGIN_VERSION_REVISION'"/g' vscript.sp -------------------------------------------------------------------------------- /scripts/package.sh: -------------------------------------------------------------------------------- 1 | # Go to build dir 2 | cd build 3 | 4 | # Create package dir 5 | mkdir -p package/addons/sourcemod/plugins 6 | mkdir -p package/addons/sourcemod/gamedata 7 | mkdir -p package/addons/sourcemod/scripting 8 | 9 | # Copy all required stuffs to package 10 | cp -r addons/sourcemod/plugins/vscript.smx package/addons/sourcemod/plugins 11 | cp -r ../gamedata/vscript.txt package/addons/sourcemod/gamedata 12 | cp -r ../scripting/include package/addons/sourcemod/scripting 13 | cp -r ../LICENSE package 14 | 15 | # Create ZIP file 16 | cd package 17 | zip -r ../VScript-$PLUGIN_VERSION.$PLUGIN_VERSION_REVISION.zip * -------------------------------------------------------------------------------- /.github/workflows/compile.yml: -------------------------------------------------------------------------------- 1 | name: Compile 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | version: ["1.12"] 12 | 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v1 16 | 17 | - name: Environments 18 | run: | 19 | echo "SM_VERSION=${{ matrix.version }}" >> $GITHUB_ENV 20 | 21 | - name: Install 22 | run: | 23 | bash scripts/install.sh 24 | 25 | - name: Compile 26 | run: | 27 | cd build/addons/sourcemod/scripting 28 | ./spcomp -E vscript.sp -o ../plugins/vscript.smx 29 | ./spcomp -E vscript_test.sp -o ../plugins/vscript_test.smx -------------------------------------------------------------------------------- /.github/workflows/package.yml: -------------------------------------------------------------------------------- 1 | name: Package 2 | 3 | on: 4 | push: 5 | branches: main 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v1 14 | 15 | - name: Environments 16 | run: | 17 | echo "SM_VERSION=1.12" >> $GITHUB_ENV 18 | echo "PLUGIN_VERSION_REVISION<> $GITHUB_ENV 19 | git rev-list --count HEAD >> $GITHUB_ENV 20 | echo 'EOF' >> $GITHUB_ENV 21 | 22 | - name: Install 23 | run: | 24 | bash scripts/install.sh 25 | 26 | - name: Set Version 27 | run: | 28 | bash scripts/version.sh 29 | 30 | - name: Compile 31 | run: | 32 | cd build/addons/sourcemod/scripting 33 | ./spcomp vscript.sp -o ../plugins/vscript.smx 34 | 35 | - name: Package 36 | run: | 37 | bash scripts/package.sh 38 | 39 | - name: Release 40 | uses: softprops/action-gh-release@v2 41 | with: 42 | files: build/VScript-${{ env.PLUGIN_VERSION }}.${{ env.PLUGIN_VERSION_REVISION }}.zip 43 | tag_name: ${{ env.PLUGIN_VERSION }}.${{ env.PLUGIN_VERSION_REVISION }} 44 | generate_release_notes: true 45 | -------------------------------------------------------------------------------- /scripting/vscript/gamesystem.sp: -------------------------------------------------------------------------------- 1 | // Could've used sig to get VScriptServerInit and VScriptServerTerm, but were doing viruals only so it's more easier to work with multiple games 2 | 3 | static Handle g_hSDKCallLevelInitPreEntity; 4 | static Handle g_hSDKCallLevelShutdownPostEntity; 5 | 6 | static int g_iGameSystem_AllowEntityCreationInScripts; 7 | 8 | void GameSystem_LoadGamedata(GameData hGameData) 9 | { 10 | g_iGameSystem_AllowEntityCreationInScripts = hGameData.GetOffset("CVScriptGameSystem::m_bAllowEntityCreationInScripts"); 11 | 12 | g_hSDKCallLevelInitPreEntity = CreateSDKCall(hGameData, "IGameSystem", "LevelInitPreEntity"); 13 | g_hSDKCallLevelShutdownPostEntity = CreateSDKCall(hGameData, "IGameSystem", "LevelShutdownPostEntity"); 14 | 15 | // Figure out where g_pScriptVM is stored by searching through IGameSystem::FrameUpdatePostEntityThink and finding the correct instructions 16 | 17 | Address pFunction = VTable_GetAddressFromName(hGameData, "IGameSystem", "FrameUpdatePostEntityThink"); 18 | 19 | int iOffset; 20 | for (iOffset = 0; iOffset <= 100; iOffset++) 21 | { 22 | Address pInstruction = LoadFromAddress(pFunction + view_as
(iOffset), NumberType_Int8); 23 | 24 | if (g_bWindows) 25 | { 26 | // Windows need "8B 0D" 27 | Address pNext = LoadFromAddress(pFunction + view_as
(iOffset + 1), NumberType_Int8); 28 | if (pInstruction == view_as
(0x8B) && pNext == view_as
(0x0D)) 29 | { 30 | iOffset += 2; 31 | break; 32 | } 33 | } 34 | else 35 | { 36 | // Linux just need "A1" 37 | if (pInstruction == view_as
(0xA1)) 38 | { 39 | iOffset++; 40 | break; 41 | } 42 | } 43 | 44 | if (pInstruction == view_as
(0xCC) || iOffset >= 100) 45 | { 46 | LogError("Could not find address to get g_pScriptVM"); 47 | return; 48 | } 49 | } 50 | 51 | g_pToScriptVM = LoadFromAddress(pFunction + view_as
(iOffset), NumberType_Int32); 52 | } 53 | 54 | void GameSystem_ServerInit() 55 | { 56 | // m_bAllowEntityCreationInScripts is touched, but we don't really care unless if CreateSceneEntity is actually used 57 | MemoryBlock pMemory = new MemoryBlock(g_iGameSystem_AllowEntityCreationInScripts + 4); 58 | SDKCall(g_hSDKCallLevelInitPreEntity, pMemory.Address); 59 | delete pMemory; 60 | } 61 | 62 | void GameSystem_ServerTerm() 63 | { 64 | MemoryBlock pMemory = new MemoryBlock(g_iGameSystem_AllowEntityCreationInScripts + 4); 65 | SDKCall(g_hSDKCallLevelShutdownPostEntity, pMemory.Address); 66 | delete pMemory; 67 | } 68 | -------------------------------------------------------------------------------- /scripting/vscript/memory.sp: -------------------------------------------------------------------------------- 1 | static StringMap g_mMemoryBlocks; 2 | 3 | void Memory_Init() 4 | { 5 | g_mMemoryBlocks = new StringMap(); 6 | } 7 | 8 | void Memory_DeleteAddress(Address pAddress) 9 | { 10 | MemoryBlock hMemory; 11 | if (!g_mMemoryBlocks.GetValue(Memory_AddressToString(pAddress), hMemory)) 12 | return; 13 | 14 | delete hMemory; 15 | g_mMemoryBlocks.Remove(Memory_AddressToString(pAddress)); 16 | } 17 | 18 | void Memory_SetAddress(Address pAddress, MemoryBlock hMemory) 19 | { 20 | Memory_DeleteAddress(pAddress); 21 | StoreToAddress(pAddress, hMemory.Address, NumberType_Int32); 22 | g_mMemoryBlocks.SetValue(Memory_AddressToString(pAddress), hMemory); 23 | } 24 | 25 | void Memory_DisownAll() 26 | { 27 | StringMapSnapshot mSnapshot = g_mMemoryBlocks.Snapshot(); 28 | int iLength = mSnapshot.Length; 29 | for (int i = 0; i < iLength; i++) 30 | { 31 | char sKey[16]; 32 | mSnapshot.GetKey(i, sKey, sizeof(sKey)); 33 | MemoryBlock hMemory; 34 | g_mMemoryBlocks.GetValue(sKey, hMemory); 35 | hMemory.Disown(); 36 | } 37 | 38 | delete mSnapshot; 39 | } 40 | 41 | char[] Memory_AddressToString(Address pAddress) 42 | { 43 | char sBuffer[16]; 44 | Format(sBuffer, sizeof(sBuffer), "%08X", pAddress); 45 | return sBuffer; 46 | } 47 | 48 | /* 49 | CUtlVector { 50 | Data *m_pMemory; // +0 Ptr to items 51 | int m_nAllocationCount; // +4 Amount of allocated space 52 | int m_nGrowSize; // +8 Size by which memory grows 53 | int m_Size; // +12 Number of items in vector 54 | Data *m_pElements; // +16 Same as m_pMemory, used for debugging 55 | } 56 | */ 57 | 58 | void Memory_UtlVectorSetSize(Address pUtlVector, int iSize, int iCount) 59 | { 60 | int iAllocationCount = LoadFromAddress(pUtlVector + view_as
(4), NumberType_Int32); 61 | int iCurrentCount = LoadFromAddress(pUtlVector + view_as
(12), NumberType_Int32); 62 | 63 | if (iCurrentCount < iCount) 64 | StoreToAddress(pUtlVector + view_as
(12), iCount, NumberType_Int32); 65 | 66 | if (iAllocationCount < iCount) 67 | { 68 | Address pData = LoadFromAddress(pUtlVector + view_as
(0), NumberType_Int32); 69 | 70 | MemoryBlock hMemory = new MemoryBlock(iSize * iCount); 71 | for (int i = 0; i < iAllocationCount * iSize; i++) 72 | hMemory.StoreToOffset(i, LoadFromAddress(pData + view_as
(i), NumberType_Int8), NumberType_Int8); 73 | 74 | iAllocationCount = iCount; 75 | StoreToAddress(pUtlVector + view_as
(4), iAllocationCount, NumberType_Int32); 76 | 77 | Memory_SetAddress(pUtlVector + view_as
(0), hMemory); 78 | StoreToAddress(pUtlVector + view_as
(16), hMemory.Address, NumberType_Int32); 79 | } 80 | } 81 | 82 | Address Memory_CreateEmptyFunction(bool bReturn) 83 | { 84 | int iInstructions[8]; 85 | Memory_GetEmptyFunctionInstructions(iInstructions, bReturn); 86 | 87 | // TODO proper way to handle this 88 | 89 | MemoryBlock hEmptyFunction = new MemoryBlock(sizeof(iInstructions)); 90 | for (int i = 0; i < sizeof(iInstructions); i++) 91 | hEmptyFunction.StoreToOffset(i, iInstructions[i], NumberType_Int8); 92 | 93 | Address pAddress = hEmptyFunction.Address; 94 | hEmptyFunction.Disown(); 95 | delete hEmptyFunction; 96 | return pAddress; 97 | } 98 | 99 | bool Memory_IsEmptyFunction(Address pFunction, bool bReturn) 100 | { 101 | int iInstructions[8]; 102 | Memory_GetEmptyFunctionInstructions(iInstructions, bReturn); 103 | for (int i = 0; i < sizeof(iInstructions); i++) 104 | { 105 | if (LoadFromAddress(pFunction + view_as
(i), NumberType_Int8) != iInstructions[i]) 106 | return false; 107 | } 108 | 109 | return true; 110 | } 111 | 112 | static void Memory_GetEmptyFunctionInstructions(int iInstructions[8], bool bReturn) 113 | { 114 | int iCount = 0; 115 | if (bReturn) 116 | { 117 | // Set return value as 0 118 | iInstructions[iCount++] = 0x31; 119 | iInstructions[iCount++] = 0xC0; 120 | } 121 | 122 | // Return 123 | iInstructions[iCount++] = 0xC3; 124 | 125 | for (int i = iCount; i < sizeof(iInstructions); i++) 126 | iInstructions[i] = 0x90; // Fill the rest as skip 127 | } -------------------------------------------------------------------------------- /scripting/vscript/entity.sp: -------------------------------------------------------------------------------- 1 | static Handle g_hSDKGetScriptDesc; 2 | static Handle g_hSDKCallRegisterInstance; 3 | static Handle g_hSDKCallSetInstanceUniqeId; 4 | static Handle g_hSDKCallGenerateUniqueKey; 5 | 6 | static int g_iOffsetScriptScope = -1; 7 | static int g_iOffsetScriptInstance = -1; 8 | static int g_iOffsetScriptModelKeyValues = -1; 9 | 10 | /* 11 | CBaseEntity props for offsets 12 | 13 | string_t m_iszVScripts; 14 | string_t m_iszScriptThinkFunction; 15 | CScriptScope m_ScriptScope; 16 | HSCRIPT m_hScriptInstance; 17 | string_t m_iszScriptId; 18 | CScriptKeyValues *m_pScriptModelKeyValues; 19 | */ 20 | 21 | void Entity_LoadGamedata(GameData hGameData) 22 | { 23 | StartPrepSDKCall(SDKCall_Entity); 24 | PrepSDKCall_SetFromConf(hGameData, SDKConf_Virtual, "CBaseEntity::GetScriptDesc"); 25 | PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain); 26 | g_hSDKGetScriptDesc = EndPrepSDKCall(); 27 | if (!g_hSDKGetScriptDesc) 28 | LogError("Failed to create SDKCall: CBaseEntity::GetScriptDesc"); 29 | 30 | g_hSDKCallRegisterInstance = CreateSDKCall(hGameData, "IScriptVM", "RegisterInstance", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_CBaseEntity); 31 | g_hSDKCallSetInstanceUniqeId = CreateSDKCall(hGameData, "IScriptVM", "SetInstanceUniqeId", _, SDKType_PlainOldData, SDKType_String); 32 | g_hSDKCallGenerateUniqueKey = CreateSDKCall(hGameData, "IScriptVM", "GenerateUniqueKey", SDKType_Bool, SDKType_String, SDKType_String, SDKType_PlainOldData); 33 | } 34 | 35 | void Entity_LoadOffsets(int iEntity) 36 | { 37 | if (g_iOffsetScriptScope == -1) 38 | { 39 | // m_ScriptScope right below m_iszScriptThinkFunction 40 | g_iOffsetScriptScope = FindDataMapInfo(iEntity, "m_iszScriptThinkFunction"); 41 | if (g_iOffsetScriptScope == -1) 42 | ThrowError("Could not get offset for CBaseEntity::m_ScriptScope, file a bug report."); 43 | else 44 | g_iOffsetScriptScope += 4; 45 | } 46 | 47 | if (g_iOffsetScriptInstance == -1 || g_iOffsetScriptModelKeyValues == -1) 48 | { 49 | // m_hScriptInstance right above m_iszScriptId 50 | int iOffset = FindDataMapInfo(iEntity, "m_iszScriptId"); 51 | if (iOffset == -1) 52 | ThrowError("Could not get offset for CBaseEntity::m_hScriptInstance, file a bug report."); 53 | 54 | g_iOffsetScriptInstance = iOffset - 4; 55 | g_iOffsetScriptModelKeyValues = iOffset + 4; 56 | } 57 | } 58 | 59 | HSCRIPT Entity_GetScriptScope(int iEntity) 60 | { 61 | Entity_LoadOffsets(iEntity); 62 | return view_as(GetEntData(iEntity, g_iOffsetScriptScope)); 63 | } 64 | 65 | HSCRIPT Entity_GetScriptInstance(int iEntity) 66 | { 67 | Entity_LoadOffsets(iEntity); 68 | // Below exact same as CBaseEntity::GetScriptInstance 69 | 70 | HSCRIPT pScriptInstance = view_as(GetEntData(iEntity, g_iOffsetScriptInstance)); 71 | if (!pScriptInstance) 72 | { 73 | char sId[1024]; 74 | GetEntPropString(iEntity, Prop_Data, "m_iszScriptId", sId, sizeof(sId)); 75 | if (!sId[0]) 76 | { 77 | char sName[1024]; 78 | GetEntPropString(iEntity, Prop_Data, "m_iName", sName, sizeof(sName)); 79 | if (!sName[0]) 80 | GetEntityClassname(iEntity, sName, sizeof(sName)); 81 | 82 | SDKCall(g_hSDKCallGenerateUniqueKey, GetScriptVM(), sName, sId, sizeof(sId)); 83 | SetEntPropString(iEntity, Prop_Data, "m_iszScriptId", sId); 84 | } 85 | 86 | pScriptInstance = SDKCall(g_hSDKCallRegisterInstance, GetScriptVM(), Entity_GetScriptDesc(iEntity), iEntity); 87 | SetEntData(iEntity, g_iOffsetScriptInstance, pScriptInstance); 88 | SDKCall(g_hSDKCallSetInstanceUniqeId, GetScriptVM(), pScriptInstance, sId); 89 | } 90 | 91 | return pScriptInstance; 92 | } 93 | 94 | VScriptClass Entity_GetScriptDesc(int iEntity) 95 | { 96 | return SDKCall(g_hSDKGetScriptDesc, iEntity); 97 | } 98 | 99 | void Entity_Clear(int iEntity) 100 | { 101 | Entity_LoadOffsets(iEntity); 102 | SetEntData(iEntity, g_iOffsetScriptScope, INVALID_HSCRIPT); 103 | SetEntData(iEntity, g_iOffsetScriptInstance, Address_Null); 104 | SetEntData(iEntity, g_iOffsetScriptModelKeyValues, Address_Null); 105 | SetEntPropString(iEntity, Prop_Data, "m_iszScriptId", NULL_STRING); 106 | } 107 | -------------------------------------------------------------------------------- /scripting/vscript/util.sp: -------------------------------------------------------------------------------- 1 | HSCRIPT GetScriptVM() 2 | { 3 | return view_as(LoadFromAddress(g_pToScriptVM, NumberType_Int32)); 4 | } 5 | 6 | void SetScriptVM(HSCRIPT pScript) 7 | { 8 | StoreToAddress(g_pToScriptVM, pScript, NumberType_Int32); 9 | } 10 | 11 | int LoadPointerStringFromAddress(Address pPointer, char[] sBuffer, int iMaxLen) 12 | { 13 | Address pString = LoadFromAddress(pPointer, NumberType_Int32); 14 | return LoadStringFromAddress(pString, sBuffer, iMaxLen); 15 | } 16 | 17 | int LoadStringFromAddress(Address pString, char[] sBuffer, int iMaxLen) 18 | { 19 | int iChar; 20 | char sChar; 21 | 22 | do 23 | { 24 | sChar = view_as(LoadFromAddress(pString + view_as
(iChar), NumberType_Int8)); 25 | sBuffer[iChar] = sChar; 26 | } 27 | while (sChar && ++iChar < iMaxLen - 1); 28 | 29 | return iChar; 30 | } 31 | 32 | int LoadPointerStringLengthFromAddress(Address pPointer) 33 | { 34 | Address pString = LoadFromAddress(pPointer, NumberType_Int32); 35 | return LoadStringLengthFromAddress(pString); 36 | } 37 | 38 | int LoadStringLengthFromAddress(Address pString) 39 | { 40 | int iChar; 41 | char sChar; 42 | 43 | do 44 | { 45 | sChar = view_as(LoadFromAddress(pString + view_as
(iChar), NumberType_Int8)); 46 | iChar++; 47 | } 48 | while (sChar); 49 | 50 | return iChar; 51 | } 52 | 53 | Address GetEmptyString() 54 | { 55 | static Address pEmptyString; 56 | if (!pEmptyString) 57 | { 58 | MemoryBlock hMemory = new MemoryBlock(1); 59 | pEmptyString = hMemory.Address; 60 | hMemory.Disown(); 61 | delete hMemory; 62 | } 63 | 64 | return pEmptyString; 65 | } 66 | 67 | MemoryBlock CreateStringMemory(const char[] sBuffer) 68 | { 69 | int iLength = strlen(sBuffer); 70 | MemoryBlock hString = new MemoryBlock(iLength + 1); // plus 1 for null term 71 | for (int i = 0; i < iLength; i++) 72 | hString.StoreToOffset(i, sBuffer[i], NumberType_Int8); 73 | 74 | return hString; 75 | } 76 | 77 | void StoreNativePointerStringToAddress(Address pAddress, int iParam) 78 | { 79 | int iLength; 80 | GetNativeStringLength(iParam, iLength); 81 | iLength++; 82 | 83 | char[] sBuffer = new char[iLength]; 84 | GetNativeString(iParam, sBuffer, iLength); 85 | 86 | MemoryBlock hString = CreateStringMemory(sBuffer); 87 | Memory_SetAddress(pAddress, hString); 88 | } 89 | 90 | void LoadVectorFromAddress(Address pVector, float vecBuffer[3]) 91 | { 92 | for (int i = 0; i < sizeof(vecBuffer); i++) 93 | vecBuffer[i] = LoadFromAddress(pVector + view_as
(i * 4), NumberType_Int32); 94 | } 95 | 96 | MemoryBlock CreateVectorMemory(float vecBuffer[3]) 97 | { 98 | MemoryBlock hVector = new MemoryBlock(sizeof(vecBuffer) * 4); 99 | for (int i = 0; i < sizeof(vecBuffer); i++) 100 | hVector.StoreToOffset(i * 4, view_as(vecBuffer[i]), NumberType_Int32); 101 | 102 | return hVector; 103 | } 104 | 105 | Handle CreateSDKCall(GameData hGameData, const char[] sClass, const char[] sFunction, SDKType nReturn = SDKType_Unknown, SDKType nParam1 = SDKType_Unknown, SDKType nParam2 = SDKType_Unknown, SDKType nParam3 = SDKType_Unknown, SDKType nParam4 = SDKType_Unknown, SDKType nParam5 = SDKType_Unknown, SDKType nParam6 = SDKType_Unknown) 106 | { 107 | StartPrepSDKCall(SDKCall_Raw); 108 | PrepSDKCall_SetAddress(VTable_GetAddressFromName(hGameData, sClass, sFunction)); 109 | 110 | SDKAddParameter(nParam1); 111 | SDKAddParameter(nParam2); 112 | SDKAddParameter(nParam3); 113 | SDKAddParameter(nParam4); 114 | SDKAddParameter(nParam5); 115 | SDKAddParameter(nParam6); 116 | 117 | if (nReturn == SDKType_CBaseEntity || nReturn == SDKType_String) 118 | PrepSDKCall_SetReturnInfo(nReturn, SDKPass_Pointer); 119 | else if (nReturn != SDKType_Unknown) 120 | PrepSDKCall_SetReturnInfo(nReturn, SDKPass_Plain); 121 | 122 | Handle hSDKCall = EndPrepSDKCall(); 123 | if (!hSDKCall) 124 | LogError("Failed to create SDKCall: %s::%s", sClass, sFunction); 125 | 126 | return hSDKCall; 127 | } 128 | 129 | static void SDKAddParameter(SDKType nParam) 130 | { 131 | if (nParam == SDKType_Unknown) 132 | return; 133 | 134 | if (nParam == SDKType_CBaseEntity || nParam == SDKType_String) 135 | PrepSDKCall_AddParameter(nParam, SDKPass_Pointer, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWNOTINGAME|VDECODE_FLAG_ALLOWWORLD); // Don't want VENCODE_FLAG_COPYBACK here 136 | else 137 | PrepSDKCall_AddParameter(nParam, SDKPass_Plain); 138 | } 139 | -------------------------------------------------------------------------------- /scripting/vscript/field.sp: -------------------------------------------------------------------------------- 1 | enum SMField 2 | { 3 | SMField_Unknwon, 4 | SMField_Void, 5 | SMField_Cell, 6 | SMField_String, 7 | SMField_Vector, 8 | } 9 | 10 | const int FIELD_MAX = view_as(FIELD_UINT32) + 1; 11 | 12 | enum struct FieldInfo 13 | { 14 | char sName[64]; 15 | SMField nSMField; 16 | SDKType nSDKType; 17 | SDKPassMethod nSDKPassMethod; 18 | ReturnType nReturnType; 19 | HookParamType nHookParamType; 20 | int iSize; 21 | 22 | int iGameValue; 23 | } 24 | 25 | static FieldInfo g_FieldInfos[FIELD_MAX] = { 26 | { "FIELD_VOID", SMField_Void, SDKType_Unknown, SDKPass_Unknown, ReturnType_Void, HookParamType_Unknown, -1 }, 27 | { "FIELD_FLOAT", SMField_Cell, SDKType_Float, SDKPass_Plain, ReturnType_Float, HookParamType_Float, 4 }, 28 | { "FIELD_VECTOR", SMField_Vector, SDKType_Vector, SDKPass_ByValue, ReturnType_VectorPtr, HookParamType_Object, 12 }, 29 | { "FIELD_INTEGER", SMField_Cell, SDKType_PlainOldData, SDKPass_Plain, ReturnType_Int, HookParamType_Int, 4 }, 30 | { "FIELD_BOOLEAN", SMField_Cell, SDKType_Bool, SDKPass_Plain, ReturnType_Bool, HookParamType_Bool, 4 }, 31 | { "FIELD_TYPEUNKNOWN", SMField_Unknwon, SDKType_Unknown, SDKPass_Unknown, ReturnType_Unknown, HookParamType_Unknown, -1 }, 32 | { "FIELD_CSTRING", SMField_String, SDKType_String, SDKPass_Pointer, ReturnType_CharPtr, HookParamType_CharPtr, 4 }, 33 | { "FIELD_HSCRIPT", SMField_Cell, SDKType_PlainOldData, SDKPass_Plain, ReturnType_Int, HookParamType_Int, 4 }, 34 | { "FIELD_VARIANT", SMField_Unknwon, SDKType_Unknown, SDKPass_Unknown, ReturnType_Unknown, HookParamType_Unknown, -1 }, 35 | { "FIELD_QANGLE", SMField_Vector, SDKType_QAngle, SDKPass_ByValue, ReturnType_VectorPtr, HookParamType_Object, 12 }, 36 | { "FIELD_UINT32", SMField_Cell, SDKType_PlainOldData, SDKPass_Plain, ReturnType_Int, HookParamType_Int, 4 }, 37 | }; 38 | 39 | void Field_LoadGamedata(GameData hGameData) 40 | { 41 | for (int i = 0; i < FIELD_MAX; i++) 42 | { 43 | char sKeyValue[12]; 44 | hGameData.GetKeyValue(g_FieldInfos[i].sName, sKeyValue, sizeof(sKeyValue)); 45 | g_FieldInfos[i].iGameValue = StringToInt(sKeyValue); 46 | } 47 | } 48 | 49 | fieldtype_t Field_GameToEnum(int iField) 50 | { 51 | for (int i = 0; i < FIELD_MAX; i++) 52 | if (g_FieldInfos[i].iGameValue == iField) 53 | return view_as(i); 54 | 55 | ThrowError("Unknown field value '%d'", iField); 56 | return FIELD_VOID; 57 | } 58 | 59 | int Field_EnumToGame(fieldtype_t nField) 60 | { 61 | return g_FieldInfos[nField].iGameValue; 62 | } 63 | 64 | char[] Field_GetName(fieldtype_t nField) 65 | { 66 | return g_FieldInfos[nField].sName; 67 | } 68 | 69 | SMField Field_GetSMField(fieldtype_t nField) 70 | { 71 | if (g_FieldInfos[nField].nSMField != SMField_Unknwon) 72 | return g_FieldInfos[nField].nSMField; 73 | 74 | ThrowError("Invalid field type '%s'", Field_GetName(nField)); 75 | return SMField_Unknwon; 76 | } 77 | 78 | SDKType Field_GetSDKType(fieldtype_t nField) 79 | { 80 | if (g_FieldInfos[nField].nSDKType != SDKType_Unknown) 81 | return g_FieldInfos[nField].nSDKType; 82 | 83 | ThrowError("Invalid field type '%s' for SDKType", Field_GetName(nField)); 84 | return SDKType_Unknown; 85 | } 86 | 87 | SDKPassMethod Field_GetSDKPassMethod(fieldtype_t nField) 88 | { 89 | if (g_FieldInfos[nField].nSDKPassMethod != SDKPass_Unknown) 90 | return g_FieldInfos[nField].nSDKPassMethod; 91 | 92 | ThrowError("Invalid field type '%s' for SDKPassMethod", Field_GetName(nField)); 93 | return SDKPass_Unknown; 94 | } 95 | 96 | ReturnType Field_GetReturnType(fieldtype_t nField) 97 | { 98 | if (g_FieldInfos[nField].nReturnType != ReturnType_Unknown) 99 | return g_FieldInfos[nField].nReturnType; 100 | 101 | ThrowError("Invalid field type '%s' for ReturnType", Field_GetName(nField)); 102 | return ReturnType_Unknown; 103 | } 104 | 105 | HookParamType Field_GetParamType(fieldtype_t nField) 106 | { 107 | if (g_FieldInfos[nField].nHookParamType != HookParamType_Unknown) 108 | return g_FieldInfos[nField].nHookParamType; 109 | 110 | ThrowError("Invalid field type '%s' for HookParamType", Field_GetName(nField)); 111 | return HookParamType_Unknown; 112 | } 113 | 114 | int Field_GetSize(fieldtype_t nField) 115 | { 116 | if (g_FieldInfos[nField].iSize != -1) 117 | return g_FieldInfos[nField].iSize; 118 | 119 | ThrowError("Invalid field type '%s' for size", Field_GetName(nField)); 120 | return -1; 121 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VScript 2 | 3 | SourceMod plugin that exposes many VScript features to make use of it. 4 | 5 | Currently supports the following games: 6 | - Counter Strike: Source 7 | - Day of Defeat: Source 8 | - Half-Life 2: Deathmatch 9 | - Left 4 Dead 2 10 | - Team Fortress 2 11 | 12 | ## Builds 13 | All builds can be found in [releases](https://github.com/FortyTwoFortyTwo/VScript/releases) page, auto-built on every commits done in main branch. 14 | 15 | ## Requirements 16 | - At least SourceMod version 1.12.0.6924 17 | - [sourcescramble](https://forums.alliedmods.net/showthread.php?p=2657347) 18 | 19 | ## Features 20 | 21 | [vscript.inc](https://github.com/FortyTwoFortyTwo/VScript/blob/main/scripting/include/vscript.inc) and [vscript_test.sp](https://github.com/FortyTwoFortyTwo/VScript/blob/main/scripting/vscript_test.sp) should give enough documentation on how to make use of it, but below gives some basic examples on common features: 22 | 23 | #### Compiles and Executes a script 24 | 25 | Compiles and executes a script code with params and returns, helpful when `RunScriptCode` input does not support receiving returns. 26 | ```sp 27 | public void OnAllPluginsLoaded() 28 | { 29 | HSCRIPT script = VScript_CompileScript("printl(\"Wow a message!\"); return 4242; function PrintMessage(param) { printl(param) }"); 30 | 31 | VScriptExecute execute = new VScriptExecute(script); 32 | execute.Execute(); 33 | int ret = execute.ReturnValue; 34 | PrintToServer("%d", ret); // Expected to print 4242 35 | 36 | delete execute; 37 | 38 | // Call a PrintMessage function 39 | execute = new VScriptExecute(HSCRIPT_RootTable.GetValue("PrintMessage")); 40 | execute.SetParamString(1, FIELD_CSTRING, "Hello!"); 41 | execute.Execute(); 42 | 43 | delete execute; 44 | script.ReleaseScript(); 45 | } 46 | ``` 47 | 48 | #### SDKCall/DHook native function 49 | 50 | This allows to directly call or detour a function without needing to manually get gamedata signatures. Parameters and returns are automatically set to the handle. 51 | ```sp 52 | Handle g_SDKCallGetAngles; 53 | 54 | public void OnAllPluginsLoaded() 55 | { 56 | VScriptFunction func = VScript_GetClassFunction("CBaseEntity", "GetAngles"); 57 | g_SDKCallGetAngles = func.CreateSDKCall(); 58 | DynamicDetour detour = func.CreateDetour(); 59 | detour.Enable(Hook_Post, Detour_GetAngles); 60 | 61 | RegConsoleCmd("sm_getangles", Command_GetAngles); 62 | } 63 | 64 | Action Command_GetAngles(int client, int args) 65 | { 66 | float angles[3]; 67 | SDKCall(g_SDKCallGetAngles, client, angles); 68 | ReplyToCommand(client, "result: x = %.2f, y = %.2f, z = %.2f", angles[0], angles[1], angles[2]); 69 | return Plugin_Handled; 70 | } 71 | 72 | MRESReturn Detour_GetAngles(int entity, DHookReturn ret) 73 | { 74 | float angles[3]; 75 | ret.GetVector(angles); 76 | PrintToServer("entity %d angles: x = %.2f, y = %.2f, z = %.2f", entity, angles[0], angles[1], angles[2]); 77 | return MRES_Ignored; 78 | } 79 | ``` 80 | 81 | #### Create new native function 82 | 83 | Creates a new native function where scripts can make use of it. Does nothing by default but can use `VScriptFunction.CreateDetour` above to do actions and set return. 84 | ```sp 85 | VScriptFunction g_NewFunction; 86 | 87 | public void OnAllPluginsLoaded() 88 | { 89 | // Create a new function, or get an existing one if name already exists 90 | g_NewFunction = VScript_CreateGlobalFunction("NewFunction"); 91 | g_NewFunction.SetParam(1, FIELD_FLOAT); 92 | g_NewFunction.Return = FIELD_INTEGER; 93 | g_NewFunction.SetFunctionEmpty(); 94 | 95 | // If plugin were to be lateloaded and that script vm is already initialized, just manually call it. 96 | if (VScript_IsScriptVMInitialized()) 97 | VScript_OnScriptVMInitialized(); 98 | } 99 | 100 | public void VScript_OnScriptVMInitialized() 101 | { 102 | // Global function need to be registered everytime g_pScriptVM has been reset, which happens right before this forward 103 | g_NewFunction.Register(); 104 | } 105 | ``` 106 | 107 | #### VScript_EntityToHScript and VScript_HScriptToEntity 108 | 109 | VScript uses FIELD_HSCRIPT to interact with entities, so `VScript_EntityToHScript` and `VScript_HScriptToEntity` are helpful functions to convert between entity index and hscript object to manage with it. 110 | 111 | ## Known Issues 112 | 113 | In L4D2 linux, attempting to reset g_pScriptVM will eventually cause a crash. For now a plugin prevents any attempts to reset such, meaning that not everything may work properly until a mapchange occurs. -------------------------------------------------------------------------------- /scripting/vscript/list.sp: -------------------------------------------------------------------------------- 1 | static int g_iInitializing; 2 | static GlobalForward g_fOnScriptVMInitialized; 3 | 4 | static ArrayList g_aGlobalFunctions; 5 | static ArrayList g_aClasses; 6 | 7 | void List_LoadGamedata(GameData hGameData) 8 | { 9 | g_fOnScriptVMInitialized = new GlobalForward("VScript_OnScriptVMInitialized", ET_Ignore); 10 | 11 | DynamicDetour hDetour; 12 | 13 | hDetour = VTable_CreateDetour(hGameData, "IScriptVM", "Init", ReturnType_Bool); 14 | hDetour.Enable(Hook_Pre, List_Init); 15 | 16 | hDetour = VTable_CreateDetour(hGameData, "IScriptVM", "RegisterFunction", _, HookParamType_Int); 17 | hDetour.Enable(Hook_Post, List_RegisterFunction); 18 | 19 | hDetour = VTable_CreateDetour(hGameData, "IScriptVM", "RegisterClass", ReturnType_Bool, HookParamType_Int); 20 | hDetour.Enable(Hook_Post, List_RegisterClass); 21 | 22 | hDetour = VTable_CreateDetour(hGameData, "IGameSystem", "LevelInitPreEntity", ReturnType_Bool); 23 | hDetour.Enable(Hook_Post, List_LevelInitPreEntity); 24 | } 25 | 26 | void List_LoadDefaults() 27 | { 28 | g_aGlobalFunctions = new ArrayList(); 29 | g_aClasses = new ArrayList(); 30 | 31 | if (!g_bAllowResetScriptVM) 32 | return; 33 | 34 | HSCRIPT pScriptVM = GetScriptVM(); 35 | 36 | // Create new vscriptvm and set back, so we can collect all of the default stuffs 37 | g_iInitializing = -1; // Don't want to call forward from this 38 | SetScriptVM(view_as(Address_Null)); 39 | GameSystem_ServerInit(); 40 | GameSystem_ServerTerm(); 41 | SetScriptVM(pScriptVM); 42 | g_iInitializing = 0; 43 | 44 | int iEntity = INVALID_ENT_REFERENCE; 45 | while ((iEntity = FindEntityByClassname(iEntity, "*")) != INVALID_ENT_REFERENCE) 46 | List_AddEntityScriptDesc(iEntity); 47 | } 48 | 49 | MRESReturn List_Init(Address pScriptVM, DHookReturn hReturn) 50 | { 51 | if (g_iInitializing == 0) 52 | g_iInitializing = 1; 53 | 54 | g_aGlobalFunctions.Clear(); 55 | g_aClasses.Clear(); 56 | return MRES_Ignored; 57 | } 58 | 59 | MRESReturn List_RegisterFunction(Address pScriptVM, DHookParam hParam) 60 | { 61 | VScriptFunction pFunction = hParam.Get(1); 62 | if (g_aGlobalFunctions.FindValue(pFunction) == -1) 63 | g_aGlobalFunctions.Push(pFunction); 64 | 65 | return MRES_Ignored; 66 | } 67 | 68 | MRESReturn List_RegisterClass(Address pScriptVM, DHookReturn hReturn, DHookParam hParam) 69 | { 70 | if (hReturn.Value == false) 71 | return MRES_Ignored; 72 | 73 | VScriptClass pClass = hParam.Get(1); 74 | List_AddClass(pClass); 75 | 76 | return MRES_Ignored; 77 | } 78 | 79 | MRESReturn List_LevelInitPreEntity(Address pGameSystem, DHookReturn hReturn) 80 | { 81 | if (g_iInitializing == 1) 82 | { 83 | g_iInitializing = 0; 84 | Call_StartForward(g_fOnScriptVMInitialized); 85 | Call_Finish(); 86 | } 87 | 88 | return MRES_Ignored; 89 | } 90 | 91 | void List_AddEntityScriptDesc(int iEntity) 92 | { 93 | VScriptClass pClass = Entity_GetScriptDesc(iEntity); 94 | while (pClass) 95 | { 96 | if (g_aClasses.FindValue(pClass) != -1) 97 | return; 98 | 99 | g_aClasses.Push(pClass); 100 | pClass = Class_GetBaseDesc(pClass); 101 | } 102 | } 103 | 104 | void List_AddClass(VScriptClass pClass) 105 | { 106 | if (g_aClasses.FindValue(pClass) == -1) 107 | g_aClasses.Push(pClass); 108 | } 109 | 110 | ArrayList List_GetAllGlobalFunctions() 111 | { 112 | return g_aGlobalFunctions; 113 | } 114 | 115 | VScriptFunction List_GetFunction(const char[] sName) 116 | { 117 | for (int i = 0; i < g_aGlobalFunctions.Length; i++) 118 | { 119 | VScriptFunction pFunction = g_aGlobalFunctions.Get(i); 120 | 121 | char sScriptName[256]; 122 | Function_GetScriptName(pFunction, sScriptName, sizeof(sScriptName)); 123 | if (StrEqual(sScriptName, sName)) 124 | return pFunction; 125 | } 126 | 127 | return VScriptFunction_Invalid; 128 | } 129 | 130 | ArrayList List_GetAllClasses() 131 | { 132 | return g_aClasses; 133 | } 134 | 135 | VScriptClass List_GetClass(const char[] sName) 136 | { 137 | for (int i = 0; i < g_aClasses.Length; i++) 138 | { 139 | VScriptClass pClass = g_aClasses.Get(i); 140 | 141 | char sScriptName[256]; 142 | Class_GetScriptName(pClass, sScriptName, sizeof(sScriptName)); 143 | if (StrEqual(sScriptName, sName)) 144 | return pClass; 145 | } 146 | 147 | return VScriptClass_Invalid; 148 | } 149 | 150 | VScriptClass List_GetClassFromFunction(VScriptFunction pFunction) 151 | { 152 | for (int i = 0; i < g_aClasses.Length; i++) 153 | { 154 | VScriptClass pClass = g_aClasses.Get(i); 155 | 156 | int iFunctionCount = Class_GetFunctionCount(pClass); 157 | for (int j = 0; j < iFunctionCount; j++) 158 | if (Class_GetFunctionFromIndex(pClass, j) == pFunction) 159 | return pClass; 160 | } 161 | 162 | return VScriptClass_Invalid; 163 | } 164 | -------------------------------------------------------------------------------- /scripting/vscript/variant.sp: -------------------------------------------------------------------------------- 1 | 2 | #define SQOBJECT_REF_COUNTED 0x08000000 3 | #define SQOBJECT_NUMERIC 0x04000000 4 | #define SQOBJECT_DELEGABLE 0x02000000 5 | #define SQOBJECT_CANBEFALSE 0x01000000 6 | 7 | #define _RT_NULL 0x00000001 8 | #define _RT_INTEGER 0x00000002 9 | #define _RT_FLOAT 0x00000004 10 | #define _RT_BOOL 0x00000008 11 | #define _RT_STRING 0x00000010 12 | #define _RT_TABLE 0x00000020 13 | #define _RT_ARRAY 0x00000040 14 | #define _RT_USERDATA 0x00000080 15 | #define _RT_CLOSURE 0x00000100 16 | #define _RT_NATIVECLOSURE 0x00000200 17 | #define _RT_GENERATOR 0x00000400 18 | #define _RT_USERPOINTER 0x00000800 19 | #define _RT_THREAD 0x00001000 20 | #define _RT_FUNCPROTO 0x00002000 21 | #define _RT_CLASS 0x00004000 22 | #define _RT_INSTANCE 0x00008000 23 | #define _RT_WEAKREF 0x00010000 24 | 25 | enum SQObjectType 26 | { 27 | OT_NULL = (_RT_NULL|SQOBJECT_CANBEFALSE), 28 | OT_INTEGER = (_RT_INTEGER|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE), 29 | OT_FLOAT = (_RT_FLOAT|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE), 30 | OT_BOOL = (_RT_BOOL|SQOBJECT_CANBEFALSE), 31 | OT_STRING = (_RT_STRING|SQOBJECT_REF_COUNTED), 32 | OT_TABLE = (_RT_TABLE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE), 33 | OT_ARRAY = (_RT_ARRAY|SQOBJECT_REF_COUNTED), 34 | OT_USERDATA = (_RT_USERDATA|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE), 35 | OT_CLOSURE = (_RT_CLOSURE|SQOBJECT_REF_COUNTED), 36 | OT_NATIVECLOSURE = (_RT_NATIVECLOSURE|SQOBJECT_REF_COUNTED), 37 | OT_GENERATOR = (_RT_GENERATOR|SQOBJECT_REF_COUNTED), 38 | OT_USERPOINTER = _RT_USERPOINTER, 39 | OT_THREAD = (_RT_THREAD|SQOBJECT_REF_COUNTED) , 40 | OT_FUNCPROTO = (_RT_FUNCPROTO|SQOBJECT_REF_COUNTED), //internal usage only 41 | OT_CLASS = (_RT_CLASS|SQOBJECT_REF_COUNTED), 42 | OT_INSTANCE = (_RT_INSTANCE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE), 43 | OT_WEAKREF = (_RT_WEAKREF|SQOBJECT_REF_COUNTED) 44 | }; 45 | 46 | stock char[] Variant_GetObjectTypeName(SQObjectType nType) 47 | { 48 | char sValue[64]; 49 | 50 | switch (nType) 51 | { 52 | case OT_NULL: strcopy(sValue, sizeof(sValue), "null"); 53 | case OT_INTEGER: strcopy(sValue, sizeof(sValue), "integer"); 54 | case OT_FLOAT: strcopy(sValue, sizeof(sValue), "float"); 55 | case OT_BOOL: strcopy(sValue, sizeof(sValue), "bool"); 56 | case OT_STRING: strcopy(sValue, sizeof(sValue), "string"); 57 | case OT_TABLE: strcopy(sValue, sizeof(sValue), "table"); 58 | case OT_ARRAY: strcopy(sValue, sizeof(sValue), "array"); 59 | case OT_USERDATA: strcopy(sValue, sizeof(sValue), "userdata"); 60 | case OT_CLOSURE: strcopy(sValue, sizeof(sValue), "closure"); 61 | case OT_NATIVECLOSURE: strcopy(sValue, sizeof(sValue), "nativeclosure"); 62 | case OT_GENERATOR: strcopy(sValue, sizeof(sValue), "generator"); 63 | case OT_USERPOINTER: strcopy(sValue, sizeof(sValue), "userpointer"); 64 | case OT_THREAD: strcopy(sValue, sizeof(sValue), "thread"); 65 | case OT_FUNCPROTO: strcopy(sValue, sizeof(sValue), "funcproto"); 66 | case OT_CLASS: strcopy(sValue, sizeof(sValue), "class"); 67 | case OT_INSTANCE: strcopy(sValue, sizeof(sValue), "instance"); 68 | case OT_WEAKREF: strcopy(sValue, sizeof(sValue), "weakref"); 69 | default: Format(sValue, sizeof(sValue), "unknown [%d]", nType); 70 | } 71 | 72 | return sValue; 73 | } 74 | 75 | 76 | methodmap ScriptVariant_t < MemoryBlock 77 | { 78 | public ScriptVariant_t() 79 | { 80 | return view_as(new MemoryBlock(g_iScriptVariant_sizeof)); 81 | } 82 | 83 | property any nValue 84 | { 85 | public get() 86 | { 87 | return this.LoadFromOffset(g_iScriptVariant_union, NumberType_Int32); 88 | } 89 | 90 | public set(any nValue) 91 | { 92 | this.StoreToOffset(g_iScriptVariant_union, nValue, NumberType_Int32); 93 | } 94 | } 95 | 96 | public void GetString(char[] sBuffer, int iLength) 97 | { 98 | LoadPointerStringFromAddress(this.Address + view_as
(g_iScriptVariant_union), sBuffer, iLength); 99 | } 100 | 101 | public int GetStringLength() 102 | { 103 | return LoadPointerStringLengthFromAddress(this.Address + view_as
(g_iScriptVariant_union)); 104 | } 105 | 106 | public void GetVector(float vecBuffer[3]) 107 | { 108 | LoadVectorFromAddress(this.nValue, vecBuffer); 109 | } 110 | 111 | property fieldtype_t nType 112 | { 113 | public get() 114 | { 115 | return Field_GameToEnum(this.LoadFromOffset(g_iScriptVariant_type, NumberType_Int16)); 116 | } 117 | 118 | public set(fieldtype_t nField) 119 | { 120 | this.StoreToOffset(g_iScriptVariant_type, Field_EnumToGame(nField), NumberType_Int16); 121 | } 122 | } 123 | 124 | property SQObjectType ObjectType 125 | { 126 | public get() 127 | { 128 | if (this.nType != FIELD_HSCRIPT) 129 | ThrowError("Field must be FIELD_HSCRIPT"); 130 | 131 | return LoadFromAddress(this.nValue + view_as
(0), NumberType_Int32); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /scripting/vscript/binding.sp: -------------------------------------------------------------------------------- 1 | enum struct BindingInfo 2 | { 3 | VScriptFunction pFunction; 4 | Address pAddress; 5 | Handle hSDKCall; 6 | } 7 | 8 | static ArrayList g_aBindingInfos; 9 | static Address g_pCustomBinding; 10 | 11 | void Binding_Init() 12 | { 13 | g_aBindingInfos = new ArrayList(sizeof(BindingInfo)); 14 | g_pCustomBinding = Memory_CreateEmptyFunction(true); 15 | 16 | DynamicDetour hDetour = new DynamicDetour(g_pCustomBinding, CallConv_CDECL, ReturnType_Bool, ThisPointer_Ignore); 17 | hDetour.AddParam(HookParamType_Int, g_iScriptFunctionBinding_sizeof); // pFunction 18 | hDetour.AddParam(HookParamType_Int); // pContext 19 | hDetour.AddParam(HookParamType_Int); // pArguments 20 | hDetour.AddParam(HookParamType_Int); // nArguments 21 | hDetour.AddParam(HookParamType_Int); // pReturn 22 | 23 | hDetour.Enable(Hook_Pre, Binding_Detour); 24 | 25 | // Find all existing functions with empty binding to rehook it 26 | Binding_CheckFunctions(List_GetAllGlobalFunctions()); 27 | 28 | ArrayList aClasses = List_GetAllClasses(); 29 | int iLength = aClasses.Length; 30 | for (int i = 0; i < iLength; i++) 31 | { 32 | ArrayList aFunctions = Class_GetAllFunctions(aClasses.Get(i)); 33 | Binding_CheckFunctions(aFunctions); 34 | delete aFunctions; 35 | } 36 | } 37 | 38 | void Binding_CheckFunctions(ArrayList aList) 39 | { 40 | int iLength = aList.Length; 41 | for (int i = 0; i < iLength; i++) 42 | { 43 | VScriptFunction pFunction = aList.Get(i); 44 | if (Memory_IsEmptyFunction(Function_GetBinding(pFunction), true)) 45 | Binding_SetCustom(pFunction); 46 | } 47 | } 48 | 49 | void Binding_SetCustom(VScriptFunction pFunction) 50 | { 51 | Binding_Delete(pFunction); 52 | 53 | BindingInfo info; 54 | info.pFunction = pFunction; 55 | info.pAddress = Function_GetFunction(pFunction); 56 | 57 | info.hSDKCall = Function_CreateSDKCall(pFunction, false, true); 58 | if (!info.hSDKCall) 59 | ThrowError("Unable to create SDKCall from binding detour, file a bug report."); 60 | 61 | g_aBindingInfos.PushArray(info); 62 | Function_SetBinding(pFunction, g_pCustomBinding); 63 | } 64 | 65 | bool Binding_Delete(VScriptFunction pFunction) 66 | { 67 | int iIndex = g_aBindingInfos.FindValue(pFunction, BindingInfo::pFunction); 68 | if (iIndex == -1) 69 | return false; 70 | 71 | BindingInfo info; 72 | g_aBindingInfos.GetArray(iIndex, info); 73 | delete info.hSDKCall; 74 | g_aBindingInfos.Erase(iIndex); 75 | return true; 76 | } 77 | 78 | public MRESReturn Binding_Detour(DHookReturn hReturn, DHookParam hParam) 79 | { 80 | Address pFunction = hParam.Get(1); 81 | Address pMember = hParam.Get(2); 82 | Address pArguments = hParam.Get(3); 83 | int iArguments = hParam.Get(4); 84 | Address pReturn = hParam.Get(5); 85 | 86 | int iIndex = g_aBindingInfos.FindValue(pFunction, BindingInfo::pAddress); 87 | if (iIndex == -1) 88 | ThrowError("Could not find binding info from function '%08X'", pFunction); 89 | 90 | BindingInfo info; 91 | g_aBindingInfos.GetArray(iIndex, info); 92 | 93 | // Figure out how big the array need to be 94 | int iMaxSize = 1; 95 | for (int i = 0; i < iArguments; i++) 96 | { 97 | int iSize; 98 | 99 | switch (Field_GetSMField(Function_GetParam(info.pFunction, i))) 100 | { 101 | case SMField_Cell: 102 | { 103 | iSize = 1; 104 | } 105 | case SMField_String: 106 | { 107 | Address pString = LoadFromAddress(pArguments + view_as
(i * g_iScriptVariant_sizeof + g_iScriptVariant_union), NumberType_Int32); 108 | iSize = LoadStringLengthFromAddress(pString); 109 | } 110 | case SMField_Vector: 111 | { 112 | iSize = 3; 113 | } 114 | } 115 | 116 | if (iMaxSize < iSize) 117 | iMaxSize = iSize; 118 | } 119 | 120 | any[][] a = new any[16][iMaxSize]; // VScript allows a max of 14 params 121 | int iCount; 122 | 123 | if (pMember) 124 | a[iCount++][0] = pMember; 125 | 126 | for (int i = 0; i < iArguments; i++) 127 | { 128 | any nValue = LoadFromAddress(pArguments + view_as
(i * g_iScriptVariant_sizeof + g_iScriptVariant_union), NumberType_Int32); 129 | 130 | switch (Field_GetSMField(Function_GetParam(info.pFunction, i))) 131 | { 132 | case SMField_Cell: 133 | { 134 | a[iCount][0] = nValue; 135 | } 136 | case SMField_String: 137 | { 138 | LoadStringFromAddress(nValue, view_as(a[iCount]), iMaxSize); 139 | } 140 | case SMField_Vector: 141 | { 142 | for (int j = 0; j < 3; j++) 143 | a[iCount][j] = LoadFromAddress(nValue + (j * 4), NumberType_Int32); 144 | } 145 | } 146 | 147 | iCount++; 148 | } 149 | 150 | fieldtype_t nField = Function_GetReturnType(info.pFunction); 151 | 152 | // No other simple way to do it /shrug 153 | any nResult = SDKCall(info.hSDKCall, a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13],a[14],a[15]); 154 | 155 | if (nField == FIELD_FLOAT) 156 | { 157 | if (nResult == 0xFFC00000) 158 | { 159 | // from SetFunctionEmpty returning "0", return null instead 160 | nField = FIELD_VOID; 161 | nResult = 0; 162 | } 163 | } 164 | else if (Field_GetSMField(nField) == SMField_Vector) 165 | { 166 | if (nResult == 0) 167 | { 168 | // null vector 169 | nField = FIELD_VOID; 170 | nResult = 0; 171 | } 172 | else 173 | { 174 | // Prevent memory crash by creating new memory 175 | float vecResult[3]; 176 | LoadVectorFromAddress(nResult, vecResult); 177 | MemoryBlock hVector = CreateVectorMemory(vecResult); 178 | nResult = hVector.Address; 179 | hVector.Disown(); 180 | delete hVector; 181 | } 182 | } 183 | 184 | if (pReturn) 185 | { 186 | StoreToAddress(pReturn + view_as
(g_iScriptVariant_type), Field_EnumToGame(nField), NumberType_Int16); 187 | StoreToAddress(pReturn + view_as
(g_iScriptVariant_union), nResult, NumberType_Int32); 188 | } 189 | 190 | hReturn.Value = true; 191 | return MRES_Supercede; 192 | } 193 | -------------------------------------------------------------------------------- /scripting/vscript/class.sp: -------------------------------------------------------------------------------- 1 | static int g_iClassDesc_ScriptName; 2 | static int g_iClassDesc_ClassName; 3 | static int g_iClassDesc_Description; 4 | static int g_iClassDesc_BaseDesc; 5 | static int g_iClassDesc_FunctionBindings; 6 | static int g_iClassDesc_NextDesc; 7 | static int g_iClassDesc_sizeof; 8 | 9 | static int g_iFunctionBinding_sizeof; 10 | 11 | void Class_LoadGamedata(GameData hGameData) 12 | { 13 | g_iClassDesc_ScriptName = hGameData.GetOffset("ScriptClassDesc_t::m_pszScriptName"); 14 | g_iClassDesc_ClassName = hGameData.GetOffset("ScriptClassDesc_t::m_pszClassname"); 15 | g_iClassDesc_Description = hGameData.GetOffset("ScriptClassDesc_t::m_pszDescription"); 16 | g_iClassDesc_BaseDesc = hGameData.GetOffset("ScriptClassDesc_t::m_pBaseDesc"); 17 | g_iClassDesc_FunctionBindings = hGameData.GetOffset("ScriptClassDesc_t::m_FunctionBindings"); 18 | g_iClassDesc_NextDesc = hGameData.GetOffset("ScriptClassDesc_t::m_pNextDesc"); 19 | g_iClassDesc_sizeof = hGameData.GetOffset("sizeof(ScriptClassDesc_t)"); 20 | g_iFunctionBinding_sizeof = hGameData.GetOffset("sizeof(ScriptFunctionBinding_t)"); 21 | } 22 | 23 | VScriptClass Class_Create() 24 | { 25 | // TODO proper way to handle with memory? 26 | 27 | MemoryBlock hClass = new MemoryBlock(g_iClassDesc_sizeof); 28 | 29 | VScriptClass pClass = view_as(hClass.Address); 30 | 31 | hClass.Disown(); 32 | delete hClass; 33 | 34 | List_AddClass(pClass); 35 | return pClass; 36 | } 37 | 38 | void Class_Init(VScriptClass pClass) 39 | { 40 | for (int i = 0; i < g_iClassDesc_sizeof; i++) // Make sure that all is cleared first 41 | StoreToAddress(pClass + view_as
(i), 0, NumberType_Int8); 42 | 43 | // Set strings as empty, but not null 44 | Address pEmptyString = GetEmptyString(); 45 | StoreToAddress(pClass + view_as
(g_iClassDesc_ScriptName), pEmptyString, NumberType_Int32); 46 | StoreToAddress(pClass + view_as
(g_iClassDesc_ClassName), pEmptyString, NumberType_Int32); 47 | StoreToAddress(pClass + view_as
(g_iClassDesc_Description), pEmptyString, NumberType_Int32); 48 | 49 | // Add to the list for m_pNextDesc to register all. 50 | // Correct way to do this is to fetch ScriptClassDesc_t::GetDescList and update function's retrun. 51 | // But we can instead just look through existing list and update the last class in list to point at this class instead. 52 | 53 | VScriptClass pOther = List_GetAllClasses().Get(0); 54 | VScriptClass pNext = pOther; 55 | 56 | do 57 | { 58 | pOther = pNext; 59 | pNext = LoadFromAddress(pOther + view_as
(g_iClassDesc_NextDesc), NumberType_Int32); 60 | } 61 | while (pNext); 62 | 63 | StoreToAddress(pOther + view_as
(g_iClassDesc_NextDesc), pClass, NumberType_Int32); 64 | } 65 | 66 | void Class_GetScriptName(VScriptClass pClass, char[] sBuffer, int iLength) 67 | { 68 | LoadPointerStringFromAddress(pClass + view_as
(g_iClassDesc_ScriptName), sBuffer, iLength); 69 | } 70 | 71 | void Class_SetScriptName(VScriptClass pClass, int iParam) 72 | { 73 | StoreNativePointerStringToAddress(pClass + view_as
(g_iClassDesc_ScriptName), iParam); 74 | } 75 | 76 | void Class_GetClassName(VScriptClass pClass, char[] sBuffer, int iLength) 77 | { 78 | LoadPointerStringFromAddress(pClass + view_as
(g_iClassDesc_ClassName), sBuffer, iLength); 79 | } 80 | 81 | void Class_SetClassName(VScriptClass pClass, int iParam) 82 | { 83 | StoreNativePointerStringToAddress(pClass + view_as
(g_iClassDesc_ClassName), iParam); 84 | } 85 | 86 | void Class_GetDescription(VScriptClass pClass, char[] sBuffer, int iLength) 87 | { 88 | LoadPointerStringFromAddress(pClass + view_as
(g_iClassDesc_Description), sBuffer, iLength); 89 | } 90 | 91 | void Class_SetDescription(VScriptClass pClass, int iParam) 92 | { 93 | StoreNativePointerStringToAddress(pClass + view_as
(g_iClassDesc_Description), iParam); 94 | } 95 | 96 | ArrayList Class_GetAllFunctions(VScriptClass pClass) 97 | { 98 | ArrayList aList = new ArrayList(); 99 | 100 | Address pData = LoadFromAddress(pClass + view_as
(g_iClassDesc_FunctionBindings), NumberType_Int32); 101 | int iFunctionCount = LoadFromAddress(pClass + view_as
(g_iClassDesc_FunctionBindings) + view_as
(0x0C), NumberType_Int32); 102 | for (int i = 0; i < iFunctionCount; i++) 103 | { 104 | VScriptFunction pFunction = view_as(pData + view_as
(g_iFunctionBinding_sizeof * i)); 105 | aList.Push(pFunction); 106 | } 107 | 108 | return aList; 109 | } 110 | 111 | int Class_GetFunctionCount(VScriptClass pClass) 112 | { 113 | return LoadFromAddress(pClass + view_as
(g_iClassDesc_FunctionBindings) + view_as
(0x0C), NumberType_Int32); 114 | } 115 | 116 | VScriptFunction Class_GetFunctionFromIndex(VScriptClass pClass, int iIndex) 117 | { 118 | Address pData = LoadFromAddress(pClass + view_as
(g_iClassDesc_FunctionBindings), NumberType_Int32); 119 | return view_as(pData + view_as
(g_iFunctionBinding_sizeof * iIndex)); 120 | } 121 | 122 | VScriptFunction Class_GetFunctionFromName(VScriptClass pClass, const char[] sName) 123 | { 124 | Address pData = LoadFromAddress(pClass + view_as
(g_iClassDesc_FunctionBindings), NumberType_Int32); 125 | int iFunctionCount = Class_GetFunctionCount(pClass); 126 | for (int i = 0; i < iFunctionCount; i++) 127 | { 128 | VScriptFunction pFunction = view_as(pData + view_as
(g_iFunctionBinding_sizeof * i)); 129 | 130 | char sFunctionName[256]; 131 | Function_GetScriptName(pFunction, sFunctionName, sizeof(sFunctionName)); 132 | if (StrEqual(sFunctionName, sName)) 133 | return pFunction; 134 | } 135 | 136 | return VScriptFunction_Invalid; 137 | } 138 | 139 | VScriptFunction Class_CreateFunction(VScriptClass pClass) 140 | { 141 | int iFunctionCount = Class_GetFunctionCount(pClass); 142 | 143 | // Clear any binding detours we have first before moving over to new memory 144 | ArrayList aList = new ArrayList(); 145 | for (int i = 0; i < iFunctionCount; i++) 146 | if (Binding_Delete(Class_GetFunctionFromIndex(pClass, i))) 147 | aList.Push(i); 148 | 149 | Address pFunctionBindings = pClass + view_as
(g_iClassDesc_FunctionBindings); 150 | Memory_UtlVectorSetSize(pFunctionBindings, g_iFunctionBinding_sizeof, iFunctionCount + 1); 151 | 152 | // Re-detour any bindings 153 | for (int i = 0; i < aList.Length; i++) 154 | Binding_SetCustom(Class_GetFunctionFromIndex(pClass, aList.Get(i))); 155 | 156 | delete aList; 157 | 158 | VScriptFunction pFunction = Class_GetFunctionFromIndex(pClass, iFunctionCount); 159 | Function_Init(pFunction, true); 160 | return pFunction; 161 | } 162 | 163 | VScriptClass Class_GetBaseDesc(VScriptClass pClass) 164 | { 165 | return LoadFromAddress(pClass + view_as
(g_iClassDesc_BaseDesc), NumberType_Int32); 166 | } 167 | 168 | bool Class_IsDerivedFrom(VScriptClass pClass, VScriptClass pBase) 169 | { 170 | // Dunno why game would not allow this, but we can allow it 171 | if (pClass == pBase) 172 | return true; 173 | 174 | // CSquirrelVM::IsClassDerivedFrom 175 | VScriptClass pType = Class_GetBaseDesc(pClass); 176 | while (pType) 177 | { 178 | if (pType == pBase) 179 | return true; 180 | 181 | pType = Class_GetBaseDesc(pType); 182 | } 183 | 184 | return false; 185 | } 186 | -------------------------------------------------------------------------------- /gamedata/vscript.txt: -------------------------------------------------------------------------------- 1 | "Games" 2 | { 3 | "#default" 4 | { 5 | "Keys" 6 | { 7 | "OS" 8 | { 9 | "linux" "linux" 10 | "windows" "windows" 11 | } 12 | 13 | "IGameSystem" "CVScriptGameSystem" 14 | "IScriptVM" "CSquirrelVM" 15 | 16 | // Extra virtual functions that exists for each games 17 | "IGameSystem::ExtraOffsets" "" 18 | "IScriptVM::ExtraOffsets" "" 19 | 20 | "AllowResetScriptVM" "1" 21 | 22 | "FIELD_VOID" "0" 23 | "FIELD_FLOAT" "1" 24 | "FIELD_VECTOR" "3" 25 | "FIELD_INTEGER" "5" 26 | "FIELD_BOOLEAN" "6" 27 | "FIELD_TYPEUNKNOWN" "29" // Normally shouldn't be allowed, but some functions have it 28 | "FIELD_CSTRING" "30" 29 | "FIELD_HSCRIPT" "31" 30 | "FIELD_VARIANT" "32" 31 | "FIELD_UINT32" "37" 32 | "FIELD_QANGLE" "39" 33 | } 34 | 35 | "Offsets" 36 | { 37 | // All offsets are based on any game with smallest offset numbers, of which a large amount of it is based from sdk2013 38 | 39 | "ScriptClassDesc_t::m_pszScriptName" 40 | { 41 | "linux" "0" 42 | "windows" "0" 43 | } 44 | 45 | "ScriptClassDesc_t::m_pszClassname" 46 | { 47 | "linux" "4" 48 | "windows" "4" 49 | } 50 | 51 | "ScriptClassDesc_t::m_pszDescription" 52 | { 53 | "linux" "8" 54 | "windows" "8" 55 | } 56 | 57 | "ScriptClassDesc_t::m_pBaseDesc" 58 | { 59 | "linux" "12" 60 | "windows" "12" 61 | } 62 | 63 | "ScriptClassDesc_t::m_FunctionBindings" 64 | { 65 | "linux" "16" 66 | "windows" "16" 67 | } 68 | 69 | "ScriptClassDesc_t::m_pNextDesc" 70 | { 71 | "linux" "48" 72 | "windows" "48" 73 | } 74 | 75 | "sizeof(ScriptClassDesc_t)" 76 | { 77 | "linux" "52" 78 | "windows" "52" 79 | } 80 | 81 | "ScriptFunctionBinding_t::m_pszScriptName" 82 | { 83 | "linux" "0" 84 | "windows" "0" 85 | } 86 | 87 | "ScriptFunctionBinding_t::m_pszFunction" 88 | { 89 | "linux" "4" 90 | "windows" "4" 91 | } 92 | 93 | "ScriptFunctionBinding_t::m_pszDescription" 94 | { 95 | "linux" "8" 96 | "windows" "8" 97 | } 98 | 99 | "ScriptFunctionBinding_t::m_ReturnType" 100 | { 101 | "linux" "12" 102 | "windows" "12" 103 | } 104 | 105 | "ScriptFunctionBinding_t::m_Parameters" 106 | { 107 | "linux" "16" 108 | "windows" "16" 109 | } 110 | 111 | "ScriptFunctionBinding_t::m_pfnBinding" 112 | { 113 | "linux" "36" 114 | "windows" "36" 115 | } 116 | 117 | "ScriptFunctionBinding_t::m_pFunction" 118 | { 119 | "linux" "40" 120 | "windows" "40" 121 | } 122 | 123 | "ScriptFunctionBinding_t::m_flags" 124 | { 125 | "linux" "44" 126 | "windows" "44" 127 | } 128 | 129 | "sizeof(ScriptFunctionBinding_t)" 130 | { 131 | "linux" "48" 132 | "windows" "48" 133 | } 134 | 135 | "ScriptVariant_t::union" 136 | { 137 | "linux" "0" 138 | "windows" "0" 139 | } 140 | 141 | "ScriptVariant_t::m_type" 142 | { 143 | "linux" "8" 144 | "windows" "8" 145 | } 146 | 147 | "sizeof(ScriptVariant_t)" 148 | { 149 | "linux" "12" 150 | "windows" "16" 151 | } 152 | 153 | "sizeof(ScriptFunctionBindingStorageType_t)" 154 | { 155 | "linux" "4" 156 | "windows" "4" 157 | } 158 | 159 | "CBaseEntity::GetScriptDesc" 160 | { 161 | "linux" "13" 162 | "windows" "12" 163 | } 164 | 165 | "IGameSystem::LevelInitPreEntity" 166 | { 167 | "linux" "4" 168 | "windows" "4" 169 | } 170 | 171 | "IGameSystem::LevelShutdownPostEntity" 172 | { 173 | "linux" "7" 174 | "windows" "7" 175 | } 176 | 177 | "IGameSystem::FrameUpdatePostEntityThink" 178 | { 179 | "linux" "15" 180 | "windows" "14" 181 | } 182 | 183 | "CVScriptGameSystem::m_bAllowEntityCreationInScripts" 184 | { 185 | "linux" "12" 186 | "windows" "12" 187 | } 188 | 189 | "IScriptVM::Init" 190 | { 191 | "linux" "0" 192 | "windows" "0" 193 | } 194 | 195 | "IScriptVM::CompileScript" // vtable dumper got it wrong 196 | { 197 | "linux" "9" 198 | "windows" "11" 199 | } 200 | 201 | "IScriptVM::ReleaseScript" 202 | { 203 | "linux" "10" 204 | "windows" "12" 205 | } 206 | 207 | "IScriptVM::CreateScope" 208 | { 209 | "linux" "13" 210 | "windows" "13" 211 | } 212 | 213 | "IScriptVM::ReleaseScope" 214 | { 215 | "linux" "15" 216 | "windows" "15" 217 | } 218 | 219 | "IScriptVM::ExecuteFunction" 220 | { 221 | "linux" "18" 222 | "windows" "18" 223 | } 224 | 225 | "IScriptVM::RegisterFunction" 226 | { 227 | "linux" "19" 228 | "windows" "19" 229 | } 230 | 231 | "IScriptVM::RegisterClass" 232 | { 233 | "linux" "20" 234 | "windows" "20" 235 | } 236 | 237 | "IScriptVM::RegisterInstance" 238 | { 239 | "linux" "21" 240 | "windows" "21" 241 | } 242 | 243 | "IScriptVM::SetInstanceUniqeId" 244 | { 245 | "linux" "22" 246 | "windows" "22" 247 | } 248 | 249 | "IScriptVM::GetInstanceValue" 250 | { 251 | "linux" "24" 252 | "windows" "24" 253 | } 254 | 255 | "IScriptVM::GenerateUniqueKey" 256 | { 257 | "linux" "25" 258 | "windows" "25" 259 | } 260 | 261 | "IScriptVM::SetValue" 262 | { 263 | "linux" "28" 264 | "windows" "27" 265 | } 266 | 267 | "IScriptVM::CreateTable" 268 | { 269 | "linux" "29" 270 | "windows" "29" 271 | } 272 | 273 | "IScriptVM::GetKeyValue" 274 | { 275 | "linux" "31" 276 | "windows" "31" 277 | } 278 | 279 | "IScriptVM::GetValue" 280 | { 281 | "linux" "32" 282 | "windows" "32" 283 | } 284 | 285 | "IScriptVM::ReleaseValue" 286 | { 287 | "linux" "33" 288 | "windows" "33" 289 | } 290 | 291 | "IScriptVM::ClearValue" 292 | { 293 | "linux" "34" 294 | "windows" "34" 295 | } 296 | } 297 | } 298 | 299 | "#default" 300 | { 301 | "#supported" 302 | { 303 | "game" "cstrike" // Counter Strike: Source 304 | "game" "dod" // Day of Defeat: Source 305 | "game" "hl2mp" // Half-Life 2: Deathmatch 306 | "game" "tf" // Team Fortress 2 307 | } 308 | 309 | "Keys" 310 | { 311 | // 6 - IGameSystem::LevelShutdownPreClearSteamAPIContext 312 | "IGameSystem::ExtraOffsets" "6" 313 | } 314 | 315 | "Offsets" 316 | { 317 | // These games have m_pFunction sized 16 bytes, giving 12 extra bytes 318 | 319 | "ScriptFunctionBinding_t::m_flags" 320 | { 321 | "linux" "56" 322 | "windows" "56" 323 | } 324 | 325 | "sizeof(ScriptFunctionBinding_t)" 326 | { 327 | "linux" "60" 328 | "windows" "60" 329 | } 330 | 331 | "sizeof(ScriptFunctionBindingStorageType_t)" 332 | { 333 | "linux" "16" 334 | "windows" "16" 335 | } 336 | } 337 | } 338 | 339 | 340 | "left4dead2" 341 | { 342 | // Left 4 Dead 2 343 | 344 | "Keys" 345 | { 346 | // 6 - IScriptVM::GetInternalVM 347 | // 8 - IScriptVM::ForwardConsoleCommand 348 | //31/29- IScriptVM::SetValue (integer version) 349 | // 33 - IScriptVM::IsTable 350 | //37/36- IScriptVM::GetValue (integer version) 351 | // 38 - IScriptVM::GetScalarValue 352 | // There are more after 41, but none of our offsets go beyond that yet 353 | "IScriptVM::ExtraOffsets" 354 | { 355 | "linux" "6 8 31 33 37 38" 356 | "windows" "6 8 29 33 36 38" 357 | } 358 | 359 | // resetting g_pScriptVM in linux causes crash 360 | 361 | "AllowResetScriptVM" 362 | { 363 | "linux" "0" 364 | } 365 | 366 | "FIELD_TYPEUNKNOWN" "30" // +1 367 | "FIELD_CSTRING" "31" // +1 368 | "FIELD_HSCRIPT" "32" // +1 369 | "FIELD_VARIANT" "33" // +1 370 | "FIELD_UINT32" "38" // +1 371 | "FIELD_QANGLE" "40" // +1 372 | } 373 | } 374 | } -------------------------------------------------------------------------------- /scripting/vscript/execute.sp: -------------------------------------------------------------------------------- 1 | // VScriptExecute is an ArrayList, with index 0 as Execute, index 1 to arg count as ExecuteParam, and rest above as value of string 2 | 3 | enum struct ExecuteParam 4 | { 5 | fieldtype_t nType; 6 | any nValue; // int 7 | float vecValue[3]; // vector 8 | int iStringCount; // Amount of array indexs used to store string value 9 | } 10 | 11 | enum struct Execute 12 | { 13 | ExecuteParam nReturn; // must be first in this struct 14 | HSCRIPT pHScript; 15 | HSCRIPT hScope; 16 | int iNumParams; 17 | } 18 | 19 | static Handle g_hSDKCallExecuteFunction; 20 | 21 | void Execute_LoadGamedata(GameData hGameData) 22 | { 23 | g_hSDKCallExecuteFunction = CreateSDKCall(hGameData, "IScriptVM", "ExecuteFunction", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_Bool); 24 | } 25 | 26 | VScriptExecute Execute_Create(HSCRIPT pHScript, HSCRIPT hScope) 27 | { 28 | ArrayList aExecute = new ArrayList(sizeof(Execute)); 29 | 30 | Execute execute; 31 | execute.pHScript = pHScript; 32 | execute.hScope = hScope; 33 | aExecute.PushArray(execute); 34 | return view_as(aExecute); 35 | } 36 | 37 | static void Execute_InsertAt(VScriptExecute aExecute, int iParam, any[] nValues) 38 | { 39 | int iLength = view_as(aExecute).Length; 40 | if (iLength == iParam) 41 | { 42 | view_as(aExecute).PushArray(nValues); 43 | } 44 | else 45 | { 46 | view_as(aExecute).ShiftUp(iParam); 47 | view_as(aExecute).SetArray(iParam, nValues); 48 | } 49 | } 50 | 51 | void Execute_GetInfo(VScriptExecute aExecute, Execute execute) 52 | { 53 | view_as(aExecute).GetArray(0, execute); 54 | } 55 | 56 | static void Execute_SetInfo(VScriptExecute aExecute, Execute execute) 57 | { 58 | view_as(aExecute).SetArray(0, execute); 59 | } 60 | 61 | int Execute_GetParamCount(VScriptExecute aExecute) 62 | { 63 | return view_as(aExecute).Get(0, Execute::iNumParams); 64 | } 65 | 66 | static void Execute_SetParamCount(VScriptExecute aExecute, int iNumParams) 67 | { 68 | view_as(aExecute).Set(0, iNumParams, Execute::iNumParams); 69 | } 70 | 71 | int Execute_AddParam(VScriptExecute aExecute, ExecuteParam param) 72 | { 73 | int iNumParams = Execute_GetParamCount(aExecute) + 1; 74 | Execute_SetParamCount(aExecute, iNumParams); 75 | Execute_InsertAt(aExecute, iNumParams, param); 76 | return iNumParams; 77 | } 78 | 79 | void Execute_SetParam(VScriptExecute aExecute, int iParam, ExecuteParam param) 80 | { 81 | for (int i = Execute_GetParamCount(aExecute) + 1; i <= iParam; i++) 82 | { 83 | // Fill any new params between as void 84 | ExecuteParam nothing; 85 | Execute_SetParamCount(aExecute, i); 86 | Execute_InsertAt(aExecute, i, nothing); 87 | } 88 | 89 | Execute_ClearParamString(aExecute, iParam); 90 | view_as(aExecute).SetArray(iParam, param); 91 | } 92 | 93 | int Execute_GetParamStringCount(VScriptExecute aExecute, int iParam) 94 | { 95 | return view_as(aExecute).Get(iParam, ExecuteParam::iStringCount); 96 | } 97 | 98 | void Execute_GetParamString(VScriptExecute aExecute, int iParam, char[] sBuffer, int iLength) 99 | { 100 | int iStartingIndex = Execute_GetParamCount(aExecute) + 1; 101 | for (int i = 0; i < iParam; i++) 102 | iStartingIndex += Execute_GetParamStringCount(aExecute, i); 103 | 104 | int iStringCount = Execute_GetParamStringCount(aExecute, iParam); 105 | for (int i = iStartingIndex; i < iStartingIndex + iStringCount; i++) 106 | { 107 | char sValue[sizeof(Execute)]; 108 | view_as(aExecute).GetString(i, sValue, sizeof(sValue)); 109 | StrCat(sBuffer, iLength, sValue); 110 | } 111 | } 112 | 113 | int Execute_SetParamString(VScriptExecute aExecute, int iParam, const char[] sBuffer) 114 | { 115 | Execute_ClearParamString(aExecute, iParam); 116 | 117 | int iStartingIndex = Execute_GetParamCount(aExecute) + 1; 118 | for (int i = 0; i < iParam; i++) 119 | iStartingIndex += Execute_GetParamStringCount(aExecute, i); 120 | 121 | // How many indexes do we need? 122 | int iStringCount = RoundToCeil(float(strlen(sBuffer)) / float(sizeof(Execute) - 1)); 123 | view_as(aExecute).Set(iParam, iStringCount, ExecuteParam::iStringCount); 124 | 125 | for (int i = iStartingIndex; i < iStartingIndex + iStringCount; i++) 126 | { 127 | char sValue[sizeof(Execute)]; 128 | strcopy(sValue, sizeof(sValue), sBuffer[(i - iStartingIndex) * (sizeof(Execute) - 1)]); 129 | Execute_InsertAt(aExecute, i, view_as(sValue)); 130 | } 131 | 132 | return iStringCount; 133 | } 134 | 135 | void Execute_ClearParamString(VScriptExecute aExecute, int iParam) 136 | { 137 | int iStringCount = Execute_GetParamStringCount(aExecute, iParam); 138 | if (!iStringCount) 139 | return; 140 | 141 | int iStartingIndex = Execute_GetParamCount(aExecute) + 1; 142 | for (int i = 0; i < iParam; i++) 143 | iStartingIndex += Execute_GetParamStringCount(aExecute, i); 144 | 145 | for (int i = 0; i < iStringCount; i++) 146 | view_as(aExecute).Erase(iStartingIndex); 147 | 148 | view_as(aExecute).Set(iParam, 0, ExecuteParam::iStringCount); 149 | } 150 | 151 | ScriptStatus_t Execute_Execute(VScriptExecute aExecute) 152 | { 153 | MemoryBlock hArgs = null; 154 | ScriptVariant_t pReturn = new ScriptVariant_t(); 155 | 156 | Execute execute; 157 | Execute_GetInfo(aExecute, execute); 158 | 159 | int iNumParams = Execute_GetParamCount(aExecute); 160 | if (iNumParams) 161 | hArgs = new MemoryBlock(iNumParams * g_iScriptVariant_sizeof); 162 | 163 | MemoryBlock[] hValue = new MemoryBlock[iNumParams]; 164 | 165 | for (int iParam = 0; iParam < iNumParams; iParam++) 166 | { 167 | ExecuteParam param; 168 | view_as(aExecute).GetArray(iParam + 1, param, sizeof(param)); 169 | 170 | any nValue; 171 | 172 | switch (Field_GetSMField(param.nType)) 173 | { 174 | case SMField_Cell: 175 | { 176 | nValue = param.nValue; 177 | } 178 | case SMField_String: 179 | { 180 | int iLength = Execute_GetParamStringCount(aExecute, iParam + 1) * (sizeof(Execute) - 1) + 1; 181 | char[] sBuffer = new char[iLength]; 182 | Execute_GetParamString(aExecute, iParam + 1, sBuffer, iLength); 183 | 184 | hValue[iParam] = CreateStringMemory(sBuffer); 185 | nValue = hValue[iParam].Address; 186 | } 187 | case SMField_Vector: 188 | { 189 | hValue[iParam] = CreateVectorMemory(param.vecValue); 190 | nValue = hValue[iParam].Address; 191 | } 192 | } 193 | 194 | hArgs.StoreToOffset((iParam * g_iScriptVariant_sizeof) + g_iScriptVariant_type, Field_EnumToGame(param.nType), NumberType_Int16); 195 | hArgs.StoreToOffset((iParam * g_iScriptVariant_sizeof) + g_iScriptVariant_union, nValue, NumberType_Int32); 196 | } 197 | 198 | ScriptStatus_t nStatus = SDKCall(g_hSDKCallExecuteFunction, GetScriptVM(), execute.pHScript, hArgs ? hArgs.Address : Address_Null, iNumParams, pReturn.Address, execute.hScope, true); 199 | 200 | Execute_ClearParamString(aExecute, 0); // Clear previous return string value 201 | 202 | switch (Field_GetSMField(pReturn.nType)) 203 | { 204 | case SMField_Void: 205 | { 206 | execute.nReturn.nValue = 0; 207 | } 208 | case SMField_Cell: 209 | { 210 | execute.nReturn.nValue = pReturn.nValue; 211 | } 212 | case SMField_String: 213 | { 214 | int iLength = pReturn.GetStringLength(); 215 | char[] sBuffer = new char[iLength]; 216 | pReturn.GetString(sBuffer, iLength); 217 | 218 | execute.nReturn.iStringCount = Execute_SetParamString(aExecute, 0, sBuffer); 219 | } 220 | case SMField_Vector: 221 | { 222 | pReturn.GetVector(execute.nReturn.vecValue); 223 | } 224 | default: 225 | { 226 | execute.nReturn.nValue = 0; 227 | } 228 | } 229 | 230 | execute.nReturn.nType = pReturn.nType; 231 | Execute_SetInfo(aExecute, execute); 232 | 233 | delete hArgs; 234 | delete pReturn; 235 | 236 | for (int i = 0; i < iNumParams; i++) 237 | delete hValue[i]; 238 | 239 | return nStatus; 240 | } -------------------------------------------------------------------------------- /scripting/vscript/hscript.sp: -------------------------------------------------------------------------------- 1 | static Handle g_hSDKCallCreateScope; 2 | static Handle g_hSDKCallCreateTable; 3 | static Handle g_hSDKCallGetKeyValue; 4 | static Handle g_hSDKCallGetValue; 5 | static Handle g_hSDKCallSetValue; 6 | static Handle g_hSDKCallReleaseValue; 7 | static Handle g_hSDKCallClearValue; 8 | static Handle g_hSDKCallRegisterInstance; 9 | static Handle g_hSDKCallGetInstanceValue; 10 | static Handle g_hSDKCallReleaseScope; 11 | static Handle g_hSDKCallReleaseScript; 12 | 13 | void HScript_LoadGamedata(GameData hGameData) 14 | { 15 | g_hSDKCallCreateScope = CreateSDKCall(hGameData, "IScriptVM", "CreateScope", SDKType_PlainOldData, SDKType_String, SDKType_PlainOldData); 16 | g_hSDKCallCreateTable = CreateSDKCall(hGameData, "IScriptVM", "CreateTable", _, SDKType_PlainOldData); 17 | g_hSDKCallGetKeyValue = CreateSDKCall(hGameData, "IScriptVM", "GetKeyValue", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData); 18 | g_hSDKCallGetValue = CreateSDKCall(hGameData, "IScriptVM", "GetValue", SDKType_Bool, SDKType_PlainOldData, SDKType_String, SDKType_PlainOldData); 19 | g_hSDKCallSetValue = CreateSDKCall(hGameData, "IScriptVM", "SetValue", SDKType_Bool, SDKType_PlainOldData, SDKType_String, SDKType_PlainOldData); 20 | g_hSDKCallReleaseValue = CreateSDKCall(hGameData, "IScriptVM", "ReleaseValue", _, SDKType_PlainOldData); 21 | g_hSDKCallClearValue = CreateSDKCall(hGameData, "IScriptVM", "ClearValue", SDKType_Bool, SDKType_PlainOldData, SDKType_String); 22 | g_hSDKCallRegisterInstance = CreateSDKCall(hGameData, "IScriptVM", "RegisterInstance", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData); 23 | g_hSDKCallGetInstanceValue = CreateSDKCall(hGameData, "IScriptVM", "GetInstanceValue", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_PlainOldData); 24 | g_hSDKCallReleaseScope = CreateSDKCall(hGameData, "IScriptVM", "ReleaseScope", _, SDKType_PlainOldData); 25 | g_hSDKCallReleaseScript = CreateSDKCall(hGameData, "IScriptVM", "ReleaseScript", _, SDKType_PlainOldData); 26 | } 27 | 28 | HSCRIPT HScript_CreateScope(const char[] sName, HSCRIPT pParent) 29 | { 30 | return SDKCall(g_hSDKCallCreateScope, GetScriptVM(), sName, pParent); 31 | } 32 | 33 | HSCRIPT HScript_CreateTable() 34 | { 35 | ScriptVariant_t pTable = new ScriptVariant_t(); 36 | SDKCall(g_hSDKCallCreateTable, GetScriptVM(), pTable.Address); 37 | 38 | HSCRIPT pHScript = pTable.nValue; 39 | delete pTable; 40 | return pHScript; 41 | } 42 | 43 | HSCRIPT HScript_CreateInstance(VScriptClass pClass, Address pInstance) 44 | { 45 | return SDKCall(g_hSDKCallRegisterInstance, GetScriptVM(), pClass, pInstance); 46 | } 47 | 48 | int HScript_GetKeyValue(HSCRIPT pHScript, int iIterator, ScriptVariant_t pKey, ScriptVariant_t pValue) 49 | { 50 | return SDKCall(g_hSDKCallGetKeyValue, GetScriptVM(), pHScript, iIterator, pKey.Address, pValue.Address); 51 | } 52 | 53 | int HScript_GetKey(HSCRIPT pHScript, int iIterator, char[] sKey, int iLength, fieldtype_t &nField) 54 | { 55 | // if pHScript is null, g_pScriptVM is used instead 56 | 57 | ScriptVariant_t pKey = new ScriptVariant_t(); 58 | ScriptVariant_t pValue = new ScriptVariant_t(); 59 | 60 | iIterator = HScript_GetKeyValue(pHScript, iIterator, pKey, pValue); 61 | 62 | if (iIterator != -1) 63 | { 64 | pKey.GetString(sKey, iLength); 65 | nField = pValue.nType; 66 | } 67 | 68 | delete pKey; 69 | delete pValue; 70 | 71 | return iIterator; 72 | } 73 | 74 | bool HScript_GetValue(HSCRIPT pHScript, const char[] sKey, ScriptVariant_t pValue) 75 | { 76 | return SDKCall(g_hSDKCallGetValue, GetScriptVM(), pHScript, sKey, pValue.Address); 77 | } 78 | 79 | ScriptVariant_t HScript_NativeGetValue(SMField nSMField = SMField_Unknwon) 80 | { 81 | int iLength; 82 | GetNativeStringLength(2, iLength); 83 | 84 | char[] sBuffer = new char[iLength + 1]; 85 | GetNativeString(2, sBuffer, iLength + 1); 86 | 87 | ScriptVariant_t pValue = new ScriptVariant_t(); 88 | bool bResult = HScript_GetValue(GetNativeCell(1), sBuffer, pValue); 89 | 90 | if (!bResult) 91 | { 92 | delete pValue; 93 | ThrowNativeError(SP_ERROR_NATIVE, "Key name '%s' either don't exist or value is null", sBuffer); 94 | } 95 | 96 | if (nSMField == SMField_Unknwon) 97 | return pValue; // skip field check 98 | 99 | fieldtype_t nField = pValue.nType; 100 | if (Field_GetSMField(nField) != nSMField) 101 | { 102 | delete pValue; 103 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid field use '%s'", Field_GetName(nField)); 104 | } 105 | 106 | return pValue; 107 | } 108 | 109 | ScriptVariant_t HScript_NativeGetValueEx(bool bError) 110 | { 111 | // Same as HScript_NativeGetValue, but can get null values, 112 | // IScriptVM::ValueExists and IScriptVM::GetValue have no way to tell the difference between null and actually not existing 113 | HSCRIPT pHScript = GetNativeCell(1); 114 | 115 | int iLength; 116 | GetNativeStringLength(2, iLength); 117 | 118 | char[] sKey = new char[iLength + 1]; 119 | GetNativeString(2, sKey, iLength + 1); 120 | 121 | ScriptVariant_t pKey = new ScriptVariant_t(); 122 | ScriptVariant_t pValue = new ScriptVariant_t(); 123 | 124 | int iIterator; 125 | while ((iIterator = HScript_GetKeyValue(pHScript, iIterator, pKey, pValue)) != -1) 126 | { 127 | char[] sBuffer = new char[iLength + 2]; 128 | pKey.GetString(sBuffer, iLength + 2); 129 | if (!StrEqual(sKey, sBuffer)) 130 | continue; 131 | 132 | delete pKey; 133 | return pValue; 134 | } 135 | 136 | delete pKey; 137 | delete pValue; 138 | 139 | if (bError) 140 | ThrowNativeError(SP_ERROR_NATIVE, "Key name '%s' don't exist", sKey); 141 | 142 | return null; 143 | } 144 | 145 | bool HScript_SetValue(HSCRIPT pHScript, const char[] sKey, ScriptVariant_t pValue) 146 | { 147 | return SDKCall(g_hSDKCallSetValue, GetScriptVM(), pHScript, sKey, pValue.Address); 148 | } 149 | 150 | void HScript_NativeSetValue(SMField nSMField) 151 | { 152 | int iLength; 153 | GetNativeStringLength(2, iLength); 154 | 155 | char[] sBuffer = new char[iLength + 1]; 156 | GetNativeString(2, sBuffer, iLength + 1); 157 | 158 | // HSCRIPT.SetValueNull used SMField_Void, which is just setting void field 159 | fieldtype_t nField = FIELD_VOID; 160 | if (nSMField != SMField_Void) 161 | { 162 | nField = GetNativeCell(3); 163 | if (Field_GetSMField(nField) != nSMField) 164 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid field use '%s'", Field_GetName(nField)); 165 | } 166 | 167 | ScriptVariant_t pValue = new ScriptVariant_t(); 168 | pValue.nType = nField; 169 | 170 | MemoryBlock hMemory; 171 | 172 | switch (nSMField) 173 | { 174 | case SMField_Cell: 175 | { 176 | pValue.nValue = GetNativeCell(4); 177 | } 178 | case SMField_String: 179 | { 180 | GetNativeStringLength(4, iLength); 181 | iLength++; 182 | 183 | char[] sValue = new char[iLength]; 184 | GetNativeString(4, sValue, iLength); 185 | hMemory = CreateStringMemory(sValue); 186 | pValue.nValue = hMemory.Address; 187 | } 188 | case SMField_Vector: 189 | { 190 | float vecVector[3]; 191 | GetNativeArray(4, vecVector, sizeof(vecVector)); 192 | hMemory = CreateVectorMemory(vecVector); 193 | pValue.nValue = hMemory.Address; 194 | } 195 | } 196 | 197 | bool bResult = HScript_SetValue(GetNativeCell(1), sBuffer, pValue); 198 | 199 | delete hMemory; 200 | delete pValue; 201 | 202 | if (!bResult) 203 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid HSCRIPT object '%x'", GetNativeCell(1)); 204 | } 205 | 206 | void HScript_ReleaseValue(HSCRIPT pHScript) 207 | { 208 | ScriptVariant_t pValue = new ScriptVariant_t(); 209 | pValue.nType = FIELD_HSCRIPT; 210 | pValue.nValue = pHScript; 211 | 212 | SDKCall(g_hSDKCallReleaseValue, GetScriptVM(), pValue.Address); 213 | delete pValue; 214 | } 215 | 216 | void HScript_ClearValue(HSCRIPT pHScript, const char[] sKey) 217 | { 218 | SDKCall(g_hSDKCallClearValue, GetScriptVM(), pHScript, sKey); 219 | } 220 | 221 | Address HScript_GetInstanceValue(HSCRIPT pHScript) 222 | { 223 | return SDKCall(g_hSDKCallGetInstanceValue, GetScriptVM(), pHScript, 0); 224 | } 225 | 226 | void HScript_ReleaseScope(HSCRIPT pHScript) 227 | { 228 | SDKCall(g_hSDKCallReleaseScope, GetScriptVM(), pHScript); 229 | } 230 | 231 | void HScript_ReleaseScript(HSCRIPT pHScript) 232 | { 233 | SDKCall(g_hSDKCallReleaseScript, GetScriptVM(), pHScript); 234 | } 235 | -------------------------------------------------------------------------------- /scripting/vscript/vtable.sp: -------------------------------------------------------------------------------- 1 | // Gamedata Sig? whos needs that? 2 | // Using class name and virtual offset, get the address of the function, 3 | // This is helpful when not needing to get sig for multiple games 4 | 5 | static KeyValues g_kvTempGamedata; 6 | 7 | enum struct VTable 8 | { 9 | char sClass[64]; // Name of the class 10 | char sLibrary[16]; // Library name to open 11 | char sSymbol[64]; // String name of symbols from gamedata to search in TF2 12 | int iExtraOffsets[16]; // Extra offsets added from gamedata 13 | Address pAddress; // The address of the VTable symbol 14 | } 15 | 16 | static ArrayList g_aVTables; 17 | 18 | static VTable g_VTableGameData[] = { 19 | { "IGameSystem", "server" }, 20 | { "IScriptVM", "vscript" }, 21 | } 22 | 23 | void VTable_LoadGamedata(GameData hGameData) 24 | { 25 | g_aVTables = new ArrayList(sizeof(VTable)); 26 | 27 | for (int i = 0; i < sizeof(g_VTableGameData); i++) 28 | { 29 | VTable vtable; 30 | vtable = g_VTableGameData[i]; 31 | 32 | hGameData.GetKeyValue(vtable.sClass, vtable.sSymbol, sizeof(vtable.sSymbol)); 33 | VTable_CreateSymbol(vtable.sSymbol, vtable.sSymbol, sizeof(vtable.sSymbol)); 34 | 35 | char sName[64], sExtraOffsets[64]; 36 | Format(sName, sizeof(sName), "%s::ExtraOffsets", vtable.sClass); 37 | hGameData.GetKeyValue(sName, sExtraOffsets, sizeof(sExtraOffsets)); 38 | 39 | char sValues[sizeof(vtable.iExtraOffsets)][12]; 40 | int iCount = ExplodeString(sExtraOffsets, " ", sValues, sizeof(sValues), sizeof(sValues[])); 41 | 42 | for (int j = 0; j < iCount; j++) 43 | vtable.iExtraOffsets[j] = StringToInt(sValues[j]); 44 | 45 | g_aVTables.PushArray(vtable); 46 | } 47 | 48 | VTable_LoadGamedataAddress(); 49 | } 50 | 51 | void VTable_LoadGamedataAddress() 52 | { 53 | int iLength = g_aVTables.Length; 54 | 55 | g_kvTempGamedata = new KeyValues("Games"); 56 | g_kvTempGamedata.JumpToKey("#default", true); 57 | g_kvTempGamedata.JumpToKey("Signatures", true); 58 | 59 | for (int i = 0; i < iLength; i++) 60 | { 61 | VTable vtable; 62 | g_aVTables.GetArray(i, vtable); 63 | VTable_SetSymbol(vtable.sSymbol, vtable.sLibrary); 64 | } 65 | 66 | g_kvTempGamedata.GoBack(); 67 | g_kvTempGamedata.GoBack(); 68 | int iExtraOffset; 69 | 70 | //Not sure if these offsets is the correct method, but it worksTM for TF2 71 | if (g_bWindows) 72 | { 73 | // Extra zeros at the start to prevent possible multi-matches 74 | VTable_LoadAddress(8, 12); 75 | VTable_LoadAddress(0); 76 | } 77 | else 78 | { 79 | // Symbol is located 4 bytes before where vtable offset starts 80 | iExtraOffset = 4; 81 | } 82 | 83 | GameData hTempGameData = VTable_GetGamedata(); 84 | 85 | for (int i = 0; i < iLength; i++) 86 | { 87 | VTable vtable; 88 | g_aVTables.GetArray(i, vtable); 89 | vtable.pAddress = hTempGameData.GetMemSig(vtable.sSymbol) + view_as
(iExtraOffset); 90 | g_aVTables.SetArray(i, vtable); 91 | 92 | if (!vtable.pAddress) 93 | LogError("Could not get address of symbol '%s'", vtable.sSymbol); 94 | } 95 | 96 | delete hTempGameData; 97 | 98 | delete g_kvTempGamedata; 99 | } 100 | 101 | Address VTable_GetAddressFromName(GameData hGameData, const char[] sClass, const char[] sFunction) 102 | { 103 | VTable vtable; 104 | int iLength = g_aVTables.Length; 105 | 106 | for (int i = 0; i < iLength; i++) 107 | { 108 | g_aVTables.GetArray(i, vtable); 109 | if (StrEqual(vtable.sClass, sClass)) 110 | break; 111 | 112 | if (i + 1 == iLength) // checked all of it 113 | LogError("Invalid class in VTable '%s'", sClass); 114 | } 115 | 116 | char sOffset[256]; 117 | Format(sOffset, sizeof(sOffset), "%s::%s", sClass, sFunction); 118 | 119 | int iOffset = hGameData.GetOffset(sOffset); 120 | if (iOffset == -1) 121 | LogError("Could not get offset '%s'", sOffset); 122 | 123 | // Check if game has extra offsets between 124 | for (int i = 0; i < sizeof(vtable.iExtraOffsets); i++) 125 | if (vtable.iExtraOffsets[i] && iOffset >= vtable.iExtraOffsets[i]) 126 | iOffset++; 127 | 128 | Address pAddress = vtable.pAddress + view_as
((iOffset + 1) * 4); 129 | return LoadFromAddress(pAddress, NumberType_Int32); 130 | } 131 | 132 | Address VTable_GetAddressFromOffset(const char[] sClass, int iOffset) 133 | { 134 | VTable vtable; 135 | int iLength = g_aVTables.Length; 136 | 137 | for (int i = 0; i < iLength; i++) 138 | { 139 | g_aVTables.GetArray(i, vtable); 140 | if (StrEqual(vtable.sClass, sClass)) 141 | break; 142 | 143 | if (i + 1 == iLength) // checked all of it 144 | { 145 | // Create a new one to load 146 | strcopy(vtable.sClass, sizeof(vtable.sClass), sClass); 147 | vtable.sLibrary = "server"; 148 | VTable_CreateSymbol(sClass, vtable.sSymbol, sizeof(vtable.sSymbol)); 149 | 150 | g_aVTables.PushArray(vtable); 151 | VTable_LoadGamedataAddress(); 152 | g_aVTables.GetArray(iLength, vtable); 153 | } 154 | } 155 | 156 | Address pAddress = vtable.pAddress + view_as
((iOffset + 1) * 4); 157 | return LoadFromAddress(pAddress, NumberType_Int32); 158 | } 159 | 160 | DynamicDetour VTable_CreateDetour(GameData hGameData, const char[] sClass, const char[] sFunction, ReturnType nReturn = ReturnType_Void, HookParamType nParam1 = HookParamType_Unknown, HookParamType nParam2 = HookParamType_Unknown) 161 | { 162 | Address pFunction = VTable_GetAddressFromName(hGameData, sClass, sFunction); 163 | 164 | DynamicDetour hDetour = new DynamicDetour(pFunction, CallConv_THISCALL, nReturn, ThisPointer_Address); 165 | 166 | if (nParam1 != HookParamType_Unknown) 167 | hDetour.AddParam(nParam1); 168 | 169 | if (nParam2 != HookParamType_Unknown) 170 | hDetour.AddParam(nParam2); 171 | 172 | return hDetour; 173 | } 174 | 175 | void VTable_SetSymbol(const char[] sSymbol, const char[] sLibrary) 176 | { 177 | g_kvTempGamedata.JumpToKey(sSymbol, true); 178 | g_kvTempGamedata.SetString("library", sLibrary); 179 | 180 | if (sSymbol[0] == '@') 181 | { 182 | g_kvTempGamedata.SetString(g_sOperatingSystem, sSymbol); 183 | } 184 | else 185 | { 186 | char sInstructions[256]; 187 | for (int i = 0; i < strlen(sSymbol); i++) 188 | StrCat(sInstructions, sizeof(sInstructions), VTable_GetSigValue(sSymbol[i])); 189 | 190 | // Null terminator at the end 191 | StrCat(sInstructions, sizeof(sInstructions), VTable_GetSigValue(0)); 192 | 193 | g_kvTempGamedata.SetString(g_sOperatingSystem, sInstructions); 194 | } 195 | 196 | g_kvTempGamedata.GoBack(); 197 | } 198 | 199 | void VTable_LoadAddress(int iBackOffset, int iExtraZeros = 0) 200 | { 201 | GameData hGameData = VTable_GetGamedata(); 202 | 203 | g_kvTempGamedata.JumpToKey("#default", true); 204 | g_kvTempGamedata.JumpToKey("Signatures", true); 205 | 206 | VTable vtable; 207 | int iLength = g_aVTables.Length; 208 | 209 | for (int i = 0; i < iLength; i++) 210 | { 211 | g_aVTables.GetArray(i, vtable); 212 | Address pAddress = hGameData.GetMemSig(vtable.sSymbol); 213 | if (pAddress) 214 | VTable_loadAddressSymbol(vtable.sSymbol, vtable.sLibrary, pAddress - view_as
(iBackOffset), iExtraZeros); 215 | else 216 | LogError("Could not find VTable class '%s'", vtable.sSymbol); 217 | } 218 | 219 | g_kvTempGamedata.GoBack(); 220 | g_kvTempGamedata.GoBack(); 221 | 222 | delete hGameData; 223 | } 224 | 225 | void VTable_loadAddressSymbol(const char[] sSymbol, const char[] sLibrary, Address pAddress, int iExtraZeros) 226 | { 227 | g_kvTempGamedata.JumpToKey(sSymbol, true); 228 | 229 | int iTemp = view_as(pAddress); 230 | 231 | char sInstructions[256]; 232 | 233 | for (int i = 0; i < iExtraZeros; i++) // This is for adding zeros before the address in attempt to avoid multiple matches 234 | StrCat(sInstructions, sizeof(sInstructions), VTable_GetSigValue(0)); 235 | 236 | for (int i = 0; i < 4; i++) 237 | { 238 | int iValue = iTemp % 0x100; 239 | if (iValue < 0) 240 | iValue += 0x100; 241 | 242 | StrCat(sInstructions, sizeof(sInstructions), VTable_GetSigValue(iValue)); 243 | iTemp = RoundToNearest(float(iTemp - iValue) / float(0x100)); 244 | } 245 | 246 | g_kvTempGamedata.SetString("library", sLibrary); 247 | g_kvTempGamedata.SetString(g_sOperatingSystem, sInstructions); 248 | 249 | g_kvTempGamedata.GoBack(); 250 | } 251 | 252 | char[] VTable_GetSigValue(int iValue) 253 | { 254 | char sSig[16]; 255 | Format(sSig, sizeof(sSig), "\\x%02X", iValue); 256 | return sSig; 257 | } 258 | 259 | void VTable_CreateSymbol(const char[] sName, char[] sBuffer, int iLength) 260 | { 261 | if (g_bWindows) 262 | Format(sBuffer, iLength, ".?AV%s@@", sName); // this is so scuffed 263 | else 264 | Format(sBuffer, iLength, "@_ZTV%d%s", strlen(sName), sName); // e.g. @_ZTV11CSquirrelVM 265 | } 266 | 267 | GameData VTable_GetGamedata(const char[] sFile = "__vscript__") 268 | { 269 | char sPath[PLATFORM_MAX_PATH]; 270 | BuildPath(Path_SM, sPath, sizeof(sPath), "gamedata/%s.txt", sFile); 271 | g_kvTempGamedata.ExportToFile(sPath); 272 | 273 | GameData hGamedata = new GameData(sFile); 274 | DeleteFile(sPath); 275 | 276 | return hGamedata; 277 | } -------------------------------------------------------------------------------- /scripting/vscript/function.sp: -------------------------------------------------------------------------------- 1 | static int g_iFunctionBinding_ScriptName; 2 | static int g_iFunctionBinding_FunctionName; 3 | static int g_iFunctionBinding_Description; 4 | static int g_iFunctionBinding_ReturnType; 5 | static int g_iFunctionBinding_Parameters; 6 | static int g_iFunctionBinding_Binding; 7 | static int g_iFunctionBinding_Function; 8 | static int g_iFunctionBinding_Flags; 9 | static int g_iFunctionBinding_sizeof; 10 | 11 | static Handle g_hSDKCallRegisterFunction; 12 | 13 | enum ScriptFuncBindingFlags_t 14 | { 15 | SF_MEMBER_FUNC = 0x01, 16 | }; 17 | 18 | void Function_LoadGamedata(GameData hGameData) 19 | { 20 | g_iFunctionBinding_ScriptName = hGameData.GetOffset("ScriptFunctionBinding_t::m_pszScriptName"); 21 | g_iFunctionBinding_FunctionName = hGameData.GetOffset("ScriptFunctionBinding_t::m_pszFunction"); 22 | g_iFunctionBinding_Description = hGameData.GetOffset("ScriptFunctionBinding_t::m_pszDescription"); 23 | g_iFunctionBinding_ReturnType = hGameData.GetOffset("ScriptFunctionBinding_t::m_ReturnType"); 24 | g_iFunctionBinding_Parameters = hGameData.GetOffset("ScriptFunctionBinding_t::m_Parameters"); 25 | g_iFunctionBinding_Binding = hGameData.GetOffset("ScriptFunctionBinding_t::m_pfnBinding"); 26 | g_iFunctionBinding_Function = hGameData.GetOffset("ScriptFunctionBinding_t::m_pFunction"); 27 | g_iFunctionBinding_Flags = hGameData.GetOffset("ScriptFunctionBinding_t::m_flags"); 28 | g_iFunctionBinding_sizeof = hGameData.GetOffset("sizeof(ScriptFunctionBinding_t)"); 29 | 30 | g_hSDKCallRegisterFunction = CreateSDKCall(hGameData, "IScriptVM", "RegisterFunction", _, SDKType_PlainOldData); 31 | } 32 | 33 | VScriptFunction Function_Create() 34 | { 35 | // TODO proper way to handle with memory? 36 | 37 | MemoryBlock hFunction = new MemoryBlock(g_iFunctionBinding_sizeof); 38 | 39 | VScriptFunction pFunction = view_as(hFunction.Address); 40 | 41 | hFunction.Disown(); 42 | delete hFunction; 43 | 44 | return pFunction; 45 | } 46 | 47 | void Function_Init(VScriptFunction pFunction, bool bClass) 48 | { 49 | for (int i = 0; i < g_iFunctionBinding_sizeof; i++) // Make sure that all is cleared first 50 | StoreToAddress(pFunction + view_as
(i), 0, NumberType_Int8); 51 | 52 | // Set strings as empty, but not null 53 | Address pEmptyString = GetEmptyString(); 54 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_ScriptName), pEmptyString, NumberType_Int32); 55 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_FunctionName), pEmptyString, NumberType_Int32); 56 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_Description), pEmptyString, NumberType_Int32); 57 | 58 | // Right now just need to set flags, currently we can only support member functions 59 | if (bClass) 60 | Function_SetFlags(pFunction, SF_MEMBER_FUNC); 61 | } 62 | 63 | void Function_GetScriptName(VScriptFunction pFunction, char[] sBuffer, int iLength) 64 | { 65 | LoadPointerStringFromAddress(pFunction + view_as
(g_iFunctionBinding_ScriptName), sBuffer, iLength); 66 | } 67 | 68 | void Function_SetScriptName(VScriptFunction pFunction, int iParam) 69 | { 70 | StoreNativePointerStringToAddress(pFunction + view_as
(g_iFunctionBinding_ScriptName), iParam); 71 | } 72 | 73 | void Function_GetFunctionName(VScriptFunction pFunction, char[] sBuffer, int iLength) 74 | { 75 | LoadPointerStringFromAddress(pFunction + view_as
(g_iFunctionBinding_FunctionName), sBuffer, iLength); 76 | } 77 | 78 | void Function_SetFunctionName(VScriptFunction pFunction, int iParam) 79 | { 80 | StoreNativePointerStringToAddress(pFunction + view_as
(g_iFunctionBinding_FunctionName), iParam); 81 | } 82 | 83 | Address Function_GetDescription(VScriptFunction pFunction, char[] sBuffer, int iLength) 84 | { 85 | Address pString = LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Description), NumberType_Int32); 86 | LoadStringFromAddress(pString, sBuffer, iLength); 87 | return pString; 88 | } 89 | 90 | void Function_SetDescription(VScriptFunction pFunction, int iParam) 91 | { 92 | StoreNativePointerStringToAddress(pFunction + view_as
(g_iFunctionBinding_Description), iParam); 93 | } 94 | 95 | fieldtype_t Function_GetReturnType(VScriptFunction pFunction) 96 | { 97 | return Field_GameToEnum(LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_ReturnType), NumberType_Int32)); 98 | } 99 | 100 | void Function_SetReturnType(VScriptFunction pFunction, fieldtype_t nField) 101 | { 102 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_ReturnType), Field_EnumToGame(nField), NumberType_Int32); 103 | } 104 | 105 | fieldtype_t Function_GetParam(VScriptFunction pFunction, int iPosition) 106 | { 107 | Address pData = LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Parameters), NumberType_Int32); 108 | return Field_GameToEnum(LoadFromAddress(pData + view_as
(4 * iPosition), NumberType_Int32)); 109 | } 110 | 111 | void Function_SetParam(VScriptFunction pFunction, int iPosition, fieldtype_t nField) 112 | { 113 | // Create any new needed params 114 | Memory_UtlVectorSetSize(pFunction + view_as
(g_iFunctionBinding_Parameters), 4, iPosition + 1); 115 | 116 | Address pData = LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Parameters), NumberType_Int32); 117 | StoreToAddress(pData + view_as
(4 * iPosition), Field_EnumToGame(nField), NumberType_Int32); 118 | } 119 | 120 | int Function_GetParamCount(VScriptFunction pFunction) 121 | { 122 | return LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Parameters) + view_as
(0x0C), NumberType_Int32); 123 | } 124 | 125 | Address Function_GetBinding(VScriptFunction pFunction) 126 | { 127 | return LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Binding), NumberType_Int32); 128 | } 129 | 130 | void Function_SetBinding(VScriptFunction pFunction, Address pBinding) 131 | { 132 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_Binding), pBinding, NumberType_Int32); 133 | } 134 | 135 | Address Function_GetFunction(VScriptFunction pFunction) 136 | { 137 | int iOffset = Function_GetOffset(pFunction); 138 | if (iOffset == -1) 139 | { 140 | // Simple function address 141 | return LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Function), NumberType_Int32); 142 | } 143 | else 144 | { 145 | // Virtual function 146 | char sClass[64]; 147 | VScriptClass pClass = List_GetClassFromFunction(pFunction); 148 | Class_GetScriptName(pClass, sClass, sizeof(sClass)); 149 | return VTable_GetAddressFromOffset(sClass, iOffset); 150 | } 151 | } 152 | 153 | void Function_SetFunction(VScriptFunction pFunction, Address pFunc) 154 | { 155 | for (int iOffset = 4; iOffset < g_iScriptFunctionBinding_sizeof; iOffset++) // Fill any extra empty space as nothing 156 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_Function + iOffset), 0x00, NumberType_Int8); 157 | 158 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_Function), pFunc, NumberType_Int32); 159 | } 160 | 161 | int Function_GetOffset(VScriptFunction pFunction) 162 | { 163 | Address pAddress = LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Function), NumberType_Int32); 164 | 165 | // This function could be virtual, check it 166 | int iOffset; 167 | if (g_bWindows) 168 | { 169 | if (g_iScriptFunctionBinding_sizeof > 4 && LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Function + 4), NumberType_Int32) != 0) 170 | return -1; // It's a virtual in linux, but not windows 171 | 172 | // Windows only gives function address that directly calls a virtual, load the instruction 173 | Address pInstruction = LoadFromAddress(pAddress, NumberType_Int32); 174 | 175 | int iRead = RoundToFloor(float(view_as(pInstruction)) / float(0x01000000)); 176 | if (iRead < 0) 177 | iRead += 0x100; 178 | 179 | pInstruction = pInstruction & view_as
(0x00FFFFFF); 180 | 181 | if (pInstruction != view_as
(0xFF018B)) // First 3 bytes on virtual call 182 | return -1; // normal function 183 | 184 | switch (iRead) 185 | { 186 | // 1-byte read 187 | case 0x60: iOffset = LoadFromAddress(pAddress + view_as
(4), NumberType_Int8); 188 | 189 | // 4-byte read 190 | case 0xA0: iOffset = LoadFromAddress(pAddress + view_as
(4), NumberType_Int32); 191 | 192 | case 0x90: return -1; // 4-byte read, but does not return and does more underneath, so treat it as a normal function 193 | 194 | default: 195 | { 196 | char sName[256]; 197 | Function_GetScriptName(pFunction, sName, sizeof(sName)); 198 | LogError("Unknown virtual instruction %02x from %s", iRead, sName); 199 | return -1; 200 | } 201 | } 202 | } 203 | else 204 | { 205 | // pAddress is an offset 206 | iOffset = view_as(pAddress) - 1; 207 | if (iOffset < 0 || iOffset >= 0x01000000) 208 | return -1; // normal function 209 | } 210 | 211 | // Its a virtual function 212 | return RoundToFloor(float(iOffset) / 4.0); 213 | 214 | } 215 | 216 | ScriptFuncBindingFlags_t Function_GetFlags(VScriptFunction pFunction) 217 | { 218 | return LoadFromAddress(pFunction + view_as
(g_iFunctionBinding_Flags), NumberType_Int32); 219 | } 220 | 221 | void Function_SetFlags(VScriptFunction pFunction, ScriptFuncBindingFlags_t nFlags) 222 | { 223 | StoreToAddress(pFunction + view_as
(g_iFunctionBinding_Flags), nFlags, NumberType_Int32); 224 | } 225 | 226 | void Function_SetFunctionEmpty(VScriptFunction pFunction) 227 | { 228 | Address pAddress = Memory_CreateEmptyFunction(Function_GetReturnType(pFunction) != FIELD_VOID); 229 | Function_SetFunction(pFunction, pAddress); 230 | } 231 | 232 | void Function_CopyFrom(VScriptFunction pTo, VScriptFunction pFrom) 233 | { 234 | for (int i = 0; i < g_iFunctionBinding_sizeof; i++) 235 | StoreToAddress(pTo + view_as
(i), LoadFromAddress(pFrom + view_as
(i), NumberType_Int8), NumberType_Int8); 236 | } 237 | 238 | void Function_Register(VScriptFunction pFunction) 239 | { 240 | SDKCall(g_hSDKCallRegisterFunction, GetScriptVM(), pFunction); 241 | } 242 | 243 | Handle Function_CreateSDKCall(VScriptFunction pFunction, bool bEntity = true, bool bReturnPlain = false) 244 | { 245 | if (!(Function_GetFlags(pFunction) & SF_MEMBER_FUNC)) 246 | StartPrepSDKCall(SDKCall_Static); 247 | else if (bEntity && Class_IsDerivedFrom(List_GetClassFromFunction(pFunction), List_GetClass("CBaseEntity"))) 248 | StartPrepSDKCall(SDKCall_Entity); 249 | else 250 | StartPrepSDKCall(SDKCall_Raw); 251 | 252 | int iOffset = Function_GetOffset(pFunction); 253 | if (iOffset != -1) 254 | PrepSDKCall_SetVirtual(iOffset); 255 | else 256 | PrepSDKCall_SetAddress(Function_GetFunction(pFunction)); 257 | 258 | int iCount = Function_GetParamCount(pFunction); 259 | for (int i = 0; i < iCount; i++) 260 | { 261 | fieldtype_t nField = Function_GetParam(pFunction, i); 262 | PrepSDKCall_AddParameter(Field_GetSDKType(nField), Field_GetSDKPassMethod(nField), VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWNOTINGAME|VDECODE_FLAG_ALLOWWORLD); 263 | } 264 | 265 | fieldtype_t nField = Function_GetReturnType(pFunction); 266 | if (nField != FIELD_VOID) 267 | { 268 | if (bReturnPlain && Field_GetSMField(nField) != SMField_Cell) 269 | PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain); 270 | else 271 | PrepSDKCall_SetReturnInfo(Field_GetSDKType(nField), Field_GetSDKPassMethod(nField)); 272 | } 273 | 274 | return EndPrepSDKCall(); 275 | } 276 | 277 | DynamicDetour Function_CreateDetour(VScriptFunction pFunction) 278 | { 279 | fieldtype_t nField = Function_GetReturnType(pFunction); 280 | 281 | DynamicDetour hDetour; 282 | if (!(Function_GetFlags(pFunction) & SF_MEMBER_FUNC)) 283 | hDetour = new DynamicDetour(Function_GetFunction(pFunction), CallConv_CDECL, Field_GetReturnType(nField), ThisPointer_Ignore); 284 | else if (Class_IsDerivedFrom(List_GetClassFromFunction(pFunction), List_GetClass("CBaseEntity"))) 285 | hDetour = new DynamicDetour(Function_GetFunction(pFunction), CallConv_THISCALL, Field_GetReturnType(nField), ThisPointer_CBaseEntity); 286 | else 287 | hDetour = new DynamicDetour(Function_GetFunction(pFunction), CallConv_THISCALL, Field_GetReturnType(nField), ThisPointer_Address); 288 | 289 | Function_FillParams(pFunction, hDetour); 290 | return hDetour; 291 | } 292 | 293 | DynamicHook Function_CreateHook(VScriptFunction pFunction) 294 | { 295 | int iOffset = Function_GetOffset(pFunction); 296 | if (iOffset == -1) 297 | return null; 298 | 299 | fieldtype_t nField = Function_GetReturnType(pFunction); 300 | 301 | DynamicHook hHook; 302 | if (Class_IsDerivedFrom(List_GetClassFromFunction(pFunction), List_GetClass("CBaseEntity"))) 303 | hHook = new DynamicHook(iOffset, HookType_Entity, Field_GetReturnType(nField), ThisPointer_CBaseEntity); 304 | else 305 | hHook = new DynamicHook(iOffset, HookType_Raw, Field_GetReturnType(nField), ThisPointer_Address); 306 | 307 | Function_FillParams(pFunction, hHook); 308 | return hHook; 309 | } 310 | 311 | void Function_FillParams(VScriptFunction pFunction, DHookSetup hSetup) 312 | { 313 | int iCount = Function_GetParamCount(pFunction); 314 | for (int i = 0; i < iCount; i++) 315 | { 316 | fieldtype_t nField = Function_GetParam(pFunction, i); 317 | HookParamType nType = Field_GetParamType(nField); 318 | hSetup.AddParam(nType, nType == HookParamType_Object ? Field_GetSize(nField) : -1); 319 | } 320 | } 321 | -------------------------------------------------------------------------------- /scripting/vscript_test.sp: -------------------------------------------------------------------------------- 1 | #include "include/vscript.inc" 2 | 3 | #define TEST_ENTITY 0 // worldspawn 4 | #define TEST_CLASSNAME "worldspawn" 5 | 6 | #define TEST_INTEGER 322 7 | #define TEST_FLOAT 3.14159 8 | #define TEST_CSTRING "VScript Is Awesome!" 9 | 10 | #define TEST_BOOLEAN true 11 | #define TEST_BOOLEAN_STR "true" 12 | 13 | #define TEST_VECTOR_0 1.0 14 | #define TEST_VECTOR_1 2.0 15 | #define TEST_VECTOR_2 3.0 16 | #define TEST_VECTOR {TEST_VECTOR_0, TEST_VECTOR_1, TEST_VECTOR_2} 17 | 18 | public Plugin myinfo = 19 | { 20 | name = "VScript Tests", 21 | author = "42", 22 | description = "Test and showcase stuffs for VScript plugin", 23 | version = "1.0.0", 24 | url = "https://github.com/FortyTwoFortyTwo/VScript", 25 | }; 26 | 27 | public void OnAllPluginsLoaded() 28 | { 29 | // Best to do it in OnAllPluginsLoaded, to ensure that vscript plugin is fully loaded 30 | 31 | /* 32 | * Test member call with bunch of params, this first because of resetting g_pScriptVM 33 | */ 34 | 35 | VScriptFunction pFunction = VScript_CreateClassFunction("CBaseEntity", "BunchOfParams"); 36 | pFunction.SetParam(1, FIELD_INTEGER); 37 | pFunction.SetParam(2, FIELD_FLOAT); 38 | pFunction.SetParam(3, FIELD_BOOLEAN); 39 | pFunction.SetParam(4, FIELD_CSTRING); 40 | pFunction.SetParam(5, FIELD_VECTOR); 41 | 42 | pFunction.Return = FIELD_FLOAT; 43 | pFunction.SetFunctionEmpty(); 44 | 45 | // If script vm is already initialized, force reset it as we can use modified CBaseEntity. 46 | // If were only calling VScriptClass.RegisterInstance or VScriptFunction.Register, only need to manually call VScript_OnScriptVMInitialized() without resetting it. 47 | if (VScript_IsScriptVMInitialized()) 48 | VScript_ResetScriptVM(); 49 | } 50 | 51 | public void VScript_OnScriptVMInitialized() 52 | { 53 | VScriptFunction pFunction; 54 | VScriptExecute hExecute; 55 | int iValue; 56 | char sBuffer[256]; 57 | float vecResult[3]; 58 | 59 | // BunchOfParams created at OnAllPluginsLoaded 60 | RunScript("function BunchOfParams(entity, param1, param2, param3, param4, param5) { return entity.BunchOfParams(param1, param2, param3, param4, param5) }"); 61 | 62 | // Setup VScript Call 63 | hExecute = new VScriptExecute(HSCRIPT_RootTable.GetValue("BunchOfParams")); 64 | hExecute.SetParam(1, FIELD_HSCRIPT, VScript_EntityToHScript(TEST_ENTITY)); 65 | hExecute.SetParam(2, FIELD_INTEGER, TEST_INTEGER); 66 | hExecute.SetParam(3, FIELD_FLOAT, TEST_FLOAT); 67 | hExecute.SetParam(4, FIELD_BOOLEAN, TEST_BOOLEAN); 68 | hExecute.SetParamString(5, FIELD_CSTRING, TEST_CSTRING); 69 | hExecute.SetParamVector(6, FIELD_VECTOR, TEST_VECTOR); 70 | 71 | // Test binding without detour 72 | hExecute.Execute(); 73 | AssertInt(FIELD_VOID, hExecute.ReturnType); 74 | 75 | // Now detour the newly created function 76 | pFunction = VScript_GetClassFunction("CBaseEntity", "BunchOfParams"); 77 | pFunction.CreateDetour().Enable(Hook_Pre, Detour_BunchOfParams); 78 | 79 | // Test again 80 | hExecute.Execute(); 81 | AssertInt(FIELD_FLOAT, hExecute.ReturnType); 82 | AssertFloat(TEST_FLOAT, hExecute.ReturnValue); 83 | delete hExecute; 84 | 85 | // Test with SDKCall 86 | float flValue = SDKCall(pFunction.CreateSDKCall(), TEST_ENTITY, TEST_INTEGER, TEST_FLOAT, TEST_BOOLEAN, TEST_CSTRING, TEST_VECTOR); 87 | AssertFloat(TEST_FLOAT, flValue); 88 | 89 | /* 90 | * Create AnotherRandomInt function that does the exact same as RandomInt 91 | */ 92 | pFunction = VScript_GetGlobalFunction("AnotherRandomInt"); 93 | if (!pFunction) 94 | { 95 | pFunction = VScript_CreateFunction(); 96 | pFunction.CopyFrom(VScript_GetGlobalFunction("RandomInt")); 97 | pFunction.SetScriptName("AnotherRandomInt"); 98 | pFunction.Register(); 99 | } 100 | 101 | DynamicDetour hDetour = pFunction.CreateDetour(); 102 | hDetour.Enable(Hook_Post, Detour_RandomInt); 103 | iValue = SDKCall(pFunction.CreateSDKCall(), TEST_INTEGER, TEST_INTEGER); 104 | hDetour.Disable(Hook_Post, Detour_RandomInt); 105 | AssertInt(TEST_INTEGER, iValue); 106 | 107 | /* 108 | * Test ReturnString function 109 | */ 110 | 111 | pFunction = VScript_CreateGlobalFunction("ReturnString"); 112 | pFunction.Return = FIELD_CSTRING; 113 | pFunction.SetFunctionEmpty(); 114 | pFunction.Register(); 115 | 116 | hExecute = new VScriptExecute(HSCRIPT_RootTable.GetValue("ReturnString")); 117 | 118 | // Binding without detour 119 | hExecute.Execute(); 120 | AssertInt(FIELD_VOID, hExecute.ReturnType); // null 121 | 122 | pFunction.CreateDetour().Enable(Hook_Pre, Detour_ReturnString); 123 | 124 | // Binding with detour 125 | hExecute.Execute(); 126 | AssertInt(FIELD_CSTRING, hExecute.ReturnType); 127 | hExecute.GetReturnString(sBuffer, sizeof(sBuffer)); 128 | AssertString(TEST_CSTRING, sBuffer); 129 | delete hExecute; 130 | 131 | // Test ReturnString with SDKCall 132 | SDKCall(pFunction.CreateSDKCall(), sBuffer, sizeof(sBuffer)); 133 | AssertString(TEST_CSTRING, sBuffer); 134 | 135 | /* 136 | * Test ReturnVector function 137 | */ 138 | 139 | pFunction = VScript_CreateGlobalFunction("ReturnVector"); 140 | pFunction.Return = FIELD_VECTOR; 141 | pFunction.SetFunctionEmpty(); 142 | pFunction.Register(); 143 | 144 | hExecute = new VScriptExecute(HSCRIPT_RootTable.GetValue("ReturnVector")); 145 | 146 | // Binding without detour 147 | hExecute.Execute(); 148 | AssertInt(FIELD_VOID, hExecute.ReturnType); // null 149 | 150 | pFunction.CreateDetour().Enable(Hook_Pre, Detour_ReturnVector); 151 | 152 | // Binding with detour 153 | hExecute.Execute(); 154 | AssertInt(FIELD_VECTOR, hExecute.ReturnType); 155 | hExecute.GetReturnVector(vecResult); 156 | AssertVector(TEST_VECTOR, vecResult); 157 | delete hExecute; 158 | 159 | /* TODO Fix this 160 | 161 | // Test ReturnVector with SDKCall 162 | SDKCall(pFunction.CreateSDKCall(), vecResult); 163 | AssertVector(TEST_VECTOR, vecResult); 164 | */ 165 | 166 | /* 167 | * Test instance function 168 | */ 169 | 170 | pFunction = VScript_GetClassFunction("CEntities", "FindByClassname"); 171 | pFunction.CreateDetour().Enable(Hook_Pre, Detour_FindByClassname); 172 | 173 | HSCRIPT pEntities = HSCRIPT_RootTable.GetValue("Entities"); 174 | HSCRIPT pEntity = SDKCall(pFunction.CreateSDKCall(), pEntities.Instance, 0, TEST_CLASSNAME); 175 | AssertInt(TEST_ENTITY, VScript_HScriptToEntity(pEntity)); 176 | 177 | /* 178 | * Create a new class instance 179 | */ 180 | 181 | VScriptClass pClass = VScript_CreateClass("NewClass"); 182 | 183 | pFunction = VScript_CreateClassFunction("NewClass", "InstanceFunction"); 184 | pFunction.SetParam(1, FIELD_INTEGER); 185 | pFunction.Return = FIELD_FLOAT; 186 | pFunction.SetFunctionEmpty(); 187 | pFunction.CreateDetour().Enable(Hook_Pre, Detour_InstanceFunction); 188 | 189 | HSCRIPT pInstance = pClass.RegisterInstance("InstanceName"); 190 | 191 | AssertInt(TEST_FLOAT, SDKCall(pFunction.CreateSDKCall(), pInstance.Instance, TEST_INTEGER)); 192 | 193 | /* 194 | * Test virtual function 195 | */ 196 | 197 | pFunction = VScript_GetClassFunction("CBaseEntity", "GetMaxHealth"); 198 | if (pFunction.Offset == -1) 199 | ThrowError("Expected CBaseEntity::GetMaxHealth to be virtual, but is not"); 200 | 201 | // Test detour first 202 | hDetour = pFunction.CreateDetour(); 203 | hDetour.Enable(Hook_Pre, Hook_GetMaxHealth); 204 | iValue = SDKCall(pFunction.CreateSDKCall(), TEST_ENTITY); 205 | AssertInt(iValue, TEST_INTEGER); 206 | hDetour.Disable(Hook_Pre, Hook_GetMaxHealth); 207 | 208 | // Now test virtual hook 209 | DynamicHook hHook = pFunction.CreateHook(); 210 | hHook.HookEntity(Hook_Pre, TEST_ENTITY, Hook_GetMaxHealth); 211 | iValue = SDKCall(pFunction.CreateSDKCall(), TEST_ENTITY); 212 | AssertInt(iValue, TEST_INTEGER); 213 | 214 | /* 215 | * Check that all function params have proper field 216 | */ 217 | 218 | CheckFunctions(VScript_GetAllGlobalFunctions()); 219 | 220 | ArrayList aList = VScript_GetAllClasses(); 221 | int iLength = aList.Length; 222 | for (int i = 0; i < iLength; i++) 223 | { 224 | pClass = aList.Get(i); 225 | CheckFunctions(pClass.GetAllFunctions()); 226 | } 227 | 228 | delete aList; 229 | 230 | /* 231 | * Test compile script with param and returns 232 | */ 233 | 234 | HSCRIPT pScope = VScript_CreateScope("ThisIsAScope"); 235 | RunScript("function ReturnParam(param) { return param }", pScope); 236 | 237 | hExecute = new VScriptExecute(pScope.GetValue("ReturnParam")); 238 | 239 | hExecute.SetParam(1, FIELD_FLOAT, TEST_FLOAT); 240 | hExecute.Execute(); 241 | AssertFloat(TEST_FLOAT, hExecute.ReturnValue); 242 | 243 | hExecute.SetParamString(1, FIELD_CSTRING, TEST_CSTRING); 244 | hExecute.Execute(); 245 | hExecute.GetReturnString(sBuffer, sizeof(sBuffer)); 246 | AssertString(TEST_CSTRING, sBuffer); 247 | 248 | hExecute.SetParamVector(1, FIELD_VECTOR, {1.0, 2.0, 3.0}); 249 | hExecute.Execute(); 250 | hExecute.GetReturnVector(vecResult); 251 | AssertVector(TEST_VECTOR, vecResult); 252 | 253 | delete hExecute; 254 | pScope.ReleaseScope(); 255 | 256 | /* 257 | * Multiple params, test if ScriptVariant_t size is correct 258 | */ 259 | 260 | RunScript("function MoreParam(param1, param2, param3) { return param3 }"); 261 | 262 | hExecute = new VScriptExecute(HSCRIPT_RootTable.GetValue("MoreParam")); 263 | hExecute.SetParam(1, FIELD_FLOAT, TEST_FLOAT); 264 | hExecute.SetParam(2, FIELD_BOOLEAN, TEST_BOOLEAN); 265 | hExecute.SetParam(3, FIELD_INTEGER, TEST_INTEGER); 266 | hExecute.Execute(); 267 | AssertInt(TEST_INTEGER, hExecute.ReturnValue); 268 | 269 | /* 270 | * Test table stuffs 271 | */ 272 | 273 | HSCRIPT pTable = RunScript("return { thing = 322, empty = null, }"); 274 | 275 | AssertInt(322, pTable.GetValue("thing")); 276 | AssertInt(1, pTable.ValueExists("empty")); 277 | AssertInt(1, pTable.IsValueNull("empty")); 278 | 279 | AssertInt(0, pTable.ValueExists(TEST_CSTRING)); 280 | pTable.SetValue(TEST_CSTRING, FIELD_INTEGER, TEST_INTEGER); 281 | AssertInt(1, pTable.ValueExists(TEST_CSTRING)); 282 | AssertInt(FIELD_INTEGER, pTable.GetValueField(TEST_CSTRING)); 283 | 284 | AssertInt(0, pTable.IsValueNull(TEST_CSTRING)); 285 | pTable.SetValueNull(TEST_CSTRING); 286 | AssertInt(1, pTable.IsValueNull(TEST_CSTRING)); 287 | 288 | pTable.ClearValue(TEST_CSTRING); 289 | AssertInt(0, pTable.ValueExists(TEST_CSTRING)); 290 | 291 | pTable.Release(); 292 | 293 | PrintToServer("All tests passed!"); 294 | } 295 | 296 | any RunScript(const char[] sScript, HSCRIPT pScope = HSCRIPT_RootTable) 297 | { 298 | HSCRIPT pCompile = VScript_CompileScript(sScript); 299 | VScriptExecute hExecute = new VScriptExecute(pCompile, pScope); 300 | hExecute.Execute(); 301 | any nReturn = hExecute.ReturnValue; 302 | 303 | delete hExecute; 304 | 305 | pCompile.ReleaseScript(); 306 | return nReturn; 307 | } 308 | 309 | void CheckFunctions(ArrayList aList) 310 | { 311 | // Check that all function params don't have FIELD_VOID 312 | int iLength = aList.Length; 313 | for (int i = 0; i < iLength; i++) 314 | { 315 | VScriptFunction pFunction = aList.Get(i); 316 | 317 | pFunction.Offset; // Check if error would be thrown 318 | 319 | int iParamCount = pFunction.ParamCount; 320 | for (int j = 1; j <= iParamCount; j++) 321 | { 322 | if (pFunction.GetParam(j) != FIELD_VOID) 323 | continue; 324 | 325 | char sName[256]; 326 | pFunction.GetScriptName(sName, sizeof(sName)); 327 | ThrowError("Found FIELD_VOID in function '%s' at param '%d'", sName, j); 328 | } 329 | } 330 | 331 | delete aList; 332 | } 333 | 334 | public MRESReturn Detour_RandomInt(DHookReturn hReturn, DHookParam hParam) 335 | { 336 | AssertInt(TEST_INTEGER, hParam.Get(1)); 337 | AssertInt(TEST_INTEGER, hParam.Get(2)); 338 | return MRES_Ignored; 339 | } 340 | 341 | public MRESReturn Detour_BunchOfParams(int iEntity, DHookReturn hReturn, DHookParam hParam) 342 | { 343 | AssertInt(TEST_INTEGER, hParam.Get(1)); 344 | AssertFloat(TEST_FLOAT, hParam.Get(2)); 345 | AssertInt(TEST_BOOLEAN, hParam.Get(3)); 346 | 347 | char sBuffer[256]; 348 | hParam.GetString(4, sBuffer, sizeof(sBuffer)); 349 | AssertString(TEST_CSTRING, sBuffer); 350 | 351 | float vecBuffer[3]; 352 | for (int i = 0; i < sizeof(vecBuffer); i++) 353 | vecBuffer[i] = hParam.GetObjectVar(5, i * 4, ObjectValueType_Float); 354 | 355 | AssertVector(TEST_VECTOR, vecBuffer); 356 | 357 | hReturn.Value = TEST_FLOAT; 358 | return MRES_Supercede; 359 | } 360 | 361 | public MRESReturn Detour_ReturnString(DHookReturn hReturn) 362 | { 363 | hReturn.SetString(TEST_CSTRING); 364 | return MRES_Supercede; 365 | } 366 | 367 | public MRESReturn Detour_ReturnVector(DHookReturn hReturn) 368 | { 369 | hReturn.SetVector(TEST_VECTOR); 370 | return MRES_Supercede; 371 | } 372 | 373 | public MRESReturn Detour_FindByClassname(Address pThis, DHookReturn hReturn, DHookParam hParam) 374 | { 375 | AssertInt(0, hParam.Get(1)); 376 | 377 | char sBuffer[256]; 378 | hParam.GetString(2, sBuffer, sizeof(sBuffer)); 379 | AssertString(TEST_CLASSNAME, sBuffer); 380 | return MRES_Ignored; 381 | } 382 | 383 | public MRESReturn Detour_InstanceFunction(Address pThis, DHookReturn hReturn, DHookParam hParam) 384 | { 385 | AssertInt(TEST_INTEGER, hParam.Get(1)); 386 | hReturn.Value = TEST_FLOAT; 387 | return MRES_Supercede; 388 | } 389 | 390 | public MRESReturn Hook_GetMaxHealth(int iEntity, DHookReturn hReturn) 391 | { 392 | AssertInt(iEntity, TEST_ENTITY); 393 | hReturn.Value = TEST_INTEGER; 394 | return MRES_Supercede; 395 | } 396 | 397 | void AssertInt(any nValue1, any nValue2) 398 | { 399 | if (nValue1 != nValue2) 400 | ThrowError("Expected int '%d', found '%d'", nValue1, nValue2); 401 | } 402 | 403 | void AssertFloat(any nValue1, any nValue2) 404 | { 405 | if (nValue1 != nValue2) 406 | ThrowError("Expected float '%f', found '%f'", nValue1, nValue2); 407 | } 408 | 409 | void AssertString(const char[] sValue1, const char[] sValue2) 410 | { 411 | if (!StrEqual(sValue1, sValue2)) 412 | ThrowError("Expected string '%s', found '%s'", sValue1, sValue2); 413 | } 414 | 415 | void AssertVector(const float vecValue1[3], const float vecValue2[3]) 416 | { 417 | for (int i = 0; i < 3; i++) 418 | if (vecValue1[i] != vecValue2[i]) 419 | ThrowError("Expected vector '{%0.2f, %0.2f, %0.2f}', found '{%0.2f, %0.2f, %0.2f}'", vecValue1[0], vecValue1[1], vecValue1[2], vecValue2[0], vecValue2[1], vecValue2[2]); 420 | } 421 | -------------------------------------------------------------------------------- /scripting/include/vscript.inc: -------------------------------------------------------------------------------- 1 | #if defined _vscript_included 2 | #endinput 3 | #endif 4 | #define _vscript_included 5 | 6 | #include 7 | #include 8 | 9 | // These field values does NOT reflect with actual game values, as it vaires between games. 10 | // This should only be used with this plugin natives. 11 | enum fieldtype_t 12 | { 13 | FIELD_VOID, 14 | FIELD_FLOAT, 15 | FIELD_VECTOR, 16 | FIELD_INTEGER, 17 | FIELD_BOOLEAN, 18 | FIELD_TYPEUNKNOWN, 19 | FIELD_CSTRING, 20 | FIELD_HSCRIPT, 21 | FIELD_VARIANT, 22 | FIELD_QANGLE, 23 | FIELD_UINT32, 24 | }; 25 | 26 | enum ScriptStatus_t 27 | { 28 | SCRIPT_ERROR = -1, 29 | SCRIPT_DONE, 30 | SCRIPT_RUNNING, 31 | }; 32 | 33 | methodmap Address {} 34 | 35 | methodmap HSCRIPT < Address 36 | { 37 | // Gets the key name by interator in hscript 38 | // 39 | // @param interator Interator number, 0 for the first key. 40 | // @param buffer Buffer to store name. 41 | // @param length Size of buffer. 42 | // @param field Field type value to store, FIELD_VOID if null. 43 | // @return Interator number to get next key, -1 if there is no more to interator through. 44 | public native int GetKey(int interator, char[] buffer, int length, fieldtype_t &field = FIELD_VOID); 45 | 46 | // Gets the field value from key 47 | // 48 | // @param key Key name to get. 49 | // @return Value of the key. 50 | // @error Invalid key name or value is null. 51 | public native fieldtype_t GetValueField(const char[] key); 52 | 53 | // Gets the value from key 54 | // 55 | // @param key Key name to get. 56 | // @return Value of the key. 57 | // @error Invalid key name or field usage. 58 | public native any GetValue(const char[] key); 59 | 60 | // Gets the string value from key 61 | // 62 | // @param key Key name to get. 63 | // @param buffer Buffer to store string. 64 | // @param length Size of buffer. 65 | // @error Invalid key name or field usage. 66 | public native void GetValueString(const char[] key, char[] buffer, int length); 67 | 68 | // Gets the vector value from key 69 | // 70 | // @param key Key name to get. 71 | // @param buffer Buffer to store vector. 72 | // @error Invalid key name or field usage. 73 | public native void GetValueVector(const char[] key, float buffer[3]); 74 | 75 | // Returns whenever key value is null 76 | // 77 | // @param key Key name to test. 78 | // @return True is value is null, false otherwise. 79 | // @error Invalid key name. 80 | public native bool IsValueNull(const char[] key); 81 | 82 | // Sets a value to key 83 | // 84 | // @param key Key name to set. 85 | // @param field Field to use to set. 86 | // @param value Value to set. 87 | public native void SetValue(const char[] key, fieldtype_t field, any value); 88 | 89 | // Sets a string value to key 90 | // 91 | // @param key Key name to set. 92 | // @param field Field to use to set. 93 | // @param value String value to set. 94 | public native void SetValueString(const char[] key, fieldtype_t field, const char[] value); 95 | 96 | // Sets a vector value to key 97 | // 98 | // @param key Key name to set. 99 | // @param field Field to use to set. 100 | // @param value Vector value to set. 101 | public native void SetValueVector(const char[] key, fieldtype_t field, const float value[3]); 102 | 103 | // Sets a null value to key 104 | // 105 | // @param key Key name to set. 106 | public native void SetValueNull(const char[] key); 107 | 108 | // Returns whenever key name exists, key with null value returns true 109 | // 110 | // @param key Key name to test. 111 | // @return True if key exists, false otherwise. 112 | public native bool ValueExists(const char[] key); 113 | 114 | // Clears a key and it's value 115 | // 116 | // @param key Key name to clear. 117 | public native void ClearValue(const char[] key); 118 | 119 | // Get the address of instance 120 | property Address Instance 121 | { 122 | public native get(); 123 | } 124 | 125 | // Frees a HSCRIPT memory 126 | public native void Release(); 127 | 128 | // Frees a HSCRIPT memory. This is only used for script scopes. 129 | public native void ReleaseScope(); 130 | 131 | // Frees a HSCRIPT memory. This is only used for compiled scripts. 132 | public native void ReleaseScript(); 133 | } 134 | 135 | // Several functions accept null HScript as g_pScriptVM for root table 136 | const HSCRIPT HSCRIPT_RootTable = view_as(0); 137 | 138 | methodmap VScriptFunction < Address 139 | { 140 | // Gets the script name 141 | // 142 | // @param buffer Buffer to store name. 143 | // @param length Size of buffer. 144 | public native void GetScriptName(char[] buffer, int length); 145 | 146 | // Sets a script name 147 | // 148 | // @param value Name to set. 149 | public native void SetScriptName(const char[] value); 150 | 151 | // Gets the function name, this is only for display purpose 152 | // 153 | // @param buffer Buffer to store name. 154 | // @param length Size of buffer. 155 | public native void GetFunctionName(char[] buffer, int length); 156 | 157 | // Sets a function name, this is only for display purpose 158 | // 159 | // @param value Name to set. 160 | public native void SetFunctionName(const char[] value); 161 | 162 | // Gets the description 163 | // 164 | // @param buffer Buffer to store name. 165 | // @param length Size of buffer. 166 | public native void GetDescription(char[] buffer, int length); 167 | 168 | // Sets the description 169 | // 170 | // @param value Description to set. 171 | public native void SetDescription(const char[] value); 172 | 173 | // Gets the address of the binding 174 | // Binding gets automatically updated when SetFunctionEmpty is called when possible. 175 | property Address Binding 176 | { 177 | public native get(); 178 | } 179 | 180 | // Gets/Sets the address of the function 181 | property Address Function 182 | { 183 | public native get(); 184 | public native set(Address func); 185 | } 186 | 187 | // Gets the offset of a virtual function from class, -1 if function is not a virtual 188 | property int Offset 189 | { 190 | public native get(); 191 | } 192 | 193 | // Set the function as empty, doing nothing. This is used when wanting to create a new function only to be used for detour. 194 | // This MUST be used after return and params has been set. If return is not void, 0 or null is returned by default. 195 | public native void SetFunctionEmpty(); 196 | 197 | // Gets/Sets the return field type 198 | property fieldtype_t Return 199 | { 200 | public native get(); 201 | public native set(fieldtype_t field); 202 | } 203 | 204 | // Gets amount of parameters function has 205 | property int ParamCount 206 | { 207 | public native get(); 208 | } 209 | 210 | // Gets the field type of a parameter 211 | // 212 | // @param param Parameter number, starting from 1. 213 | // @return Field type of a parameter. 214 | // @error Parameter number out of range. 215 | public native fieldtype_t GetParam(int param); 216 | 217 | // Sets the field type of a parameter, creating any new param values when needed, initialized as FIELD_VOID 218 | // 219 | // @param param Parameter number, starting from 1. 220 | // @param field Field type to set. 221 | public native void SetParam(int param, fieldtype_t field); 222 | 223 | // Copy all datas from another function 224 | // 225 | // @param from A function to copy from. 226 | public native void CopyFrom(VScriptFunction from); 227 | 228 | // Register this as a global function until when g_pScriptVM has been reset. This should be called inside VScript_OnScriptVMInitialized forward. 229 | public native void Register(); 230 | 231 | // Creates an SDKCall with parameters auto filled 232 | // 233 | // @return SDKCall handle, must be deleted when not needed. 234 | public native Handle CreateSDKCall(); 235 | 236 | // Creates a detour handle from DynamicDetour with parameters auto filled 237 | // 238 | // @return DynamicDetour handle, must be deleted when not needed. 239 | public native DynamicDetour CreateDetour(); 240 | 241 | // Creates a hook handle from DynamicHook with parameters auto filled 242 | // 243 | // @return DynamicHook handle, must be deleted when not needed. Returns null if function is not a virtual. 244 | public native DynamicHook CreateHook(); 245 | 246 | // Gets the class that this function is associated to it, Address_Null if global function 247 | property VScriptClass Class 248 | { 249 | public native get(); 250 | } 251 | } 252 | 253 | methodmap VScriptClass < Address 254 | { 255 | // Gets the script name 256 | // 257 | // @param buffer Buffer to store script name. 258 | // @param length Size of buffer. 259 | public native void GetScriptName(char[] buffer, int length); 260 | 261 | // Sets the script name 262 | // 263 | // @param value Script name to set. 264 | public native void SetScriptName(const char[] value); 265 | 266 | // Gets the class name 267 | // 268 | // @param buffer Buffer to store class name. 269 | // @param length Size of buffer. 270 | public native void GetClassName(char[] buffer, int length); 271 | 272 | // Sets the class name 273 | // 274 | // @param value Class name to set. 275 | public native void SetClassName(const char[] value); 276 | 277 | // Gets the description 278 | // 279 | // @param buffer Buffer to store name. 280 | // @param length Size of buffer. 281 | public native void GetDescription(char[] buffer, int length); 282 | 283 | // Sets the description 284 | // 285 | // @param value Description to set. 286 | public native void SetDescription(const char[] value); 287 | 288 | // Get all of the functions used for this class 289 | // 290 | // @return Arrays of VScriptFunction, handle must be deleted when not needed. 291 | public native ArrayList GetAllFunctions(); 292 | 293 | // Gets VScriptFunction from this class 294 | // 295 | // @param functionName Function name. 296 | // @return Address of VScriptFunction, null if does not exist. 297 | public native VScriptFunction GetFunction(const char[] functionName); 298 | 299 | // Creates a new VScriptFunction connected from this class. Function will need to be filled in then call VScript_ResetScriptVM 300 | // 301 | // @param functionName Function name. 302 | // @return Address of VScriptFunction. 303 | public native VScriptFunction CreateFunction(); 304 | 305 | // Register this class as an instance. This should be used inside VScript_OnScriptVMInitialized forward. 306 | // 307 | // @param instance Name of an instance in script. 308 | // @return Created HSCRIPT instance. 309 | public native HSCRIPT RegisterInstance(const char[] instance); 310 | 311 | // Gets the class that this is based on, Address_Null if does not have base class 312 | property VScriptClass Base 313 | { 314 | public native get(); 315 | } 316 | 317 | // Return whenever if this class function is derived from other class base 318 | // 319 | // @param base Function base to find if derived at. 320 | // @return True if derived from, false otherwise. 321 | public bool IsDerivedFrom(VScriptClass base) 322 | { 323 | while (base) 324 | { 325 | if (this == base) 326 | return true; 327 | 328 | base = base.Base; 329 | } 330 | 331 | return false; 332 | } 333 | 334 | } 335 | 336 | methodmap VScriptExecute < Handle 337 | { 338 | // Creates a new handle to execute a script function 339 | // 340 | // @param script Script address to execute. 341 | // @param scope The script scope to execute the script inside of. 342 | public native VScriptExecute(HSCRIPT script, HSCRIPT scope = HSCRIPT_RootTable); 343 | 344 | // Adds a new parameter at the end 345 | // 346 | // @param type Type of field to set. 347 | // @param value Value to set. 348 | public native void AddParam(fieldtype_t type, any value); 349 | 350 | // Adds a new string parameter at the end 351 | // 352 | // @param type Type of field to set. 353 | // @param value String value to set. 354 | public native void AddParamString(fieldtype_t type, const char[] value); 355 | 356 | // Adds a new vector parameter at the end 357 | // 358 | // @param type Type of field to set. 359 | // @param value Vector value to set. 360 | public native void AddParamVector(fieldtype_t type, const float value[3]); 361 | 362 | // Sets a given parameter with value 363 | // 364 | // @param param Parameter to set. 365 | // @param type Type of field to set. 366 | // @param value Value to set. 367 | public native void SetParam(int param, fieldtype_t type, any value); 368 | 369 | // Sets a given parameter with string value 370 | // 371 | // @param param Parameter to set. 372 | // @param type Type of field to set. 373 | // @param value String value to set. 374 | public native void SetParamString(int param, fieldtype_t type, const char[] value); 375 | 376 | // Sets a given parameter with vector value 377 | // 378 | // @param param Parameter to set. 379 | // @param type Type of field to set. 380 | // @param value Vector value to set. 381 | public native void SetParamVector(int param, fieldtype_t type, const float value[3]); 382 | 383 | // Executes a function 384 | // 385 | // @return Script status after executed. 386 | public native ScriptStatus_t Execute(); 387 | 388 | // Gets return field type, FIELD_VOID if null 389 | property fieldtype_t ReturnType 390 | { 391 | public native get(); 392 | } 393 | 394 | // Gets return value 395 | property any ReturnValue 396 | { 397 | public native get(); 398 | } 399 | 400 | // Gets a return string value 401 | // 402 | // @param buffer Buffer to store string. 403 | // @param length Size of buffer. 404 | public native void GetReturnString(char[] buffer, int length); 405 | 406 | // Gets a return vector value 407 | // 408 | // @param buffer Buffer to store vector. 409 | // @param length Size of buffer. 410 | public native void GetReturnVector(float buffer[3]); 411 | } 412 | 413 | 414 | /** 415 | * Called when g_pScriptVM has been fully initialized, this is where VScriptClass.RegisterInstance and VScriptFunction.Register should be called 416 | * @note This forward does not get called on plugin lateload, use VScript_IsScriptVMInitialized to determine whenever to manually call this forward 417 | */ 418 | forward void VScript_OnScriptVMInitialized(); 419 | 420 | /** 421 | * Returns whenever g_pScriptVM has been initialized, useful for plugin start to determine whenever to call VScript_ResetScriptVM or VScript_OnScriptVMInitialized if this were to return true 422 | * 423 | * @return True if script vm is initialized, false otherwise 424 | */ 425 | native bool VScript_IsScriptVMInitialized(); 426 | 427 | /** 428 | * Deletes g_pScriptVM and creates a new one. This should be used when VScriptClass or VScriptFunction has been modified, including adding new functions to class 429 | */ 430 | native void VScript_ResetScriptVM(); 431 | 432 | /** 433 | * Compiles a script. 434 | * 435 | * @param script Script to compile 436 | * @param id Optional ID to set 437 | * 438 | * @return HSCRIPT of script, null if could not compile. This MUST be freed when not in use by using HSCRIPT.ReleaseScript(). 439 | */ 440 | native HSCRIPT VScript_CompileScript(const char[] script, const char[] id = NULL_STRING); 441 | 442 | /** 443 | * Compiles a script file. 444 | * 445 | * @param filepath Filepath to get a script, 'scripts/vscripts/' are automatically added to the filepath 446 | * 447 | * @return HSCRIPT of script, null if could not compile. This MUST be freed when not in use by using HSCRIPT.ReleaseScript(). 448 | * @error Invalid filepath. 449 | */ 450 | native HSCRIPT VScript_CompileScriptFile(const char[] filepath); 451 | 452 | /** 453 | * Creates a new HSCRIPT scope, it MUST be deleted when no longer needed by using HSCRIPT.ReleaseScope() 454 | * 455 | * @param name Name to set the scope 456 | * @param parent Parent of the HSCRIPT to set as 457 | * 458 | * @return HSCRIPT of scope. 459 | */ 460 | native HSCRIPT VScript_CreateScope(const char[] name, HSCRIPT parent = HSCRIPT_RootTable); 461 | 462 | /** 463 | * Creates a new HSCRIPT table, it MUST be deleted when no longer needed by using HSCRIPT.Release() 464 | * 465 | * @return HSCRIPT of table. 466 | */ 467 | native HSCRIPT VScript_CreateTable(); 468 | 469 | /** 470 | * Creates a new HSCRIPT instance, this is only to be used on non-entity script class that needs it's script instance created 471 | * @note You may need to manually set property to an instance to point where the newly created hscript are at 472 | * 473 | * @param class Script class an instance uses 474 | * @param instance Instance pointer address to use 475 | * 476 | * @return HSCRIPT of an instance, null if could not create one. 477 | */ 478 | native HSCRIPT VScript_CreateInstance(VScriptClass class, Address instance); 479 | 480 | /** 481 | * Get all the classes used for vscript 482 | * 483 | * @return Arrays of VScriptClass, handle must be deleted when not needed. 484 | */ 485 | native ArrayList VScript_GetAllClasses(); 486 | 487 | /** 488 | * Gets VScriptClass from class 489 | * 490 | * @param className Class name. 491 | * 492 | * @return Address of VScriptClass 493 | * @error Invalid class name 494 | */ 495 | native VScriptClass VScript_GetClass(const char[] className); 496 | 497 | /** 498 | * Gets VScriptClass from class or creates one if don't exist. VScriptClass.RegisterInstance must be called after params are filled. 499 | * 500 | * @param className Class name. 501 | * 502 | * @return Address of VScriptClass, either existing or newly created 503 | */ 504 | native VScriptClass VScript_CreateClass(const char[] className); 505 | 506 | /** 507 | * Gets VScriptFunction from class 508 | * 509 | * @param className Class name. 510 | * @param functionName Function name. 511 | * 512 | * @return Address of VScriptFunction, null if class does not have one 513 | * @error Invalid class name 514 | */ 515 | native VScriptFunction VScript_GetClassFunction(const char[] className, const char[] functionName); 516 | 517 | /** 518 | * Get all global functions used for vscript 519 | * 520 | * @return Arrays of VScriptFunction, handle must be deleted when not needed. 521 | */ 522 | native ArrayList VScript_GetAllGlobalFunctions(); 523 | 524 | /** 525 | * Gets VScriptFunction from a global 526 | * 527 | * @param functionName Function name. 528 | * 529 | * @return Address of VScriptFunction 530 | */ 531 | native VScriptFunction VScript_GetGlobalFunction(const char[] functionName); 532 | 533 | /** 534 | * Creates a new VScriptFunction as a pointer. VScriptFunction.Register should be called for function to come into effect. 535 | * 536 | * @return Address of VScriptFunction 537 | */ 538 | native VScriptFunction VScript_CreateFunction(); 539 | 540 | /** 541 | * Gets the script scope of an entity 542 | * 543 | * @param entity Entity index. 544 | * 545 | * @return Address of the HScript scope 546 | * @error Invalid entity 547 | */ 548 | native HSCRIPT VScript_GetEntityScriptScope(int entity); 549 | 550 | /** 551 | * Gets the HScript address of an entity 552 | * 553 | * @param entity Entity index. 554 | * 555 | * @return Address of the HScript 556 | * @error Invalid entity 557 | */ 558 | native HSCRIPT VScript_EntityToHScript(int entity); 559 | 560 | /** 561 | * Gets the entity index from HScript address 562 | * 563 | * @param hscript HScript address. 564 | * 565 | * @return Entity index 566 | */ 567 | native int VScript_HScriptToEntity(HSCRIPT hscript); 568 | 569 | /** 570 | * Gets VScriptFunction from class or creates one if don't exist 571 | * 572 | * @param className Class name. 573 | * @param functionName Function name. 574 | * 575 | * @return Address of VScriptFunction, either existing or newly created 576 | * @error Invalid class name 577 | */ 578 | stock VScriptFunction VScript_CreateClassFunction(const char[] className, const char[] functionName) 579 | { 580 | VScriptFunction func = VScript_GetClassFunction(className, functionName); 581 | if (func) 582 | return func; 583 | 584 | VScriptClass class = VScript_GetClass(className); 585 | func = class.CreateFunction(); 586 | func.SetScriptName(functionName); 587 | return func; 588 | } 589 | 590 | /** 591 | * Gets VScriptFunction from global or creates one if don't exist 592 | * 593 | * @param functionName Function name. 594 | * 595 | * @return Address of VScriptFunction, either existing or newly created 596 | */ 597 | stock VScriptFunction VScript_CreateGlobalFunction(const char[] functionName) 598 | { 599 | VScriptFunction func = VScript_GetGlobalFunction(functionName); 600 | if (func) 601 | return func; 602 | 603 | func = VScript_CreateFunction(); 604 | func.SetScriptName(functionName); 605 | return func; 606 | } 607 | 608 | /** 609 | * Returns the value of a constant 610 | * 611 | * @param table Enum or bitfield name to get. 612 | * @param name Value name to get. 613 | * 614 | * @return Value of the enum key. 615 | * @error Invalid enum, bitfield or value name to get. 616 | */ 617 | stock any VScript_GetConstantsValue(const char[] table, const char[] name) 618 | { 619 | HSCRIPT constants = HSCRIPT_RootTable.GetValue("Constants"); 620 | HSCRIPT keys = constants.GetValue(table); 621 | return keys.GetValue(name); 622 | } 623 | 624 | public SharedPlugin __pl_vscript = 625 | { 626 | name = "vscript", 627 | file = "vscript.smx", 628 | #if defined REQUIRE_PLUGIN 629 | required = 1, 630 | #else 631 | required = 0, 632 | #endif 633 | }; 634 | 635 | #if !defined REQUIRE_PLUGIN 636 | public void __pl_vscript_SetNTVOptional() 637 | { 638 | MarkNativeAsOptional("HSCRIPT.GetKey"); 639 | MarkNativeAsOptional("HSCRIPT.GetValueField"); 640 | MarkNativeAsOptional("HSCRIPT.GetValue"); 641 | MarkNativeAsOptional("HSCRIPT.GetValueString"); 642 | MarkNativeAsOptional("HSCRIPT.GetValueVector"); 643 | MarkNativeAsOptional("HSCRIPT.IsValueNull"); 644 | MarkNativeAsOptional("HSCRIPT.SetValue"); 645 | MarkNativeAsOptional("HSCRIPT.SetValueString"); 646 | MarkNativeAsOptional("HSCRIPT.SetValueVector"); 647 | MarkNativeAsOptional("HSCRIPT.SetValueNull"); 648 | MarkNativeAsOptional("HSCRIPT.ValueExists"); 649 | MarkNativeAsOptional("HSCRIPT.ClearValue"); 650 | MarkNativeAsOptional("HSCRIPT.Instance.get"); 651 | MarkNativeAsOptional("HSCRIPT.Release"); 652 | MarkNativeAsOptional("HSCRIPT.ReleaseScope"); 653 | MarkNativeAsOptional("HSCRIPT.ReleaseScript"); 654 | 655 | MarkNativeAsOptional("VScriptFunction.GetScriptName"); 656 | MarkNativeAsOptional("VScriptFunction.SetScriptName"); 657 | MarkNativeAsOptional("VScriptFunction.GetFunctionName"); 658 | MarkNativeAsOptional("VScriptFunction.SetFunctionName"); 659 | MarkNativeAsOptional("VScriptFunction.GetDescription"); 660 | MarkNativeAsOptional("VScriptFunction.SetDescription"); 661 | MarkNativeAsOptional("VScriptFunction.Binding.get"); 662 | MarkNativeAsOptional("VScriptFunction.Function.get"); 663 | MarkNativeAsOptional("VScriptFunction.Function.set"); 664 | MarkNativeAsOptional("VScriptFunction.Offset.get"); 665 | MarkNativeAsOptional("VScriptFunction.SetFunctionEmpty"); 666 | MarkNativeAsOptional("VScriptFunction.Return.get"); 667 | MarkNativeAsOptional("VScriptFunction.Return.set"); 668 | MarkNativeAsOptional("VScriptFunction.ParamCount.get"); 669 | MarkNativeAsOptional("VScriptFunction.GetParam"); 670 | MarkNativeAsOptional("VScriptFunction.SetParam"); 671 | MarkNativeAsOptional("VScriptFunction.CopyFrom"); 672 | MarkNativeAsOptional("VScriptFunction.Register"); 673 | MarkNativeAsOptional("VScriptFunction.CreateSDKCall"); 674 | MarkNativeAsOptional("VScriptFunction.CreateDetour"); 675 | MarkNativeAsOptional("VScriptFunction.CreateHook"); 676 | MarkNativeAsOptional("VScriptFunction.Class.get"); 677 | 678 | MarkNativeAsOptional("VScriptClass.GetScriptName"); 679 | MarkNativeAsOptional("VScriptClass.SetScriptName"); 680 | MarkNativeAsOptional("VScriptClass.GetClassName"); 681 | MarkNativeAsOptional("VScriptClass.SetClassName"); 682 | MarkNativeAsOptional("VScriptClass.GetDescription"); 683 | MarkNativeAsOptional("VScriptClass.SetDescription"); 684 | MarkNativeAsOptional("VScriptClass.GetAllFunctions"); 685 | MarkNativeAsOptional("VScriptClass.GetFunction"); 686 | MarkNativeAsOptional("VScriptClass.CreateFunction"); 687 | MarkNativeAsOptional("VScriptClass.RegisterInstance"); 688 | MarkNativeAsOptional("VScriptClass.Base.get"); 689 | 690 | MarkNativeAsOptional("VScriptExecute.VScriptExecute"); 691 | MarkNativeAsOptional("VScriptExecute.AddParam"); 692 | MarkNativeAsOptional("VScriptExecute.AddParamString"); 693 | MarkNativeAsOptional("VScriptExecute.AddParamVector"); 694 | MarkNativeAsOptional("VScriptExecute.SetParam"); 695 | MarkNativeAsOptional("VScriptExecute.SetParamString"); 696 | MarkNativeAsOptional("VScriptExecute.SetParamVector"); 697 | MarkNativeAsOptional("VScriptExecute.Execute"); 698 | MarkNativeAsOptional("VScriptExecute.ReturnType.get"); 699 | MarkNativeAsOptional("VScriptExecute.ReturnValue.get"); 700 | MarkNativeAsOptional("VScriptExecute.GetReturnString"); 701 | MarkNativeAsOptional("VScriptExecute.GetReturnVector"); 702 | 703 | MarkNativeAsOptional("VScript_IsScriptVMInitialized"); 704 | MarkNativeAsOptional("VScript_ResetScriptVM"); 705 | MarkNativeAsOptional("VScript_CompileScript"); 706 | MarkNativeAsOptional("VScript_CompileScriptFile"); 707 | MarkNativeAsOptional("VScript_CreateScope"); 708 | MarkNativeAsOptional("VScript_CreateTable"); 709 | MarkNativeAsOptional("VScript_CreateInstance"); 710 | MarkNativeAsOptional("VScript_GetAllClasses"); 711 | MarkNativeAsOptional("VScript_GetClass"); 712 | MarkNativeAsOptional("VScript_CreateClass"); 713 | MarkNativeAsOptional("VScript_GetClassFunction"); 714 | MarkNativeAsOptional("VScript_GetAllGlobalFunctions"); 715 | MarkNativeAsOptional("VScript_GetGlobalFunction"); 716 | MarkNativeAsOptional("VScript_CreateFunction"); 717 | MarkNativeAsOptional("VScript_GetEntityScriptScope"); 718 | MarkNativeAsOptional("VScript_EntityToHScript"); 719 | MarkNativeAsOptional("VScript_HScriptToEntity"); 720 | } 721 | #endif 722 | -------------------------------------------------------------------------------- /scripting/vscript.sp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "include/vscript.inc" 4 | 5 | #define PLUGIN_VERSION "1.10.0" 6 | #define PLUGIN_VERSION_REVISION "manual" 7 | 8 | char g_sOperatingSystem[16]; 9 | bool g_bWindows; 10 | bool g_bAllowResetScriptVM; 11 | 12 | Address g_pToScriptVM; 13 | 14 | int g_iScriptFunctionBinding_sizeof; 15 | 16 | int g_iScriptVariant_sizeof; 17 | int g_iScriptVariant_union; 18 | int g_iScriptVariant_type; 19 | 20 | static Handle g_hSDKCallCompileScript; 21 | static Handle g_hSDKCallRegisterInstance; 22 | static Handle g_hSDKCallGetInstanceEntity; 23 | 24 | const HSCRIPT INVALID_HSCRIPT = view_as(-1); 25 | 26 | const SDKType SDKType_Unknown = view_as(-1); 27 | const SDKPassMethod SDKPass_Unknown = view_as(-1); 28 | 29 | const VScriptClass VScriptClass_Invalid = view_as(Address_Null); 30 | const VScriptFunction VScriptFunction_Invalid = view_as(Address_Null); 31 | 32 | #include "vscript/binding.sp" 33 | #include "vscript/class.sp" 34 | #include "vscript/entity.sp" 35 | #include "vscript/execute.sp" 36 | #include "vscript/field.sp" 37 | #include "vscript/function.sp" 38 | #include "vscript/gamesystem.sp" 39 | #include "vscript/hscript.sp" 40 | #include "vscript/list.sp" 41 | #include "vscript/memory.sp" 42 | #include "vscript/util.sp" 43 | #include "vscript/variant.sp" 44 | #include "vscript/vtable.sp" 45 | 46 | public Plugin myinfo = 47 | { 48 | name = "VScript", 49 | author = "42", 50 | description = "Exposes VScript features into SourceMod", 51 | version = PLUGIN_VERSION ... "." ... PLUGIN_VERSION_REVISION, 52 | url = "https://github.com/FortyTwoFortyTwo/VScript", 53 | }; 54 | 55 | public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] sError, int iLength) 56 | { 57 | CreateNative("HSCRIPT.GetKey", Native_HScript_GetKey); 58 | CreateNative("HSCRIPT.GetValue", Native_HScript_GetValue); 59 | CreateNative("HSCRIPT.GetValueField", Native_HScript_GetValueField); 60 | CreateNative("HSCRIPT.GetValueString", Native_HScript_GetValueString); 61 | CreateNative("HSCRIPT.GetValueVector", Native_HScript_GetValueVector); 62 | CreateNative("HSCRIPT.IsValueNull", Native_HScript_IsValueNull); 63 | CreateNative("HSCRIPT.SetValue", Native_HScript_SetValue); 64 | CreateNative("HSCRIPT.SetValueString", Native_HScript_SetValueString); 65 | CreateNative("HSCRIPT.SetValueVector", Native_HScript_SetValueVector); 66 | CreateNative("HSCRIPT.SetValueNull", Native_HScript_SetValueNull); 67 | CreateNative("HSCRIPT.ValueExists", Native_HScript_ValueExists); 68 | CreateNative("HSCRIPT.ClearValue", Native_HScript_ClearValue); 69 | CreateNative("HSCRIPT.Instance.get", Native_HScript_InstanceGet); 70 | CreateNative("HSCRIPT.Release", Native_HScript_Release); 71 | CreateNative("HSCRIPT.ReleaseScope", Native_HScript_ReleaseScope); 72 | CreateNative("HSCRIPT.ReleaseScript", Native_HScript_ReleaseScript); 73 | 74 | CreateNative("VScriptFunction.GetScriptName", Native_Function_GetScriptName); 75 | CreateNative("VScriptFunction.SetScriptName", Native_Function_SetScriptName); 76 | CreateNative("VScriptFunction.GetFunctionName", Native_Function_GetFunctionName); 77 | CreateNative("VScriptFunction.SetFunctionName", Native_Function_SetFunctionName); 78 | CreateNative("VScriptFunction.GetDescription", Native_Function_GetDescription); 79 | CreateNative("VScriptFunction.SetDescription", Native_Function_SetDescription); 80 | CreateNative("VScriptFunction.Binding.get", Native_Function_BindingGet); 81 | CreateNative("VScriptFunction.Function.get", Native_Function_FunctionGet); 82 | CreateNative("VScriptFunction.Function.set", Native_Function_FunctionSet); 83 | CreateNative("VScriptFunction.Offset.get", Native_Function_OffsetGet); 84 | CreateNative("VScriptFunction.SetFunctionEmpty", Native_Function_SetFunctionEmpty); 85 | CreateNative("VScriptFunction.Return.get", Native_Function_ReturnGet); 86 | CreateNative("VScriptFunction.Return.set", Native_Function_ReturnSet); 87 | CreateNative("VScriptFunction.ParamCount.get", Native_Function_ParamCountGet); 88 | CreateNative("VScriptFunction.GetParam", Native_Function_GetParam); 89 | CreateNative("VScriptFunction.SetParam", Native_Function_SetParam); 90 | CreateNative("VScriptFunction.CopyFrom", Native_Function_CopyFrom); 91 | CreateNative("VScriptFunction.Register", Native_Function_Register); 92 | CreateNative("VScriptFunction.CreateSDKCall", Native_Function_CreateSDKCall); 93 | CreateNative("VScriptFunction.CreateDetour", Native_Function_CreateDetour); 94 | CreateNative("VScriptFunction.CreateHook", Native_Function_CreateHook); 95 | CreateNative("VScriptFunction.Class.get", Native_Function_ClassGet); 96 | 97 | CreateNative("VScriptClass.GetScriptName", Native_Class_GetScriptName); 98 | CreateNative("VScriptClass.SetScriptName", Native_Class_SetScriptName); 99 | CreateNative("VScriptClass.GetClassName", Native_Class_GetClassName); 100 | CreateNative("VScriptClass.SetClassName", Native_Class_SetClassName); 101 | CreateNative("VScriptClass.GetDescription", Native_Class_GetDescription); 102 | CreateNative("VScriptClass.SetDescription", Native_Class_SetDescription); 103 | CreateNative("VScriptClass.GetAllFunctions", Native_Class_GetAllFunctions); 104 | CreateNative("VScriptClass.GetFunction", Native_Class_GetFunction); 105 | CreateNative("VScriptClass.CreateFunction", Native_Class_CreateFunction); 106 | CreateNative("VScriptClass.RegisterInstance", Native_Class_RegisterInstance); 107 | CreateNative("VScriptClass.Base.get", Native_Class_BaseGet); 108 | CreateNative("VScriptClass.IsDerivedFrom", Native_Class_IsDerivedFrom); // legacy native, to be removed later 109 | 110 | CreateNative("VScriptExecute.VScriptExecute", Native_Execute); 111 | CreateNative("VScriptExecute.AddParam", Native_Execute_AddParam); 112 | CreateNative("VScriptExecute.AddParamString", Native_Execute_AddParamString); 113 | CreateNative("VScriptExecute.AddParamVector", Native_Execute_AddParamVector); 114 | CreateNative("VScriptExecute.SetParam", Native_Execute_SetParam); 115 | CreateNative("VScriptExecute.SetParamString", Native_Execute_SetParamString); 116 | CreateNative("VScriptExecute.SetParamVector", Native_Execute_SetParamVector); 117 | CreateNative("VScriptExecute.Execute", Native_Execute_Execute); 118 | CreateNative("VScriptExecute.ReturnType.get", Native_Execute_ReturnTypeGet); 119 | CreateNative("VScriptExecute.ReturnValue.get", Native_Execute_ReturnValueGet); 120 | CreateNative("VScriptExecute.GetReturnString", Native_Execute_GetReturnString); 121 | CreateNative("VScriptExecute.GetReturnVector", Native_Execute_GetReturnVector); 122 | 123 | CreateNative("VScript_IsScriptVMInitialized", Native_IsScriptVMInitialized); 124 | CreateNative("VScript_ResetScriptVM", Native_ResetScriptVM); 125 | CreateNative("VScript_CompileScript", Native_CompileScript); 126 | CreateNative("VScript_CompileScriptFile", Native_CompileScriptFile); 127 | CreateNative("VScript_CreateScope", Native_CreateScope); 128 | CreateNative("VScript_CreateTable", Native_CreateTable); 129 | CreateNative("VScript_CreateInstance", Native_CreateInstance); 130 | CreateNative("VScript_GetAllClasses", Native_GetAllClasses); 131 | CreateNative("VScript_GetClass", Native_GetClass); 132 | CreateNative("VScript_CreateClass", Native_CreateClass); 133 | CreateNative("VScript_GetClassFunction", Native_GetClassFunction); 134 | CreateNative("VScript_GetAllGlobalFunctions", Native_GetAllGlobalFunctions); 135 | CreateNative("VScript_GetGlobalFunction", Native_GetGlobalFunction); 136 | CreateNative("VScript_CreateFunction", Native_CreateFunction); 137 | CreateNative("VScript_GetEntityScriptScope", Native_GetEntityScriptScope); 138 | CreateNative("VScript_EntityToHScript", Native_EntityToHScript); 139 | CreateNative("VScript_HScriptToEntity", Native_HScriptToEntity); 140 | 141 | RegPluginLibrary("vscript"); 142 | return APLRes_Success; 143 | } 144 | 145 | public void OnPluginStart() 146 | { 147 | CreateConVar("vscript_version", PLUGIN_VERSION ... "." ... PLUGIN_VERSION_REVISION, "VScript plugin version", FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD); 148 | 149 | GameData hGameData = new GameData("vscript"); 150 | 151 | hGameData.GetKeyValue("OS", g_sOperatingSystem, sizeof(g_sOperatingSystem)); 152 | g_bWindows = StrEqual(g_sOperatingSystem, "windows"); 153 | 154 | char sVal[12]; 155 | hGameData.GetKeyValue("AllowResetScriptVM", sVal, sizeof(sVal)); 156 | g_bAllowResetScriptVM = !!StringToInt(sVal); 157 | 158 | g_iScriptFunctionBinding_sizeof = hGameData.GetOffset("sizeof(ScriptFunctionBindingStorageType_t)"); 159 | 160 | g_iScriptVariant_sizeof = hGameData.GetOffset("sizeof(ScriptVariant_t)"); 161 | g_iScriptVariant_union = hGameData.GetOffset("ScriptVariant_t::union"); 162 | g_iScriptVariant_type = hGameData.GetOffset("ScriptVariant_t::m_type"); 163 | 164 | VTable_LoadGamedata(hGameData); 165 | 166 | Class_LoadGamedata(hGameData); 167 | Entity_LoadGamedata(hGameData); 168 | Execute_LoadGamedata(hGameData); 169 | Field_LoadGamedata(hGameData); 170 | Function_LoadGamedata(hGameData); 171 | GameSystem_LoadGamedata(hGameData); 172 | HScript_LoadGamedata(hGameData); 173 | List_LoadGamedata(hGameData); 174 | 175 | g_hSDKCallCompileScript = CreateSDKCall(hGameData, "IScriptVM", "CompileScript", SDKType_PlainOldData, SDKType_String, SDKType_String); 176 | g_hSDKCallRegisterInstance = CreateSDKCall(hGameData, "IScriptVM", "RegisterInstance", SDKType_PlainOldData, SDKType_PlainOldData, SDKType_String); 177 | g_hSDKCallGetInstanceEntity = CreateSDKCall(hGameData, "IScriptVM", "GetInstanceValue", SDKType_CBaseEntity, SDKType_PlainOldData, SDKType_PlainOldData); 178 | 179 | delete hGameData; 180 | 181 | List_LoadDefaults(); 182 | Binding_Init(); 183 | Memory_Init(); 184 | } 185 | 186 | public void OnPluginEnd() 187 | { 188 | Memory_DisownAll(); 189 | } 190 | 191 | public void OnEntityCreated(int iEntity, const char[] sClassname) 192 | { 193 | List_AddEntityScriptDesc(iEntity); 194 | } 195 | 196 | public any Native_HScript_GetKey(Handle hPlugin, int iNumParams) 197 | { 198 | int iLength = GetNativeCell(4); 199 | char[] sBuffer = new char[iLength]; 200 | 201 | fieldtype_t nField; 202 | 203 | int iIterator = HScript_GetKey(GetNativeCell(1), GetNativeCell(2), sBuffer, iLength, nField) 204 | if (iIterator == -1) 205 | return iIterator; 206 | 207 | SetNativeString(3, sBuffer, iLength); 208 | SetNativeCellRef(5, nField); 209 | return iIterator; 210 | } 211 | 212 | public any Native_HScript_GetValueField(Handle hPlugin, int iNumParams) 213 | { 214 | ScriptVariant_t pValue = HScript_NativeGetValue(); 215 | fieldtype_t nType = pValue.nType; 216 | delete pValue; 217 | return nType; 218 | } 219 | 220 | public any Native_HScript_GetValue(Handle hPlugin, int iNumParams) 221 | { 222 | ScriptVariant_t pValue = HScript_NativeGetValue(SMField_Cell); 223 | any nValue = pValue.nValue; 224 | delete pValue; 225 | return nValue; 226 | } 227 | 228 | public any Native_HScript_GetValueString(Handle hPlugin, int iNumParams) 229 | { 230 | ScriptVariant_t pValue = HScript_NativeGetValue(SMField_String); 231 | 232 | int iLength = GetNativeCell(4); 233 | char[] sBuffer = new char[iLength]; 234 | pValue.GetString(sBuffer, iLength); 235 | SetNativeString(3, sBuffer, iLength); 236 | 237 | delete pValue; 238 | return 0; 239 | } 240 | 241 | public any Native_HScript_GetValueVector(Handle hPlugin, int iNumParams) 242 | { 243 | ScriptVariant_t pValue = HScript_NativeGetValue(SMField_Vector); 244 | 245 | float vecBuffer[3]; 246 | pValue.GetVector(vecBuffer); 247 | SetNativeArray(3, vecBuffer, sizeof(vecBuffer)); 248 | 249 | delete pValue; 250 | return 0; 251 | } 252 | 253 | public any Native_HScript_IsValueNull(Handle hPlugin, int iNumParams) 254 | { 255 | ScriptVariant_t pValue = HScript_NativeGetValueEx(true); 256 | 257 | bool bNull = pValue.nType == FIELD_VOID; 258 | delete pValue; 259 | return bNull; 260 | } 261 | 262 | public any Native_HScript_SetValue(Handle hPlugin, int iNumParams) 263 | { 264 | HScript_NativeSetValue(SMField_Cell); 265 | return 0; 266 | } 267 | 268 | public any Native_HScript_SetValueString(Handle hPlugin, int iNumParams) 269 | { 270 | HScript_NativeSetValue(SMField_String); 271 | return 0; 272 | } 273 | 274 | public any Native_HScript_SetValueVector(Handle hPlugin, int iNumParams) 275 | { 276 | HScript_NativeSetValue(SMField_Vector); 277 | return 0; 278 | } 279 | 280 | public any Native_HScript_SetValueNull(Handle hPlugin, int iNumParams) 281 | { 282 | HScript_NativeSetValue(SMField_Void); // FIELD_VOID is for null 283 | return 0; 284 | } 285 | 286 | public any Native_HScript_ValueExists(Handle hPlugin, int iNumParams) 287 | { 288 | ScriptVariant_t pValue = HScript_NativeGetValueEx(false); 289 | if (pValue) 290 | { 291 | delete pValue; 292 | return true; 293 | } 294 | 295 | return false; 296 | } 297 | 298 | public any Native_HScript_ClearValue(Handle hPlugin, int iNumParams) 299 | { 300 | int iLength; 301 | GetNativeStringLength(2, iLength); 302 | 303 | char[] sBuffer = new char[iLength + 1]; 304 | GetNativeString(2, sBuffer, iLength + 1); 305 | 306 | HScript_ClearValue(GetNativeCell(1), sBuffer); 307 | return 0; 308 | } 309 | 310 | public any Native_HScript_InstanceGet(Handle hPlugin, int iNumParams) 311 | { 312 | return HScript_GetInstanceValue(GetNativeCell(1)); 313 | } 314 | 315 | public any Native_HScript_Release(Handle hPlugin, int iNumParams) 316 | { 317 | HScript_ReleaseValue(GetNativeCell(1)); 318 | return 0; 319 | } 320 | 321 | public any Native_HScript_ReleaseScope(Handle hPlugin, int iNumParams) 322 | { 323 | HScript_ReleaseScope(GetNativeCell(1)); 324 | return 0; 325 | } 326 | 327 | public any Native_HScript_ReleaseScript(Handle hPlugin, int iNumParams) 328 | { 329 | HScript_ReleaseScript(GetNativeCell(1)); 330 | return 0; 331 | } 332 | 333 | public any Native_Function_GetScriptName(Handle hPlugin, int iNumParams) 334 | { 335 | int iLength = GetNativeCell(3); 336 | char[] sBuffer = new char[iLength]; 337 | 338 | Function_GetScriptName(GetNativeCell(1), sBuffer, iLength); 339 | SetNativeString(2, sBuffer, iLength); 340 | return 0; 341 | } 342 | 343 | public any Native_Function_SetScriptName(Handle hPlugin, int iNumParams) 344 | { 345 | VScriptFunction pFunction = GetNativeCell(1); 346 | 347 | int iLength; 348 | GetNativeStringLength(2, iLength); 349 | 350 | char[] sBuffer = new char[iLength + 1]; 351 | GetNativeString(2, sBuffer, iLength + 1); 352 | 353 | // Check if script name not already exist 354 | if (Function_GetFlags(pFunction) & SF_MEMBER_FUNC) 355 | { 356 | VScriptClass pClass = List_GetClassFromFunction(pFunction); 357 | if (Class_GetFunctionFromName(pClass, sBuffer)) 358 | { 359 | char sClass[256]; 360 | Class_GetScriptName(pClass, sClass, sizeof(sClass)); 361 | ThrowNativeError(SP_ERROR_NATIVE, "Class '%s' already have a function named '%s'", sClass, sBuffer); 362 | } 363 | } 364 | else if (List_GetFunction(sBuffer)) 365 | { 366 | ThrowNativeError(SP_ERROR_NATIVE, "Global function named '%s' already exists", sBuffer); 367 | } 368 | 369 | Function_SetScriptName(pFunction, 2); 370 | return 0; 371 | } 372 | 373 | public any Native_Function_GetFunctionName(Handle hPlugin, int iNumParams) 374 | { 375 | int iLength = GetNativeCell(3); 376 | char[] sBuffer = new char[iLength]; 377 | 378 | Function_GetFunctionName(GetNativeCell(1), sBuffer, iLength); 379 | SetNativeString(2, sBuffer, iLength); 380 | return 0; 381 | } 382 | 383 | public any Native_Function_SetFunctionName(Handle hPlugin, int iNumParams) 384 | { 385 | Function_SetFunctionName(GetNativeCell(1), 2); 386 | return 0; 387 | } 388 | 389 | public any Native_Function_GetDescription(Handle hPlugin, int iNumParams) 390 | { 391 | int iLength = GetNativeCell(3); 392 | char[] sBuffer = new char[iLength]; 393 | 394 | Function_GetDescription(GetNativeCell(1), sBuffer, iLength); 395 | SetNativeString(2, sBuffer, iLength); 396 | return 0; 397 | } 398 | 399 | public any Native_Function_SetDescription(Handle hPlugin, int iNumParams) 400 | { 401 | Function_SetDescription(GetNativeCell(1), 2); 402 | return 0; 403 | } 404 | 405 | public any Native_Function_BindingGet(Handle hPlugin, int iNumParams) 406 | { 407 | return Function_GetBinding(GetNativeCell(1)); 408 | } 409 | 410 | public any Native_Function_FunctionGet(Handle hPlugin, int iNumParams) 411 | { 412 | return Function_GetFunction(GetNativeCell(1)); 413 | } 414 | 415 | public any Native_Function_FunctionSet(Handle hPlugin, int iNumParams) 416 | { 417 | Function_SetFunction(GetNativeCell(1), GetNativeCell(2)); 418 | return 0; 419 | } 420 | 421 | public any Native_Function_OffsetGet(Handle hPlugin, int iNumParams) 422 | { 423 | return Function_GetOffset(GetNativeCell(1)); 424 | } 425 | 426 | public any Native_Function_SetFunctionEmpty(Handle hPlugin, int iNumParams) 427 | { 428 | VScriptFunction pFunction = GetNativeCell(1); 429 | Function_SetFunctionEmpty(pFunction); 430 | Binding_SetCustom(pFunction); 431 | return 0; 432 | } 433 | 434 | public any Native_Function_ReturnGet(Handle hPlugin, int iNumParams) 435 | { 436 | return Function_GetReturnType(GetNativeCell(1)); 437 | } 438 | 439 | public any Native_Function_ReturnSet(Handle hPlugin, int iNumParams) 440 | { 441 | Function_SetReturnType(GetNativeCell(1), GetNativeCell(2)); 442 | return 0; 443 | } 444 | 445 | public any Native_Function_ParamCountGet(Handle hPlugin, int iNumParams) 446 | { 447 | return Function_GetParamCount(GetNativeCell(1)); 448 | } 449 | 450 | public any Native_Function_GetParam(Handle hPlugin, int iNumParams) 451 | { 452 | VScriptFunction pFunc = GetNativeCell(1); 453 | int iParam = GetNativeCell(2); 454 | int iCount = Function_GetParamCount(pFunc); 455 | 456 | if (iParam <= 0 || iParam > iCount) 457 | return ThrowNativeError(SP_ERROR_NATIVE, "Parameter number '%d' out of range (max '%d')", iParam, iCount); 458 | 459 | return Function_GetParam(pFunc, iParam - 1); 460 | } 461 | 462 | public any Native_Function_SetParam(Handle hPlugin, int iNumParams) 463 | { 464 | Function_SetParam(GetNativeCell(1), GetNativeCell(2) - 1, GetNativeCell(3)); 465 | return 0; 466 | } 467 | 468 | public any Native_Function_CopyFrom(Handle hPlugin, int iNumParams) 469 | { 470 | Function_CopyFrom(GetNativeCell(1), GetNativeCell(2)); 471 | return 0; 472 | } 473 | 474 | public any Native_Function_Register(Handle hPlugin, int iNumParams) 475 | { 476 | VScriptFunction pFunction = GetNativeCell(1); 477 | 478 | char sBuffer[64]; 479 | Function_GetScriptName(pFunction, sBuffer, sizeof(sBuffer)); 480 | if (!sBuffer[0]) 481 | ThrowNativeError(SP_ERROR_NATIVE, "Function must have script name set before registering it"); 482 | 483 | if (Function_GetFunction(pFunction) == Address_Null) 484 | ThrowNativeError(SP_ERROR_NATIVE, "Function must have address set before registering it"); 485 | 486 | // Is function already registered? 487 | ArrayList aList = List_GetAllGlobalFunctions(); 488 | if (aList.FindValue(pFunction) != -1) 489 | return 0; // Silently do nothing 490 | 491 | Function_Register(pFunction); 492 | return 0; 493 | } 494 | 495 | public any Native_Function_CreateSDKCall(Handle hPlugin, int iNumParams) 496 | { 497 | Handle hSDKCall = Function_CreateSDKCall(GetNativeCell(1)); 498 | 499 | Handle hClone = CloneHandle(hSDKCall, hPlugin); 500 | delete hSDKCall; 501 | return hClone; 502 | } 503 | 504 | public any Native_Function_CreateDetour(Handle hPlugin, int iNumParams) 505 | { 506 | DynamicDetour hDetour = Function_CreateDetour(GetNativeCell(1)); 507 | 508 | DynamicDetour hClone = view_as(CloneHandle(hDetour, hPlugin)); 509 | delete hDetour; 510 | return hClone; 511 | } 512 | 513 | public any Native_Function_CreateHook(Handle hPlugin, int iNumParams) 514 | { 515 | DynamicHook hHook = Function_CreateHook(GetNativeCell(1)); 516 | if (!hHook) 517 | return hHook; 518 | 519 | DynamicHook hClone = view_as(CloneHandle(hHook, hPlugin)); 520 | delete hHook; 521 | return hClone; 522 | } 523 | 524 | public any Native_Function_ClassGet(Handle hPlugin, int iNumParams) 525 | { 526 | return List_GetClassFromFunction(GetNativeCell(1)); 527 | } 528 | 529 | public any Native_Class_GetScriptName(Handle hPlugin, int iNumParams) 530 | { 531 | int iLength = GetNativeCell(3); 532 | char[] sBuffer = new char[iLength]; 533 | 534 | Class_GetScriptName(GetNativeCell(1), sBuffer, iLength); 535 | SetNativeString(2, sBuffer, iLength); 536 | return 0; 537 | } 538 | 539 | public any Native_Class_SetScriptName(Handle hPlugin, int iNumParams) 540 | { 541 | VScriptClass pClass = GetNativeCell(1); 542 | 543 | int iLength; 544 | GetNativeStringLength(2, iLength); 545 | 546 | char[] sBuffer = new char[iLength + 1]; 547 | GetNativeString(2, sBuffer, iLength + 1); 548 | 549 | // Check if script name not already exist 550 | if (List_GetClass(sBuffer)) 551 | ThrowNativeError(SP_ERROR_NATIVE, "Global function named '%s' already exists", sBuffer); 552 | 553 | Class_SetScriptName(pClass, 2); 554 | return 0; 555 | } 556 | 557 | public any Native_Class_GetClassName(Handle hPlugin, int iNumParams) 558 | { 559 | int iLength = GetNativeCell(3); 560 | char[] sBuffer = new char[iLength]; 561 | 562 | Class_GetClassName(GetNativeCell(1), sBuffer, iLength); 563 | SetNativeString(2, sBuffer, iLength); 564 | return 0; 565 | } 566 | 567 | public any Native_Class_SetClassName(Handle hPlugin, int iNumParams) 568 | { 569 | // Could add an already exist check like SetScriptName, meh 570 | Class_SetClassName(GetNativeCell(1), 2); 571 | return 0; 572 | } 573 | 574 | public any Native_Class_GetDescription(Handle hPlugin, int iNumParams) 575 | { 576 | int iLength = GetNativeCell(3); 577 | char[] sBuffer = new char[iLength]; 578 | 579 | Class_GetDescription(GetNativeCell(1), sBuffer, iLength); 580 | SetNativeString(2, sBuffer, iLength); 581 | return 0; 582 | } 583 | 584 | public any Native_Class_SetDescription(Handle hPlugin, int iNumParams) 585 | { 586 | Class_SetDescription(GetNativeCell(1), 2); 587 | return 0; 588 | } 589 | 590 | public any Native_Class_GetAllFunctions(Handle hPlugin, int iNumParams) 591 | { 592 | ArrayList aList = Class_GetAllFunctions(GetNativeCell(1)); 593 | 594 | ArrayList aClone = view_as(CloneHandle(aList, hPlugin)); 595 | delete aList; 596 | return aClone; 597 | } 598 | 599 | public any Native_Class_GetFunction(Handle hPlugin, int iNumParams) 600 | { 601 | int iLength; 602 | GetNativeStringLength(2, iLength); 603 | 604 | char[] sBuffer = new char[iLength + 1]; 605 | GetNativeString(2, sBuffer, iLength + 1); 606 | 607 | return Class_GetFunctionFromName(GetNativeCell(1), sBuffer); 608 | } 609 | 610 | public any Native_Class_CreateFunction(Handle hPlugin, int iNumParams) 611 | { 612 | return Class_CreateFunction(GetNativeCell(1)); 613 | } 614 | 615 | public any Native_Class_RegisterInstance(Handle hPlugin, int iNumParams) 616 | { 617 | int iLength; 618 | GetNativeStringLength(2, iLength); 619 | 620 | char[] sBuffer = new char[iLength + 1]; 621 | GetNativeString(2, sBuffer, iLength + 1); 622 | 623 | // Second param is void *, but we can just pass string to it 624 | HSCRIPT pInstance = SDKCall(g_hSDKCallRegisterInstance, GetScriptVM(), GetNativeCell(1), sBuffer); 625 | 626 | // Not sure if this is the correct way to do it, but it works 627 | ScriptVariant_t pValue = new ScriptVariant_t(); 628 | pValue.nType = FIELD_HSCRIPT; 629 | pValue.nValue = pInstance; 630 | HScript_SetValue(HSCRIPT_RootTable, sBuffer, pValue); 631 | 632 | return pInstance; 633 | } 634 | 635 | public any Native_Class_BaseGet(Handle hPlugin, int iNumParams) 636 | { 637 | return Class_GetBaseDesc(GetNativeCell(1)); 638 | } 639 | 640 | public any Native_Class_IsDerivedFrom(Handle hPlugin, int iNumParams) 641 | { 642 | return Class_IsDerivedFrom(GetNativeCell(1), GetNativeCell(2)); 643 | } 644 | 645 | public any Native_Execute(Handle hPlugin, int iNumParams) 646 | { 647 | HSCRIPT hScript = GetNativeCell(1); 648 | HSCRIPT hScope = iNumParams > 1 ? GetNativeCell(2) : HSCRIPT_RootTable; 649 | 650 | VScriptExecute aExecute = Execute_Create(hScript, hScope); 651 | 652 | VScriptExecute aClone = view_as(CloneHandle(aExecute, hPlugin)); 653 | delete aExecute; 654 | return aClone; 655 | } 656 | 657 | public any Native_Execute_AddParam(Handle hPlugin, int iNumParams) 658 | { 659 | ExecuteParam param; 660 | param.nType = GetNativeCell(2); 661 | param.nValue = GetNativeCell(3); 662 | Execute_AddParam(GetNativeCell(1), param); 663 | return 0; 664 | } 665 | 666 | public any Native_Execute_AddParamString(Handle hPlugin, int iNumParams) 667 | { 668 | VScriptExecute aExecute = GetNativeCell(1); 669 | 670 | int iLength; 671 | GetNativeStringLength(3, iLength); 672 | 673 | char[] sBuffer = new char[iLength + 1]; 674 | GetNativeString(3, sBuffer, iLength + 1); 675 | 676 | ExecuteParam param; 677 | param.nType = GetNativeCell(2); 678 | int iParam = Execute_AddParam(aExecute, param); 679 | Execute_SetParamString(aExecute, iParam, sBuffer); 680 | return 0; 681 | } 682 | 683 | public any Native_Execute_AddParamVector(Handle hPlugin, int iNumParams) 684 | { 685 | ExecuteParam param; 686 | param.nType = GetNativeCell(2); 687 | GetNativeArray(3, param.vecValue, sizeof(param.vecValue)); 688 | Execute_AddParam(GetNativeCell(1), param); 689 | return 0; 690 | } 691 | 692 | public any Native_Execute_SetParam(Handle hPlugin, int iNumParams) 693 | { 694 | ExecuteParam param; 695 | param.nType = GetNativeCell(3); 696 | param.nValue = GetNativeCell(4); 697 | Execute_SetParam(GetNativeCell(1), GetNativeCell(2), param); 698 | return 0; 699 | } 700 | 701 | public any Native_Execute_SetParamString(Handle hPlugin, int iNumParams) 702 | { 703 | VScriptExecute aExecute = GetNativeCell(1); 704 | int iParam = GetNativeCell(2); 705 | 706 | int iLength; 707 | GetNativeStringLength(4, iLength); 708 | 709 | char[] sBuffer = new char[iLength + 1]; 710 | GetNativeString(4, sBuffer, iLength + 1); 711 | 712 | ExecuteParam param; 713 | param.nType = GetNativeCell(3); 714 | Execute_SetParam(aExecute, iParam, param); 715 | Execute_SetParamString(aExecute, iParam, sBuffer); 716 | return 0; 717 | } 718 | 719 | public any Native_Execute_SetParamVector(Handle hPlugin, int iNumParams) 720 | { 721 | ExecuteParam param; 722 | param.nType = GetNativeCell(3); 723 | GetNativeArray(4, param.vecValue, sizeof(param.vecValue)); 724 | Execute_SetParam(GetNativeCell(1), GetNativeCell(2), param); 725 | return 0; 726 | } 727 | 728 | public any Native_Execute_Execute(Handle hPlugin, int iNumParams) 729 | { 730 | return Execute_Execute(GetNativeCell(1)); 731 | } 732 | 733 | public any Native_Execute_ReturnTypeGet(Handle hPlugin, int iNumParams) 734 | { 735 | Execute execute; 736 | Execute_GetInfo(GetNativeCell(1), execute); 737 | 738 | return execute.nReturn.nType; 739 | } 740 | 741 | public any Native_Execute_ReturnValueGet(Handle hPlugin, int iNumParams) 742 | { 743 | Execute execute; 744 | Execute_GetInfo(GetNativeCell(1), execute); 745 | 746 | return execute.nReturn.nValue; 747 | } 748 | 749 | public any Native_Execute_GetReturnString(Handle hPlugin, int iNumParams) 750 | { 751 | int iLength = GetNativeCell(3); 752 | char[] sBuffer = new char[iLength]; 753 | Execute_GetParamString(GetNativeCell(1), 0, sBuffer, iLength); 754 | SetNativeString(2, sBuffer, iLength); 755 | return 0; 756 | } 757 | 758 | public any Native_Execute_GetReturnVector(Handle hPlugin, int iNumParams) 759 | { 760 | Execute execute; 761 | Execute_GetInfo(GetNativeCell(1), execute); 762 | float vecValue[3]; 763 | vecValue = execute.nReturn.vecValue; 764 | SetNativeArray(2, vecValue, sizeof(vecValue)); 765 | return 0; 766 | } 767 | 768 | public any Native_IsScriptVMInitialized(Handle hPlugin, int iNumParams) 769 | { 770 | return GetScriptVM() != Address_Null; 771 | } 772 | 773 | public any Native_ResetScriptVM(Handle hPlugin, int iNumParams) 774 | { 775 | if (!g_bAllowResetScriptVM) 776 | ThrowNativeError(SP_ERROR_NATIVE, "This feature is not supported in this game and operating system."); 777 | 778 | int iEntity = INVALID_ENT_REFERENCE; 779 | while ((iEntity = FindEntityByClassname(iEntity, "*")) != INVALID_ENT_REFERENCE) 780 | Entity_Clear(iEntity); 781 | 782 | GameSystem_ServerTerm(); 783 | GameSystem_ServerInit(); 784 | return 0; 785 | } 786 | 787 | public any Native_CompileScript(Handle hPlugin, int iNumParams) 788 | { 789 | int iScriptLength; 790 | GetNativeStringLength(1, iScriptLength); 791 | 792 | char[] sScript = new char[iScriptLength + 1]; 793 | GetNativeString(1, sScript, iScriptLength + 1); 794 | 795 | if (IsNativeParamNullString(2)) 796 | { 797 | return SDKCall(g_hSDKCallCompileScript, GetScriptVM(), sScript, 0); 798 | } 799 | else 800 | { 801 | int iIdLength; 802 | GetNativeStringLength(2, iIdLength); 803 | 804 | char[] sId = new char[iIdLength + 1]; 805 | GetNativeString(2, sId, iIdLength + 1); 806 | 807 | return SDKCall(g_hSDKCallCompileScript, GetScriptVM(), sScript, sId); 808 | } 809 | } 810 | 811 | public any Native_CompileScriptFile(Handle hPlugin, int iNumParams) 812 | { 813 | char sFilepath[PLATFORM_MAX_PATH]; 814 | GetNativeString(1, sFilepath, sizeof(sFilepath)); 815 | Format(sFilepath, sizeof(sFilepath), "scripts/vscripts/%s", sFilepath); 816 | 817 | int iLength = FileSize(sFilepath); 818 | if (iLength == -1) 819 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid vscript filepath '%s'", sFilepath); 820 | 821 | char[] sScript = new char[iLength + 1]; 822 | File hFile = OpenFile(sFilepath, "r"); 823 | if (!hFile) 824 | ThrowNativeError(SP_ERROR_NATIVE, "Could not open vscript file '%s'", sFilepath); 825 | 826 | hFile.ReadString(sScript, iLength + 1); 827 | 828 | delete hFile; 829 | 830 | int iIndex = FindCharInString(sFilepath, '\\', true); 831 | if (iIndex == -1) 832 | iIndex = FindCharInString(sFilepath, '/', true); 833 | 834 | if (iIndex == -1) 835 | { 836 | return SDKCall(g_hSDKCallCompileScript, GetScriptVM(), sScript, 0); 837 | } 838 | else 839 | { 840 | char sId[PLATFORM_MAX_PATH]; 841 | Format(sId, sizeof(sId), sFilepath[iIndex + 1]); 842 | return SDKCall(g_hSDKCallCompileScript, GetScriptVM(), sScript, sId); 843 | } 844 | } 845 | 846 | public any Native_CreateScope(Handle hPlugin, int iNumParams) 847 | { 848 | int iLength; 849 | GetNativeStringLength(1, iLength); 850 | 851 | char[] sName = new char[iLength + 1]; 852 | GetNativeString(1, sName, iLength + 1); 853 | 854 | return HScript_CreateScope(sName, GetNativeCell(2)); 855 | } 856 | 857 | public any Native_CreateTable(Handle hPlugin, int iNumParams) 858 | { 859 | return HScript_CreateTable(); 860 | } 861 | 862 | public any Native_CreateInstance(Handle hPlugin, int iNumParams) 863 | { 864 | return HScript_CreateInstance(GetNativeCell(1), GetNativeCell(2)); 865 | } 866 | 867 | public any Native_GetAllClasses(Handle hPlugin, int iNumParams) 868 | { 869 | ArrayList aList = List_GetAllClasses().Clone(); 870 | 871 | ArrayList aClone = view_as(CloneHandle(aList, hPlugin)); 872 | delete aList; 873 | return aClone; 874 | } 875 | 876 | public any Native_GetClass(Handle hPlugin, int iNumParams) 877 | { 878 | int iLength; 879 | GetNativeStringLength(1, iLength); 880 | 881 | char[] sBuffer = new char[iLength + 1]; 882 | GetNativeString(1, sBuffer, iLength + 1); 883 | 884 | VScriptClass pClass = List_GetClass(sBuffer); 885 | if (!pClass) 886 | return ThrowNativeError(SP_ERROR_NATIVE, "Could not find class name '%s'", sBuffer); 887 | 888 | return pClass; 889 | } 890 | 891 | public any Native_CreateClass(Handle hPlugin, int iNumParams) 892 | { 893 | int iLength; 894 | GetNativeStringLength(1, iLength); 895 | 896 | char[] sBuffer = new char[iLength + 1]; 897 | GetNativeString(1, sBuffer, iLength + 1); 898 | 899 | VScriptClass pClass = List_GetClass(sBuffer); 900 | if (pClass) 901 | return pClass; 902 | 903 | pClass = Class_Create(); 904 | Class_Init(pClass); 905 | Class_SetScriptName(pClass, 1); 906 | Class_SetClassName(pClass, 1); 907 | return pClass; 908 | } 909 | 910 | public any Native_GetClassFunction(Handle hPlugin, int iNumParams) 911 | { 912 | int iClassNameLength, iFunctionNameLength; 913 | GetNativeStringLength(1, iClassNameLength); 914 | GetNativeStringLength(2, iFunctionNameLength); 915 | 916 | char[] sNativeClass = new char[iClassNameLength + 1]; 917 | char[] sNativeFunction = new char[iFunctionNameLength + 1]; 918 | 919 | GetNativeString(1, sNativeClass, iClassNameLength + 1); 920 | GetNativeString(2, sNativeFunction, iFunctionNameLength + 1); 921 | 922 | VScriptClass pClass = List_GetClass(sNativeClass); 923 | if (!pClass) 924 | return ThrowNativeError(SP_ERROR_NATIVE, "Could not find class name '%s'", sNativeClass); 925 | 926 | return Class_GetFunctionFromName(pClass, sNativeFunction); 927 | } 928 | 929 | public any Native_GetAllGlobalFunctions(Handle hPlugin, int iNumParams) 930 | { 931 | ArrayList aList = List_GetAllGlobalFunctions().Clone(); 932 | 933 | ArrayList aClone = view_as(CloneHandle(aList, hPlugin)); 934 | delete aList; 935 | return aClone; 936 | } 937 | 938 | public any Native_GetGlobalFunction(Handle hPlugin, int iNumParams) 939 | { 940 | int iLength; 941 | GetNativeStringLength(1, iLength); 942 | 943 | char[] sBuffer = new char[iLength + 1]; 944 | GetNativeString(1, sBuffer, iLength + 1); 945 | 946 | return List_GetFunction(sBuffer); 947 | } 948 | 949 | public any Native_CreateFunction(Handle hPlugin, int iNumParams) 950 | { 951 | VScriptFunction pFunction = Function_Create(); 952 | Function_Init(pFunction, false); 953 | return pFunction; 954 | } 955 | 956 | public any Native_GetEntityScriptScope(Handle hPlugin, int iNumParams) 957 | { 958 | int iEntity = GetNativeCell(1); 959 | if (!IsValidEntity(iEntity)) 960 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid entity index '%d'", iEntity); 961 | 962 | return Entity_GetScriptScope(iEntity); 963 | } 964 | 965 | public any Native_EntityToHScript(Handle hPlugin, int iNumParams) 966 | { 967 | int iEntity = GetNativeCell(1); 968 | if (iEntity == INVALID_ENT_REFERENCE) 969 | return Address_Null; // follows same way to how ToHScript handles it 970 | 971 | if (!IsValidEntity(iEntity)) 972 | ThrowNativeError(SP_ERROR_NATIVE, "Invalid entity index '%d'", iEntity); 973 | 974 | return Entity_GetScriptInstance(iEntity); 975 | } 976 | 977 | public any Native_HScriptToEntity(Handle hPlugin, int iNumParams) 978 | { 979 | HSCRIPT pHScript = GetNativeCell(1); 980 | if (pHScript == Address_Null) 981 | return INVALID_ENT_REFERENCE; // follows same way to how ToEnt handles it 982 | 983 | static Address pClassDesc; 984 | if (!pClassDesc) 985 | pClassDesc = List_GetClass("CBaseEntity"); 986 | 987 | if (!pClassDesc) 988 | ThrowError("Could not find script name CBaseEntity, file a bug report."); 989 | 990 | return SDKCall(g_hSDKCallGetInstanceEntity, GetScriptVM(), pHScript, pClassDesc); 991 | } 992 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------