mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2026-08-01 12:50:33 +00:00
Compare commits
98
Commits
v1.2.7
...
930175e564
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
930175e564 | ||
|
|
0b79c0c114 | ||
|
|
befebc4b6e | ||
|
|
933192724a | ||
|
|
64c1ab0120 | ||
|
|
ad466e020b | ||
|
|
5b4b06cb03 | ||
|
|
04871f2a3d | ||
|
|
0fedd81e1f | ||
|
|
a1d52b9472 | ||
|
|
50438cef27 | ||
|
|
b2bd1e0835 | ||
|
|
e2a5d96e10 | ||
|
|
8d4fa65e3b | ||
|
|
54226a0e56 | ||
|
|
1061c08829 | ||
|
|
c75cf561f7 | ||
|
|
3ea6050498 | ||
|
|
1551d7f1fd | ||
|
|
bf1d649bc1 | ||
|
|
71727a5197 | ||
|
|
530cd10466 | ||
|
|
978e251df8 | ||
|
|
cb96052638 | ||
|
|
9b217b20e3 | ||
|
|
136a723187 | ||
|
|
ad27dcb098 | ||
|
|
96acf0718e | ||
|
|
72f936e82a | ||
|
|
9a11ac82b3 | ||
|
|
8f36c271fd | ||
|
|
e952b8a0f7 | ||
|
|
f6d78fd815 | ||
|
|
cadcca22f9 | ||
|
|
c04a0a0d8c | ||
|
|
61706336ce | ||
|
|
eb08109392 | ||
|
|
b23cb2014b | ||
|
|
4d979bf659 | ||
|
|
206d06dc7d | ||
|
|
4d8af4bd45 | ||
|
|
46ab288488 | ||
|
|
dd8c1ff71e | ||
|
|
ed1ac62153 | ||
|
|
7f7fd016cf | ||
|
|
6c093888b5 | ||
|
|
1440760aa5 | ||
|
|
105c11a8b2 | ||
|
|
795f8ac162 | ||
|
|
6dd4d2651a | ||
|
|
b2f030f349 | ||
|
|
2690eeeece | ||
|
|
098bd21008 | ||
|
|
2f474e979f | ||
|
|
2ab4da0ad9 | ||
|
|
28c5594094 | ||
|
|
6525bbb8bf | ||
|
|
1a9e3dfb15 | ||
|
|
c62f1995e7 | ||
|
|
883e38ae58 | ||
|
|
1b2d4dbc46 | ||
|
|
7dfb35233e | ||
|
|
d842022056 | ||
|
|
0a8ebc3058 | ||
|
|
8ccae5ddec | ||
|
|
cfb143f0fd | ||
|
|
0281b5bef6 | ||
|
|
70a91b271d | ||
|
|
a7576ea765 | ||
|
|
3a492f51d9 | ||
|
|
d4ba37cde5 | ||
|
|
2ded4c6a2a | ||
|
|
24b3a9369e | ||
|
|
1d94fdd88d | ||
|
|
0af6b1cb13 | ||
|
|
d72c6f7057 | ||
|
|
39b9e7e352 | ||
|
|
36fefa6bb5 | ||
|
|
4ffdb331b4 | ||
|
|
7b995b3348 | ||
|
|
14d0f401fb | ||
|
|
df23604b1e | ||
|
|
e2960bc895 | ||
|
|
db90d361a9 | ||
|
|
6ec0c5e23c | ||
|
|
c84d681a3a | ||
|
|
62d1c8671b | ||
|
|
877adfa4b6 | ||
|
|
12fd44d0da | ||
|
|
12562eb8fd | ||
|
|
286481cbcd | ||
|
|
e9a877bd74 | ||
|
|
3060df1f05 | ||
|
|
aa1ae8c574 | ||
|
|
ccd8176c25
|
||
|
|
c230a1d916
|
||
|
|
2785a60dc0
|
||
|
|
1b65bda323 |
@@ -0,0 +1,7 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: gitsubmodule
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: daily
|
||||
open-pull-requests-limit: 10
|
||||
@@ -0,0 +1,80 @@
|
||||
name: Build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "*"
|
||||
pull_request:
|
||||
branches:
|
||||
- "*"
|
||||
types: [opened, synchronize, reopened]
|
||||
concurrency:
|
||||
group: ${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
jobs:
|
||||
build:
|
||||
name: Build binaries
|
||||
runs-on: windows-2022
|
||||
strategy:
|
||||
matrix:
|
||||
configuration:
|
||||
- Debug
|
||||
- Release
|
||||
steps:
|
||||
- name: Check out files
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
fetch-depth: 0
|
||||
lfs: false
|
||||
|
||||
- name: Add msbuild to PATH
|
||||
uses: microsoft/setup-msbuild@v2
|
||||
|
||||
- name: Generate project files
|
||||
run: tools/premake5 vs2022
|
||||
|
||||
- name: Set up problem matching
|
||||
uses: ammaraskar/msvc-problem-matcher@master
|
||||
|
||||
- name: Build ${{matrix.configuration}} binaries
|
||||
run: msbuild /m /v:minimal /p:Configuration=${{matrix.configuration}} /p:PlatformTarget=x86 build/iw5-gsc-utils.sln
|
||||
|
||||
- name: Upload ${{matrix.configuration}} binaries
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{matrix.configuration}} binaries
|
||||
path: |
|
||||
build/bin/${{matrix.configuration}}/iw5-gsc-utils.dll
|
||||
deploy:
|
||||
name: Deploy artifacts
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/main')
|
||||
steps:
|
||||
- name: Setup main environment
|
||||
if: github.ref == 'refs/heads/main'
|
||||
run: echo "ALICE_MASTER_PATH=${{ secrets.ALICE_MASTER_SSH_PATH }}" >> $GITHUB_ENV
|
||||
|
||||
- name: Download Release binaries
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: Release binaries
|
||||
|
||||
- name: Install SSH key
|
||||
uses: shimataro/[email protected]
|
||||
with:
|
||||
key: ${{ secrets.ALICE_MASTER_SSH_PRIVATE_KEY }}
|
||||
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'
|
||||
|
||||
- name: Add known hosts
|
||||
run: ssh-keyscan -H ${{ secrets.ALICE_MASTER_SSH_ADDRESS }} >> ~/.ssh/known_hosts
|
||||
|
||||
- name: Remove old data files
|
||||
run: ssh ${{ secrets.ALICE_MASTER_SSH_USER }}@${{ secrets.ALICE_MASTER_SSH_ADDRESS }} rm -rf ${{ env.ALICE_MASTER_PATH }}/iw5-gsc-utils/*
|
||||
|
||||
- name: Upload binary
|
||||
run: rsync -avz iw5-gsc-utils.dll ${{ secrets.ALICE_MASTER_SSH_USER }}@${{ secrets.ALICE_MASTER_SSH_ADDRESS }}:${{ env.ALICE_MASTER_PATH }}/iw5-gsc-utils/
|
||||
|
||||
- name: Publish changes
|
||||
run: ssh ${{ secrets.ALICE_MASTER_SSH_USER }}@${{ secrets.ALICE_MASTER_SSH_ADDRESS }} ${{ secrets.ALICE_MASTER_SSH_CHANGE_PUBLISH_COMMAND }}
|
||||
+3
-1
@@ -147,4 +147,6 @@ UpgradeLog*.htm
|
||||
user*.bat
|
||||
|
||||
# Premake binary
|
||||
#premake5.exe
|
||||
#premake5.exe
|
||||
|
||||
deps/mysql
|
||||
|
||||
+25
-6
@@ -4,12 +4,31 @@
|
||||
[submodule "deps/GSL"]
|
||||
path = deps/GSL
|
||||
url = https://github.com/microsoft/GSL.git
|
||||
[submodule "deps/sol2"]
|
||||
path = deps/sol2
|
||||
url = https://github.com/ThePhD/sol2.git
|
||||
[submodule "deps/lua"]
|
||||
path = deps/lua
|
||||
url = https://github.com/lua/lua.git
|
||||
[submodule "deps/json"]
|
||||
path = deps/json
|
||||
url = https://github.com/nlohmann/json.git
|
||||
branch = develop
|
||||
[submodule "deps/gsc-tool"]
|
||||
path = deps/gsc-tool
|
||||
url = https://github.com/xensik/gsc-tool.git
|
||||
[submodule "deps/zlib"]
|
||||
path = deps/zlib
|
||||
url = https://github.com/madler/zlib.git
|
||||
[submodule "deps/plutonium-sdk"]
|
||||
path = deps/plutonium-sdk
|
||||
url = https://github.com/plutoniummod/plutonium-sdk.git
|
||||
[submodule "deps/libtommath"]
|
||||
path = deps/libtommath
|
||||
url = https://github.com/libtom/libtommath.git
|
||||
[submodule "deps/libtomcrypt"]
|
||||
path = deps/libtomcrypt
|
||||
url = https://github.com/libtom/libtomcrypt.git
|
||||
[submodule "deps/sqlpp11"]
|
||||
path = deps/sqlpp11
|
||||
url = https://github.com/rbock/sqlpp11.git
|
||||
[submodule "deps/date"]
|
||||
path = deps/date
|
||||
url = https://github.com/HowardHinnant/date
|
||||
[submodule "deps/curl"]
|
||||
path = deps/curl
|
||||
url = https://github.com/curl/curl.git
|
||||
|
||||
@@ -2,22 +2,13 @@
|
||||
|
||||
This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
|
||||
# Installation
|
||||
* Download the latest version [iw5-gsc-utils.dll](https://github.alicent.cat/iw5-gsc-utils/iw5-gsc-utils.dll)
|
||||
* Copy it to `Plutonium/plugins/`
|
||||
|
||||
# Misc
|
||||
|
||||
* `executeCommand(command)`: Executes a console command.
|
||||
* `replaceFunc(what, with)`: Replaces a function with another:
|
||||
|
||||
```c
|
||||
init()
|
||||
{
|
||||
replaceFunc(maps\mp\gametypes\_damage::Callback_PlayerDamage, ::callbackPlayerDamage);
|
||||
}
|
||||
|
||||
callbackPlayerDamage(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset)
|
||||
{
|
||||
|
||||
}
|
||||
```
|
||||
* `addCommand(name, callback)`: Adds a console command (gets removed after a map restart):
|
||||
|
||||
```c
|
||||
@@ -44,9 +35,6 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
}
|
||||
```
|
||||
# Player
|
||||
* `say(message)`: Prints a message to all players' chat.
|
||||
|
||||
* `self tell(message)`: Prints a message to the player's chat.
|
||||
* `self setName(name)`: Sets a player's name.
|
||||
* `self resetName(name)`: Resets a player's name to its original.
|
||||
* `self setClantag(name)`: Sets a player's clantag.
|
||||
@@ -54,28 +42,7 @@ This plugin adds some useful functions/methods to IW5's GSC VM
|
||||
* `self removeClantag(name)`: Removes a player's clantag.
|
||||
# IO
|
||||
|
||||
The basepath for all IO functions is `Plutonium/storage/iw5`
|
||||
|
||||
* `fopen(path, mode)`: Opens a file of given name with given mode, returns a file stream.
|
||||
* `fwrite(stream, text)`: Writes a string to a stream.
|
||||
* `fread(stream)`: Reads entire file.
|
||||
* `fclose(stream)`: Closes a file stream.
|
||||
* `fremove(path)`: Deletes a file.
|
||||
|
||||
```c
|
||||
init()
|
||||
{
|
||||
basePath = getDvar("fs_basegame") + "/";
|
||||
|
||||
file = fopen(basePath + "test.txt", "w");
|
||||
fwrite(file, "test");
|
||||
fclose(file);
|
||||
|
||||
file = fopen(basePath + "test.txt", "r");
|
||||
print(fread(file));
|
||||
fclose(file);
|
||||
}
|
||||
```
|
||||
The basepath for all IO functions is `Plutonium/storage/iw5`.
|
||||
* `fileExists(path)`: Returns true if the file exists.
|
||||
* `writeFile(path, data[, append])`: Creates a file if it doesn't exist and writes/appends text to it.
|
||||
* `readFile(path)`: Reads a file.
|
||||
@@ -86,6 +53,19 @@ The basepath for all IO functions is `Plutonium/storage/iw5`
|
||||
* `listFiles(path)`: Returns the list of files in the directory as an array.
|
||||
* `copyFolder(source, target)`: Copies a folder.
|
||||
|
||||
# HTTP
|
||||
|
||||
* `httpGet(url)`: creates an HTTP request.
|
||||
|
||||
```c
|
||||
init()
|
||||
{
|
||||
req = httpget("http://example.com");
|
||||
req waittill("done", data);
|
||||
print(data);
|
||||
}
|
||||
```
|
||||
|
||||
# JSON
|
||||
|
||||
* `jsonSerialize(variable[, indent])`: Converts GSC variables (such as arrays) into JSON:
|
||||
|
||||
Vendored
+1
-1
Submodule deps/GSL updated: c31a9ad5e8...b39e7e4b09
+1
Submodule deps/curl added at cccd76f2aa
+1
Submodule deps/date added at c05cd34119
Vendored
+1
-1
Submodule deps/json updated: e10a3fac8a...d0d29039da
+1
Submodule deps/libtomcrypt added at a8ed78c2ca
+1
Submodule deps/libtommath added at ae40a87a92
Vendored
+1
-1
Submodule deps/minhook updated: 423d1e45af...d94c64d32e
+1
Submodule deps/plutonium-sdk added at 17e9a0a4d5
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
curl = {
|
||||
source = path.join(dependencies.basePath, "curl"),
|
||||
}
|
||||
|
||||
function curl.import()
|
||||
links { "curl" }
|
||||
|
||||
filter "toolset:msc*"
|
||||
links { "Crypt32.lib" }
|
||||
filter {}
|
||||
|
||||
curl.includes()
|
||||
end
|
||||
|
||||
function curl.includes()
|
||||
filter "toolset:msc*"
|
||||
includedirs {
|
||||
path.join(curl.source, "include"),
|
||||
}
|
||||
|
||||
defines {
|
||||
"CURL_STRICTER",
|
||||
"CURL_STATICLIB",
|
||||
"CURL_DISABLE_LDAP",
|
||||
}
|
||||
filter {}
|
||||
end
|
||||
|
||||
function curl.project()
|
||||
if not os.istarget("windows") then
|
||||
return
|
||||
end
|
||||
|
||||
project "curl"
|
||||
language "C"
|
||||
|
||||
curl.includes()
|
||||
|
||||
includedirs {
|
||||
path.join(curl.source, "lib"),
|
||||
}
|
||||
|
||||
files {
|
||||
path.join(curl.source, "lib/**.c"),
|
||||
path.join(curl.source, "lib/**.h"),
|
||||
}
|
||||
|
||||
defines {
|
||||
"BUILDING_LIBCURL",
|
||||
}
|
||||
|
||||
filter "toolset:msc*"
|
||||
|
||||
defines {
|
||||
"USE_SCHANNEL",
|
||||
"USE_WINDOWS_SSPI",
|
||||
"USE_THREADS_WIN32",
|
||||
}
|
||||
|
||||
filter "toolset:not msc*"
|
||||
|
||||
defines {
|
||||
"USE_GNUTLS",
|
||||
"USE_THREADS_POSIX",
|
||||
}
|
||||
|
||||
filter {}
|
||||
|
||||
warnings "Off"
|
||||
kind "StaticLib"
|
||||
end
|
||||
|
||||
table.insert(dependencies, curl)
|
||||
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
libtomcrypt = {
|
||||
source = path.join(dependencies.basePath, "libtomcrypt"),
|
||||
}
|
||||
|
||||
function libtomcrypt.import()
|
||||
links {
|
||||
"libtomcrypt"
|
||||
}
|
||||
|
||||
libtomcrypt.includes()
|
||||
end
|
||||
|
||||
function libtomcrypt.includes()
|
||||
includedirs {
|
||||
path.join(libtomcrypt.source, "src/headers")
|
||||
}
|
||||
|
||||
defines {
|
||||
"LTC_NO_FAST",
|
||||
"LTC_NO_PROTOTYPES",
|
||||
"LTC_NO_RSA_BLINDING",
|
||||
}
|
||||
end
|
||||
|
||||
function libtomcrypt.project()
|
||||
project "libtomcrypt"
|
||||
language "C"
|
||||
|
||||
libtomcrypt.includes()
|
||||
libtommath.import()
|
||||
|
||||
files {
|
||||
path.join(libtomcrypt.source, "src/**.c"),
|
||||
}
|
||||
|
||||
removefiles {
|
||||
path.join(libtomcrypt.source, "src/**/*tab.c"),
|
||||
path.join(libtomcrypt.source, "src/encauth/ocb3/**.c"),
|
||||
}
|
||||
|
||||
defines {
|
||||
"_CRT_SECURE_NO_WARNINGS",
|
||||
"LTC_SOURCE",
|
||||
"_LIB",
|
||||
"USE_LTM"
|
||||
}
|
||||
|
||||
removedefines {
|
||||
"_DLL",
|
||||
"_USRDLL"
|
||||
}
|
||||
|
||||
linkoptions {
|
||||
"-IGNORE:4221"
|
||||
}
|
||||
|
||||
warnings "Off"
|
||||
kind "StaticLib"
|
||||
end
|
||||
|
||||
table.insert(dependencies, libtomcrypt)
|
||||
Vendored
+52
@@ -0,0 +1,52 @@
|
||||
libtommath = {
|
||||
source = path.join(dependencies.basePath, "libtommath"),
|
||||
}
|
||||
|
||||
function libtommath.import()
|
||||
links {
|
||||
"libtommath"
|
||||
}
|
||||
|
||||
libtommath.includes()
|
||||
end
|
||||
|
||||
function libtommath.includes()
|
||||
includedirs {
|
||||
libtommath.source
|
||||
}
|
||||
|
||||
defines {
|
||||
"LTM_DESC",
|
||||
"__STDC_IEC_559__",
|
||||
"MP_NO_DEV_URANDOM",
|
||||
}
|
||||
end
|
||||
|
||||
function libtommath.project()
|
||||
project "libtommath"
|
||||
language "C"
|
||||
|
||||
libtommath.includes()
|
||||
|
||||
files {
|
||||
path.join(libtommath.source, "*.c"),
|
||||
}
|
||||
|
||||
defines {
|
||||
"_LIB"
|
||||
}
|
||||
|
||||
removedefines {
|
||||
"_DLL",
|
||||
"_USRDLL"
|
||||
}
|
||||
|
||||
linkoptions {
|
||||
"-IGNORE:4221"
|
||||
}
|
||||
|
||||
warnings "Off"
|
||||
kind "StaticLib"
|
||||
end
|
||||
|
||||
table.insert(dependencies, libtommath)
|
||||
Vendored
+52
@@ -0,0 +1,52 @@
|
||||
if (os.host() ~= "windows") then
|
||||
error("automatic mysql installation is not supported on your os")
|
||||
end
|
||||
|
||||
mysql = {
|
||||
source = path.join(dependencies.basePath, "mysql"),
|
||||
version = "5.7.43",
|
||||
download = "https://cdn.alicent.cat/mysql-5.7.43-win32.zip",
|
||||
}
|
||||
|
||||
function mysql.install()
|
||||
local hfile = io.open(string.format("%s/include/mysql.h", mysql.source), "r")
|
||||
if (hfile) then
|
||||
return
|
||||
end
|
||||
|
||||
os.execute(string.format("mkdir \"%s\" 2> nul", mysql.source))
|
||||
|
||||
local folder = path.join(mysql.source, "mysql-5.7.43-win32")
|
||||
local archive = path.join(mysql.source, "mysql-5.7.43-win32.zip")
|
||||
|
||||
print("Downloading MYSQL")
|
||||
os.execute(string.format("curl \"%s\" -L -o \"%s\"", mysql.download, archive))
|
||||
|
||||
os.execute(string.format("powershell -command \"Expand-Archive -Force \\\"%s\\\" \\\"%s\\\"\"", archive, mysql.source))
|
||||
os.execute(string.format("powershell -command \"mv \\\"%s/*\\\" \\\"%s\\\"\"", folder, mysql.source))
|
||||
os.execute(string.format("powershell -command \"rm \\\"%s\\\"\"", archive))
|
||||
os.execute(string.format("rmdir \"%s\"", folder))
|
||||
end
|
||||
|
||||
function mysql.import()
|
||||
mysql.install()
|
||||
mysql.includes()
|
||||
end
|
||||
|
||||
function mysql.includes()
|
||||
includedirs {
|
||||
path.join(mysql.source, "include"),
|
||||
}
|
||||
end
|
||||
|
||||
function mysql.project()
|
||||
project "mysql"
|
||||
language "C"
|
||||
|
||||
mysql.includes()
|
||||
|
||||
warnings "Off"
|
||||
kind "StaticLib"
|
||||
end
|
||||
|
||||
table.insert(dependencies, mysql)
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
plutonium_sdk = {
|
||||
source = path.join(dependencies.basePath, "plutonium-sdk"),
|
||||
}
|
||||
|
||||
function plutonium_sdk.import()
|
||||
plutonium_sdk.includes()
|
||||
end
|
||||
|
||||
function plutonium_sdk.includes()
|
||||
includedirs {
|
||||
plutonium_sdk.source,
|
||||
}
|
||||
end
|
||||
|
||||
function plutonium_sdk.project()
|
||||
end
|
||||
|
||||
table.insert(dependencies, plutonium_sdk)
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
sqlpp11 = {
|
||||
source = path.join(dependencies.basePath, "sqlpp11"),
|
||||
}
|
||||
|
||||
function sqlpp11.import()
|
||||
sqlpp11.includes()
|
||||
end
|
||||
|
||||
function sqlpp11.includes()
|
||||
includedirs {
|
||||
path.join(sqlpp11.source, "include"),
|
||||
path.join(dependencies.basePath, "date/include"),
|
||||
}
|
||||
end
|
||||
|
||||
function sqlpp11.project()
|
||||
project "sqlpp11"
|
||||
language "C++"
|
||||
|
||||
sqlpp11.includes()
|
||||
|
||||
warnings "Off"
|
||||
kind "StaticLib"
|
||||
end
|
||||
|
||||
table.insert(dependencies, sqlpp11)
|
||||
+1
Submodule deps/sqlpp11 added at 8c05983a31
+2
-1
@@ -1,2 +1,3 @@
|
||||
@echo off
|
||||
tools\windows\premake5.exe vs2019
|
||||
call git submodule update --init --recursive
|
||||
tools\premake5.exe vs2022
|
||||
|
||||
+34
-19
@@ -36,32 +36,35 @@ workspace "iw5-gsc-utils"
|
||||
targetdir "%{wks.location}/bin/%{cfg.buildcfg}"
|
||||
targetname "%{prj.name}"
|
||||
|
||||
configurations { "Debug", "Release", }
|
||||
|
||||
language "C++"
|
||||
cppdialect "C++20"
|
||||
|
||||
architecture "x86"
|
||||
|
||||
buildoptions "/std:c++latest"
|
||||
systemversion "latest"
|
||||
symbols "On"
|
||||
staticruntime "On"
|
||||
editandcontinue "Off"
|
||||
warnings "Extra"
|
||||
characterset "ASCII"
|
||||
|
||||
flags
|
||||
{
|
||||
"NoIncrementalLink",
|
||||
"MultiProcessorCompile",
|
||||
}
|
||||
|
||||
configurations { "Debug", "Release", }
|
||||
|
||||
symbols "On"
|
||||
|
||||
configuration "Release"
|
||||
filter "configurations:Release"
|
||||
optimize "Full"
|
||||
defines { "NDEBUG" }
|
||||
configuration{}
|
||||
filter {}
|
||||
|
||||
configuration "Debug"
|
||||
filter "configurations:Debug"
|
||||
optimize "Debug"
|
||||
defines { "DEBUG", "_DEBUG" }
|
||||
configuration {}
|
||||
filter {}
|
||||
|
||||
startproject "iw5-gsc-utils"
|
||||
|
||||
@@ -72,18 +75,30 @@ workspace "iw5-gsc-utils"
|
||||
pchheader "stdinc.hpp"
|
||||
pchsource "src/stdinc.cpp"
|
||||
|
||||
includedirs
|
||||
{
|
||||
"src"
|
||||
}
|
||||
|
||||
files
|
||||
{
|
||||
"src/**.h",
|
||||
"src/**.hpp",
|
||||
"src/**.cpp"
|
||||
files
|
||||
{
|
||||
"./src/**.h",
|
||||
"./src/**.hpp",
|
||||
"./src/**.cpp",
|
||||
"./src/**.rc",
|
||||
}
|
||||
|
||||
includedirs
|
||||
{
|
||||
"%{prj.location}/src",
|
||||
"./src",
|
||||
"./deps/mysql/include"
|
||||
}
|
||||
|
||||
libdirs {"./deps/mysql/lib"}
|
||||
|
||||
resincludedirs
|
||||
{
|
||||
"$(ProjectDir)src"
|
||||
}
|
||||
|
||||
linkoptions {"/DELAYLOAD:libmysql.dll"}
|
||||
|
||||
dependencies.imports()
|
||||
|
||||
group "Dependencies"
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
namespace command
|
||||
{
|
||||
std::unordered_map<std::string, std::function<void(params&)>> handlers;
|
||||
std::vector<std::string> script_commands;
|
||||
utils::memory::allocator allocator;
|
||||
|
||||
void main_handler()
|
||||
{
|
||||
@@ -46,9 +48,14 @@ namespace command
|
||||
|
||||
for (auto i = index; i < this->size(); i++)
|
||||
{
|
||||
if (i > index) result.append(" ");
|
||||
if (i > index)
|
||||
{
|
||||
result.append(" ");
|
||||
}
|
||||
|
||||
result.append(this->get(i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -69,14 +76,11 @@ namespace command
|
||||
handlers[command] = callback;
|
||||
}
|
||||
|
||||
std::vector<std::string> script_commands;
|
||||
utils::memory::allocator allocator;
|
||||
|
||||
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback)
|
||||
{
|
||||
script_commands.push_back(name);
|
||||
const auto _name = allocator.duplicate_string(name);
|
||||
add(_name, callback);
|
||||
const auto name_str = allocator.duplicate_string(name);
|
||||
add(name_str, callback);
|
||||
}
|
||||
|
||||
void clear_script_commands()
|
||||
@@ -94,9 +98,8 @@ namespace command
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -25,4 +25,4 @@ namespace command
|
||||
|
||||
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback);
|
||||
void clear_script_commands();
|
||||
}
|
||||
}
|
||||
|
||||
+154
-237
@@ -6,7 +6,6 @@
|
||||
|
||||
#include "game/scripting/event.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
#include "game/scripting/array.hpp"
|
||||
#include "game/scripting/function.hpp"
|
||||
|
||||
@@ -14,56 +13,8 @@
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
std::unordered_map<unsigned, script_function> functions;
|
||||
std::unordered_map<unsigned, script_method> methods;
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string method_name(unsigned int id)
|
||||
{
|
||||
const auto map = *game::plutonium::method_map_rev;
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string function_name(unsigned int id)
|
||||
{
|
||||
const auto map = *game::plutonium::function_map_rev;
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function_args get_arguments()
|
||||
{
|
||||
std::vector<scripting::script_value> args;
|
||||
|
||||
const auto top = game::scr_VmPub->top;
|
||||
|
||||
for (auto i = 0; i < game::scr_VmPub->outparamcount; i++)
|
||||
{
|
||||
const auto value = game::scr_VmPub->top[-i];
|
||||
args.push_back(value);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
void return_value(const scripting::script_value& value)
|
||||
{
|
||||
if (game::scr_VmPub->outparamcount)
|
||||
@@ -74,236 +25,187 @@ namespace gsc
|
||||
scripting::push_value(value);
|
||||
}
|
||||
|
||||
auto function_map_start = 0x200;
|
||||
auto method_map_start = 0x8400;
|
||||
auto token_map_start = 0x8000;
|
||||
auto field_offset_start = 0xA000;
|
||||
auto field_offset_start = 0xA000u;
|
||||
|
||||
struct entity_field
|
||||
{
|
||||
std::string name;
|
||||
std::function<scripting::script_value(unsigned int entnum)> getter;
|
||||
std::function<void(unsigned int entnum, scripting::script_value)> setter;
|
||||
};
|
||||
|
||||
std::vector<std::function<void()>> post_load_callbacks;
|
||||
std::unordered_map<unsigned int, std::unordered_map<unsigned int, entity_field>> custom_fields;
|
||||
|
||||
void call_function(unsigned int id)
|
||||
{
|
||||
if (id < 0x200)
|
||||
{
|
||||
return reinterpret_cast<builtin_function*>(game::plutonium::function_table.get())[id]();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const auto result = functions[id](get_arguments());
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
if (type)
|
||||
{
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing function %s:\n", function_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
}
|
||||
}
|
||||
|
||||
void call_method(game::scr_entref_t ent, unsigned int id)
|
||||
{
|
||||
if (id < 0x8400)
|
||||
{
|
||||
return reinterpret_cast<builtin_method*>(game::plutonium::method_table.get())[id - 0x8000](ent);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const auto result = methods[id](ent, get_arguments());
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
if (type)
|
||||
{
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing method %s:\n", method_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
}
|
||||
}
|
||||
|
||||
__declspec(naked) void call_builtin_stub()
|
||||
{
|
||||
__asm
|
||||
{
|
||||
push eax
|
||||
|
||||
mov eax, 0x20B4A5C
|
||||
mov [eax], esi
|
||||
|
||||
mov eax, 0x20B4A90
|
||||
mov [eax], edx
|
||||
|
||||
pop eax
|
||||
|
||||
pushad
|
||||
push eax
|
||||
call call_function
|
||||
pop eax
|
||||
popad
|
||||
|
||||
push 0x56C900
|
||||
retn
|
||||
}
|
||||
}
|
||||
|
||||
__declspec(naked) void call_builtin_method_stub()
|
||||
{
|
||||
__asm
|
||||
{
|
||||
pushad
|
||||
push ecx
|
||||
push ebx
|
||||
call call_method
|
||||
pop ebx
|
||||
pop ecx
|
||||
popad
|
||||
|
||||
push ebx
|
||||
add esp, 0xC
|
||||
|
||||
push 0x56CBE9
|
||||
retn
|
||||
}
|
||||
}
|
||||
std::unordered_map<unsigned int, entity_field_t> custom_fields[class_id_t::class_count];
|
||||
|
||||
utils::hook::detour scr_get_object_field_hook;
|
||||
utils::hook::detour scr_set_object_field_hook;
|
||||
utils::hook::detour scr_post_load_scripts_hook;
|
||||
|
||||
const char* get_script_loc()
|
||||
{
|
||||
return "unknown location";
|
||||
}
|
||||
|
||||
void print_error(const char* fmt, ...)
|
||||
{
|
||||
static char buffer[0x1000];
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
_vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
printf("\n"
|
||||
"******* script runtime error *******\n"
|
||||
"%s\n"
|
||||
"\tat %s\n"
|
||||
"************************************",
|
||||
buffer, get_script_loc());
|
||||
}
|
||||
|
||||
const gsc::entity_field_t* find_field(unsigned int classnum, unsigned int offset)
|
||||
{
|
||||
const auto& class_map = custom_fields[classnum];
|
||||
const auto field_iter = class_map.find(offset);
|
||||
if (field_iter == class_map.end())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &field_iter->second;
|
||||
}
|
||||
|
||||
void scr_get_object_field_stub(unsigned int classnum, int entnum, unsigned int offset)
|
||||
{
|
||||
if (custom_fields[classnum].find(offset) == custom_fields[classnum].end())
|
||||
const auto field = find_field(classnum, offset);
|
||||
if (field == nullptr)
|
||||
{
|
||||
return scr_get_object_field_hook.invoke<void>(classnum, entnum, offset);
|
||||
}
|
||||
|
||||
const auto field = custom_fields[classnum][offset];
|
||||
|
||||
try
|
||||
{
|
||||
const auto result = field.getter(entnum);
|
||||
const auto result = field->getter(entnum);
|
||||
return_value(result);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error getting field %s:\n", field.name.data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
print_error("while getting builtin field \"%s\"\n%s", field->name.data(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
utils::hook::detour scr_set_object_field_hook;
|
||||
void scr_set_object_field_stub(unsigned int classnum, int entnum, unsigned int offset)
|
||||
{
|
||||
if (custom_fields[classnum].find(offset) == custom_fields[classnum].end())
|
||||
const auto field = find_field(classnum, offset);
|
||||
if (field == nullptr)
|
||||
{
|
||||
return scr_set_object_field_hook.invoke<void>(classnum, entnum, offset);
|
||||
}
|
||||
|
||||
const auto args = get_arguments();
|
||||
const auto field = custom_fields[classnum][offset];
|
||||
function_args args;
|
||||
|
||||
try
|
||||
{
|
||||
field.setter(entnum, args[0]);
|
||||
field->setter(entnum, args[0]);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error setting field %s:\n", field.name.data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
print_error("while setting builtin field \"%s\"\n%s", field->name.data(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
utils::hook::detour scr_post_load_scripts_hook;
|
||||
void scr_post_load_scripts_stub()
|
||||
{
|
||||
for (const auto& callback : post_load_callbacks)
|
||||
for (auto i = 0; i < class_id_t::class_count; i++)
|
||||
{
|
||||
callback();
|
||||
const auto& class_map = custom_fields[i];
|
||||
for (const auto& [offset, field] : class_map)
|
||||
{
|
||||
const auto str_id = game::SL_GetString(field.name.data(), 0);
|
||||
const auto canon_str = game::SL_GetCanonicalString(field.name.data());
|
||||
game::Scr_AddClassField(i, str_id, canon_str, offset);
|
||||
}
|
||||
}
|
||||
|
||||
return scr_post_load_scripts_hook.invoke<void>();
|
||||
}
|
||||
}
|
||||
|
||||
namespace function
|
||||
void call_function(const script_function& function, const std::string& name)
|
||||
{
|
||||
void add(const std::string& name, const script_function& func)
|
||||
try
|
||||
{
|
||||
const auto index = function_map_start++;
|
||||
function_args args;
|
||||
const auto result = function.operator()(args);
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
functions[index] = func;
|
||||
(*game::plutonium::function_map_rev)[name] = index;
|
||||
if (type)
|
||||
{
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
print_error("in call to builtin function \"%s\"\n%s", name.data(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
namespace method
|
||||
void call_method(const script_method& method, const std::string& name, game::scr_entref_t ent)
|
||||
{
|
||||
void add(const std::string& name, const script_method& func)
|
||||
try
|
||||
{
|
||||
const auto index = method_map_start++;
|
||||
function_args args;
|
||||
const auto result = method.operator()(ent, args);
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
methods[index] = func;
|
||||
(*game::plutonium::method_map_rev)[name] = index;
|
||||
if (type)
|
||||
{
|
||||
return_value(result);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
print_error("in call to builtin method \"%s\"\n%s", name.data(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
namespace field
|
||||
{
|
||||
void add(const classid classnum, const std::string& name,
|
||||
const std::function<scripting::script_value(unsigned int entnum)>& getter,
|
||||
const std::function<void(unsigned int entnum, const scripting::script_value&)>& setter)
|
||||
void add(const class_id_t classnum, const std::string& name, const field_getter_t getter, const field_setter_t& setter)
|
||||
{
|
||||
const auto token_id = token_map_start++;
|
||||
const auto offset = field_offset_start++;
|
||||
auto& class_map = custom_fields[classnum];
|
||||
|
||||
custom_fields[classnum][offset] = {name, getter, setter};
|
||||
(*game::plutonium::token_map_rev)[name] = token_id;
|
||||
|
||||
post_load_callbacks.push_back([classnum, name, token_id, offset]()
|
||||
{
|
||||
const auto name_str = game::SL_GetString(name.data(), 0);
|
||||
game::Scr_AddClassField(classnum, name_str, token_id, offset);
|
||||
});
|
||||
entity_field_t field{};
|
||||
field.name = name;
|
||||
field.getter = getter;
|
||||
field.setter = setter;
|
||||
class_map.insert(std::make_pair(offset, field));
|
||||
}
|
||||
}
|
||||
|
||||
function_args::function_args(std::vector<scripting::script_value> values)
|
||||
function_args::function_args()
|
||||
{
|
||||
for (auto i = 0u; i < game::scr_VmPub->outparamcount; i++)
|
||||
{
|
||||
const auto value = game::scr_VmPub->top[-i];
|
||||
this->values_.emplace_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
function_args::function_args(const std::vector<scripting::script_value>& values)
|
||||
: values_(values)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned int function_args::size() const
|
||||
std::uint32_t function_args::size() const
|
||||
{
|
||||
return this->values_.size();
|
||||
}
|
||||
|
||||
std::vector<scripting::script_value> function_args::get_raw() const
|
||||
const std::vector<scripting::script_value>& function_args::get_raw() const
|
||||
{
|
||||
return this->values_;
|
||||
}
|
||||
|
||||
scripting::value_wrap function_args::get(const int index) const
|
||||
std::vector<scripting::script_value>& function_args::get_raw()
|
||||
{
|
||||
return this->values_;
|
||||
}
|
||||
|
||||
scripting::value_wrap function_args::get(const std::uint32_t index) const
|
||||
{
|
||||
if (index >= this->values_.size())
|
||||
{
|
||||
@@ -316,13 +218,13 @@ namespace gsc
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scr_get_object_field_hook.create(0x52BDB0, scr_get_object_field_stub);
|
||||
scr_set_object_field_hook.create(0x52BCC0, scr_set_object_field_stub);
|
||||
scr_post_load_scripts_hook.create(0x628B50, scr_post_load_scripts_stub);
|
||||
|
||||
field::add(classid::entity, "flags",
|
||||
field::add(class_id_t::class_entity, "entityflags",
|
||||
[](unsigned int entnum) -> scripting::script_value
|
||||
{
|
||||
const auto entity = &game::g_entities[entnum];
|
||||
@@ -335,7 +237,7 @@ namespace gsc
|
||||
}
|
||||
);
|
||||
|
||||
field::add(classid::entity, "clientflags",
|
||||
field::add(class_id_t::class_entity, "clientflags",
|
||||
[](unsigned int entnum) -> scripting::script_value
|
||||
{
|
||||
const auto entity = &game::g_entities[entnum];
|
||||
@@ -348,13 +250,15 @@ namespace gsc
|
||||
}
|
||||
);
|
||||
|
||||
function::add("executecommand", [](const function_args& args) -> scripting::script_value
|
||||
function::add("executecommand", [](const function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
game::Cbuf_AddText(0, args[0].as<const char*>());
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("addcommand", [](const function_args& args) -> scripting::script_value
|
||||
function::add("addcommand", [](const function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
const auto function = args[1].as<scripting::function>();
|
||||
@@ -372,68 +276,81 @@ namespace gsc
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("say", [](const function_args& args) -> scripting::script_value
|
||||
function::add("dropallbots", [](const function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto message = args[0].as<std::string>();
|
||||
game::SV_GameSendServerCommand(-1, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
for (auto i = 0; i < *game::svs_clientCount; i++)
|
||||
{
|
||||
if (game::svs_clients[i].header.state != game::CS_FREE
|
||||
&& game::svs_clients[i].header.netchan.remoteAddress.type == game::NA_BOT)
|
||||
{
|
||||
game::SV_GameDropClient(i, "GAME_GET_TO_COVER");
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("tell", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
method::add("specialtymarathon", [](const game::scr_entref_t ent, const function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
const auto message = args[0].as<std::string>();
|
||||
|
||||
game::SV_GameSendServerCommand(client, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("specialtymarathon", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto num = ent.entnum;
|
||||
const auto toggle = args[0].as<int>();
|
||||
auto flags = game::g_entities[client].client->ps.perks[0];
|
||||
|
||||
auto g_client = game::g_entities[num].client;
|
||||
auto playerState = &g_client->ps;
|
||||
auto flags = playerState->perks[0];
|
||||
|
||||
playerState->perks[0] = toggle
|
||||
? flags | 0x4000u
|
||||
game::g_entities[client].client->ps.perks[0] = toggle
|
||||
? flags | 0x4000u
|
||||
: flags & ~0x4000u;
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("isbot", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
method::add("isbot", [](const game::scr_entref_t ent, const function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("entity is not a player");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
return game::svs_clients[client].bIsTestClient;
|
||||
});
|
||||
|
||||
utils::hook::jump(0x56C8EB, call_builtin_stub);
|
||||
utils::hook::jump(0x56CBDC, call_builtin_method_stub);
|
||||
method::add("arecontrolsfrozen", [](const game::scr_entref_t ent, const function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
return (game::g_entities[client].client->flags & 4) != 0;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+64
-17
@@ -4,25 +4,37 @@
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
enum classid
|
||||
enum class_id_t
|
||||
{
|
||||
entity,
|
||||
hudelem,
|
||||
pathnode,
|
||||
node,
|
||||
count
|
||||
class_entity,
|
||||
class_hudelem,
|
||||
class_pathnode,
|
||||
class_node,
|
||||
class_count
|
||||
};
|
||||
|
||||
using field_getter_t = std::function<scripting::script_value(unsigned int entnum)>;
|
||||
using field_setter_t = std::function<void(unsigned int entnum, const scripting::script_value&)>;
|
||||
|
||||
struct entity_field_t
|
||||
{
|
||||
std::string name;
|
||||
field_getter_t getter;
|
||||
field_setter_t setter;
|
||||
};
|
||||
|
||||
class function_args
|
||||
{
|
||||
public:
|
||||
function_args(std::vector<scripting::script_value>);
|
||||
function_args();
|
||||
function_args(const std::vector<scripting::script_value>&);
|
||||
|
||||
unsigned int size() const;
|
||||
std::vector<scripting::script_value> get_raw() const;
|
||||
scripting::value_wrap get(const int index) const;
|
||||
std::uint32_t size() const;
|
||||
const std::vector<scripting::script_value>& get_raw() const;
|
||||
std::vector<scripting::script_value>& get_raw();
|
||||
scripting::value_wrap get(const std::uint32_t index) const;
|
||||
|
||||
scripting::value_wrap operator[](const int index) const
|
||||
scripting::value_wrap operator[](const std::uint32_t index) const
|
||||
{
|
||||
return this->get(index);
|
||||
}
|
||||
@@ -36,20 +48,55 @@ namespace gsc
|
||||
using script_function = std::function<scripting::script_value(const function_args&)>;
|
||||
using script_method = std::function<scripting::script_value(const game::scr_entref_t, const function_args&)>;
|
||||
|
||||
void call_function(const script_function& function, const std::string& name);
|
||||
void call_method(const script_method& method, const std::string& name, game::scr_entref_t ent);
|
||||
|
||||
namespace function
|
||||
{
|
||||
void add(const std::string& name, const script_function& func);
|
||||
template <typename F>
|
||||
void add(const std::string& n, F&& f)
|
||||
{
|
||||
static auto done = false;
|
||||
assert(!done);
|
||||
done = true;
|
||||
|
||||
static struct
|
||||
{
|
||||
std::string name;
|
||||
script_function function;
|
||||
} ctx{n, f};
|
||||
|
||||
plugin::get()->get_interface()->gsc()->register_function(n, []()
|
||||
{
|
||||
call_function(ctx.function, ctx.name);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
namespace method
|
||||
{
|
||||
void add(const std::string& name, const script_method& func);
|
||||
template <typename F>
|
||||
void add(const std::string& n, F&& f)
|
||||
{
|
||||
static auto done = false;
|
||||
assert(!done);
|
||||
done = true;
|
||||
|
||||
static struct
|
||||
{
|
||||
std::string name;
|
||||
script_method function;
|
||||
} ctx{n, f};
|
||||
|
||||
plugin::get()->get_interface()->gsc()->register_method(n, [](plutonium::sdk::types::entref entref)
|
||||
{
|
||||
call_method(ctx.function, ctx.name, *reinterpret_cast<game::scr_entref_t*>(&entref));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
namespace field
|
||||
{
|
||||
void add(const classid classnum, const std::string& name,
|
||||
const std::function<scripting::script_value(unsigned int entnum)>& getter,
|
||||
const std::function<void(unsigned int entnum, const scripting::script_value&)>& setter);
|
||||
void add(const class_id_t classnum, const std::string& name, const field_getter_t getter, const field_setter_t& setter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "game/structs.hpp"
|
||||
#include "game/game.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
#include "scheduler.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
#include <utils/http.hpp>
|
||||
#include <utils/concurrency.hpp>
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace http
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr const auto max_result_size = 0x5000u;
|
||||
|
||||
struct http_request_params_t
|
||||
{
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string fields;
|
||||
utils::http::headers headers;
|
||||
};
|
||||
|
||||
struct http_request_t
|
||||
{
|
||||
http_request_params_t params;
|
||||
scripting::object handle;
|
||||
std::optional<utils::http::result> result;
|
||||
std::atomic_bool completed;
|
||||
};
|
||||
|
||||
std::vector<std::shared_ptr<http_request_t>> requests;
|
||||
|
||||
void notify_request_result(std::shared_ptr<http_request_t>& request)
|
||||
{
|
||||
const auto handle_id = request->handle.get_entity_id();
|
||||
|
||||
if (!request->result.has_value())
|
||||
{
|
||||
scripting::notify(handle_id, "done", {{}, false, "unknown error"});
|
||||
return;
|
||||
}
|
||||
|
||||
auto& result = request->result.value();
|
||||
const auto error = curl_easy_strerror(result.code);
|
||||
|
||||
if (result.code != CURLE_OK)
|
||||
{
|
||||
scripting::notify(handle_id, "done", {{}, false, error});
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.buffer.size() >= max_result_size)
|
||||
{
|
||||
printf("^3WARNING: http result size bigger than %i bytes (%i), truncating!", max_result_size,
|
||||
static_cast<int>(result.buffer.size()));
|
||||
result.buffer.resize(max_result_size);
|
||||
}
|
||||
|
||||
scripting::notify(handle_id, "done", {result.buffer, true});
|
||||
}
|
||||
|
||||
void check_requests()
|
||||
{
|
||||
for (auto i = requests.begin(); i != requests.end(); )
|
||||
{
|
||||
auto& request = *i;
|
||||
if (!request->completed)
|
||||
{
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
notify_request_result(request);
|
||||
i = requests.erase(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scripting::object create_request(const http_request_params_t& params)
|
||||
{
|
||||
const auto request = std::make_shared<http_request_t>();
|
||||
requests.emplace_back(request);
|
||||
|
||||
request->params = params;
|
||||
scheduler::thread_pool.push([request]
|
||||
{
|
||||
request->result = utils::http::get_data(
|
||||
request->params.url,
|
||||
request->params.fields,
|
||||
request->params.headers,
|
||||
request->params.method);
|
||||
request->completed = true;
|
||||
});
|
||||
|
||||
return request->handle;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void push_request(T&& r)
|
||||
{
|
||||
const auto request = std::make_shared<http_request_t>(std::forward<T>(r));
|
||||
requests.emplace_back(request);
|
||||
}
|
||||
|
||||
void wait_and_clear_requests()
|
||||
{
|
||||
for (auto& task : requests)
|
||||
{
|
||||
while (!task->completed)
|
||||
{
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
}
|
||||
|
||||
requests.clear();
|
||||
}
|
||||
|
||||
void parse_request_options(http_request_params_t& params, const scripting::array& options)
|
||||
{
|
||||
const auto fields = options["parameters"];
|
||||
const auto body = options["body"];
|
||||
const auto headers = options["headers"];
|
||||
const auto method = options["method"];
|
||||
|
||||
if (method.is<std::string>())
|
||||
{
|
||||
params.method = method.as<std::string>();
|
||||
}
|
||||
|
||||
if (fields.is<scripting::array>())
|
||||
{
|
||||
const auto fields_ = fields.as<scripting::array>();
|
||||
const auto keys = fields_.get_keys();
|
||||
|
||||
for (const auto& key : keys)
|
||||
{
|
||||
if (!key.is<std::string>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto key_ = key.as<std::string>();
|
||||
const auto value = fields_[key].to_string();
|
||||
params.fields += key_ + "=" + value + "&";
|
||||
}
|
||||
|
||||
}
|
||||
else if (body.is<std::string>())
|
||||
{
|
||||
params.fields = body.as<std::string>();
|
||||
}
|
||||
|
||||
if (headers.is<scripting::array>())
|
||||
{
|
||||
const auto headers_arr = headers.as<scripting::array>();
|
||||
const auto keys = headers_arr.get_keys();
|
||||
|
||||
for (const auto& key : keys)
|
||||
{
|
||||
if (!key.is<std::string>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto key_str = key.as<std::string>();
|
||||
const auto value = headers_arr[key].to_string();
|
||||
|
||||
params.headers[key_str] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void on_shutdown([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scripting::on_shutdown(wait_and_clear_requests);
|
||||
}
|
||||
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scheduler::loop(check_requests, scheduler::server);
|
||||
scripting::on_shutdown(wait_and_clear_requests);
|
||||
|
||||
gsc::function::add("httpget", [](const gsc::function_args& args)
|
||||
{
|
||||
http_request_params_t params{};
|
||||
params.url = args[0].as<std::string>();
|
||||
return create_request(params);
|
||||
});
|
||||
|
||||
gsc::function::add("httprequest", [](const gsc::function_args& args)
|
||||
{
|
||||
http_request_params_t params{};
|
||||
params.url = args[0].as<std::string>();
|
||||
|
||||
if (args.size() > 1)
|
||||
{
|
||||
const auto options = args[1].as<scripting::array>();
|
||||
parse_request_options(params, options);
|
||||
}
|
||||
|
||||
return create_request(params);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(http::component)
|
||||
+18
-81
@@ -4,85 +4,42 @@
|
||||
#include "scheduler.hpp"
|
||||
#include "gsc.hpp"
|
||||
#include "json.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
namespace io
|
||||
{
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_after_dvar_init([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
const auto path = game::Dvar_FindVar("fs_basegame")->current.string;
|
||||
std::filesystem::current_path(path);
|
||||
const auto fs_basegame = game::Dvar_FindVar("fs_basegame");
|
||||
if (fs_basegame == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gsc::function::add("jsonprint", [](const gsc::function_args& args) -> scripting::script_value
|
||||
printf("working directory: %s", fs_basegame->current.string);
|
||||
std::filesystem::current_path(fs_basegame->current.string);
|
||||
}
|
||||
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("jsonprint", [](const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
std::string buffer;
|
||||
|
||||
for (const auto arg : args.get_raw())
|
||||
for (const auto& arg : args.get_raw())
|
||||
{
|
||||
buffer.append(json::gsc_to_string(arg));
|
||||
buffer.append("\t");
|
||||
}
|
||||
|
||||
printf("%s\n", buffer.data());
|
||||
printf("%s", buffer.data());
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::function::add("fremove", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<const char*>();
|
||||
return std::remove(path);
|
||||
});
|
||||
|
||||
gsc::function::add("fopen", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto* path = args[0].as<const char*>();
|
||||
const auto* mode = args[1].as<const char*>();
|
||||
|
||||
const auto handle = fopen(path, mode);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
printf("fopen: Invalid path\n");
|
||||
}
|
||||
|
||||
return handle;
|
||||
});
|
||||
|
||||
gsc::function::add("fclose", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
return fclose(handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fwrite", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
const auto text = args[1].as<const char*>();
|
||||
|
||||
return fprintf(handle, text);
|
||||
});
|
||||
|
||||
gsc::function::add("fread", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
fseek(handle, 0, SEEK_END);
|
||||
const auto length = ftell(handle);
|
||||
|
||||
fseek(handle, 0, SEEK_SET);
|
||||
char* buffer = (char*)calloc(length, sizeof(char));
|
||||
|
||||
fread(buffer, sizeof(char), length, handle);
|
||||
|
||||
const std::string result = buffer;
|
||||
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
gsc::function::add("fileexists", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
@@ -155,28 +112,8 @@ namespace io
|
||||
|
||||
return scripting::script_value{};
|
||||
});
|
||||
|
||||
gsc::function::add("httpget", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto url = args[0].as<std::string>();
|
||||
const auto object = scripting::entity(scripting::make_object());
|
||||
|
||||
scheduler::once([object, url]()
|
||||
{
|
||||
const auto result = utils::http::get_data(url.data());
|
||||
scheduler::once([object, result]()
|
||||
{
|
||||
const auto value = result.has_value()
|
||||
? result.value().substr(0, 0x5000)
|
||||
: "";
|
||||
scripting::notify(object, "done", {value});
|
||||
});
|
||||
}, scheduler::pipeline::async);
|
||||
|
||||
return object;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(io::component)
|
||||
REGISTER_COMPONENT(io::component)
|
||||
|
||||
@@ -20,7 +20,8 @@ namespace json
|
||||
|
||||
auto string_indexed = -1;
|
||||
const auto keys = array.get_keys();
|
||||
for (auto i = 0; i < keys.size(); i++)
|
||||
|
||||
for (auto i = 0u; i < keys.size(); i++)
|
||||
{
|
||||
const auto is_int = keys[i].is<int>();
|
||||
const auto is_string = keys[i].is<std::string>();
|
||||
@@ -144,7 +145,7 @@ namespace json
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("array", [](const gsc::function_args& args)
|
||||
{
|
||||
@@ -194,4 +195,4 @@ namespace json
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(json::component)
|
||||
REGISTER_COMPONENT(json::component)
|
||||
|
||||
@@ -0,0 +1,556 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "mysql.hpp"
|
||||
#include "component/gsc.hpp"
|
||||
#include "component/scheduler.hpp"
|
||||
#include "component/scripting.hpp"
|
||||
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace mysql
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct mysql_result_t
|
||||
{
|
||||
MYSQL_RES* result;
|
||||
MYSQL_STMT* stmt;
|
||||
uint64_t affected_rows;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
struct task_t
|
||||
{
|
||||
scripting::object handle;
|
||||
mysql_result_t result{};
|
||||
std::atomic_bool completed;
|
||||
};
|
||||
|
||||
std::vector<std::shared_ptr<task_t>> tasks;
|
||||
std::array<connection, max_connections> connection_pool;
|
||||
|
||||
scripting::script_value field_to_value(const MYSQL_FIELD* field, const std::string& row)
|
||||
{
|
||||
switch (field->type)
|
||||
{
|
||||
case enum_field_types::MYSQL_TYPE_INT24:
|
||||
case enum_field_types::MYSQL_TYPE_LONG:
|
||||
case enum_field_types::MYSQL_TYPE_SHORT:
|
||||
return std::atoi(row.data());
|
||||
case enum_field_types::MYSQL_TYPE_LONGLONG:
|
||||
return row;
|
||||
case enum_field_types::MYSQL_TYPE_FLOAT:
|
||||
case enum_field_types::MYSQL_TYPE_DOUBLE:
|
||||
return static_cast<float>(std::atof(row.data()));
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
scripting::script_value bind_to_value(const MYSQL_BIND* bind)
|
||||
{
|
||||
switch (bind->buffer_type)
|
||||
{
|
||||
case enum_field_types::MYSQL_TYPE_INT24:
|
||||
case enum_field_types::MYSQL_TYPE_LONG:
|
||||
case enum_field_types::MYSQL_TYPE_SHORT:
|
||||
return *reinterpret_cast<int*>(bind->buffer);
|
||||
case enum_field_types::MYSQL_TYPE_LONGLONG:
|
||||
return std::to_string(*reinterpret_cast<std::int64_t*>(bind->buffer));
|
||||
case enum_field_types::MYSQL_TYPE_FLOAT:
|
||||
return *reinterpret_cast<float*>(bind->buffer);
|
||||
case enum_field_types::MYSQL_TYPE_DOUBLE:
|
||||
return static_cast<float>(*reinterpret_cast<double*>(bind->buffer));
|
||||
case enum_field_types::MYSQL_TYPE_STRING:
|
||||
return std::string{reinterpret_cast<char*>(bind->buffer), bind->buffer_length};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
scripting::array generate_result(MYSQL_STMT* stmt)
|
||||
{
|
||||
if (stmt == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
utils::memory::allocator allocator;
|
||||
|
||||
scripting::array result_arr;
|
||||
|
||||
const auto meta = mysql_stmt_result_metadata(stmt);
|
||||
if (meta == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto column_count = mysql_num_fields(meta);
|
||||
const auto fields = mysql_fetch_fields(meta);
|
||||
|
||||
const auto is_null = allocator.allocate_array<my_bool>(column_count);
|
||||
const auto errors = allocator.allocate_array<my_bool>(column_count);
|
||||
const auto real_lengths = allocator.allocate_array<unsigned long>(column_count);
|
||||
const auto binds = allocator.allocate_array<MYSQL_BIND>(column_count);
|
||||
|
||||
for (auto i = 0u; i < column_count; i++)
|
||||
{
|
||||
binds[i].length = &real_lengths[i];
|
||||
binds[i].is_null = &is_null[i];
|
||||
binds[i].error = &errors[i];
|
||||
}
|
||||
|
||||
if (mysql_stmt_bind_result(stmt, binds) != 0 ||
|
||||
mysql_stmt_store_result(stmt) != 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
|
||||
{
|
||||
for (auto i = 0u; i < column_count; i++)
|
||||
{
|
||||
switch (fields[i].type)
|
||||
{
|
||||
case enum_field_types::MYSQL_TYPE_INT24:
|
||||
case enum_field_types::MYSQL_TYPE_LONG:
|
||||
case enum_field_types::MYSQL_TYPE_SHORT:
|
||||
{
|
||||
binds[i].buffer_type = MYSQL_TYPE_LONG;
|
||||
binds[i].buffer = allocator.allocate<int>();
|
||||
binds[i].buffer_length = sizeof(int);
|
||||
break;
|
||||
};
|
||||
case enum_field_types::MYSQL_TYPE_LONGLONG:
|
||||
{
|
||||
binds[i].buffer_type = MYSQL_TYPE_LONGLONG;
|
||||
binds[i].buffer = allocator.allocate<std::int64_t>();
|
||||
binds[i].buffer_length = sizeof(std::int64_t);
|
||||
break;
|
||||
};
|
||||
case enum_field_types::MYSQL_TYPE_FLOAT:
|
||||
binds[i].buffer_type = MYSQL_TYPE_FLOAT;
|
||||
binds[i].buffer = allocator.allocate<float>();
|
||||
binds[i].buffer_length = sizeof(float);
|
||||
break;
|
||||
case enum_field_types::MYSQL_TYPE_DOUBLE:
|
||||
{
|
||||
binds[i].buffer_type = MYSQL_TYPE_DOUBLE;
|
||||
binds[i].buffer = allocator.allocate<double>();
|
||||
binds[i].buffer_length = sizeof(double);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
binds[i].buffer_type = MYSQL_TYPE_STRING;
|
||||
binds[i].buffer = allocator.allocate_array<char>(real_lengths[i]);
|
||||
binds[i].buffer_length = real_lengths[i];
|
||||
break;
|
||||
}
|
||||
|
||||
mysql_stmt_fetch_column(stmt, &binds[i], i, 0);
|
||||
}
|
||||
|
||||
scripting::array row_arr;
|
||||
|
||||
for (auto i = 0u; i < column_count; i++)
|
||||
{
|
||||
const auto field = &fields[i];
|
||||
const std::string field_name = {field->name, field->name_length};
|
||||
row_arr[field_name] = bind_to_value(&binds[i]);
|
||||
}
|
||||
|
||||
result_arr.push(row_arr);
|
||||
}
|
||||
|
||||
mysql_free_result(meta);
|
||||
|
||||
return result_arr;
|
||||
}
|
||||
|
||||
scripting::array generate_result(MYSQL_RES* result)
|
||||
{
|
||||
if (result == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
scripting::array result_arr;
|
||||
|
||||
const auto num_rows = mysql_num_rows(result);
|
||||
const auto num_fields = mysql_num_fields(result);
|
||||
const auto fields = mysql_fetch_fields(result);
|
||||
|
||||
for (auto i = 0u; i < num_rows; i++)
|
||||
{
|
||||
scripting::array row_arr;
|
||||
|
||||
const auto row = mysql_fetch_row(result);
|
||||
const auto lengths = mysql_fetch_lengths(result);
|
||||
|
||||
for (auto f = 0u; f < num_fields; f++)
|
||||
{
|
||||
const auto field = &fields[f];
|
||||
|
||||
const std::string field_str = {field->name, field->name_length};
|
||||
const std::string row_str = {row[f], lengths[f]};
|
||||
const auto value = field_to_value(field, row_str);
|
||||
|
||||
row_arr[field_str] = value;
|
||||
}
|
||||
|
||||
result_arr.push(row_arr);
|
||||
}
|
||||
|
||||
return result_arr;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
scripting::object create_mysql_query(F&& cb)
|
||||
{
|
||||
auto task = std::make_shared<task_t>();
|
||||
tasks.emplace_back(task);
|
||||
|
||||
scheduler::thread_pool.push([=]
|
||||
{
|
||||
try
|
||||
{
|
||||
mysql::access([&](database_t& db)
|
||||
{
|
||||
task->result = cb(db);
|
||||
task->completed = true;
|
||||
});
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("^1MySQL ERROR: %s\n", e.what());
|
||||
task->completed = true;
|
||||
}
|
||||
});
|
||||
|
||||
return task->handle;
|
||||
}
|
||||
|
||||
void wait_and_clear_tasks()
|
||||
{
|
||||
for (auto& task : tasks)
|
||||
{
|
||||
while (!task->completed)
|
||||
{
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
}
|
||||
|
||||
tasks.clear();
|
||||
}
|
||||
|
||||
void notify_task_result(std::shared_ptr<task_t>& task)
|
||||
{
|
||||
const auto result = task->result.result
|
||||
? generate_result(task->result.result)
|
||||
: generate_result(task->result.stmt);
|
||||
|
||||
const auto rows = static_cast<std::size_t>(task->result.affected_rows);
|
||||
scripting::notify(task->handle.get_entity_id(), "done", {result, rows, task->result.error});
|
||||
|
||||
if (task->result.result != nullptr)
|
||||
{
|
||||
mysql_free_result(task->result.result);
|
||||
task->result.result = nullptr;
|
||||
}
|
||||
|
||||
if (task->result.stmt != nullptr)
|
||||
{
|
||||
mysql_stmt_close(task->result.stmt);
|
||||
task->result.stmt = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void check_tasks()
|
||||
{
|
||||
for (auto i = tasks.begin(); i != tasks.end(); )
|
||||
{
|
||||
auto& task = *i;
|
||||
if (!task->completed)
|
||||
{
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
notify_task_result(task);
|
||||
i = tasks.erase(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
MYSQL_BIND* bind_statement_args(const T& args, std::size_t& bind_count)
|
||||
{
|
||||
bind_count = args.size();
|
||||
const auto binds = utils::memory::allocate_array<MYSQL_BIND>(bind_count);
|
||||
|
||||
for (auto i = 0u; i < args.size(); i++)
|
||||
{
|
||||
const auto& arg = args[i];
|
||||
const auto& raw_value = arg.get_raw();
|
||||
|
||||
switch (raw_value.type)
|
||||
{
|
||||
case game::SCRIPT_FLOAT:
|
||||
{
|
||||
binds[i].buffer = utils::memory::allocate<float>();
|
||||
binds[i].buffer_type = MYSQL_TYPE_FLOAT;
|
||||
*reinterpret_cast<float*>(binds[i].buffer) = raw_value.u.floatValue;
|
||||
break;
|
||||
}
|
||||
case game::SCRIPT_INTEGER:
|
||||
{
|
||||
binds[i].buffer = utils::memory::allocate<int>();
|
||||
binds[i].buffer_type = MYSQL_TYPE_LONG;
|
||||
*reinterpret_cast<int*>(binds[i].buffer) = raw_value.u.intValue;
|
||||
break;
|
||||
}
|
||||
case game::SCRIPT_STRING:
|
||||
{
|
||||
const auto str = arg.as<std::string>();
|
||||
const auto str_copy = utils::memory::duplicate_string(str);
|
||||
binds[i].buffer = str_copy;
|
||||
binds[i].buffer_length = str.size();
|
||||
binds[i].buffer_type = MYSQL_TYPE_STRING;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
binds[i].buffer_type = MYSQL_TYPE_NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return binds;
|
||||
}
|
||||
|
||||
void cleanup_connections()
|
||||
{
|
||||
for (auto& connection : connection_pool)
|
||||
{
|
||||
connection.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
void free_statement_binds(MYSQL_BIND* binds, const std::size_t bind_count)
|
||||
{
|
||||
if (binds == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 0u; i < bind_count; i++)
|
||||
{
|
||||
utils::memory::free(binds[i].buffer);
|
||||
}
|
||||
|
||||
utils::memory::free(binds);
|
||||
}
|
||||
}
|
||||
|
||||
utils::concurrency::container<sql::connection_config>& get_config()
|
||||
{
|
||||
static utils::concurrency::container<sql::connection_config> config;
|
||||
static auto initialized = false;
|
||||
|
||||
if (!initialized)
|
||||
{
|
||||
config.access([&](sql::connection_config& cfg)
|
||||
{
|
||||
cfg.user = "root";
|
||||
cfg.password = "root";
|
||||
cfg.host = "localhost";
|
||||
cfg.port = 3306;
|
||||
cfg.database = "default";
|
||||
});
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
void connection::check()
|
||||
{
|
||||
const auto now = std::chrono::high_resolution_clock::now();
|
||||
const auto diff = now - this->start_;
|
||||
|
||||
if (this->db.get() == nullptr || !this->db->ping_server() || diff >= connection_timeout)
|
||||
{
|
||||
get_config().access([&](sql::connection_config& cfg)
|
||||
{
|
||||
this->db = std::make_unique<sql::connection>(cfg);
|
||||
this->start_ = now;
|
||||
});
|
||||
}
|
||||
|
||||
this->last_access_ = now;
|
||||
}
|
||||
|
||||
void connection::cleanup()
|
||||
{
|
||||
std::unique_lock<database_mutex_t> lock(this->mutex, std::try_to_lock);
|
||||
if (!lock.owns_lock())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto now = std::chrono::high_resolution_clock::now();
|
||||
const auto diff = now - this->last_access_;
|
||||
if (diff >= connection_timeout)
|
||||
{
|
||||
this->db.reset();
|
||||
}
|
||||
}
|
||||
|
||||
connection* get_connection(std::unique_lock<database_mutex_t>& lock)
|
||||
{
|
||||
static thread_local connection* last_connection{};
|
||||
if (last_connection != nullptr)
|
||||
{
|
||||
lock = std::unique_lock(last_connection->mutex, std::try_to_lock);
|
||||
if (lock.owns_lock())
|
||||
{
|
||||
return last_connection;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0u; i < connection_pool.size(); i++)
|
||||
{
|
||||
auto connection = &connection_pool[i];
|
||||
if (connection == last_connection)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
lock = std::unique_lock(connection->mutex, std::try_to_lock);
|
||||
if (!lock.owns_lock())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
last_connection = connection;
|
||||
return connection;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void on_shutdown([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
wait_and_clear_tasks();
|
||||
}
|
||||
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scripting::on_shutdown(wait_and_clear_tasks);
|
||||
scheduler::loop(cleanup_connections, scheduler::async, 60s);
|
||||
scheduler::loop(check_tasks, scheduler::server);
|
||||
|
||||
gsc::function::add("mysql_set_config", [](const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto config = args[0].as<scripting::array>();
|
||||
get_config().access([&](sql::connection_config& cfg)
|
||||
{
|
||||
cfg.host = config["host"].as<std::string>();
|
||||
cfg.user = config["user"].as<std::string>();
|
||||
cfg.password = config["password"].as<std::string>();
|
||||
cfg.port = config["port"].as<unsigned short>();
|
||||
cfg.database = config["database"].as<std::string>();
|
||||
});
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::function::add("mysql_query", [](const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto query = args[0].as<std::string>();
|
||||
return create_mysql_query([=](database_t& db)
|
||||
{
|
||||
const auto handle = db->get_handle();
|
||||
|
||||
mysql_result_t result{};
|
||||
if (mysql_query(handle, query.data()) != 0)
|
||||
{
|
||||
result.error = mysql_error(handle);
|
||||
return result;
|
||||
}
|
||||
|
||||
result.result = mysql_store_result(handle);
|
||||
result.affected_rows = mysql_affected_rows(handle);
|
||||
|
||||
return result;
|
||||
});
|
||||
});
|
||||
|
||||
gsc::function::add("mysql_prepared_statement", [](const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
MYSQL_BIND* binds = nullptr;
|
||||
auto bind_count = 0u;
|
||||
|
||||
const auto query = args[0].as<std::string>();
|
||||
auto args_vec = args.get_raw();
|
||||
args_vec.erase(args_vec.begin());
|
||||
|
||||
try
|
||||
{
|
||||
if (args_vec.size() > 0 && args_vec[0].is<scripting::array>())
|
||||
{
|
||||
binds = bind_statement_args(args_vec[0].as<scripting::array>(), bind_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
binds = bind_statement_args(args_vec, bind_count);
|
||||
}
|
||||
|
||||
const auto handle = create_mysql_query([binds, bind_count, query](database_t& db)
|
||||
{
|
||||
const auto _0 = gsl::finally([=]
|
||||
{
|
||||
free_statement_binds(binds, bind_count);
|
||||
});
|
||||
|
||||
mysql_result_t result{};
|
||||
|
||||
const auto handle = db->get_handle();
|
||||
const auto stmt = mysql_stmt_init(handle);
|
||||
|
||||
if (mysql_stmt_prepare(stmt, query.data(), query.size()) != 0 ||
|
||||
(binds != nullptr && mysql_stmt_bind_param(stmt, binds) != 0) ||
|
||||
mysql_stmt_execute(stmt) != 0)
|
||||
{
|
||||
result.error = mysql_stmt_error(stmt);
|
||||
return result;
|
||||
}
|
||||
|
||||
result.stmt = stmt;
|
||||
result.affected_rows = mysql_stmt_affected_rows(stmt);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
return handle;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
free_statement_binds(binds, bind_count);
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(mysql::component)
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils/concurrency.hpp>
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4127)
|
||||
#pragma warning(disable: 4267)
|
||||
#pragma warning(disable: 4018)
|
||||
#pragma warning(disable: 4996)
|
||||
#pragma warning(disable: 4244)
|
||||
#include <sqlpp11/sqlpp11.h>
|
||||
#include <sqlpp11/mysql/mysql.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
namespace sql = sqlpp::mysql;
|
||||
|
||||
namespace mysql
|
||||
{
|
||||
constexpr auto max_connections = 100;
|
||||
constexpr auto connection_timeout = 200s;
|
||||
|
||||
using database_mutex_t = std::recursive_mutex;
|
||||
using database_t = std::unique_ptr<sql::connection>;
|
||||
|
||||
utils::concurrency::container<sql::connection_config>& get_config();
|
||||
|
||||
class connection
|
||||
{
|
||||
public:
|
||||
connection() = default;
|
||||
void check();
|
||||
void cleanup();
|
||||
|
||||
database_t db;
|
||||
database_mutex_t mutex;
|
||||
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point start_;
|
||||
std::chrono::high_resolution_clock::time_point last_access_;
|
||||
|
||||
};
|
||||
|
||||
connection* get_connection(std::unique_lock<database_mutex_t>& lock);
|
||||
|
||||
template <typename T = void, typename F>
|
||||
T access(F&& accessor)
|
||||
{
|
||||
std::unique_lock<database_mutex_t> lock;
|
||||
auto conn = get_connection(lock);
|
||||
if (conn == nullptr)
|
||||
{
|
||||
throw std::runtime_error("out of connections");
|
||||
}
|
||||
|
||||
conn->check();
|
||||
return accessor(conn->db);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "scheduler.hpp"
|
||||
#include "gsc.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
namespace notifies
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::vector<scripting::function> say_callbacks;
|
||||
utils::hook::detour client_command_hook;
|
||||
|
||||
void client_command_stub(int clientNum)
|
||||
{
|
||||
char cmd[1024] = { 0 };
|
||||
|
||||
game::SV_Cmd_ArgvBuffer(0, cmd, 1024);
|
||||
|
||||
auto hidden = false;
|
||||
if (cmd == "say"s || cmd == "say_team"s)
|
||||
{
|
||||
std::string message = game::ConcatArgs(1);
|
||||
message.erase(0, 1);
|
||||
|
||||
for (const auto& callback : say_callbacks)
|
||||
{
|
||||
const auto entity_id = game::Scr_GetEntityId(clientNum, 0);
|
||||
const auto result = callback(entity_id, {message, cmd == "say_team"s});
|
||||
|
||||
if (result.is<int>() && !hidden)
|
||||
{
|
||||
hidden = result.as<int>() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hidden)
|
||||
{
|
||||
client_command_hook.invoke<void>(clientNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
client_command_hook.create(0x502CB0, client_command_stub);
|
||||
|
||||
scripting::on_shutdown([]()
|
||||
{
|
||||
say_callbacks.clear();
|
||||
});
|
||||
|
||||
gsc::function::add("onplayersay", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto function = args[0].as<scripting::function>();
|
||||
say_callbacks.push_back(function);
|
||||
return {};
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(notifies::component)
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
namespace scheduler
|
||||
{
|
||||
utils::thread_pool thread_pool;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct task
|
||||
@@ -77,6 +79,7 @@ namespace scheduler
|
||||
};
|
||||
|
||||
std::thread thread;
|
||||
std::atomic_bool killed;
|
||||
task_pipeline pipelines[pipeline::count];
|
||||
|
||||
void execute(const pipeline type)
|
||||
@@ -128,11 +131,14 @@ namespace scheduler
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
thread_pool.initialize(8);
|
||||
thread_pool.start();
|
||||
|
||||
thread = std::thread([]()
|
||||
{
|
||||
while (true)
|
||||
while (!killed)
|
||||
{
|
||||
execute(pipeline::async);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
@@ -141,6 +147,17 @@ namespace scheduler
|
||||
|
||||
utils::hook::call(0x50CEDC, server_frame_stub);
|
||||
}
|
||||
|
||||
void on_shutdown([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
thread_pool.stop();
|
||||
killed = true;
|
||||
|
||||
if (thread.joinable())
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils/thread_pool.hpp>
|
||||
|
||||
namespace scheduler
|
||||
{
|
||||
enum pipeline
|
||||
@@ -18,4 +20,6 @@ namespace scheduler
|
||||
std::chrono::milliseconds delay = 0ms);
|
||||
void once(const std::function<void()>& callback, pipeline type = pipeline::server,
|
||||
std::chrono::milliseconds delay = 0ms);
|
||||
|
||||
extern utils::thread_pool thread_pool;
|
||||
}
|
||||
|
||||
@@ -7,74 +7,17 @@
|
||||
|
||||
#include "game/scripting/event.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour vm_notify_hook;
|
||||
utils::hook::detour scr_add_class_field_hook;
|
||||
|
||||
utils::hook::detour scr_load_level_hook;
|
||||
utils::hook::detour g_shutdown_game_hook;
|
||||
|
||||
utils::hook::detour scr_set_thread_position_hook;
|
||||
utils::hook::detour process_script_hook;
|
||||
|
||||
std::string current_file;
|
||||
|
||||
std::vector<std::function<void()>> shutdown_callbacks;
|
||||
|
||||
void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value,
|
||||
game::VariableValue* top)
|
||||
{
|
||||
const auto* name = game::SL_ConvertToString(string_value);
|
||||
|
||||
if (name)
|
||||
{
|
||||
event e;
|
||||
e.name = name;
|
||||
e.entity = notify_list_owner_id;
|
||||
|
||||
for (auto* value = top; value->type != game::SCRIPT_END; --value)
|
||||
{
|
||||
e.arguments.emplace_back(*value);
|
||||
}
|
||||
|
||||
if (e.name == "connected")
|
||||
{
|
||||
const auto player = e.arguments[0].as<scripting::entity>();
|
||||
const auto client = player.call("getentitynumber").as<int>();
|
||||
userinfo::clear_client_overrides(client);
|
||||
}
|
||||
}
|
||||
|
||||
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
|
||||
}
|
||||
|
||||
void scr_add_class_field_stub(unsigned int classnum, unsigned int _name, unsigned int canonicalString, unsigned int offset)
|
||||
{
|
||||
const auto name = game::SL_ConvertToString(_name);
|
||||
|
||||
if (fields_table[classnum].find(name) == fields_table[classnum].end())
|
||||
{
|
||||
fields_table[classnum][name] = offset;
|
||||
}
|
||||
|
||||
scr_add_class_field_hook.invoke<void>(classnum, _name, canonicalString, offset);
|
||||
}
|
||||
|
||||
void scr_load_level_stub()
|
||||
{
|
||||
scr_load_level_hook.invoke<void>();
|
||||
}
|
||||
|
||||
void g_shutdown_game_stub(const int free_scripts)
|
||||
{
|
||||
userinfo::clear_overrides();
|
||||
@@ -87,31 +30,6 @@ namespace scripting
|
||||
|
||||
g_shutdown_game_hook.invoke<void>(free_scripts);
|
||||
}
|
||||
|
||||
void process_script_stub(const char* filename)
|
||||
{
|
||||
current_file = filename;
|
||||
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
current_file = scripting::file_list[file_id];
|
||||
}
|
||||
|
||||
process_script_hook.invoke<void>(filename);
|
||||
}
|
||||
|
||||
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
|
||||
{
|
||||
const auto function_name = scripting::find_token(threadName);
|
||||
|
||||
if (!function_name.empty())
|
||||
{
|
||||
script_function_table[current_file][function_name] = codePos;
|
||||
}
|
||||
|
||||
scr_set_thread_position_hook.invoke<void>(threadName, codePos);
|
||||
}
|
||||
}
|
||||
|
||||
void on_shutdown(const std::function<void()>& callback)
|
||||
@@ -122,18 +40,11 @@ namespace scripting
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scr_load_level_hook.create(0x527AF0, scr_load_level_stub);
|
||||
g_shutdown_game_hook.create(0x50C100, g_shutdown_game_stub);
|
||||
|
||||
scr_add_class_field_hook.create(0x567CD0, scr_add_class_field_stub);
|
||||
vm_notify_hook.create(0x569720, vm_notify_stub);
|
||||
|
||||
scr_set_thread_position_hook.create(0x5616D0, scr_set_thread_position_stub);
|
||||
process_script_hook.create(0x56B130, process_script_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(scripting::component)
|
||||
REGISTER_COMPONENT(scripting::component)
|
||||
|
||||
@@ -2,8 +2,5 @@
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
|
||||
void on_shutdown(const std::function<void()>& callback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
// lua/lstrlib.c
|
||||
#define MAX_FORMAT 32
|
||||
#define L_FMTFLAGSF "-+#0 "
|
||||
#define L_FMTFLAGSX "-#0"
|
||||
#define L_FMTFLAGSI "-+0 "
|
||||
#define L_FMTFLAGSU "-0"
|
||||
#define L_FMTFLAGSC "-"
|
||||
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace string
|
||||
{
|
||||
namespace
|
||||
{
|
||||
// lua/lstrlib.c
|
||||
const char* getformat(const char* strfrmt, char* form)
|
||||
{
|
||||
const auto len = std::strspn(strfrmt, L_FMTFLAGSF "123456789.") + 1;
|
||||
if (len >= MAX_FORMAT - 10)
|
||||
{
|
||||
throw std::runtime_error("invalid format (too long)");
|
||||
}
|
||||
|
||||
*(form++) = '%';
|
||||
std::memcpy(form, strfrmt, len * sizeof(char));
|
||||
*(form + len) = '\0';
|
||||
return strfrmt + len - 1;
|
||||
}
|
||||
|
||||
// lua/lstrlib.c
|
||||
const char* get_2_digits(const char* s)
|
||||
{
|
||||
if (isdigit(static_cast<unsigned char>(*s)))
|
||||
{
|
||||
s++;
|
||||
if (isdigit(static_cast<unsigned char>(*s)))
|
||||
{
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
// lua/lstrlib.c
|
||||
void check_format(const char* form, const char* flags, int precision)
|
||||
{
|
||||
const char* spec = form + 1;
|
||||
spec += std::strspn(spec, flags);
|
||||
if (*spec != '0')
|
||||
{
|
||||
spec = get_2_digits(spec);
|
||||
if (*spec == '.' && precision)
|
||||
{
|
||||
spec++;
|
||||
spec = get_2_digits(spec);
|
||||
}
|
||||
}
|
||||
if (!std::isalpha(static_cast<unsigned char>(*spec)))
|
||||
{
|
||||
throw std::runtime_error(utils::string::va("invalid conversion specification: '%s'", form));
|
||||
}
|
||||
}
|
||||
|
||||
// partially lua/lstrlib.c
|
||||
std::string format_string(const gsc::function_args& args)
|
||||
{
|
||||
std::string buffer{};
|
||||
size_t va_index = 1;
|
||||
|
||||
const auto fmt = args[0].as<std::string>();
|
||||
|
||||
const char* strfrmt = fmt.data();
|
||||
const char* strfrmt_end = strfrmt + fmt.size();
|
||||
|
||||
while (strfrmt < strfrmt_end)
|
||||
{
|
||||
if (*strfrmt != '%')
|
||||
{
|
||||
buffer.push_back(*strfrmt++);
|
||||
}
|
||||
else if (*++strfrmt == '%')
|
||||
{
|
||||
buffer.push_back(*strfrmt++);
|
||||
}
|
||||
else
|
||||
{
|
||||
char form[MAX_FORMAT]{};
|
||||
const char* flags = "";
|
||||
strfrmt = getformat(strfrmt, form);
|
||||
|
||||
switch (*strfrmt++)
|
||||
{
|
||||
case 'd':
|
||||
case 'i':
|
||||
flags = L_FMTFLAGSI;
|
||||
goto intcase;
|
||||
case 'u':
|
||||
case 'p':
|
||||
flags = L_FMTFLAGSU;
|
||||
goto intcase;
|
||||
case 'o':
|
||||
case 'x':
|
||||
case 'X':
|
||||
flags = L_FMTFLAGSX;
|
||||
intcase:
|
||||
{
|
||||
check_format(form, flags, 1);
|
||||
const auto value = args[va_index].as<int>();
|
||||
buffer.append(utils::string::va(form, value));
|
||||
va_index++;
|
||||
break;
|
||||
}
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
{
|
||||
check_format(form, L_FMTFLAGSF, 1);
|
||||
const auto value = args[va_index].as<float>();
|
||||
buffer.append(utils::string::va(form, value));
|
||||
va_index++;
|
||||
break;
|
||||
}
|
||||
case 'c':
|
||||
{
|
||||
const auto value = args[va_index].as<int>();
|
||||
check_format(form, L_FMTFLAGSC, 0);
|
||||
buffer.append(utils::string::va(form, static_cast<char>(value)));
|
||||
va_index++;
|
||||
break;
|
||||
}
|
||||
case 's':
|
||||
{
|
||||
const auto str = args[va_index].as<std::string>();
|
||||
buffer.append(str);
|
||||
va_index++;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw std::runtime_error(utils::string::va("invalid conversion '%s' to 'format'", form));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("getchar", [](const gsc::function_args& args)
|
||||
{
|
||||
auto index = 0;
|
||||
if (args.size() > 1)
|
||||
{
|
||||
index = args[1].as<int>();
|
||||
}
|
||||
|
||||
const auto string = args[0].as<std::string>();
|
||||
if (index >= static_cast<int>(string.size()))
|
||||
{
|
||||
throw std::runtime_error("char index out of bounds");
|
||||
}
|
||||
|
||||
return static_cast<int>(string[index]);
|
||||
});
|
||||
|
||||
gsc::function::add("chartostring", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto char_ = static_cast<char>(args[0].as<int>());
|
||||
return std::string(1, char_);
|
||||
});
|
||||
|
||||
gsc::function::add("parsehex", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto str = args[0].as<std::string>();
|
||||
return static_cast<unsigned int>(std::strtoul(str.data(), nullptr, 0x10));
|
||||
});
|
||||
|
||||
gsc::function::add("string_format", format_string);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(string::component)
|
||||
+81
-35
@@ -7,23 +7,23 @@
|
||||
namespace userinfo
|
||||
{
|
||||
using userinfo_map = std::unordered_map<std::string, std::string>;
|
||||
std::unordered_map<int, userinfo_map> userinfo_overrides;
|
||||
std::array<userinfo_map, 18> userinfo_overrides;
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour sv_getuserinfo_hook;
|
||||
|
||||
userinfo_map userinfo_to_map(std::string userinfo)
|
||||
userinfo_map userinfo_to_map(const char* userinfo)
|
||||
{
|
||||
userinfo_map map{};
|
||||
|
||||
if (userinfo[0] == '\\')
|
||||
{
|
||||
userinfo = userinfo.substr(1);
|
||||
++userinfo;
|
||||
}
|
||||
|
||||
const auto args = utils::string::split(userinfo, '\\');
|
||||
for (size_t i = 0; !args.empty() && i < (args.size() - 1); i += 2)
|
||||
for (auto i = 0u; !args.empty() && i < (args.size() - 1); i += 2)
|
||||
{
|
||||
map[args[i]] = args[i + 1];
|
||||
}
|
||||
@@ -49,12 +49,8 @@ namespace userinfo
|
||||
void sv_getuserinfo_stub(int index, char* buffer, int bufferSize)
|
||||
{
|
||||
sv_getuserinfo_hook.invoke<void>(index, buffer, bufferSize);
|
||||
auto map = userinfo_to_map(buffer);
|
||||
|
||||
if (userinfo_overrides.find(index) == userinfo_overrides.end())
|
||||
{
|
||||
userinfo_overrides[index] = {};
|
||||
}
|
||||
auto map = userinfo_to_map(buffer);
|
||||
|
||||
for (const auto& values : userinfo_overrides[index])
|
||||
{
|
||||
@@ -71,45 +67,77 @@ namespace userinfo
|
||||
const auto userinfo = map_to_userinfo(map);
|
||||
strcpy_s(buffer, 1024, userinfo.data());
|
||||
}
|
||||
}
|
||||
|
||||
void clear_client_overrides(int client)
|
||||
{
|
||||
userinfo_overrides[client].clear();
|
||||
void clear_client_overrides(unsigned int client)
|
||||
{
|
||||
userinfo_overrides[client].clear();
|
||||
}
|
||||
|
||||
void* client_connect_stub(int client_num, int script_pers_id)
|
||||
{
|
||||
const auto res = utils::hook::invoke<void*>(0x4FAFB0, client_num, script_pers_id);
|
||||
|
||||
if (res == nullptr)
|
||||
{
|
||||
const scripting::entity player = game::Scr_GetEntityId(client_num, 0);
|
||||
scripting::notify(*game::levelEntityId, "direct_connect", {player});
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
void clear_overrides()
|
||||
{
|
||||
userinfo_overrides.clear();
|
||||
for (auto& entry : userinfo_overrides)
|
||||
{
|
||||
entry.clear();
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
sv_getuserinfo_hook.create(0x573E00, sv_getuserinfo_stub);
|
||||
utils::hook::call(0x573042, client_connect_stub);
|
||||
|
||||
gsc::method::add("setname", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
plugin->get_interface()->callbacks()->on_player_connect(clear_client_overrides);
|
||||
plugin->get_interface()->callbacks()->on_player_disconnect(clear_client_overrides);
|
||||
|
||||
gsc::method::add("setname", [](const game::scr_entref_t ent, const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
userinfo_overrides[ent.entnum]["name"] = name;
|
||||
game::ClientUserinfoChanged(ent.entnum);
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("name");
|
||||
@@ -118,15 +146,21 @@ namespace userinfo
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("setclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
gsc::method::add("setclantag", [](const game::scr_entref_t ent, const gsc::function_args& args)
|
||||
-> scripting::script_value
|
||||
{
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_TagText"] = name;
|
||||
userinfo_overrides[ent.entnum]["ec_usingTag"] = "1";
|
||||
@@ -135,11 +169,17 @@ namespace userinfo
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("clantag");
|
||||
@@ -150,11 +190,17 @@ namespace userinfo
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args&)
|
||||
-> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0 || ent.entnum > 17)
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = "";
|
||||
@@ -168,4 +214,4 @@ namespace userinfo
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(userinfo::component)
|
||||
REGISTER_COMPONENT(userinfo::component)
|
||||
|
||||
@@ -2,6 +2,5 @@
|
||||
|
||||
namespace userinfo
|
||||
{
|
||||
void clear_client_overrides(int client);
|
||||
void clear_overrides();
|
||||
}
|
||||
}
|
||||
|
||||
+71
-17
@@ -1,24 +1,78 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||
#include "plugin.hpp"
|
||||
|
||||
#include "game/game.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/binary_resource.hpp>
|
||||
#include <utils/nt.hpp>
|
||||
#include <utils/cryptography.hpp>
|
||||
#include <utils/io.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
const auto value = *reinterpret_cast<DWORD*>(0x21000000);
|
||||
if (value != 0x7AC6)
|
||||
{
|
||||
MessageBoxA(NULL,
|
||||
"This version of iw5-gsc-utils is outdated.\n" \
|
||||
"Download the latest dll from here: https://github.com/fedddddd/iw5-gsc-utils/releases" \
|
||||
, "ERROR", MB_ICONERROR);
|
||||
utils::hook::detour load_library_hook;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
std::string extract_resource(const std::string& name, const int resource)
|
||||
{
|
||||
const auto data = utils::nt::load_resource(resource);
|
||||
const auto path = std::filesystem::current_path() / "tmp" / name;
|
||||
const auto path_str = path.generic_string();
|
||||
|
||||
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), game::plutonium::printf);
|
||||
component_loader::post_unpack();
|
||||
}
|
||||
if (!utils::io::write_file(path_str, data))
|
||||
{
|
||||
const auto current = utils::io::read_file(path_str);
|
||||
const auto hash_current = utils::cryptography::md5::compute(current);
|
||||
const auto hash_target = utils::cryptography::md5::compute(data);
|
||||
if (hash_target != hash_current)
|
||||
{
|
||||
throw std::runtime_error("failed to extract libmysql.dll!");
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
return path_str;
|
||||
}
|
||||
|
||||
HMODULE __stdcall load_library_stub(LPCSTR lib_name, HANDLE file, DWORD flags)
|
||||
{
|
||||
if (lib_name == "libmysql.dll"s)
|
||||
{
|
||||
const auto path = extract_resource(lib_name, LIBMYSQL_DLL);
|
||||
const auto handle = load_library_hook.invoke_pascal<HMODULE>(path.data(), file, flags);
|
||||
|
||||
if (handle != nullptr)
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error(std::format("failed to load libmysql.dll: {}", GetLastError()));
|
||||
}
|
||||
}
|
||||
|
||||
return load_library_hook.invoke_pascal<HMODULE>(lib_name, file, flags);
|
||||
}
|
||||
}
|
||||
|
||||
PLUTONIUM_API plutonium::sdk::plugin* PLUTONIUM_CALLBACK on_initialize()
|
||||
{
|
||||
return plugin::get();
|
||||
}
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE module, DWORD ul_reason_for_call, LPVOID /*reserved*/)
|
||||
{
|
||||
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
utils::nt::library::set_current_handle(module);
|
||||
load_library_hook.create(LoadLibraryExA, load_library_stub);
|
||||
}
|
||||
|
||||
if (ul_reason_for_call == DLL_PROCESS_DETACH)
|
||||
{
|
||||
component_loader::on_shutdown();
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
+7
-1
@@ -2,5 +2,11 @@
|
||||
|
||||
namespace game
|
||||
{
|
||||
|
||||
void SV_GameDropClient(int clientNum, const char* reason)
|
||||
{
|
||||
if (clientNum >= 0 && clientNum < sv_maxclients->current.integer)
|
||||
{
|
||||
SV_DropClient(&svs_clients[clientNum], reason, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,11 @@ namespace game
|
||||
return dedi_;
|
||||
}
|
||||
|
||||
void set(const size_t dedi)
|
||||
{
|
||||
this->dedi_ = reinterpret_cast<T*>(dedi);
|
||||
}
|
||||
|
||||
operator T* () const
|
||||
{
|
||||
return this->get();
|
||||
@@ -29,6 +34,8 @@ namespace game
|
||||
private:
|
||||
T* dedi_;
|
||||
};
|
||||
|
||||
void SV_GameDropClient(int clientNum, const char* reason);
|
||||
}
|
||||
|
||||
#include "symbols.hpp"
|
||||
@@ -15,30 +15,30 @@ namespace scripting
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
game::VariableValue variable{};
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
void array_value::operator=(const script_value& _value)
|
||||
void array_value::operator=(const script_value& value)
|
||||
{
|
||||
if (!this->id_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const auto value = _value.get_raw();
|
||||
const auto& value_raw = value.get_raw();
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)];
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::AddRefToValue(value_raw.type, value_raw.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
variable->type = value_raw.type;
|
||||
variable->u.u = value_raw.u;
|
||||
|
||||
this->value_ = value;
|
||||
this->value_ = value_raw;
|
||||
}
|
||||
|
||||
array::array(const unsigned int id)
|
||||
@@ -170,7 +170,7 @@ namespace scripting
|
||||
return game::Scr_GetSelf(this->id_);
|
||||
}
|
||||
|
||||
unsigned int array::push(script_value value) const
|
||||
unsigned int array::push(const script_value& value) const
|
||||
{
|
||||
this->set(this->size(), value);
|
||||
return this->size();
|
||||
@@ -212,8 +212,6 @@ namespace scripting
|
||||
{
|
||||
return this->get(key.as<std::string>());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
script_value array::get(const std::string& key) const
|
||||
@@ -227,7 +225,7 @@ namespace scripting
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
game::VariableValue variable{};
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
@@ -244,7 +242,7 @@ namespace scripting
|
||||
}
|
||||
|
||||
const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
game::VariableValue variable;
|
||||
game::VariableValue variable{};
|
||||
variable.u = value.u.u;
|
||||
variable.type = (game::scriptType_e)value.type;
|
||||
|
||||
@@ -263,11 +261,9 @@ namespace scripting
|
||||
}
|
||||
}
|
||||
|
||||
void array::set(const std::string& key, const script_value& _value) const
|
||||
void array::set(const std::string& key, const script_value& value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto& value_raw = value.get_raw();
|
||||
const auto variable_id = this->get_value_id(key);
|
||||
|
||||
if (!variable_id)
|
||||
@@ -277,16 +273,16 @@ namespace scripting
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::AddRefToValue(value_raw.type, value_raw.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
variable->type = value_raw.type;
|
||||
variable->u.u = value_raw.u;
|
||||
}
|
||||
|
||||
void array::set(const unsigned int index, const script_value& _value) const
|
||||
void array::set(const unsigned int index, const script_value& value) const
|
||||
{
|
||||
const auto value = _value.get_raw();
|
||||
const auto& value_raw = value.get_raw();
|
||||
const auto variable_id = this->get_value_id(index);
|
||||
|
||||
if (!variable_id)
|
||||
@@ -296,11 +292,11 @@ namespace scripting
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)];
|
||||
|
||||
game::AddRefToValue(value.type, value.u);
|
||||
game::AddRefToValue(value_raw.type, value_raw.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = value.type;
|
||||
variable->u.u = value.u;
|
||||
variable->type = value_raw.type;
|
||||
variable->u.u = value_raw.u;
|
||||
}
|
||||
|
||||
unsigned int array::get_entity_id() const
|
||||
@@ -324,6 +320,7 @@ namespace scripting
|
||||
unsigned int array::get_value_id(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
return game::GetNewArrayVariable(this->id_, index);
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace scripting
|
||||
std::vector<script_value> get_keys() const;
|
||||
unsigned int size() const;
|
||||
|
||||
unsigned int push(script_value) const;
|
||||
unsigned int push(const script_value&) const;
|
||||
void erase(const unsigned int) const;
|
||||
void erase(const std::string&) const;
|
||||
script_value pop() const;
|
||||
@@ -69,13 +69,15 @@ namespace scripting
|
||||
{
|
||||
if (key.is<I>())
|
||||
{
|
||||
return { this->id_, this->get_value_id(key.as<I>()) };
|
||||
return {this->id_, this->get_value_id(key.as<I>())};
|
||||
}
|
||||
|
||||
if (key.is<S>())
|
||||
{
|
||||
return { this->id_, this->get_value_id(key.as<S>()) };
|
||||
return {this->id_, this->get_value_id(key.as<S>())};
|
||||
}
|
||||
|
||||
throw std::runtime_error("invalid key type");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -96,20 +96,4 @@ namespace scripting
|
||||
game::RemoveRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->entity_id_)});
|
||||
}
|
||||
}
|
||||
|
||||
void entity::set(const std::string& field, const script_value& value) const
|
||||
{
|
||||
set_entity_field(*this, field, value);
|
||||
}
|
||||
|
||||
template <>
|
||||
script_value entity::get<script_value>(const std::string& field) const
|
||||
{
|
||||
return get_entity_field(*this, field);
|
||||
}
|
||||
|
||||
script_value entity::call(const std::string& name, const std::vector<script_value>& arguments) const
|
||||
{
|
||||
return call_function(name, *this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,6 @@ namespace scripting
|
||||
entity& operator=(const entity& other);
|
||||
entity& operator=(entity&& other) noexcept;
|
||||
|
||||
void set(const std::string& field, const script_value& value) const;
|
||||
|
||||
template <typename T = script_value>
|
||||
T get(const std::string& field) const;
|
||||
|
||||
script_value call(const std::string& name, const std::vector<script_value>& arguments = {}) const;
|
||||
|
||||
unsigned int get_entity_id() const;
|
||||
game::scr_entref_t get_entity_reference() const;
|
||||
|
||||
@@ -37,13 +30,4 @@ namespace scripting
|
||||
void add() const;
|
||||
void release() const;
|
||||
};
|
||||
|
||||
template <>
|
||||
script_value entity::get(const std::string& field) const;
|
||||
|
||||
template <typename T>
|
||||
T entity::get(const std::string& field) const
|
||||
{
|
||||
return this->get<script_value>(field).as<T>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "execution.hpp"
|
||||
#include "safe_execution.hpp"
|
||||
#include "stack_isolation.hpp"
|
||||
#include "event.hpp"
|
||||
|
||||
@@ -32,16 +31,6 @@ namespace scripting
|
||||
return script_value(game::scr_VmPub->top[1 - game::scr_VmPub->outparamcount]);
|
||||
}
|
||||
|
||||
int get_field_id(const int classnum, const std::string& field)
|
||||
{
|
||||
if (scripting::fields_table[classnum].find(field) != scripting::fields_table[classnum].end())
|
||||
{
|
||||
return scripting::fields_table[classnum][field];
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void scr_notify_id(int id, unsigned int stringValue, unsigned int paramcount)
|
||||
{
|
||||
if (game::scr_VmPub->outparamcount)
|
||||
@@ -95,47 +84,6 @@ namespace scripting
|
||||
scr_notify_id(entity.get_entity_id(), event_id, game::scr_VmPub->inparamcount);
|
||||
}
|
||||
|
||||
script_value call_function(const std::string& name, const entity& entity,
|
||||
const std::vector<script_value>& arguments)
|
||||
{
|
||||
const auto entref = entity.get_entity_reference();
|
||||
|
||||
const auto is_method_call = *reinterpret_cast<const int*>(&entref) != -1;
|
||||
const auto function = find_function(name, !is_method_call);
|
||||
if (!function)
|
||||
{
|
||||
throw std::runtime_error("Unknown function '" + name + "'");
|
||||
}
|
||||
|
||||
stack_isolation _;
|
||||
|
||||
for (auto i = arguments.rbegin(); i != arguments.rend(); ++i)
|
||||
{
|
||||
push_value(*i);
|
||||
}
|
||||
|
||||
game::scr_VmPub->outparamcount = game::scr_VmPub->inparamcount;
|
||||
game::scr_VmPub->inparamcount = 0;
|
||||
|
||||
if (!safe_execution::call(function, entref))
|
||||
{
|
||||
throw std::runtime_error("Error executing function '" + name + "'");
|
||||
}
|
||||
|
||||
return get_return_value();
|
||||
}
|
||||
|
||||
script_value call_function(const std::string& name, const std::vector<script_value>& arguments)
|
||||
{
|
||||
return call_function(name, entity(), arguments);
|
||||
}
|
||||
|
||||
template <>
|
||||
script_value call(const std::string& name, const std::vector<script_value>& arguments)
|
||||
{
|
||||
return call_function(name, arguments);
|
||||
}
|
||||
|
||||
script_value exec_ent_thread(const entity& entity, const char* pos, const std::vector<script_value>& arguments)
|
||||
{
|
||||
const auto id = entity.get_entity_id();
|
||||
@@ -155,124 +103,6 @@ namespace scripting
|
||||
return get_return_value();
|
||||
}
|
||||
|
||||
const char* get_function_pos(const std::string& filename, const std::string& function)
|
||||
{
|
||||
if (scripting::script_function_table.find(filename) == scripting::script_function_table.end())
|
||||
{
|
||||
throw std::runtime_error("File '" + filename + "' not found");
|
||||
};
|
||||
|
||||
const auto functions = scripting::script_function_table[filename];
|
||||
if (functions.find(function) == functions.end())
|
||||
{
|
||||
throw std::runtime_error("Function '" + function + "' in file '" + filename + "' not found");
|
||||
}
|
||||
|
||||
return functions.at(function);
|
||||
}
|
||||
|
||||
script_value call_script_function(const entity& entity, const std::string& filename,
|
||||
const std::string& function, const std::vector<script_value>& arguments)
|
||||
{
|
||||
const auto pos = get_function_pos(filename, function);
|
||||
return exec_ent_thread(entity, pos, arguments);
|
||||
}
|
||||
|
||||
static std::unordered_map<unsigned int, std::unordered_map<std::string, script_value>> custom_fields;
|
||||
|
||||
script_value get_custom_field(const entity& entity, const std::string& field)
|
||||
{
|
||||
auto fields = custom_fields[entity.get_entity_id()];
|
||||
const auto _field = fields.find(field);
|
||||
if (_field != fields.end())
|
||||
{
|
||||
return _field->second;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void set_custom_field(const entity& entity, const std::string& field, const script_value& value)
|
||||
{
|
||||
const auto id = entity.get_entity_id();
|
||||
|
||||
if (custom_fields[id].find(field) != custom_fields[id].end())
|
||||
{
|
||||
custom_fields[id][field] = value;
|
||||
return;
|
||||
}
|
||||
|
||||
custom_fields[id].insert(std::make_pair(field, value));
|
||||
}
|
||||
|
||||
void clear_entity_fields(const entity& entity)
|
||||
{
|
||||
const auto id = entity.get_entity_id();
|
||||
|
||||
if (custom_fields.find(id) != custom_fields.end())
|
||||
{
|
||||
custom_fields[id].clear();
|
||||
}
|
||||
}
|
||||
|
||||
void clear_custom_fields()
|
||||
{
|
||||
custom_fields.clear();
|
||||
}
|
||||
|
||||
void set_entity_field(const entity& entity, const std::string& field, const script_value& value)
|
||||
{
|
||||
const auto entref = entity.get_entity_reference();
|
||||
const int id = get_field_id(entref.classnum, field);
|
||||
|
||||
if (id != -1)
|
||||
{
|
||||
stack_isolation _;
|
||||
push_value(value);
|
||||
|
||||
game::scr_VmPub->outparamcount = game::scr_VmPub->inparamcount;
|
||||
game::scr_VmPub->inparamcount = 0;
|
||||
|
||||
if (!safe_execution::set_entity_field(entref, id))
|
||||
{
|
||||
throw std::runtime_error("Failed to set value for field '" + field + "'");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
set_custom_field(entity, field, value);
|
||||
}
|
||||
}
|
||||
|
||||
script_value get_entity_field(const entity& entity, const std::string& field)
|
||||
{
|
||||
const auto entref = entity.get_entity_reference();
|
||||
const auto id = get_field_id(entref.classnum, field);
|
||||
|
||||
if (id != -1)
|
||||
{
|
||||
stack_isolation _;
|
||||
|
||||
game::VariableValue value{};
|
||||
if (!safe_execution::get_entity_field(entref, id, &value))
|
||||
{
|
||||
throw std::runtime_error("Failed to get value for field '" + field + "'");
|
||||
}
|
||||
|
||||
const auto __ = gsl::finally([value]()
|
||||
{
|
||||
game::RemoveRefToValue(value.type, value.u);
|
||||
});
|
||||
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return get_custom_field(entity, field);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned int make_array()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "game/game.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "array.hpp"
|
||||
#include "object.hpp"
|
||||
#include "function.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
@@ -9,10 +10,6 @@ namespace scripting
|
||||
{
|
||||
void push_value(const script_value& value);
|
||||
|
||||
script_value call_function(const std::string& name, const std::vector<script_value>& arguments);
|
||||
script_value call_function(const std::string& name, const entity& entity,
|
||||
const std::vector<script_value>& arguments);
|
||||
|
||||
template <typename T = script_value>
|
||||
T call(const std::string& name, const std::vector<script_value>& arguments = {});
|
||||
|
||||
@@ -26,14 +23,6 @@ namespace scripting
|
||||
}
|
||||
|
||||
script_value exec_ent_thread(const entity& entity, const char* pos, const std::vector<script_value>& arguments);
|
||||
script_value call_script_function(const entity& entity, const std::string& filename,
|
||||
const std::string& function, const std::vector<script_value>& arguments);
|
||||
|
||||
void clear_entity_fields(const entity& entity);
|
||||
void clear_custom_fields();
|
||||
|
||||
void set_entity_field(const entity& entity, const std::string& field, const script_value& value);
|
||||
script_value get_entity_field(const entity& entity, const std::string& field);
|
||||
|
||||
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
|
||||
|
||||
|
||||
@@ -11,10 +11,9 @@ namespace scripting
|
||||
|
||||
script_value function::get_raw() const
|
||||
{
|
||||
game::VariableValue value;
|
||||
game::VariableValue value{};
|
||||
value.type = game::SCRIPT_FUNCTION;
|
||||
value.u.codePosValue = this->pos_;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -23,7 +22,7 @@ namespace scripting
|
||||
return this->pos_;
|
||||
}
|
||||
|
||||
script_value function::call(const entity& self, std::vector<script_value> arguments) const
|
||||
script_value function::call(const entity& self, const std::vector<script_value>& arguments) const
|
||||
{
|
||||
return exec_ent_thread(self, this->pos_, arguments);
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ namespace scripting
|
||||
script_value get_raw() const;
|
||||
const char* get_pos() const;
|
||||
|
||||
script_value call(const entity& self, std::vector<script_value> arguments) const;
|
||||
script_value call(const entity& self, const std::vector<script_value>& arguments) const;
|
||||
|
||||
script_value operator()(const entity& self, std::vector<script_value> arguments) const
|
||||
script_value operator()(const entity& self, const std::vector<script_value>& arguments) const
|
||||
{
|
||||
return this->call(self, arguments);
|
||||
}
|
||||
|
||||
script_value operator()(std::vector<script_value> arguments) const
|
||||
script_value operator()(const std::vector<script_value>& arguments) const
|
||||
{
|
||||
return this->call(*game::levelEntityId, arguments);
|
||||
}
|
||||
@@ -28,7 +28,9 @@ namespace scripting
|
||||
{
|
||||
return this->call(*game::levelEntityId, {});
|
||||
}
|
||||
|
||||
private:
|
||||
const char* pos_;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,106 +0,0 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "functions.hpp"
|
||||
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::unordered_map<std::string, unsigned> lowercase_map(
|
||||
const std::unordered_map<std::string, unsigned>& old_map)
|
||||
{
|
||||
std::unordered_map<std::string, unsigned> new_map{};
|
||||
for (auto& entry : old_map)
|
||||
{
|
||||
new_map[utils::string::to_lower(entry.first)] = entry.second;
|
||||
}
|
||||
|
||||
return new_map;
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, unsigned>& get_methods()
|
||||
{
|
||||
static auto methods = lowercase_map(method_map);
|
||||
return methods;
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, unsigned>& get_functions()
|
||||
{
|
||||
static auto function = lowercase_map(function_map);
|
||||
return function;
|
||||
}
|
||||
|
||||
int find_function_index(const std::string& name, const bool prefer_global)
|
||||
{
|
||||
const auto target = utils::string::to_lower(name);
|
||||
|
||||
const auto& primary_map = prefer_global
|
||||
? get_functions()
|
||||
: get_methods();
|
||||
const auto& secondary_map = !prefer_global
|
||||
? get_functions()
|
||||
: get_methods();
|
||||
|
||||
auto function_entry = primary_map.find(target);
|
||||
if (function_entry != primary_map.end())
|
||||
{
|
||||
return function_entry->second;
|
||||
}
|
||||
|
||||
function_entry = secondary_map.find(target);
|
||||
if (function_entry != secondary_map.end())
|
||||
{
|
||||
return function_entry->second;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
script_function get_function_by_index(const unsigned index)
|
||||
{
|
||||
static const auto function_table = game::plutonium::function_table.get();
|
||||
static const auto method_table = game::plutonium::method_table.get();
|
||||
|
||||
if (index < 0x1C7)
|
||||
{
|
||||
return reinterpret_cast<script_function*>(function_table)[index];
|
||||
}
|
||||
|
||||
return reinterpret_cast<script_function*>(method_table)[index - 0x8000];
|
||||
}
|
||||
}
|
||||
|
||||
std::string find_token(unsigned int id)
|
||||
{
|
||||
for (const auto& token : token_map)
|
||||
{
|
||||
if (token.second == id)
|
||||
{
|
||||
return token.first;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
int find_token_id(const std::string& name)
|
||||
{
|
||||
const auto result = token_map.find(name);
|
||||
|
||||
if (result != token_map.end())
|
||||
{
|
||||
return result->second;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
script_function find_function(const std::string& name, const bool prefer_global)
|
||||
{
|
||||
const auto index = find_function_index(name, prefer_global);
|
||||
if (index < 0) return nullptr;
|
||||
|
||||
return get_function_by_index(index);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
#include "game/game.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
extern std::unordered_map<std::string, unsigned> method_map;
|
||||
extern std::unordered_map<std::string, unsigned> function_map;
|
||||
extern std::unordered_map<std::string, unsigned> token_map;
|
||||
extern std::unordered_map<unsigned, std::string> file_list;
|
||||
|
||||
using script_function = void(*)(game::scr_entref_t);
|
||||
|
||||
script_function find_function(const std::string& name, const bool prefer_global);
|
||||
int find_token_id(const std::string& name);
|
||||
std::string find_token(unsigned int id);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "object.hpp"
|
||||
#include "script_value.hpp"
|
||||
#include "execution.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
object::object(const unsigned int id)
|
||||
: id_(id)
|
||||
{
|
||||
this->add();
|
||||
}
|
||||
|
||||
object::object(const object& other)
|
||||
: object(other.id_)
|
||||
{
|
||||
}
|
||||
|
||||
object::object(object&& other) noexcept
|
||||
{
|
||||
this->id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
}
|
||||
|
||||
object::object()
|
||||
{
|
||||
this->id_ = make_object();
|
||||
}
|
||||
|
||||
object::~object()
|
||||
{
|
||||
this->release();
|
||||
}
|
||||
|
||||
object& object::operator=(const object& other)
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
this->release();
|
||||
this->id_ = other.id_;
|
||||
this->add();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
object& object::operator=(object&& other) noexcept
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
this->release();
|
||||
this->id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void object::add() const
|
||||
{
|
||||
if (this->id_)
|
||||
{
|
||||
game::AddRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
|
||||
}
|
||||
}
|
||||
|
||||
void object::release() const
|
||||
{
|
||||
if (this->id_)
|
||||
{
|
||||
game::RemoveRefToValue(game::SCRIPT_OBJECT, {static_cast<int>(this->id_)});
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int object::size() const
|
||||
{
|
||||
return game::Scr_GetSelf(this->id_);
|
||||
}
|
||||
|
||||
unsigned int object::get_entity_id() const
|
||||
{
|
||||
return this->id_;
|
||||
}
|
||||
|
||||
entity object::get_raw() const
|
||||
{
|
||||
return entity(this->id_);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "game/game.hpp"
|
||||
#include "script_value.hpp"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
class object final
|
||||
{
|
||||
public:
|
||||
object();
|
||||
object(const unsigned int);
|
||||
|
||||
object(const object& other);
|
||||
object(object&& other) noexcept;
|
||||
|
||||
~object();
|
||||
|
||||
object& operator=(const object& other);
|
||||
object& operator=(object&& other) noexcept;
|
||||
|
||||
unsigned int size() const;
|
||||
unsigned int get_entity_id() const;
|
||||
|
||||
entity get_raw() const;
|
||||
|
||||
private:
|
||||
void add() const;
|
||||
void release() const;
|
||||
|
||||
unsigned int id_;
|
||||
};
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "safe_execution.hpp"
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4611)
|
||||
|
||||
namespace scripting::safe_execution
|
||||
{
|
||||
namespace
|
||||
{
|
||||
bool execute_with_seh(const script_function function, const game::scr_entref_t& entref)
|
||||
{
|
||||
__try
|
||||
{
|
||||
function(entref);
|
||||
return true;
|
||||
}
|
||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool call(const script_function function, const game::scr_entref_t& entref)
|
||||
{
|
||||
*game::g_script_error_level += 1;
|
||||
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
|
||||
{
|
||||
*game::g_script_error_level -= 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto result = execute_with_seh(function, entref);
|
||||
*game::g_script_error_level -= 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool set_entity_field(const game::scr_entref_t& entref, const int offset)
|
||||
{
|
||||
*game::g_script_error_level += 1;
|
||||
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
|
||||
{
|
||||
*game::g_script_error_level -= 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
game::Scr_SetObjectField(entref.classnum, entref.entnum, offset);
|
||||
|
||||
*game::g_script_error_level -= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool get_entity_field(const game::scr_entref_t& entref, const int offset, game::VariableValue* value)
|
||||
{
|
||||
*game::g_script_error_level += 1;
|
||||
if (game::_setjmp(&game::g_script_error[*game::g_script_error_level], 0, 0, 0))
|
||||
{
|
||||
value->type = game::SCRIPT_NONE;
|
||||
value->u.intValue = 0;
|
||||
*game::g_script_error_level -= 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto _value = game::GetEntityFieldValue(entref.classnum, entref.entnum, offset);
|
||||
value->type = _value.type;
|
||||
value->u = _value.u;
|
||||
|
||||
*game::g_script_error_level -= 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include "functions.hpp"
|
||||
|
||||
namespace scripting::safe_execution
|
||||
{
|
||||
bool call(script_function function, const game::scr_entref_t& entref);
|
||||
|
||||
bool set_entity_field(const game::scr_entref_t& entref, int offset);
|
||||
bool get_entity_field(const game::scr_entref_t& entref, int offset, game::VariableValue* value);
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "script_value.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "array.hpp"
|
||||
#include "object.hpp"
|
||||
#include "function.hpp"
|
||||
|
||||
namespace scripting
|
||||
@@ -117,6 +118,15 @@ namespace scripting
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
script_value::script_value(const object& value)
|
||||
{
|
||||
game::VariableValue variable{};
|
||||
variable.type = game::SCRIPT_OBJECT;
|
||||
variable.u.pointerValue = value.get_entity_id();
|
||||
|
||||
this->value_ = variable;
|
||||
}
|
||||
|
||||
script_value::script_value(const function& value)
|
||||
{
|
||||
game::VariableValue variable{};
|
||||
@@ -142,6 +152,12 @@ namespace scripting
|
||||
return this->is<int>();
|
||||
}
|
||||
|
||||
template <>
|
||||
bool script_value::is<unsigned short>() const
|
||||
{
|
||||
return this->is<int>();
|
||||
}
|
||||
|
||||
template <>
|
||||
bool script_value::is<bool>() const
|
||||
{
|
||||
@@ -160,6 +176,12 @@ namespace scripting
|
||||
return this->get_raw().u.uintValue;
|
||||
}
|
||||
|
||||
template <>
|
||||
unsigned short script_value::get() const
|
||||
{
|
||||
return static_cast<unsigned short>(this->get_raw().u.uintValue);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool script_value::get() const
|
||||
{
|
||||
@@ -262,6 +284,32 @@ namespace scripting
|
||||
return array(this->get_raw().u.uintValue);
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************
|
||||
* Array
|
||||
**************************************************************/
|
||||
|
||||
template <>
|
||||
bool script_value::is<object>() const
|
||||
{
|
||||
if (this->get_raw().type != game::SCRIPT_OBJECT)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto id = this->get_raw().u.uintValue;
|
||||
const auto type = game::scr_VarGlob->objectVariableValue[id].w.type;
|
||||
|
||||
return type == game::SCRIPT_STRUCT;
|
||||
}
|
||||
|
||||
template <>
|
||||
object script_value::get() const
|
||||
{
|
||||
return object(this->get_raw().u.uintValue);
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************
|
||||
* Struct
|
||||
**************************************************************/
|
||||
@@ -321,7 +369,45 @@ namespace scripting
|
||||
return this->value_.get();
|
||||
}
|
||||
|
||||
value_wrap::value_wrap(const scripting::script_value& value, int argument_index)
|
||||
std::string script_value::to_string() const
|
||||
{
|
||||
if (this->is<int>())
|
||||
{
|
||||
return utils::string::va("%i", this->as<int>());
|
||||
}
|
||||
|
||||
if (this->is<float>())
|
||||
{
|
||||
return utils::string::va("%f", this->as<float>());
|
||||
}
|
||||
|
||||
if (this->is<std::string>())
|
||||
{
|
||||
return this->as<std::string>();
|
||||
}
|
||||
|
||||
if (this->is<vector>())
|
||||
{
|
||||
const auto vec = this->as<vector>();
|
||||
return utils::string::va("(%g, %g, %g)",
|
||||
vec.get_x(),
|
||||
vec.get_y(),
|
||||
vec.get_z()
|
||||
);
|
||||
}
|
||||
|
||||
if (this->is<function>())
|
||||
{
|
||||
const auto func = this->as<function>();
|
||||
const auto pos = func.get_pos();
|
||||
return utils::string::va("[[ function: %p ]]", pos);
|
||||
}
|
||||
|
||||
return this->type_name();
|
||||
}
|
||||
|
||||
|
||||
value_wrap::value_wrap(const scripting::script_value& value, const std::uint32_t argument_index)
|
||||
: value_(value)
|
||||
, argument_index_(argument_index)
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace scripting
|
||||
{
|
||||
class entity;
|
||||
class array;
|
||||
class object;
|
||||
class function;
|
||||
class value_wrap;
|
||||
|
||||
@@ -82,6 +83,11 @@ namespace scripting
|
||||
return "array";
|
||||
}
|
||||
|
||||
if (info == typeid(object))
|
||||
{
|
||||
return "object";
|
||||
}
|
||||
|
||||
if (info == typeid(function))
|
||||
{
|
||||
return "function";
|
||||
@@ -121,6 +127,8 @@ namespace scripting
|
||||
|
||||
script_value(const array& value);
|
||||
|
||||
script_value(const object& value);
|
||||
|
||||
script_value(const function& value);
|
||||
|
||||
template <typename T>
|
||||
@@ -153,8 +161,15 @@ namespace scripting
|
||||
return reinterpret_cast<T*>(value);
|
||||
}
|
||||
|
||||
std::string type_name() const
|
||||
{
|
||||
return get_typename(this->get_raw());
|
||||
}
|
||||
|
||||
const game::VariableValue& get_raw() const;
|
||||
|
||||
std::string to_string() const;
|
||||
|
||||
variable_value value_{};
|
||||
private:
|
||||
template <typename T>
|
||||
@@ -164,7 +179,7 @@ namespace scripting
|
||||
class value_wrap
|
||||
{
|
||||
public:
|
||||
value_wrap(const scripting::script_value& value, int argument_index);
|
||||
value_wrap(const scripting::script_value& value, const std::uint32_t);
|
||||
|
||||
template <typename T>
|
||||
T as() const
|
||||
@@ -179,7 +194,7 @@ namespace scripting
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename I = int>
|
||||
template <typename T>
|
||||
T* as_ptr()
|
||||
{
|
||||
try
|
||||
@@ -203,7 +218,7 @@ namespace scripting
|
||||
return this->value_.get_raw();
|
||||
}
|
||||
|
||||
int argument_index_{};
|
||||
std::uint32_t argument_index_{};
|
||||
scripting::script_value value_;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ namespace scripting
|
||||
void set_z(float value);
|
||||
|
||||
private:
|
||||
game::vec3_t value_{ 0 };
|
||||
game::vec3_t value_{};
|
||||
};
|
||||
}
|
||||
+91
-3
@@ -26,20 +26,35 @@ namespace game
|
||||
const char** argv[8];
|
||||
};
|
||||
|
||||
enum netsrc_t
|
||||
{
|
||||
NS_CLIENT1 = 0x0,
|
||||
NS_CLIENT2 = 0x1,
|
||||
NS_CLIENT3 = 0x2,
|
||||
NS_CLIENT4 = 0x3,
|
||||
NS_MAXCLIENTS = 0x4,
|
||||
NS_SERVER = 0x4,
|
||||
NS_PACKET = 0x5,
|
||||
NS_INVALID_NETSRC = 0x6
|
||||
};
|
||||
|
||||
struct msg_t
|
||||
{
|
||||
int overflowed;
|
||||
int readOnly;
|
||||
char* data;
|
||||
char* splitData;
|
||||
unsigned char* data;
|
||||
unsigned char* splitData;
|
||||
int maxsize;
|
||||
int cursize;
|
||||
int splitSize;
|
||||
int readcount;
|
||||
int bit;
|
||||
int lastEntityRef;
|
||||
netsrc_t targetLocalNetID;
|
||||
};
|
||||
|
||||
static_assert(sizeof(msg_t) == 0x2C);
|
||||
|
||||
struct XZoneInfo
|
||||
{
|
||||
const char* name;
|
||||
@@ -328,9 +343,82 @@ namespace game
|
||||
char __pad2[0xEC];
|
||||
};
|
||||
|
||||
enum netadrtype_t
|
||||
{
|
||||
NA_BOT = 0x0,
|
||||
NA_BAD = 0x1,
|
||||
NA_LOOPBACK = 0x2,
|
||||
NA_BROADCAST = 0x3,
|
||||
NA_IP = 0x4
|
||||
};
|
||||
|
||||
struct netadr_s
|
||||
{
|
||||
netadrtype_t type;
|
||||
unsigned char ip[4];
|
||||
unsigned __int16 port;
|
||||
unsigned char ipx[10];
|
||||
unsigned int addrHandleIndex;
|
||||
};
|
||||
|
||||
static_assert(sizeof(netadr_s) == 24);
|
||||
|
||||
struct netchan_t
|
||||
{
|
||||
int outgoingSequence;
|
||||
netsrc_t sock;
|
||||
int dropped;
|
||||
int incomingSequence;
|
||||
netadr_s remoteAddress;
|
||||
int qport;
|
||||
int fragmentSequence;
|
||||
int fragmentLength;
|
||||
unsigned char* fragmentBuffer;
|
||||
int fragmentBufferSize;
|
||||
int unsentFragments;
|
||||
int unsentFragmentStart;
|
||||
int unsentLength;
|
||||
unsigned char* unsentBuffer;
|
||||
int unsentBufferSize;
|
||||
unsigned char __pad0[0x5E0];
|
||||
};
|
||||
|
||||
static_assert(sizeof(netchan_t) == 0x630);
|
||||
|
||||
enum clientState_t
|
||||
{
|
||||
CS_FREE = 0,
|
||||
CS_ZOMBIE = 1,
|
||||
CS_RECONNECTING = 2,
|
||||
CS_CONNECTED = 3,
|
||||
CS_CLIENTLOADING = 4,
|
||||
CS_ACTIVE = 5
|
||||
};
|
||||
|
||||
struct clientHeader_t
|
||||
{
|
||||
clientState_t state; // 0
|
||||
int sendAsActive; // 4
|
||||
int deltaMessage; // 8
|
||||
int rateDealyed; // 12
|
||||
int hasAckedBaselineData; // 16
|
||||
int hugeSnapshotSent; // 20
|
||||
netchan_t netchan; // 24
|
||||
vec3_t predictedOrigin;
|
||||
int predictedOriginServerTime;
|
||||
int migrationState;
|
||||
vec3_t predictedVehicleOrigin;
|
||||
int predictedVehicleServerTime;
|
||||
};
|
||||
|
||||
static_assert(sizeof(clientHeader_t) == 0x66C);
|
||||
|
||||
struct client_s
|
||||
{
|
||||
char __pad0[0x41CB2];
|
||||
clientHeader_t header;
|
||||
const char* dropReason;
|
||||
char userinfo[1024];
|
||||
char __pad0[0x41242];
|
||||
unsigned __int16 scriptId; // 269490
|
||||
int bIsTestClient; // 269492
|
||||
int serverId; // 269496
|
||||
|
||||
+5
-13
@@ -25,8 +25,6 @@ namespace game
|
||||
|
||||
WEAK symbol<const dvar_t*(const char*)> Dvar_FindVar{0x5BDCC0};
|
||||
|
||||
WEAK symbol<char*(const char*)> I_CleanStr{0x0};
|
||||
|
||||
WEAK symbol<VariableValue(unsigned int classnum, int entnum, int offset)> GetEntityFieldValue{0x56AF20};
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindVariable{0x5651F0};
|
||||
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindObject{0x565BD0};
|
||||
@@ -50,6 +48,7 @@ namespace game
|
||||
WEAK symbol<void()> Scr_MakeArray{0x56ADE0};
|
||||
WEAK symbol<void(unsigned int stringValue)> Scr_AddArrayStringIndexed{0x56AE70};
|
||||
WEAK symbol<void(unsigned int classnum, unsigned int name, unsigned int canonicalString, unsigned int offset)> Scr_AddClassField{0x567CD0};
|
||||
WEAK symbol<void()> Scr_ErrorInternal{0x568FD0};
|
||||
|
||||
WEAK symbol<unsigned int(const char* str, unsigned int user)> SL_GetString{0x5649E0};
|
||||
WEAK symbol<unsigned int(const char* str)> SL_GetCanonicalString{0x5619A0};
|
||||
@@ -57,6 +56,7 @@ namespace game
|
||||
|
||||
WEAK symbol<void(int clientNum, int type, const char* command)> SV_GameSendServerCommand{0x573220};
|
||||
WEAK symbol<void(int arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{0x5459F0};
|
||||
WEAK symbol<void(client_s* drop, const char* reason, bool tellThem)> SV_DropClient{0x570980};
|
||||
|
||||
WEAK symbol<void(unsigned int notifyListOwnerId, unsigned int stringValue, VariableValue* top)> VM_Notify{0x569720};
|
||||
WEAK symbol<unsigned int(unsigned int localId, const char* pos, unsigned int paramcount)> VM_Execute{0x56DFE0};
|
||||
@@ -79,15 +79,7 @@ namespace game
|
||||
WEAK symbol<gentity_s> g_entities{0x1A66E28};
|
||||
WEAK symbol<unsigned int> levelEntityId{0x208E1A4};
|
||||
|
||||
WEAK symbol<dvar_t> sv_maxclients{0x1BA0E4C};
|
||||
WEAK symbol<int> svs_clientCount{0x4B5CF8C};
|
||||
WEAK symbol<client_s> svs_clients{0x4B5CF90};
|
||||
|
||||
namespace plutonium
|
||||
{
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0x206964D0};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x206964F0};
|
||||
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0x20696530};
|
||||
WEAK symbol<int(const char* fmt, ...)> printf{0x20887840};
|
||||
WEAK symbol<void*> function_table{0x2068F210};
|
||||
WEAK symbol<void*> method_table{0x2068F9E0};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "../plugin.hpp"
|
||||
|
||||
class component_interface
|
||||
{
|
||||
@@ -7,25 +8,24 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
virtual void post_start()
|
||||
virtual void on_startup([[maybe_unused]] plugin::plugin* plugin)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void post_load()
|
||||
virtual void on_dvar_init([[maybe_unused]] plugin::plugin* plugin)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void pre_destroy()
|
||||
virtual void on_after_dvar_init([[maybe_unused]] plugin::plugin* plugin)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void post_unpack()
|
||||
virtual void on_game_init([[maybe_unused]] plugin::plugin* plugin)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void* load_import([[maybe_unused]] const std::string& library, [[maybe_unused]] const std::string& function)
|
||||
virtual void on_shutdown([[maybe_unused]] plugin::plugin* plugin)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual bool is_supported()
|
||||
|
||||
@@ -1,87 +1,48 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "component_loader.hpp"
|
||||
|
||||
void component_loader::register_component(std::unique_ptr<component_interface>&& component_)
|
||||
void component_loader::register_component(const std::string& name, std::unique_ptr<component_interface>&& component_)
|
||||
{
|
||||
get_components().push_back(std::move(component_));
|
||||
get_components().push_back(std::make_pair(name, std::move(component_)));
|
||||
}
|
||||
|
||||
bool component_loader::post_start()
|
||||
{
|
||||
static auto handled = false;
|
||||
if (handled) return true;
|
||||
handled = true;
|
||||
#define ON_CALLBACK(__name__) \
|
||||
void component_loader::__name__() \
|
||||
{ \
|
||||
static auto handled = false; \
|
||||
if (handled) \
|
||||
{ \
|
||||
return; \
|
||||
} \
|
||||
\
|
||||
handled = true; \
|
||||
\
|
||||
for (const auto& component_ : get_components()) \
|
||||
{ \
|
||||
try \
|
||||
{ \
|
||||
component_.second->__name__(plugin::get()); \
|
||||
} \
|
||||
catch (const std::exception& e) \
|
||||
{ \
|
||||
printf("error executing component \"%s\" callback \"%s\": %s\n", component_.first.data(), #__name__, e.what()); \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
|
||||
try
|
||||
{
|
||||
for (const auto& component_ : get_components())
|
||||
{
|
||||
component_->post_start();
|
||||
}
|
||||
}
|
||||
catch (premature_shutdown_trigger&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool component_loader::post_load()
|
||||
{
|
||||
static auto handled = false;
|
||||
if (handled) return true;
|
||||
handled = true;
|
||||
|
||||
clean();
|
||||
|
||||
try
|
||||
{
|
||||
for (const auto& component_ : get_components())
|
||||
{
|
||||
component_->post_load();
|
||||
}
|
||||
}
|
||||
catch (premature_shutdown_trigger&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void component_loader::post_unpack()
|
||||
{
|
||||
static auto handled = false;
|
||||
if (handled) return;
|
||||
handled = true;
|
||||
|
||||
for (const auto& component_ : get_components())
|
||||
{
|
||||
component_->post_unpack();
|
||||
}
|
||||
}
|
||||
|
||||
void component_loader::pre_destroy()
|
||||
{
|
||||
static auto handled = false;
|
||||
if (handled) return;
|
||||
handled = true;
|
||||
|
||||
for (const auto& component_ : get_components())
|
||||
{
|
||||
component_->pre_destroy();
|
||||
}
|
||||
}
|
||||
ON_CALLBACK(on_startup);
|
||||
ON_CALLBACK(on_dvar_init);
|
||||
ON_CALLBACK(on_after_dvar_init);
|
||||
ON_CALLBACK(on_shutdown);
|
||||
|
||||
void component_loader::clean()
|
||||
{
|
||||
auto& components = get_components();
|
||||
for (auto i = components.begin(); i != components.end();)
|
||||
{
|
||||
if (!(*i)->is_supported())
|
||||
if (!(*i).second->is_supported())
|
||||
{
|
||||
(*i)->pre_destroy();
|
||||
(*i).second->on_shutdown(plugin::get());
|
||||
i = components.erase(i);
|
||||
}
|
||||
else
|
||||
@@ -91,35 +52,19 @@ void component_loader::clean()
|
||||
}
|
||||
}
|
||||
|
||||
void* component_loader::load_import(const std::string& library, const std::string& function)
|
||||
{
|
||||
void* function_ptr = nullptr;
|
||||
|
||||
for (const auto& component_ : get_components())
|
||||
{
|
||||
auto* const component_function_ptr = component_->load_import(library, function);
|
||||
if (component_function_ptr)
|
||||
{
|
||||
function_ptr = component_function_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return function_ptr;
|
||||
}
|
||||
|
||||
void component_loader::trigger_premature_shutdown()
|
||||
{
|
||||
throw premature_shutdown_trigger();
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<component_interface>>& component_loader::get_components()
|
||||
std::vector<std::pair<std::string, std::unique_ptr<component_interface>>>& component_loader::get_components()
|
||||
{
|
||||
using component_vector = std::vector<std::unique_ptr<component_interface>>;
|
||||
using component_vector = std::vector<std::pair<std::string, std::unique_ptr<component_interface>>>;
|
||||
using component_vector_container = std::unique_ptr<component_vector, std::function<void(component_vector*)>>;
|
||||
|
||||
static component_vector_container components(new component_vector, [](component_vector* component_vector)
|
||||
{
|
||||
pre_destroy();
|
||||
on_shutdown();
|
||||
delete component_vector;
|
||||
});
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ public:
|
||||
static_assert(std::is_base_of<component_interface, T>::value, "component has invalid base class");
|
||||
|
||||
public:
|
||||
installer()
|
||||
installer(const std::string& name)
|
||||
{
|
||||
register_component(std::make_unique<T>());
|
||||
register_component(name, std::make_unique<T>());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,12 +38,12 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void register_component(std::unique_ptr<component_interface>&& component);
|
||||
static void register_component(const std::string& name, std::unique_ptr<component_interface>&& component);
|
||||
|
||||
static bool post_start();
|
||||
static bool post_load();
|
||||
static void post_unpack();
|
||||
static void pre_destroy();
|
||||
static void on_startup();
|
||||
static void on_dvar_init();
|
||||
static void on_after_dvar_init();
|
||||
static void on_shutdown();
|
||||
static void clean();
|
||||
|
||||
static void* load_import(const std::string& library, const std::string& function);
|
||||
@@ -51,11 +51,11 @@ public:
|
||||
static void trigger_premature_shutdown();
|
||||
|
||||
private:
|
||||
static std::vector<std::unique_ptr<component_interface>>& get_components();
|
||||
static std::vector<std::pair<std::string, std::unique_ptr<component_interface>>>& get_components();
|
||||
};
|
||||
|
||||
#define REGISTER_COMPONENT(name) \
|
||||
#define REGISTER_COMPONENT(name) \
|
||||
namespace \
|
||||
{ \
|
||||
static component_loader::installer<name> __component; \
|
||||
static component_loader::installer<name> __component(#name); \
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "plugin.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace plugin
|
||||
{
|
||||
namespace
|
||||
{
|
||||
void printf_stub(const char* fmt, ...)
|
||||
{
|
||||
char buffer[0x2000] = {};
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
|
||||
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, fmt, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
get()->get_interface()->logging()->info(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
std::uint32_t plugin::plugin_version()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* plugin::plugin_name()
|
||||
{
|
||||
return "iw5-gsc-utils";
|
||||
}
|
||||
|
||||
bool plugin::is_game_supported([[maybe_unused]] plutonium::sdk::game game)
|
||||
{
|
||||
return game == plutonium::sdk::game::iw5;
|
||||
}
|
||||
|
||||
void plugin::on_startup(plutonium::sdk::iinterface* interface_ptr, plutonium::sdk::game game)
|
||||
{
|
||||
this->interface_ = interface_ptr;
|
||||
this->game_ = game;
|
||||
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), printf_stub);
|
||||
component_loader::on_startup();
|
||||
|
||||
interface_ptr->callbacks()->on_dvar_init(component_loader::on_dvar_init);
|
||||
interface_ptr->callbacks()->on_after_dvar_init(component_loader::on_after_dvar_init);
|
||||
}
|
||||
|
||||
void plugin::on_shutdown()
|
||||
{
|
||||
component_loader::on_shutdown();
|
||||
}
|
||||
|
||||
plutonium::sdk::iinterface* plugin::get_interface()
|
||||
{
|
||||
return this->interface_;
|
||||
}
|
||||
|
||||
plutonium::sdk::game plugin::get_game()
|
||||
{
|
||||
return this->game_;
|
||||
}
|
||||
|
||||
plugin* get()
|
||||
{
|
||||
static plugin instance;
|
||||
return &instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <plutonium_sdk.hpp>
|
||||
|
||||
namespace plugin
|
||||
{
|
||||
class plugin : public plutonium::sdk::plugin
|
||||
{
|
||||
public:
|
||||
~plugin() = default;
|
||||
|
||||
std::uint32_t plugin_version() override;
|
||||
const char* plugin_name() override;
|
||||
|
||||
bool is_game_supported([[maybe_unused]] plutonium::sdk::game game) override;
|
||||
|
||||
void on_startup(plutonium::sdk::iinterface* interface_ptr, plutonium::sdk::game game) override;
|
||||
void on_shutdown() override;
|
||||
|
||||
plutonium::sdk::iinterface* get_interface();
|
||||
plutonium::sdk::game get_game();
|
||||
|
||||
private:
|
||||
plutonium::sdk::iinterface* interface_{};
|
||||
plutonium::sdk::game game_{};
|
||||
|
||||
};
|
||||
|
||||
plugin* get();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define LIBMYSQL_DLL 100
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "resource.hpp"
|
||||
|
||||
LIBMYSQL_DLL RCDATA "../../deps/mysql/lib/libmysql.dll"
|
||||
+24
-1
@@ -1 +1,24 @@
|
||||
#include <stdinc.hpp>
|
||||
#include <stdinc.hpp>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int s_read_arc4random(void*, size_t)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int s_read_getrandom(void*, size_t)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int s_read_urandom(void*, size_t)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int s_read_ltm_rng(void*, size_t)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
+12
-2
@@ -12,6 +12,9 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
@@ -28,21 +31,28 @@
|
||||
#include <map>
|
||||
#include <csetjmp>
|
||||
#include <atlcomcli.h>
|
||||
#include <Psapi.h>
|
||||
|
||||
#pragma comment(lib, "urlmon.lib")
|
||||
#pragma comment(lib, "libmysql.lib")
|
||||
#pragma comment(lib, "delayimp.lib")
|
||||
#pragma comment(lib, "secur32.lib")
|
||||
|
||||
#include "resource.hpp"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
#define __has_warning(...) 0
|
||||
|
||||
#include <gsl/gsl>
|
||||
#include <MinHook.h>
|
||||
|
||||
#include "utils/memory.hpp"
|
||||
#include "utils/string.hpp"
|
||||
#include "utils/hook.hpp"
|
||||
#include "utils/concurrent_list.hpp"
|
||||
#include "utils/io.hpp"
|
||||
#include "utils/concurrency.hpp"
|
||||
#include "utils/http.hpp"
|
||||
|
||||
#include "game/structs.hpp"
|
||||
#include "game/game.hpp"
|
||||
#include "game/game.hpp"
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "binary_resource.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include "nt.hpp"
|
||||
#include "io.hpp"
|
||||
|
||||
namespace utils
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::string get_temp_folder()
|
||||
{
|
||||
char path[MAX_PATH] = {0};
|
||||
if (!GetTempPathA(sizeof(path), path))
|
||||
{
|
||||
throw std::runtime_error("Unable to get temp path");
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string write_exitisting_temp_file(const std::string& file, const std::string& data,
|
||||
const bool fatal_if_overwrite_fails)
|
||||
{
|
||||
const auto temp = get_temp_folder();
|
||||
auto file_path = temp + file;
|
||||
|
||||
std::string current_data;
|
||||
if (!io::read_file(file_path, ¤t_data))
|
||||
{
|
||||
if (!io::write_file(file_path, data))
|
||||
{
|
||||
throw std::runtime_error("Failed to write file: " + file_path);
|
||||
}
|
||||
|
||||
return file_path;
|
||||
}
|
||||
|
||||
if (current_data == data || io::write_file(file_path, data) || !fatal_if_overwrite_fails)
|
||||
{
|
||||
return file_path;
|
||||
}
|
||||
|
||||
throw std::runtime_error(
|
||||
"Temporary file was already written, but differs. It can't be overwritten as it's still in use: " +
|
||||
file_path);
|
||||
}
|
||||
}
|
||||
|
||||
binary_resource::binary_resource(const int id, std::string file)
|
||||
: filename_(std::move(file))
|
||||
{
|
||||
this->resource_ = nt::load_resource(id);
|
||||
|
||||
if (this->resource_.empty())
|
||||
{
|
||||
throw std::runtime_error("Unable to load resource: " + std::to_string(id));
|
||||
}
|
||||
}
|
||||
|
||||
std::string binary_resource::get_extracted_file(const bool fatal_if_overwrite_fails)
|
||||
{
|
||||
if (this->path_.empty())
|
||||
{
|
||||
this->path_ = write_exitisting_temp_file(this->filename_, this->resource_, fatal_if_overwrite_fails);
|
||||
}
|
||||
|
||||
return this->path_;
|
||||
}
|
||||
|
||||
const std::string& binary_resource::get_data() const
|
||||
{
|
||||
return this->resource_;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
class binary_resource
|
||||
{
|
||||
public:
|
||||
binary_resource(int id, std::string file);
|
||||
|
||||
std::string get_extracted_file(bool fatal_if_overwrite_fails = false);
|
||||
const std::string& get_data() const;
|
||||
|
||||
private:
|
||||
std::string resource_;
|
||||
std::string filename_;
|
||||
std::string path_;
|
||||
};
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
|
||||
// This class is trash. Need to get rid of it.
|
||||
|
||||
namespace utils
|
||||
{
|
||||
template <typename T>
|
||||
class concurrent_list final
|
||||
{
|
||||
public:
|
||||
class element final
|
||||
{
|
||||
public:
|
||||
explicit element(std::recursive_mutex* mutex, std::shared_ptr<T> entry = {},
|
||||
std::shared_ptr<element> next = {}) :
|
||||
mutex_(mutex),
|
||||
entry_(std::move(entry)),
|
||||
next_(std::move(next))
|
||||
{
|
||||
}
|
||||
|
||||
void remove(const std::shared_ptr<T>& element)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
if (!this->next_) return;
|
||||
|
||||
if (this->next_->entry_.get() == element.get())
|
||||
{
|
||||
this->next_ = this->next_->next_;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->next_->remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] std::shared_ptr<element> get_next() const
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
return this->next_;
|
||||
}
|
||||
|
||||
std::shared_ptr<T> operator*() const
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
return this->entry_;
|
||||
}
|
||||
|
||||
element& operator++()
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
*this = this->next_ ? *this->next_ : element(this->mutex_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
element operator++(int)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
auto result = *this;
|
||||
this->operator++();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const element& other)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
return this->entry_.get() == other.entry_.get();
|
||||
}
|
||||
|
||||
bool operator!=(const element& other)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(*this->mutex_);
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
private:
|
||||
std::recursive_mutex* mutex_;
|
||||
std::shared_ptr<T> entry_;
|
||||
std::shared_ptr<element> next_;
|
||||
};
|
||||
|
||||
element begin()
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
return this->entry_ ? *this->entry_ : this->end();
|
||||
}
|
||||
|
||||
element end()
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
return element(&this->mutex_);
|
||||
}
|
||||
|
||||
void remove(const element& entry)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
this->remove(*entry);
|
||||
}
|
||||
|
||||
void remove(const std::shared_ptr<T>& element)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
if (!this->entry_) return;
|
||||
|
||||
if ((**this->entry_).get() == element.get())
|
||||
{
|
||||
this->entry_ = this->entry_->get_next();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->entry_->remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
void add(const T& object)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
|
||||
const auto object_ptr = std::make_shared<T>(object);
|
||||
this->entry_ = std::make_shared<element>(&this->mutex_, object_ptr, this->entry_);
|
||||
}
|
||||
|
||||
void add(T&& object)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
|
||||
const auto object_ptr = std::make_shared<T>(std::move(object));
|
||||
this->entry_ = std::make_shared<element>(&this->mutex_, object_ptr, this->entry_);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> _(this->mutex_);
|
||||
this->entry_ = {};
|
||||
}
|
||||
|
||||
private:
|
||||
std::recursive_mutex mutex_;
|
||||
std::shared_ptr<element> entry_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,691 @@
|
||||
#include <stdinc.hpp>
|
||||
|
||||
#include "cryptography.hpp"
|
||||
#include "nt.hpp"
|
||||
#include <gsl/gsl>
|
||||
|
||||
#undef max
|
||||
using namespace std::string_literals;
|
||||
|
||||
/// http://www.opensource.apple.com/source/CommonCrypto/CommonCrypto-55010/Source/libtomcrypt/doc/libTomCryptDoc.pdf
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4996)
|
||||
|
||||
namespace utils::cryptography
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct __
|
||||
{
|
||||
__()
|
||||
{
|
||||
ltc_mp = ltm_desc;
|
||||
|
||||
register_cipher(&aes_desc);
|
||||
register_cipher(&des3_desc);
|
||||
|
||||
register_prng(&sprng_desc);
|
||||
register_prng(&fortuna_desc);
|
||||
register_prng(&yarrow_desc);
|
||||
|
||||
register_hash(&sha1_desc);
|
||||
register_hash(&sha256_desc);
|
||||
register_hash(&sha512_desc);
|
||||
}
|
||||
} ___;
|
||||
|
||||
[[maybe_unused]] const char* cs(const uint8_t* data)
|
||||
{
|
||||
return reinterpret_cast<const char*>(data);
|
||||
}
|
||||
|
||||
[[maybe_unused]] char* cs(uint8_t* data)
|
||||
{
|
||||
return reinterpret_cast<char*>(data);
|
||||
}
|
||||
|
||||
[[maybe_unused]] const uint8_t* cs(const char* data)
|
||||
{
|
||||
return reinterpret_cast<const uint8_t*>(data);
|
||||
}
|
||||
|
||||
[[maybe_unused]] uint8_t* cs(char* data)
|
||||
{
|
||||
return reinterpret_cast<uint8_t*>(data);
|
||||
}
|
||||
|
||||
[[maybe_unused]] unsigned long ul(const size_t value)
|
||||
{
|
||||
return static_cast<unsigned long>(value);
|
||||
}
|
||||
|
||||
class prng
|
||||
{
|
||||
public:
|
||||
prng(const ltc_prng_descriptor& descriptor, const bool autoseed = true)
|
||||
: state_(std::make_unique<prng_state>())
|
||||
, descriptor_(descriptor)
|
||||
{
|
||||
this->id_ = register_prng(&descriptor);
|
||||
if (this->id_ == -1)
|
||||
{
|
||||
throw std::runtime_error("PRNG "s + this->descriptor_.name + " could not be registered!");
|
||||
}
|
||||
|
||||
if (autoseed)
|
||||
{
|
||||
this->auto_seed();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->descriptor_.start(this->state_.get());
|
||||
}
|
||||
}
|
||||
|
||||
~prng()
|
||||
{
|
||||
this->descriptor_.done(this->state_.get());
|
||||
}
|
||||
|
||||
prng_state* get_state() const
|
||||
{
|
||||
this->descriptor_.ready(this->state_.get());
|
||||
return this->state_.get();
|
||||
}
|
||||
|
||||
int get_id() const
|
||||
{
|
||||
return this->id_;
|
||||
}
|
||||
|
||||
void add_entropy(const void* data, const size_t length) const
|
||||
{
|
||||
this->descriptor_.add_entropy(static_cast<const uint8_t*>(data), ul(length), this->state_.get());
|
||||
}
|
||||
|
||||
void read(void* data, const size_t length) const
|
||||
{
|
||||
this->descriptor_.read(static_cast<unsigned char*>(data), ul(length), this->get_state());
|
||||
}
|
||||
|
||||
private:
|
||||
int id_;
|
||||
std::unique_ptr<prng_state> state_;
|
||||
const ltc_prng_descriptor& descriptor_;
|
||||
|
||||
void auto_seed() const
|
||||
{
|
||||
rng_make_prng(128, this->id_, this->state_.get(), nullptr);
|
||||
|
||||
int i[4]; // uninitialized data
|
||||
auto* i_ptr = &i;
|
||||
this->add_entropy(reinterpret_cast<uint8_t*>(&i), sizeof(i));
|
||||
this->add_entropy(reinterpret_cast<uint8_t*>(&i_ptr), sizeof(i_ptr));
|
||||
|
||||
auto t = time(nullptr);
|
||||
this->add_entropy(reinterpret_cast<uint8_t*>(&t), sizeof(t));
|
||||
}
|
||||
};
|
||||
|
||||
const prng prng_(fortuna_desc);
|
||||
}
|
||||
|
||||
ecc::key::key()
|
||||
{
|
||||
ZeroMemory(&this->key_storage_, sizeof(this->key_storage_));
|
||||
}
|
||||
|
||||
ecc::key::~key()
|
||||
{
|
||||
this->free();
|
||||
}
|
||||
|
||||
ecc::key::key(key&& obj) noexcept
|
||||
: key()
|
||||
{
|
||||
this->operator=(std::move(obj));
|
||||
}
|
||||
|
||||
ecc::key::key(const key& obj)
|
||||
: key()
|
||||
{
|
||||
this->operator=(obj);
|
||||
}
|
||||
|
||||
ecc::key& ecc::key::operator=(key&& obj) noexcept
|
||||
{
|
||||
if (this != &obj)
|
||||
{
|
||||
std::memmove(&this->key_storage_, &obj.key_storage_, sizeof(this->key_storage_));
|
||||
ZeroMemory(&obj.key_storage_, sizeof(obj.key_storage_));
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
ecc::key& ecc::key::operator=(const key& obj)
|
||||
{
|
||||
if (this != &obj && obj.is_valid())
|
||||
{
|
||||
this->deserialize(obj.serialize(obj.key_storage_.type));
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool ecc::key::is_valid() const
|
||||
{
|
||||
return (!memory::is_set(&this->key_storage_, 0, sizeof(this->key_storage_)));
|
||||
}
|
||||
|
||||
ecc_key& ecc::key::get()
|
||||
{
|
||||
return this->key_storage_;
|
||||
}
|
||||
|
||||
const ecc_key& ecc::key::get() const
|
||||
{
|
||||
return this->key_storage_;
|
||||
}
|
||||
|
||||
std::string ecc::key::get_public_key() const
|
||||
{
|
||||
uint8_t buffer[512] = {0};
|
||||
unsigned long length = sizeof(buffer);
|
||||
|
||||
if (ecc_ansi_x963_export(&this->key_storage_, buffer, &length) == CRYPT_OK)
|
||||
{
|
||||
return std::string(cs(buffer), length);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void ecc::key::set(const std::string& pub_key_buffer)
|
||||
{
|
||||
this->free();
|
||||
|
||||
if (ecc_ansi_x963_import(cs(pub_key_buffer.data()),
|
||||
ul(pub_key_buffer.size()),
|
||||
&this->key_storage_) != CRYPT_OK)
|
||||
{
|
||||
ZeroMemory(&this->key_storage_, sizeof(this->key_storage_));
|
||||
}
|
||||
}
|
||||
|
||||
void ecc::key::deserialize(const std::string& key)
|
||||
{
|
||||
this->free();
|
||||
|
||||
if (ecc_import(cs(key.data()), ul(key.size()),
|
||||
&this->key_storage_) != CRYPT_OK
|
||||
)
|
||||
{
|
||||
ZeroMemory(&this->key_storage_, sizeof(this->key_storage_));
|
||||
}
|
||||
}
|
||||
|
||||
std::string ecc::key::serialize(const int type) const
|
||||
{
|
||||
uint8_t buffer[4096] = {0};
|
||||
unsigned long length = sizeof(buffer);
|
||||
|
||||
if (ecc_export(buffer, &length, type, &this->key_storage_) == CRYPT_OK)
|
||||
{
|
||||
return std::string(cs(buffer), length);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
void ecc::key::free()
|
||||
{
|
||||
if (this->is_valid())
|
||||
{
|
||||
ecc_free(&this->key_storage_);
|
||||
}
|
||||
|
||||
ZeroMemory(&this->key_storage_, sizeof(this->key_storage_));
|
||||
}
|
||||
|
||||
bool ecc::key::operator==(key& key) const
|
||||
{
|
||||
return (this->is_valid() && key.is_valid() && this->serialize(PK_PUBLIC) == key.serialize(PK_PUBLIC));
|
||||
}
|
||||
|
||||
uint64_t ecc::key::get_hash() const
|
||||
{
|
||||
const auto hash = sha1::compute(this->get_public_key());
|
||||
if (hash.size() >= 8)
|
||||
{
|
||||
return *reinterpret_cast<const uint64_t*>(hash.data());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ecc::key ecc::generate_key(const int bits)
|
||||
{
|
||||
key key;
|
||||
ecc_make_key(prng_.get_state(), prng_.get_id(), bits / 8, &key.get());
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
ecc::key ecc::generate_key(const int bits, const std::string& entropy)
|
||||
{
|
||||
key key{};
|
||||
const prng yarrow(yarrow_desc, false);
|
||||
yarrow.add_entropy(entropy.data(), entropy.size());
|
||||
|
||||
ecc_make_key(yarrow.get_state(), yarrow.get_id(), bits / 8, &key.get());
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
std::string ecc::sign_message(const key& key, const std::string& message)
|
||||
{
|
||||
if (!key.is_valid()) return "";
|
||||
|
||||
uint8_t buffer[512];
|
||||
unsigned long length = sizeof(buffer);
|
||||
|
||||
ecc_sign_hash(cs(message.data()), ul(message.size()), buffer, &length, prng_.get_state(), prng_.get_id(),
|
||||
&key.get());
|
||||
|
||||
return std::string(cs(buffer), length);
|
||||
}
|
||||
|
||||
bool ecc::verify_message(const key& key, const std::string& message, const std::string& signature)
|
||||
{
|
||||
if (!key.is_valid()) return false;
|
||||
|
||||
auto result = 0;
|
||||
return (ecc_verify_hash(cs(signature.data()),
|
||||
ul(signature.size()),
|
||||
cs(message.data()),
|
||||
ul(message.size()), &result,
|
||||
&key.get()) == CRYPT_OK && result != 0);
|
||||
}
|
||||
|
||||
bool ecc::encrypt(const key& key, std::string& data)
|
||||
{
|
||||
std::string out_data{};
|
||||
out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
|
||||
|
||||
auto out_len = ul(out_data.size());
|
||||
auto crypt = [&]()
|
||||
{
|
||||
return ecc_encrypt_key(cs(data.data()), ul(data.size()), cs(out_data.data()), &out_len,
|
||||
prng_.get_state(), prng_.get_id(), find_hash("sha512"), &key.get());
|
||||
};
|
||||
|
||||
auto res = crypt();
|
||||
|
||||
if (res == CRYPT_BUFFER_OVERFLOW)
|
||||
{
|
||||
out_data.resize(out_len);
|
||||
res = crypt();
|
||||
}
|
||||
|
||||
if (res != CRYPT_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
out_data.resize(out_len);
|
||||
data = std::move(out_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ecc::decrypt(const key& key, std::string& data)
|
||||
{
|
||||
std::string out_data{};
|
||||
out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
|
||||
|
||||
auto out_len = ul(out_data.size());
|
||||
auto crypt = [&]()
|
||||
{
|
||||
return ecc_decrypt_key(cs(data.data()), ul(data.size()), cs(out_data.data()), &out_len, &key.get());
|
||||
};
|
||||
|
||||
auto res = crypt();
|
||||
|
||||
if (res == CRYPT_BUFFER_OVERFLOW)
|
||||
{
|
||||
out_data.resize(out_len);
|
||||
res = crypt();
|
||||
}
|
||||
|
||||
if (res != CRYPT_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
out_data.resize(out_len);
|
||||
data = std::move(out_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string rsa::encrypt(const std::string& data, const std::string& hash, const std::string& key)
|
||||
{
|
||||
rsa_key new_key;
|
||||
rsa_import(cs(key.data()), ul(key.size()), &new_key);
|
||||
const auto _ = gsl::finally([&]()
|
||||
{
|
||||
rsa_free(&new_key);
|
||||
});
|
||||
|
||||
|
||||
std::string out_data{};
|
||||
out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
|
||||
|
||||
auto out_len = ul(out_data.size());
|
||||
auto crypt = [&]()
|
||||
{
|
||||
return rsa_encrypt_key_ex(cs(data.data()), ul(data.size()), cs(out_data.data()), &out_len, cs(hash.data()),
|
||||
ul(hash.size()), prng_.get_state(), prng_.get_id(), find_hash("sha512"), LTC_PKCS_1_V1_5, &new_key);
|
||||
};
|
||||
|
||||
auto res = crypt();
|
||||
|
||||
if (res == CRYPT_BUFFER_OVERFLOW)
|
||||
{
|
||||
out_data.resize(out_len);
|
||||
res = crypt();
|
||||
}
|
||||
|
||||
if (res == CRYPT_OK)
|
||||
{
|
||||
out_data.resize(out_len);
|
||||
return out_data;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string des3::encrypt(const std::string& data, const std::string& iv, const std::string& key)
|
||||
{
|
||||
std::string enc_data;
|
||||
enc_data.resize(data.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
const auto des3 = find_cipher("3des");
|
||||
|
||||
cbc_start(des3, cs(iv.data()), cs(key.data()), static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_encrypt(cs(data.data()), cs(enc_data.data()), ul(data.size()), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
return enc_data;
|
||||
}
|
||||
|
||||
std::string des3::decrypt(const std::string& data, const std::string& iv, const std::string& key)
|
||||
{
|
||||
std::string dec_data;
|
||||
dec_data.resize(data.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
const auto des3 = find_cipher("3des");
|
||||
|
||||
cbc_start(des3, cs(iv.data()), cs(key.data()), static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_decrypt(cs(data.data()), cs(dec_data.data()), ul(data.size()), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
return dec_data;
|
||||
}
|
||||
|
||||
std::string tiger::compute(const std::string& data, const bool hex)
|
||||
{
|
||||
return compute(cs(data.data()), data.size(), hex);
|
||||
}
|
||||
|
||||
std::string tiger::compute(const uint8_t* data, const size_t length, const bool hex)
|
||||
{
|
||||
uint8_t buffer[24] = {0};
|
||||
|
||||
hash_state state;
|
||||
tiger_init(&state);
|
||||
tiger_process(&state, data, ul(length));
|
||||
tiger_done(&state, buffer);
|
||||
|
||||
std::string hash(cs(buffer), sizeof(buffer));
|
||||
if (!hex) return hash;
|
||||
|
||||
return string::dump_hex(hash, "");
|
||||
}
|
||||
|
||||
std::string aes::encrypt(const std::string& data, const std::string& iv, const std::string& key)
|
||||
{
|
||||
std::string aligned_data = data;
|
||||
const auto mod = aligned_data.size() % 16;
|
||||
if (mod != 0)
|
||||
{
|
||||
aligned_data.resize(aligned_data.size() + (16 - mod));
|
||||
}
|
||||
|
||||
std::string enc_data;
|
||||
enc_data.resize(aligned_data.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
const auto aes = find_cipher("aes");
|
||||
|
||||
cbc_start(aes, cs(iv.data()), cs(key.data()),
|
||||
static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_encrypt(cs(aligned_data.data()),
|
||||
cs(enc_data.data()),
|
||||
ul(aligned_data.size()), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
return enc_data;
|
||||
}
|
||||
|
||||
std::string aes::decrypt(const std::string& data, const std::string& iv, const std::string& key)
|
||||
{
|
||||
std::string dec_data;
|
||||
dec_data.resize(data.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
const auto aes = find_cipher("aes");
|
||||
|
||||
cbc_start(aes, cs(iv.data()), cs(key.data()),
|
||||
static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_decrypt(cs(data.data()),
|
||||
cs(dec_data.data()),
|
||||
ul(data.size()), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
return dec_data;
|
||||
}
|
||||
|
||||
std::string hmac_sha1::compute(const std::string& data, const std::string& key)
|
||||
{
|
||||
std::string buffer;
|
||||
buffer.resize(20);
|
||||
|
||||
hmac_state state;
|
||||
hmac_init(&state, find_hash("sha1"), cs(key.data()), ul(key.size()));
|
||||
hmac_process(&state, cs(data.data()), static_cast<int>(data.size()));
|
||||
|
||||
auto out_len = ul(buffer.size());
|
||||
hmac_done(&state, cs(buffer.data()), &out_len);
|
||||
|
||||
buffer.resize(out_len);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string sha1::compute(const std::string& data, const bool hex)
|
||||
{
|
||||
return compute(cs(data.data()), data.size(), hex);
|
||||
}
|
||||
|
||||
std::string sha1::compute(const uint8_t* data, const size_t length, const bool hex)
|
||||
{
|
||||
uint8_t buffer[20] = {0};
|
||||
|
||||
hash_state state;
|
||||
sha1_init(&state);
|
||||
sha1_process(&state, data, ul(length));
|
||||
sha1_done(&state, buffer);
|
||||
|
||||
std::string hash(cs(buffer), sizeof(buffer));
|
||||
if (!hex) return hash;
|
||||
|
||||
return string::dump_hex(hash, "");
|
||||
}
|
||||
|
||||
std::string sha256::compute(const std::string& data, const bool hex)
|
||||
{
|
||||
return compute(cs(data.data()), data.size(), hex);
|
||||
}
|
||||
|
||||
std::string sha256::compute(const uint8_t* data, const size_t length, const bool hex)
|
||||
{
|
||||
uint8_t buffer[32] = {0};
|
||||
|
||||
hash_state state;
|
||||
sha256_init(&state);
|
||||
sha256_process(&state, data, ul(length));
|
||||
sha256_done(&state, buffer);
|
||||
|
||||
std::string hash(cs(buffer), sizeof(buffer));
|
||||
if (!hex) return hash;
|
||||
|
||||
return string::dump_hex(hash, "");
|
||||
}
|
||||
|
||||
std::string sha512::compute(const std::string& data, const bool hex)
|
||||
{
|
||||
return compute(cs(data.data()), data.size(), hex);
|
||||
}
|
||||
|
||||
std::string sha512::compute(const uint8_t* data, const size_t length, const bool hex)
|
||||
{
|
||||
uint8_t buffer[64] = {0};
|
||||
|
||||
hash_state state;
|
||||
sha512_init(&state);
|
||||
sha512_process(&state, data, ul(length));
|
||||
sha512_done(&state, buffer);
|
||||
|
||||
std::string hash(cs(buffer), sizeof(buffer));
|
||||
if (!hex) return hash;
|
||||
|
||||
return string::dump_hex(hash, "");
|
||||
}
|
||||
|
||||
namespace md5
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex)
|
||||
{
|
||||
return compute(cs(data.data()), data.size(), hex);
|
||||
}
|
||||
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex)
|
||||
{
|
||||
uint8_t buffer[16] = {0};
|
||||
|
||||
hash_state state;
|
||||
md5_init(&state);
|
||||
md5_process(&state, data, ul(length));
|
||||
md5_done(&state, buffer);
|
||||
|
||||
std::string hash(cs(buffer), sizeof(buffer));
|
||||
if (!hex) return hash;
|
||||
|
||||
return string::dump_hex(hash, "");
|
||||
}
|
||||
}
|
||||
|
||||
std::string base64::encode(const uint8_t* data, const size_t len)
|
||||
{
|
||||
std::string result;
|
||||
result.resize((len + 2) * 2);
|
||||
|
||||
auto out_len = ul(result.size());
|
||||
if (base64_encode(data, ul(len), result.data(), &out_len) != CRYPT_OK)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result.resize(out_len);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string base64::encode(const std::string& data)
|
||||
{
|
||||
return base64::encode(cs(data.data()), static_cast<unsigned>(data.size()));
|
||||
}
|
||||
|
||||
std::string base64::decode(const std::string& data)
|
||||
{
|
||||
std::string result;
|
||||
result.resize((data.size() + 2) * 2);
|
||||
|
||||
auto out_len = ul(result.size());
|
||||
if (base64_decode(data.data(), ul(data.size()), cs(result.data()), &out_len) != CRYPT_OK)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result.resize(out_len);
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned int jenkins_one_at_a_time::compute(const std::string& data)
|
||||
{
|
||||
return compute(data.data(), data.size());
|
||||
}
|
||||
|
||||
unsigned int jenkins_one_at_a_time::compute(const char* key, const size_t len)
|
||||
{
|
||||
unsigned int hash, i;
|
||||
for (hash = i = 0; i < len; ++i)
|
||||
{
|
||||
hash += key[i];
|
||||
hash += (hash << 10);
|
||||
hash ^= (hash >> 6);
|
||||
}
|
||||
hash += (hash << 3);
|
||||
hash ^= (hash >> 11);
|
||||
hash += (hash << 15);
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint32_t random::get_integer()
|
||||
{
|
||||
uint32_t result;
|
||||
random::get_data(&result, sizeof(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t random::get_integer(const std::uint32_t min, const std::uint32_t max)
|
||||
{
|
||||
const auto range = max - min + 1;
|
||||
const auto value = random::get_integer();
|
||||
return value % range + min;
|
||||
}
|
||||
|
||||
std::string random::get_challenge()
|
||||
{
|
||||
std::string result;
|
||||
result.resize(sizeof(uint32_t));
|
||||
random::get_data(result.data(), result.size());
|
||||
return string::dump_hex(result, "");
|
||||
}
|
||||
|
||||
void random::get_data(void* data, const size_t size)
|
||||
{
|
||||
prng_.read(data, size);
|
||||
}
|
||||
|
||||
std::string random::get_data(const size_t size)
|
||||
{
|
||||
std::string data;
|
||||
data.resize(size);
|
||||
random::get_data(data.data(), size);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
||||
@@ -0,0 +1,151 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <tomcrypt.h>
|
||||
#include "string.hpp"
|
||||
|
||||
namespace utils::cryptography
|
||||
{
|
||||
namespace ecc
|
||||
{
|
||||
class key final
|
||||
{
|
||||
public:
|
||||
key();
|
||||
~key();
|
||||
|
||||
key(key&& obj) noexcept;
|
||||
key(const key& obj);
|
||||
key& operator=(key&& obj) noexcept;
|
||||
key& operator=(const key& obj);
|
||||
|
||||
bool is_valid() const;
|
||||
|
||||
ecc_key& get();
|
||||
const ecc_key& get() const;
|
||||
|
||||
std::string get_public_key() const;
|
||||
|
||||
void set(const std::string& pub_key_buffer);
|
||||
|
||||
void deserialize(const std::string& key);
|
||||
|
||||
std::string serialize(int type = PK_PRIVATE) const;
|
||||
|
||||
void free();
|
||||
|
||||
bool operator==(key& key) const;
|
||||
|
||||
uint64_t get_hash() const;
|
||||
|
||||
private:
|
||||
ecc_key key_storage_{};
|
||||
};
|
||||
|
||||
key generate_key(int bits);
|
||||
key generate_key(int bits, const std::string& entropy);
|
||||
std::string sign_message(const key& key, const std::string& message);
|
||||
bool verify_message(const key& key, const std::string& message, const std::string& signature);
|
||||
|
||||
bool encrypt(const key& key, std::string& data);
|
||||
bool decrypt(const key& key, std::string& data);
|
||||
}
|
||||
|
||||
namespace rsa
|
||||
{
|
||||
std::string encrypt(const std::string& data, const std::string& hash, const std::string& key);
|
||||
std::string decrypt(const std::string& data, const std::string& hash, const std::string& key);
|
||||
}
|
||||
|
||||
namespace des3
|
||||
{
|
||||
std::string encrypt(const std::string& data, const std::string& iv, const std::string& key);
|
||||
std::string decrypt(const std::string& data, const std::string& iv, const std::string& key);
|
||||
}
|
||||
|
||||
namespace tiger
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex = false);
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex = false);
|
||||
}
|
||||
|
||||
namespace aes
|
||||
{
|
||||
std::string encrypt(const std::string& data, const std::string& iv, const std::string& key);
|
||||
std::string decrypt(const std::string& data, const std::string& iv, const std::string& key);
|
||||
}
|
||||
|
||||
namespace hmac_sha1
|
||||
{
|
||||
std::string compute(const std::string& data, const std::string& key);
|
||||
}
|
||||
|
||||
namespace sha1
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex = false);
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex = false);
|
||||
}
|
||||
|
||||
namespace sha256
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex = false);
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex = false);
|
||||
}
|
||||
|
||||
namespace sha512
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex = false);
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex = false);
|
||||
}
|
||||
|
||||
namespace md5
|
||||
{
|
||||
std::string compute(const std::string& data, bool hex = false);
|
||||
std::string compute(const uint8_t* data, size_t length, bool hex = false);
|
||||
}
|
||||
|
||||
namespace argon2
|
||||
{
|
||||
template <size_t HashLen = 32, size_t SaltLen = 16, std::uint32_t TCost = 2,
|
||||
std::uint32_t MCost = 1 << 6, std::uint32_t Threads = 1>
|
||||
std::string compute(const std::string& data, bool hex = false)
|
||||
{
|
||||
std::uint8_t buffer[HashLen]{};
|
||||
std::uint8_t salt[SaltLen]{};
|
||||
|
||||
argon2i_hash_raw(TCost, MCost, Threads, data.data(), data.size(), salt, SaltLen, buffer, HashLen);
|
||||
|
||||
const auto str = std::string{reinterpret_cast<char*>(buffer), HashLen};
|
||||
if (hex)
|
||||
{
|
||||
return string::dump_hex(str, "", false);
|
||||
}
|
||||
else
|
||||
{
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace base64
|
||||
{
|
||||
std::string encode(const uint8_t* data, size_t len);
|
||||
std::string encode(const std::string& data);
|
||||
std::string decode(const std::string& data);
|
||||
}
|
||||
|
||||
namespace jenkins_one_at_a_time
|
||||
{
|
||||
unsigned int compute(const std::string& data);
|
||||
unsigned int compute(const char* key, size_t len);
|
||||
};
|
||||
|
||||
namespace random
|
||||
{
|
||||
uint32_t get_integer();
|
||||
uint32_t get_integer(const std::uint32_t min, const std::uint32_t max);
|
||||
std::string get_challenge();
|
||||
void get_data(void* data, size_t size);
|
||||
std::string get_data(const size_t size);
|
||||
}
|
||||
}
|
||||
@@ -177,4 +177,17 @@ namespace utils::hook
|
||||
|
||||
delete[] bytes;
|
||||
}
|
||||
|
||||
DWORD unprotect(void* place, const size_t size)
|
||||
{
|
||||
DWORD old_protect{};
|
||||
VirtualProtect(place, size, PAGE_EXECUTE_READWRITE, &old_protect);
|
||||
return old_protect;
|
||||
}
|
||||
|
||||
void protect(void* place, const size_t size, DWORD old_protect)
|
||||
{
|
||||
VirtualProtect(place, size, old_protect, &old_protect);
|
||||
FlushInstructionCache(GetCurrentProcess(), place, size);
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,12 @@ namespace utils::hook
|
||||
return static_cast<T(*)(Args ...)>(this->get_original())(args...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T invoke_pascal(Args... args)
|
||||
{
|
||||
return static_cast<T(__stdcall*)(Args ...)>(this->get_original())(args...);
|
||||
}
|
||||
|
||||
[[nodiscard]] void* get_original() const;
|
||||
|
||||
private:
|
||||
@@ -113,4 +119,7 @@ namespace utils::hook
|
||||
{
|
||||
return static_cast<T(*)(Args ...)>(func)(args...);
|
||||
}
|
||||
|
||||
DWORD unprotect(void* place, const size_t size);
|
||||
void protect(void* place, const size_t size, DWORD old_protect);
|
||||
}
|
||||
+64
-32
@@ -1,47 +1,79 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "http.hpp"
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url)
|
||||
namespace
|
||||
{
|
||||
CComPtr<IStream> stream;
|
||||
|
||||
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
|
||||
size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp)
|
||||
{
|
||||
return {};
|
||||
const auto buffer = static_cast<std::string*>(userp);
|
||||
const auto total_size = size * nmemb;
|
||||
buffer->append(static_cast<char*>(contents), total_size);
|
||||
return total_size;
|
||||
}
|
||||
|
||||
char buffer[0x1000];
|
||||
std::string result;
|
||||
|
||||
HRESULT status{};
|
||||
|
||||
do
|
||||
{
|
||||
DWORD bytes_read = 0;
|
||||
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
|
||||
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
result.append(buffer, bytes_read);
|
||||
}
|
||||
}
|
||||
while (SUCCEEDED(status) && status != S_FALSE);
|
||||
|
||||
if (FAILED(status))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return {result};
|
||||
}
|
||||
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url)
|
||||
std::optional<result> get_data(const std::string& url, const std::string& fields,
|
||||
const headers& headers, const std::string& method)
|
||||
{
|
||||
return std::async(std::launch::async, [url]()
|
||||
curl_slist* header_list = nullptr;
|
||||
const auto curl = curl_easy_init();
|
||||
if (!curl)
|
||||
{
|
||||
return get_data(url);
|
||||
return {};
|
||||
}
|
||||
|
||||
auto _ = gsl::finally([&]()
|
||||
{
|
||||
curl_slist_free_all(header_list);
|
||||
curl_easy_cleanup(curl);
|
||||
});
|
||||
|
||||
for (const auto& header : headers)
|
||||
{
|
||||
auto data = header.first + ": " + header.second;
|
||||
header_list = curl_slist_append(header_list, data.data());
|
||||
}
|
||||
|
||||
std::string buffer{};
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.data());
|
||||
|
||||
if (!fields.empty())
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.data());
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, fields.size());
|
||||
}
|
||||
|
||||
if (!method.empty())
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.data());
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
||||
|
||||
const auto code = curl_easy_perform(curl);
|
||||
|
||||
if (code == CURLE_OK)
|
||||
{
|
||||
result result;
|
||||
result.code = code;
|
||||
result.buffer = std::move(buffer);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result result;
|
||||
result.code = code;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-3
@@ -2,10 +2,19 @@
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <future>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url);
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url);
|
||||
struct result
|
||||
{
|
||||
CURLcode code;
|
||||
std::string buffer;
|
||||
};
|
||||
|
||||
using headers = std::unordered_map<std::string, std::string>;
|
||||
|
||||
std::optional<result> get_data(const std::string& url, const std::string& fields = {},
|
||||
const headers& headers = {}, const std::string& method = {});
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#include <stdinc.hpp>
|
||||
#include <fstream>
|
||||
#include "io.hpp"
|
||||
|
||||
namespace utils::io
|
||||
{
|
||||
bool remove_file(const std::string& file)
|
||||
{
|
||||
return DeleteFileA(file.data()) == TRUE;
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& file)
|
||||
{
|
||||
return std::ifstream(file).good();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
namespace utils::io
|
||||
{
|
||||
bool remove_file(const std::string& file);
|
||||
bool file_exists(const std::string& file);
|
||||
bool write_file(const std::string& file, const std::string& data, bool append = false);
|
||||
bool read_file(const std::string& file, std::string* data);
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "nt.hpp"
|
||||
#include "string.hpp"
|
||||
|
||||
namespace utils::nt
|
||||
{
|
||||
HMODULE library::current_handle_;
|
||||
|
||||
library library::load(const std::string& name)
|
||||
{
|
||||
return library(LoadLibraryA(name.data()));
|
||||
}
|
||||
|
||||
library library::load(const std::filesystem::path& path)
|
||||
{
|
||||
return library::load(path.generic_string());
|
||||
}
|
||||
|
||||
library library::get_by_address(void* address)
|
||||
{
|
||||
HMODULE handle = nullptr;
|
||||
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast<LPCSTR>(address), &handle);
|
||||
return library(handle);
|
||||
}
|
||||
|
||||
void library::set_current_handle(HMODULE handle)
|
||||
{
|
||||
current_handle_ = handle;
|
||||
}
|
||||
|
||||
HMODULE library::get_current_handle()
|
||||
{
|
||||
return current_handle_;
|
||||
}
|
||||
|
||||
library::library()
|
||||
{
|
||||
this->module_ = GetModuleHandleA(nullptr);
|
||||
}
|
||||
|
||||
library::library(const std::string& name)
|
||||
{
|
||||
this->module_ = GetModuleHandleA(name.data());
|
||||
}
|
||||
|
||||
library::library(const HMODULE handle)
|
||||
{
|
||||
this->module_ = handle;
|
||||
}
|
||||
|
||||
bool library::operator==(const library& obj) const
|
||||
{
|
||||
return this->module_ == obj.module_;
|
||||
}
|
||||
|
||||
library::operator bool() const
|
||||
{
|
||||
return this->is_valid();
|
||||
}
|
||||
|
||||
library::operator HMODULE() const
|
||||
{
|
||||
return this->get_handle();
|
||||
}
|
||||
|
||||
PIMAGE_NT_HEADERS library::get_nt_headers() const
|
||||
{
|
||||
if (!this->is_valid()) return nullptr;
|
||||
return reinterpret_cast<PIMAGE_NT_HEADERS>(this->get_ptr() + this->get_dos_header()->e_lfanew);
|
||||
}
|
||||
|
||||
PIMAGE_DOS_HEADER library::get_dos_header() const
|
||||
{
|
||||
return reinterpret_cast<PIMAGE_DOS_HEADER>(this->get_ptr());
|
||||
}
|
||||
|
||||
PIMAGE_OPTIONAL_HEADER library::get_optional_header() const
|
||||
{
|
||||
if (!this->is_valid()) return nullptr;
|
||||
return &this->get_nt_headers()->OptionalHeader;
|
||||
}
|
||||
|
||||
std::vector<PIMAGE_SECTION_HEADER> library::get_section_headers() const
|
||||
{
|
||||
std::vector<PIMAGE_SECTION_HEADER> headers;
|
||||
|
||||
auto nt_headers = this->get_nt_headers();
|
||||
auto section = IMAGE_FIRST_SECTION(nt_headers);
|
||||
|
||||
for (uint16_t i = 0; i < nt_headers->FileHeader.NumberOfSections; ++i, ++section)
|
||||
{
|
||||
if (section) headers.push_back(section);
|
||||
else OutputDebugStringA("There was an invalid section :O");
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
std::uint8_t* library::get_ptr() const
|
||||
{
|
||||
return reinterpret_cast<std::uint8_t*>(this->module_);
|
||||
}
|
||||
|
||||
void library::unprotect() const
|
||||
{
|
||||
if (!this->is_valid()) return;
|
||||
|
||||
DWORD protection;
|
||||
VirtualProtect(this->get_ptr(), this->get_optional_header()->SizeOfImage, PAGE_EXECUTE_READWRITE,
|
||||
&protection);
|
||||
}
|
||||
|
||||
size_t library::get_relative_entry_point() const
|
||||
{
|
||||
if (!this->is_valid()) return 0;
|
||||
return this->get_nt_headers()->OptionalHeader.AddressOfEntryPoint;
|
||||
}
|
||||
|
||||
void* library::get_entry_point() const
|
||||
{
|
||||
if (!this->is_valid()) return nullptr;
|
||||
return this->get_ptr() + this->get_relative_entry_point();
|
||||
}
|
||||
|
||||
bool library::is_valid() const
|
||||
{
|
||||
return this->module_ != nullptr && this->get_dos_header()->e_magic == IMAGE_DOS_SIGNATURE;
|
||||
}
|
||||
|
||||
std::string library::get_name() const
|
||||
{
|
||||
if (!this->is_valid()) return "";
|
||||
|
||||
auto path = this->get_path();
|
||||
const auto pos = path.find_last_of("/\\");
|
||||
if (pos == std::string::npos) return path;
|
||||
|
||||
return path.substr(pos + 1);
|
||||
}
|
||||
|
||||
std::string library::get_path() const
|
||||
{
|
||||
if (!this->is_valid()) return "";
|
||||
|
||||
char name[MAX_PATH] = {0};
|
||||
GetModuleFileNameA(this->module_, name, sizeof name);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string library::get_folder() const
|
||||
{
|
||||
if (!this->is_valid()) return "";
|
||||
|
||||
const auto path = std::filesystem::path(this->get_path());
|
||||
return path.parent_path().generic_string();
|
||||
}
|
||||
|
||||
void library::free()
|
||||
{
|
||||
if (this->is_valid())
|
||||
{
|
||||
FreeLibrary(this->module_);
|
||||
this->module_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
HMODULE library::get_handle() const
|
||||
{
|
||||
return this->module_;
|
||||
}
|
||||
|
||||
void** library::get_iat_entry(const std::string& module_name, const std::string& proc_name) const
|
||||
{
|
||||
if (!this->is_valid()) return nullptr;
|
||||
|
||||
const library other_module(module_name);
|
||||
if (!other_module.is_valid()) return nullptr;
|
||||
|
||||
auto* const target_function = other_module.get_proc<void*>(proc_name);
|
||||
if (!target_function) return nullptr;
|
||||
|
||||
auto* header = this->get_optional_header();
|
||||
if (!header) return nullptr;
|
||||
|
||||
auto* import_descriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(this->get_ptr() + header->DataDirectory
|
||||
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
|
||||
|
||||
while (import_descriptor->Name)
|
||||
{
|
||||
if (!_stricmp(reinterpret_cast<char*>(this->get_ptr() + import_descriptor->Name), module_name.data()))
|
||||
{
|
||||
auto* original_thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(import_descriptor->
|
||||
OriginalFirstThunk + this->get_ptr());
|
||||
auto* thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(import_descriptor->FirstThunk + this->
|
||||
get_ptr());
|
||||
|
||||
while (original_thunk_data->u1.AddressOfData)
|
||||
{
|
||||
const size_t ordinal_number = original_thunk_data->u1.AddressOfData & 0xFFFFFFF;
|
||||
|
||||
if (ordinal_number > 0xFFFF) continue;
|
||||
|
||||
if (GetProcAddress(other_module.module_, reinterpret_cast<char*>(ordinal_number)) ==
|
||||
target_function)
|
||||
{
|
||||
return reinterpret_cast<void**>(&thunk_data->u1.Function);
|
||||
}
|
||||
|
||||
++original_thunk_data;
|
||||
++thunk_data;
|
||||
}
|
||||
|
||||
//break;
|
||||
}
|
||||
|
||||
++import_descriptor;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void raise_hard_exception()
|
||||
{
|
||||
int data = false;
|
||||
const library ntdll("ntdll.dll");
|
||||
ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data);
|
||||
ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr, 6, &data);
|
||||
}
|
||||
|
||||
std::string load_resource(const int id)
|
||||
{
|
||||
const auto self_handle = library::get_current_handle();
|
||||
const auto res = FindResource(self_handle, MAKEINTRESOURCE(id), RT_RCDATA);
|
||||
if (res == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto handle = LoadResource(self_handle, res);
|
||||
if (handle == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto str = LPSTR(LockResource(handle));
|
||||
const auto size = SizeofResource(self_handle, res);
|
||||
return std::string{str, size};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
|
||||
// min and max is required by gdi, therefore NOMINMAX won't work
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
|
||||
namespace utils::nt
|
||||
{
|
||||
class library final
|
||||
{
|
||||
public:
|
||||
static library load(const std::string& name);
|
||||
static library load(const std::filesystem::path& path);
|
||||
static library get_by_address(void* address);
|
||||
|
||||
static void set_current_handle(HMODULE handle);
|
||||
static HMODULE get_current_handle();
|
||||
|
||||
library();
|
||||
explicit library(const std::string& name);
|
||||
explicit library(HMODULE handle);
|
||||
|
||||
library(const library& a) : module_(a.module_)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator!=(const library& obj) const { return !(*this == obj); };
|
||||
bool operator==(const library& obj) const;
|
||||
|
||||
operator bool() const;
|
||||
operator HMODULE() const;
|
||||
|
||||
void unprotect() const;
|
||||
void* get_entry_point() const;
|
||||
size_t get_relative_entry_point() const;
|
||||
|
||||
bool is_valid() const;
|
||||
std::string get_name() const;
|
||||
std::string get_path() const;
|
||||
std::string get_folder() const;
|
||||
std::uint8_t* get_ptr() const;
|
||||
void free();
|
||||
|
||||
HMODULE get_handle() const;
|
||||
|
||||
template <typename T>
|
||||
T get_proc(const std::string& process) const
|
||||
{
|
||||
if (!this->is_valid()) T{};
|
||||
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::function<T> get(const std::string& process) const
|
||||
{
|
||||
if (!this->is_valid()) return std::function<T>();
|
||||
return static_cast<T*>(this->get_proc<void*>(process));
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T invoke(const std::string& process, Args ... args) const
|
||||
{
|
||||
auto method = this->get<T(__cdecl)(Args ...)>(process);
|
||||
if (method) return method(args...);
|
||||
return T();
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T invoke_pascal(const std::string& process, Args ... args) const
|
||||
{
|
||||
auto method = this->get<T(__stdcall)(Args ...)>(process);
|
||||
if (method) return method(args...);
|
||||
return T();
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T invoke_this(const std::string& process, void* this_ptr, Args ... args) const
|
||||
{
|
||||
auto method = this->get<T(__thiscall)(void*, Args ...)>(this_ptr, process);
|
||||
if (method) return method(args...);
|
||||
return T();
|
||||
}
|
||||
|
||||
std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const;
|
||||
|
||||
PIMAGE_NT_HEADERS get_nt_headers() const;
|
||||
PIMAGE_DOS_HEADER get_dos_header() const;
|
||||
PIMAGE_OPTIONAL_HEADER get_optional_header() const;
|
||||
|
||||
void** get_iat_entry(const std::string& module_name, const std::string& proc_name) const;
|
||||
|
||||
private:
|
||||
HMODULE module_;
|
||||
static HMODULE current_handle_;
|
||||
|
||||
};
|
||||
|
||||
__declspec(noreturn) void raise_hard_exception();
|
||||
std::string load_resource(int id);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <stdinc.hpp>
|
||||
|
||||
#include "thread_pool.hpp"
|
||||
|
||||
namespace utils
|
||||
{
|
||||
thread_pool::worker::worker()
|
||||
{
|
||||
}
|
||||
|
||||
void thread_pool::worker::start(thread_pool& pool)
|
||||
{
|
||||
this->thread_ = std::thread([&]
|
||||
{
|
||||
this->loop(pool);
|
||||
});
|
||||
}
|
||||
|
||||
void thread_pool::worker::stop()
|
||||
{
|
||||
if (this->thread_.joinable())
|
||||
{
|
||||
this->thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::worker::loop(thread_pool& pool)
|
||||
{
|
||||
while (!pool.stopped_)
|
||||
{
|
||||
pool.run_job();
|
||||
}
|
||||
}
|
||||
|
||||
thread_pool::job_ptr thread_pool::pop_job()
|
||||
{
|
||||
thread_pool::job_ptr job = std::move(this->queue_.front());
|
||||
this->queue_.pop_front();
|
||||
return job;
|
||||
}
|
||||
|
||||
void thread_pool::run_job()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(this->mutex_);
|
||||
|
||||
this->event_.wait(lock, [&]()
|
||||
{
|
||||
return !this->queue_.empty() || this->stopped_;
|
||||
});
|
||||
|
||||
if (this->stopped_ || this->queue_.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto job = this->pop_job();
|
||||
lock.unlock();
|
||||
job->operator()();
|
||||
}
|
||||
|
||||
thread_pool::thread_pool(const std::size_t num_workers)
|
||||
{
|
||||
this->initialize(num_workers);
|
||||
}
|
||||
|
||||
void thread_pool::initialize(const std::size_t num_workers)
|
||||
{
|
||||
for (auto i = 0u; i < num_workers; i++)
|
||||
{
|
||||
this->workers_.emplace_back(std::make_unique<thread_pool::worker>());
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::start()
|
||||
{
|
||||
for (auto& worker : this->workers_)
|
||||
{
|
||||
worker->start(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void thread_pool::stop()
|
||||
{
|
||||
this->stopped_ = true;
|
||||
this->event_.notify_all();
|
||||
for (auto& worker : this->workers_)
|
||||
{
|
||||
worker->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
namespace utils
|
||||
{
|
||||
class thread_pool
|
||||
{
|
||||
public:
|
||||
using job = std::function<void()>;
|
||||
using job_ptr = std::unique_ptr<job>;
|
||||
|
||||
friend class worker;
|
||||
class worker
|
||||
{
|
||||
public:
|
||||
worker();
|
||||
|
||||
friend class thread_pool;
|
||||
|
||||
void start(thread_pool& pool);
|
||||
void stop();
|
||||
void loop(thread_pool& pool);
|
||||
|
||||
private:
|
||||
std::thread thread_;
|
||||
|
||||
};
|
||||
|
||||
thread_pool() = default;
|
||||
thread_pool(const std::size_t num_workers);
|
||||
void initialize(const std::size_t num_workers);
|
||||
|
||||
void start();
|
||||
void update();
|
||||
void stop();
|
||||
|
||||
template <typename F>
|
||||
void push(F&& job)
|
||||
{
|
||||
if (this->stopped_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard lock(this->mutex_);
|
||||
this->queue_.emplace_back(std::make_unique<thread_pool::job>(std::forward<F>(job)));
|
||||
this->event_.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
thread_pool::job_ptr pop_job();
|
||||
void run_job();
|
||||
|
||||
std::mutex mutex_;
|
||||
std::atomic_bool stopped_;
|
||||
std::deque<job_ptr> queue_;
|
||||
std::condition_variable event_;
|
||||
std::deque<std::unique_ptr<worker>> workers_;
|
||||
|
||||
};
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user