MAKE A MHODUME PLUGIN: THE GUIDE (api 1)
========================================

The same guide as https://mhodume.com/plugins/docs/ , to read offline.

Contents
  1. Overview
  2. Quick start
  3. The folder
  4. How it runs
  5. The api
       basics · callbacks & events · settings · settings.json ·
       your page: live values, buttons · files · the player ·
       keys · drawing · replacing the mod's text
  6. Limits & sandbox
  7. Test on your PC
  8. Publish
  9. Examples


1. OVERVIEW
-----------
A Mhodume plugin is a folder holding a plugin.json and a main.lua. The mod
loads it in game and gives it `api`: read where the player is and how fast
they go, react to keys and to level changes, draw lines, boxes and text on top
of the game, keep files, and even redraw the mod's own text in another font.

  - Training only. Plugins run where the mod runs: in Training, never in
    Compete, never in online play.
  - A sandbox. A plugin can't open files outside its folder, run programs,
    reach the network, or call the game engine directly. On VHOLUME a wrong
    engine call doesn't fail, it crashes the game: the api only offers what
    has been proved safe.
  - Lua 5.4. The language UE4SS runs.
  - Errors are contained. A plugin that keeps failing is switched off; the
    mod and the other plugins go on.

What a plugin can and can't do
------------------------------
A plugin can READ what the game and the mod show, and DRAW on top of the game.
It can't CHANGE the mod's data or behaviour. Keep that split in mind:
changing how a text looks on screen is not the same as changing the thing it
describes.

  CAN                                       CAN'T
  Draw lines, boxes and text on top of      Change the game: move the player,
  the game                                  its speed, the physics, VHOLUME's
                                            own HUD and menus
  Read the player's position, speed and     Change the mod's data: sections,
  view, and the level's name                checkpoints, profiles, presets,
                                            crosshair settings, Training
  React to keys, level changes, its         Rename the mod's sections or
  settings changing, clicks on its buttons  anything it stores. It can only
                                            change how a text is drawn.
  Change how the mod's own in-game texts    Change the Launcher or the overlay:
  are drawn (font, even the string drawn)   menus, section names, other pages
  Keep files in its own folder              Read other files, other plugins'
                                            data, or the network
  Have a page in the Launcher and the       Run code in the Launcher or the
  overlay: settings, live values, buttons   overlay
  Show a message in the middle of the       Use UE4SS, or run in Compete or
  screen                                    online


2. QUICK START
--------------
  1. This folder is the template (Top Speed: your best speed on the current
     map, under the crosshair). Every line of its main.lua is commented.
  2. Put it in  %LOCALAPPDATA%\Mhodume\plugins\top-speed\
     (the folder's name is the plugin's id).
  3. Create  %LOCALAPPDATA%\Mhodume\plugins.json :

       { "top-speed": { "enabled": true, "stamp": 1 } }

  4. Start VHOLUME in Training: run fast, your top speed shows under the
     crosshair. K resets it.
  5. Rename the folder and the id in plugin.json, and make it yours.

The smallest plugin there is:

    -- main.lua
    api.onDraw(function(w, h)
        api.draw.text("HELLO", 40, 40, 20, { 1, 0.29, 0.12, 1 })
    end)


3. THE FOLDER
-------------
    my-plugin/
      plugin.json     required: who the plugin is
      main.lua        required: what it does
      settings.json   optional: the settings the Launcher shows
      README.md       optional: say what it does and its settings
      any data        read with api.load (.txt .json .csv .md, pictures)

plugin.json:

    {
      "id": "my-plugin",
      "name": "My Plugin",
      "version": "1.0.0",
      "api": 1,
      "author": "you",
      "description": "One line: what a player gets."
    }

  id           2 to 40 of a-z 0-9 - _, starting with a letter or digit. Must
               be the folder's name. It is yours once published: nobody else
               can upload under it.
  name         What players see.
  version      major.minor.patch, e.g. 1.2.0. Each upload must be higher than
               the last, and match the version typed in the upload form.
  api          The api version you wrote for. Today: 1. A mod older than your
               api refuses to load the plugin rather than run it half-working.
  author,
  description  Free text, for people reading the folder.


