include/msvcrt: Define more CPU control word flags.
[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
180     if(SUCCEEDED(hr))
181         for(i=0; i<MAX_CATEGORIES; ++i)
182         {
183             if(lstrlenW(stats->categories[i].sName)==0)
184                 continue;
185
186             V_VT(&vValue) = VT_INT;
187             V_INT(&vValue) = NODE_ELEMENT;
188
189             hr = IXMLDOMDocument_createNode(document, vValue, bstrCategory, NULL, &categoryNode);
190
191             if(SUCCEEDED(hr))
192                 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
193
194             V_INT(&vValue) = i;
195             if(SUCCEEDED(hr))
196                 hr = IXMLDOMElement_setAttribute(categoryElement, bstrIndex, vValue);
197
198             if(SUCCEEDED(hr))
199             {
200                 V_VT(&vValue) = VT_BSTR;
201                 V_BSTR(&vValue) = SysAllocString(stats->categories[i].sName);
202                 if(!V_BSTR(&vValue))
203                     hr = E_OUTOFMEMORY;
204             }
205
206             if(SUCCEEDED(hr))
207             {
208                 TRACE("storing category %d: %s\n", i, debugstr_w(V_BSTR(&vValue)));
209                 hr = IXMLDOMElement_setAttribute(categoryElement, bstrName, vValue);
210             }
211
212             SysFreeString(V_BSTR(&vValue));
213
214             if(SUCCEEDED(hr))
215             {
216                 for(j=0; j<MAX_STATS_PER_CATEGORY; ++j)
217                 {
218                     if(lstrlenW(stats->categories[i].stats[j].sName)==0)
219                         continue;
220
221                     V_VT(&vValue) = VT_INT;
222                     V_INT(&vValue) = NODE_ELEMENT;
223
224                     hr = IXMLDOMDocument_createNode(document, vValue, bstrStatistic, NULL, &statisticsNode);
225
226                     if(SUCCEEDED(hr))
227                         hr = IXMLDOMNode_QueryInterface(statisticsNode, &IID_IXMLDOMElement, (LPVOID*)&statisticsElement);
228
229                     V_INT(&vValue) = j;
230                     if(SUCCEEDED(hr))
231                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrIndex, vValue);
232
233                     if(SUCCEEDED(hr))
234                     {
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;
239                     }
240
241                     if(SUCCEEDED(hr))
242                     {
243                         TRACE("    storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
244                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrName, vValue);
245                     }
246
247                     SysFreeString(V_BSTR(&vValue));
248
249                     if(SUCCEEDED(hr))
250                     {
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;
255                     }
256
257                     if(SUCCEEDED(hr))
258                     {
259                         TRACE("    storing statistic %d: name: %s\n", j, debugstr_w(V_BSTR(&vValue)));
260                         hr = IXMLDOMElement_setAttribute(statisticsElement, bstrValue, vValue);
261                     }
262
263                     SysFreeString(V_BSTR(&vValue));
264
265                     if(SUCCEEDED(hr))
266                         hr = IXMLDOMElement_appendChild(categoryNode, statisticsNode, &statisticsNode);
267
268                     IXMLDOMElement_Release(statisticsElement);
269                     IXMLDOMNode_Release(statisticsNode);
270                 }
271             }
272
273             if(SUCCEEDED(hr))
274                 hr = IXMLDOMElement_appendChild(root, categoryNode, &categoryNode);
275
276             IXMLDOMElement_Release(categoryElement);
277             IXMLDOMNode_Release(categoryNode);
278
279             if(FAILED(hr))
280                 break;
281         }
282
283     if(SUCCEEDED(hr))
284         hr = IXMLDOMDocument_putref_documentElement(document, root);
285
286     IXMLDOMElement_Release(root);
287
288     TRACE("saving game statistics in %s file\n", debugstr_w(stats->sStatsFile));
289     if(SUCCEEDED(hr))
290         hr = GAMEUX_createStatsDirectory(stats->sStatsFile);
291
292     if(SUCCEEDED(hr))
293         hr = IXMLDOMDocument_save(document, vStatsFilePath);
294
295     IXMLDOMDocument_Release(document);
296
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;
306 }
307 /*******************************************************************************
308  * GAMEUX_buildStatisticsFilePath
309  * Creates path to file contaning statistics of game with given id.
310  *
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
317  */
318 static HRESULT GAMEUX_buildStatisticsFilePath(
319         LPCWSTR lpApplicationId,
320         LPWSTR lpStatisticsFile)
321 {
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};
328
329     HRESULT hr;
330
331     hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, lpStatisticsFile);
332
333     if(SUCCEEDED(hr))
334     {
335         lstrcatW(lpStatisticsFile, sStatisticsDir);
336         lstrcatW(lpStatisticsFile, lpApplicationId);
337         lstrcatW(lpStatisticsFile, sBackslash);
338         lstrcatW(lpStatisticsFile, lpApplicationId);
339         lstrcatW(lpStatisticsFile, sDotGamestats);
340     }
341
342     return hr;
343 }
344 /*******************************************************************************
345  * GAMEUX_getAppIdFromGDFPath
346  *
347  * Loads application identifier associated with given GDF binary.
348  * Routine reads identifier from registry, so will fail if game
349  * is not registered.
350  *
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
356  */
357 static HRESULT GAMEUX_getAppIdFromGDFPath(
358         LPCWSTR GDFBinaryPath,
359         LPWSTR lpApplicationId)
360 {
361     static const WCHAR sApplicationId[] =
362             {'A','p','p','l','i','c','a','t','i','o','n','I','d',0};
363
364     HRESULT hr;
365     GAME_INSTALL_SCOPE installScope;
366     GUID instanceId;
367     LPWSTR lpRegistryPath;
368     DWORD dwLength = 49*sizeof(WCHAR);/* place for GUID */
369
370     TRACE("(%s, %p)\n", debugstr_w(GDFBinaryPath), lpApplicationId);
371
372     if(!GDFBinaryPath)
373         return E_INVALIDARG;
374
375     installScope = GIS_CURRENT_USER;
376     hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
377
378     if(hr == S_FALSE)
379     {
380         installScope = GIS_ALL_USERS;
381         hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
382     }
383
384     if(hr == S_FALSE)
385         /* game not registered, so statistics cannot be used */
386         hr = E_FAIL;
387
388     if(SUCCEEDED(hr))
389         /* game is registered, let's read it's application id from registry */
390         hr = GAMEUX_buildGameRegistryPath(installScope, &instanceId, &lpRegistryPath);
391
392     if(SUCCEEDED(hr))
393         hr = HRESULT_FROM_WIN32(RegGetValueW(HKEY_LOCAL_MACHINE,
394                 lpRegistryPath, sApplicationId, RRF_RT_REG_SZ,
395                 NULL, lpApplicationId, &dwLength));
396
397     HeapFree(GetProcessHeap(), 0, lpRegistryPath);
398
399     TRACE("found app id: %s, return: %#x\n", debugstr_w(lpApplicationId), hr);
400     return hr;
401 }
402 /*******************************************************************
403  * GAMEUX_loadGameStatisticsFromFile
404  * Helper function, loads game statistics from file and stores them
405  * in the structure.
406  *
407  * Parameters:
408  *  data                [I/O]   structure containing file name to
409  *                              load and data fields to store data in
410  */
411 static HRESULT GAMEUX_loadStatisticsFromFile(struct GAMEUX_STATS *data)
412 {
413     static const WCHAR sStatistics[] = {'S','t','a','t','i','s','t','i','c','s',0};
414     static const WCHAR sCategory[] = {'C','a','t','e','g','o','r','y',0};
415     static const WCHAR sIndex[] = {'I','n','d','e','x',0};
416     static const WCHAR sStatistic[] = {'S','t','a','t','i','s','t','i','c',0};
417     static const WCHAR sName[] = {'N','a','m','e',0};
418     static const WCHAR sValue[] = {'V','a','l','u','e',0};
419
420     HRESULT hr = S_OK;
421     IXMLDOMDocument *document = NULL;
422     IXMLDOMElement *root = NULL, *categoryElement, *statisticElement;
423     IXMLDOMNode *categoryNode, *statisticNode;
424     IXMLDOMNodeList *rootChildren = NULL, *categoryChildren;
425     VARIANT vStatsFilePath, vValue;
426     BSTR bstrStatistics = NULL, bstrCategory = NULL, bstrIndex = NULL,
427         bstrStatistic = NULL, bstrName = NULL, bstrValue = NULL;
428     VARIANT_BOOL isSuccessful =  VARIANT_FALSE;
429     int i, j;
430
431     TRACE("(%p)\n", data);
432
433     V_VT(&vStatsFilePath) = VT_BSTR;
434     V_BSTR(&vStatsFilePath) = SysAllocString(data->sStatsFile);
435     if(!V_BSTR(&vStatsFilePath))
436         hr = E_OUTOFMEMORY;
437
438     if(SUCCEEDED(hr))
439         hr = CoCreateInstance(&CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&document);
440
441     if(SUCCEEDED(hr))
442     {
443         bstrStatistics = SysAllocString(sStatistics);
444         if(!bstrStatistics)
445             hr = E_OUTOFMEMORY;
446     }
447
448     if(SUCCEEDED(hr))
449     {
450         bstrCategory = SysAllocString(sCategory);
451         if(!bstrCategory)
452             hr = E_OUTOFMEMORY;
453     }
454
455     if(SUCCEEDED(hr))
456     {
457         bstrIndex = SysAllocString(sIndex);
458         if(!bstrIndex)
459             hr = E_OUTOFMEMORY;
460     }
461
462     if(SUCCEEDED(hr))
463     {
464         bstrStatistic = SysAllocString(sStatistic);
465         if(!bstrStatistic)
466             hr = E_OUTOFMEMORY;
467     }
468
469     if(SUCCEEDED(hr))
470     {
471         bstrName = SysAllocString(sName);
472         if(!bstrName)
473             hr = E_OUTOFMEMORY;
474     }
475
476     if(SUCCEEDED(hr))
477     {
478         bstrValue = SysAllocString(sValue);
479         if(!bstrValue)
480             hr = E_OUTOFMEMORY;
481     }
482
483     if(SUCCEEDED(hr))
484         hr = IXMLDOMDocument_load(document, vStatsFilePath, &isSuccessful);
485
486     if(hr == S_OK && isSuccessful != VARIANT_TRUE)
487         hr = S_FALSE;
488
489     if( hr == S_OK )
490         hr = IXMLDOMDocument_get_documentElement(document, &root);
491
492     if(hr == S_OK)
493         hr = IXMLDOMElement_get_childNodes(root, &rootChildren);
494
495     if(hr == S_OK)
496     {
497         hr = S_OK;
498         while(hr == S_OK)
499         {
500             hr = IXMLDOMNodeList_nextNode(rootChildren, &categoryNode);
501
502             if(hr == S_OK)
503             {
504                 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
505
506                 if(SUCCEEDED(hr))
507                 {
508                     hr = IXMLDOMElement_getAttribute(categoryElement, bstrIndex, &vValue);
509                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
510                         hr = E_FAIL;
511
512                     if(SUCCEEDED(hr))
513                     {
514                         i = StrToIntW(V_BSTR(&vValue));
515                         hr = IXMLDOMElement_getAttribute(categoryElement, bstrName, &vValue);
516                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
517                             hr = E_FAIL;
518                     }
519
520                     if(SUCCEEDED(hr))
521                     {
522                         lstrcpynW(data->categories[i].sName, V_BSTR(&vValue), MAX_CATEGORY_LENGTH);
523                         TRACE("category %d name %s\n", i, debugstr_w(data->categories[i].sName));
524                         hr = IXMLDOMElement_get_childNodes(categoryElement, &categoryChildren);
525                     }
526
527                     if(SUCCEEDED(hr))
528                     {
529                         hr = S_OK;
530                         while(hr == S_OK)
531                         {
532                             hr = IXMLDOMNodeList_nextNode(categoryChildren, &statisticNode);
533
534                             if(hr == S_OK)
535                             {
536                                 hr = IXMLDOMNode_QueryInterface(statisticNode, &IID_IXMLDOMElement, (LPVOID*)&statisticElement);
537
538                                 if(SUCCEEDED(hr))
539                                 {
540                                     hr = IXMLDOMElement_getAttribute(statisticElement, bstrIndex, &vValue);
541                                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
542                                         hr = E_FAIL;
543
544                                     if(SUCCEEDED(hr))
545                                     {
546                                         j = StrToIntW(V_BSTR(&vValue));
547                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrName, &vValue);
548                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
549                                             hr = E_FAIL;
550                                     }
551
552                                     if(SUCCEEDED(hr))
553                                     {
554                                         lstrcpynW(data->categories[i].stats[j].sName, V_BSTR(&vValue), MAX_NAME_LENGTH);
555                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrValue, &vValue);
556                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
557                                             hr = E_FAIL;
558                                     }
559
560                                     if(SUCCEEDED(hr))
561                                         lstrcpynW(data->categories[i].stats[j].sValue, V_BSTR(&vValue), MAX_VALUE_LENGTH);
562
563                                     TRACE("  statistic %d name %s value %s\n", j,
564                                             debugstr_w(data->categories[i].stats[j].sName),
565                                             debugstr_w(data->categories[i].stats[j].sValue));
566                                     IXMLDOMElement_Release(statisticElement);
567                                 }
568
569                                 IXMLDOMNode_Release(statisticNode);
570                             }
571                         }
572
573                         if(SUCCEEDED(hr))
574                             hr = S_OK;
575                     }
576                     IXMLDOMElement_Release(categoryElement);
577                 }
578
579                 IXMLDOMNode_Release(categoryNode);
580             }
581         }
582         if(SUCCEEDED(hr))
583             hr = S_OK;
584     }
585
586     if(rootChildren) IXMLDOMNodeList_Release(rootChildren);
587     if(root) IXMLDOMElement_Release(root);
588     if(document) IXMLDOMDocument_Release(document);
589
590     SysFreeString(bstrValue);
591     SysFreeString(bstrName);
592     SysFreeString(bstrStatistic);
593     SysFreeString(bstrIndex);
594     SysFreeString(bstrCategory);
595     SysFreeString(bstrStatistics);
596     SysFreeString(V_BSTR(&vStatsFilePath));
597     return hr;
598 }
599 /*******************************************************************
600  * GAMEUX_loadGameStatistics
601  *
602  * Helper function which loads game statistics associated with game
603  * into interface's internal structures
604  *
605  * Parameters:
606  *  pStats              [O]     structure which will receive data
607  *  sGameId             [I]     application instance Id, stored as string
608  *                              to avoid additional conversions
609  *  openType            [I]     allowed ways of opening statistics
610  *  pOpenResult         [O]     way used to open statistics
611  *
612  */
613 static HRESULT GAMEUX_loadGameStatistics(struct GAMEUX_STATS *pStats,
614         LPWSTR sGameId,
615         GAMESTATS_OPEN_TYPE openType,
616         GAMESTATS_OPEN_RESULT* pOpenResult)
617 {
618     HRESULT hr;
619     TRACE("(%p, %s, %d, %p)\n", pStats, debugstr_w(sGameId), openType, pOpenResult);
620
621     hr = GAMEUX_buildStatisticsFilePath(sGameId, pStats->sStatsFile);
622
623     hr = GAMEUX_loadStatisticsFromFile(pStats);
624     TRACE("ldstats finished, res: %#x\n", hr);
625     if(hr == S_OK)
626     {
627         *pOpenResult = GAMESTATS_OPEN_OPENED;
628     }
629     else if(hr == S_FALSE && openType == GAMESTATS_OPEN_OPENORCREATE) /* file does not exist */
630     {
631         /* create new statitics, not yet connected with file */
632         ZeroMemory(pStats->categories, sizeof(pStats->categories));
633         *pOpenResult = GAMESTATS_OPEN_CREATED;
634         hr = S_OK;
635     }
636     else
637         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
638
639     TRACE("openResult=%#x ret=%#x\n", *pOpenResult, hr);
640     return hr;
641 }
642  /*******************************************************************
643  * IGameStatistics implementation
644  */
645 typedef struct _GameStatisticsImpl
646 {
647     const struct IGameStatisticsVtbl *lpVtbl;
648     LONG ref;
649     struct GAMEUX_STATS stats;
650 } GameStatisticsImpl;
651
652 static inline GameStatisticsImpl *impl_from_IGameStatistics( IGameStatistics *iface )
653 {
654     return (GameStatisticsImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsImpl, lpVtbl));
655 }
656 static inline IGameStatistics *IGameStatistics_from_impl( GameStatisticsImpl* This )
657 {
658     return (struct IGameStatistics*)&This->lpVtbl;
659 }
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     *pTitle = NULL;
816
817     if(!pTitle || categoryIndex >= MAX_CATEGORIES)
818         hr = E_INVALIDARG;
819
820
821     if(SUCCEEDED(hr))
822     {
823         nLength = lstrlenW(This->stats.categories[categoryIndex].sName);
824         if(nLength != 0)
825         {
826             *pTitle = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
827             lstrcpyW(*pTitle, This->stats.categories[categoryIndex].sName);
828         }
829     }
830
831     return hr;
832 }
833
834 static HRESULT WINAPI GameStatisticsImpl_GetStatistic(
835     IGameStatistics *iface,
836     WORD categoryIndex,
837     WORD statIndex,
838     LPWSTR *pName,
839     LPWSTR *pValue)
840 {
841     HRESULT hr = S_OK;
842     LONG nLength;
843     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
844
845     TRACE("%p, %d,%d, %p, %p\n", This, categoryIndex, statIndex, pName, pValue);
846
847     if(!pName || !pValue)
848         return E_INVALIDARG;
849
850     *pName = NULL;
851     *pValue = NULL;
852
853     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
854         hr = E_INVALIDARG;
855
856     if(SUCCEEDED(hr))
857     {
858         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sName);
859         if(nLength != 0)
860         {
861             *pName = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
862             if(!(*pName))
863                 hr = E_OUTOFMEMORY;
864             else
865                 lstrcpyW(*pName, This->stats.categories[categoryIndex].stats[statIndex].sName);
866         }
867     }
868
869     if(SUCCEEDED(hr))
870     {
871         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sValue);
872         if(nLength != 0)
873         {
874             *pValue = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
875             if(!(*pValue))
876                 hr = E_OUTOFMEMORY;
877             else
878                 lstrcpyW(*pValue, This->stats.categories[categoryIndex].stats[statIndex].sValue);
879         }
880     }
881
882     TRACE("returning pair; %s => %s\n", debugstr_w(*pName), debugstr_w(*pValue));
883     return hr;
884 }
885
886 static HRESULT WINAPI GameStatisticsImpl_SetStatistic(
887     IGameStatistics *iface,
888     WORD categoryIndex,
889     WORD statIndex,
890     LPCWSTR name,
891     LPCWSTR value)
892 {
893     HRESULT hr = S_OK;
894     DWORD dwNameLen, dwValueLen;
895     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
896
897     TRACE("(%p, %d, %d, %s, %s)\n", This, categoryIndex, statIndex,
898           debugstr_w(name), debugstr_w(value));
899
900     if(!name)
901         return S_FALSE;
902
903     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
904         return E_INVALIDARG;
905
906     dwNameLen = lstrlenW(name);
907
908     if(dwNameLen > MAX_NAME_LENGTH)
909     {
910         hr = S_FALSE;
911         dwNameLen = MAX_NAME_LENGTH;
912     }
913
914     lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sName,
915               name, dwNameLen+1);
916
917     if(value)
918     {
919         dwValueLen = lstrlenW(value);
920
921         if(dwValueLen > MAX_VALUE_LENGTH)
922         {
923             hr = S_FALSE;
924             dwValueLen = MAX_VALUE_LENGTH;
925         }
926
927         lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sValue,
928                   value, dwValueLen+1);
929     }
930     else
931         /* Windows allows to pass NULL as value */
932         This->stats.categories[categoryIndex].stats[statIndex].sValue[0] = 0;
933
934     return hr;
935 }
936
937 static HRESULT WINAPI GameStatisticsImpl_Save(
938     IGameStatistics *iface,
939     BOOL trackChanges)
940 {
941     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
942     HRESULT hr = S_OK;
943
944     TRACE("(%p, %d)\n", This, trackChanges);
945
946     if(trackChanges == TRUE)
947         FIXME("tracking changes not yet implemented\n");
948
949     hr = GAMEUX_updateStatisticsFile(&This->stats);
950
951     return hr;
952 }
953
954 static HRESULT WINAPI GameStatisticsImpl_SetLastPlayedCategory(
955     IGameStatistics *iface,
956     UINT categoryIndex)
957 {
958     FIXME("stub\n");
959     return E_NOTIMPL;
960 }
961
962 static HRESULT WINAPI GameStatisticsImpl_GetLastPlayedCategory(
963     IGameStatistics *iface,
964     UINT *pCategoryIndex)
965 {
966     FIXME("stub\n");
967     return E_NOTIMPL;
968 }
969
970 static const struct IGameStatisticsVtbl GameStatisticsImplVtbl =
971 {
972     GameStatisticsImpl_QueryInterface,
973     GameStatisticsImpl_AddRef,
974     GameStatisticsImpl_Release,
975     GameStatisticsImpl_GetMaxCategoryLength,
976     GameStatisticsImpl_GetMaxNameLength,
977     GameStatisticsImpl_GetMaxValueLength,
978     GameStatisticsImpl_GetMaxCategories,
979     GameStatisticsImpl_GetMaxStatsPerCategory,
980     GameStatisticsImpl_SetCategoryTitle,
981     GameStatisticsImpl_GetCategoryTitle,
982     GameStatisticsImpl_GetStatistic,
983     GameStatisticsImpl_SetStatistic,
984     GameStatisticsImpl_Save,
985     GameStatisticsImpl_SetLastPlayedCategory,
986     GameStatisticsImpl_GetLastPlayedCategory
987 };
988
989
990 HRESULT create_IGameStatistics(GameStatisticsImpl** ppStats)
991 {
992     TRACE("(%p)\n", ppStats);
993
994     *ppStats = HeapAlloc( GetProcessHeap(), 0, sizeof(**ppStats));
995     if(!(*ppStats))
996         return E_OUTOFMEMORY;
997
998     (*ppStats)->lpVtbl = &GameStatisticsImplVtbl;
999     (*ppStats)->ref = 1;
1000
1001     TRACE("returning coclass: %p\n", *ppStats);
1002     return S_OK;
1003 }
1004
1005 /*******************************************************************************
1006  * IGameStatisticsMgr implementation
1007  */
1008 typedef struct _GameStatisticsMgrImpl
1009 {
1010     const struct IGameStatisticsMgrVtbl *lpVtbl;
1011     LONG ref;
1012 } GameStatisticsMgrImpl;
1013
1014 static inline GameStatisticsMgrImpl *impl_from_IGameStatisticsMgr( IGameStatisticsMgr *iface )
1015 {
1016     return (GameStatisticsMgrImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsMgrImpl, lpVtbl));
1017 }
1018
1019
1020 static HRESULT WINAPI GameStatisticsMgrImpl_QueryInterface(
1021         IGameStatisticsMgr *iface,
1022         REFIID riid,
1023         void **ppvObject)
1024 {
1025     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1026
1027     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
1028
1029     *ppvObject = NULL;
1030
1031     if(IsEqualGUID(riid, &IID_IUnknown) ||
1032        IsEqualGUID(riid, &IID_IGameStatisticsMgr) )
1033     {
1034         *ppvObject = iface;
1035     }
1036     else
1037     {
1038         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1039         return E_NOINTERFACE;
1040     }
1041
1042     IGameStatisticsMgr_AddRef( iface );
1043     return S_OK;
1044 }
1045
1046 static ULONG WINAPI GameStatisticsMgrImpl_AddRef(IGameStatisticsMgr *iface)
1047 {
1048     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1049     LONG ref;
1050
1051     ref = InterlockedIncrement(&This->ref);
1052
1053     TRACE("(%p): ref=%d\n", This, ref);
1054     return ref;
1055 }
1056
1057 static ULONG WINAPI GameStatisticsMgrImpl_Release(IGameStatisticsMgr *iface)
1058 {
1059     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1060     LONG ref;
1061
1062     ref = InterlockedDecrement(&This->ref);
1063     TRACE("(%p): ref=%d\n", This, ref);
1064
1065     if ( ref == 0 )
1066     {
1067         TRACE("freeing GameStatistics object\n");
1068         HeapFree( GetProcessHeap(), 0, This);
1069     }
1070
1071     return ref;
1072 }
1073
1074 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_GetGameStatistics(
1075         IGameStatisticsMgr* iface,
1076         LPCWSTR GDFBinaryPath,
1077         GAMESTATS_OPEN_TYPE openType,
1078         GAMESTATS_OPEN_RESULT *pOpenResult,
1079         IGameStatistics **ppiStats)
1080 {
1081     HRESULT hr;
1082     WCHAR lpApplicationId[49];
1083     GameStatisticsImpl *statisticsImpl;
1084
1085     TRACE("(%p, %s, 0x%x, %p, %p)\n", iface, debugstr_w(GDFBinaryPath), openType, pOpenResult, ppiStats);
1086
1087     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1088
1089     if(SUCCEEDED(hr))
1090         hr = create_IGameStatistics(&statisticsImpl);
1091
1092     if(SUCCEEDED(hr))
1093     {
1094         *ppiStats = IGameStatistics_from_impl(statisticsImpl);
1095         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, statisticsImpl->stats.sStatsFile);
1096     }
1097
1098     if(SUCCEEDED(hr))
1099         hr = GAMEUX_loadGameStatistics(&statisticsImpl->stats, lpApplicationId, openType, pOpenResult);
1100
1101     return hr;
1102 }
1103
1104 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_RemoveGameStatistics(
1105         IGameStatisticsMgr* iface,
1106         LPCWSTR GDFBinaryPath)
1107 {
1108     HRESULT hr;
1109     WCHAR lpApplicationId[49];
1110     WCHAR sStatsFile[MAX_PATH];
1111
1112     TRACE("(%p, %s)\n", iface, debugstr_w(GDFBinaryPath));
1113
1114     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1115
1116     if(SUCCEEDED(hr))
1117         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, sStatsFile);
1118
1119     if(SUCCEEDED(hr))
1120         hr = (DeleteFileW(sStatsFile)==TRUE ? S_OK : HRESULT_FROM_WIN32(GetLastError()));
1121
1122     return hr;
1123 }
1124
1125 static const struct IGameStatisticsMgrVtbl GameStatisticsMgrImplVtbl =
1126 {
1127     GameStatisticsMgrImpl_QueryInterface,
1128     GameStatisticsMgrImpl_AddRef,
1129     GameStatisticsMgrImpl_Release,
1130     GameStatisticsMgrImpl_GetGameStatistics,
1131     GameStatisticsMgrImpl_RemoveGameStatistics,
1132 };
1133
1134 HRESULT GameStatistics_create(
1135         IUnknown *pUnkOuter,
1136         IUnknown **ppObj)
1137 {
1138     GameStatisticsMgrImpl *pGameStatistics;
1139
1140     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1141
1142     pGameStatistics = HeapAlloc( GetProcessHeap(), 0, sizeof (*pGameStatistics) );
1143
1144     if( !pGameStatistics )
1145         return E_OUTOFMEMORY;
1146
1147     pGameStatistics->lpVtbl = &GameStatisticsMgrImplVtbl;
1148     pGameStatistics->ref = 1;
1149
1150     *ppObj = (IUnknown*)(&pGameStatistics->lpVtbl);
1151
1152     TRACE("returning iface %p\n", *ppObj);
1153     return S_OK;
1154 }