void CJSONValue::InsertHandoff (CJSONValue &Source) // InsertHandoff // // Appends to the end of an array { ASSERT(m_iType == typeArray); ArrayType *pArray = (ArrayType *)m_pValue; CJSONValue *pNewValue = pArray->Insert(); pNewValue->TakeHandoff(Source); }
void CJSONValue::InsertHandoff (const CString &sKey, CJSONValue &Source) // InsertHandoff // // Appends to a key to a structure { ASSERT(m_iType == typeObject); ObjectType *pObj = (ObjectType *)m_pValue; CJSONValue *pNewValue = pObj->Insert(sKey); pNewValue->TakeHandoff(Source); }
ALERROR CMultiverseFileRef::InitFromJSON (const CJSONValue &Desc, CString *retsResult) // InitFromJSON // // Initialize from JSON value. { m_sFilePath = Desc.GetElement(FIELD_FILE_PATH).AsString(); CString sUploadedOn = Desc.GetElement(FIELD_UPLOADED_ON).AsString(); // Initialize m_sFilespec = NULL_STR; m_dwSize = 0; // Done return NOERROR; }
void HexarcTest (CUniverse &Universe, CXMLElement *pCmdLine) { int i, j; CString sHostspec = pCmdLine->GetAttribute(OPTION_HOST); if (sHostspec.IsBlank()) sHostspec = HOSTSPEC_DEFAULT; CHTTPClientSession Session; if (!Connect(sHostspec, Session)) return; // Read the list of high scores for (j = 0; j < 5; j++) { CJSONValue Payload = CJSONValue(CJSONValue::typeObject); Payload.InsertHandoff(FIELD_MAX_GAMES, CJSONValue(100)); CJSONValue Result; if (!ServerCommand(Session, METHOD_OPTIONS, FUNC_HIGH_SCORE_GAMES, Payload, &Result)) { printf("%s\n", Result.AsString().GetASCIIZPointer()); return; } // For each adventure, print high score if (Result.GetCount() == 0) printf("No game records.\n"); else { for (i = 0; i < Result.GetCount(); i++) { const CJSONValue &Record = Result.GetElement(i); DWORD dwAdventure = (DWORD)Record.GetElement(FIELD_ADVENTURE).AsInt32(); if (dwAdventure == 0) continue; CGameRecord GameRecord; if (GameRecord.InitFromJSON(Record) != NOERROR) { printf("Unable to parse JSON record.\n"); continue; } DWORD dwAdventureUNID = GameRecord.GetAdventureUNID(); CString sUsername = GameRecord.GetUsername(); int iScore = GameRecord.GetScore(); printf("%x %s %d\n", dwAdventureUNID, sUsername.GetASCIIZPointer(), iScore); } } } }
void CMultiverseModel::SetUpgradeVersion (const CJSONValue &Entry) // SetUpgradeVersion // // Sets the engine version available on the Multiverse. { // If this is not for our engine, then ignore it. if (!strEquals(Entry.GetElement(FIELD_UNID).AsString(), UPGRADE_ENTRY_UNID)) return; // Get the upgrade URL m_sUpgradeURL = Entry.GetElement(FIELD_DOWNLOAD_URL).AsString(); if (m_sUpgradeURL.IsBlank()) { ::kernelDebugLogMessage("Missing download URL in upgrade entry."); return; } // Parse the fileVersion m_UpgradeVersion.sProductVersion = Entry.GetElement(FIELD_FILE_VERSION).AsString(); TArray<CString> Parts; if (::strDelimit(m_UpgradeVersion.sProductVersion, '.', 0, &Parts) != NOERROR || Parts.GetCount() != 4) { ::kernelDebugLogMessage("Invalid upgrade entry fileVersion: %s", m_UpgradeVersion.sProductVersion); return; } m_UpgradeVersion.dwProductVersion = (((ULONG64)strToInt(Parts[0], 0)) << 48) | (((ULONG64)strToInt(Parts[1], 0)) << 32) | (((ULONG64)strToInt(Parts[2], 0)) << 16) | (((ULONG64)strToInt(Parts[3], 0))); }
ALERROR CMultiverseModel::SetCollection (const CJSONValue &Data, CString *retsResult) // SetCollection // // Sets the collection from a JSON value. Caller should have called // OnCollectionLoading. { CSmartLock Lock(m_cs); int i; // Try to load the collection into a temporary array. If we get any errors // then we abort without damage. bool bErrors = false; TArray<CMultiverseCatalogEntry *> NewCollection; for (i = 0; i < Data.GetCount(); i++) { const CJSONValue &Entry = Data.GetElement(i); // If this is a game engine entry then see if it tells us to upgrade // our engine. if (strEquals(TYPE_GAME_ENGINE, Entry.GetElement(FIELD_TYPE).AsString())) { SetUpgradeVersion(Entry); continue; } // Create a catalog entry and add to our collection CMultiverseCatalogEntry *pNewEntry; if (CMultiverseCatalogEntry::CreateFromJSON(Entry, &pNewEntry, retsResult) != NOERROR) { bErrors = true; continue; } // If this entry is not valid (perhaps because it is still in // development) then ignore it. if (!pNewEntry->IsValid()) { delete pNewEntry; continue; } // Add to the new collection NewCollection.Insert(pNewEntry); } // If we had errors while loading, and we didn't load anything then abort. // Otherwise we assume that only a couple of entries were bad (possibly // because they are under development). if (bErrors && NewCollection.GetCount() == 0) { m_fLoadingCollection = false; return ERR_FAIL; } // Otherwise, replace our collection. return SetCollection(NewCollection); }
ALERROR CMultiverseCatalogEntry::CreateFromJSON (const CJSONValue &Entry, CMultiverseCatalogEntry **retpEntry, CString *retsResult) // CreateFromJSON // // Creates a new entry from a JSON value. { int i; CMultiverseCatalogEntry *pNewEntry = new CMultiverseCatalogEntry; pNewEntry->m_sUNID = Entry.GetElement(FIELD_UNID).AsString(); if (pNewEntry->m_sUNID.IsBlank()) { delete pNewEntry; *retsResult = ERR_INVALID_UNID; return ERR_FAIL; } pNewEntry->m_dwRelease = (DWORD)Entry.GetElement(FIELD_RELEASE).AsInt32(); pNewEntry->m_dwVersion = (DWORD)Entry.GetElement(FIELD_VERSION).AsInt32(); pNewEntry->m_sName = Entry.GetElement(FIELD_NAME).AsString(); pNewEntry->m_sDesc = Entry.GetElement(FIELD_DESCRIPTION).AsString(); // Parse the fully qualified UNID and get just the hex UNID. if (strStartsWith(pNewEntry->m_sUNID, STR_UNID_PREFIX)) { pNewEntry->m_dwUNID = strParseIntOfBase(pNewEntry->m_sUNID.GetASCIIZPointer() + STR_UNID_PREFIX.GetLength(), 16, 0); if (pNewEntry->m_dwUNID == 0) { delete pNewEntry; *retsResult = ERR_INVALID_UNID; return ERR_FAIL; } } else { delete pNewEntry; *retsResult = ERR_INVALID_UNID; return ERR_FAIL; } // Get the type CString sType = Entry.GetElement(FIELD_TYPE).AsString(); if (strEquals(sType, TYPE_ADVENTURE)) pNewEntry->m_iType = extAdventure; else if (strEquals(sType, TYPE_LIBRARY)) pNewEntry->m_iType = extLibrary; else pNewEntry->m_iType = extExtension; // Get the license type CString sLicense = Entry.GetElement(FIELD_LICENSE_TYPE).AsString(); if (strEquals(sLicense, LICENSE_AUTO)) pNewEntry->m_iLicenseType = licenseAuto; else if (strEquals(sLicense, LICENSE_CORE)) pNewEntry->m_iLicenseType = licenseCore; else if (strEquals(sLicense, LICENSE_FREE)) pNewEntry->m_iLicenseType = licenseFree; else if (strEquals(sLicense, LICENSE_PAID)) pNewEntry->m_iLicenseType = licensePaid; else pNewEntry->m_iLicenseType = licenseUnknown; // Get the TDB file (only if not core) if (pNewEntry->m_iLicenseType != licenseCore) { if (pNewEntry->m_TDBFile.InitFromJSON(Entry.GetElement(FIELD_FILE_TDB), retsResult) != NOERROR) { delete pNewEntry; return ERR_FAIL; } } // Get the resources const CJSONValue &Resources = Entry.GetElement(FIELD_RESOURCES); if (Resources.GetType() == CJSONValue::typeArray) { pNewEntry->m_Resources.InsertEmpty(Resources.GetCount()); for (i = 0; i < Resources.GetCount(); i++) { if (pNewEntry->m_Resources[i].InitFromJSON(Resources.GetElement(i), retsResult) != NOERROR) { delete pNewEntry; return ERR_FAIL; } } } // Initialize status pNewEntry->m_iStatus = statusUnknown; pNewEntry->m_pIcon = NULL; // Done *retpEntry = pNewEntry; return NOERROR; }