mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Try to fix ZStd stuff for servers.
This commit is contained in:
19
Robust.Shared/Native/Libc.cs
Normal file
19
Robust.Shared/Native/Libc.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Robust.Shared.Native;
|
||||
|
||||
internal static class Libc
|
||||
{
|
||||
public const int RTLD_LAZY = 0x00001;
|
||||
public const int RTLD_NOW = 0x00002;
|
||||
public const int RTLD_BINDING_MASK = 0x3;
|
||||
public const int RTLD_NOLOAD = 0x00004;
|
||||
public const int RTLD_DEEPBIND = 0x00008;
|
||||
public const int RTLD_GLOBAL = 0x00100;
|
||||
public const int RTLD_LOCAL = 0;
|
||||
public const int RTLD_NODELETE = 0x01000;
|
||||
|
||||
[DllImport("libdl.so.2")]
|
||||
public static extern IntPtr dlopen([MarshalAs(UnmanagedType.LPUTF8Str)] string name, int flags);
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpZstd.Interop;
|
||||
using static Robust.Shared.Native.Libc;
|
||||
using static SharpZstd.Interop.Zstd;
|
||||
|
||||
namespace Robust.Shared.Utility;
|
||||
@@ -30,6 +32,61 @@ public static class ZStd
|
||||
return (int) result;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable CA2255
|
||||
[ModuleInitializer]
|
||||
#pragma warning restore CA2255
|
||||
internal static void InitZStd()
|
||||
{
|
||||
try
|
||||
{
|
||||
NativeLibrary.SetDllImportResolver(
|
||||
typeof(Zstd).Assembly,
|
||||
ResolveZstd
|
||||
);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Launcher loader probably already set this, ignore.
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr ResolveZstd(string name, Assembly assembly, DllImportSearchPath? path)
|
||||
{
|
||||
if (name == "zstd" && OperatingSystem.IsLinux())
|
||||
{
|
||||
try
|
||||
{
|
||||
var paths = (string)AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES")!;
|
||||
foreach (var p in paths.Split(':', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var tryPath = Path.Join(p, "zstd.so");
|
||||
// Console.WriteLine($"TRYING: {tryPath}");
|
||||
var result = dlopen(tryPath, RTLD_LOCAL | RTLD_DEEPBIND | RTLD_LAZY);
|
||||
// Console.WriteLine(result);
|
||||
if (result != IntPtr.Zero)
|
||||
{
|
||||
// Console.WriteLine($"FOUND: {tryPath}");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Catch and at least provide some way of retrieving this.
|
||||
System.Console.Error.WriteLine($"Exception during ZStd libdl search: {ex}");
|
||||
}
|
||||
|
||||
// Try some extra paths too worst case.
|
||||
if (NativeLibrary.TryLoad("libzstd.so.1", out var handle))
|
||||
return handle;
|
||||
|
||||
if (NativeLibrary.TryLoad("libzstd.so", out handle))
|
||||
return handle;
|
||||
}
|
||||
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
|
||||
Reference in New Issue
Block a user