npPlugin.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <string>
  23. #include <vector>
  24. #include "npWebGamePlugin.h"
  25. #include "../common/webCommon.h"
  26. // Functions exported from the browser to our plugin
  27. static NPNetscapeFuncs NPNFuncs;
  28. // Set once both the web page and plugin are fully loaded (including any possible Javascript <-> TorqueScript exports/imports)
  29. static BOOL gInitialized = false;
  30. #ifndef WIN32
  31. // Entry points into our plugin for Safari
  32. extern "C" {
  33. #pragma GCC visibility push(default)
  34. NPError NP_Initialize (NPNetscapeFuncs *browser);
  35. NPError NP_GetEntryPoints (NPPluginFuncs *plugin);
  36. NPError NP_Shutdown();
  37. #pragma GCC visibility pop
  38. }
  39. #endif
  40. // Converts a NPVARIANT variable to a C string representation
  41. const char* MY_NPVARIANT_TO_STRING(const NPVariant& f)
  42. {
  43. static std::string result;
  44. char r[1024] = {0};
  45. if (NPVARIANT_IS_VOID(f) || NPVARIANT_IS_NULL(f))
  46. return "";
  47. if (NPVARIANT_IS_STRING(f))
  48. {
  49. NPString str = NPVARIANT_TO_STRING(f);
  50. result = std::string(str.UTF8Characters, str.UTF8Length);
  51. return result.c_str();
  52. }
  53. if (NPVARIANT_IS_BOOLEAN(f))
  54. {
  55. if (NPVARIANT_TO_BOOLEAN(f))
  56. return "1";
  57. return "0";
  58. }
  59. if (NPVARIANT_IS_INT32(f))
  60. {
  61. sprintf(r, "%i", NPVARIANT_TO_INT32(f));
  62. result = r;
  63. return result.c_str();
  64. }
  65. if (NPVARIANT_IS_DOUBLE(f))
  66. {
  67. sprintf(r, "%f", NPVARIANT_TO_DOUBLE(f));
  68. result = r;
  69. return result.c_str();
  70. }
  71. return "";
  72. }
  73. // Javascript -> TorqueScript function calling (with parser)
  74. const char* CallScript(const char* code)
  75. {
  76. std::string scode = code;
  77. const char* sig = scode.c_str();
  78. // do not allow large strings which could be used maliciously
  79. if (scode.length() > 255 || !gInitialized)
  80. {
  81. return "";
  82. }
  83. // data buffers for laying out data in a Torque 3D console friendly manner
  84. char nameSpace[256];
  85. char fname[256];
  86. char argv[256][256];
  87. char* argvv[256];
  88. int argc = 0;
  89. int argBegin = 0;
  90. memset(nameSpace, 0, 256);
  91. memset(fname, 0, 256);
  92. memset(argv, 0, 256 * 256);
  93. for (int i = 0; i < scode.length(); i++)
  94. {
  95. if (sig[i] == ')' || sig[i] == ';')
  96. {
  97. //scan out last arg is any
  98. char dummy[256];
  99. memset(dummy, 0, 256);
  100. WebCommon::StringCopy(dummy, &sig[argBegin], i - argBegin);
  101. if (strlen(dummy))
  102. {
  103. strcpy(argv[argc], dummy);
  104. argvv[argc] = argv[argc];
  105. argc++;
  106. }
  107. break; // done
  108. }
  109. // handle namespace
  110. if (sig[i]==':')
  111. {
  112. if (nameSpace[0] || fname[0])
  113. {
  114. return "";
  115. }
  116. if (i > 0 && sig[i-1] == ':')
  117. {
  118. if (i - 2 > 0)
  119. WebCommon::StringCopy(nameSpace, sig, i - 1);
  120. }
  121. continue;
  122. }
  123. // arguments begin
  124. if (sig[i] == '(' )
  125. {
  126. if (fname[0] || i < 1)
  127. {
  128. return "";
  129. }
  130. //everything before this is function name, minus nameSpace
  131. if (nameSpace[0])
  132. {
  133. int nlen = strlen(nameSpace);
  134. WebCommon::StringCopy(fname, &sig[nlen + 2], i - nlen - 2);
  135. }
  136. else
  137. {
  138. WebCommon::StringCopy(fname, sig, i);
  139. }
  140. WebCommon::StringCopy(argv[0], fname, strlen(fname)+1);
  141. argvv[0] = argv[0];
  142. argc++;
  143. argBegin = i + 1;
  144. }
  145. // argument
  146. if (sig[i] == ',' )
  147. {
  148. if (argBegin >= i || argc == 255)
  149. {
  150. return "";
  151. }
  152. WebCommon::StringCopy(argv[argc], &sig[argBegin], i - argBegin);
  153. argvv[argc] = argv[argc];
  154. argc++;
  155. argBegin = i + 1;
  156. }
  157. }
  158. static std::string retVal;
  159. if (fname[0])
  160. {
  161. // call into the Torque 3D shared library (console system) and get return value
  162. retVal = torque_callsecurefunction(nameSpace, fname, argc, (const char **) argvv);
  163. return retVal.c_str();
  164. }
  165. return "";
  166. }
  167. // TorqueScript -> JavaScript
  168. const char* CallJavaScriptFunction(const char* name, int numArguments, const char* argv[])
  169. {
  170. // our plugin instance
  171. NPP pNPInstance = NPWebGamePlugin::sInstance->mInstance;
  172. // holds the generated Javascript encoded as a NPString
  173. NPString npScript;
  174. // retrieve our plugin object from the browser
  175. NPObject* pluginObject;
  176. if (NPERR_NO_ERROR != NPNFuncs.getvalue(pNPInstance, NPNVPluginElementNPObject, &pluginObject))
  177. {
  178. return NULL;
  179. }
  180. // generate Javascript to be evaluated
  181. std::string script = name;
  182. script += "(";
  183. for (int i = 1; i < numArguments; i++)
  184. {
  185. script += "\"";
  186. script += argv[i];
  187. script += "\"";
  188. if ( i + 1 < numArguments)
  189. script += ", ";
  190. }
  191. script += ");";
  192. //encode as a NPString
  193. npScript.UTF8Characters = script.c_str();
  194. npScript.UTF8Length = script.length();
  195. // finally, have browser evaluate our script and get the return value
  196. NPVariant result;
  197. NPNFuncs.evaluate(pNPInstance,pluginObject,&npScript,&result);
  198. return MY_NPVARIANT_TO_STRING(result);
  199. }
  200. // the sole entry point for Torque 3D console system into our browser plugin (handed over as a function pointer)
  201. static const char * MyStringCallback(void *obj, int argc, const char* argv[])
  202. {
  203. static char ret[4096];
  204. strcpy(ret,CallJavaScriptFunction(argv[0], argc, argv));
  205. return ret;
  206. }
  207. // these can be added on the page before we're initialized, so we cache until we're ready for them
  208. typedef struct
  209. {
  210. std::string jsCallback; //javascript function name
  211. unsigned int numArguments; //the number of arguments it takes
  212. } JavasScriptExport;
  213. static std::vector<JavasScriptExport> gJavaScriptExports;
  214. // this actually exports the function to the Torque 3D console system
  215. // we do this in two steps as we can't guarantee that Torque 3D is initialized on web page
  216. // before JavaScript calls are made
  217. void ExportFunctionInternal(const JavasScriptExport& jsexport)
  218. {
  219. torque_exportstringcallback(MyStringCallback,"JS",jsexport.jsCallback.c_str(),"",jsexport.numArguments,jsexport.numArguments);
  220. }
  221. // invoked via the Javascript plugin object startup() method once the page/plugin are fully loaded
  222. void Startup()
  223. {
  224. if (gInitialized)
  225. return;
  226. // actually do the export on any cached functions
  227. gInitialized = true;
  228. std::vector<JavasScriptExport>::iterator i;
  229. for (i = gJavaScriptExports.begin(); i != gJavaScriptExports.end();i++)
  230. {
  231. ExportFunctionInternal(*i);
  232. }
  233. // setup the secure TorqueScript function calls we can call from Javascript (see webConfig.h)
  234. WebCommon::AddSecureFunctions();
  235. }
  236. // Export a Javascript function to the Torque 3D console system, possibly caching it
  237. void ExportFunction(const char* callback, int numArguments)
  238. {
  239. JavasScriptExport jsexport;
  240. jsexport.jsCallback = callback;
  241. jsexport.numArguments = numArguments;
  242. if (!gInitialized)
  243. {
  244. //queue it up
  245. gJavaScriptExports.push_back(jsexport);
  246. }
  247. else
  248. {
  249. ExportFunctionInternal(jsexport);
  250. }
  251. }
  252. // NP Plugin Interface
  253. // Our plugin object structure "inherited" from NPObject
  254. typedef struct
  255. {
  256. // NPObject fields
  257. NPClass *_class;
  258. uint32_t referenceCount;
  259. // Here begins our custom fields (well, field)
  260. // Platform specific game plugin class (handles refresh, sizing, initialization of Torque 3D, etc)
  261. NPWebGamePlugin* webPlugin;
  262. } PluginObject;
  263. static PluginObject* gPluginObject = NULL;
  264. // interface exports for our plugin that are expected to the browser
  265. void pluginInvalidate ();
  266. bool pluginHasProperty (NPClass *theClass, NPIdentifier name);
  267. bool pluginHasMethod (NPObject *npobj, NPIdentifier name);
  268. bool pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant);
  269. bool pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant);
  270. bool pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, uint32_t argCount, NPVariant *result);
  271. bool pluginInvokeDefault (PluginObject *obj, NPVariant *args, uint32_t argCount, NPVariant *result);
  272. NPObject *pluginAllocate (NPP npp, NPClass *theClass);
  273. void pluginDeallocate (PluginObject *obj);
  274. static NPClass _pluginFunctionPtrs = {
  275. NP_CLASS_STRUCT_VERSION,
  276. (NPAllocateFunctionPtr) pluginAllocate,
  277. (NPDeallocateFunctionPtr) pluginDeallocate,
  278. (NPInvalidateFunctionPtr) pluginInvalidate,
  279. (NPHasMethodFunctionPtr) pluginHasMethod,
  280. (NPInvokeFunctionPtr) pluginInvoke,
  281. (NPInvokeDefaultFunctionPtr) pluginInvokeDefault,
  282. (NPHasPropertyFunctionPtr) pluginHasProperty,
  283. (NPGetPropertyFunctionPtr) pluginGetProperty,
  284. (NPSetPropertyFunctionPtr) pluginSetProperty,
  285. };
  286. // versioning information
  287. static bool identifiersInitialized = false;
  288. #define ID_VERSION_PROPERTY 0
  289. #define NUM_PROPERTY_IDENTIFIERS 1
  290. static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS];
  291. static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = {
  292. "version",
  293. };
  294. // methods that are callable on the plugin from Javascript
  295. #define ID_SETVARIABLE_METHOD 0
  296. #define ID_GETVARIABLE_METHOD 1
  297. #define ID_EXPORTFUNCTION_METHOD 2
  298. #define ID_CALLSCRIPT_METHOD 3
  299. #define ID_STARTUP_METHOD 4
  300. #define NUM_METHOD_IDENTIFIERS 5
  301. static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
  302. static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
  303. "setVariable",
  304. "getVariable",
  305. "exportFunction",
  306. "callScript",
  307. "startup",
  308. };
  309. NPClass *getPluginClass(void)
  310. {
  311. return &_pluginFunctionPtrs;
  312. }
  313. // Sets up the property and method identifier arrays used by the browser
  314. // via the hasProperty and hasMethod fuction pointers
  315. static void initializeIdentifiers()
  316. {
  317. // fill the property identifier array
  318. NPNFuncs.getstringidentifiers(pluginPropertyIdentifierNames,
  319. NUM_PROPERTY_IDENTIFIERS,
  320. pluginPropertyIdentifiers);
  321. // fill the method identifier array
  322. NPNFuncs.getstringidentifiers(pluginMethodIdentifierNames,
  323. NUM_METHOD_IDENTIFIERS,
  324. pluginMethodIdentifiers);
  325. };
  326. bool pluginHasProperty (NPClass *theClass, NPIdentifier name)
  327. {
  328. for (int i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++) {
  329. if (name == pluginPropertyIdentifiers[i]) {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. bool pluginHasMethod(NPObject *npobj, NPIdentifier name)
  336. {
  337. for (int i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
  338. if (name == pluginMethodIdentifiers[i]) {
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. // utility function that sets up a NPVariant from a std::string
  345. void FillString(const std::string& src, NPVariant* variant)
  346. {
  347. variant->type = NPVariantType_String;
  348. variant->value.stringValue.UTF8Length = static_cast<uint32_t>(src.length());
  349. variant->value.stringValue.UTF8Characters = reinterpret_cast<NPUTF8 *>(NPNFuncs.memalloc(src.size()));
  350. memcpy((void*)variant->value.stringValue.UTF8Characters, src.c_str(), src.size());
  351. }
  352. bool pluginGetProperty (PluginObject *obj, NPIdentifier name, NPVariant *variant)
  353. {
  354. VOID_TO_NPVARIANT(*variant);
  355. if (name == pluginPropertyIdentifiers[ID_VERSION_PROPERTY]) {
  356. FillString(std::string("1.0"), variant);
  357. return true;
  358. }
  359. //unknown property
  360. return false;
  361. }
  362. bool pluginSetProperty (PluginObject *obj, NPIdentifier name, const NPVariant *variant)
  363. {
  364. return false;
  365. }
  366. // handle our plugin methods using standard np plugin conventions.
  367. bool pluginInvoke (PluginObject *obj, NPIdentifier name, NPVariant *args, unsigned argCount, NPVariant *result)
  368. {
  369. VOID_TO_NPVARIANT(*result);
  370. // sanity check
  371. if (argCount > 16)
  372. return false;
  373. // plugin.startup(); - called once web page is fully loaded and plugin (including Torque 3D) is initialized
  374. if (name == pluginMethodIdentifiers[ID_STARTUP_METHOD]) {
  375. result->type = NPVariantType_Void;
  376. Startup();
  377. return true;
  378. }
  379. // plugin.setVariable("$MyVariable", 42); - set a Torque 3D console variable
  380. else if (name == pluginMethodIdentifiers[ID_SETVARIABLE_METHOD]) {
  381. result->type = NPVariantType_Void;
  382. if (argCount != 2)
  383. return false;
  384. std::string arg0(MY_NPVARIANT_TO_STRING(args[0]));
  385. std::string arg1(MY_NPVARIANT_TO_STRING(args[1]));
  386. WebCommon::SetVariable(arg0.c_str(), arg1.c_str());
  387. return true;
  388. }
  389. // plugin.getVariable("$MyVariable"); - get a Torque 3D console variable
  390. else if (name == pluginMethodIdentifiers[ID_GETVARIABLE_METHOD]) {
  391. if (argCount != 1)
  392. return false;
  393. std::string value;
  394. std::string arg0(MY_NPVARIANT_TO_STRING(args[0]));
  395. value = WebCommon::GetVariable(arg0.c_str());
  396. FillString(value, result);
  397. return true;
  398. }
  399. // plugin.exportFunction("MyJavascriptFunction",3); - export a Javascript function to the Torque 3D console system via its name and argument count
  400. else if (name == pluginMethodIdentifiers[ID_EXPORTFUNCTION_METHOD]) {
  401. result->type = NPVariantType_Void;
  402. if (argCount != 2)
  403. return false;
  404. std::string fname(MY_NPVARIANT_TO_STRING(args[0]));
  405. int argCount = 0;
  406. if (NPVARIANT_IS_DOUBLE(args[1]))
  407. argCount = NPVARIANT_TO_DOUBLE(args[1]);
  408. else
  409. argCount = NPVARIANT_TO_INT32(args[1]);
  410. ExportFunction(fname.c_str(), argCount);
  411. return true;
  412. }
  413. // var result = plugin.callScript("mySecureFunction('one', 'two', 'three');"); - call a TorqueScript function marked as secure in webConfig.h with supplied arguments
  414. else if (name == pluginMethodIdentifiers[ID_CALLSCRIPT_METHOD]) {
  415. if (argCount != 1)
  416. return false;
  417. std::string value;
  418. std::string code(MY_NPVARIANT_TO_STRING(args[0]));
  419. value = CallScript(code.c_str());
  420. FillString(value, result);
  421. return true;
  422. }
  423. return false;
  424. }
  425. bool pluginInvokeDefault (PluginObject *obj, NPVariant *args, unsigned argCount, NPVariant *result)
  426. {
  427. VOID_TO_NPVARIANT(*result);
  428. return false;
  429. }
  430. void pluginInvalidate ()
  431. {
  432. // Make sure we've released any remaining references to JavaScript
  433. // objects.
  434. }
  435. NPObject *pluginAllocate (NPP npp, NPClass *theClass)
  436. {
  437. PluginObject *newInstance = new PluginObject;
  438. gPluginObject = newInstance;
  439. if (!identifiersInitialized)
  440. {
  441. identifiersInitialized = true;
  442. initializeIdentifiers();
  443. }
  444. // platform specific NPWebGamePlugin instantiation
  445. newInstance->webPlugin = new NPWebGamePlugin(npp);
  446. gInitialized = false;
  447. gJavaScriptExports.clear();
  448. return (NPObject *)newInstance;
  449. }
  450. void pluginDeallocate (PluginObject *obj)
  451. {
  452. delete obj;
  453. gPluginObject = NULL;
  454. }
  455. int32 NPP_Write (NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer);
  456. NPError OSCALL NP_GetEntryPoints(NPPluginFuncs* pFuncs)
  457. {
  458. if (pFuncs == NULL) {
  459. return NPERR_INVALID_FUNCTABLE_ERROR;
  460. }
  461. // Safari sets the size field of pFuncs to 0
  462. if (pFuncs->size == 0)
  463. pFuncs->size = sizeof(NPPluginFuncs);
  464. if (pFuncs->size < sizeof(NPPluginFuncs)) {
  465. return NPERR_INVALID_FUNCTABLE_ERROR;
  466. }
  467. pFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
  468. pFuncs->newp = NPP_New;
  469. pFuncs->destroy = NPP_Destroy;
  470. pFuncs->setwindow = NPP_SetWindow;
  471. pFuncs->newstream = NPP_NewStream;
  472. pFuncs->destroystream = NPP_DestroyStream;
  473. pFuncs->asfile = NPP_StreamAsFile;
  474. pFuncs->writeready = NPP_WriteReady;
  475. pFuncs->write = NPP_Write;
  476. pFuncs->print = NPP_Print;
  477. pFuncs->event = NPP_HandleEvent;
  478. pFuncs->urlnotify = NPP_URLNotify;
  479. pFuncs->getvalue = NPP_GetValue;
  480. pFuncs->setvalue = NPP_SetValue;
  481. pFuncs->javaClass = NULL;
  482. return NPERR_NO_ERROR;
  483. }
  484. NPError OSCALL NP_Initialize(NPNetscapeFuncs* pFuncs)
  485. {
  486. static bool _initialized = false;
  487. if (!_initialized) {
  488. _initialized = true;
  489. }
  490. if (pFuncs == NULL)
  491. return NPERR_INVALID_FUNCTABLE_ERROR;
  492. if ((pFuncs->version >> 8) > NP_VERSION_MAJOR)
  493. return NPERR_INCOMPATIBLE_VERSION_ERROR;
  494. // Safari sets the pfuncs size to 0
  495. if (pFuncs->size == 0)
  496. pFuncs->size = sizeof(NPNetscapeFuncs);
  497. if (pFuncs->size < sizeof (NPNetscapeFuncs))
  498. return NPERR_INVALID_FUNCTABLE_ERROR;
  499. NPNFuncs = *pFuncs;
  500. return NPERR_NO_ERROR;
  501. }
  502. NPError OSCALL NP_Shutdown()
  503. {
  504. if (WebCommon::gTorque3DModule)
  505. WebCommon::ShutdownTorque3D();
  506. return NPERR_NO_ERROR;
  507. }
  508. NPError NPP_New(NPMIMEType pluginType,
  509. NPP instance, uint16 mode,
  510. int16 argc, char *argn[],
  511. char *argv[], NPSavedData *saved)
  512. {
  513. WebCommon::gPluginMIMEType = pluginType;
  514. if (gPluginObject)
  515. {
  516. WebCommon::MessageBox( 0, "This plugin allows only one instance", "Error");
  517. return NPERR_GENERIC_ERROR;
  518. }
  519. // Get the location we're loading the plugin from (http://, file://) including address
  520. // this is used by the domain locking feature to ensure that your plugin is only
  521. // being used from your web site
  522. NPObject* windowObject = NULL;
  523. NPNFuncs.getvalue( instance, NPNVWindowNPObject, &windowObject );
  524. NPVariant variantValue;
  525. NPIdentifier identifier = NPNFuncs.getstringidentifier( "location" );
  526. if (!NPNFuncs.getproperty( instance, windowObject, identifier, &variantValue ))
  527. return NPERR_GENERIC_ERROR;
  528. NPObject *locationObj = variantValue.value.objectValue;
  529. identifier = NPNFuncs.getstringidentifier( "href" );
  530. if (!NPNFuncs.getproperty( instance, locationObj, identifier, &variantValue ))
  531. return NPERR_GENERIC_ERROR;
  532. std::string url = MY_NPVARIANT_TO_STRING(variantValue);
  533. if (!WebCommon::CheckDomain(url.c_str()))
  534. return NPERR_GENERIC_ERROR;
  535. // everything checks out, let's rock
  536. if (NPNFuncs.version >= 14) {
  537. // this calls pluginAllocate
  538. instance->pdata = NPNFuncs.createobject(instance, getPluginClass());
  539. }
  540. #ifndef WIN32
  541. // On Mac, make sure we're using CoreGraphics (otherwise 3D rendering fails)
  542. NPNFuncs.setvalue(instance, (NPPVariable)NPNVpluginDrawingModel, (void *) NPDrawingModelCoreGraphics);
  543. #endif
  544. //PluginObject *plugin = (PluginObject*)instance->pdata;
  545. return NPERR_NO_ERROR;
  546. }
  547. // here is the place to clean up and destroy the object
  548. NPError NPP_Destroy (NPP instance, NPSavedData** save)
  549. {
  550. if (instance == NULL)
  551. return NPERR_INVALID_INSTANCE_ERROR;
  552. if (instance->pdata != NULL) {
  553. PluginObject *plugin = (PluginObject *)instance->pdata;
  554. delete plugin->webPlugin;
  555. NPNFuncs.releaseobject((NPObject*) instance->pdata);
  556. instance->pdata = NULL;
  557. }
  558. return NPERR_NO_ERROR;
  559. }
  560. NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
  561. {
  562. if (variable == NPPVpluginScriptableNPObject) {
  563. if (instance->pdata == NULL) {
  564. instance->pdata = NPNFuncs.createobject(instance, getPluginClass());
  565. }
  566. NPObject* obj = reinterpret_cast<NPObject*>(instance->pdata);
  567. NPNFuncs.retainobject(obj);
  568. void **v = (void **)value;
  569. *v = obj;
  570. return NPERR_NO_ERROR;
  571. }
  572. return NPERR_GENERIC_ERROR;
  573. }
  574. NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
  575. {
  576. return NPERR_GENERIC_ERROR;
  577. }
  578. NPError NPP_NewStream(NPP instance,
  579. NPMIMEType type,
  580. NPStream* stream,
  581. NPBool seekable,
  582. uint16* stype)
  583. {
  584. if(instance == NULL)
  585. return NPERR_INVALID_INSTANCE_ERROR;
  586. return NPERR_NO_ERROR;
  587. }
  588. int32 NPP_WriteReady (NPP instance, NPStream *stream)
  589. {
  590. if (instance == NULL)
  591. return NPERR_INVALID_INSTANCE_ERROR;
  592. return 0x0fffffff;
  593. }
  594. int32 NPP_Write (NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
  595. {
  596. if (instance == NULL)
  597. return NPERR_INVALID_INSTANCE_ERROR;
  598. return NPERR_NO_ERROR;
  599. }
  600. NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
  601. {
  602. if(instance == NULL)
  603. return NPERR_INVALID_INSTANCE_ERROR;
  604. return NPERR_NO_ERROR;
  605. }
  606. void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
  607. {
  608. }
  609. void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
  610. {
  611. }
  612. void NPP_Print (NPP instance, NPPrint* printInfo)
  613. {
  614. }
  615. int16 NPP_HandleEvent(NPP instance, void* event)
  616. {
  617. return 0;
  618. }
  619. // browser communicated window changes (creation, etc) here
  620. NPError NPP_SetWindow(NPP instance, NPWindow* window)
  621. {
  622. // strange...
  623. if (!window || !window->window)
  624. return NPERR_GENERIC_ERROR;
  625. // strange...
  626. if (!instance)
  627. return NPERR_INVALID_INSTANCE_ERROR;
  628. // get back the plugin instance object
  629. PluginObject *plugin = (PluginObject *)instance->pdata;
  630. NPWebGamePlugin* webPlugin = plugin->webPlugin;
  631. if (webPlugin) {
  632. if (!window->window) {
  633. }
  634. else {
  635. // handle platform specific window and Torque 3D initialization
  636. webPlugin->Open(window);
  637. }
  638. return NPERR_NO_ERROR;
  639. }
  640. // return an error if no object defined
  641. return NPERR_GENERIC_ERROR;
  642. }