LIB3MFMETHODIMP CCOMModelMeshObject::SetTriangle(_In_ DWORD nIndex, _In_ MODELMESHTRIANGLE * pTriangle)
	{
		UINT j;

		try {
			if (!pTriangle)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CMesh * pMesh = getMesh();
			__NMRASSERT(pMesh);

			// Check for input validity
			UINT nNodeCount = pMesh->getNodeCount();
			for (j = 0; j < 3; j++)
				if (pTriangle->m_nIndices[j] >= nNodeCount)
					throw CNMRException_Windows(NMR_ERROR_INVALIDINDEX, LIB3MF_INVALIDARG);
			if ((pTriangle->m_nIndices[0] == pTriangle->m_nIndices[1]) ||
				(pTriangle->m_nIndices[0] == pTriangle->m_nIndices[2]) ||
				(pTriangle->m_nIndices[1] == pTriangle->m_nIndices[2]))
				throw CNMRException_Windows(NMR_ERROR_INVALIDINDEX, LIB3MF_INVALIDARG);

			// retrieve node and return position
			MESHFACE * pFace = pMesh->getFace(nIndex);
			for (j = 0; j < 3; j++)
				pFace->m_nodeindices[j] = pTriangle->m_nIndices[j];

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelMeshObject::SetGeometry(_In_ MODELMESHVERTEX * pVertices, _In_ DWORD nVertexCount, _In_ MODELMESHTRIANGLE * pTriangles, _In_ DWORD nTriangleCount)
	{
		UINT j, nIndex;


		try {
			if ((!pVertices) || (!pTriangles))
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CMesh * pMesh = getMesh();
			__NMRASSERT(pMesh);

			// Clear old mesh
			pMesh->clear();

			// Rebuild Mesh Coordinates
			MODELMESHVERTEX * pVertex = pVertices;
			for (nIndex = 0; nIndex < nVertexCount; nIndex++) {
				NVEC3 vPosition;
				for (j = 0; j < 3; j++) {
					FLOAT fCoord = pVertex->m_fPosition[j];
					if (fabs(fCoord) > NMR_MESH_MAXCOORDINATE)
						throw CNMRException_Windows(NMR_ERROR_INVALIDCOORDINATES, LIB3MF_INVALIDARG);
					vPosition.m_fields[j] = fCoord;
				}
				pMesh->addNode(vPosition);

				pVertex++;
			}

			// Rebuild Mesh Faces
			MODELMESHTRIANGLE * pTriangle = pTriangles;
			for (nIndex = 0; nIndex < nTriangleCount; nIndex++) {
				MESHNODE * pNodes[3];

				for (j = 0; j < 3; j++) {
					if (pTriangle->m_nIndices[j] >= nVertexCount)
						throw CNMRException_Windows(NMR_ERROR_INVALIDINDEX, LIB3MF_INVALIDARG);
					pNodes[j] = pMesh->getNode(pTriangle->m_nIndices[j]);
				}

				if ((pTriangle->m_nIndices[0] == pTriangle->m_nIndices[1]) ||
					(pTriangle->m_nIndices[0] == pTriangle->m_nIndices[2]) ||
					(pTriangle->m_nIndices[1] == pTriangle->m_nIndices[2]))
					throw CNMRException_Windows(NMR_ERROR_INVALIDINDEX, LIB3MF_INVALIDARG);

				pMesh->addFace(pNodes[0], pNodes[1], pNodes[2]);

				pTriangle++;
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelMeshObject::AddVertex(_In_ MODELMESHVERTEX * pVertex, _Out_opt_ DWORD * pnIndex)
	{
		UINT j;

		try {
			if (!pVertex)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CMesh * pMesh = getMesh();
			__NMRASSERT(pMesh);

			// Create Position Vector and check input
			NVEC3 vPosition;
			for (j = 0; j < 3; j++) {
				FLOAT fCoord = pVertex->m_fPosition[j];
				if (fabs(fCoord) > NMR_MESH_MAXCOORDINATE)
					throw CNMRException_Windows(NMR_ERROR_INVALIDCOORDINATES, LIB3MF_INVALIDARG);
				vPosition.m_fields[j] = fCoord;
			}

			// Set position to node
			MESHNODE * pNode = pMesh->addNode(vPosition);
			if (pnIndex) {
				*pnIndex = pNode->m_index;
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelMeshObject::GetTriangle(_In_ DWORD nIndex, _Out_ MODELMESHTRIANGLE * pTriangle)
	{
		UINT j;

		try {
			if (!pTriangle)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CMesh * pMesh = getMesh();
			__NMRASSERT(pMesh);

			// retrieve node and return position
			MESHFACE * pFace = pMesh->getFace(nIndex);
			for (j = 0; j < 3; j++)
				pTriangle->m_nIndices[j] = pFace->m_nodeindices[j];

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelMeshObject::GetVertex(_In_ DWORD nIndex, _Out_ MODELMESHVERTEX * pVertex)
	{
		UINT j;

		try {
			if (!pVertex)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CMesh * pMesh = getMesh();
			__NMRASSERT(pMesh);

			// retrieve node and return position
			MESHNODE * pNode = pMesh->getNode(nIndex);
			for (j = 0; j < 3; j++)
				pVertex->m_fPosition[j] = pNode->m_position.m_fields[j];

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
Esempio n. 6
0
void Service::evaluate(iostream::XdrInputStream& istream,
		       iostream::XdrOutputStream& ostream)
{
  getLogger().info(CONTAINER_SERVICE, "Evaluate called on service");

  TechniqueService* technique = handleTechniqueLookup(istream, ostream);

  getLogger().info(CONTAINER_SERVICE, "Technique service " + technique->getUrn() + " looked up");

  if (technique == 0) {
    getLogger().info(CONTAINER_SERVICE, "Technique service lookup failed");
    handleFailedLookup(istream, ostream);

  } else {
    getLogger().info(CONTAINER_SERVICE, "Technique service lookup succeeded");
    RootConceptInstance* subgraph = extractSubgraph(istream, ostream);

    try {
      technique->evaluate(*subgraph);
      handleSuccess(istream, ostream);

    } catch (...) {
      handleError(istream, ostream);
    }

    releaseSubgraph(*subgraph);
  }
}
	LIB3MFMETHODIMP CCOMModelBuildItem::GetPartNumber(_Out_opt_ LPWSTR pwszBuffer, _In_ ULONG cbBufferSize, _Out_opt_ ULONG * pcbNeededChars)
	{
		try	{
			if (!m_pModelBuildItem.get())
				throw CNMRException(NMR_ERROR_INVALIDBUILDITEM);

			if (cbBufferSize > MODEL_MAXSTRINGBUFFERLENGTH)
				throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);

			// Safely call StringToBuffer
			nfUint32 nNeededChars = 0;
			fnWStringToBufferSafe(m_pModelBuildItem->getPartNumber(), pwszBuffer, cbBufferSize, &nNeededChars);

			// Return length if needed
			if (pcbNeededChars)
				*pcbNeededChars = nNeededChars;

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelMeshObject::GetName(_Out_opt_ LPWSTR pwszBuffer, _In_ ULONG cbBufferSize, _Out_opt_ ULONG * pcbNeededChars)
	{
		try {
			if (cbBufferSize > MODEL_MAXSTRINGBUFFERLENGTH)
				throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);

			CModelMeshObject * pObject = getMeshObject();
			__NMRASSERT(pObject);

			// Safely call StringToBuffer
			nfUint32 nNeededChars = 0;
			fnWStringToBufferSafe(pObject->getName(), pwszBuffer, cbBufferSize, &nNeededChars);

			// Return length if needed
			if (pcbNeededChars)
				*pcbNeededChars = nNeededChars;

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelComponentsObject::GetNameUTF8(_Out_opt_ LPSTR pszBuffer, _In_ ULONG cbBufferSize, _Out_opt_ ULONG * pcbNeededChars)
	{
		try {
			if (cbBufferSize > MODEL_MAXSTRINGBUFFERLENGTH)
				throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);

			CModelComponentsObject * pObject = getComponentsObject();
			__NMRASSERT(pObject);

			std::wstring sUTF16Name = pObject->getName();
			std::string sUTF8Name = fnUTF16toUTF8(sUTF16Name);

			// Safely call StringToBuffer
			nfUint32 nNeededChars = 0;
			fnStringToBufferSafe(sUTF8Name, pszBuffer, cbBufferSize, &nNeededChars);

			// Return length if needed
			if (pcbNeededChars)
				*pcbNeededChars = nNeededChars;

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelWriter::WriteToFileUTF8(_In_z_ LPCSTR pwszFilename)
	{

		try {
			if (pwszFilename == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);
			if (m_pModelWriter.get() == nullptr)
				throw CNMRException(NMR_ERROR_RESOURCENOTFOUND);

			// Convert to UTF16
			std::string sUTF8FileName(pwszFilename);
			std::wstring sUTF16FileName = fnUTF8toUTF16(sUTF8FileName);

			PExportStream pStream = fnCreateExportStreamInstance(sUTF16FileName.c_str());
			m_pModelWriter->exportToStream(pStream);

			return handleSuccess();
		}
		catch (CNMRException_Windows & WinException) {
			return handleNMRException(&WinException);
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelTexture2D::GetBox2D(_Out_ FLOAT * pfU, _Out_ FLOAT * pfV, _Out_ FLOAT * pfWidth, _Out_ FLOAT * pfHeight)
	{
		try {
			if ((pfU == nullptr) || (pfV == nullptr) || (pfWidth == nullptr) || (pfHeight == nullptr))
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			nfFloat fU, fV, fWidth, fHeight;
			if (pTextureResource->getBox2D(fU, fV, fWidth, fHeight)) {
				*pfU = fU;
				*pfV = fV;
				*pfWidth = fWidth;
				*pfHeight = fHeight;
			}
			else {
				*pfU = 0.0f;
				*pfV = 0.0f;
				*pfWidth = 1.0f;
				*pfHeight = 1.0f;
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}


	}
	LIB3MFMETHODIMP CCOMModelTexture2D::GetStreamSize(_Out_ ULONG64 * pcbStreamSize)
	{
		try {
			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureStream = pTextureResource->getTextureStream();

			if (pTextureStream.get() != nullptr) {
				*pcbStreamSize = pTextureStream->retrieveSize();
			}
			else {
				*pcbStreamSize = 0;
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelTexture2D::ReadFromBuffer(_In_ BYTE * pBuffer, _In_ ULONG64 cbBufferSize)
	{
		try {
			if (pBuffer == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pImportStream = std::make_shared<CImportStream_Memory>(pBuffer, cbBufferSize);

			CModel * pModel = pTextureResource->getModel();
			__NMRASSERT(pModel);

			pModel->removeAttachment(pTextureResource->getPath());
			pModel->addAttachment(pTextureResource->getPath(), PACKAGE_TEXTURE_RELATIONSHIP_TYPE, pImportStream);

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelTexture2D::GetPath(_Out_opt_ LPWSTR pwszBuffer, _In_ ULONG cbBufferSize, _Out_ ULONG * pcbNeededChars)
	{
		try {
			if (cbBufferSize > MODEL_MAXSTRINGBUFFERLENGTH)
				throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			// Retrieve Path
			std::wstring sPath = pTextureResource->getPath();

			// Safely call StringToBuffer
			nfUint32 nNeededChars = 0;
			fnWStringToBufferSafe(sPath, pwszBuffer, cbBufferSize, &nNeededChars);

			// Return length if needed
			if (pcbNeededChars)
				*pcbNeededChars = nNeededChars;


			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}


	}
	LIB3MFMETHODIMP CCOMModelTexture2D::WriteToCallback(_In_ void * pWriteCallback, _In_opt_ void * pUserData)
	{
		try {
			if (pWriteCallback == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureAttachment = pTextureResource->getTextureStream();
			ExportStream_WriteCallbackType pTypedWriteCallback = (ExportStream_WriteCallbackType)pWriteCallback;

			if (pTextureAttachment.get() != nullptr) {
				PExportStream pExportStream = std::make_shared<CExportStream_Callback>(pTypedWriteCallback, nullptr, pUserData);
				pExportStream->copyFrom(pTextureAttachment.get(), pTextureAttachment->retrieveSize(), MODELTEXTURE2D_BUFFERSIZE);
			}
			else {
				throw CNMRException(NMR_ERROR_NOTEXTURESTREAM);
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelTexture2D::WriteToBuffer(_Out_ BYTE * pBuffer, _In_ ULONG64 cbBufferSize)
	{
		try {
			if (pBuffer == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureStream = pTextureResource->getTextureStream();

			if (pTextureStream.get() != nullptr) {
				nfUint64 cbStreamSize = pTextureStream->retrieveSize();
				if (cbStreamSize > cbBufferSize)
					throw CNMRException(NMR_ERROR_INSUFFICIENTBUFFERSIZE);
					
				pTextureStream->seekPosition(0, true);
				pTextureStream->readBuffer(pBuffer, cbStreamSize, true);
			}
			else {
				throw CNMRException(NMR_ERROR_NOTEXTURESTREAM);
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelTexture2D::SetAttachment(_In_ ILib3MFModelAttachment * pTextureAttachment)
	{
		try {
			if (pTextureAttachment == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			// check relationship type
			std::string sRelationshipType;
			ULONG cbNeededChars;
			pTextureAttachment->GetRelationshipTypeUTF8(nullptr, 0, &cbNeededChars);
			sRelationshipType.resize(cbNeededChars);
			pTextureAttachment->GetRelationshipTypeUTF8(&(sRelationshipType[0]), cbNeededChars + 1, &cbNeededChars);
			if (!(sRelationshipType == PACKAGE_TEXTURE_RELATIONSHIP_TYPE))
				throw CNMRException(NMR_ERROR_INVALIDRELATIONSHIPTYPEFORTEXTURE);

			std::string sPath;
			pTextureAttachment->GetPathUTF8(nullptr, 0, &cbNeededChars);
			sPath.resize(cbNeededChars);
			pTextureAttachment->GetPathUTF8(&(sPath[0]), cbNeededChars+1, &cbNeededChars);

			pTextureResource->setPath(sPath);

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelTexture2D::WriteToFileUTF8(_In_z_ LPCSTR pszFilename)
	{
		try {
			if (pszFilename == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureAttachment = pTextureResource->getTextureStream();

			if (pTextureAttachment.get() != nullptr) {
				std::string sUTF8FileName(pszFilename);
				std::wstring sUTF16FileName = fnUTF8toUTF16(sUTF8FileName);

				pTextureAttachment->writeToFile(sUTF16FileName.c_str());
			}
			else {
				throw CNMRException(NMR_ERROR_NOTEXTURESTREAM);
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelBuildItem::SetObjectTransform(_In_ MODELTRANSFORM * pmTransform)
	{
		try {
			if (!pmTransform)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			if (!m_pModelBuildItem.get())
				throw CNMRException(NMR_ERROR_INVALIDBUILDITEM);

			NMATRIX3 mMatrix = fnMATRIX3_identity ();
			int i, j;

			for (i = 0; i < 3; i++) {
				for (j = 0; j < 4; j++) {
					mMatrix.m_fields[i][j] = pmTransform->m_fFields[i][j];
				}
			}

			m_pModelBuildItem->setTransform(mMatrix);

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelTexture2D::GetAttachment(_Out_ ILib3MFModelAttachment ** ppTextureAttachment)
	{
		try {
			if (ppTextureAttachment == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			// Retrieve Path
			std::string sPath = pTextureResource->getPath();

			CModel * pModel = pTextureResource->getModel();
			__NMRASSERT(pModel);
			PModelAttachment pAttachment = pModel->findModelAttachment(sPath);
			
			CCOMObject<CCOMModelAttachment> * pCOMObject = new CCOMObject<CCOMModelAttachment>();
			pCOMObject->AddRef();
			pCOMObject->setAttachment(pAttachment);
			*ppTextureAttachment = pCOMObject;

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
	LIB3MFMETHODIMP CCOMModelTexture2D::ReadFromFile(_In_z_ LPCWSTR pwszFilename)
	{
		try {
			if (pwszFilename == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pImportStream = std::make_shared<CImportStream_COM>(pwszFilename);

			CModel * pModel = pTextureResource->getModel();
			__NMRASSERT(pModel);

			pModel->removeTextureStream(pTextureResource->getPath());
			pModel->addTextureStream(pTextureResource->getPath(), pImportStream);

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
Esempio n. 22
0
bool MinecraftDynmapProto::doSendMessage(const std::string &message_text)
{
	handleEntry(__FUNCTION__);

	JSONNode json(JSON_NODE);
	json.push_back(JSONNode("name", m_nick.c_str()));
	json.push_back(JSONNode("message", message_text.c_str()));
	std::string data = json.write();

	http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_MESSAGE, &data);

	if (resp.code == HTTP_CODE_OK) {
		JSONNode root = JSONNode::parse(resp.data.c_str());
		if (root) {
			const JSONNode &error_ = root["error"];
			if (error_) {
				std::string error = error_.as_string();
				if (error == "none") {
					return handleSuccess(__FUNCTION__);
				}
				else if (error == "not-allowed") {
					UpdateChat(NULL, Translate("Message was not sent. Probably you are sending them too fast or chat is disabled completely."));
				}
			}
		}
	}

	return handleError(__FUNCTION__);
}
	LIB3MFMETHODIMP CCOMModelTexture2D::WriteToFile(_In_z_ LPCWSTR pwszFilename)
	{
		try {
			if (pwszFilename == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureStream = pTextureResource->getTextureStream();

			if (pTextureStream.get() != nullptr) {
				pTextureStream->writeToFile(pwszFilename);
			}
			else {
				throw CNMRException(NMR_ERROR_NOTEXTURESTREAM);
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelTexture2D::ReadFromFileUTF8(_In_z_ LPCSTR pszFilename)
	{
		try {
			if (pszFilename == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			std::string sUTF8FileName(pszFilename);
			PImportStream pImportStream = fnCreateImportStreamInstance(sUTF8FileName.c_str());

			CModel * pModel = pTextureResource->getModel();
			__NMRASSERT(pModel);

			pModel->removeAttachment(pTextureResource->getPath());
			pModel->addAttachment(pTextureResource->getPath(), PACKAGE_TEXTURE_RELATIONSHIP_TYPE, pImportStream);

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
	LIB3MFMETHODIMP CCOMModelComponentsObject::AddComponent(_In_ ILib3MFModelObjectResource * pObject, _In_opt_ MODELTRANSFORM * pmTransform, _Outptr_ ILib3MFModelComponent ** ppComponent)
	{

		try {
			if (!pObject)
				throw CNMRException (NMR_ERROR_INVALIDPOINTER);

			CModelComponentsObject * pComponentsObject = getComponentsObject();
			CModel * pModel = pComponentsObject->getModel();
			__NMRASSERT(pModel);

			// Get Resource ID
			ModelResourceID nObjectID;
			HRESULT hResult = pObject->GetResourceID(&nObjectID);
			if (hResult != LIB3MF_OK)
				return hResult;

			// Find class instance
			CModelObject * pObject = pModel->findObject(nObjectID);
			if (pObject == nullptr)
				throw CNMRException(NMR_ERROR_RESOURCENOTFOUND);

			// Convert Transform, if given
			NMATRIX3 mMatrix = fnMATRIX3_identity();
			if (pmTransform) {
				int i, j;

				for (i = 0; i < 3; i++) {
					for (j = 0; j < 4; j++) {
						mMatrix.m_fields[i][j] = pmTransform->m_fFields[i][j];
					}
				}
			}

			// Create Model component
			PModelComponent pNewComponent = std::make_shared<CModelComponent>(pObject, mMatrix);
			pComponentsObject->addComponent(pNewComponent);

			// Create Component COM Wrapper
			if (ppComponent) {
				CCOMObject<CCOMModelComponent> * pResult = new CCOMObject<CCOMModelComponent>();
				pResult->AddRef();
				pResult->setComponent(pNewComponent);
				*ppComponent = pResult;
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}
	}
Esempio n. 26
0
bool MinecraftDynmapProto::doSignOn()
{
	handleEntry(__FUNCTION__);

	http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_CONFIGURATION);

	if (resp.code != HTTP_CODE_OK) {
		return handleError(__FUNCTION__, "Can't load configuration", true);
	}

	JSONNode root = JSONNode::parse(resp.data.c_str());
	if (!root)
		return false;

	/*
	const JSONNode &allowchat_ = root["allowchat"]; // boolean
	const JSONNode &allowwebchat_ = root["allowwebchat"]; // boolean
	const JSONNode &loggedin_ = root["loggedin"]; // boolean
	const JSONNode &loginEnabled_ = root["login-enabled"]; // boolean
	const JSONNode &loginRequired_ = root["webchat-requires-login"]; // boolean
	*/

	const JSONNode &title_ = root["title"]; // name of server
	const JSONNode &interval_ = root["webchat-interval"]; // limit in seconds for sending messages
	const JSONNode &rate_ = root["updaterate"]; // probably update rate for events request

	if (!title_ || !interval_ || !rate_) {
		return handleError(__FUNCTION__, "No title, interval or rate in configuration", true);
	}

	m_title = title_.as_string();
	m_interval = interval_.as_int();
	m_updateRate = rate_.as_int();
	m_cookie.clear();

	if (resp.headers.find("Set-Cookie") != resp.headers.end()) {
		// Load Session identifier
		std::string cookies = resp.headers["Set-Cookie"];

		const char *findStr = "JSESSIONID=";
		std::string::size_type start = cookies.find(findStr);
		
		if (start != std::string::npos) {
			m_cookie = cookies.substr(start, cookies.find(";") - start);
		}
	}

	if (m_cookie.empty()) {
		return handleError(__FUNCTION__, "Empty session id", true);
	}

	return handleSuccess(__FUNCTION__);
}
	LIB3MFMETHODIMP CCOMModelTexture2D::ReadFromBuffer(_In_ BYTE * pBuffer, _In_ ULONG64 cbBufferSize)
		try {
		if (pBuffer == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDPOINTER);

		HRESULT hResult;

		CModelTexture2DResource * pTextureResource = getTexture2D();
		__NMRASSERT(pTextureResource);

		CComPtr<IStream> pMemoryStream = nullptr;
		hResult = CreateStreamOnHGlobal(nullptr, true, &pMemoryStream);
		if (hResult != S_OK)
			throw CNMRException_Windows(NMR_ERROR_COULDNOTCREATESTREAM, hResult);

		BYTE * pByte = pBuffer;

		nfUint64 cbBytesLeft = cbBufferSize;
		while (cbBytesLeft > 0) {
			nfUint64 cbLength = cbBytesLeft;
			if (cbLength > NMR_IMPORTSTREAM_COPYBUFFERSIZE)
				cbLength = NMR_IMPORTSTREAM_COPYBUFFERSIZE;

			ULONG cbWrittenBytes = 0;
			hResult = pMemoryStream->Write(pByte, (nfUint32)cbLength, &cbWrittenBytes);
			if (hResult != S_OK)
				throw CNMRException_Windows(NMR_ERROR_COULDNOTWRITESTREAM, hResult);

			if (cbWrittenBytes != cbLength)
				throw CNMRException(NMR_ERROR_COULDNOTWRITEFULLDATA);
			cbBytesLeft -= cbLength;
			pByte += cbLength;
		}


		PImportStream pImportStream = std::make_shared<CImportStream_COM>(pMemoryStream);

		CModel * pModel = pTextureResource->getModel();
		__NMRASSERT(pModel);

		pModel->removeTextureStream(pTextureResource->getPath());
		pModel->addTextureStream(pTextureResource->getPath(), pImportStream);

		return handleSuccess();
	}
	catch (CNMRException & Exception) {
		return handleNMRException(&Exception);
	}
	catch (...) {
		return handleGenericException();
	}
Esempio n. 28
0
std::string MinecraftDynmapProto::doGetPage(const int request_type)
{
	handleEntry(__FUNCTION__);

	http::response resp = sendRequest(request_type);

	if (resp.code == HTTP_CODE_OK) {
		handleSuccess(__FUNCTION__);
	} else {
		handleError(__FUNCTION__);
	}

	return resp.data;
}
	LIB3MFMETHODIMP CCOMModelTexture2D::WriteToStream(_In_ IStream * pStream)
	{
		try {
			if (pStream == nullptr)
				throw CNMRException(NMR_ERROR_INVALIDPOINTER);

			CModelTexture2DResource * pTextureResource = getTexture2D();
			__NMRASSERT(pTextureResource);

			PImportStream pTextureStream = pTextureResource->getTextureStream();

			if (pTextureStream.get() != nullptr) {
				nfUint64 cbStreamSize = pTextureStream->retrieveSize();

				pTextureStream->seekPosition(0, true);
				std::array<nfByte, NMR_IMPORTSTREAM_COPYBUFFERSIZE> pBuffer;
				
				nfUint64 cbBytesLeft = cbStreamSize;
				while (cbBytesLeft > 0) {
					nfUint64 cbLength = cbBytesLeft;
					if (cbLength > NMR_IMPORTSTREAM_COPYBUFFERSIZE)
						cbLength = NMR_IMPORTSTREAM_COPYBUFFERSIZE;

					ULONG cbWrittenBytes = 0;
					pTextureStream->readBuffer(&pBuffer[0], cbLength, true);
					HRESULT hResult = pStream->Write(&pBuffer[0], (nfUint32) cbLength, &cbWrittenBytes);
					if (hResult != S_OK)
						throw CNMRException_Windows(NMR_ERROR_COULDNOTWRITESTREAM, hResult);
					
					if (cbWrittenBytes != cbLength)
						throw CNMRException(NMR_ERROR_COULDNOTWRITEFULLDATA);
					cbBytesLeft -= cbLength;
				}

			}
			else {
				throw CNMRException(NMR_ERROR_NOTEXTURESTREAM);
			}

			return handleSuccess();
		}
		catch (CNMRException & Exception) {
			return handleNMRException(&Exception);
		}
		catch (...) {
			return handleGenericException();
		}

	}
Esempio n. 30
0
bool MinecraftDynmapProto::doEvents()
{
	handleEntry(__FUNCTION__);

	// Get update
	http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_EVENTS);

	if (resp.code != HTTP_CODE_OK)
		return handleError(__FUNCTION__, "Response is not code 200");

	JSONNode root = JSONNode::parse(resp.data.c_str());
	if (!root)
		return handleError(__FUNCTION__, "Invalid JSON response");

	const JSONNode &timestamp_ = root["timestamp"];
	if (!timestamp_)
		return handleError(__FUNCTION__, "Received no timestamp node");

	m_timestamp = timestamp_.as_string();

	const JSONNode &updates_ = root["updates"];
	if (!updates_)
		return handleError(__FUNCTION__, "Received no updates node");

	for (auto it = updates_.begin(); it != updates_.end(); ++it) {
		const JSONNode &type_ = (*it)["type"];
		if (type_ && type_.as_string() == "chat") {
			const JSONNode &time_ = (*it)["timestamp"];
			// const JSONNode &source_ = (*it)["source"]; // e.g. "web"
			const JSONNode &playerName_ = (*it)["playerName"];
			const JSONNode &message_ = (*it)["message"];
			// TODO: there are also "channel" and "account" elements

			if (!time_ || !playerName_ || !message_) {
				debugLog(_T("Error: No player name, time or text for message"));
				continue;
			}

			time_t timestamp = utils::time::from_string(time_.as_string());
			std::string name = playerName_.as_string();
			std::string message = message_.as_string();

			debugLog(_T("Received message: [%d] %s -> %s"), timestamp, name.c_str(), message.c_str());
			UpdateChat(name.c_str(), message.c_str(), timestamp);
		}
	}

	return handleSuccess(__FUNCTION__);
}