Skip to content

Commit 49542e5

Browse files
committed
fix defold api breaking change, set minimum version to 1.12.3
1 parent d80a6c7 commit 49542e5

5 files changed

Lines changed: 46 additions & 15 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Batteries-included development environment for the [Defold game engine](https://
1717

1818
## System Requirements
1919

20+
### Defold
21+
22+
The minimum supported Defold version is: **1.12.3**
23+
2024
### Operating System
2125

2226
- Linux 🐧 - First class & fully supported

crates/core/src/editor.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
use std::{collections::HashMap, fs, path::Path};
1+
use std::{fs, path::Path};
22

3-
use anyhow::{Result, bail};
3+
use anyhow::{Context, Result, bail};
4+
use serde_json::Value;
5+
6+
fn editor_url(port: u16) -> String {
7+
format!("http://127.0.0.1:{port}")
8+
}
9+
10+
fn openapi_url(port: u16) -> String {
11+
format!("{}/openapi.json", editor_url(port))
12+
}
413

514
fn command_url(port: u16, command: Option<String>) -> String {
615
format!(
7-
"http://127.0.0.1:{port}/command/{}",
16+
"{}/command/{}",
17+
editor_url(port),
818
command.unwrap_or_default()
919
)
1020
}
@@ -32,23 +42,35 @@ pub fn find_port(game_root: &Path) -> Option<u16> {
3242
#[must_use]
3343
pub fn is_editor_port(port: u16) -> bool {
3444
reqwest::blocking::Client::new()
35-
.head(command_url(port, None))
45+
.head(editor_url(port))
3646
.send()
3747
.is_ok_and(|r| r.status().is_success())
3848
}
3949

40-
pub fn list_commands(port: u16) -> Result<HashMap<String, String>> {
41-
let url = command_url(port, None);
50+
pub fn list_commands(port: u16) -> Result<Vec<String>> {
51+
let url = openapi_url(port);
4252

4353
let res = reqwest::blocking::get(url)?;
4454

4555
if !res.status().is_success() {
4656
bail!("could not list commands, status: {:?}", res.status());
4757
}
4858

49-
let content = res.text()?.clone();
59+
let root: Value = res.json().context("Failed to parse OpenAPI JSON")?;
5060

51-
serde_json::from_str(&content).map_err(anyhow::Error::from)
61+
let enum_values =
62+
&root["paths"]["/command/{command}"]["post"]["parameters"][0]["schema"]["enum"];
63+
64+
if let Some(list) = enum_values.as_array() {
65+
let commands = list
66+
.iter()
67+
.filter_map(|v| v.as_str().map(|s| s.to_string()))
68+
.collect();
69+
70+
Ok(commands)
71+
} else {
72+
bail!("Could not find command values in the OpenAPI specification")
73+
}
5274
}
5375

5476
pub fn send_command(port: u16, cmd: &str) -> Result<()> {

crates/sidecar/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ fn is_editor_port(_lua: &Lua, port: u16) -> LuaResult<bool> {
143143
fn list_commands(lua: &Lua, port: u16) -> LuaResult<LuaTable> {
144144
let commands = editor::list_commands(port)?;
145145

146-
lua.create_table_from(commands)
146+
let table = lua.create_table()?;
147+
148+
for (i, cmd) in commands.into_iter().enumerate() {
149+
table.set(i + 1, cmd)?;
150+
}
151+
152+
Ok(table)
147153
}
148154

149155
#[instrument(level = "debug", err(Debug), skip_all)]

lua/defold/editor.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local M = {}
22

33
---List all available Defold commands
4-
---@return table|nil
4+
---@return string[]
55
function M.list_commands()
66
local log = require "defold.service.logger"
77
local project = require "defold.project"
@@ -10,7 +10,7 @@ function M.list_commands()
1010

1111
if not port then
1212
log.error "Could not find Defold editor, is it running?"
13-
return
13+
return {}
1414
end
1515

1616
local sidecar = require "defold.sidecar"
@@ -19,10 +19,9 @@ function M.list_commands()
1919

2020
if not ok then
2121
log.error(string.format("Could not fetch commands from Defold, because: %s", res))
22-
return nil
22+
return {}
2323
end
2424

25-
---@cast res table<string, string>
2625
return res
2726
end
2827

lua/defold/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@ function M.load_plugin()
258258
return
259259
end
260260

261-
for cmd, desc in pairs(commands) do
261+
for _, cmd in ipairs(commands) do
262262
-- hide debugger related commands as they'd give the user the impression that these
263263
-- work with our debugger integration
264264
local is_debugger_command = string.find(cmd, "debugger")
265265

266266
if not is_debugger_command then
267267
table.insert(cmds, cmd)
268-
table.insert(options, string.format("%s - %s", cmd, desc))
268+
table.insert(options, cmd)
269269
end
270270
end
271271

0 commit comments

Comments
 (0)