advapi32: Added support for SERVICE_CONFIG_PRESHUTDOWN_INFO.
[wine] / dlls / gameux / gamestatistics.c
1 /*
2  *    Gameux library coclass GameStatistics implementation
3  *
4  * Copyright (C) 2010 Mariusz PluciƄski
5  *
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.
10  *
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.
15  *
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
19  */
20 #define COBJMACROS
21
22 #include "config.h"
23
24 #include "ole2.h"
25 #include "winreg.h"
26 #include "msxml2.h"
27 #include "shlwapi.h"
28 #include "shlobj.h"
29
30 #include "gameux.h"
31 #include "gameux_private.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(gameux);
36
37 /*
38  * constant definitions
39  */
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
47  */
48 /*******************************************************************************
49  * struct GAMEUX_STATS
50  *
51  * set of structures for containing game's data
52  */
53 struct GAMEUX_STATS_STAT
54 {
55     WCHAR sName[MAX_NAME_LENGTH+1];
56     WCHAR sValue[MAX_VALUE_LENGTH+1];
57 };
58 struct GAMEUX_STATS_CATEGORY
59 {
60     WCHAR sName[MAX_CATEGORY_LENGTH+1];
61     struct GAMEUX_STATS_STAT stats[MAX_STATS_PER_CATEGORY];
62 };
63 struct GAMEUX_STATS
64 {
65     WCHAR sStatsFile[MAX_PATH];
66     struct GAMEUX_STATS_CATEGORY categories[MAX_CATEGORIES];
67 };
68 /*******************************************************************************
69  * GAMEUX_createStatsDirectory
70  *
71  * Helper function, creates directory to store game statistics
72  *
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
77  */
78 static HRESULT GAMEUX_createStatsDirectory(LPCWSTR lpFilePath)
79 {
80     HRESULT hr;
81     WCHAR lpDirectoryPath[MAX_PATH];
82     LPCWSTR lpEnd;
83
84     lpEnd = StrRChrW(lpFilePath, NULL, '\\');
85     lstrcpynW(lpDirectoryPath, lpFilePath, lpEnd-lpFilePath+1);
86
87     hr = HRESULT_FROM_WIN32(SHCreateDirectoryExW(NULL, lpDirectoryPath, NULL));
88
89     if(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) ||
90        hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
91         hr = S_FALSE;
92
93     return hr;
94 }
95 /*******************************************************************
96  * GAMEUX_updateStatisticsFile
97  *
98  * Helper function updating data stored in statistics file
99  *
100  * Parameters:
101  *  data                [I]     pointer to struct containing
102  *                              statistics data
103  */
104 static HRESULT GAMEUX_updateStatisticsFile(struct GAMEUX_STATS *stats)
105 {
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};
112
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;
121
122     TRACE("(%p)\n", stats);
123
124     V_VT(&vStatsFilePath) = VT_BSTR;
125     V_BSTR(&vStatsFilePath) = SysAllocString(stats->sStatsFile);
126     if(!V_BSTR(&vStatsFilePath))
127         hr = E_OUTOFMEMORY;
128
129     if(SUCCEEDED(hr))
130         hr = CoCreateInstance(&CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
131                               &IID_IXMLDOMDocument, (void**)&document);
132
133     if(SUCCEEDED(hr))
134     {
135         bstrStatistics = SysAllocString(sStatistics);
136         if(!bstrStatistics)
137             hr = E_OUTOFMEMORY;
138     }
139
140     if(SUCCEEDED(hr))
141         hr = IXMLDOMDocument_createElement(document, bstrStatistics, &root);
142
143     if(SUCCEEDED(hr))
144     {
145         bstrCategory = SysAllocString(sCategory);
146         if(!bstrCategory)
147             hr = E_OUTOFMEMORY;
148     }
149
150     if(SUCCEEDED(hr))
151     {
152         bstrIndex = SysAllocString(sIndex);
153         if(!bstrIndex)
154             hr = E_OUTOFMEMORY;
155     }
156
157     if(SUCCEEDED(hr))
158     {
159         bstrStatistic = SysAllocString(sStatistic);
160         if(!bstrStatistic)
161             hr = E_OUTOFMEMORY;
162     }
163
164     if(SUCCEEDED(hr))
165     {
166         bstrName = SysAllocString(sName);
167         if(!bstrName)
168             hr = E_OUTOFMEMORY;
169     }
170
171     if(SUCCEEDED(hr))
172     {
173         bstrValue = SysAllocString(sValue);
174         if(!bstrValue)
175             hr = E_OUTOFMEMORY;
176     }
177
178     if(SUCCEEDED(hr))
179         for(i=0; i<MAX_CATEGORIES; ++i)
180         {
181             if(lstrlenW(stats->categories[i].sName)==0)
182                 continue;
183
184             V_VT(&vValue) = VT_INT;
185             V_INT(&vValue) = NODE_ELEMENT;
186
187             hr = IXMLDOMDocument_createNode(document, vValue, bstrCategory, NULL, &categoryNode);
188
189             if(SUCCEEDED(hr))
190                 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
191
192             V_INT(&vValue) = i;
193             if(SUCCEEDED(hr))
194                 hr = IXMLDOMElement_setAttribute(categoryElement, bstrIndex, vValue);
195
196             if(SUCCEEDED(hr))
197             {
198                 V_VT(&vValue) = VT_BSTR;
199                 V_BSTR(&vValue) = SysAllocString(stats->categories[i].sName);
200                 if(!V_BSTR(&vValue))
201                     hr = E_OUTOFMEMORY;
202             }
203
204             if(SUCCEEDED(hr))
205             {
206                 TRACE("storing category %d: %s\n", i, debugstr_w(V_BSTR(&vValue)));
207                 hr = IXMLDOMElement_setAttribute(categoryElement, bstrName, vValue);
208             }
209
210             SysFreeString(V_BSTR(&vValue));
211
212             if(SUCCEEDED(hr))
213             {
214                 for(j=0; j<MAX_STATS_PER_CATEGORY; ++j)
215                 {
216                     if(lstrlenW(stats->categories[i].stats[j].sName)==0)
217                         continue;
218
219                     V_VT(&vValue) = VT_INT;
220                     V_INT(&vValue) = NODE_ELEMENT;
221
222                     hr = IXMLDOMDocument_createNode(document, vValue, bstrStatistic, NULL, &statisticsNode);
223
224                     if(SUCCEEDED(hr))
225                         hr = IXMLDOMNode_QueryInterface(statisticsNode, &IID_IXMLDOMElement, (LPVOID*)&statisticsElement);
226
227                     V_INT(&vValue) = j;
228                     if(SUCCEEDED(hr))
229                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrIndex, vValue);
230
231                     if(SUCCEEDED(hr))
232                     {
233                         V_VT(&vValue) = VT_BSTR;
234                         V_BSTR(&vValue) = SysAllocString(stats->categories[i].stats[j].sName);
235                         if(!V_BSTR(&vValue))
236                             hr = E_OUTOFMEMORY;
237                     }
238
239                     if(SUCCEEDED(hr))
240                     {
241                         TRACE("    storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
242                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrName, vValue);
243                     }
244
245                     SysFreeString(V_BSTR(&vValue));
246
247                     if(SUCCEEDED(hr))
248                     {
249                         V_VT(&vValue) = VT_BSTR;
250                         V_BSTR(&vValue) = SysAllocString(stats->categories[i].stats[j].sValue);
251                         if(!V_BSTR(&vValue))
252                             hr = E_OUTOFMEMORY;
253                     }
254
255                     if(SUCCEEDED(hr))
256                     {
257                         TRACE("    storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
258                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrValue, vValue);
259                     }
260
261                     SysFreeString(V_BSTR(&vValue));
262
263                     if(SUCCEEDED(hr))
264                         hr = IXMLDOMElement_appendChild(categoryNode, statisticsNode, &statisticsNode);
265
266                     IXMLDOMElement_Release(statisticsElement);
267                     IXMLDOMNode_Release(statisticsNode);
268                 }
269             }
270
271             if(SUCCEEDED(hr))
272                 hr = IXMLDOMElement_appendChild(root, categoryNode, &categoryNode);
273
274             IXMLDOMElement_Release(categoryElement);
275             IXMLDOMNode_Release(categoryNode);
276
277             if(FAILED(hr))
278                 break;
279         }
280
281     if(SUCCEEDED(hr))
282         hr = IXMLDOMDocument_putref_documentElement(document, root);
283
284     IXMLDOMElement_Release(root);
285
286     TRACE("saving game statistics in %s file\n", debugstr_w(stats->sStatsFile));
287     if(SUCCEEDED(hr))
288         hr = GAMEUX_createStatsDirectory(stats->sStatsFile);
289
290     if(SUCCEEDED(hr))
291         hr = IXMLDOMDocument_save(document, vStatsFilePath);
292
293     IXMLDOMDocument_Release(document);
294
295     SysFreeString(bstrValue);
296     SysFreeString(bstrName);
297     SysFreeString(bstrStatistic);
298     SysFreeString(bstrIndex);
299     SysFreeString(bstrCategory);
300     SysFreeString(bstrStatistics);
301     SysFreeString(V_BSTR(&vStatsFilePath));
302     TRACE("ret=0x%x\n", hr);
303     return hr;
304 }
305 /*******************************************************************************
306  * GAMEUX_buildStatisticsFilePath
307  * Creates path to file containing statistics of game with given id.
308  *
309  * Parameters:
310  *  lpApplicationId                         [I]     application id of game,
311  *                                                  as string
312  *  lpStatisticsFile                        [O]     array where path will be
313  *                                                  stored. Its size must be
314  *                                                  at least MAX_PATH
315  */
316 static HRESULT GAMEUX_buildStatisticsFilePath(
317         LPCWSTR lpApplicationId,
318         LPWSTR lpStatisticsFile)
319 {
320     static const WCHAR sBackslash[] = {'\\',0};
321     static const WCHAR sStatisticsDir[] = {'\\','M','i','c','r','o','s','o','f','t',
322             '\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p',
323             'l','o','r','e','r','\\','G','a','m','e','S','t','a','t','i','s',
324             't','i','c','s','\\',0};
325     static const WCHAR sDotGamestats[] = {'.','g','a','m','e','s','t','a','t','s',0};
326
327     HRESULT hr;
328
329     hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, lpStatisticsFile);
330
331     if(SUCCEEDED(hr))
332     {
333         lstrcatW(lpStatisticsFile, sStatisticsDir);
334         lstrcatW(lpStatisticsFile, lpApplicationId);
335         lstrcatW(lpStatisticsFile, sBackslash);
336         lstrcatW(lpStatisticsFile, lpApplicationId);
337         lstrcatW(lpStatisticsFile, sDotGamestats);
338     }
339
340     return hr;
341 }
342 /*******************************************************************************
343  * GAMEUX_getAppIdFromGDFPath
344  *
345  * Loads application identifier associated with given GDF binary.
346  * Routine reads identifier from registry, so will fail if game
347  * is not registered.
348  *
349  * Parameters:
350  *  GDFBinaryPath                       [I]     path to gdf binary
351  *  lpApplicationId                     [O]     place to store application id.
352  *                                              must be at least 49 characters
353  *                                              to store guid and termination 0
354  */
355 static HRESULT GAMEUX_getAppIdFromGDFPath(
356         LPCWSTR GDFBinaryPath,
357         LPWSTR lpApplicationId)
358 {
359     static const WCHAR sApplicationId[] =
360             {'A','p','p','l','i','c','a','t','i','o','n','I','d',0};
361
362     HRESULT hr;
363     GAME_INSTALL_SCOPE installScope;
364     GUID instanceId;
365     LPWSTR lpRegistryPath = NULL;
366     HKEY hKey;
367     DWORD dwLength = 49*sizeof(WCHAR);/* place for GUID */
368
369     TRACE("(%s, %p)\n", debugstr_w(GDFBinaryPath), lpApplicationId);
370
371     if(!GDFBinaryPath)
372         return E_INVALIDARG;
373
374     installScope = GIS_CURRENT_USER;
375     hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
376
377     if(hr == S_FALSE)
378     {
379         installScope = GIS_ALL_USERS;
380         hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
381     }
382
383     if(hr == S_FALSE)
384         /* game not registered, so statistics cannot be used */
385         hr = E_FAIL;
386
387     if(SUCCEEDED(hr))
388         /* game is registered, let's read it's application id from registry */
389         hr = GAMEUX_buildGameRegistryPath(installScope, &instanceId, &lpRegistryPath);
390
391     if(SUCCEEDED(hr))
392         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE,
393                 lpRegistryPath, 0, KEY_READ | KEY_WOW64_64KEY, &hKey));
394
395     if(SUCCEEDED(hr))
396         hr = HRESULT_FROM_WIN32(RegGetValueW(hKey,
397                 NULL, sApplicationId, RRF_RT_REG_SZ,
398                 NULL, lpApplicationId, &dwLength));
399
400     HeapFree(GetProcessHeap(), 0, lpRegistryPath);
401     RegCloseKey(hKey);
402
403     TRACE("found app id: %s, return: %#x\n", debugstr_w(lpApplicationId), hr);
404     return hr;
405 }
406 /*******************************************************************
407  * GAMEUX_loadGameStatisticsFromFile
408  * Helper function, loads game statistics from file and stores them
409  * in the structure.
410  *
411  * Parameters:
412  *  data                [I/O]   structure containing file name to
413  *                              load and data fields to store data in
414  */
415 static HRESULT GAMEUX_loadStatisticsFromFile(struct GAMEUX_STATS *data)
416 {
417     static const WCHAR sStatistics[] = {'S','t','a','t','i','s','t','i','c','s',0};
418     static const WCHAR sCategory[] = {'C','a','t','e','g','o','r','y',0};
419     static const WCHAR sIndex[] = {'I','n','d','e','x',0};
420     static const WCHAR sStatistic[] = {'S','t','a','t','i','s','t','i','c',0};
421     static const WCHAR sName[] = {'N','a','m','e',0};
422     static const WCHAR sValue[] = {'V','a','l','u','e',0};
423
424     HRESULT hr = S_OK;
425     IXMLDOMDocument *document = NULL;
426     IXMLDOMElement *root = NULL, *categoryElement, *statisticElement;
427     IXMLDOMNode *categoryNode, *statisticNode;
428     IXMLDOMNodeList *rootChildren = NULL, *categoryChildren;
429     VARIANT vStatsFilePath, vValue;
430     BSTR bstrStatistics = NULL, bstrCategory = NULL, bstrIndex = NULL,
431         bstrStatistic = NULL, bstrName = NULL, bstrValue = NULL;
432     VARIANT_BOOL isSuccessful =  VARIANT_FALSE;
433     int i, j;
434
435     TRACE("(%p)\n", data);
436
437     V_VT(&vStatsFilePath) = VT_BSTR;
438     V_BSTR(&vStatsFilePath) = SysAllocString(data->sStatsFile);
439     if(!V_BSTR(&vStatsFilePath))
440         hr = E_OUTOFMEMORY;
441
442     if(SUCCEEDED(hr))
443         hr = CoCreateInstance(&CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&document);
444
445     if(SUCCEEDED(hr))
446     {
447         bstrStatistics = SysAllocString(sStatistics);
448         if(!bstrStatistics)
449             hr = E_OUTOFMEMORY;
450     }
451
452     if(SUCCEEDED(hr))
453     {
454         bstrCategory = SysAllocString(sCategory);
455         if(!bstrCategory)
456             hr = E_OUTOFMEMORY;
457     }
458
459     if(SUCCEEDED(hr))
460     {
461         bstrIndex = SysAllocString(sIndex);
462         if(!bstrIndex)
463             hr = E_OUTOFMEMORY;
464     }
465
466     if(SUCCEEDED(hr))
467     {
468         bstrStatistic = SysAllocString(sStatistic);
469         if(!bstrStatistic)
470             hr = E_OUTOFMEMORY;
471     }
472
473     if(SUCCEEDED(hr))
474     {
475         bstrName = SysAllocString(sName);
476         if(!bstrName)
477             hr = E_OUTOFMEMORY;
478     }
479
480     if(SUCCEEDED(hr))
481     {
482         bstrValue = SysAllocString(sValue);
483         if(!bstrValue)
484             hr = E_OUTOFMEMORY;
485     }
486
487     if(SUCCEEDED(hr))
488         hr = IXMLDOMDocument_load(document, vStatsFilePath, &isSuccessful);
489
490     if(hr == S_OK && isSuccessful != VARIANT_TRUE)
491         hr = S_FALSE;
492
493     if( hr == S_OK )
494         hr = IXMLDOMDocument_get_documentElement(document, &root);
495
496     if(hr == S_OK)
497         hr = IXMLDOMElement_get_childNodes(root, &rootChildren);
498
499     if(hr == S_OK)
500     {
501         hr = S_OK;
502         while(hr == S_OK)
503         {
504             hr = IXMLDOMNodeList_nextNode(rootChildren, &categoryNode);
505
506             if(hr == S_OK)
507             {
508                 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
509
510                 if(SUCCEEDED(hr))
511                 {
512                     hr = IXMLDOMElement_getAttribute(categoryElement, bstrIndex, &vValue);
513                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
514                         hr = E_FAIL;
515
516                     if(SUCCEEDED(hr))
517                     {
518                         i = StrToIntW(V_BSTR(&vValue));
519                         hr = IXMLDOMElement_getAttribute(categoryElement, bstrName, &vValue);
520                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
521                             hr = E_FAIL;
522                     }
523
524                     if(SUCCEEDED(hr))
525                     {
526                         lstrcpynW(data->categories[i].sName, V_BSTR(&vValue), MAX_CATEGORY_LENGTH);
527                         TRACE("category %d name %s\n", i, debugstr_w(data->categories[i].sName));
528                         hr = IXMLDOMElement_get_childNodes(categoryElement, &categoryChildren);
529                     }
530
531                     if(SUCCEEDED(hr))
532                     {
533                         hr = S_OK;
534                         while(hr == S_OK)
535                         {
536                             hr = IXMLDOMNodeList_nextNode(categoryChildren, &statisticNode);
537
538                             if(hr == S_OK)
539                             {
540                                 hr = IXMLDOMNode_QueryInterface(statisticNode, &IID_IXMLDOMElement, (LPVOID*)&statisticElement);
541
542                                 if(SUCCEEDED(hr))
543                                 {
544                                     hr = IXMLDOMElement_getAttribute(statisticElement, bstrIndex, &vValue);
545                                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
546                                         hr = E_FAIL;
547
548                                     if(SUCCEEDED(hr))
549                                     {
550                                         j = StrToIntW(V_BSTR(&vValue));
551                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrName, &vValue);
552                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
553                                             hr = E_FAIL;
554                                     }
555
556                                     if(SUCCEEDED(hr))
557                                     {
558                                         lstrcpynW(data->categories[i].stats[j].sName, V_BSTR(&vValue), MAX_NAME_LENGTH);
559                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrValue, &vValue);
560                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
561                                             hr = E_FAIL;
562                                     }
563
564                                     if(SUCCEEDED(hr))
565                                     {
566                                         lstrcpynW(data->categories[i].stats[j].sValue, V_BSTR(&vValue), MAX_VALUE_LENGTH);
567                                         TRACE("statistic %d name %s value %s\n", j,
568                                               debugstr_w(data->categories[i].stats[j].sName),
569                                               debugstr_w(data->categories[i].stats[j].sValue));
570                                     }
571                                     IXMLDOMElement_Release(statisticElement);
572                                 }
573
574                                 IXMLDOMNode_Release(statisticNode);
575                             }
576                         }
577
578                         if(SUCCEEDED(hr))
579                             hr = S_OK;
580                     }
581                     IXMLDOMElement_Release(categoryElement);
582                 }
583
584                 IXMLDOMNode_Release(categoryNode);
585             }
586         }
587         if(SUCCEEDED(hr))
588             hr = S_OK;
589     }
590
591     if(rootChildren) IXMLDOMNodeList_Release(rootChildren);
592     if(root) IXMLDOMElement_Release(root);
593     if(document) IXMLDOMDocument_Release(document);
594
595     SysFreeString(bstrValue);
596     SysFreeString(bstrName);
597     SysFreeString(bstrStatistic);
598     SysFreeString(bstrIndex);
599     SysFreeString(bstrCategory);
600     SysFreeString(bstrStatistics);
601     SysFreeString(V_BSTR(&vStatsFilePath));
602     return hr;
603 }
604 /*******************************************************************
605  * GAMEUX_loadGameStatistics
606  *
607  * Helper function which loads game statistics associated with game
608  * into interface's internal structures
609  *
610  * Parameters:
611  *  pStats              [O]     structure which will receive data
612  *  sGameId             [I]     application instance Id, stored as string
613  *                              to avoid additional conversions
614  *  openType            [I]     allowed ways of opening statistics
615  *  pOpenResult         [O]     way used to open statistics
616  *
617  */
618 static HRESULT GAMEUX_loadGameStatistics(struct GAMEUX_STATS *pStats,
619         LPWSTR sGameId,
620         GAMESTATS_OPEN_TYPE openType,
621         GAMESTATS_OPEN_RESULT* pOpenResult)
622 {
623     HRESULT hr;
624     TRACE("(%p, %s, %d, %p)\n", pStats, debugstr_w(sGameId), openType, pOpenResult);
625
626     hr = GAMEUX_buildStatisticsFilePath(sGameId, pStats->sStatsFile);
627
628     hr = GAMEUX_loadStatisticsFromFile(pStats);
629     TRACE("ldstats finished, res: %#x\n", hr);
630     if(hr == S_OK)
631     {
632         *pOpenResult = GAMESTATS_OPEN_OPENED;
633     }
634     else if(hr == S_FALSE && openType == GAMESTATS_OPEN_OPENORCREATE) /* file does not exist */
635     {
636         /* create new statistics, not yet connected with file */
637         ZeroMemory(pStats->categories, sizeof(pStats->categories));
638         *pOpenResult = GAMESTATS_OPEN_CREATED;
639         hr = S_OK;
640     }
641     else
642         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
643
644     TRACE("openResult=%#x ret=%#x\n", *pOpenResult, hr);
645     return hr;
646 }
647  /*******************************************************************
648  * IGameStatistics implementation
649  */
650 typedef struct _GameStatisticsImpl
651 {
652     IGameStatistics IGameStatistics_iface;
653     LONG ref;
654     struct GAMEUX_STATS stats;
655 } GameStatisticsImpl;
656
657 static inline GameStatisticsImpl *impl_from_IGameStatistics( IGameStatistics *iface )
658 {
659     return CONTAINING_RECORD(iface, GameStatisticsImpl, IGameStatistics_iface);
660 }
661
662 static HRESULT WINAPI GameStatisticsImpl_QueryInterface(
663         IGameStatistics *iface,
664         REFIID riid,
665         void **ppvObject)
666 {
667     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
668
669     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
670
671     *ppvObject = NULL;
672
673     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
674          IsEqualGUID( riid, &IID_IGameStatistics ) )
675     {
676         *ppvObject = iface;
677     }
678     else
679     {
680         FIXME("interface %s not implemented\n", debugstr_guid(riid));
681         return E_NOINTERFACE;
682     }
683
684     IGameStatistics_AddRef( iface );
685     return S_OK;
686 }
687
688 static ULONG WINAPI GameStatisticsImpl_AddRef(IGameStatistics *iface)
689 {
690     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
691     LONG ref;
692
693     ref = InterlockedIncrement(&This->ref);
694
695     TRACE("(%p): ref=%d\n", This, ref);
696     return ref;
697 }
698
699 static ULONG WINAPI GameStatisticsImpl_Release(IGameStatistics *iface)
700 {
701     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
702     LONG ref;
703
704     ref = InterlockedDecrement( &This->ref );
705     TRACE("(%p): ref=%d\n", This, ref);
706
707     if ( ref == 0 )
708     {
709         TRACE("freeing IGameStatistics\n");
710         HeapFree( GetProcessHeap(), 0, This );
711     }
712
713     return ref;
714 }
715
716 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategoryLength(
717     IGameStatistics *iface,
718     UINT *cch)
719 {
720     TRACE("(%p, %p)\n", iface, cch);
721     if(!cch)
722         return E_INVALIDARG;
723
724     *cch = MAX_CATEGORY_LENGTH;
725     return S_OK;
726 }
727
728 static HRESULT WINAPI GameStatisticsImpl_GetMaxNameLength(
729     IGameStatistics *iface,
730     UINT *cch)
731 {
732     TRACE("(%p, %p)\n", iface, cch);
733     if(!cch)
734         return E_INVALIDARG;
735
736     *cch = MAX_NAME_LENGTH;
737     return S_OK;
738 }
739
740 static HRESULT WINAPI GameStatisticsImpl_GetMaxValueLength(
741     IGameStatistics *iface,
742     UINT *cch)
743 {
744     TRACE("(%p, %p)\n", iface, cch);
745     if(!cch)
746         return E_INVALIDARG;
747
748     *cch = MAX_VALUE_LENGTH;
749     return S_OK;
750 }
751
752 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategories(
753     IGameStatistics *iface,
754     WORD *pMax)
755 {
756     TRACE("(%p, %p)\n", iface, pMax);
757     if(!pMax)
758         return E_INVALIDARG;
759
760     *pMax = MAX_CATEGORIES;
761     return S_OK;
762 }
763
764 static HRESULT WINAPI GameStatisticsImpl_GetMaxStatsPerCategory(
765     IGameStatistics *iface,
766     WORD *pMax)
767 {
768     TRACE("(%p, %p)\n", iface, pMax);
769     if(!pMax)
770         return E_INVALIDARG;
771
772     *pMax = MAX_STATS_PER_CATEGORY;
773     return S_OK;
774 }
775
776 static HRESULT WINAPI GameStatisticsImpl_SetCategoryTitle(
777     IGameStatistics *iface,
778     WORD categoryIndex,
779     LPCWSTR title)
780 {
781     HRESULT hr = S_OK;
782     DWORD dwLength;
783     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
784
785     TRACE("(%p, %d, %s)\n", This, categoryIndex, debugstr_w(title));
786
787     if(!title || categoryIndex >= MAX_CATEGORIES)
788         return E_INVALIDARG;
789
790     dwLength = lstrlenW(title);
791
792     if(dwLength > MAX_CATEGORY_LENGTH)
793     {
794         hr = S_FALSE;
795         dwLength = MAX_CATEGORY_LENGTH;
796     }
797
798     lstrcpynW(This->stats.categories[categoryIndex].sName,
799               title, dwLength+1);
800
801     return hr;
802 }
803
804 static HRESULT WINAPI GameStatisticsImpl_GetCategoryTitle(
805     IGameStatistics *iface,
806     WORD categoryIndex,
807     LPWSTR *pTitle)
808 {
809     HRESULT hr = S_OK;
810     LONG nLength;
811     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
812
813     TRACE("%p, %d, %p\n", This, categoryIndex, pTitle);
814
815     if(!pTitle)
816         return E_INVALIDARG;
817     *pTitle = NULL;
818
819     if (categoryIndex >= MAX_CATEGORIES)
820         hr = E_INVALIDARG;
821
822     if(SUCCEEDED(hr))
823     {
824         nLength = lstrlenW(This->stats.categories[categoryIndex].sName);
825         if(nLength != 0)
826         {
827             *pTitle = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
828             lstrcpyW(*pTitle, This->stats.categories[categoryIndex].sName);
829         }
830     }
831
832     return hr;
833 }
834
835 static HRESULT WINAPI GameStatisticsImpl_GetStatistic(
836     IGameStatistics *iface,
837     WORD categoryIndex,
838     WORD statIndex,
839     LPWSTR *pName,
840     LPWSTR *pValue)
841 {
842     HRESULT hr = S_OK;
843     LONG nLength;
844     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
845
846     TRACE("%p, %d,%d, %p, %p\n", This, categoryIndex, statIndex, pName, pValue);
847
848     if(!pName || !pValue)
849         return E_INVALIDARG;
850
851     *pName = NULL;
852     *pValue = NULL;
853
854     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
855         hr = E_INVALIDARG;
856
857     if(SUCCEEDED(hr))
858     {
859         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sName);
860         if(nLength != 0)
861         {
862             *pName = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
863             if(!(*pName))
864                 hr = E_OUTOFMEMORY;
865             else
866                 lstrcpyW(*pName, This->stats.categories[categoryIndex].stats[statIndex].sName);
867         }
868     }
869
870     if(SUCCEEDED(hr))
871     {
872         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sValue);
873         if(nLength != 0)
874         {
875             *pValue = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
876             if(!(*pValue))
877                 hr = E_OUTOFMEMORY;
878             else
879                 lstrcpyW(*pValue, This->stats.categories[categoryIndex].stats[statIndex].sValue);
880         }
881     }
882
883     TRACE("returning pair; %s => %s\n", debugstr_w(*pName), debugstr_w(*pValue));
884     return hr;
885 }
886
887 static HRESULT WINAPI GameStatisticsImpl_SetStatistic(
888     IGameStatistics *iface,
889     WORD categoryIndex,
890     WORD statIndex,
891     LPCWSTR name,
892     LPCWSTR value)
893 {
894     HRESULT hr = S_OK;
895     DWORD dwNameLen, dwValueLen;
896     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
897
898     TRACE("(%p, %d, %d, %s, %s)\n", This, categoryIndex, statIndex,
899           debugstr_w(name), debugstr_w(value));
900
901     if(!name)
902         return S_FALSE;
903
904     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
905         return E_INVALIDARG;
906
907     dwNameLen = lstrlenW(name);
908
909     if(dwNameLen > MAX_NAME_LENGTH)
910     {
911         hr = S_FALSE;
912         dwNameLen = MAX_NAME_LENGTH;
913     }
914
915     lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sName,
916               name, dwNameLen+1);
917
918     if(value)
919     {
920         dwValueLen = lstrlenW(value);
921
922         if(dwValueLen > MAX_VALUE_LENGTH)
923         {
924             hr = S_FALSE;
925             dwValueLen = MAX_VALUE_LENGTH;
926         }
927
928         lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sValue,
929                   value, dwValueLen+1);
930     }
931     else
932         /* Windows allows to pass NULL as value */
933         This->stats.categories[categoryIndex].stats[statIndex].sValue[0] = 0;
934
935     return hr;
936 }
937
938 static HRESULT WINAPI GameStatisticsImpl_Save(
939     IGameStatistics *iface,
940     BOOL trackChanges)
941 {
942     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
943     HRESULT hr = S_OK;
944
945     TRACE("(%p, %d)\n", This, trackChanges);
946
947     if(trackChanges)
948         FIXME("tracking changes not yet implemented\n");
949
950     hr = GAMEUX_updateStatisticsFile(&This->stats);
951
952     return hr;
953 }
954
955 static HRESULT WINAPI GameStatisticsImpl_SetLastPlayedCategory(
956     IGameStatistics *iface,
957     UINT categoryIndex)
958 {
959     FIXME("stub\n");
960     return E_NOTIMPL;
961 }
962
963 static HRESULT WINAPI GameStatisticsImpl_GetLastPlayedCategory(
964     IGameStatistics *iface,
965     UINT *pCategoryIndex)
966 {
967     FIXME("stub\n");
968     return E_NOTIMPL;
969 }
970
971 static const struct IGameStatisticsVtbl GameStatisticsImplVtbl =
972 {
973     GameStatisticsImpl_QueryInterface,
974     GameStatisticsImpl_AddRef,
975     GameStatisticsImpl_Release,
976     GameStatisticsImpl_GetMaxCategoryLength,
977     GameStatisticsImpl_GetMaxNameLength,
978     GameStatisticsImpl_GetMaxValueLength,
979     GameStatisticsImpl_GetMaxCategories,
980     GameStatisticsImpl_GetMaxStatsPerCategory,
981     GameStatisticsImpl_SetCategoryTitle,
982     GameStatisticsImpl_GetCategoryTitle,
983     GameStatisticsImpl_GetStatistic,
984     GameStatisticsImpl_SetStatistic,
985     GameStatisticsImpl_Save,
986     GameStatisticsImpl_SetLastPlayedCategory,
987     GameStatisticsImpl_GetLastPlayedCategory
988 };
989
990
991 static HRESULT create_IGameStatistics(GameStatisticsImpl** ppStats)
992 {
993     TRACE("(%p)\n", ppStats);
994
995     *ppStats = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**ppStats));
996     if(!(*ppStats))
997         return E_OUTOFMEMORY;
998
999     (*ppStats)->IGameStatistics_iface.lpVtbl = &GameStatisticsImplVtbl;
1000     (*ppStats)->ref = 1;
1001
1002     TRACE("returning coclass: %p\n", *ppStats);
1003     return S_OK;
1004 }
1005
1006 /*******************************************************************************
1007  * IGameStatisticsMgr implementation
1008  */
1009 typedef struct _GameStatisticsMgrImpl
1010 {
1011     IGameStatisticsMgr IGameStatisticsMgr_iface;
1012     LONG ref;
1013 } GameStatisticsMgrImpl;
1014
1015 static inline GameStatisticsMgrImpl *impl_from_IGameStatisticsMgr( IGameStatisticsMgr *iface )
1016 {
1017     return CONTAINING_RECORD(iface, GameStatisticsMgrImpl, IGameStatisticsMgr_iface);
1018 }
1019
1020
1021 static HRESULT WINAPI GameStatisticsMgrImpl_QueryInterface(
1022         IGameStatisticsMgr *iface,
1023         REFIID riid,
1024         void **ppvObject)
1025 {
1026     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1027
1028     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
1029
1030     *ppvObject = NULL;
1031
1032     if(IsEqualGUID(riid, &IID_IUnknown) ||
1033        IsEqualGUID(riid, &IID_IGameStatisticsMgr) )
1034     {
1035         *ppvObject = iface;
1036     }
1037     else
1038     {
1039         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1040         return E_NOINTERFACE;
1041     }
1042
1043     IGameStatisticsMgr_AddRef( iface );
1044     return S_OK;
1045 }
1046
1047 static ULONG WINAPI GameStatisticsMgrImpl_AddRef(IGameStatisticsMgr *iface)
1048 {
1049     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1050     LONG ref;
1051
1052     ref = InterlockedIncrement(&This->ref);
1053
1054     TRACE("(%p): ref=%d\n", This, ref);
1055     return ref;
1056 }
1057
1058 static ULONG WINAPI GameStatisticsMgrImpl_Release(IGameStatisticsMgr *iface)
1059 {
1060     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1061     LONG ref;
1062
1063     ref = InterlockedDecrement(&This->ref);
1064     TRACE("(%p): ref=%d\n", This, ref);
1065
1066     if ( ref == 0 )
1067     {
1068         TRACE("freeing GameStatistics object\n");
1069         HeapFree( GetProcessHeap(), 0, This);
1070     }
1071
1072     return ref;
1073 }
1074
1075 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_GetGameStatistics(
1076         IGameStatisticsMgr* iface,
1077         LPCWSTR GDFBinaryPath,
1078         GAMESTATS_OPEN_TYPE openType,
1079         GAMESTATS_OPEN_RESULT *pOpenResult,
1080         IGameStatistics **ppiStats)
1081 {
1082     HRESULT hr;
1083     WCHAR lpApplicationId[49];
1084     GameStatisticsImpl *statisticsImpl = NULL;
1085     IGameStatistics *output_iface;
1086
1087     TRACE("(%p, %s, 0x%x, %p, %p)\n", iface, debugstr_w(GDFBinaryPath), openType, pOpenResult, ppiStats);
1088
1089     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1090
1091     if(SUCCEEDED(hr))
1092         hr = create_IGameStatistics(&statisticsImpl);
1093
1094     if(SUCCEEDED(hr))
1095     {
1096         output_iface = &statisticsImpl->IGameStatistics_iface;
1097         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, statisticsImpl->stats.sStatsFile);
1098     }
1099
1100     if(SUCCEEDED(hr))
1101         hr = GAMEUX_loadGameStatistics(&statisticsImpl->stats, lpApplicationId, openType, pOpenResult);
1102
1103     if(SUCCEEDED(hr))
1104         *ppiStats = output_iface;
1105     else
1106     {
1107         HeapFree(GetProcessHeap(), 0, statisticsImpl);
1108         *ppiStats = NULL;
1109     }
1110
1111     return hr;
1112 }
1113
1114 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_RemoveGameStatistics(
1115         IGameStatisticsMgr* iface,
1116         LPCWSTR GDFBinaryPath)
1117 {
1118     HRESULT hr;
1119     WCHAR lpApplicationId[49];
1120     WCHAR sStatsFile[MAX_PATH];
1121
1122     TRACE("(%p, %s)\n", iface, debugstr_w(GDFBinaryPath));
1123
1124     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1125
1126     if(SUCCEEDED(hr))
1127         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, sStatsFile);
1128
1129     if(SUCCEEDED(hr))
1130         hr = (DeleteFileW(sStatsFile)==TRUE ? S_OK : HRESULT_FROM_WIN32(GetLastError()));
1131
1132     return hr;
1133 }
1134
1135 static const struct IGameStatisticsMgrVtbl GameStatisticsMgrImplVtbl =
1136 {
1137     GameStatisticsMgrImpl_QueryInterface,
1138     GameStatisticsMgrImpl_AddRef,
1139     GameStatisticsMgrImpl_Release,
1140     GameStatisticsMgrImpl_GetGameStatistics,
1141     GameStatisticsMgrImpl_RemoveGameStatistics,
1142 };
1143
1144 HRESULT GameStatistics_create(
1145         IUnknown *pUnkOuter,
1146         IUnknown **ppObj)
1147 {
1148     GameStatisticsMgrImpl *pGameStatistics;
1149
1150     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1151
1152     pGameStatistics = HeapAlloc( GetProcessHeap(), 0, sizeof (*pGameStatistics) );
1153
1154     if( !pGameStatistics )
1155         return E_OUTOFMEMORY;
1156
1157     pGameStatistics->IGameStatisticsMgr_iface.lpVtbl = &GameStatisticsMgrImplVtbl;
1158     pGameStatistics->ref = 1;
1159
1160     *ppObj = (IUnknown*)&pGameStatistics->IGameStatisticsMgr_iface;
1161
1162     TRACE("returning iface %p\n", *ppObj);
1163     return S_OK;
1164 }