Skip to content

Commit

Permalink
Merge branch 'verification' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
RainOrigami committed Sep 24, 2023
2 parents 5cfcdf8 + 1b043a0 commit 72fb247
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 75 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build-artifacts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build Artifacts

permissions:
contents: write

on:
push:
branches:
- '*'

jobs:
build-artifacts:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up .NET Core
uses: actions/[email protected]
with:
dotnet-version: 6

- name: NuGet Restore
run: dotnet restore

- name: Build and Publish project
run: dotnet publish BattleBitAPIRunner/BattleBitAPIRunner.csproj -c Release -o ./publish --self-contained false --framework net6.0 --runtime win-x64

- name: Build nuget
run: dotnet build BattleBitAPIRunner/BattleBitAPIRunner.csproj -c Release

- name: Create NuGet package
run: dotnet pack BBRAPIModules/BBRAPIModules.csproj --configuration Release --output ./nuget

- name: Upload NuGet package artifact
uses: actions/upload-artifact@v2
with:
name: NuGet Package
path: ./nuget/*.nupkg

- name: Upload Release artifact
uses: actions/upload-artifact@v2
with:
name: BattleBitAPIRunner-beta
path: ./publish/*
15 changes: 15 additions & 0 deletions BBRAPIModuleVerfication/BBRAPIModuleVerification.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BattleBitAPIRunner\BattleBitAPIRunner.csproj" />
<ProjectReference Include="..\BBRAPIModules\BBRAPIModules.csproj" />
</ItemGroup>

</Project>
99 changes: 99 additions & 0 deletions BBRAPIModuleVerfication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using BattleBitAPIRunner;
using BBRAPIModules;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using Microsoft.CodeAnalysis.CSharp;
using System.Text;
using System.Collections.ObjectModel;

namespace BBRAPIModuleVerification;

internal class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: BBRAPIModuleVerification <path to module.cs>");
return;
}

string filePath = args[0];

Module.logToConsole = false;

Module module;
try
{
module = new Module(filePath);
}
catch (Exception e)
{
Console.WriteLine(JsonConvert.SerializeObject(new VerificationResponse(false, Path.GetFileNameWithoutExtension(filePath), null, null, null, null, e.Message)));
return;
}

List<string> missingDependencies = new();
List<PortableExecutableReference> references = new();

foreach (string dependency in module.RequiredDependencies)
{
if (!Directory.Exists($"./cache/modules/{dependency}"))
{
missingDependencies.Add(dependency);
}
else
{
references.Add(MetadataReference.CreateFromFile($"./cache/modules/{dependency}/{dependency}.dll"));
}
}

if (missingDependencies.Count > 0)
{
Console.WriteLine(JsonConvert.SerializeObject(new VerificationResponse(false, module.Name, module.Description, module.Version, module.RequiredDependencies, module.OptionalDependencies, $"Missing dependencies: {string.Join(", ", missingDependencies)}")));
return;
}

try
{
module.Compile(references.ToArray());
if (!Directory.Exists($"./cache/modules/{module.Name}"))
{
Directory.CreateDirectory($"./cache/modules/{module.Name}");
}
File.WriteAllBytes($"./cache/modules/{module.Name}/{module.Name}.dll", module.AssemblyBytes);

Console.WriteLine(JsonConvert.SerializeObject(new VerificationResponse(true, module.Name, module.Description, module.Version, module.RequiredDependencies, module.OptionalDependencies, null)));
}
catch (Exception e)
{
Console.WriteLine(JsonConvert.SerializeObject(new VerificationResponse(false, module.Name, module.Description, module.Version, module.RequiredDependencies, module.OptionalDependencies, e.Message)));
return;
}
}


}

internal class VerificationResponse
{
public VerificationResponse(bool success, string name, string description, string version, string[] requiredDependencies, string[] optionalDependencies, string errors)
{
this.Success = success;
this.Name = name;
this.Description = description;
this.Version = version;
this.RequiredDependencies = requiredDependencies;
this.OptionalDependencies = optionalDependencies;
this.Errors = errors;
}

public bool Success { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string? Version { get; set; }
public string[]? RequiredDependencies { get; set; }
public string[]? OptionalDependencies { get; set; }
public string? Errors { get; set; }
}
8 changes: 8 additions & 0 deletions BBRAPIModuleVerfication/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"BBRAPIModuleVerification": {
"commandName": "Project",
"commandLineArgs": "E:\\Create\\Development\\Software\\C#\\BattleBit\\BattleBitBaseModules\\SpectateControl.cs"
}
}
}
2 changes: 1 addition & 1 deletion BBRAPIModules/BBRAPIModules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>BattleBit Remastered Modular Community Server API</Title>
<AssemblyVersion>0.4.11</AssemblyVersion>
<AssemblyVersion>0.4.13</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<Version>$(AssemblyVersion)</Version>
<Authors>RainOrigami</Authors>
Expand Down
40 changes: 2 additions & 38 deletions BBRAPIModules/BattleBitModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("BattleBitAPIRunner")]
Expand All @@ -19,41 +17,6 @@ internal void SetServer(RunnerServer server)
this.Server = server;
}

public void Call(string methodName, params object?[]? parameters)
{
this.Call<object?>(methodName, parameters);
}

public T Call<T>(string methodName, params object?[]? parameters)
{
var method = this.GetType().GetMethod(methodName);
if (method == null)
{
return default(T);
}

ParameterInfo[] methodParameters = method.GetParameters();

List<object?> fullParameters = new(parameters ?? new object?[] { });

if (methodParameters.Length > 0 && methodParameters.Length != parameters?.Length)
{
for (int i = parameters?.Length ?? 0; i < methodParameters.Length; i++)
{
if (methodParameters[i].IsOptional || methodParameters[i].HasDefaultValue)
{
fullParameters.Add(methodParameters[i].DefaultValue);
}
else
{
throw new ArgumentException($"Parameter {methodParameters[i].Name} is not optional and does not have a default value.");
}
}
}

return (T)method.Invoke(this, fullParameters.ToArray());
}

public void Unload()
{
this.IsLoaded = false;
Expand All @@ -63,6 +26,7 @@ public void Unload()

public virtual void OnModulesLoaded() { } // sighs silently
public virtual void OnModuleUnloading() { }
public virtual void OnConsoleCommand(string command) { }

#region GameServer.cs copy-paste
// TODO: there must be a better way to do this!?
Expand Down
20 changes: 20 additions & 0 deletions BBRAPIModules/ModuleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBRAPIModules;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class ModuleAttribute : Attribute
{
public string Description { get; set; }
public string Version { get; set; }

public ModuleAttribute(string description, string version)
{
this.Description = description;
this.Version = version;
}
}
3 changes: 2 additions & 1 deletion BBRAPIModules/RunnerServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace BBRAPIModules
{
Expand Down Expand Up @@ -114,7 +115,7 @@ private async Task<bool> invokeOnModulesWithBoolReturnValue(string method, param
return result;
}

private async Task invokeOnModules(string method, params object?[] parameters)
internal async Task invokeOnModules(string method, params object?[] parameters)
{
Stopwatch stopwatch = new();
foreach (BattleBitModule module in this.modules)
Expand Down
6 changes: 6 additions & 0 deletions BattleBitAPIRunner.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BattleBitAPIRunner", "Battl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BBRAPIModules", "BBRAPIModules\BBRAPIModules.csproj", "{B17890B8-4D8C-4FA7-BFFB-20679A42AF7B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BBRAPIModuleVerification", "BBRAPIModuleVerfication\BBRAPIModuleVerification.csproj", "{2A5EC0BE-9755-4383-A4B6-A3D2275DA9A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{B17890B8-4D8C-4FA7-BFFB-20679A42AF7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B17890B8-4D8C-4FA7-BFFB-20679A42AF7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B17890B8-4D8C-4FA7-BFFB-20679A42AF7B}.Release|Any CPU.Build.0 = Release|Any CPU
{2A5EC0BE-9755-4383-A4B6-A3D2275DA9A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A5EC0BE-9755-4383-A4B6-A3D2275DA9A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A5EC0BE-9755-4383-A4B6-A3D2275DA9A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A5EC0BE-9755-4383-A4B6-A3D2275DA9A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion BattleBitAPIRunner/BattleBitAPIRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>0.4.12.6</AssemblyVersion>
<AssemblyVersion>0.4.13</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
</PropertyGroup>

Expand Down
Loading

0 comments on commit 72fb247

Please sign in to comment.