-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInGameOfferManager.cpp
More file actions
273 lines (233 loc) · 8.91 KB
/
InGameOfferManager.cpp
File metadata and controls
273 lines (233 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ======================================================================================
// File : InGameOfferManager.cpp
// Author : Li Chen
// Last Change : 01/07/2011 | 14:48:54 PM | Friday,January
// Description :
// ======================================================================================
#include "stdafx.h"
#if defined(_XBOX)
#include "InGameDownloadManager.h"
#include "InGameOfferManager.h"
#include "SignIn.h"
#include "Master.h"
#include "Task.h"
#define MAX_ENUMERATION_RESULTS 10
namespace GameService
{
OfferManager::OfferManager(MessageMgr* msgMgr)
: m_hEnumeration( INVALID_HANDLE_VALUE ),
m_dwBufferSize( 0 ),
m_pBuffer( NULL ),
m_bIsOfferEnumerationFinished(FALSE)
{
if (msgMgr)
{
msgMgr->Register(EMessage_SignInChanged,this);
msgMgr->Register(EMessage_OnlineTaskDone,this);
}
}
// ========================================================
// Initialize
// ========================================================
GS_BOOL OfferManager::Initialize()
{
Cleanup();
return TRUE;
}
// ========================================================
// Cleanup
// ========================================================
GS_VOID OfferManager::Cleanup()
{
m_bIsOfferEnumerationFinished = FALSE;
// Free the handle
if ( m_hEnumeration != INVALID_HANDLE_VALUE )
{
XCloseHandle( m_hEnumeration );
m_hEnumeration = INVALID_HANDLE_VALUE;
}
// Delete the buffer
if (m_pBuffer)
{
delete [] m_pBuffer;
m_pBuffer = NULL;
}
m_dwBufferSize = 0;
ClearOffers();
}
//--------------------------------------------------------------------------------------
// Name: Update
//--------------------------------------------------------------------------------------
GS_VOID OfferManager::Update()
{
}
//--------------------------------------------------------------------------------------
// Name: EnumerateOffers
//--------------------------------------------------------------------------------------
GS_BOOL OfferManager::EnumerateOffers(GS_INT category)
{
if (!SignIn::AreUsersSignedIn())
return FALSE;
Cleanup();
// Enumerate at most MAX_ENUMERATION_RESULTS items
GS_DWORD dwError = XMarketplaceCreateOfferEnumerator
(
SignIn::GetActiveUserIndex(), // Get the offer data for this player
XMARKETPLACE_OFFERING_TYPE_CONTENT, // Marketplace content (can combine values using ||)
category, // Retrieve all content catagories
MAX_ENUMERATION_RESULTS, // Number of results per call to XEnumerate
&m_dwBufferSize, // Size of buffer needed for results
&m_hEnumeration // Enumeration handle
);
if ( FAILED( HRESULT_FROM_WIN32(dwError) ) )
{
Cleanup();
return FALSE;
}
m_pBuffer = GS_NEW BYTE[m_dwBufferSize];
if ( !m_pBuffer )
{
Master::G()->Log("OfferManager::Initialize - Out of Memory!");
Cleanup();
return FALSE;
}
// Enumerate contents asynchronized
CTaskID id = 0;
GS_DWORD dwStatus = XEnumerate( m_hEnumeration, m_pBuffer, m_dwBufferSize, NULL,
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_OfferMgrEnumeration,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,dwStatus);
if( dwStatus != ERROR_IO_PENDING )
{
Master::G()->Log("OfferManager::Enumerate() failed with error %d", dwStatus);
}
return TRUE;
}
//--------------------------------------------------------------------------------------
// Name: _AddOffer
// Desc: Create a copy of the referenced offer and add it to the array of offers during
// enumeration.
//--------------------------------------------------------------------------------------
GS_VOID OfferManager::AddOffer( const XMARKETPLACE_CONTENTOFFER_INFO& Offer )
{
// Caculate the size needed for the structure
GS_DWORD dwBufferSize = sizeof( XMARKETPLACE_CONTENTOFFER_INFO );
// arrange for extra storage at the end to hold the three strings
dwBufferSize += Offer.dwOfferNameLength;
dwBufferSize += Offer.dwSellTextLength;
dwBufferSize += Offer.dwTitleNameLength;
// Allocate and initialize
PXMARKETPLACE_CONTENTOFFER_INFO pOfferData = (PXMARKETPLACE_CONTENTOFFER_INFO)GS_NEW BYTE[dwBufferSize];
ZeroMemory( pOfferData, dwBufferSize );
XMemCpy( pOfferData, &Offer, sizeof( XMARKETPLACE_CONTENTOFFER_INFO ) );
// convert wchar to char!!!
size_t ret_value = 0;
// Copy the Offer Name
pOfferData->wszOfferName = (WCHAR *)&pOfferData[1];
wcstombs_s(&ret_value, (char*)pOfferData->wszOfferName, Offer.dwOfferNameLength, Offer.wszOfferName, Offer.dwOfferNameLength);
// XMemCpy(pOfferData->wszOfferName, Offer.wszOfferName, Offer.dwOfferNameLength);
// Copy the Sell Text
pOfferData->wszSellText = pOfferData->wszOfferName + Offer.dwOfferNameLength;
wcstombs_s(&ret_value, (char*)pOfferData->wszSellText, Offer.dwSellTextLength, Offer.wszSellText, Offer.dwSellTextLength);
// XMemCpy(pOfferData->wszSellText, Offer.wszSellText, Offer.dwSellTextLength);
// Copy the Title Name
pOfferData->wszTitleName = pOfferData->wszSellText + Offer.dwSellTextLength;
wcstombs_s(&ret_value, (char*)pOfferData->wszTitleName, Offer.dwTitleNameLength, Offer.wszTitleName, Offer.dwTitleNameLength);
// XMemCpy(pOfferData->wszTitleName, Offer.wszTitleName, Offer.dwTitleNameLength);
// Add the record to the table
m_aOfferData.AddItem(pOfferData);
}
//--------------------------------------------------------------------------------------
// Name: ClearOffers
// Desc: Free each structure in the collection then clear the collection
//--------------------------------------------------------------------------------------
GS_VOID OfferManager::ClearOffers()
{
for (unsigned i = 0; i < m_aOfferData.Num(); ++i)
{
GS_DELETE [] m_aOfferData(i);
}
m_aOfferData.Empty();
}
// ========================================================
//
// ========================================================
GS_BOOL OfferManager::IsOfferEnumerationFinished()
{
return m_bIsOfferEnumerationFinished;
}
GS_VOID OfferManager::GetContentList(TArray<CMarketplaceItem>& productList)
{
if (m_bIsOfferEnumerationFinished)
{
for (GS_UINT i=0; i<m_aOfferData.Num(); i++)
{
CMarketplaceItem tmp;
tmp.m_iIndex = i;
strcpy(tmp.m_strName, (char*)(m_aOfferData(i)->wszOfferName));
productList.AddItem(tmp);
}
}
}
GS_VOID OfferManager::GetProductDetail(GS_INT index, CMarketplaceDetail& detail)
{
if (m_bIsOfferEnumerationFinished)
{
strcpy(detail.m_strName, (char*)(m_aOfferData(index)->wszOfferName));
strcpy(detail.m_strDesc, (char*)(m_aOfferData(index)->wszSellText));
// get image URL
WCHAR tmp_imageURL[XMARKETPLACE_IMAGE_URL_MINIMUM_WCHARCOUNT*2];
XMarketplaceGetImageUrl(m_aOfferData(index)->dwTitleID, m_aOfferData(index)->qwOfferID,
XMARKETPLACE_IMAGE_URL_MINIMUM_WCHARCOUNT*2, tmp_imageURL);
size_t ret_value = 0;
wcstombs_s(&ret_value, m_cCurrentProductImageURL, XMARKETPLACE_IMAGE_URL_MINIMUM_WCHARCOUNT*2, tmp_imageURL, XMARKETPLACE_IMAGE_URL_MINIMUM_WCHARCOUNT*2);
strcpy(detail.m_strImagePath, m_cCurrentProductImageURL);
}
}
GS_BOOL OfferManager::GetOfferIDByIndex( GS_INT index, ULONGLONG& offerID )
{
if ( (size_t)index < m_aOfferData.Num() )
{
offerID = m_aOfferData( index )->qwOfferID;
return TRUE;
}
return FALSE;
}
// ========================================================
// MessageResponse
// ========================================================
GS_VOID OfferManager::MessageResponse(Message* message)
{
if (EMessage_SignInChanged == message->GetMessageID())
{
return;
}
CTaskID task_id = *(CTaskID*)message->ReadPayload(0);
GS_TaskType taskType = (GS_TaskType)(*(GS_INT*)message->ReadPayload(1));
GS_DWORD taskResult = *(GS_DWORD*)message->ReadPayload(2);
GS_DWORD taskDetailedResult = *(GS_DWORD*)message->ReadPayload(3);
#if defined(_XBOX) || defined(_XENON)
GS_DWORD taskExtendedResult = *(GS_DWORD*)message->ReadPayload(4);
#endif
#if defined(_XBOX) || defined(_XENON)
if (taskResult == ERROR_SUCCESS)
#elif defined(_PS3)
if (0 == taskResult && taskDetailedResult >= 0)
#endif
{
switch(taskType)
{
case EGSTaskType_OfferMgrEnumeration:
{
PXMARKETPLACE_CONTENTOFFER_INFO pTempOfferData = (PXMARKETPLACE_CONTENTOFFER_INFO)m_pBuffer;
for ( GS_DWORD dw = 0; dw < taskDetailedResult; ++dw )
{
AddOffer( pTempOfferData[dw] );
}
m_bIsOfferEnumerationFinished = TRUE;
}
break;
}
}
}
} // namespace
#endif