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
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 /*******************************************************************************
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
];
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
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
)
81 WCHAR lpDirectoryPath
[MAX_PATH
];
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
))
95 /*******************************************************************
96 * GAMEUX_updateStatisticsFile
98 * Helper function updating data stored in statistics file
101 * data [I] pointer to struct containing
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};
114 IXMLDOMDocument
*document
;
115 IXMLDOMElement
*root
, *statisticsElement
;
116 IXMLDOMNode
*categoryNode
, *statisticsNode
;
117 VARIANT vStatsFilePath
, vValue
;
118 BSTR bstrStatistics
= NULL
, bstrCategory
= NULL
, bstrIndex
= NULL
,
119 bstrStatistic
= NULL
, bstrName
= NULL
, bstrValue
= NULL
;
122 TRACE("(%p)\n", stats
);
124 V_VT(&vStatsFilePath
) = VT_BSTR
;
125 V_BSTR(&vStatsFilePath
) = SysAllocString(stats
->sStatsFile
);
126 if(!V_BSTR(&vStatsFilePath
))
130 hr
= CoCreateInstance(&CLSID_DOMDocument
, NULL
, CLSCTX_INPROC_SERVER
,
131 &IID_IXMLDOMDocument
, (void**)&document
);
135 bstrStatistics
= SysAllocString(sStatistics
);
141 hr
= IXMLDOMDocument_createElement(document
, bstrStatistics
, &root
);
145 bstrCategory
= SysAllocString(sCategory
);
152 bstrIndex
= SysAllocString(sIndex
);
159 bstrStatistic
= SysAllocString(sStatistic
);
166 bstrName
= SysAllocString(sName
);
173 bstrValue
= SysAllocString(sValue
);
179 for(i
=0; i
<MAX_CATEGORIES
; ++i
)
181 IXMLDOMElement
*categoryElement
= NULL
;
183 if(!stats
->categories
[i
].sName
[0])
186 V_VT(&vValue
) = VT_INT
;
187 V_INT(&vValue
) = NODE_ELEMENT
;
189 hr
= IXMLDOMDocument_createNode(document
, vValue
, bstrCategory
, NULL
, &categoryNode
);
192 hr
= IXMLDOMNode_QueryInterface(categoryNode
, &IID_IXMLDOMElement
, (void**)&categoryElement
);
196 hr
= IXMLDOMElement_setAttribute(categoryElement
, bstrIndex
, vValue
);
200 V_VT(&vValue
) = VT_BSTR
;
201 V_BSTR(&vValue
) = SysAllocString(stats
->categories
[i
].sName
);
208 TRACE("storing category %d: %s\n", i
, debugstr_w(V_BSTR(&vValue
)));
209 hr
= IXMLDOMElement_setAttribute(categoryElement
, bstrName
, vValue
);
213 IXMLDOMElement_Release(categoryElement
);
215 SysFreeString(V_BSTR(&vValue
));
219 for(j
=0; j
<MAX_STATS_PER_CATEGORY
; ++j
)
221 if(!stats
->categories
[i
].stats
[j
].sName
[0])
224 V_VT(&vValue
) = VT_INT
;
225 V_INT(&vValue
) = NODE_ELEMENT
;
227 hr
= IXMLDOMDocument_createNode(document
, vValue
, bstrStatistic
, NULL
, &statisticsNode
);
230 hr
= IXMLDOMNode_QueryInterface(statisticsNode
, &IID_IXMLDOMElement
, (LPVOID
*)&statisticsElement
);
234 hr
= IXMLDOMElement_setAttribute(statisticsElement
, bstrIndex
, vValue
);
238 V_VT(&vValue
) = VT_BSTR
;
239 V_BSTR(&vValue
) = SysAllocString(stats
->categories
[i
].stats
[j
].sName
);
246 TRACE(" storing statistic %d: name: %s\n", j
, debugstr_w(V_BSTR(&vValue
)));
247 hr
= IXMLDOMElement_setAttribute(statisticsElement
, bstrName
, vValue
);
248 SysFreeString(V_BSTR(&vValue
));
253 V_VT(&vValue
) = VT_BSTR
;
254 V_BSTR(&vValue
) = SysAllocString(stats
->categories
[i
].stats
[j
].sValue
);
261 TRACE(" storing statistic %d: name: %s\n", j
, debugstr_w(V_BSTR(&vValue
)));
262 hr
= IXMLDOMElement_setAttribute(statisticsElement
, bstrValue
, vValue
);
263 SysFreeString(V_BSTR(&vValue
));
267 hr
= IXMLDOMNode_appendChild(categoryNode
, statisticsNode
, &statisticsNode
);
269 IXMLDOMElement_Release(statisticsElement
);
270 IXMLDOMNode_Release(statisticsNode
);
275 hr
= IXMLDOMElement_appendChild(root
, categoryNode
, &categoryNode
);
277 IXMLDOMNode_Release(categoryNode
);
284 hr
= IXMLDOMDocument_putref_documentElement(document
, root
);
286 IXMLDOMElement_Release(root
);
288 TRACE("saving game statistics in %s file\n", debugstr_w(stats
->sStatsFile
));
290 hr
= GAMEUX_createStatsDirectory(stats
->sStatsFile
);
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
);
307 /*******************************************************************************
308 * GAMEUX_buildStatisticsFilePath
309 * Creates path to file containing statistics of game with given id.
312 * lpApplicationId [I] application id of game,
314 * lpStatisticsFile [O] array where path will be
315 * stored. Its size must be
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};
331 hr
= SHGetFolderPathW(NULL
, CSIDL_LOCAL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
, lpStatisticsFile
);
335 lstrcatW(lpStatisticsFile
, sStatisticsDir
);
336 lstrcatW(lpStatisticsFile
, lpApplicationId
);
337 lstrcatW(lpStatisticsFile
, sBackslash
);
338 lstrcatW(lpStatisticsFile
, lpApplicationId
);
339 lstrcatW(lpStatisticsFile
, sDotGamestats
);
344 /*******************************************************************************
345 * GAMEUX_getAppIdFromGDFPath
347 * Loads application identifier associated with given GDF binary.
348 * Routine reads identifier from registry, so will fail if game
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};
365 GAME_INSTALL_SCOPE installScope
;
367 LPWSTR lpRegistryPath
= NULL
;
369 DWORD dwLength
= 49*sizeof(WCHAR
);/* place for GUID */
371 TRACE("(%s, %p)\n", debugstr_w(GDFBinaryPath
), lpApplicationId
);
376 installScope
= GIS_CURRENT_USER
;
377 hr
= GAMEUX_FindGameInstanceId(GDFBinaryPath
, installScope
, &instanceId
);
381 installScope
= GIS_ALL_USERS
;
382 hr
= GAMEUX_FindGameInstanceId(GDFBinaryPath
, installScope
, &instanceId
);
386 /* game not registered, so statistics cannot be used */
390 /* game is registered, let's read its application id from registry */
391 hr
= GAMEUX_buildGameRegistryPath(installScope
, &instanceId
, &lpRegistryPath
);
394 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
395 lpRegistryPath
, 0, KEY_READ
| KEY_WOW64_64KEY
, &hKey
));
397 hr
= HRESULT_FROM_WIN32(RegGetValueW(hKey
,
398 NULL
, sApplicationId
, RRF_RT_REG_SZ
,
399 NULL
, lpApplicationId
, &dwLength
));
404 HeapFree(GetProcessHeap(), 0, lpRegistryPath
);
406 TRACE("found app id: %s, return: %#x\n", debugstr_w(lpApplicationId
), hr
);
409 /*******************************************************************
410 * GAMEUX_loadGameStatisticsFromFile
411 * Helper function, loads game statistics from file and stores them
415 * data [I/O] structure containing file name to
416 * load and data fields to store data in
418 static HRESULT
GAMEUX_loadStatisticsFromFile(struct GAMEUX_STATS
*data
)
420 static const WCHAR sStatistics
[] = {'S','t','a','t','i','s','t','i','c','s',0};
421 static const WCHAR sCategory
[] = {'C','a','t','e','g','o','r','y',0};
422 static const WCHAR sIndex
[] = {'I','n','d','e','x',0};
423 static const WCHAR sStatistic
[] = {'S','t','a','t','i','s','t','i','c',0};
424 static const WCHAR sName
[] = {'N','a','m','e',0};
425 static const WCHAR sValue
[] = {'V','a','l','u','e',0};
428 IXMLDOMDocument
*document
= NULL
;
429 IXMLDOMElement
*root
= NULL
, *categoryElement
, *statisticElement
;
430 IXMLDOMNode
*categoryNode
, *statisticNode
;
431 IXMLDOMNodeList
*rootChildren
= NULL
, *categoryChildren
;
432 VARIANT vStatsFilePath
, vValue
;
433 BSTR bstrStatistics
= NULL
, bstrCategory
= NULL
, bstrIndex
= NULL
,
434 bstrStatistic
= NULL
, bstrName
= NULL
, bstrValue
= NULL
;
435 VARIANT_BOOL isSuccessful
= VARIANT_FALSE
;
438 TRACE("(%p)\n", data
);
440 V_VT(&vStatsFilePath
) = VT_BSTR
;
441 V_BSTR(&vStatsFilePath
) = SysAllocString(data
->sStatsFile
);
442 if(!V_BSTR(&vStatsFilePath
))
446 hr
= CoCreateInstance(&CLSID_DOMDocument30
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IXMLDOMDocument
, (void**)&document
);
450 bstrStatistics
= SysAllocString(sStatistics
);
457 bstrCategory
= SysAllocString(sCategory
);
464 bstrIndex
= SysAllocString(sIndex
);
471 bstrStatistic
= SysAllocString(sStatistic
);
478 bstrName
= SysAllocString(sName
);
485 bstrValue
= SysAllocString(sValue
);
491 hr
= IXMLDOMDocument_load(document
, vStatsFilePath
, &isSuccessful
);
493 if(hr
== S_OK
&& isSuccessful
!= VARIANT_TRUE
)
497 hr
= IXMLDOMDocument_get_documentElement(document
, &root
);
500 hr
= IXMLDOMElement_get_childNodes(root
, &rootChildren
);
507 hr
= IXMLDOMNodeList_nextNode(rootChildren
, &categoryNode
);
511 hr
= IXMLDOMNode_QueryInterface(categoryNode
, &IID_IXMLDOMElement
, (LPVOID
*)&categoryElement
);
515 hr
= IXMLDOMElement_getAttribute(categoryElement
, bstrIndex
, &vValue
);
516 if( hr
== S_OK
&& V_VT(&vValue
) != VT_BSTR
)
521 i
= StrToIntW(V_BSTR(&vValue
));
522 hr
= IXMLDOMElement_getAttribute(categoryElement
, bstrName
, &vValue
);
523 if( hr
== S_OK
&& V_VT(&vValue
) != VT_BSTR
)
529 lstrcpynW(data
->categories
[i
].sName
, V_BSTR(&vValue
), MAX_CATEGORY_LENGTH
);
530 TRACE("category %d name %s\n", i
, debugstr_w(data
->categories
[i
].sName
));
531 hr
= IXMLDOMElement_get_childNodes(categoryElement
, &categoryChildren
);
539 hr
= IXMLDOMNodeList_nextNode(categoryChildren
, &statisticNode
);
543 hr
= IXMLDOMNode_QueryInterface(statisticNode
, &IID_IXMLDOMElement
, (LPVOID
*)&statisticElement
);
547 hr
= IXMLDOMElement_getAttribute(statisticElement
, bstrIndex
, &vValue
);
548 if( hr
== S_OK
&& V_VT(&vValue
) != VT_BSTR
)
553 j
= StrToIntW(V_BSTR(&vValue
));
554 hr
= IXMLDOMElement_getAttribute(statisticElement
, bstrName
, &vValue
);
555 if( hr
== S_OK
&& V_VT(&vValue
) != VT_BSTR
)
561 lstrcpynW(data
->categories
[i
].stats
[j
].sName
, V_BSTR(&vValue
), MAX_NAME_LENGTH
);
562 hr
= IXMLDOMElement_getAttribute(statisticElement
, bstrValue
, &vValue
);
563 if( hr
== S_OK
&& V_VT(&vValue
) != VT_BSTR
)
569 lstrcpynW(data
->categories
[i
].stats
[j
].sValue
, V_BSTR(&vValue
), MAX_VALUE_LENGTH
);
570 TRACE("statistic %d name %s value %s\n", j
,
571 debugstr_w(data
->categories
[i
].stats
[j
].sName
),
572 debugstr_w(data
->categories
[i
].stats
[j
].sValue
));
574 IXMLDOMElement_Release(statisticElement
);
577 IXMLDOMNode_Release(statisticNode
);
584 IXMLDOMElement_Release(categoryElement
);
587 IXMLDOMNode_Release(categoryNode
);
594 if(rootChildren
) IXMLDOMNodeList_Release(rootChildren
);
595 if(root
) IXMLDOMElement_Release(root
);
596 if(document
) IXMLDOMDocument_Release(document
);
598 SysFreeString(bstrValue
);
599 SysFreeString(bstrName
);
600 SysFreeString(bstrStatistic
);
601 SysFreeString(bstrIndex
);
602 SysFreeString(bstrCategory
);
603 SysFreeString(bstrStatistics
);
604 SysFreeString(V_BSTR(&vStatsFilePath
));
607 /*******************************************************************
608 * GAMEUX_loadGameStatistics
610 * Helper function which loads game statistics associated with game
611 * into interface's internal structures
614 * pStats [O] structure which will receive data
615 * sGameId [I] application instance Id, stored as string
616 * to avoid additional conversions
617 * openType [I] allowed ways of opening statistics
618 * pOpenResult [O] way used to open statistics
621 static HRESULT
GAMEUX_loadGameStatistics(struct GAMEUX_STATS
*pStats
,
623 GAMESTATS_OPEN_TYPE openType
,
624 GAMESTATS_OPEN_RESULT
* pOpenResult
)
627 TRACE("(%p, %s, %d, %p)\n", pStats
, debugstr_w(sGameId
), openType
, pOpenResult
);
629 hr
= GAMEUX_buildStatisticsFilePath(sGameId
, pStats
->sStatsFile
);
630 if (FAILED(hr
)) return hr
;
632 hr
= GAMEUX_loadStatisticsFromFile(pStats
);
633 TRACE("ldstats finished, res: %#x\n", hr
);
636 *pOpenResult
= GAMESTATS_OPEN_OPENED
;
638 else if(hr
== S_FALSE
&& openType
== GAMESTATS_OPEN_OPENORCREATE
) /* file does not exist */
640 /* create new statistics, not yet connected with file */
641 ZeroMemory(pStats
->categories
, sizeof(pStats
->categories
));
642 *pOpenResult
= GAMESTATS_OPEN_CREATED
;
646 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
648 TRACE("openResult=%#x ret=%#x\n", *pOpenResult
, hr
);
651 /*******************************************************************
652 * IGameStatistics implementation
654 typedef struct _GameStatisticsImpl
656 IGameStatistics IGameStatistics_iface
;
658 struct GAMEUX_STATS stats
;
659 } GameStatisticsImpl
;
661 static inline GameStatisticsImpl
*impl_from_IGameStatistics( IGameStatistics
*iface
)
663 return CONTAINING_RECORD(iface
, GameStatisticsImpl
, IGameStatistics_iface
);
666 static HRESULT WINAPI
GameStatisticsImpl_QueryInterface(
667 IGameStatistics
*iface
,
671 GameStatisticsImpl
*This
= impl_from_IGameStatistics( iface
);
673 TRACE("%p %s %p\n", This
, debugstr_guid( riid
), ppvObject
);
677 if ( IsEqualGUID( riid
, &IID_IUnknown
) ||
678 IsEqualGUID( riid
, &IID_IGameStatistics
) )
684 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
685 return E_NOINTERFACE
;
688 IGameStatistics_AddRef( iface
);
692 static ULONG WINAPI
GameStatisticsImpl_AddRef(IGameStatistics
*iface
)
694 GameStatisticsImpl
*This
= impl_from_IGameStatistics( iface
);
697 ref
= InterlockedIncrement(&This
->ref
);
699 TRACE("(%p): ref=%d\n", This
, ref
);
703 static ULONG WINAPI
GameStatisticsImpl_Release(IGameStatistics
*iface
)
705 GameStatisticsImpl
*This
= impl_from_IGameStatistics( iface
);
708 ref
= InterlockedDecrement( &This
->ref
);
709 TRACE("(%p): ref=%d\n", This
, ref
);
713 TRACE("freeing IGameStatistics\n");
714 HeapFree( GetProcessHeap(), 0, This
);
720 static HRESULT WINAPI
GameStatisticsImpl_GetMaxCategoryLength(
721 IGameStatistics
*iface
,
724 TRACE("(%p, %p)\n", iface
, cch
);
728 *cch
= MAX_CATEGORY_LENGTH
;
732 static HRESULT WINAPI
GameStatisticsImpl_GetMaxNameLength(
733 IGameStatistics
*iface
,
736 TRACE("(%p, %p)\n", iface
, cch
);
740 *cch
= MAX_NAME_LENGTH
;
744 static HRESULT WINAPI
GameStatisticsImpl_GetMaxValueLength(
745 IGameStatistics
*iface
,
748 TRACE("(%p, %p)\n", iface
, cch
);
752 *cch
= MAX_VALUE_LENGTH
;
756 static HRESULT WINAPI
GameStatisticsImpl_GetMaxCategories(
757 IGameStatistics
*iface
,
760 TRACE("(%p, %p)\n", iface
, pMax
);
764 *pMax
= MAX_CATEGORIES
;
768 static HRESULT WINAPI
GameStatisticsImpl_GetMaxStatsPerCategory(
769 IGameStatistics
*iface
,
772 TRACE("(%p, %p)\n", iface
, pMax
);
776 *pMax
= MAX_STATS_PER_CATEGORY
;
780 static HRESULT WINAPI
GameStatisticsImpl_SetCategoryTitle(
781 IGameStatistics
*iface
,
787 GameStatisticsImpl
*This
= impl_from_IGameStatistics(iface
);
789 TRACE("(%p, %d, %s)\n", This
, categoryIndex
, debugstr_w(title
));
791 if(!title
|| categoryIndex
>= MAX_CATEGORIES
)
794 dwLength
= lstrlenW(title
);
796 if(dwLength
> MAX_CATEGORY_LENGTH
)
799 dwLength
= MAX_CATEGORY_LENGTH
;
802 lstrcpynW(This
->stats
.categories
[categoryIndex
].sName
,
808 static HRESULT WINAPI
GameStatisticsImpl_GetCategoryTitle(
809 IGameStatistics
*iface
,
815 GameStatisticsImpl
*This
= impl_from_IGameStatistics(iface
);
817 TRACE("%p, %d, %p\n", This
, categoryIndex
, pTitle
);
823 if (categoryIndex
>= MAX_CATEGORIES
)
828 nLength
= lstrlenW(This
->stats
.categories
[categoryIndex
].sName
);
831 *pTitle
= CoTaskMemAlloc(sizeof(WCHAR
)*(nLength
+1));
832 lstrcpyW(*pTitle
, This
->stats
.categories
[categoryIndex
].sName
);
839 static HRESULT WINAPI
GameStatisticsImpl_GetStatistic(
840 IGameStatistics
*iface
,
848 GameStatisticsImpl
*This
= impl_from_IGameStatistics(iface
);
850 TRACE("%p, %d,%d, %p, %p\n", This
, categoryIndex
, statIndex
, pName
, pValue
);
852 if(!pName
|| !pValue
)
858 if(categoryIndex
>= MAX_CATEGORIES
|| statIndex
>= MAX_STATS_PER_CATEGORY
)
863 nLength
= lstrlenW(This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sName
);
866 *pName
= CoTaskMemAlloc(sizeof(WCHAR
)*(nLength
+1));
870 lstrcpyW(*pName
, This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sName
);
876 nLength
= lstrlenW(This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sValue
);
879 *pValue
= CoTaskMemAlloc(sizeof(WCHAR
)*(nLength
+1));
883 lstrcpyW(*pValue
, This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sValue
);
887 TRACE("returning pair; %s => %s\n", debugstr_w(*pName
), debugstr_w(*pValue
));
891 static HRESULT WINAPI
GameStatisticsImpl_SetStatistic(
892 IGameStatistics
*iface
,
899 DWORD dwNameLen
, dwValueLen
;
900 GameStatisticsImpl
*This
= impl_from_IGameStatistics(iface
);
902 TRACE("(%p, %d, %d, %s, %s)\n", This
, categoryIndex
, statIndex
,
903 debugstr_w(name
), debugstr_w(value
));
908 if(categoryIndex
>= MAX_CATEGORIES
|| statIndex
>= MAX_STATS_PER_CATEGORY
)
911 dwNameLen
= lstrlenW(name
);
913 if(dwNameLen
> MAX_NAME_LENGTH
)
916 dwNameLen
= MAX_NAME_LENGTH
;
919 lstrcpynW(This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sName
,
924 dwValueLen
= lstrlenW(value
);
926 if(dwValueLen
> MAX_VALUE_LENGTH
)
929 dwValueLen
= MAX_VALUE_LENGTH
;
932 lstrcpynW(This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sValue
,
933 value
, dwValueLen
+1);
936 /* Windows allows passing NULL as value */
937 This
->stats
.categories
[categoryIndex
].stats
[statIndex
].sValue
[0] = 0;
942 static HRESULT WINAPI
GameStatisticsImpl_Save(
943 IGameStatistics
*iface
,
946 GameStatisticsImpl
*This
= impl_from_IGameStatistics(iface
);
948 TRACE("(%p, %d)\n", This
, trackChanges
);
951 FIXME("tracking changes not yet implemented\n");
953 return GAMEUX_updateStatisticsFile(&This
->stats
);
956 static HRESULT WINAPI
GameStatisticsImpl_SetLastPlayedCategory(
957 IGameStatistics
*iface
,
964 static HRESULT WINAPI
GameStatisticsImpl_GetLastPlayedCategory(
965 IGameStatistics
*iface
,
966 UINT
*pCategoryIndex
)
972 static const struct IGameStatisticsVtbl GameStatisticsImplVtbl
=
974 GameStatisticsImpl_QueryInterface
,
975 GameStatisticsImpl_AddRef
,
976 GameStatisticsImpl_Release
,
977 GameStatisticsImpl_GetMaxCategoryLength
,
978 GameStatisticsImpl_GetMaxNameLength
,
979 GameStatisticsImpl_GetMaxValueLength
,
980 GameStatisticsImpl_GetMaxCategories
,
981 GameStatisticsImpl_GetMaxStatsPerCategory
,
982 GameStatisticsImpl_SetCategoryTitle
,
983 GameStatisticsImpl_GetCategoryTitle
,
984 GameStatisticsImpl_GetStatistic
,
985 GameStatisticsImpl_SetStatistic
,
986 GameStatisticsImpl_Save
,
987 GameStatisticsImpl_SetLastPlayedCategory
,
988 GameStatisticsImpl_GetLastPlayedCategory
992 static HRESULT
create_IGameStatistics(GameStatisticsImpl
** ppStats
)
994 TRACE("(%p)\n", ppStats
);
996 *ppStats
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**ppStats
));
998 return E_OUTOFMEMORY
;
1000 (*ppStats
)->IGameStatistics_iface
.lpVtbl
= &GameStatisticsImplVtbl
;
1001 (*ppStats
)->ref
= 1;
1003 TRACE("returning coclass: %p\n", *ppStats
);
1007 /*******************************************************************************
1008 * IGameStatisticsMgr implementation
1010 typedef struct _GameStatisticsMgrImpl
1012 IGameStatisticsMgr IGameStatisticsMgr_iface
;
1014 } GameStatisticsMgrImpl
;
1016 static inline GameStatisticsMgrImpl
*impl_from_IGameStatisticsMgr( IGameStatisticsMgr
*iface
)
1018 return CONTAINING_RECORD(iface
, GameStatisticsMgrImpl
, IGameStatisticsMgr_iface
);
1022 static HRESULT WINAPI
GameStatisticsMgrImpl_QueryInterface(
1023 IGameStatisticsMgr
*iface
,
1027 GameStatisticsMgrImpl
*This
= impl_from_IGameStatisticsMgr( iface
);
1029 TRACE("%p %s %p\n", This
, debugstr_guid( riid
), ppvObject
);
1033 if(IsEqualGUID(riid
, &IID_IUnknown
) ||
1034 IsEqualGUID(riid
, &IID_IGameStatisticsMgr
) )
1040 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
1041 return E_NOINTERFACE
;
1044 IGameStatisticsMgr_AddRef( iface
);
1048 static ULONG WINAPI
GameStatisticsMgrImpl_AddRef(IGameStatisticsMgr
*iface
)
1050 GameStatisticsMgrImpl
*This
= impl_from_IGameStatisticsMgr( iface
);
1053 ref
= InterlockedIncrement(&This
->ref
);
1055 TRACE("(%p): ref=%d\n", This
, ref
);
1059 static ULONG WINAPI
GameStatisticsMgrImpl_Release(IGameStatisticsMgr
*iface
)
1061 GameStatisticsMgrImpl
*This
= impl_from_IGameStatisticsMgr( iface
);
1064 ref
= InterlockedDecrement(&This
->ref
);
1065 TRACE("(%p): ref=%d\n", This
, ref
);
1069 TRACE("freeing GameStatistics object\n");
1070 HeapFree( GetProcessHeap(), 0, This
);
1076 static HRESULT STDMETHODCALLTYPE
GameStatisticsMgrImpl_GetGameStatistics(
1077 IGameStatisticsMgr
* iface
,
1078 LPCWSTR GDFBinaryPath
,
1079 GAMESTATS_OPEN_TYPE openType
,
1080 GAMESTATS_OPEN_RESULT
*pOpenResult
,
1081 IGameStatistics
**ppiStats
)
1084 WCHAR lpApplicationId
[49];
1085 GameStatisticsImpl
*statisticsImpl
= NULL
;
1086 IGameStatistics
*output_iface
;
1088 TRACE("(%p, %s, 0x%x, %p, %p)\n", iface
, debugstr_w(GDFBinaryPath
), openType
, pOpenResult
, ppiStats
);
1090 hr
= GAMEUX_getAppIdFromGDFPath(GDFBinaryPath
, lpApplicationId
);
1093 hr
= create_IGameStatistics(&statisticsImpl
);
1097 output_iface
= &statisticsImpl
->IGameStatistics_iface
;
1098 hr
= GAMEUX_buildStatisticsFilePath(lpApplicationId
, statisticsImpl
->stats
.sStatsFile
);
1102 hr
= GAMEUX_loadGameStatistics(&statisticsImpl
->stats
, lpApplicationId
, openType
, pOpenResult
);
1105 *ppiStats
= output_iface
;
1108 HeapFree(GetProcessHeap(), 0, statisticsImpl
);
1115 static HRESULT STDMETHODCALLTYPE
GameStatisticsMgrImpl_RemoveGameStatistics(
1116 IGameStatisticsMgr
* iface
,
1117 LPCWSTR GDFBinaryPath
)
1120 WCHAR lpApplicationId
[49];
1121 WCHAR sStatsFile
[MAX_PATH
];
1123 TRACE("(%p, %s)\n", iface
, debugstr_w(GDFBinaryPath
));
1125 hr
= GAMEUX_getAppIdFromGDFPath(GDFBinaryPath
, lpApplicationId
);
1128 hr
= GAMEUX_buildStatisticsFilePath(lpApplicationId
, sStatsFile
);
1131 hr
= DeleteFileW(sStatsFile
) ? S_OK
: HRESULT_FROM_WIN32(GetLastError());
1136 static const struct IGameStatisticsMgrVtbl GameStatisticsMgrImplVtbl
=
1138 GameStatisticsMgrImpl_QueryInterface
,
1139 GameStatisticsMgrImpl_AddRef
,
1140 GameStatisticsMgrImpl_Release
,
1141 GameStatisticsMgrImpl_GetGameStatistics
,
1142 GameStatisticsMgrImpl_RemoveGameStatistics
,
1145 HRESULT
GameStatistics_create(
1146 IUnknown
*pUnkOuter
,
1149 GameStatisticsMgrImpl
*pGameStatistics
;
1151 TRACE("(%p, %p)\n", pUnkOuter
, ppObj
);
1153 pGameStatistics
= HeapAlloc( GetProcessHeap(), 0, sizeof (*pGameStatistics
) );
1155 if( !pGameStatistics
)
1156 return E_OUTOFMEMORY
;
1158 pGameStatistics
->IGameStatisticsMgr_iface
.lpVtbl
= &GameStatisticsMgrImplVtbl
;
1159 pGameStatistics
->ref
= 1;
1161 *ppObj
= (IUnknown
*)&pGameStatistics
->IGameStatisticsMgr_iface
;
1163 TRACE("returning iface %p\n", *ppObj
);