예제 #1
0
		void LocalHighscores::parse() {
			ARK2D::getLog()->v("Parsing LocalHighscores");

			string s;
			#if defined(ARK2D_ANDROID)  
				if (m_threaded && m_data != NULL) { 
					s = string((char*) m_data);
				} else if (StringUtil::file_exists(m_filename.c_str())) {
					s = StringUtil::file_get_contents(m_filename.c_str());
				} else {
					s = string((char*) m_data);
				}
			#else

				if (m_data != NULL) {
					s = string((char*) m_data);
				} else {
					s = StringUtil::file_get_contents(m_filename.c_str());
				}

			#endif
			ARK2D::getLog()->v(StringUtil::append("JSON String is: ", s));

			if (s.length() == 0) {
				ARK2D::getLog()->e("JSON String was empty. Huh?");
				return;
			} 

			if (s == "[]") { 
				ARK2D::getLog()->v("Local highscores were blank. That's fine. ");
			} else {
	 
				JSONNode* arr = libJSON::Parse(s);
				if (arr == NULL) {
					ARK2D::getLog()->e("Could not parse json");
					return;
				}
				for(unsigned int i = 0; i < arr->NodeSize(); i++) { 
					JSONNode* item = arr->NodeAt(i);

					LocalHighscoreItem* it = new LocalHighscoreItem();
					it->name = item->GetNode("name")->NodeAsString();
					it->score = item->GetNode("score")->NodeAsInt();
	 
					//StringUtil::str_replace("\"", "\\\"", it->name); 

					m_items.push_back(it);
				}
				ARK2D::getLog()->i("loaded local highscores");

				// fix memory leaks
				ARK2D::getLog()->i("Freeing up memory.");
				libJSON::Delete(arr);

			}

			this->sort();
		}
예제 #2
0
		PathGroup* PathIO::createFromJSON(string json) {
		#ifdef EXCEPTIONS_AVAILABLE
			try {
		#endif
				JSONNode* root = libJSON::Parse(json);

				PathGroup* pathGroup = new PathGroup();
				pathGroup->setName(root->GetNode("name")->NodeAsString());
				pathGroup->setDescription(root->GetNode("description")->NodeAsString());

				if (root->GetNode("relative") == NULL) {
					pathGroup->setRelative(false);
				} else {
					pathGroup->setRelative(root->GetNode("relative")->NodeAsBool());
				}

				JSONNode* paths = root->GetNode("paths");
				for(unsigned int i = 0; i < paths->NodeSize(); i++)
				{

					JSONNode* path = paths->Children[i];

					Path* pathObj = new Path();
					pathObj->setDuration(path->GetNode("duration")->NodeAsFloat());
					pathObj->setEasing(Easing::getByString(path->GetNode("easing")->NodeAsString()));

					JSONNode* points = path->GetNode("points");
					for(unsigned int j = 0; j < points->NodeSize(); j++)
					{
						JSONNode* point = points->Children[j];
						pathObj->addPoint(point->GetNode("x")->NodeAsInt(), point->GetNode("y")->NodeAsInt());
					}
					pathGroup->addPath(pathObj);
				}
				return pathGroup;
		#ifdef EXCEPTIONS_AVAILABLE
			} catch(...) {
				ErrorDialog::createAndShow("exception in PathIO::createFromJson");
				return NULL;
			}
		#endif
			return NULL;
		}