Skip to content

Commit

Permalink
Convert SewerRat.cpp to new Message system
Browse files Browse the repository at this point in the history
  • Loading branch information
winterheart committed Aug 2, 2024
1 parent c146e79 commit eef4e92
Showing 1 changed file with 8 additions and 185 deletions.
193 changes: 8 additions & 185 deletions scripts/SewerRat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
// Filename: SewerRat.cpp
// Version: 3
/////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <map>
#include <string>

#include "osiris_import.h"
#include "osiris_common.h"
#include "DallasFuncs.cpp"
Expand Down Expand Up @@ -118,180 +118,12 @@ void RestoreGlobalActionCtrs(void *file_ptr) {
// Message File Data
// =================

#define MAX_SCRIPT_MESSAGES 256
#define MAX_MSG_FILEBUF_LEN 1024
#define NO_MESSAGE_STRING "*Message Not Found*"
#define INV_MSGNAME_STRING "*Message Name Invalid*"
#define WHITESPACE_CHARS " \t\r\n"

// Structure for storing a script message
struct tScriptMessage {
char *name; // the name of the message
char *message; // the actual message text
};

// Global storage for level script messages
tScriptMessage *message_list[MAX_SCRIPT_MESSAGES];
int num_messages;

// ======================
// Message File Functions
// ======================

// Initializes the Message List
void InitMessageList(void) {
for (int j = 0; j < MAX_SCRIPT_MESSAGES; j++)
message_list[j] = NULL;
num_messages = 0;
}

// Clear the Message List
void ClearMessageList(void) {
for (int j = 0; j < num_messages; j++) {
free(message_list[j]->name);
free(message_list[j]->message);
free(message_list[j]);
message_list[j] = NULL;
}
num_messages = 0;
}

// Adds a message to the list
int AddMessageToList(char *name, char *msg) {
int pos;

// Make sure there is room in the list
if (num_messages >= MAX_SCRIPT_MESSAGES)
return false;

// Allocate memory for this message entry
pos = num_messages;
message_list[pos] = (tScriptMessage *)malloc(sizeof(tScriptMessage));
if (message_list[pos] == NULL)
return false;

// Allocate memory for the message name
message_list[pos]->name = (char *)malloc(strlen(name) + 1);
if (message_list[pos]->name == NULL) {
free(message_list[pos]);
return false;
}
strcpy(message_list[pos]->name, name);

// Allocate memory for the message name
message_list[pos]->message = (char *)malloc(strlen(msg) + 1);
if (message_list[pos]->message == NULL) {
free(message_list[pos]->name);
free(message_list[pos]);
return false;
}
strcpy(message_list[pos]->message, msg);
num_messages++;

return true;
}

// Removes any whitespace padding from the end of a string
void RemoveTrailingWhitespace(char *s) {
int last_char_pos;

last_char_pos = strlen(s) - 1;
while (last_char_pos >= 0 && isspace(s[last_char_pos])) {
s[last_char_pos] = '\0';
last_char_pos--;
}
}

// Returns a pointer to the first non-whitespace char in given string
char *SkipInitialWhitespace(char *s) {
while ((*s) != '\0' && isspace(*s))
s++;

return (s);
}
std::map<std::string, std::string> Messages;

// Read in the Messages
int ReadMessageFile(const char *filename) {
void *infile;
char filebuffer[MAX_MSG_FILEBUF_LEN + 1];
char *line, *msg_start;
int line_num;
bool next_msgid_found;

// Try to open the file for loading
infile = File_Open(filename, "rt");
if (!infile)
return false;

line_num = 0;
next_msgid_found = true;

// Clear the message list
ClearMessageList();

// Read in and parse each line of the file
while (!File_eof(infile)) {

// Clear the buffer
strcpy(filebuffer, "");

// Read in a line from the file
File_ReadString(filebuffer, MAX_MSG_FILEBUF_LEN, infile);
line_num++;

// Remove whitespace padding at start and end of line
RemoveTrailingWhitespace(filebuffer);
line = SkipInitialWhitespace(filebuffer);

// If line is a comment, or empty, discard it
if (strlen(line) == 0 || strncmp(line, "//", 2) == 0)
continue;

if (!next_msgid_found) { // Parse out the last message ID number

// Grab the first keyword, make sure it's valid
line = strtok(line, WHITESPACE_CHARS);
if (line == NULL)
continue;

// Grab the second keyword, and assign it as the next message ID
line = strtok(NULL, WHITESPACE_CHARS);
if (line == NULL)
continue;

next_msgid_found = true;
} else { // Parse line as a message line

// Find the start of message, and mark it
msg_start = strchr(line, '=');
if (msg_start == NULL)
continue;
msg_start[0] = '\0';
msg_start++;

// Add the message to the list
AddMessageToList(line, msg_start);
}
}
File_Close(infile);

return true;
}

// Find a message
const char *GetMessage(const char *name) {
// Make sure given name is valid
if (name == NULL)
return INV_MSGNAME_STRING;

// Search message list for name
for (int j = 0; j < num_messages; j++)
if (strcmp(message_list[j]->name, name) == 0)
return (message_list[j]->message);

// Couldn't find it
return NO_MESSAGE_STRING;
}
#define TXT(MSG) GetMessageNew(MSG, Messages)
#define ReadMessageFile(filename) CreateMessageMap(filename, Messages)
#define ClearMessageList() DestroyMessageMap(Messages)

//======================
// Name List Arrays
Expand Down Expand Up @@ -338,10 +170,6 @@ int Matcen_indexes[NUM_MATCEN_NAMES];
const char **Goal_names = NULL;
int *Goal_indexes = NULL;

#define NUM_MESSAGE_NAMES 0
const char **Message_names = NULL;
const char **Message_strings = NULL;

// ===============
// InitializeDLL()
// ===============
Expand All @@ -355,7 +183,6 @@ char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {

ClearGlobalActionCtrs();
dfInit();
InitMessageList();

// Build the filename of the message file
char filename[_MAX_PATH + 32];
Expand Down Expand Up @@ -412,10 +239,6 @@ char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {
for (j = 0; j < NUM_GOAL_NAMES; j++)
Goal_indexes[j] = Scrpt_FindLevelGoalName(Goal_names[j]);

// Do Message Name lookups
for (j = 0; j < NUM_MESSAGE_NAMES; j++)
Message_strings[j] = GetMessage(Message_names[j]);

return 1;
}

Expand Down

0 comments on commit eef4e92

Please sign in to comment.