-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvirons.Loader.cpp
460 lines (328 loc) · 13.2 KB
/
Environs.Loader.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* Environs Loader
* Loads the Environs library and creates an instance of Environs.
* ------------------------------------------------------------------
* Copyright (c) Chi-Tai Dang
*
* @author Chi-Tai Dang
* @version 1.0
* @remarks
*
* This file is part of the Environs framework developed at the
* Lab for Human Centered Multimedia of the University of Augsburg.
* http://hcm-lab.de/environs
*
* Environ is free software; you can redistribute it and/or modify
* it under the terms of the Eclipse Public License v1.0.
* A copy of the license may be obtained at:
* http://www.eclipse.org/org/documents/epl-v10.html
* --------------------------------------------------------------------
*/
#include "stdafx.h"
/// Compiler flag that enables verbose debug output
#ifndef NDEBUG
//# define DEBUGVERB
//# define DEBUGVERBVerb
#endif
#include "Environs.h"
#include "Environs.Release.h"
#include "Environs.Native.h"
#ifdef __APPLE__
#include <dlfcn.h>
#endif
#include <string>
#ifdef _WIN32
# include <direct.h>
#endif
using namespace std;
using namespace environs::lib;
#define CLASS_NAME "Environs.Loader. . . . ."
void ShowLibMissingDialog ( const char * libName );
namespace environs
{
#ifdef USE_ENVIRONS_LOG_POINTERS
pCOutLog COutLog = 0;
pCOutArgLog COutArgLog = 0;
#endif
void EnvironsDisposer ( IEnvironsDispose * obj )
{
if ( obj ) ( ( IEnvironsDispose * ) obj )->Release ();
}
namespace lib
{
/// C function implemented in the loader (statically build into each app)
/*sp ( IEnvirons ) ENVIRONS_CreateInstance ()
{
sp ( IEnvirons ) obj ( (IEnvirons *) ENVIRONS_CreateInstance1 ( ENVIRONS_BUILD_CRT ) );
return obj;
}
*/
/// C function implemented in the loader (statically build into each app)
/*void * CallConv ENVIRONS_CreateInstanceObject ()
{
return ENVIRONS_CreateInstance1 ( ENVIRONS_BUILD_CRT );
}
*/
}
namespace Loader
{
HMODULE g_EnvironsModuleHandle = 0;
/// Forward declarations
environs::Environs * LocateLoadEnvirons ( COBSTR module, int crt );
void DisposeEnvironsLib ();
class StaticDisposer
{
public:
~StaticDisposer () {
DisposeEnvironsLib ();
}
};
StaticDisposer staticDisposer;
#ifdef USE_ENVIRONS_LOG_POINTERS
void LocateLoadLogMethods ( COBSTR module, int crt, void ** outLog, void ** outLogArg );
#endif
/**
* Create an Environs object.
*
* @return An Environs object wrapped into a smart pointer.
*/
sp ( environs::Environs ) ENVIRONS_CreateInstance ()
{
sp ( environs::Environs ) obj ( LocateLoadEnvirons ( "Environs", ENVIRONS_BUILD_CRT ), EnvironsDisposer );
return obj;
}
/**
* Create an Environs object.
*
* @param appName The application name for the application environment.
* @param areaName The area name for the application environment.
*
* @return An Environs object wrapped into a smart pointer.
*/
sp ( environs::Environs ) ENVIRONS_CreateInstance ( const char * appName, const char * areaName )
{
sp ( environs::Environs ) obj ( LocateLoadEnvirons ( "Environs", ENVIRONS_BUILD_CRT ), EnvironsDisposer );
if ( obj )
obj->LoadSettings ( appName, areaName );
return obj;
}
/**
* Create an Environs object.
*
* @return An Environs object interface.
*/
environs::Environs * ENVIRONS_CreateInstanceObject ()
{
/// Load Environs.dll and create an instance
return LocateLoadEnvirons ( "Environs", ENVIRONS_BUILD_CRT );
}
/**
* Create an Environs object.
*
* @param appName The application name for the application environment.
* @param areaName The area name for the application environment.
*
* @return An Environs object interface.
*/
environs::Environs * ENVIRONS_CreateInstanceObject ( const char * appName, const char * areaName )
{
/// Load Environs.dll and create an instance
environs::Environs * env = LocateLoadEnvirons ( "Environs", ENVIRONS_BUILD_CRT );
if ( env )
env->LoadSettings ( appName, areaName );
return env;
}
#ifdef USE_ENVIRONS_LOG_POINTERS
/**
* Get log methods from Environs object.
*
*/
void ENVIRONS_GetLogMethods ( void ** outLog, void ** outLogArg )
{
LocateLoadLogMethods ( "Environs", ENVIRONS_BUILD_CRT, outLog, outLogArg );
}
#endif
bool LocateLoadLib ( COBSTR module, int crt )
{
CVerbN ( "LocateLoadLib" );
char absPath [ 1024 ];
HMODULE hModLib = g_EnvironsModuleHandle;
if ( !hModLib )
{
sprintf ( absPath, ENVLIBPREFIX "%s" LIBEXTENSION, module );
hModLib = dlopen ( module, RTLD_LAZY );
if ( !hModLib )
{
CVerbVerbN ( "LocateLoadLib: Not found in system search path." );
do
{
sprintf ( absPath, "./" ENVLIBPREFIX "%s" LIBEXTENSION, module );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
CVerbVerbN ( "LocateLoadLib: Not found in system search path [./]." );
sprintf ( absPath, "./" LIBNAME_EXT_DIR "/" ENVLIBPREFIX "%s" LIBEXTENSION, module );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
#ifdef __APPLE__
Dl_info info;
if ( dladdr ( &g_EnvironsModuleHandle, &info ) ) {
if ( info.dli_fname )
{
int length = ( int ) strlen ( info.dli_fname );
length -= 2;
while ( length > 0 ) {
if ( info.dli_fname [ length ] == '/' )
break;
length--;
}
if ( length > 0 ) {
sprintf ( absPath, "%s", info.dli_fname );
sprintf ( absPath + length + 1, "../../../" ENVLIBPREFIX "%s" LIBEXTENSION, module );
CLogArgN ( "LocateLoadLib: [%s]", absPath );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
}
}
}
CVerbVerbN ( "LocateLoadLib: Not found in root folder of app [./../]" );
sprintf ( absPath, "./../../../" ENVLIBPREFIX "%s" LIBEXTENSION, module );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
CVerbVerbN ( "LocateLoadLib: Not found in root folder of app [./../../../.]" );
#endif
#ifdef _WIN32
# ifdef TEST_DIFFERENT_CRT
# undef ENVIRONS_TSDIR
# define ENVIRONS_TSDIR TEST_DIFFERENT_CRT
# endif
sprintf ( absPath, LIBNAME_EXT_DIR "/" ENVIRONS_TSDIR "/%s" LIBEXTENSION, module );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
CVerbVerbN ( "LocateLoadLib: Not found in toolset search path [" LIBNAME_EXT_DIR "/" ENVIRONS_TSDIR "/]" );
if ( !_getcwd ( absPath, 1024 ) )
break;
int pos = ( int ) strlen ( absPath );
if ( pos <= 0 )
break;
sprintf ( absPath + pos, "/" LIBNAME_EXT_DIR "/" ENVIRONS_TSDIR "/%s" LIBEXTENSION, module );
CVerbN ( "LocateLoadLib: Trying [/" LIBNAME_EXT_DIR "/" ENVIRONS_TSDIR "/]" );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
CVerbVerbN ( "LocateLoadLib: Not found in toolset working directory path." );
#else
sprintf ( absPath, LIBNAME_EXT_DIR "/" ENVLIBPREFIX "%s" LIBEXTENSION, module );
hModLib = dlopen ( absPath, RTLD_LAZY );
if ( hModLib )
break;
#endif
if ( !hModLib ) {
#ifdef _WIN32
CLogN ( "LocateLoadLib: Not found." );
#else
CLogArgN ( "LocateLoadLib: [%s]", dlerror () );
#endif
}
}
while ( 0 );
}
}
if ( !hModLib ) {
CErrN ( "LocateLoadLib: Cannot find Environs library." );
ShowLibMissingDialog ( module );
return false;
}
g_EnvironsModuleHandle = hModLib;
return true;
}
environs::Environs * LocateLoadEnvirons ( COBSTR module, int crt )
{
CVerbN ( "LocateLoadEnvirons" );
if ( !LocateLoadLib ( module , crt ) || !g_EnvironsModuleHandle )
{
return 0;
}
ENVIRONS_PP ( ENVIRONS_CreateInstance1 ) pCreate = 0;
pCreate = ( ENVIRONS_PP ( ENVIRONS_CreateInstance1 ) )
dlsym ( g_EnvironsModuleHandle, ENVIRONS_TOSTRING ( ENVIRONS_CreateInstance1 ) );
if ( pCreate ) {
CVerbN ( "LocateLoadEnvirons: Creating Instance." );
return ( environs::Environs * ) pCreate ( ENVIRONS_BUILD_CRT );
}
CErrN ( "LocateLoadEnvirons: Cannot find " ENVIRONS_TOSTRING ( ENVIRONS_CreateInstance1 ) " in Environs library." );
return 0;
}
#ifdef _WIN32
void CommitPreDispose ( )
{
CVerbN ( "CommitPreDispose" );
if ( !g_EnvironsModuleHandle )
return;
pPreDispose PreDispose = 0;
PreDispose = ( pPreDispose )
dlsym ( g_EnvironsModuleHandle, "PreDispose" );
if ( PreDispose ) {
CVerbN ( "CommitPreDispose: Pre disposing." );
PreDispose ();
}
}
#endif
#ifdef USE_ENVIRONS_LOG_POINTERS
void LocateLoadLogMethods ( COBSTR module, int crt, void ** outLog, void ** outLogArg )
{
CVerbN ( "LocateLoadLogMethods" );
if ( !LocateLoadLib ( module , crt ) || !g_EnvironsModuleHandle )
{
return;
}
ENVIRONS_PP ( ENVIRONS_GetLogMethods1 ) pGet = 0;
pGet = ( ENVIRONS_PP ( ENVIRONS_GetLogMethods1 ) )
dlsym ( g_EnvironsModuleHandle, ENVIRONS_TOSTRING ( ENVIRONS_GetLogMethods1 ) );
if ( pGet ) {
CVerbN ( "LocateLoadLogMethods: Updating log methods." );
pGet ( outLog, outLogArg );
return;
}
CErrN ( "LocateLoadLogMethods: Cannot find " ENVIRONS_TOSTRING ( ENVIRONS_GetLogMethods1 ) " in Environs library." );
}
#endif
void DisposeEnvironsLib ()
{
if ( !g_EnvironsModuleHandle )
return;
#ifdef _WIN32
CommitPreDispose ();
#endif
dlclose ( g_EnvironsModuleHandle );
g_EnvironsModuleHandle = 0;
}
}
}
#ifdef __OBJC__
#import <Foundation/Foundation.h>
void ShowLibMissingDialog ( const char * libName )
{
NSString * msg = [[NSString alloc ] initWithFormat:@ENVLIBPREFIX "%s" LIBEXTENSION " is missing!", libName];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:msg];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
}
#else
# ifdef _WIN32
void ShowLibMissingDialog ( const char * libName )
{
MessageBoxA ( NULL, ENVLIBPREFIX "Environs" LIBEXTENSION " is missing!", NULL, NULL );
}
# else
void ShowLibMissingDialog ( const char * libName )
{
}
# endif
#endif