win32: add+use CXML wrapper class

This commit is contained in:
OV2 2012-10-18 15:44:42 +02:00
parent 59b0a71a43
commit 88f3702a6a
7 changed files with 238 additions and 81 deletions

View File

@ -187,6 +187,7 @@
#include "wsnes9x.h"
#include <Dxerr.h>
#include <commctrl.h>
#include "CXML.h"
#include "../filter/hq2x.h"
#include "../filter/2xsai.h"
@ -425,17 +426,9 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file)
TCHAR folder[MAX_PATH];
TCHAR rubyLUTfileName[MAX_PATH];
TCHAR *slash;
char *shaderText = NULL;
TCHAR errorMsg[MAX_PATH + 50];
IXMLDOMDocument * pXMLDoc = NULL;
IXMLDOMElement * pXDE = NULL;
IXMLDOMNode * pXDN = NULL;
BSTR queryString, nodeContent;
HRESULT hr;
shaderTimer = 1.0f;
shaderTimeStart = 0;
shaderTimeElapsed = 0;
@ -453,76 +446,20 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file)
if (file == NULL || *file==TEXT('\0'))
return true;
hr = CoCreateInstance(CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pXMLDoc));
CXML xml;
if(FAILED(hr)) {
MessageBox(NULL, TEXT("Error creating XML Parser"), TEXT("Shader Loading Error"),
MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(!xml.loadXmlFile(file))
return false;
VARIANT fileName;
VARIANT_BOOL ret;
fileName.vt = VT_BSTR;
#ifdef UNICODE
fileName.bstrVal = SysAllocString(file);
#else
wchar_t tempfilename[MAX_PATH];
MultiByteToWideChar(CP_UTF8,0,file,-1,tempfilename,MAX_PATH);
fileName.bstrVal = SysAllocString(tempfilename);
#endif
hr = pXMLDoc->load(fileName,&ret);
SysFreeString(fileName.bstrVal);
TCHAR *lang = xml.getAttribute(TEXT("/shader"),TEXT("language"));
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading HLSL shader file:\n%s"),file);
if(lstrcmpi(lang,TEXT("hlsl"))) {
_stprintf(errorMsg,TEXT("Shader language is <%s>, expected <HLSL> in file:\n%s"),lang,file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
pXMLDoc->Release();
return false;
}
VARIANT attributeValue;
BSTR attributeName;
hr = pXMLDoc->get_documentElement(&pXDE);
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading root element from file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
pXMLDoc->Release();
return false;
}
attributeName=SysAllocString(L"language");
pXDE->getAttribute(attributeName,&attributeValue);
SysFreeString(attributeName);
pXDE->Release();
if(attributeValue.vt!=VT_BSTR || lstrcmpiW(attributeValue.bstrVal,L"hlsl")) {
_stprintf(errorMsg,TEXT("Shader language is <%s>, expected <HLSL> in file:\n%s"),attributeValue.bstrVal,file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal);
pXMLDoc->Release();
return false;
}
if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal);
queryString=SysAllocString(L"/shader/source");
hr = pXMLDoc->selectSingleNode(queryString,&pXDN);
SysFreeString(queryString);
if(hr == S_OK) {
hr = pXDN->get_text(&nodeContent);
if(hr == S_OK) {
int requiredChars = WideCharToMultiByte(CP_ACP,0,nodeContent,-1,shaderText,0,NULL,NULL);
shaderText = new char[requiredChars];
WideCharToMultiByte(CP_UTF8,0,nodeContent,-1,shaderText,requiredChars,NULL,NULL);
}
SysFreeString(nodeContent);
pXDN->Release();
pXDN = NULL;
}
pXMLDoc->Release();
TCHAR *shaderText = xml.getNodeContent(TEXT("/shader/source"));
if(!shaderText) {
_stprintf(errorMsg,TEXT("No HLSL shader program in file:\n%s"),file);
@ -532,11 +469,17 @@ bool CDirect3D::SetShaderHLSL(const TCHAR *file)
}
LPD3DXBUFFER pBufferErrors = NULL;
hr = D3DXCreateEffect( pDevice,shaderText,strlen(shaderText),NULL, NULL,
#ifdef UNICODE
HRESULT hr = D3DXCreateEffect( pDevice,WideToCP(shaderText,CP_ACP),strlen(WideToCP(shaderText,CP_ACP)),NULL, NULL,
D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect,
&pBufferErrors );
delete[] shaderText;
if( FAILED(hr) ) {
#else
HRESULT hr = D3DXCreateEffect( pDevice,shaderText,strlen(shaderText),NULL, NULL,
D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect,
&pBufferErrors );
#endif
if( FAILED(hr) ) {
_stprintf(errorMsg,TEXT("Error parsing HLSL shader file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
if(pBufferErrors) {

162
win32/CXML.cpp Normal file
View File

@ -0,0 +1,162 @@
#include "CXML.h"
#include <windows.h>
#include "_tfwopen.h"
#ifndef UNICODE
#define _tFromTCHAR(x) CPToWide(x,CP_ACP)
#define _tToTCHAR(x) WideToCP(x,CP_ACP)
#else
#define _tFromTCHAR
#define _tToTCHAR
#endif
CXML::CXML(void)
{
pXMLDoc = NULL;
xmlLoaded = false;
nodeContent = NULL;
attrValue = NULL;
}
CXML::~CXML(void)
{
unloadXml();
}
void CXML::unloadXml()
{
if(pXMLDoc) {
pXMLDoc->Release();
pXMLDoc = NULL;
}
if(nodeContent) {
delete [] nodeContent;
nodeContent = NULL;
}
if(attrValue) {
delete [] attrValue;
attrValue = NULL;
}
xmlLoaded = false;
}
bool CXML::loadXmlFile(TCHAR const *file)
{
unloadXml();
if(!file)
return false;
TCHAR errorMsg[MAX_PATH + 50];
HRESULT hr = CoCreateInstance(CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pXMLDoc));
if(FAILED(hr)) {
MessageBox(NULL, TEXT("Error creating XML Parser"), TEXT("XML Error"),
MB_OK|MB_ICONEXCLAMATION);
pXMLDoc = NULL;
return false;
}
VARIANT fileName;
VARIANT_BOOL ret;
fileName.vt = VT_BSTR;
fileName.bstrVal = SysAllocString(_tFromTCHAR(file));
hr = pXMLDoc->load(fileName,&ret);
SysFreeString(fileName.bstrVal);
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading XML file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("XML Error"),
MB_OK|MB_ICONEXCLAMATION);
unloadXml();
return false;
}
hr = pXMLDoc->get_documentElement(&pXrootElement);
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading root element from file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("XML Error"), MB_OK|MB_ICONEXCLAMATION);
unloadXml();
return false;
}
xmlLoaded = true;
return true;
}
IXMLDOMNode *CXML::getNode(TCHAR const *searchNode)
{
IXMLDOMNode *pXDN;
BSTR queryString=SysAllocString(_tFromTCHAR(searchNode));
HRESULT hr = pXMLDoc->selectSingleNode(queryString,&pXDN);
SysFreeString(queryString);
return pXDN;
}
TCHAR *CXML::getAttribute(TCHAR const *searchNode, TCHAR const *attrName)
{
IXMLDOMNode *pXDN = getNode(searchNode);
if(!pXDN)
return NULL;
VARIANT attributeValue;
BSTR attributeName;
IXMLDOMElement * pXDE = NULL;
HRESULT hr = pXDN->QueryInterface(IID_PPV_ARGS(&pXDE));
if(FAILED(hr)) {
pXDN->Release();
return NULL;
}
attributeName=SysAllocString(_tFromTCHAR(attrName));
pXDE->getAttribute(attributeName,&attributeValue);
SysFreeString(attributeName);
pXDE->Release();
if(attributeValue.vt!=VT_BSTR)
return NULL;
if(attrValue) {
delete [] attrValue;
attrValue = NULL;
}
attrValue = new TCHAR[lstrlen(_tToTCHAR(attributeValue.bstrVal)) + 1];
lstrcpy(attrValue,_tToTCHAR(attributeValue.bstrVal));
SysFreeString(attributeValue.bstrVal);
return attrValue;
}
TCHAR *CXML::getNodeContent(TCHAR const *searchNode)
{
IXMLDOMNode *pXDN = getNode(searchNode);
if(!pXDN)
return NULL;
BSTR nodeText;
HRESULT hr = pXDN->get_text(&nodeText);
pXDN->Release();
if(hr != S_OK)
return NULL;
if(nodeContent) {
delete [] nodeContent;
nodeContent = NULL;
}
nodeContent = new TCHAR[lstrlen(_tToTCHAR(nodeText)) + 1];
lstrcpy(nodeContent,_tToTCHAR(nodeText));
SysFreeString(nodeText);
return nodeContent;
}

29
win32/CXML.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef CXML_H
#define CXML_H
#include "msxml2.h"
#include <tchar.h>
class CXML
{
private:
IXMLDOMDocument * pXMLDoc;
IXMLDOMElement * pXrootElement;
bool xmlLoaded;
TCHAR *nodeContent;
TCHAR *attrValue;
IXMLDOMNode *getNode(TCHAR const *searchNode);
public:
CXML(void);
~CXML(void);
bool loadXmlFile(TCHAR const *xmlFile);
void unloadXml();
TCHAR *getAttribute(TCHAR const *searchNode, TCHAR const *attrName);
TCHAR *getNodeContent(TCHAR const *searchNode);
};
#endif

View File

@ -192,10 +192,16 @@ WideToUtf8::WideToUtf8(const wchar_t *wideChars) {
WideCharToMultiByte(CP_UTF8,0,wideChars,-1,utf8Chars,requiredChars,NULL,NULL);
}
MS932ToWide::MS932ToWide(const char *ms932Chars) {
int requiredChars = MultiByteToWideChar(932,0,ms932Chars,-1,wideChars,0);
CPToWide::CPToWide(const char *chars, unsigned int cp) {
int requiredChars = MultiByteToWideChar(cp,0,chars,-1,wideChars,0);
wideChars = new wchar_t[requiredChars];
MultiByteToWideChar(932,0,ms932Chars,-1,wideChars,requiredChars);
MultiByteToWideChar(cp,0,chars,-1,wideChars,requiredChars);
}
WideToCP::WideToCP(const wchar_t *wideChars, unsigned int cp) {
int requiredChars = WideCharToMultiByte(cp,0,wideChars,-1,cpchars,0,NULL,NULL);
cpchars = new char[requiredChars];
WideCharToMultiByte(cp,0,wideChars,-1,cpchars,requiredChars,NULL,NULL);
}
extern "C" FILE *_tfwopen(const char *filename, const char *mode ) {

View File

@ -215,15 +215,24 @@ public:
operator char *() { return utf8Chars; }
};
class MS932ToWide {
class CPToWide {
private:
wchar_t *wideChars;
public:
MS932ToWide(const char *ms932Chars);
~MS932ToWide() { delete [] wideChars; }
CPToWide(const char *chars, unsigned int cp);
~CPToWide() { delete [] wideChars; }
operator wchar_t *() { return wideChars; }
};
class WideToCP {
private:
char *cpchars;
public:
WideToCP(const wchar_t *wideChars, unsigned int cp);
~WideToCP() { delete [] cpchars; }
operator char *() { return cpchars; }
};
namespace std {
class u8nifstream: public std::ifstream
{

View File

@ -3014,6 +3014,14 @@
RelativePath=".\AVIOutput.h"
>
</File>
<File
RelativePath=".\CXML.cpp"
>
</File>
<File
RelativePath=".\CXML.h"
>
</File>
<File
RelativePath=".\InputCustom.cpp"
>

View File

@ -212,7 +212,7 @@
#ifdef UNICODE
#define _tToChar WideToUtf8
#define _tFromChar Utf8ToWide
#define _tFromMS932 MS932ToWide
#define _tFromMS932(x) CPToWide(x,932)
#else
#define _tToChar
#define _tFromChar