예제 #1
0
		ZD_STATUS Language::LoadNode(JsonBox::Value &node, const std::string &prefix)
		{
			if (node.isObject())
			{
				JsonBox::Object obj = node.getObject();

				for (auto &i : obj)
				{
					std::stringstream new_prefix;

					if (prefix.length() > 0)
					{
						new_prefix << prefix << m_delimiter;						
					}

					new_prefix << i.first;

					LoadNode(i.second, new_prefix.str());
				}
			}
			else if (node.isString())
			{
				m_words[prefix] = node.getString();
			}
			else if (node.isInteger())
			{
				m_words[prefix] = Helpers::String::To(node.getInt());
			}
			return ZD_NOERROR;
		}
/**
 * 从数据包加载任务
 */
bool
MCTaskAccessor::loadTasks(const char *aFilePath)
{
    bool result = false;
    
    do {
        JsonBox::Value in;
        JsonBox::Object o;
        JsonBox::Object tasksDict;
        JsonBox::Object::iterator oIter;
        
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        CCString* pstrFileContent = CCString::createWithContentsOfFile(aFilePath);
        if (pstrFileContent) {
            in.loadFromString(pstrFileContent->getCString());
        }
#else
        in.loadFromFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(aFilePath).c_str());
#endif
        
        CC_BREAK_IF(! in.isObject());
        
        tasksDict = in.getObject();
        for (oIter = tasksDict.begin(); oIter != tasksDict.end(); ++oIter) {
            std::string t_id = oIter->first;
            const char *s_t_id = t_id.c_str();
            mc_object_id_t o_id = {
                s_t_id[0],
                s_t_id[1],
                s_t_id[2],
                s_t_id[3]
            };
            MCTask *task = new MCTask;
            task->autorelease();
            if (task) {
                task->setID(o_id);
                o = oIter->second.getObject();
                task->loadTaskContent(o);
                /* 默认为未完成状态,然后会在加载存档数据的时候更新为正确状态 */
                task->setTaskStatus(MCTaskUncompleted);
                task->proto_ = NULL;
                tasks_->setObject(task, MCObjectIdToDickKey(o_id));
            }
        }
        
        result = true;
    } while (0);
    
    return result;
}
예제 #3
0
bool Camera::parse(const JsonBox::Value &cameraVal, Camera &camera)
{
	JsonBox::Object cameraObj;

	if (!cameraVal.isObject())
	{
		cerr << "Error: Camera must be an Object." << endl;
		return false;
	}

	cameraObj = cameraVal.getObject();

	if
	(
		!cameraObj["origin"].isArray() || !cameraObj["direction"].isArray() || !cameraObj["top"].isArray()
		||
		!cameraObj["origin"].getArray()[0].isNumber()	|| !cameraObj["origin"].getArray()[1].isNumber() || !cameraObj["origin"].getArray()[2].isNumber()
		||
		!cameraObj["direction"].getArray()[0].isNumber() || !cameraObj["direction"].getArray()[1].isNumber() || !cameraObj["direction"].getArray()[2].isNumber()
		||
		!cameraObj["top"].getArray()[0].isNumber() || !cameraObj["top"].getArray()[1].isNumber() || !cameraObj["top"].getArray()[2].isNumber()
	)
	{
		cerr << "Error: invalid Camera vectors (origin, direction and top)." << endl;
		return false;
	}

	if (!cameraObj["fovy"].isNumber() || !cameraObj["width"].isInteger() || !cameraObj["height"].isInteger() || !cameraObj["filename"].isString())
	{
		cerr << "Error: invalid Camera parameters (fovy, width, height and filename)." << endl;
		return false;
	}

	Point origin = Point(cameraObj["origin"].getArray()[0].getNumber(), cameraObj["origin"].getArray()[1].getNumber(), cameraObj["origin"].getArray()[2].getNumber());
	Vector direction = Point(cameraObj["direction"].getArray()[0].getNumber(), cameraObj["direction"].getArray()[1].getNumber(), cameraObj["direction"].getArray()[2].getNumber());
	Vector top = Point(cameraObj["top"].getArray()[0].getNumber(), cameraObj["top"].getArray()[1].getNumber(), cameraObj["top"].getArray()[2].getNumber());

	camera = Camera(origin, direction, top, cameraObj["fovy"].getNumber() * (2 * M_PI) / 360.0, cameraObj["width"].getInt(), cameraObj["height"].getInt(), cameraObj["filename"].getString());

	return true;
}
예제 #4
0
bool Material::parse(const JsonBox::Value &materialVal, Material &material)
{
	JsonBox::Object materialObj;

	double infinity = numeric_limits<double>::infinity();


	if (!materialVal.isObject())
	{
		cerr << "Error: material is not an Object." << endl;
		return false;
	}

	materialObj = materialVal.getObject();

	if (!materialObj["name"].isString())
	{
		cerr << "Error: material's name is not a string." << endl;
		return false;
	}

	material.name = materialObj["name"].getString();

	if
	(
		!materialObj["color"].isArray()
		||
		(!materialObj["color"].getArray()[0].isInteger() && !materialObj["color"].getArray()[0].isDouble())
		||
		(!materialObj["color"].getArray()[1].isInteger() && !materialObj["color"].getArray()[1].isDouble())
		||
		(!materialObj["color"].getArray()[2].isInteger() && !materialObj["color"].getArray()[2].isDouble())
	)
	{
		/* color is not an array or one of the components is neither an integer nor a double */
		cerr << "Error: material's color is not an (valid) array." << endl;
		return false;
	}

	material.color.r = (materialObj["color"].getArray()[0].isInteger() ? materialObj["color"].getArray()[0].getInt() : materialObj["color"].getArray()[0].getDouble());
	material.color.g = (materialObj["color"].getArray()[1].isInteger() ? materialObj["color"].getArray()[1].getInt() : materialObj["color"].getArray()[1].getDouble());
	material.color.b = (materialObj["color"].getArray()[2].isInteger() ? materialObj["color"].getArray()[2].getInt() : materialObj["color"].getArray()[2].getDouble());

	if
	(
		(!materialObj["roughness"].isInteger() && !materialObj["roughness"].isDouble())
		||
		(!materialObj["absorvance"].isInteger() && !materialObj["absorvance"].isDouble())
		||
		(!materialObj["emittance"].isInteger() && !materialObj["emittance"].isDouble())
		||
		(!materialObj["n"].isInteger() && !materialObj["n"].isDouble() && (!materialObj["n"].isString() || materialObj["n"].getString().compare("infinity") != 0))
	)
	{
		cerr << "Error: at least one of the material's parameters (roughness, absorvance, emittance or n) is invalid." << endl;
		return false;
	}

	material.roughness = (materialObj["roughness"].isInteger() ? materialObj["roughness"].getInt() : materialObj["roughness"].getDouble());
	material.absorvance = (materialObj["absorvance"].isInteger() ? materialObj["absorvance"].getInt() : materialObj["absorvance"].getDouble());
	material.emittance = (materialObj["emittance"].isInteger() ? materialObj["emittance"].getInt() : materialObj["emittance"].getDouble());
	material.n = (materialObj["n"].isInteger() ? materialObj["n"].getInt() : (materialObj["n"].isDouble() ? materialObj["n"].getDouble() : infinity));

	return true;
}