From fc762d8f094f5be3efb8a461fa78825bf627a56c Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 10 Sep 2009 00:38:35 +0000 Subject: [PATCH] JS features --- direct/src/plugin/p3dInstance.cxx | 20 +++++- direct/src/plugin/p3dInstance.h | 1 + direct/src/plugin/p3dInstanceManager.cxx | 5 ++ direct/src/plugin/p3dMainObject.cxx | 91 ++++++++++++++++-------- direct/src/plugin/p3dMainObject.h | 7 +- 5 files changed, 91 insertions(+), 33 deletions(-) diff --git a/direct/src/plugin/p3dInstance.cxx b/direct/src/plugin/p3dInstance.cxx index b48f6814d8..45a0d713a8 100644 --- a/direct/src/plugin/p3dInstance.cxx +++ b/direct/src/plugin/p3dInstance.cxx @@ -898,12 +898,27 @@ make_xml() { // Function: P3DInstance::splash_button_clicked // Access: Public // Description: Called by the P3DSplashWindow code when the user -// clicks the play button visible on the splash window. +// clicks the button visible on the splash window. //////////////////////////////////////////////////////////////////// void P3DInstance:: splash_button_clicked() { - // If we haven't launched yet, launch now. if (_session == NULL) { + play_button_clicked(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: P3DInstance::play_button_clicked +// Access: Public +// Description: Called to start the game by the user clicking the +// "play" button, or by JavaScript calling start(). +//////////////////////////////////////////////////////////////////// +void P3DInstance:: +play_button_clicked() { + if (_session == NULL) { + if (_splash_window != NULL) { + _splash_window->set_button_active(false); + } set_background_image(IT_launch); P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr(); inst_mgr->start_instance(this); @@ -1529,6 +1544,7 @@ start_next_download() { //////////////////////////////////////////////////////////////////// void P3DInstance:: ready_to_start() { + send_notify("onready"); if (_auto_start) { set_background_image(IT_launch); P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr(); diff --git a/direct/src/plugin/p3dInstance.h b/direct/src/plugin/p3dInstance.h index f989dfd2c1..1b530368fe 100644 --- a/direct/src/plugin/p3dInstance.h +++ b/direct/src/plugin/p3dInstance.h @@ -101,6 +101,7 @@ public: TiXmlElement *make_xml(); void splash_button_clicked(); + void play_button_clicked(); private: class ImageDownload : public P3DFileDownload { diff --git a/direct/src/plugin/p3dInstanceManager.cxx b/direct/src/plugin/p3dInstanceManager.cxx index c1933db463..367c1376b0 100644 --- a/direct/src/plugin/p3dInstanceManager.cxx +++ b/direct/src/plugin/p3dInstanceManager.cxx @@ -321,6 +321,11 @@ set_p3d_filename(P3DInstance *inst, bool is_local, //////////////////////////////////////////////////////////////////// bool P3DInstanceManager:: start_instance(P3DInstance *inst) { + if (inst->is_started()) { + // Already started. + return true; + } + P3DSession *session; Sessions::iterator si = _sessions.find(inst->get_session_key()); if (si == _sessions.end()) { diff --git a/direct/src/plugin/p3dMainObject.cxx b/direct/src/plugin/p3dMainObject.cxx index a51ced8882..edc39a4401 100644 --- a/direct/src/plugin/p3dMainObject.cxx +++ b/direct/src/plugin/p3dMainObject.cxx @@ -15,7 +15,6 @@ #include "p3dMainObject.h" #include "p3dInstance.h" #include "p3dSession.h" -#include "p3dUndefinedObject.h" #include "p3dStringObject.h" #include "p3dInstanceManager.h" @@ -184,7 +183,9 @@ set_property(const string &property, P3D_object *value) { bool P3DMainObject:: has_method(const string &method_name) { // Some special-case methods implemented in-place. - if (method_name == "read_game_log") { + if (method_name == "start") { + return true; + } else if (method_name == "read_game_log") { return true; } else if (method_name == "read_system_log") { return true; @@ -214,10 +215,12 @@ has_method(const string &method_name) { P3D_object *P3DMainObject:: call(const string &method_name, bool needs_response, P3D_object *params[], int num_params) { - if (method_name == "read_game_log") { - return call_read_game_log(); + if (method_name == "start") { + return call_start(params, num_params); + } else if (method_name == "read_game_log") { + return call_read_game_log(params, num_params); } else if (method_name == "read_system_log") { - return call_read_system_log(); + return call_read_system_log(params, num_params); } if (_pyobj == NULL) { @@ -294,6 +297,28 @@ set_instance(P3DInstance *inst) { _inst = inst; } +//////////////////////////////////////////////////////////////////// +// Function: P3DMainObject::call_start +// Access: Private +// Description: Starts the process remotely, as if the start button +// had been clicked. Only applicable if the application +// was in the ready state. Returns true if the +// application is now started, false otherwise. +//////////////////////////////////////////////////////////////////// +P3D_object *P3DMainObject:: +call_start(P3D_object *params[], int num_params) { + P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr(); + if (_inst == NULL) { + return inst_mgr->new_bool_object(false); + } + + if (!_inst->is_started()) { + _inst->play_button_clicked(); + } + + return inst_mgr->new_bool_object(_inst->is_started()); +} + //////////////////////////////////////////////////////////////////// // Function: P3DMainObject::call_read_game_log // Access: Private @@ -301,35 +326,19 @@ set_instance(P3DInstance *inst) { // to the calling JavaScript process. //////////////////////////////////////////////////////////////////// P3D_object *P3DMainObject:: -call_read_game_log() { +call_read_game_log(P3D_object *params[], int num_params) { + P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr(); if (_inst == NULL) { - return new P3DUndefinedObject(); + return inst_mgr->new_undefined_object(); } P3DSession *session = _inst->get_session(); if (session == NULL) { - return new P3DUndefinedObject(); + return inst_mgr->new_undefined_object(); } string log_pathname = session->get_log_pathname(); - ifstream log(log_pathname.c_str(), ios::in); - - // Get the size of the file. - log.seekg(0, ios::end); - size_t size = (size_t)log.tellg(); - log.seekg(0, ios::beg); - - // Read the entire file into memory all at once. - char *buffer = new char[size]; - if (buffer == NULL) { - return NULL; - } - - log.read(buffer, size); - P3D_object *result = new P3DStringObject(buffer, size); - delete[] buffer; - - return result; + return read_log(log_pathname, params, num_params); } //////////////////////////////////////////////////////////////////// @@ -339,18 +348,42 @@ call_read_game_log() { // the installation process. //////////////////////////////////////////////////////////////////// P3D_object *P3DMainObject:: -call_read_system_log() { +call_read_system_log(P3D_object *params[], int num_params) { P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr(); string log_pathname = inst_mgr->get_log_pathname(); + return read_log(log_pathname, params, num_params); +} + +//////////////////////////////////////////////////////////////////// +// Function: P3DMainObject::call_read_log +// Access: Private +// Description: The generic log-reader function. +//////////////////////////////////////////////////////////////////// +P3D_object *P3DMainObject:: +read_log(const string &log_pathname, P3D_object *params[], int num_params) { ifstream log(log_pathname.c_str(), ios::in); + // Check the parameter, if any--if specified, it specifies the last + // n bytes to retrieve. + int max_bytes = 0; + if (num_params > 0) { + max_bytes = P3D_OBJECT_GET_INT(params[0]); + } + // Get the size of the file. log.seekg(0, ios::end); size_t size = (size_t)log.tellg(); - log.seekg(0, ios::beg); - // Read the entire file into memory all at once. + if (max_bytes > 0 && max_bytes < size) { + // Apply the limit. + log.seekg(size - max_bytes, ios::beg); + size = (size_t)max_bytes; + } else { + // Read the entire file. + log.seekg(0, ios::beg); + } + char *buffer = new char[size]; if (buffer == NULL) { return NULL; diff --git a/direct/src/plugin/p3dMainObject.h b/direct/src/plugin/p3dMainObject.h index e9a778a2e6..8bc60c23b9 100644 --- a/direct/src/plugin/p3dMainObject.h +++ b/direct/src/plugin/p3dMainObject.h @@ -67,8 +67,11 @@ public: void set_instance(P3DInstance *inst); private: - P3D_object *call_read_game_log(); - P3D_object *call_read_system_log(); + P3D_object *call_start(P3D_object *params[], int num_params); + P3D_object *call_read_game_log(P3D_object *params[], int num_params); + P3D_object *call_read_system_log(P3D_object *params[], int num_params); + P3D_object *read_log(const string &log_pathname, + P3D_object *params[], int num_params); private: P3D_object *_pyobj;