Skip to content

API for Submod Submods

Kristian edited this page Sep 14, 2021 · 2 revisions

API for Submod-Submods

Introduction

MAS-Additions consists of several smaller submods, these include MASM (Monika After Story Module), FDAR (Face Detection and Recognition) and MIDI (MIDI keyboard support). Other than the MIDI submod, the MASM and FDAR submods have API's that can used by other Submods as well.

MASM

MASM consists of 2 parts, MAS-side and Binary-side.

MAS-side handles starting up external binary and closing it along with Monika After Story.

Binary-side runs a Python 3.9 interpreter and allows scripts and modules using dynamic libraries to be used.

Both sides communicate with each other using threaded UDP sockets (ports 24488 and 24489) with buffer size of 256. If the buffer is too small for your use-case, you can create an issue for raising it or create a pull request.

Main files

  • [Windows] MASM.exe, python39.dll, zlib.dll - Windows binary and it's required files.
  • [Linux] MASM - Linux binary.
  • python39 - Folder for OS specific Python files and external packages (more below).
  • socketer.py - Binary-side script, holds API for sending and receiving data.
  • serverSim.py - MAS-side script simulator, used for testing without needing to open MAS.
  • MonikaAfterStoryModule.rpy - MAS-side script, holds API for sending and receiving data.

The python39 folder

Due to how Python embedding works it's standard library has to be shipped as a folder or archive, with MASM it's done as folder so new packages can be added easily.

  • DLLs - Folder for OS specific dynamic libraries Python requires.
  • Lib - Folder with Python standard library files.
  • site-packages - Folder for external packages required by Submods (e.g. numpy).

API Reference:

Sending data

Sending data to other side is done with MASM.sendData(str, value = True) function, where str is the packet name and value is the data to be sent (defaults to True).

Examples:

MASM.sendData("MyFirstPacket")
MASM.sendData("DataPacket", "Hello!")
MASM.sendData("FancyPacket", ("You can put any type here", True))
MASM.sendData("SplitPacket.42", True)

Checking for data

Checking for received data is done with either MASM.hasDataBool(str), MASM.hasDataValue(str) or MASM.hasDataWith(str) function, where str is the packet name to look for. The packet is removed if it was succesfully found.

MASM.hasDataBool(str) returns True if packet was found, False otherwise.

MASM.hasDataValue(str) returns packet's value if packet was found, None otherwise.

MASM.hasDataWith(str) returns tuple (str, value) of packet if name starting with str was found, (None, None) otherwise

Examples:

if MASM.hasDataBool("MyFirstPacket"):
	print("I received my first packet!")

if MASM.hasDataBool("DataPacket"):
	print("I received my data packet but I'm not using it's value!")

myValue = MASM.hasDataValue("DataPacket")
if myValue and myValue == "Hello!":
	print("I received my data packet and used it's value!")

myFancyValue = MASM.hasDataValue("FancyPacket")
if myFancyValue:
	(fancyString, fancyBool) = myFancyValue
	print(f"I received my fancy packet and received: {fancyString} and {fancyBool} !")

mySplitName, mySplitValue = MASM.hasDataWith("SplitPacket")
if mySplitName and mySplitValue is not None:
	split = mySplitName.split(".")
	print(f"I received a split packet with name {split[0]} and ID {split[1]} with value of {mySplitValue} !")