ArkScript
A small, fast, functional and scripting language for video games
Plugin.hpp
Go to the documentation of this file.
1/**
2 * @file Plugin.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Loads .dll/.so/.dynlib files
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef ARK_VM_PLUGIN_HPP
12#define ARK_VM_PLUGIN_HPP
13
14#include <Ark/Platform.hpp>
15
16#if defined(ARK_OS_WINDOWS)
17# include <Proxy/MiniWindows.h>
18#elif defined(ARK_OS_LINUX)
19# include <dlfcn.h>
20#else
21# error "Can not identify the platform on which you are running, aborting"
22#endif
23
24#include <string>
25#include <system_error>
26
27#include <fmt/core.h>
28
29namespace Ark::internal
30{
31 /**
32 * @brief Handling a shared library as an ArkScript plugin
33 *
34 */
36 {
37 public:
38 /**
39 * @brief Construct a new Shared Library object
40 *
41 */
43
44 /**
45 * @brief Disable copy semantics as this contains a pointer.
46 *
47 */
48 SharedLibrary(const SharedLibrary&) = delete;
49
50 /**
51 * @brief Disable copy semantics as this contains a pointer.
52 *
53 */
55
56 /**
57 * @brief Construct a new Shared Library object
58 *
59 * @param path path to the shared library
60 */
61 explicit SharedLibrary(std::string path);
62
63 /**
64 * @brief Destroy the Shared Library object
65 *
66 */
68
69 /**
70 * @brief Load a shared library
71 *
72 * @param path path to the shared library
73 */
74 void load(const std::string& path);
75
76 /**
77 * @brief Unload the shared library
78 *
79 */
80 void unload() const;
81
82 [[nodiscard]] const std::string& path() const { return m_path; }
83
84 /**
85 * @brief Return a function from the shared library
86 *
87 * @tparam T the type of the function to retrieve
88 * @param procname the name of the function to retrieve
89 * @return T the function from the shared library, if it was found
90 */
91 template <typename T>
92 T get(const std::string& procname)
93 {
94 T funcptr;
95
96#if defined(ARK_OS_WINDOWS)
97 if (NULL == (funcptr = reinterpret_cast<T>(GetProcAddress(m_instance, procname.c_str()))))
98 {
99 throw std::system_error(
100 std::error_code(::GetLastError(), std::system_category()), std::string("PluginError: Couldn't find ") + procname);
101 }
102#elif defined(ARK_OS_LINUX)
103 if (NULL == (funcptr = reinterpret_cast<T>(dlsym(m_instance, procname.c_str()))))
104 {
105 throw std::system_error(
106 std::error_code(errno, std::system_category()), fmt::format("PluginError: Couldn't find {}, {}", procname, dlerror()));
107 }
108#endif
109 return funcptr;
110 }
111
112 private:
113#if defined(ARK_OS_WINDOWS)
114 HINSTANCE m_instance;
115#elif defined(ARK_OS_LINUX)
116 void* m_instance;
117#endif
118 std::string m_path;
120 };
121}
122
123#endif
ArkScript configuration macros.
Handling a shared library as an ArkScript plugin.
Definition Plugin.hpp:36
~SharedLibrary()
Destroy the Shared Library object.
Definition Plugin.cpp:21
SharedLibrary()
Construct a new Shared Library object.
Definition Plugin.cpp:8
const std::string & path() const
Definition Plugin.hpp:82
SharedLibrary(const SharedLibrary &)=delete
Disable copy semantics as this contains a pointer.
void load(const std::string &path)
Load a shared library.
Definition Plugin.cpp:26
SharedLibrary & operator=(const SharedLibrary &)=delete
Disable copy semantics as this contains a pointer.
void unload() const
Unload the shared library.
Definition Plugin.cpp:49
T get(const std::string &procname)
Return a function from the shared library.
Definition Plugin.hpp:92