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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
/*
GameSpy PT SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com
Copyright 2000 GameSpy Industries, Inc
*/
#include <stdio.h>
#include "pt.h"
#include "../ghttp/ghttp.h"
#include "../common/gsStringUtil.h"
#include "../common/gsAvailable.h"
/************
** DEFINES **
************/
#define PTA_DEFAULT_VERCHECK_URL "http://motd." GSI_DOMAIN_NAME "/motd/vercheck.asp"
#define PTA_DEFAULT_MOTD_URL "http://motd." GSI_DOMAIN_NAME "/motd/motd.asp"
#define PTA_DEFAULT_FILEPLANET_URL "http://www.fileplanet.com/dlfileraw.asp"
#define MAX_MIRRORS 32
#define PTA_MAX_STRING_SIZE 2048
char gPTAVercheckURL[PTA_MAX_STRING_SIZE];
char gPTAMOTDURL[PTA_MAX_STRING_SIZE];
char gPTAFilePlanetURL[PTA_MAX_STRING_SIZE];
/**********
** TYPES **
**********/
typedef struct ptaPatchData
{
ptPatchCallback callback;
void * param;
} ptaPatchData;
typedef struct ptaFilePlanetInfoData
{
int fileID;
ptFilePlanetInfoCallback callback;
void * param;
} ptaFilePlanetInfoData;
/************
** GLOBALS **
************/
//static char URL[PTA_MAX_STRING_SIZE];
/**************
** FUNCTIONS **
**************/
static const char * ptaGetKeyValue
(
const char * buffer,
const char * key
)
{
static char value[PTA_MAX_STRING_SIZE];
const char * str;
int len;
str = strstr(buffer, key);
if(!str)
return NULL;
str += strlen(key);
len = (int)strcspn(str, "\\");
len = min(len, (int)sizeof(value) - 1);
memcpy(value, str, (unsigned int)len);
value[len] = '\0';
return value;
}
static char Line[PTA_MAX_STRING_SIZE];
static int ptaFillLine
(
const char * buffer
)
{
char * str = Line;
int i;
// Skip white space.
////////////////////
for(i = 0 ; isspace(*buffer) ; i++)
buffer++;
// Check for EOF.
/////////////////
if(*buffer == '\0')
return EOF;
// Copy off the line.
/////////////////////
for( ; *buffer && ((*buffer != 0x0A) && (*buffer != 0x0D)) ; i++)
{
if(i == sizeof(Line) - 1)
{
// This line is too big for our buffer.
///////////////////////////////////////
assert(0);
return EOF;
}
*str++ = *buffer++;
}
*str = '\0';
return i;
}
static void ptaCallPatchCallback
(
ptaPatchData * data,
PTBool available,
PTBool mandatory,
const char * versionName,
int fileID,
const char * downloadURL
)
{
if(data->callback)
{
#ifndef GSI_UNICODE
data->callback(available, mandatory, versionName, fileID, downloadURL, data->param);
#else
unsigned short versionName_W[255];
unsigned short downloadURL_W[PTA_MAX_STRING_SIZE*2];
UTF8ToUCS2String(versionName, versionName_W);
AsciiToUCS2String(downloadURL, downloadURL_W);
data->callback(available, mandatory, versionName_W, fileID, downloadURL_W, data->param);
#endif
}
gsifree(data);
}
// already returns ghttptrue
static GHTTPBool ptaPatchFailed
(
ptaPatchData * data
)
{
ptaCallPatchCallback(data, PTFalse, PTFalse, "", 0, "");
return GHTTPTrue;
}
static void ptaCallFilePlanetInfoCallback
(
ptaFilePlanetInfoData * data,
PTBool found,
const char * description,
const char * size,
int numMirrors,
char ** mirrorNames,
char ** mirrorURLs
)
{
int i;
if(data->callback)
{
if (!found)
data->callback(data->fileID, PTFalse, NULL, NULL, 0, NULL, NULL, data->param);
else
{
#ifndef GSI_UNICODE
data->callback(data->fileID, found, description, size, numMirrors, (const char**)mirrorNames, (const char**)mirrorURLs, data->param);
#else
unsigned short description_W[255];
unsigned short size_W[1024];
unsigned short** mirrorNames_W;
unsigned short** mirrorURLs_W;
int i;
UTF8ToUCS2String(description, description_W);
AsciiToUCS2String(size, size_W);
mirrorNames_W = UTF8ToUCS2StringArrayAlloc((const UTF8String *)mirrorNames, numMirrors);
mirrorURLs_W = UTF8ToUCS2StringArrayAlloc((const UTF8String *)mirrorURLs, numMirrors);
data->callback(data->fileID, found, description_W, size_W, numMirrors, (const unsigned short**)mirrorNames_W, (const unsigned short**)mirrorURLs_W, data->param);
for (i=0; i < numMirrors; i++)
{
gsifree(mirrorNames[i]);
gsifree(mirrorURLs[i]);
}
gsifree(mirrorNames);
gsifree(mirrorURLs);
#endif
}
}
for(i = 0 ; i < numMirrors ; i++)
{
gsifree(mirrorNames[i]);
gsifree(mirrorURLs[i]);
}
gsifree(data);
}
// already returns ghttptrue
static GHTTPBool ptaFilePlanetInfoFailed
(
ptaFilePlanetInfoData * data
)
{
ptaCallFilePlanetInfoCallback(data, PTFalse, NULL, NULL, 0, NULL, NULL);
return GHTTPTrue;
}
static GHTTPBool ptaPatchCompletedCallback
(
GHTTPRequest request,
GHTTPResult result,
char * buffer,
GHTTPByteCount bufferLen,
void * param
)
{
ptaPatchData * data = (ptaPatchData *)param;
const char * value;
PTBool mandatory;
int fileID;
char versionName[PTA_MAX_STRING_SIZE];
char downloadURL[PTA_MAX_STRING_SIZE];
GSI_UNUSED(bufferLen);
GSI_UNUSED(request);
// Check for success.
/////////////////////
if(result != GHTTPSuccess)
return ptaPatchFailed(data);
// Check for a patch.
/////////////////////
value = ptaGetKeyValue(buffer, "\\newver\\");
if(!value || !atoi(value))
return ptaPatchFailed(data);
// Check the mandatory flag.
////////////////////////////
value = ptaGetKeyValue(buffer, "\\lockout\\");
mandatory = (value && atoi(value));
// Get the file id.
///////////////////
value = ptaGetKeyValue(buffer, "\\fpfileid\\");
if(value)
fileID = atoi(value);
else
fileID = 0;
// Get the name.
////////////////
value = ptaGetKeyValue(buffer, "\\newvername\\");
if(value)
{
strncpy(versionName, value, sizeof(versionName));
versionName[sizeof(versionName) - 1] = '\0';
}
else
versionName[0] = '\0';
// Get the URL.
///////////////
value = ptaGetKeyValue(buffer, "\\dlurl\\");
if(value)
{
strncpy(downloadURL, value, sizeof(downloadURL));
downloadURL[sizeof(downloadURL) - 1] = '\0';
}
else
downloadURL[0] = '\0';
// Call the callback.
/////////////////////
ptaCallPatchCallback(data, PTTrue, mandatory, versionName, fileID, downloadURL);
return GHTTPTrue;
}
PTBool ptCheckForPatchA
(
int productID,
const char * versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
void * param
)
{
int charsWritten;
char aURL[PTA_MAX_STRING_SIZE];
ptaPatchData * data;
// check if the backend is available
if(__GSIACResult != GSIACAvailable)
return PTFalse;
// Check the arguments.
///////////////////////
assert(versionUniqueID);
if(!versionUniqueID)
return PTFalse;
assert(callback);
if(!callback)
return PTFalse;
// override hostname?
if (gPTAVercheckURL[0] == '\0')
sprintf(gPTAVercheckURL, PTA_DEFAULT_VERCHECK_URL);
// Store some data.
///////////////////
data = (ptaPatchData *)gsimalloc(sizeof(ptaPatchData));
if(!data)
return PTFalse;
memset(data, 0, sizeof(ptaPatchData));
data->callback = callback;
data->param = param;
// Build the URL.
/////////////////
charsWritten =
snprintf(aURL, PTA_MAX_STRING_SIZE,
"%s?productid=%d&versionuniqueid=%s&distid=%d&gamename=%s",
gPTAVercheckURL, productID, versionUniqueID, distributionID,
__GSIACGamename);
assert(charsWritten >= 0);
if (charsWritten < 0)
return PTFalse;
// Send the request.
////////////////////
if((ghttpGetFileA(aURL, (GHTTPBool)blocking, ptaPatchCompletedCallback, data) == (unsigned char)(-1)) && !blocking)
return PTFalse;
return PTTrue;
}
#ifdef GSI_UNICODE
PTBool ptCheckForPatchW
(
int productID,
const unsigned short* versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
void * param
)
{
char versionUniqueID_A[255];
UCS2ToUTF8String(versionUniqueID, versionUniqueID_A);
return ptCheckForPatchA(productID, versionUniqueID_A, distributionID, callback, blocking, param);
}
#endif
PTBool ptTrackUsageA
(
int userID,
int productID,
const char * versionUniqueID,
int distributionID,
PTBool blocking
)
{
int charsWritten;
char aURL[PTA_MAX_STRING_SIZE];
// check if the backend is available
if(__GSIACResult != GSIACAvailable)
return PTFalse;
// Check the arguments.
///////////////////////
assert(versionUniqueID);
if(!versionUniqueID)
return PTFalse;
// override hostname?
if (gPTAMOTDURL[0] == '\0')
sprintf(gPTAMOTDURL, PTA_DEFAULT_MOTD_URL);
// Build the URL.
/////////////////
charsWritten = snprintf(aURL, PTA_MAX_STRING_SIZE,
"%s?userid=%d&productid=%d&versionuniqueid=%s&distid=%d&uniqueid=%s&gamename=%s",
gPTAMOTDURL, userID, productID, versionUniqueID, distributionID, GOAGetUniqueID(),
__GSIACGamename);
assert(charsWritten >= 0);
if (charsWritten < 0)
return PTFalse;
// Send the info.
/////////////////
if(ghttpGetFileA(aURL, (GHTTPBool)blocking, NULL, NULL) == -1)
return PTFalse;
return PTTrue;
}
#ifdef GSI_UNICODE
PTBool ptTrackUsageW
(
int userID,
int productID,
const unsigned short* versionUniqueID,
int distributionID,
PTBool blocking
)
{
char versionUniqueID_A[255];
UCS2ToUTF8String(versionUniqueID, versionUniqueID_A);
return ptTrackUsageA(userID, productID, versionUniqueID_A, distributionID, blocking);
}
#endif
PTBool ptCreateCheckPatchTrackUsageReqA
(
int userID,
int productID,
const char * versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
int *request,
void * param
)
{
int charsWritten;
char aURL[PTA_MAX_STRING_SIZE];
ptaPatchData * data;
// check if the backend is available
if(__GSIACResult != GSIACAvailable)
return PTFalse;
// Check the arguments.
///////////////////////
assert(versionUniqueID);
if(!versionUniqueID)
return PTFalse;
assert(callback);
if(!callback)
return PTFalse;
// Store some data.
///////////////////
data = (ptaPatchData *)gsimalloc(sizeof(ptaPatchData));
if(!data)
return PTFalse;
memset(data, 0, sizeof(ptaPatchData));
data->callback = callback;
data->param = param;
// override hostname?
if (gPTAVercheckURL[0] == '\0')
sprintf(gPTAVercheckURL, PTA_DEFAULT_VERCHECK_URL);
// Build the URL.
/////////////////
charsWritten = snprintf(aURL, PTA_MAX_STRING_SIZE,
"%s?userid=%d&productid=%d&versionuniqueid=%s&distid=%d&uniqueid=%s&gamename=%s",
gPTAVercheckURL, userID, productID, versionUniqueID, distributionID, GOAGetUniqueID(),
__GSIACGamename);
assert(charsWritten >= 0);
if (charsWritten < 0)
return PTFalse;
// Send the request.
////////////////////
*request = ghttpGetFileA(aURL, (GHTTPBool)blocking, ptaPatchCompletedCallback, data);
if (*request == -1)
return PTFalse;
return PTTrue;
}
#ifdef GSI_UNICODE
PTBool ptCreateCheckPatchTrackUsageReqW
(
int userID,
int productID,
const unsigned short * versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
int *request,
void * param
)
{
char versionUniqueID_A[255];
UCS2ToUTF8String(versionUniqueID, versionUniqueID_A);
return ptCreateCheckPatchTrackUsageReqA(userID, productID, versionUniqueID_A, distributionID, callback, blocking, request, param);
}
#endif
PTBool ptCheckForPatchAndTrackUsageA
(
int userID,
int productID,
const char * versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
void * param
)
{
GHTTPRequest aRequest;
// create the request and send it.
///////////////////////////////////
return ptCreateCheckPatchTrackUsageReqA(userID, productID, versionUniqueID, distributionID, callback, blocking, &aRequest, param);
}
#ifdef GSI_UNICODE
PTBool ptCheckForPatchAndTrackUsageW
(
int userID,
int productID,
const unsigned short * versionUniqueID,
int distributionID,
ptPatchCallback callback,
PTBool blocking,
void * param
)
{
char versionUniqueID_A[255];
UCS2ToUTF8String(versionUniqueID, versionUniqueID_A);
return ptCheckForPatchAndTrackUsageA(userID, productID, versionUniqueID_A, distributionID, callback, blocking, param);
}
#endif
static GHTTPBool ptaFilePlanetCompletedCallback
(
GHTTPRequest request,
GHTTPResult result,
char * buffer,
GHTTPByteCount bufferLen,
void * param
)
{
ptaFilePlanetInfoData * data = (ptaFilePlanetInfoData *)param;
int len;
char description[256];
char size[64];
char * mirrorNames[MAX_MIRRORS];
char * mirrorURLs[MAX_MIRRORS];
int i;
char * str;
// check if the backend is available
if(__GSIACResult != GSIACAvailable)
return GHTTPFalse;
GSI_UNUSED(request);
GSI_UNUSED(bufferLen);
// Check for success.
/////////////////////
if(result != GHTTPSuccess)
return ptaFilePlanetInfoFailed(data);
// Get the description.
///////////////////////
len = ptaFillLine(buffer);
if(len == EOF)
return ptaFilePlanetInfoFailed(data);
buffer += len;
strncpy(description, Line, sizeof(description));
description[sizeof(description) - 1] = '\0';
// Get the size.
////////////////
len = ptaFillLine(buffer);
if(len == EOF)
return ptaFilePlanetInfoFailed(data);
buffer += len;
strncpy(size, Line, sizeof(size));
size[sizeof(size) - 1] = '\0';
// Get the mirrors.
///////////////////
for(i = 0 ; (i < MAX_MIRRORS) && ((len = ptaFillLine(buffer)) != EOF) ; )
{
// Adjust the buffer.
/////////////////////
buffer += len;
// Find the tab.
////////////////
str = strchr(Line, '\t');
if(!str)
continue;
// Copy off the name.
/////////////////////
len = (str - Line);
mirrorNames[i] = (char *)gsimalloc((unsigned int)len + 1);
if(!mirrorNames[i])
break;
memcpy(mirrorNames[i], Line, (unsigned int)len);
mirrorNames[i][len] = '\0';
// Copy off the URL.
////////////////////
str++;
len = (int)strlen(str);
mirrorURLs[i] = (char *)gsimalloc((unsigned int)len + 1);
if(!mirrorURLs[i])
{
gsifree(mirrorNames[i]);
break;
}
strcpy(mirrorURLs[i], str);
// One more mirror.
///////////////////
i++;
}
// Call the callback.
/////////////////////
ptaCallFilePlanetInfoCallback(data, PTTrue, description, size, i, mirrorNames, mirrorURLs);
return GHTTPTrue;
}
// 9/7/2004 (xgd) ptLookupFilePlanetInfo() deprecated; per case 2724.
//
PTBool ptLookupFilePlanetInfo
(
int fileID,
ptFilePlanetInfoCallback callback,
PTBool blocking,
void * param
)
{
char aURL[PTA_MAX_STRING_SIZE];
ptaFilePlanetInfoData * data;
// Check the arguments.
///////////////////////
assert(callback);
if(!callback)
return PTFalse;
// override hostname?
if (gPTAFilePlanetURL[0] == '\0')
sprintf(gPTAFilePlanetURL, PTA_DEFAULT_FILEPLANET_URL);
// Store some data.
///////////////////
data = (ptaFilePlanetInfoData *)gsimalloc(sizeof(ptaFilePlanetInfoData));
if(!data)
return PTFalse;
memset(data, 0, sizeof(ptaFilePlanetInfoData));
data->callback = callback;
data->param = param;
data->fileID = fileID;
// Build the URL.
/////////////////
// 11/24/2004 - Added By Saad Nader
// Now using string size as limit for printing
// also null terminate string automatically
///////////////////////////////////////////////
snprintf(aURL, PTA_MAX_STRING_SIZE,
"%s?file=%d&gamename=%s", gPTAFilePlanetURL, fileID, __GSIACGamename);
// Send the request.
////////////////////
if((ghttpGetFileA(aURL, (GHTTPBool)blocking, ptaFilePlanetCompletedCallback, data) == -1) && !blocking)
return PTFalse;
return PTTrue;
}
|