From 4696a1ec3281aeff3be586397c987620183ef803 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Thu, 6 Apr 2023 23:09:20 +0100 Subject: [PATCH] Adedd some comments to schemeintegration.c --- src/schemeintegration.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/schemeintegration.c b/src/schemeintegration.c index 91742fe..3c0a94d 100644 --- a/src/schemeintegration.c +++ b/src/schemeintegration.c @@ -3,8 +3,9 @@ #include #include "schemeintegration.h" +// Create a player skill and add it to a given skill list from Scheme: SCM scheme_create_skill(SCM string, SCM skilllist) -{ +{ size_t skillNameLength = 0; char * skillName = scm_to_latin1_stringn(string, &skillNameLength); createSkill(scm_to_pointer(skilllist), skillName, skillNameLength, false); @@ -12,33 +13,56 @@ SCM scheme_create_skill(SCM string, SCM skilllist) return SCM_BOOL_T; } +// Message every currently connected player from Scheme: SCM scheme_message_everyone(SCM sendername, SCM messagecontent, SCM outputqueue) { + // Allocate the memory for the needed data structures: outputMessage * newOutputMessage = calloc(1, sizeof(userMessage)); userMessage * newMessage = calloc(1, sizeof(userMessage)); + + // Set some basic information for the output message, allowing it to be sent to everyone: newOutputMessage->content = newMessage; newOutputMessage->recipientsCount = 0; newOutputMessage->recipients = NULL; + // Convert the Scheme strings to C strings, and ensure they're NULL terminated: scm_to_locale_stringbuf(sendername, newMessage->senderName, 31); newMessage->senderName[31] = '\0'; scm_to_locale_stringbuf(messagecontent, newMessage->messageContent, MAX - 1); newMessage->messageContent[MAX - 1] = '\0'; + // Clean up the message contents to ensure they're safe to send and display correctly: userNameSanatize(newMessage->senderName, 32); userInputSanatize(newMessage->messageContent, MAX); + + // Push it to the queue, where it will be handled and de-allocated: pushQueue(scm_to_pointer(outputqueue), newOutputMessage, OUTPUT_MESSAGE); + + // Return a Scheme #t value: return SCM_BOOL_T; } + +// The function ran by the Scheme thread which handles all game-master and interpreter interaction: void * schemeHandler(void * parameters) { + // Take in the needed values from the main thread and make it back into the struct: SchemeThreadParameters * schemeThreadParameters = parameters; - + + // Initialize GNU Guile: scm_init_guile(); + + // Register the various functions: scm_c_define_gsubr("create-skill", 2, 0, 0, &scheme_create_skill); scm_c_define_gsubr("message-everyone", 3, 0, 0, &scheme_message_everyone); + + // Define the various game state pointers as Scheme objects: scm_c_define("skill-list", scm_from_pointer(schemeThreadParameters->skillList, NULL)); scm_c_define("output-queue", scm_from_pointer(schemeThreadParameters->outputQueue, NULL)); + + // Drop into the Scheme interpreter: scm_shell(0, NULL); + + // Return NULL. This should be unreachable. + return NULL; }