aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/component/win/ComponentManager.cpp
blob: ff36fa38b89ac2226333d83f8b14f85e14074b8e (plain) (blame)
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
#include "ComponentManager.h"
#include "foundation/error.h"
#include "nx/nxuri.h"

int ComponentManager::AddComponent(nx_uri_t filename)
{
	if (phase > PHASE_LOADED)
		return NErr_Error;

	HMODULE hLib = LoadLibraryW(filename->string);
	if (hLib)
	{
		GETCOMPONENT_FUNC pr = (GETCOMPONENT_FUNC)GetProcAddress(hLib, "GetWasabi2Component");
		if (pr)
		{
			ifc_component *component = pr();
			if (component)
			{
				if (component->component_info.wasabi_version != wasabi2_component_version
					|| component->component_info.nx_api_version != nx_api_version
					|| component->component_info.nx_platform_guid != nx_platform_guid)
				{
					FreeLibrary(hLib);
					return NErr_IncompatibleVersion;
				}

				component->component_info.hModule = hLib;
				component->component_info.filename = NXURIRetain(filename);
				int ret = component->Initialize(service_api);
				if (ret != NErr_Success)
				{
					NXURIRelease(component->component_info.filename);
					FreeLibrary(hLib);
					return ret;
				}

				/* if the component was added late, we'll need to run some extra stages */
				ret = LateLoad(component);
				if (ret != NErr_Success)
				{
					NXURIRelease(component->component_info.filename);
					FreeLibrary(hLib);
					return ret;
				}

				components.push_back(component);
				return NErr_Success;
			}
		}
		return NErr_Error;
	}
	else
	{
		return NErr_FileNotFound;
	}
}

int ComponentManager::AddDirectory(nx_uri_t directory)
{
	WIN32_FIND_DATAW find_data = {0};

	nx_uri_t directory_mask;
	int ret = NXURICreateFromPath(&directory_mask, L"*.w6c", directory);
	if (ret != NErr_Success)
		return ret;

	HANDLE find_handle = FindFirstFileW(directory_mask->string, &find_data);
	if (find_handle != INVALID_HANDLE_VALUE)
	{
		do
		{			
			nx_uri_t w6c_filename;
			if (NXURICreateFromPath(&w6c_filename, find_data.cFileName, directory) == NErr_Success)
			{
				AddComponent(w6c_filename);
				NXURIRelease(w6c_filename);
			}
		}
		while (FindNextFileW(find_handle,&find_data));
		FindClose(find_handle);
	}
	return NErr_Success;
}

void ComponentManager::CloseComponent(ifc_component *component)
{
	FreeLibrary(component->component_info.hModule);
}