4. HOW IT RUNS
--------------
  1. Loading. main.lua runs once, at the top of a game frame. That's where you
     register your callbacks (api.onTick, api.onDraw, api.on) and read your
     settings and files.
  2. Each frame, in this order: your onTick functions (read the game, react to
     keys; nothing can be drawn yet), then the mod draws (crosshair, speed,
     panels…), then your onDraw functions, on top of everything.
  3. Events arrive at the top of a frame: "map" when a level is entered,
     "settings" when the player changes your settings, and what happened in
     a run (section_complete and the others, below).
  4. Switching off in plugins.json stops calling your functions at once;
     switching on again resumes them without loading the code again. A new
     "stamp" loads your files again from scratch.

When it fails: an error in one of your functions is caught and written to the
log once (the mod's, and your plugin's own plugin.log). After 60 failures in a row (about a second) the plugin is switched
off until the player presses F6 or reloads it. An error while loading main.lua
stops the plugin from loading; the reason is in the log and in
plugins\status.txt.


The life of a plugin, in order:
  1. Off in plugins.json, or not there: nothing of it runs.
  2. Turned on: at the top of the next frame of a level, main.lua runs once
     in a fresh sandbox. Plugins load in the order of their ids (a, b, c...).
     Register your callbacks here: an event that happens before you register
     for it is not sent again.
  3. First frames: as soon as the level's name is known, every plugin gets
     "map" (with old nil the first time). Then, each frame: onTick, the mod
     draws, onDraw.
  4. In menus and loading screens there is no HUD to draw on, so no callback
     is called. Your Lua stays as it was: the next level picks up where you
     left.
  5. Turned off: your callbacks stop being called, your state is kept, and
     the mod draws its own texts again. Turned on again: they resume, main.lua
     doesn't run again.
  6. A new stamp (or an update installed by the Launcher): everything of
     yours is dropped (callbacks, keys, live values, the text you took over)
     and main.lua runs again from scratch.
  7. Errors: 60 in a row switch the plugin off until F6 in game or a new
     stamp. An error in main.lua itself means it isn't loaded until a new
     stamp or the next game.
  8. The game closes: everything ends. Next game, it all starts over at step
     2, and your log becomes plugin-1.log.

5. THE API
----------
Everything below is a field of the global `api`. Coordinates are screen
pixels, (0, 0) top-left. A colour is a table { r, g, b, a }, each from 0 to 1.
Speeds and positions are in the game's units (centimetres, and centimetres per
second).

-- Basics --

  api.id                     Your plugin's id.
  api.version                The api version of the mod running you (1).
  api.log(...)               Writes a line to the mod's log, tagged [your-id].
                             print does the same.
  api.notice(text, seconds)  Shows a message in the middle of the screen,
                             3 seconds by default. One message at a time,
                             shared with the mod.
  api.time()                 Seconds, with decimals. Use differences, not the
                             value itself.
  api.frame()                The frame counter.
  api.screen()               The screen's width and height, in pixels.
  api.map()                  The current level's name, e.g. Gold_racetrack,
                             or nil.
  api.show(id, value)        Shows a value live on your plugin's page, on the
                             "value" line of settings.json with this id. Text
                             up to 200 characters; nil clears it.

