From 018d191d12cc59f4fef5301683b3a36203cc2739 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 13:33:15 +0500 Subject: [PATCH 01/29] initial --- .editorconfig | 9 +++ .github/workflows/build.yml | 116 +++++++++++++++++++++++++++++++++++ .github/workflows/test.lua | 21 +++++++ .gitignore | 3 + Examples.sln | 34 ++++++++++ NativeMath/NativeMath.cs | 29 +++++++++ NativeMath/NativeMath.csproj | 10 +++ NativeMath/nuget.config | 8 +++ 8 files changed, 230 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/test.lua create mode 100644 .gitignore create mode 100644 Examples.sln create mode 100644 NativeMath/NativeMath.cs create mode 100644 NativeMath/NativeMath.csproj create mode 100644 NativeMath/nuget.config diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9ab54b0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +[*] +end_of_line = crlf +charset = utf-8 +indent_style = tab +indent_size = tab +trim_trailing_whitespace = true +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..8af4ed0 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,116 @@ +name: Build & Test +on: + push: + branches: [ \* ] + pull_request: + branches: [ main ] +jobs: + linux: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.100 + - name: Install dependencies + run: dotnet restore Examples.sln + - name: Install Steam and Garry's Mod Dedicated Server + run: | + wget https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz + tar -xvzf steamcmd_linux.tar.gz + rm -rfv steamcmd_linux.tar.gz + ./steamcmd.sh +login anonymous +force_install_dir gmod "+app_update 4020 -beta x86-64 validate" +quit + - name: Install GmodDotNet + run: | + wget https://gleb-krasilich.fra1.digitaloceanspaces.com/GmodNETStorage/storage/gmod-dot-net-linux.0.7.0-beta.2.30293992.master.tar.gz -O gmoddotnet.tar.gz + mkdir ./gmod/garrysmod/lua/bin + tar -xvzf gmoddotnet.tar.gz -C ./gmod/garrysmod/lua/bin + rm -rfv gmoddotnet.tar.gz + - name: Build Module + run: dotnet publish NativeMath/NativeMath.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/NativeMath + - name: Upload a Build Artifact + uses: actions/upload-artifact@v2 + with: + name: NativeMath linux-x64 + path: ./gmod/garrysmod/lua/bin/Modules/NativeMath/* + retention-days: 10 + - name: Copy test.lua + run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun + - name: Run Garry's Mod + run: ./srcds_run_x64 -game garrysmod -systemtest -condebug +sv_hibernate_think 1 || true + working-directory: ./gmod/ + timeout-minutes: 1 + continue-on-error: true + - name: Print log + run: cat gmod/garrysmod/console.log + - name: Tests successfull? + uses: andstor/file-existence-action@v1 + with: + files: "gmod/garrysmod/data/success.txt" + allow_failure: true + windows-build: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.100 + - name: Install dependencies + run: dotnet restore Examples.sln + - name: Install Steam and Garry's Mod Dedicated Server + shell: bash + run: | + curl https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip -O -L + powershell -Command 'Expand-Archive -LiteralPath ./steamcmd.zip -DestinationPath ./' + ./steamcmd.exe +login anonymous +force_install_dir gmod "+app_update 4020 -beta x86-64 validate" +quit || true + - name: Install GmodDotNet + shell: bash + run: | + curl -o gmoddotnet.zip https://gleb-krasilich.fra1.digitaloceanspaces.com/GmodNETStorage/storage/gmod-dot-net-windows.0.7.0-beta.2.30293992.master.zip -O -L + mkdir ./gmod/garrysmod/lua/bin + powershell -Command 'Expand-Archive -LiteralPath ./gmoddotnet.zip -DestinationPath ./gmod/garrysmod/lua/bin' + - name: Build Module + run: dotnet publish NativeMath/NativeMath.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/NativeMath + - name: Upload a Build Artifact + uses: actions/upload-artifact@v2 + with: + name: NativeMath win-x64 + path: ./gmod/garrysmod/lua/bin/Modules/NativeMath/* + retention-days: 10 + - name: Copy test.lua + run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun + - name: Run Garry's Mod + shell: bash + run: | + powershell -Command './gmod/srcds_win64.exe -console -systemtest -condebug -game "garrysmod" +exec "server.cfg" +gamemode sandbox +map gm_construct +maxplayers 16 +sv_hibernate_think 1' + powershell -Command 'Wait-Process -Name srcds_win64' + continue-on-error: true + - name: Print log + shell: bash + run: cat gmod/garrysmod/console.log + - name: Tests successfull? + id: check_files + uses: andstor/file-existence-action@v1 + with: + files: "gmod/garrysmod/data/success.txt" + allow_failure: true + macos-build: + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.100 + - name: Install dependencies + run: dotnet restore Examples.sln + - name: Build Module + run: dotnet publish Examples.sln --configuration Release --framework net5 + - name: Upload a Build Artifact + uses: actions/upload-artifact@v2 + with: + name: NativeMath osx-x64 + path: NativeMath/bin/Release/net5/osx-x64/publish/* + retention-days: 10 diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua new file mode 100644 index 0000000..e0684de --- /dev/null +++ b/.github/workflows/test.lua @@ -0,0 +1,21 @@ +hook.Add("Tick", "Template_CloseServer", engine.CloseServer) +require("dotnet") + +local function run_test() + local module_loaded = dotnet.load("NativeMath") + assert(module_loaded) + + ----------------------- + + + + ----------------------- + + local module_unloaded = dotnet.unload("NativeMath") + assert(module_unloaded) +end + +run_test() + +print("tests are successful!") +file.Write("success.txt", "done") diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac16142 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +bin +obj diff --git a/Examples.sln b/Examples.sln new file mode 100644 index 0000000..170ac2b --- /dev/null +++ b/Examples.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30803.129 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeMath", "NativeMath\NativeMath.csproj", "{5849F24D-B5D7-4A48-9469-021DEE4B8531}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{342AA3D8-AFF4-48DE-B97C-DE6E0CF35634}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + .gitignore = .gitignore + .github\workflows\build.yml = .github\workflows\build.yml + README.md = README.md + .github\workflows\test.lua = .github\workflows\test.lua + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5849F24D-B5D7-4A48-9469-021DEE4B8531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5849F24D-B5D7-4A48-9469-021DEE4B8531}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5849F24D-B5D7-4A48-9469-021DEE4B8531}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5849F24D-B5D7-4A48-9469-021DEE4B8531}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {27D856A3-D44D-42EB-AB15-26DF09A9E065} + EndGlobalSection +EndGlobal diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs new file mode 100644 index 0000000..815779f --- /dev/null +++ b/NativeMath/NativeMath.cs @@ -0,0 +1,29 @@ +using GmodNET.API; +using System; + +namespace NativeMath +{ + public class NativeMath : IModule + { + public string ModuleName => "NativeMath"; + + public string ModuleVersion => "0.0.1"; + + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) + { + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.PushManagedFunction((lua) => + { + Console.WriteLine("native mafs"); + return 0; + }); + lua.SetField(-2, "NativeMath"); + lua.Pop(); + } + + public void Unload(ILua lua) + { + + } + } +} diff --git a/NativeMath/NativeMath.csproj b/NativeMath/NativeMath.csproj new file mode 100644 index 0000000..a236f91 --- /dev/null +++ b/NativeMath/NativeMath.csproj @@ -0,0 +1,10 @@ + + + Library + net5.0 + + + + + + diff --git a/NativeMath/nuget.config b/NativeMath/nuget.config new file mode 100644 index 0000000..d526a78 --- /dev/null +++ b/NativeMath/nuget.config @@ -0,0 +1,8 @@ + + + + + + + + From 9cdc7cfb9026f540011705dcf7840079aa5604c9 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 13:35:35 +0500 Subject: [PATCH 02/29] NativeMath actions --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8af4ed0..a4f9920 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ name: Build & Test on: push: - branches: [ \* ] + branches: [ main, NativeMath ] pull_request: branches: [ main ] jobs: From 2e9f0dda26035aa84c1b13df4b5c054ecdac5093 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 13:38:12 +0500 Subject: [PATCH 03/29] net5 --- NativeMath/NativeMath.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NativeMath/NativeMath.csproj b/NativeMath/NativeMath.csproj index a236f91..099cb17 100644 --- a/NativeMath/NativeMath.csproj +++ b/NativeMath/NativeMath.csproj @@ -1,7 +1,7 @@ Library - net5.0 + net5 From baee9052cfccde393cdd2863a8bb585e73dcb983 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 13:55:22 +0500 Subject: [PATCH 04/29] set NativeMath to nil --- NativeMath/NativeMath.cs | 45 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index 815779f..06e3c94 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -1,29 +1,32 @@ -using GmodNET.API; +using GmodNET.API; using System; namespace NativeMath { - public class NativeMath : IModule - { - public string ModuleName => "NativeMath"; + public class NativeMath : IModule + { + public string ModuleName => "NativeMath"; - public string ModuleVersion => "0.0.1"; + public string ModuleVersion => "0.0.1"; - public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) - { - lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.PushManagedFunction((lua) => - { - Console.WriteLine("native mafs"); - return 0; - }); - lua.SetField(-2, "NativeMath"); - lua.Pop(); - } + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) + { + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.PushManagedFunction((lua) => + { + Console.WriteLine("native mafs"); + return 0; + }); + lua.SetField(-2, "NativeMath"); + lua.Pop(); + } - public void Unload(ILua lua) - { - - } - } + public void Unload(ILua lua) + { + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.PushNil(); + lua.SetField(-2, "NativeMath"); + lua.Pop(); + } + } } From 5560ec6cb74b1ff8354a97fd26ffcfad2242a108 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 14:09:54 +0500 Subject: [PATCH 05/29] free NativeMath using GCHandle --- NativeMath/NativeMath.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index 06e3c94..ba340b7 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -1,5 +1,6 @@ using GmodNET.API; using System; +using System.Runtime.InteropServices; namespace NativeMath { @@ -9,10 +10,12 @@ public class NativeMath : IModule public string ModuleVersion => "0.0.1"; + private GCHandle NativeMathHandle; + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.PushManagedFunction((lua) => + NativeMathHandle = lua.PushManagedFunction((lua) => { Console.WriteLine("native mafs"); return 0; @@ -23,10 +26,7 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl public void Unload(ILua lua) { - lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.PushNil(); - lua.SetField(-2, "NativeMath"); - lua.Pop(); + NativeMathHandle.Free(); } } } From 5760496644dea4a365a0a3baeea9416c531e6065 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 14:30:40 +0500 Subject: [PATCH 06/29] lua tests --- .github/workflows/test.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index e0684de..f0bd771 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -1,13 +1,44 @@ hook.Add("Tick", "Template_CloseServer", engine.CloseServer) require("dotnet") +local function benchmark_single(name, fn) + local startTime = SysTime() + fn() + local elapsed = SysTime()-startTime + print(name.."_single"..elapsed) + return elapsed +end + +local function benchmark_thousand(name, fn) + local startTime = SysTime() + for i=0,1000 do + fn() + end + local elapsed = SysTime()-startTime + print(name.."_thousand"..elapsed) + return elapsed +end + +local function benchmark(name, fn) + return { + single = benchmark_single(name, fn), + thousand = benchmark_thousand(name, fn) + } +end + local function run_test() local module_loaded = dotnet.load("NativeMath") assert(module_loaded) ----------------------- + print(jit.status()) + + local lua_math_Approach = benchmark("math_Approach", function() + return math.Approach(0,100,20) + end) + PrintTable(lua_math_Approach) ----------------------- From 3fd9906a8917e3b6df1c5925ebc9bf45355d89d1 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 14:46:43 +0500 Subject: [PATCH 07/29] math.Approach --- .github/workflows/test.lua | 13 +++++++++++-- NativeMath/NativeMath.cs | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index f0bd771..2ed4cec 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -32,14 +32,23 @@ local function run_test() ----------------------- - print(jit.status()) - local lua_math_Approach = benchmark("math_Approach", function() return math.Approach(0,100,20) end) PrintTable(lua_math_Approach) + NativeMath() + + print(type(math.Approach)) + + local native_math_Approach = benchmark("math_Approach", function() + return math.Approach(0,100,20) + end) + + PrintTable(native_math_Approach) + + ----------------------- local module_unloaded = dotnet.unload("NativeMath") diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index ba340b7..d151daf 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -18,6 +18,30 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl NativeMathHandle = lua.PushManagedFunction((lua) => { Console.WriteLine("native mafs"); + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.GetField(-1, "math"); + lua.PushManagedFunction((lua) => + { + double current = Math.Abs(lua.GetNumber(1)); + double target = lua.GetNumber(2); + double increment = lua.GetNumber(3); + + if (current < target) + { + lua.PushNumber(Math.Min(current + increment, target)); + } + else if (current > target) + { + lua.PushNumber(Math.Max(current - increment, target)); + } + else + { + lua.PushNumber(target); + } + return 1; + }); + lua.SetField(-2, "Approach"); + lua.Pop(); return 0; }); lua.SetField(-2, "NativeMath"); From 4068c9d0ee75d8aeee615dcf65d9eceeaff37fec Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 14:53:44 +0500 Subject: [PATCH 08/29] donothingwithit --- .github/workflows/test.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 2ed4cec..9a886b0 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -1,6 +1,8 @@ hook.Add("Tick", "Template_CloseServer", engine.CloseServer) require("dotnet") +function donothingwithit(something) end + local function benchmark_single(name, fn) local startTime = SysTime() fn() @@ -33,7 +35,7 @@ local function run_test() ----------------------- local lua_math_Approach = benchmark("math_Approach", function() - return math.Approach(0,100,20) + donothingwithit(math.Approach(0,100,20)) end) PrintTable(lua_math_Approach) @@ -43,7 +45,7 @@ local function run_test() print(type(math.Approach)) local native_math_Approach = benchmark("math_Approach", function() - return math.Approach(0,100,20) + donothingwithit(math.Approach(0,100,20)) end) PrintTable(native_math_Approach) From 414eab0936f621d24c289685b009118c6a493b15 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 14:57:12 +0500 Subject: [PATCH 09/29] handles list --- NativeMath/NativeMath.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index d151daf..9ba855f 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -1,5 +1,6 @@ using GmodNET.API; using System; +using System.Collections.Generic; using System.Runtime.InteropServices; namespace NativeMath @@ -10,17 +11,20 @@ public class NativeMath : IModule public string ModuleVersion => "0.0.1"; - private GCHandle NativeMathHandle; + private List handles; public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - NativeMathHandle = lua.PushManagedFunction((lua) => + + handles = new(); + + handles.Add(lua.PushManagedFunction((lua) => { Console.WriteLine("native mafs"); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "math"); - lua.PushManagedFunction((lua) => + handles.Add(lua.PushManagedFunction((lua) => { double current = Math.Abs(lua.GetNumber(1)); double target = lua.GetNumber(2); @@ -39,18 +43,22 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl lua.PushNumber(target); } return 1; - }); + })); lua.SetField(-2, "Approach"); lua.Pop(); return 0; - }); + })); lua.SetField(-2, "NativeMath"); lua.Pop(); } public void Unload(ILua lua) { - NativeMathHandle.Free(); + foreach (var handle in handles) + { + handle.Free(); + } + handles = null; } } } From def37eda094a6ffa0eb4a3e5f0fdaa394d0fb1bd Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 15:01:41 +0500 Subject: [PATCH 10/29] current --- .github/workflows/test.lua | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 9a886b0..53ed0a4 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -3,11 +3,11 @@ require("dotnet") function donothingwithit(something) end -local function benchmark_single(name, fn) +local function benchmark(name, fn) local startTime = SysTime() fn() local elapsed = SysTime()-startTime - print(name.."_single"..elapsed) + print(name.." "..elapsed) return elapsed end @@ -21,35 +21,27 @@ local function benchmark_thousand(name, fn) return elapsed end -local function benchmark(name, fn) - return { - single = benchmark_single(name, fn), - thousand = benchmark_thousand(name, fn) - } -end - local function run_test() local module_loaded = dotnet.load("NativeMath") assert(module_loaded) ----------------------- - local lua_math_Approach = benchmark("math_Approach", function() - donothingwithit(math.Approach(0,100,20)) - end) + local current = 0 - PrintTable(lua_math_Approach) + benchmark("math_Approach", function() + current = math.Approach(0,100,0.1) + donothingwithit(current) + end) NativeMath() - print(type(math.Approach)) - - local native_math_Approach = benchmark("math_Approach", function() - donothingwithit(math.Approach(0,100,20)) + current = 0 + benchmark("math_Approach", function() + current = math.Approach(current,100,0.1) + donothingwithit(current) end) - PrintTable(native_math_Approach) - ----------------------- From b0d3d9454641ca0182244b4b10074346cb259572 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 15:06:35 +0500 Subject: [PATCH 11/29] thousand --- .github/workflows/test.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 53ed0a4..7cc9450 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -27,19 +27,23 @@ local function run_test() ----------------------- - local current = 0 benchmark("math_Approach", function() - current = math.Approach(0,100,0.1) - donothingwithit(current) + local current = 0 + for i=0,1000 do + current = math.Approach(0,100,0.1) + end + print(current) end) NativeMath() - current = 0 benchmark("math_Approach", function() - current = math.Approach(current,100,0.1) - donothingwithit(current) + local current = 0 + for i=0,1000 do + current = math.Approach(0,100,0.1) + end + print(current) end) From 7a02eb01564738b38be4c5e7640796be2b04c536 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 15:09:52 +0500 Subject: [PATCH 12/29] current --- .github/workflows/test.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 7cc9450..ef2353a 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -31,7 +31,7 @@ local function run_test() benchmark("math_Approach", function() local current = 0 for i=0,1000 do - current = math.Approach(0,100,0.1) + current = math.Approach(current,100,0.1) end print(current) end) @@ -41,7 +41,7 @@ local function run_test() benchmark("math_Approach", function() local current = 0 for i=0,1000 do - current = math.Approach(0,100,0.1) + current = math.Approach(current,100,0.1) end print(current) end) From a388698b9254702738cd64b8ebfb6ea1985762bf Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 15:29:00 +0500 Subject: [PATCH 13/29] Distance --- .github/workflows/test.lua | 36 ++++++++++++++++++++++-------------- NativeMath/NativeMath.cs | 20 +++++++++++++++++++- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index ef2353a..ce459be 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -21,31 +21,39 @@ local function benchmark_thousand(name, fn) return elapsed end -local function run_test() - local module_loaded = dotnet.load("NativeMath") - assert(module_loaded) - - ----------------------- - - +local function math_Approach() benchmark("math_Approach", function() local current = 0 - for i=0,1000 do - current = math.Approach(current,100,0.1) + for i=0,10000 do + current = math.Approach(current,100,0.01) end print(current) end) +end - NativeMath() - - benchmark("math_Approach", function() +local function math_Distance() + benchmark("math_Distance", function() local current = 0 - for i=0,1000 do - current = math.Approach(current,100,0.1) + for i=0,10000 do + current = math.Distance(current,0,200,100) end print(current) end) +end + +local function run_test() + local module_loaded = dotnet.load("NativeMath") + assert(module_loaded) + + ----------------------- + + math_Approach() + math_Distance() + + NativeMath() + math_Approach() + math_Distance() ----------------------- diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index 9ba855f..21441f5 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -21,7 +21,6 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl handles.Add(lua.PushManagedFunction((lua) => { - Console.WriteLine("native mafs"); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "math"); handles.Add(lua.PushManagedFunction((lua) => @@ -46,6 +45,25 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl })); lua.SetField(-2, "Approach"); lua.Pop(); + + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.GetField(-1, "math"); + handles.Add(lua.PushManagedFunction((lua) => + { + double x1 = lua.GetNumber(1); + double y1 = lua.GetNumber(2); + double x2 = lua.GetNumber(3); + double y2 = lua.GetNumber(4); + + double xd = x2 - x1; + double yd = y2 - y1; + + lua.PushNumber(Math.Sqrt(xd * xd + yd * yd)); + + return 1; + })); + lua.SetField(-2, "Distance"); + lua.Pop(); return 0; })); lua.SetField(-2, "NativeMath"); From 2b60337645e7e55295935b584c790b89c4345596 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 17:31:34 +0500 Subject: [PATCH 14/29] disable jit --- .github/workflows/test.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index ce459be..53bbfbe 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -1,6 +1,8 @@ hook.Add("Tick", "Template_CloseServer", engine.CloseServer) require("dotnet") +jit.off() + function donothingwithit(something) end local function benchmark(name, fn) From 6fc8605415a933d82f1a70b007e120ba3f47a94a Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Fri, 1 Jan 2021 18:21:09 +0500 Subject: [PATCH 15/29] enable JIT use static function pointer allow unsafe blocks --- .github/workflows/test.lua | 2 - NativeMath/NativeMath.cs | 80 +++++++++++++++++++++--------------- NativeMath/NativeMath.csproj | 6 +++ 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 53bbfbe..ce459be 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -1,8 +1,6 @@ hook.Add("Tick", "Template_CloseServer", engine.CloseServer) require("dotnet") -jit.off() - function donothingwithit(something) end local function benchmark(name, fn) diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs index 21441f5..eca742c 100644 --- a/NativeMath/NativeMath.cs +++ b/NativeMath/NativeMath.cs @@ -1,6 +1,8 @@ using GmodNET.API; using System; using System.Collections.Generic; +using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace NativeMath @@ -13,6 +15,46 @@ public class NativeMath : IModule private List handles; + [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] + private static int Approach(IntPtr luaState) + { + ILua lua = GmodInterop.GetLuaFromState(luaState); + double current = Math.Abs(lua.GetNumber(1)); + double target = lua.GetNumber(2); + double increment = lua.GetNumber(3); + + if (current < target) + { + lua.PushNumber(Math.Min(current + increment, target)); + } + else if (current > target) + { + lua.PushNumber(Math.Max(current - increment, target)); + } + else + { + lua.PushNumber(target); + } + return 1; + } + + [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] + private static int Distance(IntPtr luaState) + { + ILua lua = GmodInterop.GetLuaFromState(luaState); + double x1 = lua.GetNumber(1); + double y1 = lua.GetNumber(2); + double x2 = lua.GetNumber(3); + double y2 = lua.GetNumber(4); + + double xd = x2 - x1; + double yd = y2 - y1; + + lua.PushNumber(Math.Sqrt(xd * xd + yd * yd)); + + return 1; + } + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); @@ -23,45 +65,19 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "math"); - handles.Add(lua.PushManagedFunction((lua) => + unsafe { - double current = Math.Abs(lua.GetNumber(1)); - double target = lua.GetNumber(2); - double increment = lua.GetNumber(3); - - if (current < target) - { - lua.PushNumber(Math.Min(current + increment, target)); - } - else if (current > target) - { - lua.PushNumber(Math.Max(current - increment, target)); - } - else - { - lua.PushNumber(target); - } - return 1; - })); + lua.PushCFunction(&Approach); + } lua.SetField(-2, "Approach"); lua.Pop(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "math"); - handles.Add(lua.PushManagedFunction((lua) => + unsafe { - double x1 = lua.GetNumber(1); - double y1 = lua.GetNumber(2); - double x2 = lua.GetNumber(3); - double y2 = lua.GetNumber(4); - - double xd = x2 - x1; - double yd = y2 - y1; - - lua.PushNumber(Math.Sqrt(xd * xd + yd * yd)); - - return 1; - })); + lua.PushCFunction(&Distance); + } lua.SetField(-2, "Distance"); lua.Pop(); return 0; diff --git a/NativeMath/NativeMath.csproj b/NativeMath/NativeMath.csproj index 099cb17..c339c8c 100644 --- a/NativeMath/NativeMath.csproj +++ b/NativeMath/NativeMath.csproj @@ -3,6 +3,12 @@ Library net5 + + true + + + true + From 29cac0120d97a7a897f3809e2c26086e67d8b923 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:11:26 +0500 Subject: [PATCH 16/29] HashCalc --- .github/workflows/build.yml | 18 ++-- .github/workflows/test.lua | 54 +--------- Examples.sln | 2 +- HashCalc/HashCalc.cs | 56 +++++++++++ .../HashCalc.csproj | 0 {NativeMath => HashCalc}/nuget.config | 0 NativeMath/NativeMath.cs | 98 ------------------- 7 files changed, 71 insertions(+), 157 deletions(-) create mode 100644 HashCalc/HashCalc.cs rename NativeMath/NativeMath.csproj => HashCalc/HashCalc.csproj (100%) rename {NativeMath => HashCalc}/nuget.config (100%) delete mode 100644 NativeMath/NativeMath.cs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a4f9920..e121ef8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ name: Build & Test on: push: - branches: [ main, NativeMath ] + branches: [ main, HashCalc ] pull_request: branches: [ main ] jobs: @@ -28,12 +28,12 @@ jobs: tar -xvzf gmoddotnet.tar.gz -C ./gmod/garrysmod/lua/bin rm -rfv gmoddotnet.tar.gz - name: Build Module - run: dotnet publish NativeMath/NativeMath.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/NativeMath + run: dotnet publish HashCalc/HashCalc.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/HashCalc - name: Upload a Build Artifact uses: actions/upload-artifact@v2 with: - name: NativeMath linux-x64 - path: ./gmod/garrysmod/lua/bin/Modules/NativeMath/* + name: HashCalc linux-x64 + path: ./gmod/garrysmod/lua/bin/Modules/HashCalc/* retention-days: 10 - name: Copy test.lua run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun @@ -72,12 +72,12 @@ jobs: mkdir ./gmod/garrysmod/lua/bin powershell -Command 'Expand-Archive -LiteralPath ./gmoddotnet.zip -DestinationPath ./gmod/garrysmod/lua/bin' - name: Build Module - run: dotnet publish NativeMath/NativeMath.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/NativeMath + run: dotnet publish HashCalc/HashCalc.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/HashCalc - name: Upload a Build Artifact uses: actions/upload-artifact@v2 with: - name: NativeMath win-x64 - path: ./gmod/garrysmod/lua/bin/Modules/NativeMath/* + name: HashCalc win-x64 + path: ./gmod/garrysmod/lua/bin/Modules/HashCalc/* retention-days: 10 - name: Copy test.lua run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun @@ -111,6 +111,6 @@ jobs: - name: Upload a Build Artifact uses: actions/upload-artifact@v2 with: - name: NativeMath osx-x64 - path: NativeMath/bin/Release/net5/osx-x64/publish/* + name: HashCalc osx-x64 + path: HashCalc/bin/Release/net5/osx-x64/publish/* retention-days: 10 diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index ce459be..1cf9550 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -1,63 +1,19 @@ hook.Add("Tick", "Template_CloseServer", engine.CloseServer) require("dotnet") -function donothingwithit(something) end - -local function benchmark(name, fn) - local startTime = SysTime() - fn() - local elapsed = SysTime()-startTime - print(name.." "..elapsed) - return elapsed -end - -local function benchmark_thousand(name, fn) - local startTime = SysTime() - for i=0,1000 do - fn() - end - local elapsed = SysTime()-startTime - print(name.."_thousand"..elapsed) - return elapsed -end - -local function math_Approach() - benchmark("math_Approach", function() - local current = 0 - for i=0,10000 do - current = math.Approach(current,100,0.01) - end - print(current) - end) -end - -local function math_Distance() - benchmark("math_Distance", function() - local current = 0 - for i=0,10000 do - current = math.Distance(current,0,200,100) - end - print(current) - end) -end - local function run_test() - local module_loaded = dotnet.load("NativeMath") + local module_loaded = dotnet.load("HashCalc") assert(module_loaded) ----------------------- - math_Approach() - math_Distance() - - NativeMath() - - math_Approach() - math_Distance() + local sha256 = HashCalc.CalculateSHA256("test") + print("SHA256 = " .. sha256) + assert(sha256=="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08") ----------------------- - local module_unloaded = dotnet.unload("NativeMath") + local module_unloaded = dotnet.unload("HashCalc") assert(module_unloaded) end diff --git a/Examples.sln b/Examples.sln index 170ac2b..aa85342 100644 --- a/Examples.sln +++ b/Examples.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30803.129 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeMath", "NativeMath\NativeMath.csproj", "{5849F24D-B5D7-4A48-9469-021DEE4B8531}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HashCalc", "HashCalc\HashCalc.csproj", "{5849F24D-B5D7-4A48-9469-021DEE4B8531}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{342AA3D8-AFF4-48DE-B97C-DE6E0CF35634}" ProjectSection(SolutionItems) = preProject diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs new file mode 100644 index 0000000..d0aea74 --- /dev/null +++ b/HashCalc/HashCalc.cs @@ -0,0 +1,56 @@ +using GmodNET.API; +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text; + +namespace NativeMath +{ + public class HashCalc : IModule + { + public string ModuleName => "HashCalc"; + + public string ModuleVersion => "0.0.1"; + + private List handles; + + [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] + private static int CalculateSHA256(IntPtr luaState) + { + ILua lua = GmodInterop.GetLuaFromState(luaState); + SHA256 sha256 = SHA256.Create(); + string data = lua.GetString(1); + byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(data)); + lua.PushString(Encoding.UTF8.GetString(hash)); + return 1; + } + + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) + { + handles = new(); + + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + lua.CreateTable(); + unsafe + { + lua.PushCFunction(&CalculateSHA256); + + lua.SetField(-2, "CalculateSHA256"); + } + lua.SetField(-2, "HashCalc"); + lua.Pop(); + } + + public void Unload(ILua lua) + { + foreach (var handle in handles) + { + handle.Free(); + } + handles = null; + } + } +} diff --git a/NativeMath/NativeMath.csproj b/HashCalc/HashCalc.csproj similarity index 100% rename from NativeMath/NativeMath.csproj rename to HashCalc/HashCalc.csproj diff --git a/NativeMath/nuget.config b/HashCalc/nuget.config similarity index 100% rename from NativeMath/nuget.config rename to HashCalc/nuget.config diff --git a/NativeMath/NativeMath.cs b/NativeMath/NativeMath.cs deleted file mode 100644 index eca742c..0000000 --- a/NativeMath/NativeMath.cs +++ /dev/null @@ -1,98 +0,0 @@ -using GmodNET.API; -using System; -using System.Collections.Generic; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace NativeMath -{ - public class NativeMath : IModule - { - public string ModuleName => "NativeMath"; - - public string ModuleVersion => "0.0.1"; - - private List handles; - - [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] - private static int Approach(IntPtr luaState) - { - ILua lua = GmodInterop.GetLuaFromState(luaState); - double current = Math.Abs(lua.GetNumber(1)); - double target = lua.GetNumber(2); - double increment = lua.GetNumber(3); - - if (current < target) - { - lua.PushNumber(Math.Min(current + increment, target)); - } - else if (current > target) - { - lua.PushNumber(Math.Max(current - increment, target)); - } - else - { - lua.PushNumber(target); - } - return 1; - } - - [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] - private static int Distance(IntPtr luaState) - { - ILua lua = GmodInterop.GetLuaFromState(luaState); - double x1 = lua.GetNumber(1); - double y1 = lua.GetNumber(2); - double x2 = lua.GetNumber(3); - double y2 = lua.GetNumber(4); - - double xd = x2 - x1; - double yd = y2 - y1; - - lua.PushNumber(Math.Sqrt(xd * xd + yd * yd)); - - return 1; - } - - public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) - { - lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - - handles = new(); - - handles.Add(lua.PushManagedFunction((lua) => - { - lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.GetField(-1, "math"); - unsafe - { - lua.PushCFunction(&Approach); - } - lua.SetField(-2, "Approach"); - lua.Pop(); - - lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.GetField(-1, "math"); - unsafe - { - lua.PushCFunction(&Distance); - } - lua.SetField(-2, "Distance"); - lua.Pop(); - return 0; - })); - lua.SetField(-2, "NativeMath"); - lua.Pop(); - } - - public void Unload(ILua lua) - { - foreach (var handle in handles) - { - handle.Free(); - } - handles = null; - } - } -} From 95a2c410cdaa661482fe4636651ceb4ef8df5d6b Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:20:56 +0500 Subject: [PATCH 17/29] ToString("X2") --- HashCalc/HashCalc.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index d0aea74..0be45bd 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -24,8 +24,14 @@ private static int CalculateSHA256(IntPtr luaState) SHA256 sha256 = SHA256.Create(); string data = lua.GetString(1); byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(data)); - lua.PushString(Encoding.UTF8.GetString(hash)); - return 1; + int hashLength = hash.Length; + StringBuilder builder = new(); + for (int i = 0; i < hashLength; i++) + { + builder.Append(hash[i].ToString("X2")); + } + lua.PushString(builder.ToString()); + return 2; } public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) From 34ec90f94823c8af29cad8cc9c535bcf60d43b1f Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:27:35 +0500 Subject: [PATCH 18/29] print --- HashCalc/HashCalc.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 0be45bd..4990be6 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -30,6 +30,7 @@ private static int CalculateSHA256(IntPtr luaState) { builder.Append(hash[i].ToString("X2")); } + Console.WriteLine($"SHA256({data}): {hash}"); lua.PushString(builder.ToString()); return 2; } From d274c4adfa2090095def178c25cd6d1a772a0e63 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:30:59 +0500 Subject: [PATCH 19/29] builder --- HashCalc/HashCalc.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 4990be6..3cc594d 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -30,7 +30,7 @@ private static int CalculateSHA256(IntPtr luaState) { builder.Append(hash[i].ToString("X2")); } - Console.WriteLine($"SHA256({data}): {hash}"); + Console.WriteLine($"SHA256({data}): {builder}"); lua.PushString(builder.ToString()); return 2; } From b2a8ebf67e0a3b570990a2ed8db1559c6b078636 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:31:12 +0500 Subject: [PATCH 20/29] return 1 --- HashCalc/HashCalc.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 3cc594d..e480033 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -32,7 +32,7 @@ private static int CalculateSHA256(IntPtr luaState) } Console.WriteLine($"SHA256({data}): {builder}"); lua.PushString(builder.ToString()); - return 2; + return 1; } public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) From 1c2c848f014069bffab9e588af8e2020fd4f6eae Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:41:33 +0500 Subject: [PATCH 21/29] fix casing --- .github/workflows/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 1cf9550..b3d8a56 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -9,7 +9,7 @@ local function run_test() local sha256 = HashCalc.CalculateSHA256("test") print("SHA256 = " .. sha256) - assert(sha256=="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08") + assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) ----------------------- From 4c340b93cac57e6b3cc660c9241b5dbd9fe3ef04 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 18:42:33 +0500 Subject: [PATCH 22/29] sha256 unicode test --- .github/workflows/test.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index b3d8a56..c41cdb0 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -8,9 +8,11 @@ local function run_test() ----------------------- local sha256 = HashCalc.CalculateSHA256("test") - print("SHA256 = " .. sha256) assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) + local sha256_unicode = HashCalc.CalculateSHA256("☭") + assert(sha256_unicode==string.upper("d7f6fc82e3665ba40899bbf7bf6dc6f5c7f349ea054f6360c1d983f01c07601a")) + ----------------------- local module_unloaded = dotnet.unload("HashCalc") From fc08c76d38acbead3f9686172445351479823bb0 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 19:08:00 +0500 Subject: [PATCH 23/29] CalculateHash --- .github/workflows/test.lua | 4 ++-- HashCalc/HashCalc.cs | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index c41cdb0..293aa82 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -7,10 +7,10 @@ local function run_test() ----------------------- - local sha256 = HashCalc.CalculateSHA256("test") + local sha256 = CalculateHash("SHA256","test") assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) - local sha256_unicode = HashCalc.CalculateSHA256("☭") + local sha256_unicode = CalculateHash("SHA256","☭") assert(sha256_unicode==string.upper("d7f6fc82e3665ba40899bbf7bf6dc6f5c7f349ea054f6360c1d983f01c07601a")) ----------------------- diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index e480033..8d2c2a0 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -18,19 +18,19 @@ public class HashCalc : IModule private List handles; [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] - private static int CalculateSHA256(IntPtr luaState) + private static int CalculateHash(IntPtr luaState) { ILua lua = GmodInterop.GetLuaFromState(luaState); - SHA256 sha256 = SHA256.Create(); - string data = lua.GetString(1); - byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(data)); + string hashName = lua.GetString(1); + string data = lua.GetString(2); + HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName); + byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data)); int hashLength = hash.Length; StringBuilder builder = new(); for (int i = 0; i < hashLength; i++) { builder.Append(hash[i].ToString("X2")); } - Console.WriteLine($"SHA256({data}): {builder}"); lua.PushString(builder.ToString()); return 1; } @@ -40,14 +40,11 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl handles = new(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - lua.CreateTable(); unsafe { - lua.PushCFunction(&CalculateSHA256); - - lua.SetField(-2, "CalculateSHA256"); + lua.PushCFunction(&CalculateHash); + lua.SetField(-2, "CalculateHash"); } - lua.SetField(-2, "HashCalc"); lua.Pop(); } From c6659d731bf22bd6a37a79ed62523c87de2f9ce9 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 19:17:31 +0500 Subject: [PATCH 24/29] md5 test --- .github/workflows/test.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index 293aa82..a9d85ba 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -13,6 +13,11 @@ local function run_test() local sha256_unicode = CalculateHash("SHA256","☭") assert(sha256_unicode==string.upper("d7f6fc82e3665ba40899bbf7bf6dc6f5c7f349ea054f6360c1d983f01c07601a")) + local md5 = CalculateHash("MD5","test") + assert(md5==string.upper("098f6bcd4621d373cade4e832627b4f6")) + + local md5_unicode = CalculateHash("MD5","☭") + assert(md5_unicode==string.upper("7da15a125400bc658992457c218d7d47")) ----------------------- local module_unloaded = dotnet.unload("HashCalc") From cbe238489d900e0be44814b118e4f5ea658db7f8 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 2 Jan 2021 19:25:19 +0500 Subject: [PATCH 25/29] SHA512 --- .github/workflows/test.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index a9d85ba..c8970c1 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -5,7 +5,7 @@ local function run_test() local module_loaded = dotnet.load("HashCalc") assert(module_loaded) - ----------------------- + --------SHA256-------- local sha256 = CalculateHash("SHA256","test") assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) @@ -13,12 +13,23 @@ local function run_test() local sha256_unicode = CalculateHash("SHA256","☭") assert(sha256_unicode==string.upper("d7f6fc82e3665ba40899bbf7bf6dc6f5c7f349ea054f6360c1d983f01c07601a")) + ---------MD5---------- + local md5 = CalculateHash("MD5","test") assert(md5==string.upper("098f6bcd4621d373cade4e832627b4f6")) local md5_unicode = CalculateHash("MD5","☭") assert(md5_unicode==string.upper("7da15a125400bc658992457c218d7d47")) - ----------------------- + + --------SHA512-------- + + local sha512 = CalculateHash("SHA512","test") + assert(sha512==string.upper("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff")) + + local sha512_unicode = CalculateHash("SHA512","☭") + assert(sha512_unicode==string.upper("52d54669d285a26c36374220204ea3e7ac480a7ef1d39cd5af0a6852309caa46faf19ebf0122eccae9ae4d4575b805e162a0b725c92aa43fd9a169835fea1734")) + + ---------------------- local module_unloaded = dotnet.unload("HashCalc") assert(module_unloaded) From 722c57262345a8d386b01b548711d9f4f7d53a3f Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Tue, 5 Jan 2021 17:57:03 +0500 Subject: [PATCH 26/29] Managed function --- HashCalc/HashCalc.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 8d2c2a0..55c2de1 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -17,10 +17,8 @@ public class HashCalc : IModule private List handles; - [UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })] - private static int CalculateHash(IntPtr luaState) + private static int CalculateHash(ILua lua) { - ILua lua = GmodInterop.GetLuaFromState(luaState); string hashName = lua.GetString(1); string data = lua.GetString(2); HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName); @@ -40,11 +38,9 @@ public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembl handles = new(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); - unsafe - { - lua.PushCFunction(&CalculateHash); - lua.SetField(-2, "CalculateHash"); - } + GCHandle handle = lua.PushManagedFunction(CalculateHash); + handles.Add(handle); + lua.SetField(-2, "CalculateHash"); lua.Pop(); } From 62e180b4223f0e1276729ab23effc597d0b543a5 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Tue, 5 Jan 2021 20:12:20 +0500 Subject: [PATCH 27/29] remove unused includes delete .editorconfig replace ~~ussr~~ hammer and sickle unicode with heart unicode --- .editorconfig | 9 --------- .github/workflows/test.lua | 12 ++++++------ Examples.sln | 1 - HashCalc/HashCalc.cs | 3 --- 4 files changed, 6 insertions(+), 19 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 9ab54b0..0000000 --- a/.editorconfig +++ /dev/null @@ -1,9 +0,0 @@ -[*] -end_of_line = crlf -charset = utf-8 -indent_style = tab -indent_size = tab -trim_trailing_whitespace = true -[*.yml] -indent_style = space -indent_size = 2 diff --git a/.github/workflows/test.lua b/.github/workflows/test.lua index c8970c1..d153cee 100644 --- a/.github/workflows/test.lua +++ b/.github/workflows/test.lua @@ -10,24 +10,24 @@ local function run_test() local sha256 = CalculateHash("SHA256","test") assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) - local sha256_unicode = CalculateHash("SHA256","☭") - assert(sha256_unicode==string.upper("d7f6fc82e3665ba40899bbf7bf6dc6f5c7f349ea054f6360c1d983f01c07601a")) + local sha256_unicode = CalculateHash("SHA256","♥") + assert(sha256_unicode==string.upper("7c546e1819d9562a67643820b4305c3892fd9a151628e447c8e8dae162fe8f57")) ---------MD5---------- local md5 = CalculateHash("MD5","test") assert(md5==string.upper("098f6bcd4621d373cade4e832627b4f6")) - local md5_unicode = CalculateHash("MD5","☭") - assert(md5_unicode==string.upper("7da15a125400bc658992457c218d7d47")) + local md5_unicode = CalculateHash("MD5","♥") + assert(md5_unicode==string.upper("d7d4076e1ad9c734baad210480cc903a")) --------SHA512-------- local sha512 = CalculateHash("SHA512","test") assert(sha512==string.upper("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff")) - local sha512_unicode = CalculateHash("SHA512","☭") - assert(sha512_unicode==string.upper("52d54669d285a26c36374220204ea3e7ac480a7ef1d39cd5af0a6852309caa46faf19ebf0122eccae9ae4d4575b805e162a0b725c92aa43fd9a169835fea1734")) + local sha512_unicode = CalculateHash("SHA512","♥") + assert(sha512_unicode==string.upper("09de9680f8bfc115d42d36646ef28b4cfe3fbffab095bb2ac86c1be9d9ad164a67aa07227d13fea504acf320a68f5fc507c86b803942a7023ccb0e4d23b65f04")) ---------------------- diff --git a/Examples.sln b/Examples.sln index aa85342..8e0b17e 100644 --- a/Examples.sln +++ b/Examples.sln @@ -7,7 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HashCalc", "HashCalc\HashCa EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{342AA3D8-AFF4-48DE-B97C-DE6E0CF35634}" ProjectSection(SolutionItems) = preProject - .editorconfig = .editorconfig .gitignore = .gitignore .github\workflows\build.yml = .github\workflows\build.yml README.md = README.md diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 55c2de1..7c91c49 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -1,8 +1,5 @@ using GmodNET.API; -using System; using System.Collections.Generic; -using System.Numerics; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; From 064c040d15ab96aa3121f5b2b95986fc3f611f68 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Sat, 16 Jan 2021 23:01:03 +0500 Subject: [PATCH 28/29] add hashName check --- HashCalc/HashCalc.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs index 7c91c49..f4af397 100644 --- a/HashCalc/HashCalc.cs +++ b/HashCalc/HashCalc.cs @@ -1,5 +1,7 @@ using GmodNET.API; +using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; @@ -14,10 +16,19 @@ public class HashCalc : IModule private List handles; + private static readonly string[] hashNames = new[]{ + "SHA", "SHA1", "System.Security.Cryptography.SHA1", "System.Security.Cryptography.HashAlgorithm", + "MD5", "System.Security.Cryptography.MD5", + "SHA256", "SHA-256", "System.Security.Cryptography.SHA256", + "SHA384", "SHA-384", "System.Security.Cryptography.SHA384", + "SHA512", "SHA-512", "System.Security.Cryptography.SHA512" + }; + private static int CalculateHash(ILua lua) { string hashName = lua.GetString(1); string data = lua.GetString(2); + if (!hashNames.Contains(hashName)) throw new ArgumentException("invalid hash algorithm name"); HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName); byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data)); int hashLength = hash.Length; From 0d173d2f697bf402ceba7760b9f63be754119783 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Thu, 4 Feb 2021 19:30:33 +0500 Subject: [PATCH 29/29] add timeout minutes --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e121ef8..1c9894f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -86,6 +86,7 @@ jobs: run: | powershell -Command './gmod/srcds_win64.exe -console -systemtest -condebug -game "garrysmod" +exec "server.cfg" +gamemode sandbox +map gm_construct +maxplayers 16 +sv_hibernate_think 1' powershell -Command 'Wait-Process -Name srcds_win64' + timeout-minutes: 1 continue-on-error: true - name: Print log shell: bash