MCPcopy Create free account
hub / github.com/SapphireServer/Sapphire / loadModule

Method loadModule

src/world/Script/ScriptLoader.cpp:43–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43Sapphire::Scripting::ScriptInfo* Sapphire::Scripting::ScriptLoader::loadModule( const std::string& path )
44{
45 fs::path f( path );
46
47 if( isModuleLoaded( f.stem().string() ) )
48 {
49 Logger::error( "Unable to load module '{0}' as it is already loaded", f.stem().string() );
50 return nullptr;
51 }
52
53 // copy to temp dir
54 fs::path cacheDir( f.parent_path() /= m_cachePath );
55 fs::create_directories( cacheDir );
56 fs::path dest( cacheDir /= f.filename().string() );
57
58 try
59 {
60 fs::copy_file( f, dest, fs::copy_options::overwrite_existing );
61 }
62 catch( const fs::filesystem_error& err )
63 {
64 Logger::error( "Error copying file to cache: {0}", err.code().message() );
65
66 return nullptr;
67 }
68
69
70#ifdef _WIN32
71 ModuleHandle handle = LoadLibrary( dest.string().c_str() );
72#else
73 ModuleHandle handle = dlopen( dest.c_str(), RTLD_LAZY );
74#endif
75
76 if( !handle )
77 {
78 Logger::error( "Failed to load module from: {0}", path );
79
80 return nullptr;
81 }
82
83 Logger::debug( "Loaded module: {0}", f.filename().string() );
84
85 auto info = new ScriptInfo;
86 info->handle = handle;
87 info->library_name = f.stem().string();
88 info->cache_path = dest.string();
89 info->library_path = f.string();
90
91 m_scriptMap.insert( std::make_pair( f.stem().string(), info ) );
92
93 return info;
94}
95
96Sapphire::ScriptAPI::ScriptObject** Sapphire::Scripting::ScriptLoader::getScripts( ModuleHandle handle )
97{

Callers 1

loadScriptMethod · 0.80

Calls 2

insertMethod · 0.80
stringMethod · 0.45

Tested by

no test coverage detected