Skip to content

Commit a48fe85

Browse files
authored
Merge pull request #9 from vallode/feature/support-more-cpp-functions
Support functions declared in LuaApi
2 parents a790224 + c5de45d commit a48fe85

File tree

8 files changed

+149
-29
lines changed

8 files changed

+149
-29
lines changed

dist/library/modules/matinfo.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class matinfo_module
5+
---@field find function
6+
---@field decode function
7+
---@field getToken function
8+
---@field toString function
9+
---@field getCraftClass function
10+
---@field matches function
11+
dfhack.matinfo = {}
12+

dist/library/modules/pen.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class pen_module
5+
---@field parse function
6+
---@field make function
7+
---@field __index function
8+
---@field __pairs function
9+
---@field __newindex function
10+
dfhack.pen = {}
11+

dist/library/modules/penarray.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class penarray_module
5+
---@field new function
6+
---@field clear function
7+
---@field get_dims function
8+
---@field get_tile function
9+
---@field set_tile function
10+
---@field draw function
11+
dfhack.penarray = {}
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class persistent_module
5+
---@field getSiteDataString function
6+
---@field saveSiteDataString function
7+
---@field deleteSiteData function
8+
---@field getWorldDataString function
9+
---@field saveWorldDataString function
10+
---@field deleteWorldData function
11+
dfhack.persistent = {}
12+

dist/library/modules/random.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class random_module
5+
---@field new function
6+
---@field init function
7+
---@field random function
8+
---@field drandom function
9+
---@field drandom0 function
10+
---@field drandom1 function
11+
---@field unitrandom function
12+
---@field unitvector function
13+
---@field perlin function
14+
dfhack.random = {}
15+

dist/library/modules/screen.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ function dfhack.screen.keyToChar(key) end
6161
---@return df.interface_key
6262
function dfhack.screen.charToKey(code) end
6363

64-
---@param cmd df.zoom_commands
65-
---@return nil
66-
function dfhack.screen.zoom(cmd) end
67-
6864
---@return boolean
6965
function dfhack.screen.inGraphicsMode() end
7066

dist/library/modules/textures.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- THIS FILE WAS GENERATED AUTOMATICALLY. DO NOT EDIT.
2+
---@meta
3+
4+
---@class textures_module
5+
dfhack.textures = {}
6+
7+
---@param file string
8+
---@param tile_px_w integer
9+
---@param tile_px_h integer
10+
---@param reserved boolean|nil
11+
---@return { [integer]: unknown }
12+
function dfhack.textures.loadTileset(file, tile_px_w, tile_px_h, reserved) end
13+
14+
---@param handle unknown
15+
---@return number
16+
function dfhack.textures.getTexposByHandle(handle) end
17+
18+
---@param handle unknown
19+
---@return nil
20+
function dfhack.textures.deleteHandle(handle) end
21+
22+
---@param pixels { [integer]: integer }
23+
---@param tile_px_w integer
24+
---@param tile_px_h integer
25+
---@param reserved boolean|nil
26+
---@return unknown
27+
function dfhack.textures.createTile(pixels, tile_px_w, tile_px_h, reserved) end
28+
29+
---@param pixels { [integer]: integer }
30+
---@param texture_px_w integer
31+
---@param texture_px_h integer
32+
---@param tile_px_w integer
33+
---@param tile_px_h integer
34+
---@param reserved boolean|nil
35+
---@return { [integer]: unknown }
36+
function dfhack.textures.createTileset(pixels, texture_px_w, texture_px_h, tile_px_w, tile_px_h, reserved) end
37+

lib/cpp.rb

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ class << self
2121
# attempts to find the relevant module files and their function
2222
# signatures.
2323
def parse_cpp_modules(entry_point)
24+
# TODO: Of course we cannot do this forever!
2425
ignored_modules = %w[console]
2526

2627
file = File.read(entry_point)
2728
directory = File.dirname(entry_point)
2829

29-
file.scan(/^static.*module\[\][\s\S]+?};/) do |cpp_module|
30-
module_name = cpp_module[/\S+(?=_module\[\])/].gsub('dfhack_', '')
31-
32-
next if ignored_modules.include? module_name
30+
modules = file.scan(/^static.*dfhack_(.*)(?:_funcs|_module)\[\][\s\S]+?};/).flatten.uniq.reject do |name|
31+
ignored_modules.include? name
32+
end
3333

34-
module_file = if %w[dfhack internal].include? module_name
35-
file
34+
modules.each do |module_name|
35+
module_path = "#{directory}/modules/#{module_name.capitalize}.cpp"
36+
is_module = file =~ /^static.*#{module_name}_module\[\]/
37+
module_file = if File.exist? module_path
38+
File.read(module_path)
3639
else
37-
File.read("#{directory}/modules/#{module_name.capitalize}.cpp")
40+
file
3841
end
3942

