wined3d: Pass an IWineD3DBaseTextureImpl pointer to basetexture_get_lod().
[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     HKEY hKey;
369     DWORD dwLength = 49*sizeof(WCHAR);/* place for GUID */
370
371     TRACE("(%s, %p)\n", debugstr_w(GDFBinaryPath), lpApplicationId);
372
373     if(!GDFBinaryPath)
374         return E_INVALIDARG;
375
376     installScope = GIS_CURRENT_USER;
377     hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
378
379     if(hr == S_FALSE)
380     {
381         installScope = GIS_ALL_USERS;
382         hr = GAMEUX_FindGameInstanceId(GDFBinaryPath, installScope, &instanceId);
383     }
384
385     if(hr == S_FALSE)
386         /* game not registered, so statistics cannot be used */
387         hr = E_FAIL;
388
389     if(SUCCEEDED(hr))
390         /* game is registered, let's read it's application id from registry */
391         hr = GAMEUX_buildGameRegistryPath(installScope, &instanceId, &lpRegistryPath);
392
393     if(SUCCEEDED(hr))
394         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_LOCAL_MACHINE,
395                 lpRegistryPath, 0, KEY_READ | KEY_WOW64_64KEY, &hKey));
396
397     if(SUCCEEDED(hr))
398         hr = HRESULT_FROM_WIN32(RegGetValueW(hKey,
399                 NULL, sApplicationId, RRF_RT_REG_SZ,
400                 NULL, lpApplicationId, &dwLength));
401
402     HeapFree(GetProcessHeap(), 0, lpRegistryPath);
403     RegCloseKey(hKey);
404
405     TRACE("found app id: %s, return: %#x\n", debugstr_w(lpApplicationId), hr);
406     return hr;
407 }
408 /*******************************************************************
409  * GAMEUX_loadGameStatisticsFromFile
410  * Helper function, loads game statistics from file and stores them
411  * in the structure.
412  *
413  * Parameters:
414  *  data                [I/O]   structure containing file name to
415  *                              load and data fields to store data in
416  */
417 static HRESULT GAMEUX_loadStatisticsFromFile(struct GAMEUX_STATS *data)
418 {
419     static const WCHAR sStatistics[] = {'S','t','a','t','i','s','t','i','c','s',0};
420     static const WCHAR sCategory[] = {'C','a','t','e','g','o','r','y',0};
421     static const WCHAR sIndex[] = {'I','n','d','e','x',0};
422     static const WCHAR sStatistic[] = {'S','t','a','t','i','s','t','i','c',0};
423     static const WCHAR sName[] = {'N','a','m','e',0};
424     static const WCHAR sValue[] = {'V','a','l','u','e',0};
425
426     HRESULT hr = S_OK;
427     IXMLDOMDocument *document = NULL;
428     IXMLDOMElement *root = NULL, *categoryElement, *statisticElement;
429     IXMLDOMNode *categoryNode, *statisticNode;
430     IXMLDOMNodeList *rootChildren = NULL, *categoryChildren;
431     VARIANT vStatsFilePath, vValue;
432     BSTR bstrStatistics = NULL, bstrCategory = NULL, bstrIndex = NULL,
433         bstrStatistic = NULL, bstrName = NULL, bstrValue = NULL;
434     VARIANT_BOOL isSuccessful =  VARIANT_FALSE;
435     int i, j;
436
437     TRACE("(%p)\n", data);
438
439     V_VT(&vStatsFilePath) = VT_BSTR;
440     V_BSTR(&vStatsFilePath) = SysAllocString(data->sStatsFile);
441     if(!V_BSTR(&vStatsFilePath))
442         hr = E_OUTOFMEMORY;
443
444     if(SUCCEEDED(hr))
445         hr = CoCreateInstance(&CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&document);
446
447     if(SUCCEEDED(hr))
448     {
449         bstrStatistics = SysAllocString(sStatistics);
450         if(!bstrStatistics)
451             hr = E_OUTOFMEMORY;
452     }
453
454     if(SUCCEEDED(hr))
455     {
456         bstrCategory = SysAllocString(sCategory);
457         if(!bstrCategory)
458             hr = E_OUTOFMEMORY;
459     }
460
461     if(SUCCEEDED(hr))
462     {
463         bstrIndex = SysAllocString(sIndex);
464         if(!bstrIndex)
465             hr = E_OUTOFMEMORY;
466     }
467
468     if(SUCCEEDED(hr))
469     {
470         bstrStatistic = SysAllocString(sStatistic);
471         if(!bstrStatistic)
472             hr = E_OUTOFMEMORY;
473     }
474
475     if(SUCCEEDED(hr))
476     {
477         bstrName = SysAllocString(sName);
478         if(!bstrName)
479             hr = E_OUTOFMEMORY;
480     }
481
482     if(SUCCEEDED(hr))
483     {
484         bstrValue = SysAllocString(sValue);
485         if(!bstrValue)
486             hr = E_OUTOFMEMORY;
487     }
488
489     if(SUCCEEDED(hr))
490         hr = IXMLDOMDocument_load(document, vStatsFilePath, &isSuccessful);
491
492     if(hr == S_OK && isSuccessful != VARIANT_TRUE)
493         hr = S_FALSE;
494
495     if( hr == S_OK )
496         hr = IXMLDOMDocument_get_documentElement(document, &root);
497
498     if(hr == S_OK)
499         hr = IXMLDOMElement_get_childNodes(root, &rootChildren);
500
501     if(hr == S_OK)
502     {
503         hr = S_OK;
504         while(hr == S_OK)
505         {
506             hr = IXMLDOMNodeList_nextNode(rootChildren, &categoryNode);
507
508             if(hr == S_OK)
509             {
510                 hr = IXMLDOMNode_QueryInterface(categoryNode, &IID_IXMLDOMElement, (LPVOID*)&categoryElement);
511
512                 if(SUCCEEDED(hr))
513                 {
514                     hr = IXMLDOMElement_getAttribute(categoryElement, bstrIndex, &vValue);
515                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
516                         hr = E_FAIL;
517
518                     if(SUCCEEDED(hr))
519                     {
520                         i = StrToIntW(V_BSTR(&vValue));
521                         hr = IXMLDOMElement_getAttribute(categoryElement, bstrName, &vValue);
522                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
523                             hr = E_FAIL;
524                     }
525
526                     if(SUCCEEDED(hr))
527                     {
528                         lstrcpynW(data->categories[i].sName, V_BSTR(&vValue), MAX_CATEGORY_LENGTH);
529                         TRACE("category %d name %s\n", i, debugstr_w(data->categories[i].sName));
530                         hr = IXMLDOMElement_get_childNodes(categoryElement, &categoryChildren);
531                     }
532
533                     if(SUCCEEDED(hr))
534                     {
535                         hr = S_OK;
536                         while(hr == S_OK)
537                         {
538                             hr = IXMLDOMNodeList_nextNode(categoryChildren, &statisticNode);
539
540                             if(hr == S_OK)
541                             {
542                                 hr = IXMLDOMNode_QueryInterface(statisticNode, &IID_IXMLDOMElement, (LPVOID*)&statisticElement);
543
544                                 if(SUCCEEDED(hr))
545                                 {
546                                     hr = IXMLDOMElement_getAttribute(statisticElement, bstrIndex, &vValue);
547                                     if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
548                                         hr = E_FAIL;
549
550                                     if(SUCCEEDED(hr))
551                                     {
552                                         j = StrToIntW(V_BSTR(&vValue));
553                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrName, &vValue);
554                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
555                                             hr = E_FAIL;
556                                     }
557
558                                     if(SUCCEEDED(hr))
559                                     {
560                                         lstrcpynW(data->categories[i].stats[j].sName, V_BSTR(&vValue), MAX_NAME_LENGTH);
561                                         hr = IXMLDOMElement_getAttribute(statisticElement, bstrValue, &vValue);
562                                         if( hr == S_OK && V_VT(&vValue) != VT_BSTR)
563                                             hr = E_FAIL;
564                                     }
565
566                                     if(SUCCEEDED(hr))
567                                         lstrcpynW(data->categories[i].stats[j].sValue, V_BSTR(&vValue), MAX_VALUE_LENGTH);
568
569                                     TRACE("  statistic %d name %s value %s\n", j,
570                                             debugstr_w(data->categories[i].stats[j].sName),
571                                             debugstr_w(data->categories[i].stats[j].sValue));
572                                     IXMLDOMElement_Release(statisticElement);
573                                 }
574
575                                 IXMLDOMNode_Release(statisticNode);
576                             }
577                         }
578
579                         if(SUCCEEDED(hr))
580                             hr = S_OK;
581                     }
582                     IXMLDOMElement_Release(categoryElement);
583                 }
584
585                 IXMLDOMNode_Release(categoryNode);
586             }
587         }
588         if(SUCCEEDED(hr))
589             hr = S_OK;
590     }
591
592     if(rootChildren) IXMLDOMNodeList_Release(rootChildren);
593     if(root) IXMLDOMElement_Release(root);
594     if(document) IXMLDOMDocument_Release(document);
595
596     SysFreeString(bstrValue);
597     SysFreeString(bstrName);
598     SysFreeString(bstrStatistic);
599     SysFreeString(bstrIndex);
600     SysFreeString(bstrCategory);
601     SysFreeString(bstrStatistics);
602     SysFreeString(V_BSTR(&vStatsFilePath));
603     return hr;
604 }
605 /*******************************************************************
606  * GAMEUX_loadGameStatistics
607  *
608  * Helper function which loads game statistics associated with game
609  * into interface's internal structures
610  *
611  * Parameters:
612  *  pStats              [O]     structure which will receive data
613  *  sGameId             [I]     application instance Id, stored as string
614  *                              to avoid additional conversions
615  *  openType            [I]     allowed ways of opening statistics
616  *  pOpenResult         [O]     way used to open statistics
617  *
618  */
619 static HRESULT GAMEUX_loadGameStatistics(struct GAMEUX_STATS *pStats,
620         LPWSTR sGameId,
621         GAMESTATS_OPEN_TYPE openType,
622         GAMESTATS_OPEN_RESULT* pOpenResult)
623 {
624     HRESULT hr;
625     TRACE("(%p, %s, %d, %p)\n", pStats, debugstr_w(sGameId), openType, pOpenResult);
626
627     hr = GAMEUX_buildStatisticsFilePath(sGameId, pStats->sStatsFile);
628
629     hr = GAMEUX_loadStatisticsFromFile(pStats);
630     TRACE("ldstats finished, res: %#x\n", hr);
631     if(hr == S_OK)
632     {
633         *pOpenResult = GAMESTATS_OPEN_OPENED;
634     }
635     else if(hr == S_FALSE && openType == GAMESTATS_OPEN_OPENORCREATE) /* file does not exist */
636     {
637         /* create new statitics, not yet connected with file */
638         ZeroMemory(pStats->categories, sizeof(pStats->categories));
639         *pOpenResult = GAMESTATS_OPEN_CREATED;
640         hr = S_OK;
641     }
642     else
643         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
644
645     TRACE("openResult=%#x ret=%#x\n", *pOpenResult, hr);
646     return hr;
647 }
648  /*******************************************************************
649  * IGameStatistics implementation
650  */
651 typedef struct _GameStatisticsImpl
652 {
653     const struct IGameStatisticsVtbl *lpVtbl;
654     LONG ref;
655     struct GAMEUX_STATS stats;
656 } GameStatisticsImpl;
657
658 static inline GameStatisticsImpl *impl_from_IGameStatistics( IGameStatistics *iface )
659 {
660     return (GameStatisticsImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsImpl, lpVtbl));
661 }
662 static inline IGameStatistics *IGameStatistics_from_impl( GameStatisticsImpl* This )
663 {
664     return (struct IGameStatistics*)&This->lpVtbl;
665 }
666
667
668 static HRESULT WINAPI GameStatisticsImpl_QueryInterface(
669         IGameStatistics *iface,
670         REFIID riid,
671         void **ppvObject)
672 {
673     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
674
675     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
676
677     *ppvObject = NULL;
678
679     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
680          IsEqualGUID( riid, &IID_IGameStatistics ) )
681     {
682         *ppvObject = iface;
683     }
684     else
685     {
686         FIXME("interface %s not implemented\n", debugstr_guid(riid));
687         return E_NOINTERFACE;
688     }
689
690     IGameStatistics_AddRef( iface );
691     return S_OK;
692 }
693
694 static ULONG WINAPI GameStatisticsImpl_AddRef(IGameStatistics *iface)
695 {
696     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
697     LONG ref;
698
699     ref = InterlockedIncrement(&This->ref);
700
701     TRACE("(%p): ref=%d\n", This, ref);
702     return ref;
703 }
704
705 static ULONG WINAPI GameStatisticsImpl_Release(IGameStatistics *iface)
706 {
707     GameStatisticsImpl *This = impl_from_IGameStatistics( iface );
708     LONG ref;
709
710     ref = InterlockedDecrement( &This->ref );
711     TRACE("(%p): ref=%d\n", This, ref);
712
713     if ( ref == 0 )
714     {
715         TRACE("freeing IGameStatistics\n");
716         HeapFree( GetProcessHeap(), 0, This );
717     }
718
719     return ref;
720 }
721
722 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategoryLength(
723     IGameStatistics *iface,
724     UINT *cch)
725 {
726     TRACE("(%p, %p)\n", iface, cch);
727     if(!cch)
728         return E_INVALIDARG;
729
730     *cch = MAX_CATEGORY_LENGTH;
731     return S_OK;
732 }
733
734 static HRESULT WINAPI GameStatisticsImpl_GetMaxNameLength(
735     IGameStatistics *iface,
736     UINT *cch)
737 {
738     TRACE("(%p, %p)\n", iface, cch);
739     if(!cch)
740         return E_INVALIDARG;
741
742     *cch = MAX_NAME_LENGTH;
743     return S_OK;
744 }
745
746 static HRESULT WINAPI GameStatisticsImpl_GetMaxValueLength(
747     IGameStatistics *iface,
748     UINT *cch)
749 {
750     TRACE("(%p, %p)\n", iface, cch);
751     if(!cch)
752         return E_INVALIDARG;
753
754     *cch = MAX_VALUE_LENGTH;
755     return S_OK;
756 }
757
758 static HRESULT WINAPI GameStatisticsImpl_GetMaxCategories(
759     IGameStatistics *iface,
760     WORD *pMax)
761 {
762     TRACE("(%p, %p)\n", iface, pMax);
763     if(!pMax)
764         return E_INVALIDARG;
765
766     *pMax = MAX_CATEGORIES;
767     return S_OK;
768 }
769
770 static HRESULT WINAPI GameStatisticsImpl_GetMaxStatsPerCategory(
771     IGameStatistics *iface,
772     WORD *pMax)
773 {
774     TRACE("(%p, %p)\n", iface, pMax);
775     if(!pMax)
776         return E_INVALIDARG;
777
778     *pMax = MAX_STATS_PER_CATEGORY;
779     return S_OK;
780 }
781
782 static HRESULT WINAPI GameStatisticsImpl_SetCategoryTitle(
783     IGameStatistics *iface,
784     WORD categoryIndex,
785     LPCWSTR title)
786 {
787     HRESULT hr = S_OK;
788     DWORD dwLength;
789     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
790
791     TRACE("(%p, %d, %s)\n", This, categoryIndex, debugstr_w(title));
792
793     if(!title || categoryIndex >= MAX_CATEGORIES)
794         return E_INVALIDARG;
795
796     dwLength = lstrlenW(title);
797
798     if(dwLength > MAX_CATEGORY_LENGTH)
799     {
800         hr = S_FALSE;
801         dwLength = MAX_CATEGORY_LENGTH;
802     }
803
804     lstrcpynW(This->stats.categories[categoryIndex].sName,
805               title, dwLength+1);
806
807     return hr;
808 }
809
810 static HRESULT WINAPI GameStatisticsImpl_GetCategoryTitle(
811     IGameStatistics *iface,
812     WORD categoryIndex,
813     LPWSTR *pTitle)
814 {
815     HRESULT hr = S_OK;
816     LONG nLength;
817     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
818
819     TRACE("%p, %d, %p\n", This, categoryIndex, pTitle);
820
821     *pTitle = NULL;
822
823     if(!pTitle || categoryIndex >= MAX_CATEGORIES)
824         hr = E_INVALIDARG;
825
826
827     if(SUCCEEDED(hr))
828     {
829         nLength = lstrlenW(This->stats.categories[categoryIndex].sName);
830         if(nLength != 0)
831         {
832             *pTitle = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
833             lstrcpyW(*pTitle, This->stats.categories[categoryIndex].sName);
834         }
835     }
836
837     return hr;
838 }
839
840 static HRESULT WINAPI GameStatisticsImpl_GetStatistic(
841     IGameStatistics *iface,
842     WORD categoryIndex,
843     WORD statIndex,
844     LPWSTR *pName,
845     LPWSTR *pValue)
846 {
847     HRESULT hr = S_OK;
848     LONG nLength;
849     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
850
851     TRACE("%p, %d,%d, %p, %p\n", This, categoryIndex, statIndex, pName, pValue);
852
853     if(!pName || !pValue)
854         return E_INVALIDARG;
855
856     *pName = NULL;
857     *pValue = NULL;
858
859     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
860         hr = E_INVALIDARG;
861
862     if(SUCCEEDED(hr))
863     {
864         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sName);
865         if(nLength != 0)
866         {
867             *pName = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
868             if(!(*pName))
869                 hr = E_OUTOFMEMORY;
870             else
871                 lstrcpyW(*pName, This->stats.categories[categoryIndex].stats[statIndex].sName);
872         }
873     }
874
875     if(SUCCEEDED(hr))
876     {
877         nLength = lstrlenW(This->stats.categories[categoryIndex].stats[statIndex].sValue);
878         if(nLength != 0)
879         {
880             *pValue = CoTaskMemAlloc(sizeof(WCHAR)*(nLength+1));
881             if(!(*pValue))
882                 hr = E_OUTOFMEMORY;
883             else
884                 lstrcpyW(*pValue, This->stats.categories[categoryIndex].stats[statIndex].sValue);
885         }
886     }
887
888     TRACE("returning pair; %s => %s\n", debugstr_w(*pName), debugstr_w(*pValue));
889     return hr;
890 }
891
892 static HRESULT WINAPI GameStatisticsImpl_SetStatistic(
893     IGameStatistics *iface,
894     WORD categoryIndex,
895     WORD statIndex,
896     LPCWSTR name,
897     LPCWSTR value)
898 {
899     HRESULT hr = S_OK;
900     DWORD dwNameLen, dwValueLen;
901     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
902
903     TRACE("(%p, %d, %d, %s, %s)\n", This, categoryIndex, statIndex,
904           debugstr_w(name), debugstr_w(value));
905
906     if(!name)
907         return S_FALSE;
908
909     if(categoryIndex >= MAX_CATEGORIES || statIndex >= MAX_STATS_PER_CATEGORY)
910         return E_INVALIDARG;
911
912     dwNameLen = lstrlenW(name);
913
914     if(dwNameLen > MAX_NAME_LENGTH)
915     {
916         hr = S_FALSE;
917         dwNameLen = MAX_NAME_LENGTH;
918     }
919
920     lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sName,
921               name, dwNameLen+1);
922
923     if(value)
924     {
925         dwValueLen = lstrlenW(value);
926
927         if(dwValueLen > MAX_VALUE_LENGTH)
928         {
929             hr = S_FALSE;
930             dwValueLen = MAX_VALUE_LENGTH;
931         }
932
933         lstrcpynW(This->stats.categories[categoryIndex].stats[statIndex].sValue,
934                   value, dwValueLen+1);
935     }
936     else
937         /* Windows allows to pass NULL as value */
938         This->stats.categories[categoryIndex].stats[statIndex].sValue[0] = 0;
939
940     return hr;
941 }
942
943 static HRESULT WINAPI GameStatisticsImpl_Save(
944     IGameStatistics *iface,
945     BOOL trackChanges)
946 {
947     GameStatisticsImpl *This = impl_from_IGameStatistics(iface);
948     HRESULT hr = S_OK;
949
950     TRACE("(%p, %d)\n", This, trackChanges);
951
952     if(trackChanges == TRUE)
953         FIXME("tracking changes not yet implemented\n");
954
955     hr = GAMEUX_updateStatisticsFile(&This->stats);
956
957     return hr;
958 }
959
960 static HRESULT WINAPI GameStatisticsImpl_SetLastPlayedCategory(
961     IGameStatistics *iface,
962     UINT categoryIndex)
963 {
964     FIXME("stub\n");
965     return E_NOTIMPL;
966 }
967
968 static HRESULT WINAPI GameStatisticsImpl_GetLastPlayedCategory(
969     IGameStatistics *iface,
970     UINT *pCategoryIndex)
971 {
972     FIXME("stub\n");
973     return E_NOTIMPL;
974 }
975
976 static const struct IGameStatisticsVtbl GameStatisticsImplVtbl =
977 {
978     GameStatisticsImpl_QueryInterface,
979     GameStatisticsImpl_AddRef,
980     GameStatisticsImpl_Release,
981     GameStatisticsImpl_GetMaxCategoryLength,
982     GameStatisticsImpl_GetMaxNameLength,
983     GameStatisticsImpl_GetMaxValueLength,
984     GameStatisticsImpl_GetMaxCategories,
985     GameStatisticsImpl_GetMaxStatsPerCategory,
986     GameStatisticsImpl_SetCategoryTitle,
987     GameStatisticsImpl_GetCategoryTitle,
988     GameStatisticsImpl_GetStatistic,
989     GameStatisticsImpl_SetStatistic,
990     GameStatisticsImpl_Save,
991     GameStatisticsImpl_SetLastPlayedCategory,
992     GameStatisticsImpl_GetLastPlayedCategory
993 };
994
995
996 HRESULT create_IGameStatistics(GameStatisticsImpl** ppStats)
997 {
998     TRACE("(%p)\n", ppStats);
999
1000     *ppStats = HeapAlloc( GetProcessHeap(), 0, sizeof(**ppStats));
1001     if(!(*ppStats))
1002         return E_OUTOFMEMORY;
1003
1004     (*ppStats)->lpVtbl = &GameStatisticsImplVtbl;
1005     (*ppStats)->ref = 1;
1006
1007     TRACE("returning coclass: %p\n", *ppStats);
1008     return S_OK;
1009 }
1010
1011 /*******************************************************************************
1012  * IGameStatisticsMgr implementation
1013  */
1014 typedef struct _GameStatisticsMgrImpl
1015 {
1016     const struct IGameStatisticsMgrVtbl *lpVtbl;
1017     LONG ref;
1018 } GameStatisticsMgrImpl;
1019
1020 static inline GameStatisticsMgrImpl *impl_from_IGameStatisticsMgr( IGameStatisticsMgr *iface )
1021 {
1022     return (GameStatisticsMgrImpl *)((char*)iface - FIELD_OFFSET(GameStatisticsMgrImpl, lpVtbl));
1023 }
1024
1025
1026 static HRESULT WINAPI GameStatisticsMgrImpl_QueryInterface(
1027         IGameStatisticsMgr *iface,
1028         REFIID riid,
1029         void **ppvObject)
1030 {
1031     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1032
1033     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
1034
1035     *ppvObject = NULL;
1036
1037     if(IsEqualGUID(riid, &IID_IUnknown) ||
1038        IsEqualGUID(riid, &IID_IGameStatisticsMgr) )
1039     {
1040         *ppvObject = iface;
1041     }
1042     else
1043     {
1044         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1045         return E_NOINTERFACE;
1046     }
1047
1048     IGameStatisticsMgr_AddRef( iface );
1049     return S_OK;
1050 }
1051
1052 static ULONG WINAPI GameStatisticsMgrImpl_AddRef(IGameStatisticsMgr *iface)
1053 {
1054     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1055     LONG ref;
1056
1057     ref = InterlockedIncrement(&This->ref);
1058
1059     TRACE("(%p): ref=%d\n", This, ref);
1060     return ref;
1061 }
1062
1063 static ULONG WINAPI GameStatisticsMgrImpl_Release(IGameStatisticsMgr *iface)
1064 {
1065     GameStatisticsMgrImpl *This = impl_from_IGameStatisticsMgr( iface );
1066     LONG ref;
1067
1068     ref = InterlockedDecrement(&This->ref);
1069     TRACE("(%p): ref=%d\n", This, ref);
1070
1071     if ( ref == 0 )
1072     {
1073         TRACE("freeing GameStatistics object\n");
1074         HeapFree( GetProcessHeap(), 0, This);
1075     }
1076
1077     return ref;
1078 }
1079
1080 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_GetGameStatistics(
1081         IGameStatisticsMgr* iface,
1082         LPCWSTR GDFBinaryPath,
1083         GAMESTATS_OPEN_TYPE openType,
1084         GAMESTATS_OPEN_RESULT *pOpenResult,
1085         IGameStatistics **ppiStats)
1086 {
1087     HRESULT hr;
1088     WCHAR lpApplicationId[49];
1089     GameStatisticsImpl *statisticsImpl;
1090
1091     TRACE("(%p, %s, 0x%x, %p, %p)\n", iface, debugstr_w(GDFBinaryPath), openType, pOpenResult, ppiStats);
1092
1093     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1094
1095     if(SUCCEEDED(hr))
1096         hr = create_IGameStatistics(&statisticsImpl);
1097
1098     if(SUCCEEDED(hr))
1099     {
1100         *ppiStats = IGameStatistics_from_impl(statisticsImpl);
1101         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, statisticsImpl->stats.sStatsFile);
1102     }
1103
1104     if(SUCCEEDED(hr))
1105         hr = GAMEUX_loadGameStatistics(&statisticsImpl->stats, lpApplicationId, openType, pOpenResult);
1106
1107     return hr;
1108 }
1109
1110 static HRESULT STDMETHODCALLTYPE GameStatisticsMgrImpl_RemoveGameStatistics(
1111         IGameStatisticsMgr* iface,
1112         LPCWSTR GDFBinaryPath)
1113 {
1114     HRESULT hr;
1115     WCHAR lpApplicationId[49];
1116     WCHAR sStatsFile[MAX_PATH];
1117
1118     TRACE("(%p, %s)\n", iface, debugstr_w(GDFBinaryPath));
1119
1120     hr = GAMEUX_getAppIdFromGDFPath(GDFBinaryPath, lpApplicationId);
1121
1122     if(SUCCEEDED(hr))
1123         hr = GAMEUX_buildStatisticsFilePath(lpApplicationId, sStatsFile);
1124
1125     if(SUCCEEDED(hr))
1126         hr = (DeleteFileW(sStatsFile)==TRUE ? S_OK : HRESULT_FROM_WIN32(GetLastError()));
1127
1128     return hr;
1129 }
1130
1131 static const struct IGameStatisticsMgrVtbl GameStatisticsMgrImplVtbl =
1132 {
1133     GameStatisticsMgrImpl_QueryInterface,
1134     GameStatisticsMgrImpl_AddRef,
1135     GameStatisticsMgrImpl_Release,
1136     GameStatisticsMgrImpl_GetGameStatistics,
1137     GameStatisticsMgrImpl_RemoveGameStatistics,
1138 };
1139
1140 HRESULT GameStatistics_create(
1141         IUnknown *pUnkOuter,
1142         IUnknown **ppObj)
1143 {
1144     GameStatisticsMgrImpl *pGameStatistics;
1145
1146     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1147
1148     pGameStatistics = HeapAlloc( GetProcessHeap(), 0, sizeof (*pGameStatistics) );
1149
1150     if( !pGameStatistics )
1151         return E_OUTOFMEMORY;
1152
1153     pGameStatistics->lpVtbl = &GameStatisticsMgrImplVtbl;
1154     pGameStatistics->ref = 1;
1155
1156     *ppObj = (IUnknown*)(&pGameStatistics->lpVtbl);
1157
1158     TRACE("returning iface %p\n", *ppObj);
1159     return S_OK;
1160 }