1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
Mod Organizer archive handling
Copyright (C) 2020 MO2 Team. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ARCHIVE_LIBRARY_H
#define ARCHIVE_LIBRARY_H
#ifdef _WIN32
#include <Windows.h>
#else
#include <Common/MyWindows.h>
#include <cstdlib>
#include <dlfcn.h>
#include <string>
#include <unistd.h>
#include <vector>
#endif
/**
* Very small wrapper around shared libraries (DLL on Windows, .so on Linux).
*/
class ALibrary
{
public:
#ifdef _WIN32
ALibrary(const char* path) : m_Module{nullptr}, m_LastError{ERROR_SUCCESS}
{
m_Module = LoadLibraryA(path);
if (m_Module == nullptr) {
updateLastError();
}
}
~ALibrary()
{
if (m_Module) {
FreeLibrary(m_Module);
}
}
template <class T>
T resolve(const char* procName)
{
if (!m_Module) {
return nullptr;
}
auto proc = GetProcAddress(m_Module, procName);
if (!proc) {
updateLastError();
return nullptr;
}
return reinterpret_cast<T>(proc);
}
DWORD getLastError() const { return m_LastError; }
bool isOpen() const { return m_Module != nullptr; }
operator bool() const { return isOpen(); }
private:
void updateLastError() { m_LastError = ::GetLastError(); }
HMODULE m_Module;
DWORD m_LastError;
#else // Linux
ALibrary(const char* path) : m_Handle{nullptr}, m_LastError{0}
{
// Find the directory containing our own executable
std::string exeDir;
char selfPath[4096];
ssize_t len = readlink("/proc/self/exe", selfPath, sizeof(selfPath) - 1);
if (len > 0) {
selfPath[len] = '\0';
exeDir = std::string(selfPath);
auto slash = exeDir.rfind('/');
if (slash != std::string::npos)
exeDir = exeDir.substr(0, slash);
}
// Honor MO2_LIBS_DIR (set by the fluorine-manager launcher) first.
std::string envLibs;
const char* envVal = std::getenv("MO2_LIBS_DIR");
if (envVal && envVal[0] != '\0') {
envLibs = envVal;
}
// Try bundled 7z.so locations first (env override, exe dir, lib/ subdir)
std::vector<std::string> tryPaths;
if (!envLibs.empty()) {
tryPaths.push_back(envLibs + "/7z.so");
}
tryPaths.push_back(exeDir + "/7z.so");
tryPaths.push_back(exeDir + "/lib/7z.so");
tryPaths.push_back("7z.so");
tryPaths.push_back("lib/7z.so");
tryPaths.push_back("/usr/lib/p7zip/7z.so");
tryPaths.push_back("/usr/lib64/p7zip/7z.so");
tryPaths.push_back("/usr/libexec/p7zip/7z.so");
for (const auto& tryPath : tryPaths) {
m_Handle = dlopen(tryPath.c_str(), RTLD_LAZY);
if (m_Handle) break;
}
if (!m_Handle) {
// Last resort: try the original path as-is
m_Handle = dlopen(path, RTLD_LAZY);
}
if (!m_Handle) {
m_LastError = 1;
}
}
~ALibrary()
{
if (m_Handle) {
dlclose(m_Handle);
}
}
template <class T>
T resolve(const char* procName)
{
if (!m_Handle) {
return nullptr;
}
void* sym = dlsym(m_Handle, procName);
if (!sym) {
m_LastError = 1;
return nullptr;
}
return reinterpret_cast<T>(sym);
}
DWORD getLastError() const { return m_LastError; }
bool isOpen() const { return m_Handle != nullptr; }
operator bool() const { return isOpen(); }
private:
void* m_Handle;
DWORD m_LastError;
#endif
};
#endif
|