4043
File.open("dist/library/modules/#{module_name}.lua", 'w') do |output|
@@ -44,36 +47,59 @@ def parse_cpp_modules(entry_point)
4447

4548
output << "---@class #{module_name}_module\n"
4649

50+
prefix = module_name == 'dfhack' ? '' : 'dfhack.'
51+
namespace = is_module ? "#{module_name.capitalize}::" : ''
52+
53+
functions = []
54+
55+
module_declaration = file[/^static.*#{module_name}_module\[\][\s\S]+?};/]
56+
function_declaration = file[/^static.*#{module_name}_funcs\[\][\s\S]+?};/]
57+
4758
# Functions with signatures that are unlikely to be easily parsed.
48-
cpp_module.scan(/(?:WRAP_VERSION_FUNC|WRAPN)\(([^)]+)\)/) do |match|
59+
module_declaration&.scan(/(?:WRAP_VERSION_FUNC|WRAPN)\(([^)]+)\)/) do |match|
4960
function_name = match[0].split(', ')[0]
5061
output << "---@field #{function_name} function\n"
5162
end
5263

53-
prefix = module_name == 'dfhack' ? '' : 'dfhack.'
54-
namespace = module_name == 'dfhack' ? '' : "#{module_name.capitalize}::"
55-
output << "#{prefix}#{module_name} = {}\n\n"
56-
57-
functions = []
64+
# Functions that manipulate lua_state (usually?)
65+
unless is_module
66+
function_declaration&.scan(/^\s*{\s*([^,]+),\s*([^}]+)\s*}/) do |name, signature|
67+
next if name =~ /NULL/
5868

59-
# Guessing here a little bit.
60-
file.scan(/^static.*#{module_name}_funcs\[\][\s\S]+?};/) do |funcs|
61-
funcs.scan(/{([^}\n]+)}/) do
62-
match = Regexp.last_match(1)
63-
next if match =~ /NULL/
69+
function_name = name.gsub(/"/, '').strip
70+
signature_name = signature.gsub(/"/, '').strip
6471

65-
function_name = match.split(',')[0].strip.gsub(/"/, '')
66-
signature_name = match.split(',')[1].strip.gsub('"', '').gsub("#{module_name}_", '')
72+
module_file[/^(?:static\s)?(?:DFHACK_EXPORT\s)?(\S+).*?#{namespace}#{signature_name.gsub(
73+
/#{module_name}_/, ''
74+
)}\s?\(([^)]+)?\)/]
75+
next if Regexp.last_match
6776

68-
module_file[/^(?:static\s)?(?:DFHACK_EXPORT\s)?(\S+).*?#{namespace}#{signature_name}\s?\(([^)]+)?\)/]
77+
file[/^(?:static\s)?(?:DFHACK_EXPORT\s)?(\S+).*?#{signature_name}\s?\(([^)]+)?\)/] unless is_module
6978
next unless Regexp.last_match
7079

71-
functions << DFHackLuaDefinitions::CPP.parse_function(Regexp.last_match, module_name:, prefix:,
72-
function_name:)
80+
output << "---@field #{function_name} function\n"
7381
end
7482
end
7583

76-
cpp_module.scan(/(?:WRAP|WRAPM)\((.+)?\),?/) do |function_name,|
84+
output << "#{prefix}#{module_name} = {}\n\n"
85+
86+
# Guessing here a little bit.
87+
function_declaration&.scan(/^\s*{\s*([^,]+),\s*([^}]+)\s*}/) do |name, signature|
88+
next if name =~ /NULL/
89+
90+
function_name = name.gsub(/"/, '').strip
91+
signature_name = signature.gsub(/"/, '').strip
92+
93+
module_file[/^(?:static\s)?(?:DFHACK_EXPORT\s)?(\S+).*?#{namespace}#{signature_name.gsub(
94+
/#{module_name}_/, ''
95+
)}\s?\(([^)]+)?\)/]
96+
next unless Regexp.last_match
97+
98+
functions << DFHackLuaDefinitions::CPP.parse_function(Regexp.last_match, module_name:, prefix:,
99+
function_name:)
100+
end
101+
102+
module_declaration&.scan(/(?:WRAP|WRAPM)\((.+)?\),?/) do |function_name,|
77103
function_name = Regexp.last_match(1) if function_name =~ /,\s?(\S+)/
78104
signature = "#{namespace}#{function_name}"
79105

@@ -84,7 +110,6 @@ def parse_cpp_modules(entry_point)
84110
functions << DFHackLuaDefinitions::CPP.parse_function(Regexp.last_match, module_name:, prefix:,
85111
function_name:)
86112
end
87-
88113
output << functions.join
89114
end
90115
end

0 commit comments

Comments
 (0)