tts_external_api/
lib.rs

1//! A Rust implementation of the [External Editor API](https://api.tabletopsimulator.com/externaleditorapi/) for Tabletop Simulator.
2//!
3//! This is intended to make it easier to write development tools and plugins
4//! instead of using the built-in script editor.
5//!
6//! Communication between the editor and TTS occurs via two localhost TCP connections:
7//! one where TTS listens for messages and one where the [`ExternalEditorApi`] listens for messages.
8//! All communication messages are sent in JSON.
9//!
10//! # Quick Start
11//!
12//! All messages are sent and received through the [`ExternalEditorApi`] struct.
13//! Creating the struct binds the TcpListener to port 39999. In order for messages to be
14//! sent, a game has to be loaded inside Tabletop Simulator.
15//! ```
16//! use tts_external_api::ExternalEditorApi;
17//!
18//! fn main() {
19//! 	let api = ExternalEditorApi::new();
20//!
21//!     api.execute(String::from("print('Hello World')"))
22//!         .expect("Can't connect. Is a save loaded?");
23//! }
24//! ```
25
26#![deny(missing_docs)]
27
28mod error;
29pub mod messages;
30pub mod tcp;
31
32pub use crate::tcp::ExternalEditorApi;
33pub use serde_json::{json, Value};
34
35/////////////////////////////////////////////////////////////////////////////
36
37#[cfg(test)]
38mod tests {
39    use crate::{json, messages, ExternalEditorApi};
40
41    #[test]
42    fn test_get_scripts() {
43        let api = ExternalEditorApi::new();
44
45        let answer = api.get_scripts().unwrap();
46        println!("{:#?}", answer.script_states);
47    }
48
49    #[test]
50    fn test_reload() {
51        let api = ExternalEditorApi::new();
52
53        let answer = api.reload(json!([])).unwrap();
54        println!("{:#?}", answer.script_states);
55    }
56
57    #[test]
58    fn test_custom_message() {
59        let api = ExternalEditorApi::new();
60
61        api.custom_message(json![{"foo": "Foo"}]).unwrap();
62    }
63
64    #[test]
65    fn test_execute() {
66        let api = ExternalEditorApi::new();
67
68        let answer = api
69            .execute(String::from("return JSON.encode({foo = 'Foo'})"))
70            .unwrap();
71        println!("{:#?}", answer);
72    }
73
74    #[test]
75    fn test_new_object() {
76        let api = ExternalEditorApi::new();
77
78        let answer: messages::AnswerNewObject = api.wait();
79        println!("{:#?}", answer);
80    }
81
82    #[test]
83    fn test_read() {
84        let api = ExternalEditorApi::new();
85
86        loop {
87            let answer = api.read();
88            println!("{:#?}", answer);
89        }
90    }
91}