1-- load api from an external file
2local api = dofile(ide:GetPackagePath('solarus_lua_api.lua'))
3
4local function make_map_api(lua_file_path)
5  local dat_path = lua_file_path:gsub('.lua','.dat')
6  local entities = {}
7  local function create_ent(ent_type,params)
8    assert(type(params)=='table',"params should be a table")
9    local name = params.name
10    local x,y,l = params.x,params.y,params.layer
11    if name then
12      -- Name is exportable!
13      entities[name] = {
14        type = 'value',
15        description = string.format("%s at <%d,%d,%d>",ent_type,x,y,l),
16        valuetype = ent_type
17      }
18    end
19  end
20
21  local env_mt = {
22    __index = function(t,k)
23      return function(params)
24        create_ent(k,params)
25      end
26    end
27  }
28  local env = setmetatable({},env_mt)
29  local dat_func = loadfile(dat_path)
30  if dat_func then
31    setfenv(dat_func,env)
32    -- generate entities
33    dat_func()
34  end
35  return entities
36end
37
38local current_editor
39local current_api
40local function switch_editor(editor)
41  if current_editor == editor then
42    return
43  end
44  current_editor = editor
45  if not editor then
46    return
47  end
48  lua_file_path = ide:GetDocument(editor).filePath
49  if lua_file_path and lua_file_path:match('/data/maps/') then
50    local map_api = make_map_api(lua_file_path)
51    current_api = map_api
52    ide:AddAPI('lua','solarus_map',map_api)
53  else
54    if current_api then
55      --ide:RemoveApi('lua','solarus_map')
56      ide:AddAPI('lua','solarus_map',{}) --edit api to empty one
57      current_api = nil
58    end
59  end
60end
61
62local interpreter = {
63  name = "Solarus",
64  description = "An ARPG Game Engine",
65  api = {"baselib", "solarus","solarus_map"},
66  frun = function(self,wfilename, rundebug)
67    local projdir = self:fworkdir(wfilename)
68    local datalessdir = projdir:match('(.*)[/\\]data.*$') or projdir --remove data folder if present
69
70
71    local debuggerPath = ide:GetRootPath("lualibs/mobdebug/?.lua")
72    local packagePath = ide:GetPackagePath("?.lua")
73
74    local engine_cmd = ide.solarus_path or "solarus-run" --TODO take windows (and mac) into account
75    local cmd = string.format('%s "%s"',engine_cmd,datalessdir)
76    if rundebug then
77      ide:GetDebugger():SetOptions({runstart = true})
78      --adapt command to run debug connection
79      local code = string.format(
80[=[local path=package.path;package.path=[[%s;%s;]]..path;local mdb=require('mobdebug');require('solarus_pretty_printer');package.path=path;mdb.coro();mdb.start()]=],
81        debuggerPath,
82        packagePath)
83      cmd = string.format('%s -s="%s" "%s"',engine_cmd,code,datalessdir)
84    end
85    CommandLineRun(cmd,datalessdir,true,false)
86  end,
87  hasdebugger = true,
88  takeparameters = true
89}
90
91return {
92  name = "Solarus",
93  description = "An ARPG Game Engine",
94  author = "stdgregwar",
95  version = 1.6,
96
97  onRegister = function(self)
98    ide:AddAPI("lua","solarus",api)
99    ide:AddInterpreter("Solarus", interpreter)
100  end,
101
102  onEditorFocusSet = function(self,editor)
103    switch_editor(editor)
104  end,
105
106  onUnRegister = function(self)
107    ide:RemoveAPI("lua","solarus")
108    ide:RemoveInterpreter("Solarus")
109  end
110}
111