diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..1c9894f --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,117 @@ +name: Build & Test +on: + push: + branches: [ main, HashCalc ] + 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 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: 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 + - 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 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: 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 + - 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' + timeout-minutes: 1 + 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: 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 new file mode 100644 index 0000000..d153cee --- /dev/null +++ b/.github/workflows/test.lua @@ -0,0 +1,41 @@ +hook.Add("Tick", "Template_CloseServer", engine.CloseServer) +require("dotnet") + +local function run_test() + local module_loaded = dotnet.load("HashCalc") + assert(module_loaded) + + --------SHA256-------- + + local sha256 = CalculateHash("SHA256","test") + assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) + + 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("d7d4076e1ad9c734baad210480cc903a")) + + --------SHA512-------- + + local sha512 = CalculateHash("SHA512","test") + assert(sha512==string.upper("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff")) + + local sha512_unicode = CalculateHash("SHA512","♥") + assert(sha512_unicode==string.upper("09de9680f8bfc115d42d36646ef28b4cfe3fbffab095bb2ac86c1be9d9ad164a67aa07227d13fea504acf320a68f5fc507c86b803942a7023ccb0e4d23b65f04")) + + ---------------------- + + local module_unloaded = dotnet.unload("HashCalc") + 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..8e0b17e --- /dev/null +++ b/Examples.sln @@ -0,0 +1,33 @@ + +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}") = "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 + .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/HashCalc/HashCalc.cs b/HashCalc/HashCalc.cs new file mode 100644 index 0000000..f4af397 --- /dev/null +++ b/HashCalc/HashCalc.cs @@ -0,0 +1,64 @@ +using GmodNET.API; +using System; +using System.Collections.Generic; +using System.Linq; +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; + + 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; + StringBuilder builder = new(); + for (int i = 0; i < hashLength; i++) + { + builder.Append(hash[i].ToString("X2")); + } + lua.PushString(builder.ToString()); + return 1; + } + + public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) + { + handles = new(); + + lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); + GCHandle handle = lua.PushManagedFunction(CalculateHash); + handles.Add(handle); + lua.SetField(-2, "CalculateHash"); + lua.Pop(); + } + + public void Unload(ILua lua) + { + foreach (var handle in handles) + { + handle.Free(); + } + handles = null; + } + } +} diff --git a/HashCalc/HashCalc.csproj b/HashCalc/HashCalc.csproj new file mode 100644 index 0000000..c339c8c --- /dev/null +++ b/HashCalc/HashCalc.csproj @@ -0,0 +1,16 @@ + + + Library + net5 + + + true + + + true + + + + + + diff --git a/HashCalc/nuget.config b/HashCalc/nuget.config new file mode 100644 index 0000000..d526a78 --- /dev/null +++ b/HashCalc/nuget.config @@ -0,0 +1,8 @@ + + + + + + + +