ArkScript
A small, fast, functional and scripting language for video games
Plugin.cpp
Go to the documentation of this file.
1#include <Ark/VM/Plugin.hpp>
2
3#include <sstream>
4#include <iostream>
5
6namespace Ark::internal
7{
9 m_instance(nullptr),
10 m_loaded(false)
11 {}
12
13 SharedLibrary::SharedLibrary(std::string path) :
14 m_instance(nullptr),
15 m_path(std::move(path)),
16 m_loaded(false)
17 {
18 load(m_path);
19 }
20
25
26 void SharedLibrary::load(const std::string& path)
27 {
28 if (m_loaded)
29 unload();
30
31 m_path = path;
32
33#if defined(ARK_OS_WINDOWS)
34 if (NULL == (m_instance = LoadLibrary(m_path.c_str())))
35 {
36 throw std::system_error(
37 std::error_code(::GetLastError(), std::system_category()), "Couldn't load the library at " + path);
38 }
39#elif defined(ARK_OS_LINUX)
40 if (nullptr == (m_instance = dlopen(m_path.c_str(), RTLD_LAZY | RTLD_GLOBAL)))
41 {
42 throw std::system_error(
43 std::error_code(errno, std::system_category()), fmt::format("Couldn't load the library at {}, {}", path, dlerror()));
44 }
45#endif
46 m_loaded = true;
47 }
48
50 {
51 if (m_loaded)
52 {
53#if defined(ARK_OS_WINDOWS)
54 FreeLibrary(m_instance);
55#elif defined(ARK_OS_LINUX)
56 dlclose(m_instance);
57#endif
58 }
59 }
60
61}
Loads .dll/.so/.dynlib files.
~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:86
void load(const std::string &path)
Load a shared library.
Definition Plugin.cpp:26
void unload() const
Unload the shared library.
Definition Plugin.cpp:49