• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "meta_object_initializer.h"
17 
18 #include <cstdint>
19 #include <windows.h>
20 
21 #include <base/containers/string.h>
22 #include <base/util/uid.h>
23 #include <core/intf_engine.h>
24 #include <core/os/platform_create_info.h>
25 #include <core/plugin/intf_plugin.h>
26 #include <core/plugin/intf_plugin_register.h>
27 
28 #include <meta/base/plugin.h>
29 
30 namespace {
31 uint32_t gInitCount = 0;
32 }
33 
34 #if CORE_DYNAMIC == 1
35 
36 namespace {
37 HMODULE agpEngineHandle = nullptr;
38 }
39 
40 CORE_NS::IPluginRegister& (*CORE_NS::GetPluginRegister)() { nullptr };
41 void (*CORE_NS::CreatePluginRegistry)(const struct CORE_NS::PlatformCreateInfo& platformCreateInfo) { nullptr };
42 
43 #endif
44 
45 namespace environmentSetup {
46 
InitializeMetaObject(const char * const dllPath)47 void InitializeMetaObject(const char* const dllPath)
48 {
49     InitializeMetaObject(dllPath, CORE_NS::ILogger::LogLevel::LOG_VERBOSE);
50 }
51 
InitializeMetaObject(const char * const dllPath,CORE_NS::ILogger::LogLevel logLevel)52 void InitializeMetaObject(const char* const dllPath, CORE_NS::ILogger::LogLevel logLevel)
53 {
54     if (gInitCount == 0) {
55         char MyFileName[MAX_PATH + 1];
56 #ifdef _DEBUG
57         _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
58 #endif
59 #if CORE_DYNAMIC == 1
60         agpEngineHandle = LoadLibrary(dllPath);
61         CORE_NS::GetPluginRegister =
62             decltype(CORE_NS::GetPluginRegister)(GetProcAddress(agpEngineHandle, MAKEINTRESOURCE(2))); // 2: parm
63         CORE_NS::CreatePluginRegistry =
64             decltype(CORE_NS::CreatePluginRegistry)(GetProcAddress(agpEngineHandle, MAKEINTRESOURCE(1)));
65 
66         // Get the path to the engine.dll. to use as base for plugins.
67         GetModuleFileNameA(agpEngineHandle, MyFileName, sizeof(MyFileName));
68 #else
69         // Get the path to the executable. to use as base for plugins.
70         GetModuleFileNameA(GetModuleHandleA(NULL), MyFileName, sizeof(MyFileName));
71 #endif
72         CORE_NS::GetLogger()->SetLogLevel(logLevel);
73 
74         BASE_NS::string path = MyFileName;
75         auto pos = path.find_last_of("\\");
76         path.resize(pos + 1);
77 
78         CORE_NS::CreatePluginRegistry(
79             CORE_NS::PlatformCreateInfo { path.c_str(), "", (path + "plugins\\").c_str() }); // load plugins etc..*/
80 
81         const BASE_NS::Uid lst[] { META_NS::META_OBJECT_PLUGIN_UID };
82         CORE_NS::GetPluginRegister().LoadPlugins(lst); // Load all plugins
83     }
84     gInitCount++;
85 }
86 
ShutdownMetaObject()87 void ShutdownMetaObject()
88 {
89     if (gInitCount > 0) {
90         gInitCount--;
91         if (gInitCount == 0) {
92             CORE_NS::GetPluginRegister().UnloadPlugins({}); // Unload all plugins
93 #if CORE_DYNAMIC == 1
94             if (agpEngineHandle) {
95                 FreeLibrary(agpEngineHandle);
96             }
97 #endif
98         }
99     }
100 }
101 
102 } // namespace environmentSetup
103