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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#include "GlobalFunc.h"
#include "T2.h"
#include "T2DLL.h"
#include "T2PluginLoader.h"
#include "T2PluginSpecifier.h"
T2PluginLoader::T2PluginLoader() {
}
T2PluginLoader::~T2PluginLoader() {
POSITION p = mList.GetHeadPosition();
while (p) {
T2PluginSpecifier *spec = (T2PluginSpecifier *) mList.GetNext(p);
if (spec->mIsLoaded)
UnloadPlugin(spec);
delete spec;
}
}
void T2PluginLoader::InitFileList() {
char *dirBuf = (char *) malloc(1000);
GetTowerDirectory(dirBuf);
strcat(dirBuf, "Plugins\\");
char *dirBufEnd = &dirBuf[strlen(dirBuf)];
CString databasePath = GetTowerDirectory() + "plugins\\Database";
BOOL fail = false;
CFile file;
if (file.Open(databasePath, CFile::modeRead)) {
CArchive archive(&file, CArchive::load);
DWORD countDatabase = mList.GetCount();
archive >> countDatabase;
for (unsigned int index = 0; index < countDatabase; index++) {
T2PluginSpecifier *spec = new T2PluginSpecifier;
spec->Read(archive);
mList.AddTail(spec);
}
archive.Close();
file.Close();
// check that the database is up-to-date
CFileStatus status;
int numSpec = 0;
POSITION pos = mList.GetHeadPosition();
while (pos) {
T2PluginSpecifier *spec = (T2PluginSpecifier *) mList.GetNext(pos);
if (CFile::GetStatus(spec->mPath, status)) {
if (status.m_mtime == spec->mTime)
numSpec++;
}
}
if (numSpec != mList.GetCount())
fail = true;
numSpec = 0;
strcpy(dirBufEnd, "*.t2p");
WIN32_FIND_DATA findInfo;
HANDLE handle = FindFirstFile(dirBuf, &findInfo);
BOOL find = (handle != INVALID_HANDLE_VALUE);
while (find) {
find = FindNextFile(handle, &findInfo);
numSpec++;
}
FindClose(handle);
if (numSpec != mList.GetCount())
fail = true;
} else {
fail = true;
}
if (fail) {
// update the DB
AfxSetResourceHandle(gT2App->m_hInstance);
CDialog *dialog = new CDialog;
dialog->Create(1000);
gT2App->app_vfB4();
POSITION pos = mList.GetHeadPosition();
while (pos) {
T2PluginSpecifier *spec = (T2PluginSpecifier *) mList.GetNext(pos);
delete spec;
}
mList.RemoveAll();
strcpy(dirBufEnd, "*.t2p");
WIN32_FIND_DATA findInfo;
HANDLE handle = FindFirstFile(dirBuf, &findInfo);
BOOL find = (handle != INVALID_HANDLE_VALUE);
while (find) {
strcpy(dirBufEnd, findInfo.cFileName);
T2PluginSpecifier *spec = new T2PluginSpecifier;
spec->Initialize(NULL, dirBuf);
spec->mInstance = NULL;
spec->mIsLoaded = false;
mList.AddTail(spec);
find = FindNextFile(handle, &findInfo);
}
FindClose(handle);
if (file.Open(databasePath, CFile::modeWrite | CFile::modeCreate)) {
CArchive archive(&file, CArchive::store);
DWORD count = mList.GetCount();
archive << count;
pos = mList.GetHeadPosition();
while (pos) {
T2PluginSpecifier *spec = (T2PluginSpecifier *) mList.GetNext(pos);
spec->Write(archive);
}
archive.Close();
file.Close();
}
dialog->DestroyWindow();
}
free(dirBuf);
}
typedef void *(MSVC_STDCALL* ConstructProgramPluginType) (T2PluginSpecifier *);
typedef void (MSVC_STDCALL* DestructProgramPluginType) (void);
/*static*/ void* T2PluginLoader::LoadPlugin(T2PluginSpecifier* spec) {
if (!spec->mIsLoaded) {
HMODULE module = LoadLibrary(spec->mPath);
spec->mInstance = (HINSTANCE) module;
}
ConstructProgramPluginType func = (ConstructProgramPluginType) GetProcAddress((HMODULE) spec->mInstance, "ConstructProgramPlugin");
#line 142
_ASSERT(func);
spec->mIsLoaded = true;
return func(spec);
}
/*static*/ void T2PluginLoader::UnloadPlugin(T2PluginSpecifier* spec) {
if (spec->mIsSubPlugin) {
spec->mIsLoaded = false;
return;
}
#line 154
_ASSERT(spec->mIsLoaded);
DestructProgramPluginType func = (DestructProgramPluginType) GetProcAddress((HMODULE) spec->mInstance, "DestructProgramPlugin");
#line 156
_ASSERT(func);
spec->mIsLoaded = false;
func();
FreeLibrary((HMODULE) spec->mInstance);
}
void T2PluginLoader::SetTypeFilter(POSITION& p, DWORD type) {
p = mList.GetHeadPosition();
if (type)
mTypeFilter = type;
else
mTypeFilter = 0;
}
T2PluginSpecifier* T2PluginLoader::GetNext(POSITION& p) {
while (p) {
T2PluginSpecifier *spec = (T2PluginSpecifier *) mList.GetNext(p);
if (
(mTypeFilter == 0 || spec->mType == mTypeFilter) &&
(spec->m28 == -1 || spec->m28 == 1) // 1 might be a version constant here
)
return spec;
}
return NULL;
}
T2PluginSpecifier* T2PluginLoader::FindPlugin(DWORD id, DWORD type) {
POSITION p;
T2PluginSpecifier *spec;
SetTypeFilter(p, type);
while ((spec = GetNext(p))) {
if (spec->mID == id)
return spec;
}
return NULL;
}
T2PluginSpecifier* T2PluginLoader::FindPlugin(const char* name, DWORD type) {
POSITION p;
T2PluginSpecifier *spec;
SetTypeFilter(p, type);
while ((spec = GetNext(p))) {
if (spec->mPluginName == name)
return spec;
}
return NULL;
}
|