-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_defs.ps1
98 lines (81 loc) · 2.72 KB
/
generate_defs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# PewPew definitions generator
# Copyright (c) 2024 PPMS Team.
# This project is licensed under MIT license.
$raw_docs = (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/pewpewlive/ppl-docs/master/raw_documentation.json").Content
$docs_obj = ConvertFrom-Json $raw_docs -AsHashtable
function ConvertTo-LuaTypes {
param (
$type_name
)
switch ($type_name) {
"FixedPoint" { return "fixedpoint" }
"Int32" { return "integer" }
"String" { return "string" }
"EntityId" { return "entity_id" }
"Boolean" { return "boolean" }
"Callback" { return "function" }
"Map" { return "table" }
Default { return "any" }
}
}
$pewpew = $docs_obj[0]
$fmath = $docs_obj[1]
$code = ""
foreach ($enum in $pewpew.enums) {
$code += ("---@enum`npewpew.{0} = {1}" -f $enum.name, "{`n")
for ($i = 0; $i -lt $enum.values.Count; $i++) {
$code += (" {0} = {1},`n" -f $enum.values[$i], $i)
}
$code += "}`n`n"
}
foreach ($function in $pewpew.functions) {
$code += ("--- {0}`n" -f $function.comment)
foreach ($parameter in $function.parameters) {
$code += ("---@param {0} {1}`n" -f $parameter.name, (ConvertTo-LuaTypes $parameter.type))
}
foreach ($return_type in $function.return_types) {
$code += ("---@return {0}`n" -f (ConvertTo-LuaTypes $return_type.type))
}
$params = ""
for ($i = 0; $i -lt $function.parameters.Count; $i++) {
if ($i -eq $function.parameters.Count - 1) {
$params += $function.parameters[$i].name
} else {
$params += ("{0}, " -f $function.parameters[$i].name)
}
}
$code += ("function pewpew.{0}({1}) end`n`n" -f $function.func_name, $params)
}
@"
-- Auto-generated type definitions from ppl-docs (https://github.com/pewpewlive/ppl-docs)
---@meta
---@alias fixedpoint number
---@alias entity_id integer
pewpew = {}
$code
"@ | Out-File -Encoding utf8 -FilePath "./library/pewpew.lua"
$code = ""
foreach ($function in $fmath.functions) {
$code += ("--- {0}`n" -f $function.comment)
foreach ($parameter in $function.parameters) {
$code += ("---@param {0} {1}`n" -f $parameter.name, (ConvertTo-LuaTypes $parameter.type))
}
foreach ($return_type in $function.return_types) {
$code += ("---@return {0}`n" -f (ConvertTo-LuaTypes $return_type.type))
}
$params = ""
for ($i = 0; $i -lt $function.parameters.Count; $i++) {
if ($i -eq $function.parameters.Count - 1) {
$params += $function.parameters[$i].name
} else {
$params += ("{0}, " -f $function.parameters[$i].name)
}
}
$code += ("function fmath.{0}({1}) end`n`n" -f $function.func_name, $params)
}
@"
-- Auto-generated type definitions from ppl-docs (https://github.com/pewpewlive/ppl-docs)
---@meta
fmath = {}
$code
"@ | Out-File -Encoding utf8 -FilePath "./library/fmath.lua"