示例#1
0
static Variant to_variant(const pp::Var& p_var) {

	if (p_var.is_undefined() || p_var.is_null())
		return Variant();
	if (p_var.is_bool())
		return Variant(p_var.AsBool());
	if (p_var.is_double())
		return Variant(p_var.AsDouble());
	if (p_var.is_int())
		return Variant((int64_t)p_var.AsInt());
	if (p_var.is_string())
		return Variant(String::utf8(p_var.AsString().c_str()));

	return Variant();
};
  // Handler for messages coming in from the browser via postMessage().  The
  // @a var_message can contain be any pp:Var type; for example int, string
  // Array or Dictinary. Please see the pp:Var documentation for more details.
  // @param[in] var_message The message posted by the browser.
  virtual void HandleMessage(const pp::Var& var_message) {
    /*if (!var_message.is_string())
      return;

    std::string message = var_message.AsString();
    pp::Var var_reply;
    if (message == kHelloString) {
      var_reply = pp::Var(kReplyString);
      PostMessage(var_reply);
    }*/

    // Send integer events from JS straight to the CPU
    if (!var_message.is_int()) {
      uint32_t msgInt = var_message.AsInt();

      cpu->OnExtMessage(msgInt);
    }
  }
示例#3
0
void GodotInstance::HandleMessage(const pp::Var& var_message) {

	switch (state) {

		case STATE_METHOD: {

			ERR_FAIL_COND(!var_message.is_string());
			sd->method = var_message.AsString().c_str();
			state = STATE_PARAM_COUNT;
		} break;
		case STATE_PARAM_COUNT: {

			ERR_FAIL_COND(!var_message.is_number());
			sd->arg_count = var_message.AsInt();
			state = sd->arg_count>0?STATE_PARAMS:STATE_CALL;

		} break;
		case STATE_PARAMS: {

			Variant p = to_variant(var_message);
			sd->args.push_back(p);
			if (sd->args.size() >= sd->arg_count)
				state = STATE_CALL;
		} break;
		default:
			break;
	};

	if (state == STATE_CALL) {

		// call
		state = STATE_METHOD;


		if (sd->method == "package_finished") {

			GetURLHandler::Status status = package_pending->get_status();
			printf("status is %i, %i, %i\n", status, GetURLHandler::STATUS_ERROR, GetURLHandler::STATUS_COMPLETED);
			if (status == GetURLHandler::STATUS_ERROR) {
				printf("Error fetching package!\n");
			};
            if (status == GetURLHandler::STATUS_COMPLETED) {

				OSNacl* os = (OSNacl*)OS::get_singleton();
				os->add_package(pkg_url, package_pending->get_data());
			};
            memdelete(package_pending);
			package_pending = NULL;

            package_loaded = true;

            opengl_context_->MakeContextCurrent(this);
            nacl_main(init_argc, (const char**)init_argn, (const char**)init_argv);
            for (uint32_t i=0; i<init_argc; i++) {
				memfree(init_argn[i]);
				memfree(init_argv[i]);
			};
		};

		if (sd->method == "get_package_status") {

			if (package_loaded) {
				// post "loaded"
				PostMessage("loaded");
			} else if (package_pending == NULL) {
				// post "none"
				PostMessage("none");
			} else {
				// post package_pending->get_bytes_read();
				PostMessage(package_pending->get_bytes_read());
			};
		};
	};
}