diff options
-rw-r--r-- | engines/sci/engine/kstring.cpp | 4 | ||||
-rw-r--r-- | engines/sci/engine/message.cpp | 187 | ||||
-rw-r--r-- | engines/sci/engine/message.h | 35 |
3 files changed, 87 insertions, 139 deletions
diff --git a/engines/sci/engine/kstring.cpp b/engines/sci/engine/kstring.cpp index ca5cd09a39..36a3f992f3 100644 --- a/engines/sci/engine/kstring.cpp +++ b/engines/sci/engine/kstring.cpp @@ -727,7 +727,7 @@ reg_t kGetFarText(EngineState *s, int funct_nr, int argc, reg_t *argv) { static MessageState state; reg_t kMessage(EngineState *s, int funct_nr, int argc, reg_t *argv) { - if (!state.initialized) + if (!state.isInitialized()) message_state_initialize(s->resmgr, &state); switch (UKPV(0)) { @@ -782,7 +782,7 @@ reg_t kMessage(EngineState *s, int funct_nr, int argc, reg_t *argv) { } reg_t kGetMessage(EngineState *s, int funct_nr, int argc, reg_t *argv) { - if (!state.initialized) + if (!state.isInitialized()) message_state_initialize(s->resmgr, &state); char *buffer = kernel_dereference_char_pointer(s, argv[3], 0); diff --git a/engines/sci/engine/message.cpp b/engines/sci/engine/message.cpp index fac6260f90..6766224247 100644 --- a/engines/sci/engine/message.cpp +++ b/engines/sci/engine/message.cpp @@ -28,120 +28,87 @@ namespace Sci { -static int get_talker_trivial(IndexRecordCursor *cursor) { - return -1; +void MessageState::initIndexRecordCursor() { + _engineCursor.resource_beginning = _currentResource->data; + _engineCursor.index_record = _indexRecords; + _engineCursor.index = 1; } -/* Version 2.101 and later code ahead */ +void MessageState::setVersion(int version) { + _version = version; -static void index_record_parse_2101(IndexRecordCursor *cursor, MessageTuple *t) { - int noun = *(cursor->index_record + 0); - int verb = *(cursor->index_record + 1); - - t->noun = noun; - t->verb = verb; - t->cond = t->seq = 0; -} - -static void index_record_get_text_2101(IndexRecordCursor *cursor, char *buffer, int buffer_size) { - int offset = READ_LE_UINT16(cursor->index_record + 2); - char *stringptr = (char *)cursor->resource_beginning + offset; - - strncpy(buffer, stringptr, buffer_size); -} - -static int header_get_index_record_count_2101(byte *header) { - return READ_LE_UINT16(header + 4); -} - -// Version 3.411 and later code ahead - -static void index_record_parse_3411(IndexRecordCursor *cursor, MessageTuple *t) { - int noun = *(cursor->index_record + 0); - int verb = *(cursor->index_record + 1); - int cond = *(cursor->index_record + 2); - int seq = *(cursor->index_record + 3); - - t->noun = noun; - t->verb = verb; - t->cond = cond; - t->seq = seq; -} - -static int index_record_get_talker_3411(IndexRecordCursor *cursor) { - return *(cursor->index_record + 4); -} - -static void index_record_get_text_3411(IndexRecordCursor *cursor, char *buffer, int buffer_size) { - int offset = READ_LE_UINT16(cursor->index_record + 5); - char *stringptr = (char *)cursor->resource_beginning + offset; - - strncpy(buffer, stringptr, buffer_size); -} - -static int header_get_index_record_count_3411(byte *header) { - return READ_LE_UINT16(header + 8); -} - -// Generic code from here on - -static int four_tuple_match(MessageTuple *t1, MessageTuple *t2) { - return t1->noun == t2->noun && t1->verb == t2->verb && t1->cond == t2->cond && t1->seq == t2->seq; -} - -static void index_record_cursor_initialize(MessageState *state, IndexRecordCursor *cursor) { - cursor->resource_beginning = state->current_res->data; - cursor->index_record = state->index_records; - cursor->index = 1; + if (version == 2101) { + _headerSize = 6; + _indexRecordSize = 4; + } else { + _headerSize = 10; + _indexRecordSize = 11; + } } -static int index_record_next(MessageState *state, IndexRecordCursor *cursor) { - if (cursor->index == state->record_count) - return 0; - cursor->index_record += state->handler->index_record_size; - cursor->index ++; - return 1; +void MessageState::parse(IndexRecordCursor *cursor, MessageTuple *t) { + t->noun = *(cursor->index_record + 0); + t->verb = *(cursor->index_record + 1); + if (_version == 2101) { + t->cond = 0; + t->seq = 0; + } else { + t->cond = *(cursor->index_record + 2); + t->seq = *(cursor->index_record + 3); + } } -static int index_record_find(MessageState *state, MessageTuple *t, IndexRecordCursor *cursor) { +int MessageState::getSpecific(MessageTuple *t) { MessageTuple looking_at; int found = 0; - index_record_cursor_initialize(state, cursor); + initIndexRecordCursor(); do { - state->handler->parse(cursor, &looking_at); - if (four_tuple_match(t, &looking_at)) + parse(&_engineCursor, &looking_at); + if (t->noun == looking_at.noun && + t->verb == looking_at.verb && + t->cond == looking_at.cond && + t->seq == looking_at.seq) found = 1; - } while (!found && index_record_next(state, cursor)); + } while (!found && getNext()); // FIXME: Recursion not handled yet return found; } -int MessageState::getSpecific(MessageTuple *t) { - return index_record_find(this, t, &engine_cursor); -} - int MessageState::getNext() { - return index_record_next(this, &engine_cursor); + if (_engineCursor.index == _recordCount) + return 0; + _engineCursor.index_record += _indexRecordSize; + _engineCursor.index ++; + return 1; } int MessageState::getTalker() { - return handler->get_talker(&engine_cursor); + if (_version == 2101) + return -1; + else + return *(_engineCursor.index_record + 4); } int MessageState::getText(char *buffer, int length) { - handler->get_text(&engine_cursor, buffer, length); + int offset; + if (_version == 2101) + offset = READ_LE_UINT16(_engineCursor.index_record + 2); + else + offset = READ_LE_UINT16(_engineCursor.index_record + 5); + + char *stringptr = (char *)_engineCursor.resource_beginning + offset; + strncpy(buffer, stringptr, length); + return strlen(buffer); } int MessageState::getLength() { char buffer[500]; - - handler->get_text(&engine_cursor, buffer, sizeof(buffer)); - return strlen(buffer); + return getText(buffer, sizeof(buffer)); } int MessageState::loadRes(int module) { @@ -150,43 +117,35 @@ int MessageState::loadRes(int module) { // Unlock old resource if (_module != -1) - resmgr->unlockResource(current_res, _module, kResourceTypeMessage); + _resmgr->unlockResource(_currentResource, _module, kResourceTypeMessage); _module = module; - current_res = resmgr->findResource(kResourceTypeMessage, module, 1); + _currentResource = _resmgr->findResource(kResourceTypeMessage, module, 1); - if (current_res == NULL || current_res->data == NULL) { + if (_currentResource == NULL || _currentResource->data == NULL) { sciprintf("Message subsystem: Failed to load %d.MSG\n", module); _module = -1; return 0; } - record_count = handler->index_record_count(current_res->data); - index_records = current_res->data + handler->header_size; + if (_version == 2101) + _recordCount = READ_LE_UINT16(_currentResource->data + 4); + else + _recordCount = READ_LE_UINT16(_currentResource->data + 8); + + _indexRecords = _currentResource->data + _headerSize; - index_record_cursor_initialize(this, &engine_cursor); + initIndexRecordCursor(); return 1; } -static MessageHandler fixed_handler_old = { - 2101, - index_record_parse_2101, - get_talker_trivial, - index_record_get_text_2101, - header_get_index_record_count_2101, - 6, - 4 -}; - -static MessageHandler fixed_handler = { - 3411, - index_record_parse_3411, - index_record_get_talker_3411, - index_record_get_text_3411, - header_get_index_record_count_3411, - 10, - 11 -}; +void MessageState::initialize(ResourceManager *resmgr) { + _module = -1; + _resmgr = resmgr; + _currentResource = NULL; + _recordCount = 0; + _initialized = 1; +} void message_state_initialize(ResourceManager *resmgr, MessageState *state) { Resource *tester = resmgr->findResource(kResourceTypeMessage, 0, 0); @@ -196,16 +155,8 @@ void message_state_initialize(ResourceManager *resmgr, MessageState *state) { return; version = READ_LE_UINT16(tester->data); - - state->initialized = 1; - state->_module = -1; - state->resmgr = resmgr; - state->current_res = NULL; - state->record_count = 0; - if (version == 2101) - state->handler = &fixed_handler_old; - else - state->handler = &fixed_handler; + state->initialize(resmgr); + state->setVersion(version); } } // End of namespace Sci diff --git a/engines/sci/engine/message.h b/engines/sci/engine/message.h index 70012cbfbc..9f5b04cf7e 100644 --- a/engines/sci/engine/message.h +++ b/engines/sci/engine/message.h @@ -49,17 +49,6 @@ typedef int get_talker_t(IndexRecordCursor *cursor); typedef void get_text_t(IndexRecordCursor *cursor, char *buffer, int buffer_size); typedef int index_record_count_t(byte *header); -struct MessageHandler { - int version_id; - parse_index_record_t *parse; - get_talker_t *get_talker; - get_text_t *get_text; - index_record_count_t *index_record_count; - - int header_size; - int index_record_size; -}; - class MessageState { public: int getSpecific(MessageTuple *t); @@ -68,16 +57,24 @@ public: int getLength(); int getText(char *buffer, int length); int loadRes(int module); + int isInitialized() { return _initialized; } + void initialize(ResourceManager *resmgr); + void setVersion(int version); + +private: + void initIndexRecordCursor(); + void parse(IndexRecordCursor *cursor, MessageTuple *t); -public: // TODO: hide the internals - int initialized; - MessageHandler *handler; - ResourceManager *resmgr; - Resource *current_res; + int _initialized; + ResourceManager *_resmgr; + Resource *_currentResource; int _module; - int record_count; - byte *index_records; - IndexRecordCursor engine_cursor; + int _recordCount; + byte *_indexRecords; + IndexRecordCursor _engineCursor; + int _version; + int _headerSize; + int _indexRecordSize; }; void message_state_initialize(ResourceManager *resmgr, MessageState *state); |