public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                assembly_context.SetCustomNativeLibraryResolver((context, lib_name) =>
                {
                    if (lib_name == "SomeRandomLibraryName")
                    {
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            return(NativeLibrary.Load("Kernel32"));
                        }
                        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                        {
                            return(NativeLibrary.Load("libdl.so"));
                        }
                        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                        {
                            return(NativeLibrary.Load("libdl.dylib"));
                        }
                        else
                        {
                            return(IntPtr.Zero);
                        }
                    }
                    else
                    {
                        return(IntPtr.Zero);
                    }
                });

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    LoadLibraryA("Kernel32");
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    dlopen("libdl", 0);
                }
                else
                {
                    throw new PlatformNotSupportedException("The current OS platform is not supported by GmodNET.Tests");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            assembly_context.SetCustomNativeLibraryResolver(null);

            return(taskCompletion.Task);
        }
Beispiel #2
0
        void IModule.Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
        {
            if (is_serverside)
            {
                throw new Exception("Must be loaded from client");
            }

            string platformIdentifier = OperatingSystem.IsWindows() ? "win-x64" : "linux-x64";

            assembly_context.SetCustomNativeLibraryResolver((ctx, str) =>
            {
                if (str.Contains("sourcesdkc"))
                {
                    if (sourcesdkc == IntPtr.Zero)
                    {
                        Console.WriteLine("loading sourcesdkc");
                        sourcesdkc = NativeLibrary.Load($"./garrysmod/lua/bin/Modules/GetRenderTargetExample/runtimes/{platformIdentifier}/native/sourcesdkc");
                        Console.WriteLine($"loaded sourcesdkc: {sourcesdkc != IntPtr.Zero}");
                    }
                    return(sourcesdkc);
                }
                return(IntPtr.Zero);
            });

            if (interfaceh.Sys_LoadInterface("vguimatsurface", ISurface.VGUI_SURFACE_INTERFACE_VERSION, out _, out IntPtr isurfacePtr))
            {
                surface = new(isurfacePtr);
            }
            else
            {
                throw new Exception("failed loading vguimatsurface");
            }

            if (interfaceh.Sys_LoadInterface("materialsystem", IMaterialSystem.MATERIAL_SYSTEM_INTERFACE_VERSION, out _, out IntPtr materialSystemPtr))
            {
                materialSystem = new(materialSystemPtr);
            }
            else
            {
                Console.WriteLine("failed loading materialsystem");
            }

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "GetRenderTarget");
            lua.PushString("ExampleRTwithAlpha");
            lua.PushNumber(512);
            lua.PushNumber(512);
            lua.MCall(3, 1);
            rt = lua.GetUserType(-1, (int)TYPES.TEXTURE);
            lua.Pop();

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "CreateMaterial");
            lua.PushString("ExampleRTwithAlpha_Mat");
            lua.PushString("UnlitGeneric");
            lua.CreateTable();
            {
                lua.PushString("$basetexture");
                lua.PushString("ExampleRTwithAlpha");
                lua.SetTable(-3);

                lua.PushString("$translucent");
                lua.PushString("1");
                lua.SetTable(-3);
            }
            lua.MCall(3, 1);
            mat = lua.GetUserType(-1, (int)TYPES.MATERIAL);
            lua.Pop();

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, "hook");
            lua.GetField(-1, "Add");
            lua.PushString("HUDPaint");
            lua.PushString("ExampleRTwithAlpha_Render");
            lua.PushManagedFunction(Render);
            lua.MCall(3, 0);
            lua.Pop();
        }
        public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
        {
            string platformIdentifier = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win-x64" : "linux-x64";

            assembly_context.SetCustomNativeLibraryResolver((ctx, str) =>
            {
                if (str.Contains("sourcesdkc"))
                {
                    Console.WriteLine("loading sourcesdkc");
                    sourcesdkc = NativeLibrary.Load($"./garrysmod/lua/bin/Modules/SourceSDKTest/runtimes/{platformIdentifier}/native/sourcesdkc");
                    Console.WriteLine($"loaded sourcesdkc: {sourcesdkc != IntPtr.Zero}");
                    return(sourcesdkc);
                }
                return(IntPtr.Zero);
            });

            Test(() => Dbg.Msg("Msg(string)\n"));

            Test(() => Dbg.Warning("Warning(string)\n"));

            Test(() => Dbg.Warning_SpewCallStack(100, "Warning_SpewCallStack(int, string)\n"));

            Test(() => Dbg.DevMsg("DevMsg(string)\n"));
            Test(() => Dbg.DevWarning("DevWarning(string)\n"));

            Test(() => Dbg.ConColorMsg(new Color(255, 255, 0), "ConColorMsg(in Color, string)\n"));

            Test(() => Dbg.ConMsg("ConMsg(string)\n"));
            Test(() => Dbg.ConDMsg("ConDMsg(string)\n"));

            // Test(() => Dbg.COM_TimestampedLog("COM_TimestampedLog(format = %s)", "COM_TimestampedLog"));

            Test(() =>
            {
                unsafe
                {
                    string path = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "filesystem_stdio.dll" : "filesystem_stdio.so";

                    if (!interfaceh.Sys_LoadInterface(path, FileSystem.FILESYSTEM_INTERFACE_VERSION, out IntPtr module, out IntPtr fSPtr))
                    {
                        Console.WriteLine("failed loading FS");
                    }
                    if (!interfaceh.Sys_LoadInterface(path, BaseFileSystem.BASEFILESYSTEM_INTERFACE_VERSION, out module, out IntPtr baseFSPtr))
                    {
                        Console.WriteLine("failed loading BFS");
                    }

                    if (fSPtr == IntPtr.Zero || baseFSPtr == IntPtr.Zero)
                    {
                        Console.WriteLine("unloading it");
                        NativeLibrary.Free(module);
                        Console.WriteLine("unloaded ???");
                        return;
                    }

                    FileSystem fileSystem         = new(fSPtr);
                    BaseFileSystem baseFileSystem = new(baseFSPtr);

                    Console.WriteLine("get tier");

                    Console.WriteLine(fileSystem.GetTier());

                    if (interfaceh.Sys_LoadInterface("vguimatsurface", ISurface.VGUI_SURFACE_INTERFACE_VERSION, out IntPtr isurfaceModule, out IntPtr isurfaceInterface))
                    {
                        ISurface surface = new(isurfaceInterface);
                        Console.WriteLine(surface.GetTier());
                    }

                    fileSystem.PrintSearchPaths();

                    Console.WriteLine("add new path");

                    fileSystem.AddSearchPath("garrysmod", "GAME", SearchPathAdd_t.PATH_ADD_TO_HEAD);

                    fileSystem.PrintSearchPaths();

                    IntPtr fileHandle = baseFileSystem.Open("resource/GameMenu.res", "rb", "GAME");

                    if (fileHandle != IntPtr.Zero)
                    {
                        uint size = baseFileSystem.Size(fileHandle);

                        byte *buffPtr = stackalloc byte[(int)size];

                        IntPtr buffIntPtr = new(buffPtr);
                        baseFileSystem.Read(buffIntPtr, (int)size, fileHandle);
                        Console.WriteLine("Printing file contents");
                        Console.WriteLine(Encoding.UTF8.GetString(buffPtr, (int)size));
                        baseFileSystem.Close(fileHandle);
                    }
                    else
                    {
                        Console.WriteLine("not found file");
                    }
                }
            });

            assembly_context.SetCustomNativeLibraryResolver(null);

            Debug.Assert(!failed);


            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            handle = lua.PushManagedFunction(DrawSomething);
            lua.SetField(-2, "DrawSomethingPLSWORK");
            lua.Pop();
        }