-- Callbacks & events --

  api.onTick(function(w, h) ... end)
      Called every frame, before anything is drawn. Read the game and keys here.
  api.onDraw(function(w, h) ... end)
      Called every frame after the mod has drawn. The only place you can draw.
  api.onText(function(text, element) ... end)
      A text the mod shows appeared or changed (text nil: it's gone). See
      "Reading the mod's text".
  api.on("map", function(new, old) ... end)
      A level was entered, the first one too (old is nil then).
  api.on("settings", function() ... end)
      Your settings changed: read them again.
  api.on("button", function(id) ... end)
      One of your "button" settings was clicked in the Launcher or the
      overlay; id says which.

You can register several functions of each kind; they run in the order
registered.

What happens in a run - Mhodume's sections and the level's timer, at the top
of the next frame. Each function gets a table of its own (numbers, text,
true/false): no other plugin sees what you change in it, and nothing you do
changes Mhodume's times. Times in seconds.

  run_start         the first section entered, no run going
                    map, sections
  section_start     a section entered          map, section, sections
  section_complete  a section closed           map, section, sections, time,
                    best (before this one; absent the first time),
                    delta (time - best), isBest
  run_complete      every section, in order    map, sections, time, best,
                                               delta, isBest
  lap_abandoned     a section left open        map, reason
  drill_back        drilling put you back      map, section, why
                                               ("reset" or "finished")
  timer_start       the level's own timer starts (your first move)    map
  timer_reset       the level's own timer back to 0 (T)               map

These are Mhodume's sections (the Sections page), not the game's own
checkpoints. For example:

    api.on("section_complete", function(s)
        api.log(string.format("section %d in %.3f", s.section, s.time))
    end)

Your records - your best time on each level and your times at the game's
checkpoints in it, from the ghosts the game keeps of your runs (one per
level and category: your best). The Launcher reads them while it is open;
downloaded ghosts are left out. Times in seconds, a checkpoint's from the
start of the run.

  api.record(map, category)  Your record on a level: this one when map is
                             nil, in "Normal" unless you name another (or
                             the only one there is). nil when there's none.
  api.records()              Every record, every level and category.

  A record: map, category, time, checkpoints (a list, in order, of
  { id = "CP_...", time = ... } - the game's checkpoints, not Mhodume's
  sections), hops and perfectHops (nil when not counted), player.

    api.on("map", function(map)
        local r = api.record()
        if r then api.notice(string.format("Your record: %.3f", r.time)) end
    end)

-- Settings --

  api.setting(name, default)   One setting, or default when it isn't set.
  api.settings()               All of them, as a table.

Settings come from what the player set in the Launcher (or by hand in
plugins.json): numbers, text, true/false, lists. Always give a default;
declare them in settings.json, below, so the Launcher can show them.

    local size = tonumber(api.setting("size", 14)) or 14
    api.on("settings", function() size = tonumber(api.setting("size", 14)) or 14 end)

-- Settings players can change: settings.json --

Put a settings.json beside your main.lua and the Launcher shows a Settings
button for your plugin, with a control for each setting. What the player sets
reaches you through api.setting, and a "settings" event tells you when it
changes.

    {
      "settings": [
        { "id": "size",  "type": "number", "label": "Text size", "default": 14, "min": 8, "max": 40, "step": 1 },
        { "id": "color", "type": "color",  "label": "Colour",    "default": [1, 0.29, 0.12, 1] },
        { "id": "where", "type": "choice", "label": "Position",  "default": "top",
          "options": [ { "value": "top", "label": "Top" }, { "value": "bottom", "label": "Bottom" } ] },
        { "id": "font",  "type": "font",   "label": "Font",      "default": "font.txt" }
      ]
    }

Every setting has an id (a letter, then letters, digits or _), a type, a
label, a default, and may have a help line shown under the label.

  toggle   a check box            true / false
  number   a slider               a number               min, max, step
  text     a text box             text
  key      a button: the player clicks it and presses the key (or a middle
           or side mouse button)  the key's name, for api.key / api.pressed
  choice   a drop-down list       the chosen value       options: values, or { "value", "label" }
  multi    check boxes            a list of the chosen values   options, as above
  color    a colour box           { r, g, b, a }, 0 to 1
  value    a line showing a value, live   nothing: fill it with api.show(id, ...)
  button   a button               nothing: a click is a "button" event with
                                  this id; confirm: a question asked first
  list     a table, live      nothing: fill it with api.list(id, rows)
                              columns: the headings
  group    a heading that folds what follows, up to the next group
           (it needs no id)   collapsed: true to start it folded
  font     "Choose a font..."     the name of a font file in your folder
                                  heights: the text heights to prepare,
                                  default [14, 16, 19, 24, 30, 38]

Groups: a "group" line starts a section of your page with its label as a
heading; the player clicks it to fold or unfold it. Everything after it
belongs to it, up to the next group.

    { "type": "group", "label": "On screen" },
    { "id": "size", "type": "number", "label": "Text size", "default": 14 },
    { "type": "group", "label": "Colours", "collapsed": true },
    { "id": "color", "type": "color", "label": "Colour", "default": [1, 1, 1, 1] }

The key type: let the player choose your keys. A "key" setting gives you the
name of the key they pressed, ready for api.pressed; when they change it, the
old key stops counting against your 8.

    local key = api.setting("resetKey", "K")
    api.on("settings", function() key = api.setting("resetKey", "K") end)
    api.onTick(function() if api.pressed(key) then reset() end end)

The font type: the player picks any .ttf / .otf, or the .zip from
fonts.google.com. The Launcher turns it into a font-<name>.txt file in your
folder (every printable character as rectangles, at each height) and sets
your setting to that file name. Read it with api.load and draw the rectangles
with api.draw.rect: the Custom Font example has the whole reader. Ship a
default font file of your own as the default, made with
tools/make_plugin_font.py.

What you choose, what you don't: you choose which settings there are, their
order, type, label, help line, options, default and range, and what each one
does, since your Lua reads the value. The Launcher draws the window: its
layout and look are the same for every plugin, and so are its own words
("Changes apply in game within a second", "Reset to defaults", the font
control's buttons). A plugin never runs code in the Launcher: it describes
its page, sends values to show, and is told about clicks.

-- Your plugin's page: live values, lists and buttons --

Everything in settings.json makes your plugin's page. Players find it in two
places: the Launcher (Plugins -> your plugin -> Settings) and, while they
play, the overlay's Plugins section. Besides settings, a page can hold:

  - Live values: a "value" line, filled from the game with api.show. The page
    reads them every second; when the game isn't running your plugin, it
    shows "-".
  - Lists: a "list" line, a table you fill from the game with api.list: a
    history of runs, your times on each level... It scrolls past a dozen rows.
  - Buttons: a "button" line. A click reaches your plugin in game as a
    "button" event, within a second. Clicks made while the game was closed
    are not delivered later.

api.list(id, rows) fills a list: rows is a table of rows, each a table of
cells (or one value for a one-column list). Up to 200 rows of 10 cells, 80
characters a cell. Give the whole list each time; api.list(id, nil) empties
it. Sending the same rows again costs nothing.

    -- settings.json
    { "id": "history", "type": "list", "label": "Last runs",
      "columns": ["Level", "Time", "vs best"] }

    -- main.lua
    local runs = {}
    api.on("run_complete", function(r)
        table.insert(runs, 1, { r.map, string.format("%.3f", r.time),
                                r.delta and string.format("%+.3f", r.delta) or "first" })
        api.list("history", runs)
    end)

    -- settings.json
    { "settings": [
      { "id": "best",  "type": "value",  "label": "Top speed on this map" },
      { "id": "reset", "type": "button", "label": "Reset top speed", "confirm": "Reset it?" }
    ] }

    -- main.lua
    api.onTick(function()
        local speed = api.player.speed()
        if speed and speed > best then best = speed; api.show("best", string.format("%.0f", best)) end
    end)
    api.on("button", function(id)
        if id == "reset" then best = 0; api.show("best", "0") end
    end)

The values go through live.txt in your folder, the clicks through
plugins.json: nothing of yours runs outside the game. This template has both.

-- Files --

  api.load(name)         The text of a file in your folder, or nil if there
                         is none.
  api.save(name, text)   Writes a file in your folder (1 MB at most). Returns
                         true when written.
  api.json(text)         Reads JSON: the value, or nil and the reason.

A name is a plain file name with an extension, like best.txt: no folders, no
"..". plugin.json, main.lua, settings.json, live.txt and your logs can't be
written. Read files
when loading or on an event, not every frame; for big data, a simple line
format is much faster to read than JSON.

-- The player --

  api.player.position()   x, y, z, or nil when there is no player (menus,
                          loading).
  api.player.speed()      Horizontal speed, and vertical speed apart
                          (negative when falling), or nil.
  api.player.look()       pitch, yaw in degrees.

These work inside your callbacks (and while loading), and read the game once
per frame however often you ask.

-- Keys --

  api.key(name)       True while the key is held.
  api.pressed(name)   True on the one frame the key goes down. Ask it in
                      onTick, every frame.

Names are Unreal Engine's: A–Z, F1–F12, One–Zero, SpaceBar, LeftShift, Tab,
MiddleMouseButton, ThumbMouseButton… A plugin may watch 8 keys at once; when
its settings change the count starts again, so a key the player swapped for
another in a "key" setting stops counting. Keys are asked
of the game, not bound, so they cost the mod nothing and the game still
receives them: pick keys the player doesn't use for moving, and make them a
setting.

-- Drawing --

  api.draw.rect(x, y, w, h, colour)
      A filled box. Transparency works.
  api.draw.line(x1, y1, x2, y2, colour, thickness)
      A line, 1 px thick by default. Lines ignore transparency (the engine
      draws them opaque): use boxes for anything see-through.
  api.draw.text(text, x, y, height, colour)
      Text in the mod's line font, top-left at x, y. Letters, digits and
      . , : - + / % ; lowercase is drawn as capitals.
  api.draw.textWidth(text, height)
      How wide that text will be, to centre or align it.

    api.onDraw(function(w, h)
        local text = "SECTION 2"
        local width = api.draw.textWidth(text, 16)
        api.draw.rect(w / 2 - width / 2 - 6, 96, width + 12, 24, { 0, 0, 0, 0.5 })
        api.draw.text(text, w / 2 - width / 2, 100, 16, { 1, 1, 1, 1 })
    end)

Drawing works only inside onDraw (and a text face, below). All plugins
together get 4000 drawing calls a frame; past that, the call fails for the
rest of the frame.

-- Reading the mod's text --

api.onText(fn) tells you what the mod shows, without drawing it: fn(text,
element) is called at the top of the frame after a text appeared or changed,
and with text nil once that element is no longer drawn. element is one of
those listed below; an element drawn as several strings (the Sections panel)
comes as one text, a line each. As many plugins as like can read the texts,
whichever one draws them.

    api.onText(function(text, element)
        if element == "splits" and text then api.log("split shown: " .. text) end
    end)

A text that changes every frame, like the speed, calls you every frame: keep
the function short, or use api.player.speed() instead. For a run's times, the
run events give you numbers rather than text.

-- Replacing the mod's text --

api.replace("text", face) takes over how the mod draws its own text: the
speed, the badge, the panels. face has two functions; return nil from either
to leave that text to the mod:

    api.replace("text", {
        width = function(text, height, element)
            if element ~= "speed" then return nil end
            return #text * height * 0.6            -- how wide you'll draw it
        end,
        draw = function(text, x, y, height, colour, element)
            if element ~= "speed" then return nil end
            -- draw it with api.draw.rect / line, top-left at x, y
            return true
        end,
    })

element says which text it is: speed, badge, notice, panel, splits, follow,
keys. colour is the colour the mod would have used. Give the same answer to
width and draw for a text: the width is what centres it. Only one plugin can
replace the text; if your face fails, the mod's own lines take over. The
Custom Font example does exactly this.

ONE PLUGIN AT A TIME. Only one plugin can take over the mod's text. The
second one to call api.replace("text") gets the error "the text is already
drawn by <id>" (custom-font, for instance), even if the two would touch
different elements. Plugins load in the order of their ids, so the first one
wins. Turning the owner off gives the texts back to the mod but doesn't free
the place: turn it off, then restart the game (or give the second plugin a new
stamp).

What each element sends. Every string the mod draws in game is sent whole,
one call per string. These are real examples; use them, not guesses, to
decide what your plugin matches.

  speed    the speed under the crosshair: "1235", or "1234.6" with
           decimals, or "1235 u/s" with a suffix set in the Speed page
  badge    top right, in Training: "TRAINING", or "LAP NOT COUNTABLE"
           (both can be changed in the Training page)
  notice   a message in the middle of the screen: "CHECKPOINT PLACED",
           "FINISH OFF IN MULTIPLAYER - MOD LOADED", and what plugins send
           with api.notice
  splits   under the crosshair when you finish a section, or the running
           clock: "3  +0.420" (the section's number, two spaces, the gap to
           your best), "3" when there's no best yet, "1:02.345" for the clock
  panel    the Sections panel: the headers "best", "old", "new" (lowercase:
           the line font draws them as capitals), row numbers "1:", "2:"...,
           times "12.345" or "1:02.345", and "-" for no time
  follow   the score while following a drawn run: "L 87%", "R 92%"
  keys     the key pad of a drawn run: "W", "A", "S", "D", "JMP", "CRH"

Sections have numbers, not names: no text says "SECTION 2". Nothing else goes
through the face: not the Launcher or the overlay, not VHOLUME's own HUD, not
what plugins draw with api.draw.text.

The contract:
  - width(text, height, element) returns the width you will draw, in
    pixels, or nil to leave this text to the mod. draw(text, x, y, height,
    colour, element) returns true once drawn, or nil to leave it to the mod.
  - For a given text, answer both the same way. The mod centres the text
    with your width, then calls your draw at the position that gives. A width
    without a draw, or the other way round, misplaces it.
  - x, y are the top-left corner in screen pixels; height is the height of a
    capital letter in pixels; colour is { r, g, b, a } from 0 to 1, ready to
    pass to api.draw.
  - You can call api.draw.text inside draw: it always uses the mod's line
    font and never comes back to your face, so there is no loop to guard
    against.
  - An error in either function counts against your plugin, and that text is
    drawn by the mod for the frame.


-- Every call: what it returns, when it fails --

"Raises" means the call stops your function with an error, which the mod
catches and counts against your plugin (see "the life of a plugin").

  api.log(...)              nothing. Never fails; past 2000 lines a game your
                            log stops (the mod's still gets them).
  api.notice(text, s)       nothing. Never fails; replaces whatever message
                            is showing.
  api.time(), api.frame()   a number. Never fails.
  api.screen()              width, height. 0, 0 before the first frame.
  api.map()                 the level's name. nil in menus or before it is
                            known.
  api.record(map, cat)      a record. nil when there's no ghost of yours.
  api.records()             a list of records, maybe empty. Never fails.
  api.show(id, value)       nothing. Raises if id isn't a letter then
                            letters, digits or _.
  api.list(id, rows)        nothing. Raises for an id as above, or rows that
                            isn't a table or nil; past 200 rows, 10 cells or
                            80 characters, the rest is left out.
  api.onTick, api.onDraw,
  api.onText                nothing. Raise if not given a function.
  api.on(event, fn)         nothing. Raises for an event that isn't listed
                            (map, settings, button, or a run event).
  api.setting(id, default)  the value as stored, or default. Never fails, but
                            the value can be of any type if the player edited
                            plugins.json by hand: check it (tonumber, type).
  api.load(name)            the file's text, or nil if there is no such file.
                            Raises if name isn't a plain file name.
  api.save(name, text)      true once written, false if the file couldn't be
                            opened. Raises for a name that isn't plain, a
                            reserved file, or more than 1 MB.
  api.json(text)            the value, or nil and the reason.
  api.player.position()     x, y, z, or nil when there is no player. Raises
                            outside your callbacks and loading.
  api.player.speed()        horizontal, vertical. Same as position.
  api.player.look()         pitch, yaw. Same as position.
  api.key(name),
  api.pressed(name)         true or false. Raise for a name that isn't
                            letters, digits or _, or for a ninth key; a name
                            the game doesn't know is simply never pressed.
  api.draw.rect/line/text   nothing. Raise outside onDraw and a text face,
                            for a coordinate that isn't a number, and past
                            4000 drawing calls in a frame.
  api.draw.textWidth        a width in pixels. Raises if height isn't a
                            number.
  api.replace("text", face) nothing. Raises for anything but "text", a face
                            without width and draw, or when another plugin
                            has the text.

6. LIMITS & SANDBOX
-------------------
  Keys watched        8 per plugin at once
  A list              200 rows, 10 cells, 80 characters a cell
  Drawing calls       4000 per frame, all plugins together
  Errors              60 in a row, then off until F6 or a reload
  A file written      1 MB
  The uploaded .zip   5 MB, 200 files, 20 MB unzipped

Available: string, table, math, pairs, ipairs, next, select, type, tostring,
tonumber, pcall, xpcall, error, assert, setmetatable, getmetatable,
rawget/rawset/rawequal, unpack, print, and os.clock, os.time, os.date. Your
string, table and math are your own copies.

Not available: io, the rest of os, require, load, loadfile, dofile, debug,
package, coroutine, and everything of UE4SS. Only source Lua is accepted:
precompiled code is refused.

Keep it light. Your functions run every frame of the game. Do the heavy work
once (when loading, or on an event) and keep onTick and onDraw short. A
plugin that loops for ever freezes the game: there is no way for the mod to
stop it.


7. TEST ON YOUR PC
------------------
What players see: in the Launcher's Plugins window each plugin shows its
name, version, description and state: ON, OFF, COULDN'T LOAD (an error while
loading main.lua) or SWITCHED OFF AFTER ERRORS, with the reason the mod gave.
Players turn plugins on and off, open their Settings, and remove them; an
update keeps the files your plugin saved for itself. In game, the overlay's
Plugins section shows each plugin's page.

Without the game: the simulator (https://mhodume.com/plugins/docs/#sim) runs
your plugin on your PC in the mod's own plugin code, with a made-up game -
events, keys, texts, records - and says what went wrong. Install Lua 5.4 once
(winget install DEVCOM.Lua), unzip the simulator next to your folder, then:

    lua mhodume-sim.lua ..\my-plugin
    lua mhodume-sim.lua ..\my-plugin my-scenario.lua

A scenario says what happens, frame by frame, and checks what you expect:
sim.frames(n), sim.map(name), sim.player{...}, sim.press(key),
sim.event(name, data), sim.text(element, text), sim.settings{...},
sim.button(id), sim.records{...}, sim.draws(), sim.live(), sim.logs(),
sim.expect(ok, message). Its README and example-scenario.lua have the rest.

The quick way: in the Launcher, Plugins -> drop your .zip or folder on the
window. It is installed and on, and its Settings, if any, are one click away.
By hand, which is handy while you write it:

  1. Your folder goes in  %LOCALAPPDATA%\Mhodume\plugins\<id>\
  2. %LOCALAPPDATA%\Mhodume\plugins.json says which plugins are on, and their
     settings:

       {
         "my-plugin":  { "enabled": true, "stamp": 1, "settings": { "size": 18 } },
         "top-speed":  { "enabled": false }
       }

     The mod reads it a few times a second: no restart needed.
  3. Changed main.lua? Put a new value in "stamp" (2, 3…) and your plugin is
     loaded again.
  4. Your plugin's log: plugin.log, in your plugin's folder (the Launcher's
     Log button opens it). It holds what you api.log, and what the mod says
     about your plugin: loaded, refused, its errors, switched off, reloaded.
     The game before is kept as plugin-1.log. The same lines also go to the
     mod's log, %LOCALAPPDATA%\Mhodume\logs\mhodume.log, tagged [my-plugin].
  5. The status: %LOCALAPPDATA%\Mhodume\plugins\status.txt, one line per
     plugin: on, off, error (couldn't load) or failed (switched off after
     errors), with the reason.


When something goes wrong:

  COULDN'T LOAD
    Why: main.lua failed as it ran (a syntax error, or a call that raised
    while loading).
    Do: open the plugin's log (Log in the Launcher): the first error says
    where. Fix it, then give it a new stamp.

  SWITCHED OFF AFTER ERRORS
    Why: one of your functions failed 60 frames in a row.
    Do: only the first error is written, the rest are the same one. Read it
    in the log, fix it, then F6 in game or a new stamp.

  "the text is already drawn by custom-font"
    Why: another plugin took over the text first.
    Do: turn one of them off and restart the game. Two plugins can't both
    change the mod's text for now.

  ON, but nothing happens
    Why, usually one of: not in Training; not in a level (menus draw
    nothing); your plugin matches a text that never appears (see "what each
    element sends"); drawing off screen; another plugin has the text; a
    setting you read isn't the type you expect.
    Do: log what you receive, a few lines only (below), and check the screen
    size with api.screen().

  Live values show "-"
    Why: the game isn't running the plugin (closed, in a menu, or the plugin
    is off).
    Do: start a level in Training with the plugin on.

To see what your plugin really gets without flooding the log (it runs every
frame), log a little and stop:

    local seen = 0
    api.replace("text", {
        width = function(text, height, element)
            if seen < 20 then
                seen = seen + 1
                api.log("text", element, text)      -- the first 20 texts only
            end
            return nil                              -- leave them all to the mod
        end,
        draw = function() return nil end,
    })
    api.on("map", function(new) api.log("map", new) end)
    api.on("settings", function() api.log("size =", tostring(api.setting("size"))) end)

8. PUBLISH
----------
  1. Zip the contents of your folder: plugin.json and main.lua must be at the
     root of the .zip, not inside a sub-folder.
  2. Upload it at https://mhodume.com/plugins/upload/ (sign in with Discord
     first). Name, category, one line saying what it does, the version (the
     same as in plugin.json).
  3. A short wait. The team has a look at each plugin before it appears, so it
     can take a little while. If it isn't accepted, the reason shows on the
     upload page under "Your uploads".
  4. Updates: same id, a higher version, upload again. Players get the new
     version once it is approved.

What is refused: anything for online play or Compete, cheating a leaderboard,
executables or game files, other people's work without their permission, and
fonts or pictures whose licence doesn't allow sharing (put the licence file in
the .zip, as the Custom Font example does).

Files a .zip may hold: .lua .json .txt .md .csv .png .jpg .jpeg .webp


9. EXAMPLES
-----------
  Top Speed (this folder) is the template: settings.json, a live value, a
  button, events, a key, a file,
  reading the player, drawing and notices. Every line is commented.

  Custom Font draws the speed and the badge in a real font: Wallpoet, or any font
  the player chooses. Shows api.replace("text"), a "font" setting, reading a
  data file, and shipping a font licence.

  Download it, and the simulator, from
  https://mhodume.com/plugins/docs/#examples
