Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 10 additions & 7 deletions managed/CounterStrikeSharp.API/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
using CounterStrikeSharp.API.Core.Plugin.Host;
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Admin;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Serilog;
Expand All @@ -29,7 +29,7 @@
try
{
// Path to /game/csgo/addons/counterstrikesharp
var contentRoot = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.Parent.FullName;

Check warning on line 32 in managed/CounterStrikeSharp.API/Bootstrap.cs

View workflow job for this annotation

GitHub Actions / build_managed

Dereference of a possibly null reference.

Check warning on line 32 in managed/CounterStrikeSharp.API/Bootstrap.cs

View workflow job for this annotation

GitHub Actions / build_managed

Dereference of a possibly null reference.

Check warning on line 32 in managed/CounterStrikeSharp.API/Bootstrap.cs

View workflow job for this annotation

GitHub Actions / build_managed

Dereference of a possibly null reference.

Check warning on line 32 in managed/CounterStrikeSharp.API/Bootstrap.cs

View workflow job for this annotation

GitHub Actions / build_managed

Dereference of a possibly null reference.

using var host = Host.CreateDefaultBuilder()
.UseContentRoot(contentRoot)
Expand All @@ -40,16 +40,17 @@
builder.ClearProviders();
builder.AddCoreLogging(contentRoot);
});
services.AddSingleton<NativeBridge>();

services.AddSingleton<IScriptHostConfiguration, ScriptHostConfiguration>();
services.AddScoped<Application>();
services.AddSingleton<IPluginManager, PluginManager>();
services.AddSingleton<IPlayerLanguageManager, PlayerLanguageManager>();
services.AddScoped<IPluginContextQueryHandler, PluginContextQueryHandler>();
services.AddSingleton<ICommandManager, CommandManager>();

services.TryAddSingleton<IStringLocalizerFactory, CoreJsonStringLocalizerFactory>();
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
services.AddSingleton<ICommandManager, CommandManager>();
services.TryAddSingleton<IStringLocalizerFactory, CoreJsonStringLocalizerFactory>();
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
services.TryAddTransient(typeof(IStringLocalizer), typeof(StringLocalizer));

services.Scan(i => i.FromCallingAssembly()
Expand All @@ -62,6 +63,8 @@
using IServiceScope rootScope = host.Services.CreateScope();

// TODO: Improve static singleton access
var nativeBridge = rootScope.ServiceProvider.GetRequiredService<NativeBridge>();
nativeBridge.Initialize();
GameData.GameDataProvider = rootScope.ServiceProvider.GetRequiredService<GameDataProvider>();
AdminManager.CommandManagerProvider = rootScope.ServiceProvider.GetRequiredService<ICommandManager>();

Expand All @@ -77,4 +80,4 @@
return 0;
}
}
}
}
33 changes: 0 additions & 33 deletions managed/CounterStrikeSharp.API/Core/Helpers.cs

This file was deleted.

97 changes: 97 additions & 0 deletions managed/CounterStrikeSharp.API/Core/NativeBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.Extensions.Logging;

namespace CounterStrikeSharp.API.Core;

public class NativeBridge
{
private readonly ILogger<NativeBridge> _logger;
private static ILogger<NativeBridge>? _staticLogger;

private static nint _libHandle;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void InvokeNativeDelegate(IntPtr ptr);

private static InvokeNativeDelegate? _invokeNative;

public NativeBridge(ILogger<NativeBridge> logger)
{
_logger = logger;
_staticLogger ??= logger;
}

public bool Initialize()
{
var libName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "counterstrikesharp.dll"
: "counterstrikesharp.so";

try
{
_libHandle = NativeLibrary.Load(libName);
_invokeNative = GetDelegate<InvokeNativeDelegate>("InvokeNative");

_logger.LogInformation("Loaded native lib OK ({Library})", libName);
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to load native lib '{Library}'", libName);
return false;
}
}

private static T? GetDelegate<T>(string exportName) where T : class
{
try
{
var ptr = NativeLibrary.GetExport(_libHandle, exportName);
return Marshal.GetDelegateForFunctionPointer<T>(ptr);
}
catch (Exception ex)
{
_staticLogger?.LogError(ex, "Missing export {ExportName}", exportName);
return null;
}
}

[SecurityCritical]
public static void InvokeNative(IntPtr ptr)
{
if (_invokeNative == null)
{
_staticLogger?.LogError("InvokeNative not initialized!");
return;
}

try
{
_invokeNative.Invoke(ptr);
}
catch (Exception ex)
{
_staticLogger?.LogError(ex, "Error while invoking native function");
}
}
}
2 changes: 1 addition & 1 deletion managed/CounterStrikeSharp.API/Core/ScriptContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void InvokeNativeInternal()
{
fixed (fxScriptContext* cxt = &m_extContext)
{
Helpers.InvokeNative(new IntPtr(cxt));
NativeBridge.InvokeNative(new IntPtr(cxt));
}
}
}
Expand Down
Loading