d3dx9: Implemented ConvertPointRepsToAdjacency.
[wine/multimedia.git] / dlls / gameux / gamestatistics.c
blob87df88645cc804b3aef412774aef80d339eea675
1 /*
2 * Gameux library coclass GameStatistics implementation
4 * Copyright (C) 2010 Mariusz PluciƄski
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
20 #define COBJMACROS
22 #include "config.h"
24 #include "ole2.h"
25 #include "winreg.h"
26 #include "msxml2.h"
27 #include "shlwapi.h"
28 #include "shlobj.h"
30 #include "gameux.h"
31 #include "gameux_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(gameux);
38 * constant definitions
40 #define MAX_CATEGORY_LENGTH 60
41 #define MAX_NAME_LENGTH 30
42 #define MAX_VALUE_LENGTH 30
43 #define MAX_CATEGORIES 10
44 #define MAX_STATS_PER_CATEGORY 10
45 /*******************************************************************************
46 * Game statistics helper components
48 /*******************************************************************************
49 * struct GAMEUX_STATS
51 * set of structures for containing game's data
53 struct GAMEUX_STATS_STAT
55 WCHAR sName[MAX_NAME_LENGTH+1];
56 WCHAR sValue[MAX_VALUE_LENGTH+1];
58 struct GAMEUX_STATS_CATEGORY
60 WCHAR sName[MAX_CATEGORY_LENGTH+1];
61 struct GAMEUX_STATS_STAT stats[MAX_STATS_PER_CATEGORY];
63 struct GAMEUX_STATS
65 WCHAR sStatsFile[MAX_PATH];
66 struct GAMEUX_STATS_CATEGORY categories[MAX_CATEGORIES];
68 /*******************************************************************************
69 * GAMEUX_createStatsDirectory
71 * Helper function, creates directory to store game statistics
73 * Parameters
74 * path [I] path to game statistics file.
75 * base directory of this file will
76 * be created if it doesn't exists
78 static HRESULT GAMEUX_createStatsDirectory(LPCWSTR lpFilePath)
80 HRESULT hr;
81 WCHAR lpDirectoryPath[MAX_PATH];
82 LPCWSTR lpEnd;
84 lpEnd = StrRChrW(lpFilePath, NULL, '\\');
85 lstrcpynW(lpDirectoryPath, lpFilePath, lpEnd-lpFilePath+1);
87 hr = HRESULT_FROM_WIN32(SHCreateDirectoryExW(NULL, lpDirectoryPath, NULL));
89 if(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) ||
90 hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
91 hr = S_FALSE;
93 return hr;
95 /*******************************************************************
96 * GAMEUX_updateStatisticsFile
98 * Helper function updating data stored in statistics file
100 * Parameters:
101 * data [I] pointer to struct containing
102 * statistics data
104 static HRESULT GAMEUX_updateStatisticsFile(struct GAMEUX_STATS *stats)
106 static const WCHAR sStatistics[] = {'S','t','a','t','i','s','t','i','c','s',0};
107 static const WCHAR sCategory[] = {'C','a','t','e','g','o','r','y',0};
108 static const WCHAR sIndex[] = {'I','n','d','e','x',0};
109 static const WCHAR sStatistic[] = {'S','t','a','t','i','s','t','i','c',0};
110 static const WCHAR sName[] = {'N','a','m','e',0};
111 static const WCHAR sValue[] = {'V','a','l','u','e',0};
113 HRESULT hr = S_OK;
114 IXMLDOMDocument *document;
115 IXMLDOMElement *root, *categoryElement, *statisticsElement;
116 IXMLDOMNode *categoryNode, *statisticsNode;
117 VARIANT vStatsFilePath, vValue;
118 BSTR bstrStatistics = NULL, bstrCategory = NULL, bstrIndex = NULL,
119 bstrStatistic = NULL, bstrName = NULL, bstrValue = NULL;
120 int i, j;
122 TRACE("(%p)\n", stats);
124 V_VT(&vStatsFilePath) = VT_BSTR;
125 V_BSTR(&vStatsFilePath) = SysAllocString(stats->sStatsFile);
126 if(!V_BSTR(&vStatsFilePath))
127 hr = E_OUTOFMEMORY;
129 if(SUCCEEDED(hr))
130 hr = CoCreateInstance(&CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
131 &IID_IXMLDOMDocument, (void**)&document);
133 if(SUCCEEDED(hr))
135 bstrStatistics = SysAllocString(sStatistics);
136 if(!bstrStatistics)
137 hr = E_OUTOFMEMORY;
140 if(SUCCEEDED(hr))
141 hr = IXMLDOMDocument_createElement(document, bstrStatistics, &root);
143 if(SUCCEEDED(hr))
145 bstrCategory = SysAllocString(sCategory);
146 if(!bstrCategory)
147 hr = E_OUTOFMEMORY;
150 if(SUCCEEDED(hr))
152 bstrIndex = SysAllocString(sIndex);
153 if(!bstrIndex)
154 hr = E_OUTOFMEMORY;
157 if(SUCCEEDED(hr))
159 bstrStatistic = SysAllocString(sStatistic);
160 if(!bstrStatistic)
161 hr = E_OUTOFMEMORY;
164 if(SUCCEEDED(hr))
166 bstrName = SysAllocString(sName);
167 if(!bstrName)
168 hr = E_OUTOFMEMORY;
171 if(SUCCEEDED(hr))
173 bstrValue = SysAllocString(sValue);
174 if(!bstrValue)
175 hr = E_OUTOFMEMORY;
178 if(SUCCEEDED(hr))
180 if(SUCCEEDED(hr))
181 for(i=0; i<MAX_CATEGORIES; ++i)
183 if(lstrlenW(stats->categories[i].sName)==0)
184 continue;
186 V_VT(&vValue) = VT_INT;
187 V_INT(&vValue) = NODE_ELEMENT;
189 hr = IXMLDOMDocument_createNode(document, vValue, bstrCategory, NULL, &categoryNode);
191 if(SUCCEEDED(hr))
192 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
194 V_INT(&vValue) = i;
195 if(SUCCEEDED(hr))
196 hr = IXMLDOMElement_setAttribute(categoryElement, bstrIndex, vValue);
198 if(SUCCEEDED(hr))
200 V_VT(&vValue) = VT_BSTR;
201 V_BSTR(&vValue) = SysAllocString(stats->categories[i].sName);
202 if(!V_BSTR(&vValue))
203 hr = E_OUTOFMEMORY;
206 if(SUCCEEDED(hr))
208 TRACE("storing category %d: %s\n", i, debugstr_w(V_BSTR(&vValue)));
209 hr = IXMLDOMElement_setAttribute(categoryElement, bstrName, vValue);
212 SysFreeString(V_BSTR(&vValue));
214 if(SUCCEEDED(hr))
216 for(j=0; j<MAX_STATS_PER_CATEGORY; ++j)
218 if(lstrlenW(stats->categories[i].stats[j].sName)==0)
219 continue;
221 V_VT(&vValue) = VT_INT;
222 V_INT(&vValue) = NODE_ELEMENT;
224 hr = IXMLDOMDocument_createNode(document, vValue, bstrStatistic, NULL, &statisticsNode);
226 if(SUCCEEDED(hr))
227 hr = IXMLDOMNode_QueryInterface(statisticsNode, &IID_IXMLDOMElement, (LPVOID*)&statisticsElement);
229 V_INT(&vValue) = j;
230 if(SUCCEEDED(hr))
231 hr = IXMLDOMElement_setAttribute(statisticsElement, bstrIndex, vValue);
233 if(SUCCEEDED(hr))
235 V_VT(&vValue) = VT_BSTR;
236 V_BSTR(&vValue) = SysAllocString(stats->categories[i].stats[j].sName);
237 if(!V_BSTR(&vValue))
238 hr = E_OUTOFMEMORY;
241 if(SUCCEEDED(hr))
243 TRACE(" storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
244 hr = IXMLDOMElement_setAttribute(statisticsElement, bstrName, vValue);
247 SysFreeString(V_BSTR(&vValue));
249 if(SUCCEEDED(hr))
251 V_VT(&vValue) = VT_BSTR;
252 V_BSTR(&vValue) = SysAllocString(stats->categories[i].stats[j].sValue);
253 if(!V_BSTR(&vValue))
254 hr = E_OUTOFMEMORY;
257 if(SUCCEEDED(hr))
259 TRACE(" storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
260 hr = IXMLDOMElement_setAttribute(statisticsElement, bstrValue, vValue);
263 SysFreeString(V_BSTR(&vValue));
265 if(SUCCEEDED(hr))
266 hr = IXMLDOMElement_appendChild(categoryNode, statisticsNode, &statisticsNode);
268 IXMLDOMElement_Release(statisticsElement);
269 IXMLDOMNode_Release(statisticsNode);
273 if(SUCCEEDED(hr))
274 hr = IXMLDOMElement_appendChild(root, categoryNode, &categoryNode);
276 IXMLDOMElement_Release(categoryElement);
277 IXMLDOMNode_Release(categoryNode);
279 if(FAILED(hr))
280 break;
283 if(SUCCEEDED(hr))
284 hr = IXMLDOMDocument_putref_documentElement(document, root);
286 IXMLDOMElement_Release(root);
288 TRACE("saving game statistics in %s file\n", debugstr_w(stats->sStatsFile));
289 if(SUCCEEDED(hr))
290 hr = GAMEUX_createStatsDirectory(stats->sStatsFile);
292 if(SUCCEEDED(hr))
293 hr = IXMLDOMDocument_save(document, vStatsFilePath);
295 IXMLDOMDocument_Release(document);
297 SysFreeString(bstrValue);
298 SysFreeString(bstrName);
299 SysFreeString(bstrStatistic);
300 SysFreeString(bstrIndex);
301 SysFreeString(bstrCategory);
302 SysFreeString(bstrStatistics);
303 SysFreeString(V_BSTR(&vStatsFilePath));
304 TRACE("ret=0x%x\n", hr);
305 return hr;
307 /*******************************************************************************
308 * GAMEUX_buildStatisticsFilePath
309 * Creates path to file contaning statistics of game with given id.
311 * Parameters:
312 * lpApplicationId [I] application id of game,
313 * as string
314 * lpStatisticsFile [O] array where path will be
315 * stored. It's size must be
316 * at least MAX_PATH
318 static HRESULT GAMEUX_buildStatisticsFilePath(
319 LPCWSTR lpApplicationId,
320 LPWSTR lpStatisticsFile)
322 static const WCHAR sBackslash[] = {'\\',0};
323 static const WCHAR sStatisticsDir[] = {'\\','M','i','c','r','o','s','o','f','t',
324 '\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p',
325 'l','o','r','e','r','\\','G','a','m','e','S','t','a','t','i','s',
326 't','i','c','s','\\',0};
327 static const WCHAR sDotGamestats[] = {'.','g','a','m','e','s','t','a','t','s',0};
329 HRESULT hr;
331 hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, lpStatisticsFile);
333 if(SUCCEEDED(hr))
335 lstrcatW(lpStatisticsFile, sStatisticsDir);
336 lstrcatW(lpStatisticsFile, lpApplicationId);
337 lstrcatW(lpStatisticsFile, sBackslash);
338 lstrcatW(lpStatisticsFile, lpApplicationId);
339 lstrcatW(lpStatisticsFile, sDotGamestats);
342 return hr;
344 /*******************************************************************************
345 * GAMEUX_getAppIdFromGDFPath
347 * Loads application identifier associated with given GDF binary.
348 * Routine reads identifier from registry, so will fail if game
349 * is not registered.
351 * Parameters:
352 * GDFBinaryPath [I] path to gdf binary
353 * lpApplicationId [O] place to store application id.
354 * must be at least 49 characters
355 * to store guid and termination 0
357 static HRESULT GAMEUX_getAppIdFromGDFPath(
358 LPCWSTR GDFBinaryPath,
359 LPWSTR lpApplicationId)
361 static const WCHAR sApplicationId[] =
362 {'A','p','p','l','i','c','a','t','i','o','n','I','d',0};
364 HRESULT hr;
365 GAME_INSTALL_SCOPE installScope;
366 GUID instanceId;
367 LPWSTR lpRegistryPath = NULL;
368 HKEY hKey;
369 DWORD dwLength = 49*sizeof(WCHAR);/* place for GUID */
371 TRACE("(%s, %p)\n", debugstr_w(GDFBinaryPath), lpApplicationId);
373 if(!GDFBinaryPath)
374 return E_INVALIDARG;
376 installScope = GIS_CURRENT_USER;
377 hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
379 if(hr == S_FALSE)
381 installScope = GIS_ALL_USERS;
382 hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
385 if(hr == S_FALSE)
386 /* game not registered, so statistics cannot be used */
387 hr = E_FAIL;
389 if(SUCCEEDED(hr))
390 /* game is registered, let's read it's application id from registry */
391 hr = GAMEUX_buildGameRegistryPath(installScope, &instanceId, &lpRegistryPath);
393 if(SUCCEEDED(hr))
394 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE,
395 lpRegistryPath, 0, KEY_READ | KEY_WOW64_64KEY, &hKey));
397 if(SUCCEEDED(hr))
398 hr = HRESULT_FROM_WIN32(RegGetValueW(hKey,
399 NULL, sApplicationId, RRF_RT_REG_SZ,
400 NULL, lpApplicationId, &dwLength));
402 HeapFree(GetProcessHeap(), 0, lpRegistryPath);
403 RegCloseKey(hKey);
405 TRACE("found app id: %s, return: %#x\n", debugstr_w(lpApplicationId), hr);
406 return hr;
408 /*******************************************************************
409 * GAMEUX_loadGameStatisticsFromFile
410 * Helper function, loads game statistics from file and stores them
411 * in the structure.
413 * Parameters:
414 * data [I/O] structure containing file name to
415 * load and data fields to store data in
417 static HRESULT GAMEUX_loadStatisticsFromFile(struct GAMEUX_STATS *data)
419 static const WCHAR sStatistics[] = {'S','t','a','t','i','s','t','i','c','s',0};
420 static const WCHAR sCategory[] = {'C','a','t','e','g','o','r','y',0};
421 static const WCHAR sIndex[] = {'I','n','d','e','x',0};
422 static const WCHAR sStatistic[] = {'S','t','a','t','i','s','t','i','c',0};
423 static const WCHAR sName[] = {'N','a','m','e',0};
424 static const WCHAR sValue[] = {'V','a','l','u','e',0};
426 HRESULT hr = S_OK;
427 IXMLDOMDocument *document = NULL;
428 IXMLDOMElement *root = NULL, *categoryElement, *statisticElement;
429 IXMLDOMNode *categoryNode, *statisticNode;
430 IXMLDOMNodeList *rootChildren = NULL, *categoryChildren;
431 VARIANT vStatsFilePath, vValue;
432 BSTR bstrStatistics = NULL, bstrCategory = NULL, bstrIndex = NULL,
433 bstrStatistic = NULL, bstrName = NULL, bstrValue = NULL;
434 VARIANT_BOOL isSuccessful = VARIANT_FALSE;
435 int i, j;
437 TRACE("(%p)\n", data);
439 V_VT(&vStatsFilePath) = VT_BSTR;
440 V_BSTR(&vStatsFilePath) = SysAllocString(data->sStatsFile);
441 if(!V_BSTR(&vStatsFilePath))
442 hr = E_OUTOFMEMORY;
444 if(SUCCEEDED(hr))
445 hr = CoCreateInstance(&CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&document);
447 if(SUCCEEDED(hr))
449 bstrStatistics = SysAllocString(sStatistics);
450 if(!bstrStatistics)
451 hr = E_OUTOFMEMORY;
454 if(SUCCEEDED(hr))
456 bstrCategory = SysAllocString(sCategory);
457 if(!bstrCategory)
458 hr = E_OUTOFMEMORY;
461 if(SUCCEEDED(hr))
463 bstrIndex = SysAllocString(sIndex);
464 if(!bstrIndex)
465 hr = E_OUTOFMEMORY;
468 if(SUCCEEDED(hr))
470 bstrStatistic = SysAllocString(sStatistic);
471 if(!bstrStatistic)
472 hr = E_OUTOFMEMORY;
475 if(SUCCEEDED(hr))
477 bstrName = SysAllocString(sName);
478 if(!bstrName)
479 hr = E_OUTOFMEMORY;
482 if(SUCCEEDED(hr))
484 bstrValue = SysAllocString(sValue);
485 if(!bstrValue)
486 hr = E_OUTOFMEMORY;
489 if(SUCCEEDED(hr))
490 hr = IXMLDOMDocument_load(document, vStatsFilePath, &isSuccessful);
492 if(hr == S_OK && isSuccessful != VARIANT_TRUE)
493 hr = S_FALSE;
495 if( hr == S_OK )
496 hr = IXMLDOMDocument_get_documentElement(document, &root);
498 if(hr == S_OK)
499 hr = IXMLDOMElement_get_childNodes(root, &rootChildren);
501 if(hr == S_OK)
503 hr = S_OK;
504 while(hr == S_OK)
506 hr = IXMLDOMNodeList_nextNode(rootChildren, &categoryNode);
508 if(hr == S_OK)
510 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
512 if(SUCCEEDED(hr))
514 hr = IXMLDOMElement_getAttribute(categoryElement, bstrIndex, &vValue);
515 if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
516 hr = E_FAIL;
518 if(SUCCEEDED(hr))
520 i = StrToIntW(V_BSTR(&vValue));
521 hr = IXMLDOMElement_getAttribute(categoryElement, bstrName, &vValue);
522 if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
523 hr = E_FAIL;
526 if(SUCCEEDED(hr))
528 lstrcpynW(data->categories[i].sName, V_BSTR(&vValue), MAX_CATEGORY_LENGTH);
529 TRACE("category %d name %s\n", i, debugstr_w(data->categories[i].sName));
530 hr = IXMLDOMElement_get_childNodes(categoryElement, &categoryChildren);
533 if(SUCCEEDED(hr))
535 hr = S_OK;
536 while(hr == S_OK)
538 hr = IXMLDOMNodeList_nextNode(categoryChildren, &statisticNode);
540 if(hr == S_OK)
542 hr = IXMLDOMNode_QueryInterface(statisticNode, &IID_IXMLDOMElement, (LPVOID*)&statisticElement);
544 if(SUCCEEDED(hr))
546 hr = IXMLDOMElement_getAttribute(statisticElement, bstrIndex, &vValue);
547 if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
548 hr = E_FAIL;
550 if(SUCCEEDED(hr))
552 j = StrToIntW(V_BSTR(&vValue));
553 hr = IXMLDOMElement_getAttribute(statisticElement, bstrName, &vValue);
554 if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
555 hr = E_FAIL;
558 if(SUCCEEDED(hr))
560 lstrcpynW(data->categories[i].stats[j].sName, V_BSTR(&vValue), MAX_NAME_LENGTH);
561 hr = IXMLDOMElement_getAttribute(statisticElement, bstrValue, &vValue);
562 if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
563 hr = E_FAIL;
566 if(SUCCEEDED(hr))
568 lstrcpynW(data->categories[i].stats[j].sValue, V_BSTR(&vValue), MAX_VALUE_LENGTH);
569 TRACE("statistic %d name %s value %s\n", j,
570 debugstr_w(data->categories[i].stats[j].sName),
571 debugstr_w(data->categories[i].stats[j].sValue));
573 IXMLDOMElement_Release(statisticElement);
576 IXMLDOMNode_Release(statisticNode);
580 if(SUCCEEDED(hr))
581 hr = S_OK;
583 IXMLDOMElement_Release(categoryElement);
586 IXMLDOMNode_Release(categoryNode);
589 if(SUCCEEDED(hr))
590 hr = S_OK;
593 if(rootChildren) IXMLDOMNodeList_Release(rootChildren);
594 if(root) IXMLDOMElement_Release(root);
595 if(document) IXMLDOMDocument_Release(document);
597 SysFreeString(bstrValue);
598 SysFreeString(bstrName);
599 SysFreeString(bstrStatistic);
600 SysFreeString(bstrIndex);
601 SysFreeString(bstrCategory);
602 SysFreeString(bstrStatistics);
603 SysFreeString(V_BSTR(&vStatsFilePath));
604 return hr;
606 /*******************************************************************
607 * GAMEUX_loadGameStatistics
609 * Helper function which loads game statistics associated with game
610 * into interface's internal structures
612 * Parameters:
613 * pStats [O] structure which will receive data
614 * sGameId [I] application instance Id, stored as string
615 * to avoid additional conversions
616 * openType [I] allowed ways of opening statistics
617 * pOpenResult [O] way used to open statistics
620 static HRESULT GAMEUX_loadGameStatistics(struct GAMEUX_STATS *pStats,
621 LPWSTR sGameId,
622 GAMESTATS_OPEN_TYPE openType,
623 GAMESTATS_OPEN_RESULT* pOpenResult)
625 HRESULT hr;
626 TRACE("(%p, %s, %d, %p)\n", pStats, debugstr_w(sGameId), openType, pOpenResult);
628 hr = GAMEUX_buildStatisticsFilePath(sGameId, pStats->sStatsFile);
630 hr = GAMEUX_loadStatisticsFromFile(pStats);
631 TRACE("ldstats finished, res: %#x\n", hr);
632 if(hr == S_OK)
634 *pOpenResult = GAMESTATS_OPEN_OPENED;
636 else if(hr == S_FALSE && openType == GAMESTATS_OPEN_OPENORCREATE) /* file does not exist */
638 /* create new statistics, not yet connected with file */
639 ZeroMemory(pStats->categories, sizeof(pStats->categories));
640 *pOpenResult = GAMESTATS_OPEN_CREATED;
641 hr = S_OK;
643 else
644 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
646 TRACE("openResult=%#x ret=%#x\n", *pOpenResult, hr);
647 return hr;
649 /*******************************************************************
650 * IGameStatistics implementation
652 typedef struct _GameStatisticsImpl
654 const struct IGameStatisticsVtbl *lpVtbl;
655 LONG ref;
656 struct GAMEUX_STATS stats;
657 } GameStatisticsImpl;
659 static inline GameStatisticsImpl *impl_from_IGameStatistics( IGameStatistics *iface )
661 return (GameStatisticsImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsImpl, lpVtbl));
663 static inline IGameStatistics *IGameStatistics_from_impl( GameStatisticsImpl* This )
665 return (struct IGameStatistics*)&This->lpVtbl;
669 static HRESULT WINAPI GameStatisticsImpl_QueryInterface(
670 IGameStatistics *iface,
671 REFIID riid,
672 void **ppvObject)
674 GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
676 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
678 *ppvObject = NULL;
680 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
681 IsEqualGUID( riid, &IID_IGameStatistics ) )
683 *ppvObject = iface;
685 else
687 FIXME("interface %s not implemented\n", debugstr_guid(riid));
688 return E_NOINTERFACE;
691 IGameStatistics_AddRef( iface );
692 return S_OK;
695 static ULONG WINAPI GameStatisticsImpl_AddRef(IGameStatistics *iface)
697 GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
698 LONG ref;
700 ref = InterlockedIncrement(&This->ref);
702 TRACE("(%p): ref=%d\n", This, ref);
703 return ref;
706 static ULONG WINAPI GameStatisticsImpl_Release(IGameStatistics *iface)
708 GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
709 LONG ref;
711 ref = InterlockedDecrement( &This->ref );
712 TRACE("(%p): ref=%d\n", This, ref);
714 if ( ref == 0 )
716 TRACE("freeing IGameStatistics\n");
717 HeapFree( GetProcessHeap(), 0, This );
720 return ref;
723 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategoryLength(
724 IGameStatistics *iface,
725 UINT *cch)
727 TRACE("(%p, %p)\n", iface, cch);
728 if(!cch)
729 return E_INVALIDARG;
731 *cch = MAX_CATEGORY_LENGTH;
732 return S_OK;
735 static HRESULT WINAPI GameStatisticsImpl_GetMaxNameLength(
736 IGameStatistics *iface,
737 UINT *cch)
739 TRACE("(%p, %p)\n", iface, cch);
740 if(!cch)
741 return E_INVALIDARG;
743 *cch = MAX_NAME_LENGTH;
744 return S_OK;
747 static HRESULT WINAPI GameStatisticsImpl_GetMaxValueLength(
748 IGameStatistics *iface,
749 UINT *cch)
751 TRACE("(%p, %p)\n", iface, cch);
752 if(!cch)
753 return E_INVALIDARG;
755 *cch = MAX_VALUE_LENGTH;
756 return S_OK;
759 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategories(
760 IGameStatistics *iface,
761 WORD *pMax)
763 TRACE("(%p, %p)\n", iface, pMax);
764 if(!pMax)
765 return E_INVALIDARG;
767 *pMax = MAX_CATEGORIES;
768 return S_OK;
771 static HRESULT WINAPI GameStatisticsImpl_GetMaxStatsPerCategory(
772 IGameStatistics *iface,
773 WORD *pMax)
775 TRACE("(%p, %p)\n", iface, pMax);
776 if(!pMax)
777 return E_INVALIDARG;
779 *pMax = MAX_STATS_PER_CATEGORY;
780 return S_OK;
783 static HRESULT WINAPI GameStatisticsImpl_SetCategoryTitle(
784 IGameStatistics *iface,
785 WORD categoryIndex,
786 LPCWSTR title)
788 HRESULT hr = S_OK;
789 DWORD dwLength;
790 GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
792 TRACE("(%p, %d, %s)\n", This, categoryIndex, debugstr_w(title));
794 if(!title || categoryIndex >= MAX_CATEGORIES)
795 return E_INVALIDARG;
797 dwLength = lstrlenW(title);
799 if(dwLength > MAX_CATEGORY_LENGTH)
801 hr = S_FALSE;
802 dwLength = MAX_CATEGORY_LENGTH;
805 lstrcpynW(This->stats.categories[categoryIndex].sName,
806 title, dwLength+1);
808 return hr;
811 static HRESULT WINAPI GameStatisticsImpl_GetCategoryTitle(
812 IGameStatistics *iface,
813 WORD categoryIndex,
814 LPWSTR *pTitle)
816 HRESULT hr = S_OK;
817 LONG nLength;
818 GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
820 TRACE("%p, %d, %p\n", This, categoryIndex, pTitle);
822 if(!pTitle)
823 return E_INVALIDARG;
824 *pTitle = NULL;
826 if (categoryIndex >= MAX_CATEGORIES)
827 hr = E_INVALIDARG;
829 if(SUCCEEDED(hr))
831 nLength = lstrlenW(This->stats.categories[categoryIndex].sName);
832 if(nLength != 0)
834 *pTitle = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
835 lstrcpyW(*pTitle, This->stats.categories[categoryIndex].sName);
839 return hr;
842 static HRESULT WINAPI GameStatisticsImpl_GetStatistic(
843 IGameStatistics *iface,
844 WORD categoryIndex,
845 WORD statIndex,
846 LPWSTR *pName,
847 LPWSTR *pValue)
849 HRESULT hr = S_OK;
850 LONG nLength;
851 GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
853 TRACE("%p, %d,%d, %p, %p\n", This, categoryIndex, statIndex, pName, pValue);
855 if(!pName || !pValue)
856 return E_INVALIDARG;
858 *pName = NULL;
859 *pValue = NULL;
861 if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
862 hr = E_INVALIDARG;
864 if(SUCCEEDED(hr))
866 nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sName);
867 if(nLength != 0)
869 *pName = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
870 if(!(*pName))
871 hr = E_OUTOFMEMORY;
872 else
873 lstrcpyW(*pName, This->stats.categories[categoryIndex].stats[statIndex].sName);
877 if(SUCCEEDED(hr))
879 nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sValue);
880 if(nLength != 0)
882 *pValue = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
883 if(!(*pValue))
884 hr = E_OUTOFMEMORY;
885 else
886 lstrcpyW(*pValue, This->stats.categories[categoryIndex].stats[statIndex].sValue);
890 TRACE("returning pair; %s => %s\n", debugstr_w(*pName), debugstr_w(*pValue));
891 return hr;
894 static HRESULT WINAPI GameStatisticsImpl_SetStatistic(
895 IGameStatistics *iface,
896 WORD categoryIndex,
897 WORD statIndex,
898 LPCWSTR name,
899 LPCWSTR value)
901 HRESULT hr = S_OK;
902 DWORD dwNameLen, dwValueLen;
903 GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
905 TRACE("(%p, %d, %d, %s, %s)\n", This, categoryIndex, statIndex,
906 debugstr_w(name), debugstr_w(value));
908 if(!name)
909 return S_FALSE;
911 if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
912 return E_INVALIDARG;
914 dwNameLen = lstrlenW(name);
916 if(dwNameLen > MAX_NAME_LENGTH)
918 hr = S_FALSE;
919 dwNameLen = MAX_NAME_LENGTH;
922 lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sName,
923 name, dwNameLen+1);
925 if(value)
927 dwValueLen = lstrlenW(value);
929 if(dwValueLen > MAX_VALUE_LENGTH)
931 hr = S_FALSE;
932 dwValueLen = MAX_VALUE_LENGTH;
935 lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sValue,
936 value, dwValueLen+1);
938 else
939 /* Windows allows to pass NULL as value */
940 This->stats.categories[categoryIndex].stats[statIndex].sValue[0] = 0;
942 return hr;
945 static HRESULT WINAPI GameStatisticsImpl_Save(
946 IGameStatistics *iface,
947 BOOL trackChanges)
949 GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
950 HRESULT hr = S_OK;
952 TRACE("(%p, %d)\n", This, trackChanges);
954 if(trackChanges == TRUE)
955 FIXME("tracking changes not yet implemented\n");
957 hr = GAMEUX_updateStatisticsFile(&This->stats);
959 return hr;
962 static HRESULT WINAPI GameStatisticsImpl_SetLastPlayedCategory(
963 IGameStatistics *iface,
964 UINT categoryIndex)
966 FIXME("stub\n");
967 return E_NOTIMPL;
970 static HRESULT WINAPI GameStatisticsImpl_GetLastPlayedCategory(
971 IGameStatistics *iface,
972 UINT *pCategoryIndex)
974 FIXME("stub\n");
975 return E_NOTIMPL;
978 static const struct IGameStatisticsVtbl GameStatisticsImplVtbl =
980 GameStatisticsImpl_QueryInterface,
981 GameStatisticsImpl_AddRef,
982 GameStatisticsImpl_Release,
983 GameStatisticsImpl_GetMaxCategoryLength,
984 GameStatisticsImpl_GetMaxNameLength,
985 GameStatisticsImpl_GetMaxValueLength,
986 GameStatisticsImpl_GetMaxCategories,
987 GameStatisticsImpl_GetMaxStatsPerCategory,
988 GameStatisticsImpl_SetCategoryTitle,
989 GameStatisticsImpl_GetCategoryTitle,
990 GameStatisticsImpl_GetStatistic,
991 GameStatisticsImpl_SetStatistic,
992 GameStatisticsImpl_Save,
993 GameStatisticsImpl_SetLastPlayedCategory,
994 GameStatisticsImpl_GetLastPlayedCategory
998 static HRESULT create_IGameStatistics(GameStatisticsImpl** ppStats)
1000 TRACE("(%p)\n", ppStats);
1002 *ppStats = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**ppStats));
1003 if(!(*ppStats))
1004 return E_OUTOFMEMORY;
1006 (*ppStats)->lpVtbl = &GameStatisticsImplVtbl;
1007 (*ppStats)->ref = 1;
1009 TRACE("returning coclass: %p\n", *ppStats);
1010 return S_OK;
1013 /*******************************************************************************
1014 * IGameStatisticsMgr implementation
1016 typedef struct _GameStatisticsMgrImpl
1018 const struct IGameStatisticsMgrVtbl *lpVtbl;
1019 LONG ref;
1020 } GameStatisticsMgrImpl;
1022 static inline GameStatisticsMgrImpl *impl_from_IGameStatisticsMgr( IGameStatisticsMgr *iface )
1024 return (GameStatisticsMgrImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsMgrImpl, lpVtbl));
1028 static HRESULT WINAPI GameStatisticsMgrImpl_QueryInterface(
1029 IGameStatisticsMgr *iface,
1030 REFIID riid,
1031 void **ppvObject)
1033 GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1035 TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
1037 *ppvObject = NULL;
1039 if(IsEqualGUID(riid, &IID_IUnknown) ||
1040 IsEqualGUID(riid, &IID_IGameStatisticsMgr) )
1042 *ppvObject = iface;
1044 else
1046 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1047 return E_NOINTERFACE;
1050 IGameStatisticsMgr_AddRef( iface );
1051 return S_OK;
1054 static ULONG WINAPI GameStatisticsMgrImpl_AddRef(IGameStatisticsMgr *iface)
1056 GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1057 LONG ref;
1059 ref = InterlockedIncrement(&This->ref);
1061 TRACE("(%p): ref=%d\n", This, ref);
1062 return ref;
1065 static ULONG WINAPI GameStatisticsMgrImpl_Release(IGameStatisticsMgr *iface)
1067 GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1068 LONG ref;
1070 ref = InterlockedDecrement(&This->ref);
1071 TRACE("(%p): ref=%d\n", This, ref);
1073 if ( ref == 0 )
1075 TRACE("freeing GameStatistics object\n");
1076 HeapFree( GetProcessHeap(), 0, This);
1079 return ref;
1082 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_GetGameStatistics(
1083 IGameStatisticsMgr* iface,
1084 LPCWSTR GDFBinaryPath,
1085 GAMESTATS_OPEN_TYPE openType,
1086 GAMESTATS_OPEN_RESULT *pOpenResult,
1087 IGameStatistics **ppiStats)
1089 HRESULT hr;
1090 WCHAR lpApplicationId[49];
1091 GameStatisticsImpl *statisticsImpl = NULL;
1092 IGameStatistics *output_iface;
1094 TRACE("(%p, %s, 0x%x, %p, %p)\n", iface, debugstr_w(GDFBinaryPath), openType, pOpenResult, ppiStats);
1096 hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1098 if(SUCCEEDED(hr))
1099 hr = create_IGameStatistics(&statisticsImpl);
1101 if(SUCCEEDED(hr))
1103 output_iface = IGameStatistics_from_impl(statisticsImpl);
1104 hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, statisticsImpl->stats.sStatsFile);
1107 if(SUCCEEDED(hr))
1108 hr = GAMEUX_loadGameStatistics(&statisticsImpl->stats, lpApplicationId, openType, pOpenResult);
1110 if(SUCCEEDED(hr))
1111 *ppiStats = output_iface;
1112 else
1114 HeapFree(GetProcessHeap(), 0, statisticsImpl);
1115 *ppiStats = NULL;
1118 return hr;
1121 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_RemoveGameStatistics(
1122 IGameStatisticsMgr* iface,
1123 LPCWSTR GDFBinaryPath)
1125 HRESULT hr;
1126 WCHAR lpApplicationId[49];
1127 WCHAR sStatsFile[MAX_PATH];
1129 TRACE("(%p, %s)\n", iface, debugstr_w(GDFBinaryPath));
1131 hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1133 if(SUCCEEDED(hr))
1134 hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, sStatsFile);
1136 if(SUCCEEDED(hr))
1137 hr = (DeleteFileW(sStatsFile)==TRUE ? S_OK : HRESULT_FROM_WIN32(GetLastError()));
1139 return hr;
1142 static const struct IGameStatisticsMgrVtbl GameStatisticsMgrImplVtbl =
1144 GameStatisticsMgrImpl_QueryInterface,
1145 GameStatisticsMgrImpl_AddRef,
1146 GameStatisticsMgrImpl_Release,
1147 GameStatisticsMgrImpl_GetGameStatistics,
1148 GameStatisticsMgrImpl_RemoveGameStatistics,
1151 HRESULT GameStatistics_create(
1152 IUnknown *pUnkOuter,
1153 IUnknown **ppObj)
1155 GameStatisticsMgrImpl *pGameStatistics;
1157 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1159 pGameStatistics = HeapAlloc( GetProcessHeap(), 0, sizeof (*pGameStatistics) );
1161 if( !pGameStatistics )
1162 return E_OUTOFMEMORY;
1164 pGameStatistics->lpVtbl = &GameStatisticsMgrImplVtbl;
1165 pGameStatistics->ref = 1;
1167 *ppObj = (IUnknown*)(&pGameStatistics->lpVtbl);
1169 TRACE("returning iface %p\n", *ppObj);
1170 return S_OK;