Skip to content
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
116 changes: 116 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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'
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
41 changes: 41 additions & 0 deletions .github/workflows/test.lua
Original file line number Diff line number Diff line change
@@ -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("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)
end

run_test()

print("tests are successful!")
file.Write("success.txt", "done")
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs
bin
obj
34 changes: 34 additions & 0 deletions Examples.sln
Original file line number Diff line number Diff line change
@@ -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}") = "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
.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
60 changes: 60 additions & 0 deletions HashCalc/HashCalc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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<GCHandle> handles;

[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private static int CalculateHash(IntPtr luaState)
{
ILua lua = GmodInterop.GetLuaFromState(luaState);
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"));
}
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);
unsafe
{
lua.PushCFunction(&CalculateHash);
lua.SetField(-2, "CalculateHash");
}
lua.Pop();
}

public void Unload(ILua lua)
{
foreach (var handle in handles)
{
handle.Free();
}
handles = null;
}
}
}
16 changes: 16 additions & 0 deletions HashCalc/HashCalc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GmodNET.API" Version="0.7.0-*" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions HashCalc/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="gmodnet-packages" value="https://pkgs.dev.azure.com/GmodNET/gmodnet-artifacts/_packaging/gmodnet-packages/nuget/v3/index.json" />
</packageSources>
</configuration>