Custom Messages
The extension supports sending and receiving custom messages through the sendExternalMessage and onExternalMessage API. Some extension features (like macro functions) are implemented through this, but they could also be used on their own.
This section describes the supported messages and their format.
In the tables describing the fields of a message a ?
denotes and optional field.
E.g. title?
means there’s a field called title
but it is optional and can be nil
.
Messages from TTS to the extension
Those messages can be sent to the extension with the sendExternalMessage
function inside TTS.
Each message has type
field to distinguish them and fields specific to the message.
sendExternalMessage({
type = "object"
})
Messages sent from TTS are only handled when the respective setting is enabled. Otherwise, they are ignored. The setting is enabled by default, but it might make sense to disable it when working with unknown or untrusted mods as the messages can result in files to be written to the user’s filesystem. |
Request object message
This message can be sent from TTS to the extension to trigger an object selection inside VS Code. The selection will show a quick pick of currently loaded objects. Picking a selection sends the Object message to TTS. Cancelling the selection doesn’t send any messages back.
Name | Type | Usage |
---|---|---|
|
|
Used to distinguish different messages. |
|
|
An optional tile text that will be added to the top of the quick pick. |
|
|
An optional text that will be added to the text box of the quick pick (see screenshot above). |
|
|
If |
sendExternalMessage({
type = "object",
withGlobal = true,
placeholder = "Select an object to proceed",
})
Write content message
This message can be sent from TTS to request that text is shown in an editor in VS Code. Additionally, the text can also be written to a file. The content/file will also be opened in VS Code after receiving this message.
Name | Type | Usage |
---|---|---|
|
|
Used to distinguish different messages. |
|
|
The content to write to a file/to show in the editor. |
|
|
The name of the file to write. When not set, no file will be written. Instead, the content will be shown in a temporary editor in VS Code. The file will be written to the workspace directory. |
|
|
The guid of the object the content belongs to. Use |
|
|
Determines how the |
sendExternalMessage({
type = "write",
content = "Hello World",
name = "Test.txt",
})
This will write a file called Test.txt
with this content:
Hello World
sendExternalMessage({
type = "write",
content = '{ "this": { "is": "json" } }',
object = "-1",
name = "test.txt"
})
This will write a file called Global.test.txt
inside the output directory with the content formatted as:
{
"this": {
"is": "json"
}
}
Example
Files can not be written outside the workspace directory (e.g. by using a name like ../../test.txt ).
Writing a file will be ignored, if the resolved file path is not inside the workspace directory.
|
Messages from the extension to TTS
Those messages can be handled with the onExternalMessage
event inside TTS.
Each message has type
field to distinguish them and fields specific to the message.
function onExternalMessage(message)
if message.type == "object" then
-- do things with the message
end
end
Object message
This message is sent as a response to the Request object message after a selection in VS Code was made. If the selection was cancelled, no message is sent.
Name | Type | Usage |
---|---|---|
|
|
Used to distinguish different messages. |
|
|
The GUID of the object that was requested.
Will be |
function onExternalMessage(message)
if message.type == "object" then
local object
if message.guid == "-1" then
object = Global
else
object = getObjectFromGUID(message.guid)
end
print(object.UI.getXML())
end
end