From 17a8ae6988fb159c921b1e6952730772ad318b19 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 22 Dec 2022 12:20:18 +0100 Subject: [PATCH] Added validate method, and docs. --- docs/src/guide/movescript/reference.md | 20 +++++++++++++++++++- src/movescript.lua | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/src/guide/movescript/reference.md b/docs/src/guide/movescript/reference.md index 4fccbd8..b695665 100644 --- a/docs/src/guide/movescript/reference.md +++ b/docs/src/guide/movescript/reference.md @@ -11,10 +11,28 @@ ms.run("2F") Runs the given `script` string as a movescript, and optionally a `settings` table can be provided. Otherwise, [default settings](settings.md) will be used. +For example: + +```lua +local ms = require("movescript") +ms.run("3F2R3B2LUD", {debug=true}) +``` + ## `runFile(filename, settings)` Reads content from the given filename and executes it as a script. Just like with `run`, an optional `settings` table can be provided. +## `validate(script, settings)` + +Validates the given `script`, by parsing its instructions in a wrapped [`pcall`](https://www.lua.org/pil/8.4.html). It returns `true` if the script is valid, or `false` and an error message describing why the script is not valid. + +For example: + +```lua +local ms = require("movescript") +local status, message = ms.validate("not a valid script.") +``` + ## `defaultSettings` -A table containing the default settings for any script executed by the movescript module. \ No newline at end of file +A table containing the default [settings](./settings.md) for any script executed by the movescript module. \ No newline at end of file diff --git a/src/movescript.lua b/src/movescript.lua index 534e2a1..dd44e50 100644 --- a/src/movescript.lua +++ b/src/movescript.lua @@ -207,4 +207,8 @@ function movescript.runFile(filename, settings) movescript.run(script, settings) end +function movescript.validate(script, settings) + return pcall(function () parseScript(script, settings) end) +end + return movescript