wined3d: Get rid of IWineD3DTexture::Unmap().
[wine.git] / dlls / fusion / asmenum.c
blob99462ed2c5d640b79e5edc6e0b871c82099727df
1 /*
2 * IAssemblyEnum implementation
4 * Copyright 2008 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "guiddef.h"
30 #include "fusion.h"
31 #include "corerror.h"
32 #include "fusionpriv.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
40 typedef struct _tagASMNAME
42 struct list entry;
43 IAssemblyName *name;
44 } ASMNAME;
46 typedef struct
48 IAssemblyEnum IAssemblyEnum_iface;
50 struct list assemblies;
51 struct list *iter;
52 LONG ref;
53 } IAssemblyEnumImpl;
55 static inline IAssemblyEnumImpl *impl_from_IAssemblyEnum(IAssemblyEnum *iface)
57 return CONTAINING_RECORD(iface, IAssemblyEnumImpl, IAssemblyEnum_iface);
60 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
61 REFIID riid, LPVOID *ppobj)
63 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
65 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
67 *ppobj = NULL;
69 if (IsEqualIID(riid, &IID_IUnknown) ||
70 IsEqualIID(riid, &IID_IAssemblyEnum))
72 IUnknown_AddRef(iface);
73 *ppobj = This;
74 return S_OK;
77 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
78 return E_NOINTERFACE;
81 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
83 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
84 ULONG refCount = InterlockedIncrement(&This->ref);
86 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
88 return refCount;
91 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
93 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
94 ULONG refCount = InterlockedDecrement(&This->ref);
95 struct list *item, *cursor;
97 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
99 if (!refCount)
101 LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
103 ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
105 list_remove(&asmname->entry);
106 IAssemblyName_Release(asmname->name);
107 HeapFree(GetProcessHeap(), 0, asmname);
110 HeapFree(GetProcessHeap(), 0, This);
113 return refCount;
116 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
117 LPVOID pvReserved,
118 IAssemblyName **ppName,
119 DWORD dwFlags)
121 IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
122 ASMNAME *asmname;
124 TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
126 if (!ppName)
127 return E_INVALIDARG;
129 asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
130 if (!asmname)
131 return S_FALSE;
133 *ppName = asmname->name;
134 IAssemblyName_AddRef(*ppName);
136 asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
138 return S_OK;
141 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
143 IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
145 TRACE("(%p)\n", iface);
147 asmenum->iter = list_head(&asmenum->assemblies);
148 return S_OK;
151 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
152 IAssemblyEnum **ppEnum)
154 FIXME("(%p, %p) stub!\n", iface, ppEnum);
155 return E_NOTIMPL;
158 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
159 IAssemblyEnumImpl_QueryInterface,
160 IAssemblyEnumImpl_AddRef,
161 IAssemblyEnumImpl_Release,
162 IAssemblyEnumImpl_GetNextAssembly,
163 IAssemblyEnumImpl_Reset,
164 IAssemblyEnumImpl_Clone
167 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
169 WCHAR disp[MAX_PATH];
170 LPCWSTR verptr, pubkeyptr;
171 HRESULT hr;
172 DWORD size, major_size, minor_size, build_size, revision_size;
173 WORD major, minor, build, revision;
175 static const WCHAR star[] = {'*',0};
176 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
177 static const WCHAR verpubkey[] = {'%','s','\\','%','s','_','_','%','s',0};
178 static const WCHAR ver_fmt[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
180 WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
181 WCHAR token_str[TOKEN_LENGTH + 1];
182 BYTE token[BYTES_PER_TOKEN];
184 if (depth == 0)
186 size = MAX_PATH;
187 *disp = '\0';
188 hr = IAssemblyName_GetName(name, &size, disp);
189 if (SUCCEEDED(hr))
190 sprintfW(buf, ss_fmt, path, disp);
191 else
192 sprintfW(buf, ss_fmt, path, star);
194 else if (depth == 1)
196 major_size = sizeof(major);
197 IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
199 minor_size = sizeof(minor);
200 IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
202 build_size = sizeof(build);
203 IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
205 revision_size = sizeof(revision);
206 IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
208 if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
209 else
211 sprintfW(version, ver_fmt, major, minor, build, revision);
212 verptr = version;
215 size = sizeof(token);
216 IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
218 if (!size) pubkeyptr = star;
219 else
221 token_to_str(token, token_str);
222 pubkeyptr = token_str;
225 sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
229 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
231 int ret;
232 WORD version1, version2;
233 WCHAR name1[MAX_PATH], name2[MAX_PATH];
234 WCHAR token_str1[TOKEN_LENGTH + 1], token_str2[TOKEN_LENGTH + 1];
235 BYTE token1[BYTES_PER_TOKEN], token2[BYTES_PER_TOKEN];
236 DWORD size, i;
238 size = sizeof(name1);
239 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_NAME, &name1, &size);
240 size = sizeof(name2);
241 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_NAME, &name2, &size);
243 if ((ret = strcmpiW(name1, name2))) return ret;
245 for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
247 size = sizeof(version1);
248 IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
249 size = sizeof(version2);
250 IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
252 if (version1 < version2) return -1;
253 if (version1 > version2) return 1;
256 /* FIXME: compare cultures */
258 size = sizeof(token1);
259 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_PUBLIC_KEY_TOKEN, token1, &size);
260 size = sizeof(token2);
261 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_PUBLIC_KEY_TOKEN, token2, &size);
263 token_to_str(token1, token_str1);
264 token_to_str(token2, token_str2);
266 if ((ret = strcmpiW(token_str1, token_str2))) return ret;
268 return 0;
271 /* insert assembly in list preserving sort order */
272 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
274 struct list *item;
276 LIST_FOR_EACH(item, assemblies)
278 ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
280 if (compare_assembly_names(name, to_insert) > 0)
282 list_add_before(&name->entry, &to_insert->entry);
283 return;
286 list_add_tail(assemblies, &to_insert->entry);
289 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
290 int depth, LPWSTR path)
292 WIN32_FIND_DATAW ffd;
293 WCHAR buf[MAX_PATH];
294 WCHAR disp[MAX_PATH];
295 WCHAR asmpath[MAX_PATH];
296 ASMNAME *asmname;
297 HANDLE hfind;
298 LPWSTR ptr;
299 HRESULT hr = S_OK;
301 static WCHAR parent[MAX_PATH];
303 static const WCHAR dot[] = {'.',0};
304 static const WCHAR dotdot[] = {'.','.',0};
305 static const WCHAR search_fmt[] = {'%','s','\\','*',0};
306 static const WCHAR dblunder[] = {'_','_',0};
307 static const WCHAR path_fmt[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
308 static const WCHAR fmt[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
309 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
310 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
311 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
313 if (name)
314 parse_name(name, depth, path, buf);
315 else
316 sprintfW(buf, search_fmt, path);
318 hfind = FindFirstFileW(buf, &ffd);
319 if (hfind == INVALID_HANDLE_VALUE)
320 return S_OK;
324 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
325 continue;
327 if (depth == 0)
329 if (name)
330 ptr = strrchrW(buf, '\\') + 1;
331 else
332 ptr = ffd.cFileName;
334 lstrcpyW(parent, ptr);
336 else if (depth == 1)
338 sprintfW(asmpath, path_fmt, path, ffd.cFileName, parent);
340 ptr = strstrW(ffd.cFileName, dblunder);
341 *ptr = '\0';
342 ptr += 2;
344 sprintfW(disp, fmt, parent, ffd.cFileName, ptr);
346 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
347 if (!asmname)
349 hr = E_OUTOFMEMORY;
350 break;
353 hr = CreateAssemblyNameObject(&asmname->name, disp,
354 CANOF_PARSE_DISPLAY_NAME, NULL);
355 if (FAILED(hr))
357 HeapFree(GetProcessHeap(), 0, asmname);
358 break;
361 hr = IAssemblyName_SetPath(asmname->name, asmpath);
362 if (FAILED(hr))
364 IAssemblyName_Release(asmname->name);
365 HeapFree(GetProcessHeap(), 0, asmname);
366 break;
369 insert_assembly(assemblies, asmname);
370 continue;
373 sprintfW(buf, ss_fmt, path, ffd.cFileName);
374 hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
375 if (FAILED(hr))
376 break;
377 } while (FindNextFileW(hfind, &ffd) != 0);
379 FindClose(hfind);
380 return hr;
383 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
385 WCHAR path[MAX_PATH];
386 WCHAR buf[MAX_PATH];
387 HRESULT hr;
388 DWORD size;
390 static WCHAR under32[] = {'_','3','2',0};
391 static WCHAR msil[] = {'_','M','S','I','L',0};
393 size = MAX_PATH;
394 hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
395 if (FAILED(hr))
396 return hr;
398 lstrcpyW(path, buf);
399 lstrcatW(path, under32);
400 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
401 if (FAILED(hr))
402 return hr;
404 lstrcpyW(path, buf);
405 lstrcatW(path, msil);
406 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
407 if (FAILED(hr))
408 return hr;
410 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
411 if (FAILED(hr))
412 return hr;
414 return S_OK;
417 /******************************************************************
418 * CreateAssemblyEnum (FUSION.@)
420 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
421 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
423 IAssemblyEnumImpl *asmenum;
424 HRESULT hr;
426 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
427 pName, dwFlags, pvReserved);
429 if (!pEnum)
430 return E_INVALIDARG;
432 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
433 return E_INVALIDARG;
435 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
436 if (!asmenum)
437 return E_OUTOFMEMORY;
439 asmenum->IAssemblyEnum_iface.lpVtbl = &AssemblyEnumVtbl;
440 asmenum->ref = 1;
441 list_init(&asmenum->assemblies);
443 if (dwFlags & ASM_CACHE_GAC)
445 hr = enumerate_gac(asmenum, pName);
446 if (FAILED(hr))
448 HeapFree(GetProcessHeap(), 0, asmenum);
449 return hr;
453 asmenum->iter = list_head(&asmenum->assemblies);
454 *pEnum = &asmenum->IAssemblyEnum_iface;
456 return S_OK;