Example #1
0
File: brush.cpp Project: CkyLua/rme
bool Brushes::unserializeBrush(pugi::xml_node node, wxArrayString& warnings)
{
	pugi::xml_attribute attribute;
	if (!(attribute = node.attribute("name"))) {
		warnings.push_back(wxT("Brush node without name."));
		return false;
	}

	const std::string& brushName = attribute.as_string();
	if (brushName == "all" || brushName == "none") {
		warnings.push_back(wxString(wxT("Using reserved brushname \"")) << wxstr(brushName) << wxT("\"."));
		return false;
	}

	Brush* brush = getBrush(brushName);
	if (!brush) {
		if (!(attribute = node.attribute("type"))) {
			warnings.push_back(wxT("Couldn't read brush type"));
			return false;
		}

		const std::string brushType = attribute.as_string();
		if (brushType == "border" || brushType == "ground") {
			brush = newd GroundBrush();
		} else if (brushType == "wall") {
			brush = newd WallBrush();
		} else if (brushType == "wall decoration") {
			brush = newd WallDecorationBrush();
		} else if (brushType == "carpet") {
			brush = newd CarpetBrush();
		} else if (brushType == "table") {
			brush = newd TableBrush();
		} else if (brushType == "doodad") {
			brush = newd DoodadBrush();
		} else {
			warnings.push_back(wxString(wxT("Unknown brush type ")) << wxstr(brushType));
			return false;
		}

		ASSERT(brush);
		brush->setName(brushName);
	}

	if (!node.first_child()) {
		brushes.insert(std::make_pair(brush->getName(), brush));
		return true;
	}

	wxArrayString subWarnings;
	brush->load(node, subWarnings);

	if(!subWarnings.empty()) {
		warnings.push_back(wxString(wxT("Errors while loading brush \"")) << wxstr(brush->getName()) << wxT("\""));
		warnings.insert(warnings.end(), subWarnings.begin(), subWarnings.end());
	}

	if(brush->getName() == "all" || brush->getName() == "none") {
		warnings.push_back(wxString(wxT("Using reserved brushname '")) << wxstr(brush->getName()) << wxT("'."));
		delete brush;
		return false;
	}

	Brush* otherBrush = getBrush(brush->getName());
	if(otherBrush) {
		if(otherBrush != brush) {
			warnings.push_back(wxString(wxT("Duplicate brush name ")) << wxstr(brush->getName()) << wxT(". Undefined behaviour may ensue."));
		} else {
			// Don't insert
			return true;
		}
	}

	brushes.insert(std::make_pair(brush->getName(), brush));
	return true;
}