Project

General

Profile

SO3Engine
flashhandler.h
Go to the documentation of this file.
1/*
2 This file is part of Hikari, a library that allows developers
3 to use Flash in their Ogre3D applications.
4
5 Copyright (C) 2008 Adam J. Simmons
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22/*********************************************************************************************
23 ___ ___ .__ __ .__
24 / | \|__| | _______ _______|__|
25/ ~ \ | |/ /\__ \\_ __ \ |
26\ Y / | < / __ \| | \/ |
27 \___|_ /|__|__|_ \‍(____ /__| |__| v0.4
28 \/ \/ \/
29
30
31* Zed Games PC Development Team - Jaime Crespillo Vilchez (jcrespillo@zed.com)
32* Build: 0.1 - Date: 13/10/2008
33* Undocked version of Hikari Lib
34* Brief: Changes in string conversions before and after serialization to support basic types
35**********************************************************************************************/
36
37#ifndef __FlashHandler_H__
38#define __FlashHandler_H__
39
40//#import "PROGID:ShockwaveFlash.ShockwaveFlash" named_guids
41
42// generate the Flash.tlh
43#import "../../../../redist/bin/Flash.ocx" named_guids
44
46#include <sstream>
47
48namespace Hikari {
49namespace Impl {
50
51inline void replaceAll(std::wstring &sourceStr, const std::wstring &replaceWhat, const std::wstring &replaceWith)
52{
53 for(size_t i = sourceStr.find(replaceWhat); i != std::wstring::npos; i = sourceStr.find(replaceWhat, i + replaceWith.length()))
54 {
55 sourceStr.erase(i, replaceWhat.length());
56 sourceStr.insert(i, replaceWith);
57 }
58}
59
60std::wstring ConvertToWString( const std::string& str )
61{
62 std::wstring temp( str.length(), L' ' );
63 std::copy( str.begin(), str.end(), temp.begin() );
64 return temp;
65}
66
67std::wstring serializeValue(const FlashValue& value)
68{
69 switch(value.getType())
70 {
71 case FT_NULL:
72 return L"<null/>";
73 case FT_BOOLEAN:
74 return value.getBool()? L"<true/>" : L"<false/>";
75 case FT_NUMBER:
76 {
77 static std::wstringstream converter;
78 converter.clear();
79 converter.str(L"");
80 converter << value.getNumber();
81
82 return L"<number>" + converter.str() + L"</number>";
83 }
84 case FT_STRING:
85 {
87 std::wstring str2 = ConvertToWString(value.getString());
88
89 //replace reserved characters
90 replaceAll(str2, L"&", L"&amp;");
91 replaceAll(str2, L"\"", L"&quot;");
92 replaceAll(str2, L"'", L"&apos;");
93 replaceAll(str2, L"<", L"&lt;");
94 replaceAll(str2, L">", L"&gt;");
95
96 return L"<string>" + str2 + L"</string>";
97 }
98 case FT_WSTRING:
99 {
101 std::wstring str2 = value.getWString();
102
103 //replace reserved characters
104 replaceAll(str2, L"&", L"&amp;");
105 replaceAll(str2, L"\"", L"&quot;");
106 replaceAll(str2, L"'", L"&apos;");
107 replaceAll(str2, L"<", L"&lt;");
108 replaceAll(str2, L">", L"&gt;");
109
110 return L"<string>" + str2 + L"</string>";
111 }
112 }
113
114 return L"<null/>";
115}
116
117FlashValue deserializeValue(const std::wstring& valueStr)
118{
119 if(valueStr == L"<null/>")
120 return FLASH_VOID;
121 else if(valueStr == L"<true/>")
122 return FlashValue(true);
123 else if(valueStr == L"<false/>")
124 return FlashValue(false);
125
126 if(valueStr.substr(0, 8) == L"<string>")
127 {
128 std::wstring stringVal = valueStr.substr(8, valueStr.find(L"</string>", 8) - 8);
129
130 replaceAll(stringVal, L"&quot;", L"\"");
131 replaceAll(stringVal, L"&apos;", L"'");
132 replaceAll(stringVal, L"&lt;", L"<");
133 replaceAll(stringVal, L"&gt;", L">");
134 replaceAll(stringVal, L"&amp;", L"&");
135
136 return FlashValue(stringVal);
137 }
138 else if(valueStr.substr(0, 8) == L"<number>")
139 {
140 static std::wstringstream converter;
141 converter.clear();
142 float numValue = 0;
143
144 converter.str(valueStr.substr(8, valueStr.find(L"</number>", 8) - 8));
145 converter >> numValue;
146
147 return FlashValue(numValue);
148 }
149
150 return FLASH_VOID;
151}
152
153std::wstring serializeInvocation(const std::wstring& funcName, const Arguments& args)
154{
155 std::wstring result;
156
157 result += L"<invoke name=\"" + funcName + L"\" returntype=\"xml\">";
158
159 if(args.size())
160 {
161 result += L"<arguments>";
162 for(Arguments::const_iterator i = args.begin(); i != args.end(); i++)
163 result += serializeValue(*i);
164 result += L"</arguments>";
165 }
166
167 result += L"</invoke>";
168
169 return result;
170}
171
172bool deserializeInvocation(const std::wstring& xmlString, std::wstring& funcName, Arguments& args)
173{
174 size_t indexA = 0;
175 size_t indexB = 0;
176
177 if((indexA = xmlString.find(L"<invoke name=\"")) == std::wstring::npos)
178 return false;
179
180 if((indexB = xmlString.find(L"\"", indexA + 14)) == std::wstring::npos)
181 return false;
182
183 funcName = xmlString.substr(indexA + 14, indexB - (indexA + 14));
184 args.clear();
185
186 if((indexA = xmlString.find(L"<arguments>", indexB)) == std::wstring::npos)
187 return true;
188
189 indexA += 11;
190 std::wstring argString(xmlString.substr(indexA, xmlString.find(L"</arguments>", indexA) - indexA));
191
192 for(indexA = 0, indexB = 0; true;)
193 {
194 if((indexA = argString.find(L"<", indexB)) == std::wstring::npos)
195 break;
196
197 if((indexB = argString.find(L">", indexA)) == std::wstring::npos)
198 break;
199
200 if(argString[indexB-1] != L'/')
201 {
202 if((indexB = argString.find(L">", indexB + 1)) == std::wstring::npos)
203 break;
204 }
205
206 args.push_back(deserializeValue(argString.substr(indexA, indexB + 1 - indexA)));
207 }
208
209 return true;
210}
211
212class FlashHandler : public ShockwaveFlashObjects::_IShockwaveFlashEvents
213{
214public:
215 LPCONNECTIONPOINT connectionPoint;
216 DWORD cookie;
219
220public:
222 {
223 }
224
226 {
227 owner->comCount--;
228 }
229
231 {
232 this->owner = owner;
233 owner->comCount++;
234
235 HRESULT result = NOERROR;
236 LPCONNECTIONPOINTCONTAINER cPointContainer = 0;
237
238 if((owner->flashInterface->QueryInterface(IID_IConnectionPointContainer, (void**)&cPointContainer) == S_OK) &&
239 (cPointContainer->FindConnectionPoint(__uuidof(ShockwaveFlashObjects::_IShockwaveFlashEvents), &connectionPoint) == S_OK))
240 {
241 IDispatch* dispatch = 0;
242 QueryInterface(__uuidof(IDispatch), (void**)&dispatch);
243
244 if(dispatch)
245 {
246 result = connectionPoint->Advise((ShockwaveFlashObjects::_IShockwaveFlashEvents *)dispatch, &cookie);
247 dispatch->Release();
248 }
249 }
250
251 if(cPointContainer)
252 cPointContainer->Release();
253
254 return result;
255 }
256
257 HRESULT Shutdown()
258 {
259 HRESULT result = S_OK;
260
262 {
263 if(cookie)
264 {
265 result = connectionPoint->Unadvise(cookie);
266 cookie = 0;
267 }
268
269 connectionPoint->Release();
270 connectionPoint = 0;
271 }
272
273 return result;
274 }
275
276 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID* ppv)
277 {
278 *ppv = 0;
279
280 if(riid == IID_IUnknown)
281 {
282 *ppv = (LPUNKNOWN)this;
283 AddRef();
284 return S_OK;
285 }
286 else if(riid == IID_IDispatch)
287 {
288 *ppv = (IDispatch*)this;
289 AddRef();
290 return S_OK;
291 }
292 else if(riid == __uuidof(ShockwaveFlashObjects::_IShockwaveFlashEvents))
293 {
294 *ppv = (ShockwaveFlashObjects::_IShockwaveFlashEvents*) this;
295 AddRef();
296 return S_OK;
297 }
298 else
299 {
300 return E_NOTIMPL;
301 }
302 }
303
304 ULONG STDMETHODCALLTYPE AddRef()
305 {
306 return ++refCount;
307 }
308
309 ULONG STDMETHODCALLTYPE Release()
310 {
311 --refCount;
312 int localCount = refCount;
313 if(!refCount)
314 delete this;
315
316 return localCount;
317 }
318
319 virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT* pctinfo)
320 {
321 return E_NOTIMPL;
322 }
323
324 virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
325 {
326 return E_NOTIMPL;
327 }
328
329 virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid,DISPID* rgDispId)
330 {
331 return E_NOTIMPL;
332 }
333
334 virtual HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,
335 WORD wFlags, ::DISPPARAMS __RPC_FAR *pDispParams, VARIANT __RPC_FAR *pVarResult,
336 ::EXCEPINFO __RPC_FAR *pExcepInfo, UINT __RPC_FAR *puArgErr)
337 {
338 switch(dispIdMember)
339 {
340 case 0x7a6:
341 break;
342 case 0x96:
343 if((pDispParams->cArgs == 2) && (pDispParams->rgvarg[0].vt == VT_BSTR) && (pDispParams->rgvarg[1].vt == VT_BSTR))
344 FSCommand(pDispParams->rgvarg[1].bstrVal, pDispParams->rgvarg[0].bstrVal);
345 break;
346 case 0xC5:
347 if((pDispParams->cArgs == 1) && (pDispParams->rgvarg[0].vt == VT_BSTR))
348 FlashCall(pDispParams->rgvarg[0].bstrVal);
349 break;
350 case DISPID_READYSTATECHANGE:
351 break;
352 default:
353 return DISP_E_MEMBERNOTFOUND;
354 }
355
356 return NOERROR;
357 }
358
359 HRESULT OnReadyStateChange (long newState)
360 {
361 return S_OK;
362 }
363
364 HRESULT OnProgress(long percentDone )
365 {
366 return S_OK;
367 }
368
369 HRESULT FSCommand (_bstr_t command, _bstr_t args)
370 {
371 // TODO: Handle FSCommand
372
373 return S_OK;
374 }
375
376 HRESULT FlashCall (_bstr_t request)
377 {
378 owner->handleFlashCall((wchar_t*)request);
379
380 return S_OK;
381 }
382};
383
384}
385}
386
387#endif
unsigned int UINT
Definition SO3Android.h:58
std::wstring getWString() const
bool getBool() const
float getNumber() const
short getType() const
std::string getString() const
virtual void handleFlashCall(const std::wstring &xmlString)=0
ShockwaveFlashObjects::IShockwaveFlash * flashInterface
HRESULT OnProgress(long percentDone)
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
HRESULT FSCommand(_bstr_t command, _bstr_t args)
ULONG STDMETHODCALLTYPE Release()
virtual HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, ::DISPPARAMS __RPC_FAR *pDispParams, VARIANT __RPC_FAR *pVarResult, ::EXCEPINFO __RPC_FAR *pExcepInfo, UINT __RPC_FAR *puArgErr)
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID *ppv)
HRESULT FlashCall(_bstr_t request)
LPCONNECTIONPOINT connectionPoint
virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *pctinfo)
HRESULT Init(IFlashControl *owner)
ULONG STDMETHODCALLTYPE AddRef()
HRESULT OnReadyStateChange(long newState)
FlashValue deserializeValue(const std::wstring &valueStr)
void replaceAll(std::wstring &sourceStr, const std::wstring &replaceWhat, const std::wstring &replaceWith)
std::wstring ConvertToWString(const std::string &str)
std::wstring serializeValue(const FlashValue &value)
std::wstring serializeInvocation(const std::wstring &funcName, const Arguments &args)
bool deserializeInvocation(const std::wstring &xmlString, std::wstring &funcName, Arguments &args)
@ FT_WSTRING
Definition FlashValue.h:55
@ FT_STRING
Definition FlashValue.h:54
@ FT_NUMBER
Definition FlashValue.h:53
@ FT_NULL
Definition FlashValue.h:51
@ FT_BOOLEAN
Definition FlashValue.h:52
_HikariExport std::vector< FlashValue > Arguments
Definition FlashValue.h:188
STBI_EXTERN unsigned long const char * str
Definition stb_image.h:1182