Repository navigation
Expand file tree
/
Copy pathAPI.lua
More file actions
153 lines (140 loc) · 4.44 KB
/
Copy pathAPI.lua
File metadata and controls
153 lines (140 loc) · 4.44 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---@meta
local API = {
Verbose = true,
---@package
---@module "LKT.C_Keybinds"
_Keybinds_ = {},
---@type {[string]:number}
_MapKeybinds_ = {},
---@package
_Loaded_ = false
}
---Prints Debug information to console is allowed.
---@package
---@param ... string|number
---@return boolean
function API.SYSTEM_OUT(...)
if not API.Verbose then
return false
end
print("[LKT-API]",...)
return true
end
---Helper to get the absolute directory of this current script\
---@package
function API.GetScriptDirectory()
local Str = debug.getinfo(1, "S").source:sub(2)
return Str:match("(.*/)") or "./"
end
---Starts Python Builder script.
---@return boolean
function API.Build()
API.SYSTEM_OUT("Building.")
local Prefix = string.format("cd '%s';",API.GetScriptDirectory())
local Command = Prefix .. (not API.Verbose and "python ./Build.py > /dev/null 2>&1 && echo Passed || echo Failed" or "python ./Build.py && echo Passed || echo Failed")
local Passed = os.execute(Command)
return Passed or false
end
---Tries to load compiled lua file if not found > start python builder
---@return boolean
function API.Start()
if not API._Loaded_ then
API.SYSTEM_OUT("Starting.")
local Keybinds,Status = API.RAWREAD()
if not Status then
API.SYSTEM_OUT("Compiled lua file not found or errored. starting python builder.")
local Status = API.Build()
if not Status then
API.SYSTEM_OUT("build failed.")
return false
end
API.SYSTEM_OUT("Build Finished!")
Keybinds,Status = API.RAWREAD()
end
if Status then
API._Keybinds_ = Keybinds
for Key, Value in pairs(Keybinds) do
API._MapKeybinds_[Value]=Key
end
end
API._Loaded_ = true
return true
end
API.SYSTEM_OUT("API is all-ready primed.")
return false
end
---Access the compiled lua file
---This can fail and return nothing so think it as unsafe but it will not crash.
---This returns Keybinds,Status,Error Code(if any)
---@return table,boolean,string?
function API.RAWREAD()
local ErrCode
local Status,Payload = pcall(function()
return require("LKT.C_Keybinds")
end)
if not Status then
ErrCode = Payload
Payload = {}
API.SYSTEM_OUT("Compiled lua file not found or errored.")
else
API.SYSTEM_OUT("Compiled lua file found.")
end
return Payload, Status, ErrCode
end
---Primes API for use
---@param DisableAutoload boolean?
function API.PrimeAPI(DisableAutoload)
if not API._Loaded_ and not DisableAutoload then
API.SYSTEM_OUT("API is not ready, starting API.")
print(API.Start() and "API Ready!" or "API Failed to start.")
end
end
---Gets the keybind list, if it is not compiled it will build it self.
---@param DisableAutoload boolean?
---@return {[number]:string}, boolean
function API.GetKeybinds(DisableAutoload)
API.SYSTEM_OUT("Reading Keybinds.")
API.PrimeAPI(DisableAutoload)
local Keybinds = API._Keybinds_
local Valid = (#Keybinds>0)
return Keybinds, Valid
end
---Gets the Key name from key code.
---@param Keycode number
---@param DisableAutoload boolean?
---@return string?
function API.TranslateKeycode(Keycode,DisableAutoload)
API.PrimeAPI(DisableAutoload)
return API._Keybinds_[Keycode]
end
---Gets the key code from the key name.
---@param Keyname string
---@param DisableAutoload boolean?
---@return number?
function API.TranslateKeyname(Keyname,DisableAutoload)
API.PrimeAPI(DisableAutoload)
return API._MapKeybinds_[Keyname]
end
---This deletes the cached lua keybind file, Warning after it being deleted you must connect to internet to access github to fetch it again, the builder will handle this.
function API.RemoveCache()
API.SYSTEM_OUT("Removing Cache.")
local Prefix = string.format("cd '%s';",API.GetScriptDirectory())
local Command = Prefix .. " rm -rf ./C_Keybinds.lua"
os.execute(Command)
end
---Clears Cached Keybinds and reset loaded flag
function API.Reset()
API.SYSTEM_OUT("Reseting API storage.")
API._Keybinds_ = {}
API._MapKeybinds_ = {}
API._Loaded_ = false
end
---Scope Fix
do
local LKTDir = API.GetScriptDirectory()
local ParentDir = LKTDir:match("(.*/)[^/]*/$")
if ParentDir then
package.path = package.path .. ";" .. ParentDir .. "?.lua;" .. ParentDir .."?/init.lua"
end
end
return API