├── .editorconfig
├── .gitignore
├── LICENSE
├── README.md
├── azure-pipelines.yaml
├── module
├── CFunctions.cpp
├── CFunctions.h
├── Common.h
├── JobManager.h
├── Main.cpp
├── Module.cpp
├── Module.h
├── Utils.cpp
├── Utils.h
├── extra
│ ├── CLuaArgument.cpp
│ ├── CLuaArgument.h
│ ├── CLuaArguments.cpp
│ └── CLuaArguments.h
├── include
│ ├── ILuaModuleManager.h
│ ├── lauxlib.h
│ ├── lua.h
│ ├── luaconf.h
│ └── lualib.h
├── lib
│ ├── lua5.1.lib
│ └── lua5.1_64.lib
├── luaimports
│ ├── luaimports.linux.cpp
│ └── luaimports.linux.h
└── premake5.lua
├── pathfind
├── AStar.cpp
├── AStar.h
├── Graph.cpp
├── Graph.h
├── GraphReader.cpp
├── GraphReader.h
├── Vector3.h
└── premake5.lua
├── premake5.lua
├── test
├── Main.cpp
├── premake5.lua
├── sa_nodes.json
└── testnodes1.json
├── utils
├── premake5
├── premake5-macos
└── premake5.exe
└── vendor
└── json
└── json.hpp
/.editorconfig:
--------------------------------------------------------------------------------
1 | # top-most EditorConfig file
2 | root = true
3 |
4 | # Unix-style newlines with a newline ending every file
5 | [*]
6 | insert_final_newline = true
7 |
8 | [*.yml]
9 | indent_style = space
10 | indent_size = 2
11 |
12 | [{*.cpp,*.c,*.h,*.hpp}]
13 | indent_style = tab
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | Build
2 | Bin
3 | obj
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Pathfind Server Module for MTA:SA
2 | [](https://travis-ci.org/eXo-MTA/ml_pathfind)
3 | [](https://ci.appveyor.com/project/Jusonex/ml-pathfind)
4 | ## Developing
5 | 1. Download and install the EditorConfig plugin for Visual Studio: [https://github.com/editorconfig/editorconfig-visualstudio#readme](https://github.com/editorconfig/editorconfig-visualstudio#readme)
6 | 2. Launch `premake.bat`
7 | 3. Open `Build/Pathfind.sln`
8 |
9 | ## Installing
10 | 1. Put the resulting `Bin/ml_gps.dll/.so` into your MTA:SA server `modules` folder.
11 | 2. Add `` (or `.so` for Linux) to your `mtaserver.conf`.
12 | 3. Start the server
13 |
14 | ## API
15 | ### Function: `loadPathGraph`
16 | ```cpp
17 | int loadPathGraph(string path)
18 | ```
19 | * __path:__ The path to the graph definition file (relative to the resource directory)
20 |
21 | Returns _graphId_ if everything went fine, _false_ otherwise.
22 |
23 | ### Function: `unloadPathGraph`
24 | ```cpp
25 | bool unloadPathGraph(int graphId)
26 | ```
27 | * __graphId:__ The id of the graph
28 |
29 | Returns _true_ if the graph has been unloaded, _false_ otherwise.
30 |
31 | ### Function: `findShortestPathBetween`
32 | ```cpp
33 | bool findShortestPathBetween(int graphId, float startX, float startY, float startZ, float endX, float endY, float endZ, function callback)
34 | ```
35 | * __graphId:__ The id of the graph
36 | * __startX, startY, startZ:__ The start position
37 | * __endX, endY, endZ:__ The end position
38 | * __callback:__ The callback function (parameters: `table nodes`)
39 |
40 | Returns _true_ if the route calculation has been scheduled successfully, _false_ otherwise.
41 |
42 | ### Function: `isGraphLoaded`
43 | ```cpp
44 | bool isGraphLoaded(int graphId)
45 | ```
46 | * __graphId:__ The id of the graph
47 |
48 | Returns _true_ if the graph is already loaded, _false_ otherwise.
49 |
50 | ### Function: `findNodeAt`
51 | ```cpp
52 | int, float, float, float findNodeAt(int graphId, float positionX, float positionY, float positionZ)
53 | ```
54 | * __graphId:__ The id of the graph
55 | * __positionX, positionY, positionZ:__ The postion where to search for a node
56 |
57 | Returns _4 integers representing nodeId and his position_ if a node was found, _false_ otherwise.
58 |
59 | ### Function: `getNodeNeighbors`
60 | ```cpp
61 | table getNodeNeighbors(int graphId, int nodeId, int depth)
62 | ```
63 | * __graphId:__ The id of the graph
64 | * __nodeId:__ The id of the start node
65 | * __depth:__ The depth of the node neighbors
66 |
67 | Returns a _table_ with all neighbors of the node, __false__ otherwise.
68 |
69 | ## Contributors
70 | * Justus H.
71 | * Stefan K.
72 |
--------------------------------------------------------------------------------
/azure-pipelines.yaml:
--------------------------------------------------------------------------------
1 | stages:
2 | - stage: Build
3 | jobs:
4 | - job: Linux
5 | pool:
6 | vmImage: 'ubuntu-16.04'
7 | steps:
8 | - script: chmod +x utils/premake5 && utils/premake5 gmake
9 | displayName: 'Generate Makefile'
10 | - script: cd Build && make -j 3 CXX=g++-7 CC=gcc-7 config=release_x64
11 | displayName: 'Build project'
12 | - script: mv $(Build.SourcesDirectory)/Bin/Release/libml_pathfind.so $(Build.SourcesDirectory)/Bin/Release/ml_pathfind.so
13 | - publish: '$(Build.SourcesDirectory)/Bin/Release/ml_pathfind.so'
14 | artifact: 'ml_pathfind_linux'
15 | - job: Windows
16 | pool:
17 | vmImage: 'windows-2019'
18 | strategy:
19 | maxParallel: 2
20 | matrix:
21 | Win32:
22 | platform: 'win32'
23 | x64:
24 | platform: 'x64'
25 | variables:
26 | solution: 'Build/pathfind.sln'
27 | buildConfiguration: 'Release'
28 | steps:
29 | - script: utils\premake5 vs2019
30 | displayName: 'Create Visual Studio 2019 Solution'
31 | - task: VSBuild@1
32 | inputs:
33 | solution: '$(solution)'
34 | configuration: '$(buildConfiguration)'
35 | platform: '$(platform)'
36 | - script: move /Y $(Build.SourcesDirectory)\Bin\Release\ml_pathfind.dll $(Build.SourcesDirectory)\Bin\Release\ml_pathfind_$(platform).dll
37 | - publish: '$(Build.SourcesDirectory)\Bin\Release\ml_pathfind_$(platform).dll'
38 | artifact: 'ml_pathfind_$(platform)'
39 |
--------------------------------------------------------------------------------
/module/CFunctions.cpp:
--------------------------------------------------------------------------------
1 | #include "CFunctions.h"
2 | #include "include/ILuaModuleManager.h"
3 | #include "extra/CLuaArguments.h"
4 |
5 | #include
6 | #include "Module.h"
7 | #include "Utils.h"
8 |
9 | #ifndef _WIN32
10 | #include
11 | #endif
12 |
13 | int CFunctions::LoadPathGraph(lua_State* luaVM)
14 | {
15 | if (lua_type(luaVM, 1) != LUA_TSTRING)
16 | {
17 | pModuleManager->ErrorPrintf("Bad argument @ loadPathGraph\n");
18 | lua_pushboolean(luaVM, false);
19 | return 1;
20 | }
21 |
22 | // Build graph path
23 | char buf[300];
24 | if (!pModuleManager->GetResourceFilePath(luaVM, lua_tostring(luaVM, 1), buf, sizeof(buf)))
25 | {
26 | pModuleManager->ErrorPrintf("File does not exist @ loadPathGraph\n");
27 | lua_pushboolean(luaVM, false);
28 | return 1;
29 | }
30 |
31 | // Check if path is valid
32 | std::string path{ buf };
33 | if (path.find("..") != std::string::npos)
34 | {
35 | pModuleManager->ErrorPrintf("Bad path @ loadPathGraph\n");
36 | lua_pushboolean(luaVM, false);
37 | return 1;
38 | }
39 |
40 | // Load graph
41 | GraphId graphId = g_Module->LoadGraph(path);
42 | lua_pushnumber(luaVM, graphId);
43 |
44 | return 1;
45 | }
46 |
47 | int CFunctions::UnloadPathGraph(lua_State* luaVM)
48 | {
49 | if (lua_type(luaVM, 1) != LUA_TNUMBER) {
50 | pModuleManager->ErrorPrintf("Bad argument @ unloadPathGraph\n");
51 | lua_pushboolean(luaVM, false);
52 | return 1;
53 | }
54 |
55 | // Check if graph has been loaded
56 | GraphId graphId = (GraphId)lua_tonumber(luaVM, 1);
57 | if (g_Module->GetGraph(graphId) == nullptr) {
58 | pModuleManager->ErrorPrintf("Bad graph @ unloadPathGraph, cannot unload\n");
59 | lua_pushboolean(luaVM, false);
60 | return 1;
61 | }
62 |
63 | g_Module->UnloadGraph(graphId);
64 | lua_pushboolean(luaVM, true);
65 | return 1;
66 | }
67 |
68 | int CFunctions::FindShortestPathBetween(lua_State* luaVM)
69 | {
70 | // bool findShortestPathBetween(int graphId, float startX, float startY, float startZ, float endX, float endY, float endZ, function callback)
71 | if (lua_type(luaVM, 1) != LUA_TNUMBER || lua_type(luaVM, 2) != LUA_TNUMBER || lua_type(luaVM, 3) != LUA_TNUMBER || lua_type(luaVM, 4) != LUA_TNUMBER ||
72 | lua_type(luaVM, 5) != LUA_TNUMBER || lua_type(luaVM, 6) != LUA_TNUMBER || lua_type(luaVM, 7) != LUA_TNUMBER ||
73 | lua_type(luaVM, 8) != LUA_TFUNCTION)
74 | {
75 | pModuleManager->ErrorPrintf("Bad argument @ findShortestPathBetween\n");
76 | lua_pushboolean(luaVM, false);
77 | return 1;
78 | }
79 |
80 | // Check if graph has been loaded
81 | GraphId graphId = (GraphId)lua_tonumber(luaVM, 1);
82 | if (g_Module->GetGraph(graphId) == nullptr)
83 | {
84 | pModuleManager->ErrorPrintf("No graph loaded @ findShortestPathBetween\n");
85 | lua_pushboolean(luaVM, false);
86 | return 1;
87 | }
88 |
89 | // Check if the positions are valid
90 | float fromX = (float)lua_tonumber(luaVM, 2), fromY = (float)lua_tonumber(luaVM, 3), fromZ = (float)lua_tonumber(luaVM, 4);
91 | float toX = (float)lua_tonumber(luaVM, 5), toY = (float)lua_tonumber(luaVM, 6), toZ = (float)lua_tonumber(luaVM, 7);
92 | if (isinf(fromX) || isinf(fromY) || isinf(fromZ) || isinf(toX) || isinf(toY) || isinf(toZ)) {
93 | pModuleManager->ErrorPrintf("Bad argument @ findShortestPathBetween\n");
94 | lua_pushboolean(luaVM, false);
95 | return 1;
96 | }
97 |
98 | // Make vectors from Lua primitives
99 | Vector3 from(fromX, fromY, fromZ);
100 | Vector3 to(toX, toY, toZ);
101 |
102 | // Save reference of the Lua callback function
103 | // See: http://lua-users.org/lists/lua-l/2008-12/msg00193.html
104 | lua_pushvalue(luaVM, -1);
105 | int funcRef = luaL_ref(luaVM, LUA_REGISTRYINDEX);
106 |
107 | g_Module->GetJobManager().PushTask([from, to, graphId]() {
108 |
109 | // Prepare information we need to pass to A*
110 | pathfind::AStar algorithm(g_Module->GetGraph(graphId), from, to);
111 |
112 | // Run A*
113 | return algorithm.CalculateShortestPath();
114 |
115 | }, [luaVM = lua_getmainstate(luaVM), funcRef](const pathfind::AStarResult& result) {
116 |
117 | // Validate LuaVM (use ResourceStart/-Stop to manage valid lua states)
118 | if (!g_Module->HasLuaVM(luaVM))
119 | return;
120 |
121 | // Push stored reference to callback function to the stack
122 | lua_rawgeti(luaVM, LUA_REGISTRYINDEX, funcRef);
123 |
124 | // Push arguments to the stack
125 | lua_newtable(luaVM);
126 | std::size_t index = 0;
127 |
128 | for (pathfind::GraphNode* node : result)
129 | {
130 | // Set table index
131 | lua_pushnumber(luaVM, ++index);
132 |
133 | // Create a table containing the vector components
134 | lua_newtable(luaVM);
135 |
136 | lua_pushnumber(luaVM, 1);
137 | lua_pushnumber(luaVM, node->position.GetX());
138 | lua_settable(luaVM, -3);
139 |
140 | lua_pushnumber(luaVM, 2);
141 | lua_pushnumber(luaVM, node->position.GetY());
142 | lua_settable(luaVM, -3);
143 |
144 | lua_pushnumber(luaVM, 3);
145 | lua_pushnumber(luaVM, node->position.GetZ());
146 | lua_settable(luaVM, -3);
147 |
148 | // Add vector table to result table
149 | lua_settable(luaVM, -3);
150 | }
151 |
152 | // Finally, call the function
153 | int err = lua_pcall(luaVM, 1, 0, 0);
154 | if (err != 0)
155 | pModuleManager->ErrorPrintf("%s\n", lua_tostring(luaVM, -1));
156 |
157 | });
158 |
159 | lua_pushboolean(luaVM, true);
160 | return 1;
161 | }
162 |
163 | int CFunctions::IsGraphLoaded(lua_State* luaVM)
164 | {
165 | if (lua_type(luaVM, 1) != LUA_TNUMBER) {
166 | pModuleManager->ErrorPrintf("Bad argument @ unloadPathGraph\n");
167 | lua_pushboolean(luaVM, false);
168 | return 1;
169 | }
170 |
171 | GraphId graphId = (GraphId) lua_tonumber(luaVM, 1);
172 | lua_pushboolean(luaVM, g_Module->GetGraph(graphId) != nullptr);
173 | return 1;
174 | }
175 |
176 | int CFunctions::FindNodeAt(lua_State* luaVM)
177 | {
178 | if (lua_type(luaVM, 1) != LUA_TNUMBER || lua_type(luaVM, 2) != LUA_TNUMBER || lua_type(luaVM, 3) != LUA_TNUMBER || lua_type(luaVM, 4) != LUA_TNUMBER)
179 | {
180 | pModuleManager->ErrorPrintf("Bad argument @ findNodeAt\n");
181 | lua_pushboolean(luaVM, false);
182 | return 1;
183 | }
184 |
185 | GraphId graphId = (GraphId)lua_tonumber(luaVM, 1);
186 | if (g_Module->GetGraph(graphId) == nullptr) {
187 | pModuleManager->ErrorPrintf("Bad graph @ findNodeAt\n");
188 | lua_pushboolean(luaVM, false);
189 | return 1;
190 | }
191 |
192 | float fromX = (float)lua_tonumber(luaVM, 2), fromY = (float)lua_tonumber(luaVM, 3), fromZ = (float)lua_tonumber(luaVM, 4);
193 | if (isinf(fromX) || isinf(fromY) || isinf(fromZ)) {
194 | pModuleManager->ErrorPrintf("Bad argument @ findNodeAt\n");
195 | lua_pushboolean(luaVM, false);
196 | return 1;
197 | }
198 |
199 | Vector3 position(fromX, fromY, fromZ);
200 | pathfind::GraphNode* node = g_Module->GetGraph(graphId)->FindClosestNodeTo(position);
201 | if (!node) {
202 | lua_pushboolean(luaVM, false);
203 | return 1;
204 | }
205 |
206 | lua_pushnumber(luaVM, node->id);
207 | lua_pushnumber(luaVM, node->position.GetX());
208 | lua_pushnumber(luaVM, node->position.GetY());
209 | lua_pushnumber(luaVM, node->position.GetZ());
210 |
211 | return 4;
212 | }
213 |
214 | int CFunctions::GetNodeNeighbors(lua_State* luaVM)
215 | {
216 | if (lua_type(luaVM, 1) != LUA_TNUMBER || lua_type(luaVM, 2) != LUA_TNUMBER || lua_type(luaVM, 3) != LUA_TNUMBER)
217 | {
218 | pModuleManager->ErrorPrintf("Bad argument @ getNodeNeighbors\n");
219 | lua_pushboolean(luaVM, false);
220 | return 1;
221 | }
222 |
223 | pathfind::Graph* graph = g_Module->GetGraph((GraphId)lua_tonumber(luaVM, 1));
224 | if (graph == nullptr) {
225 | pModuleManager->ErrorPrintf("Bad graph @ findNodeAt\n");
226 | lua_pushboolean(luaVM, false);
227 | return 1;
228 | }
229 |
230 | int nodeId = (int)lua_tonumber(luaVM, 2);
231 | if (nodeId < 0) {
232 | pModuleManager->ErrorPrintf("Invalid NodeId @ findNodeAt\n");
233 | lua_pushboolean(luaVM, false);
234 | return 1;
235 | }
236 |
237 | pathfind::GraphNode* node = graph->GetNode(static_cast(nodeId));
238 | if (node == nullptr) {
239 | pModuleManager->ErrorPrintf("Invalid NodeId @ findNodeAt\n");
240 | lua_pushboolean(luaVM, false);
241 | return 1;
242 | }
243 |
244 | int depth = (int)lua_tonumber(luaVM, 3);
245 | if (depth > Utils::MAX_NODE_DEPTH) {
246 | pModuleManager->ErrorPrintf("Depth to high, would take to long @ findNodeAt\n");
247 | lua_pushboolean(luaVM, false);
248 | return 1;
249 | }
250 |
251 | lua_newtable(luaVM);
252 | Utils::GetNodeNeighbors(luaVM, node, depth);
253 |
254 | return 1;
255 | }
256 |
--------------------------------------------------------------------------------
/module/CFunctions.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | class ILuaModuleManager10;
4 | struct lua_State;
5 |
6 | extern ILuaModuleManager10* pModuleManager;
7 |
8 |
9 | class CFunctions
10 | {
11 | public:
12 | static int LoadPathGraph(lua_State* luaVM);
13 | static int UnloadPathGraph(lua_State* luaVM);
14 | static int FindShortestPathBetween(lua_State* luaVM);
15 | static int IsGraphLoaded(lua_State* luaVM);
16 | static int FindNodeAt(lua_State* luaVM);
17 | static int GetNodeNeighbors(lua_State* luaVM);
18 | };
19 |
--------------------------------------------------------------------------------
/module/Common.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/Common.h
--------------------------------------------------------------------------------
/module/JobManager.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | template
10 | class JobManager
11 | {
12 | public:
13 | using Task = std::function;
14 | using TaskCompleteCallback = std::function;
15 |
16 | JobManager(std::size_t numWorkers) : _numWorkers(numWorkers)
17 | {
18 | }
19 |
20 | void Start()
21 | {
22 | _running = true;
23 |
24 | for (std::size_t i = 0; i < _numWorkers; ++i)
25 | {
26 | _workers.emplace_back(&JobManager::RunWorker, this);
27 | }
28 | }
29 |
30 | void Stop()
31 | {
32 | _running = false;
33 |
34 | // Wait for threads to end
35 | for (auto& worker : _workers)
36 | {
37 | if (worker.joinable())
38 | worker.join();
39 | }
40 | }
41 |
42 | void PushTask(Task task, TaskCompleteCallback completeCallback)
43 | {
44 | std::lock_guard lock{ _mutex };
45 |
46 | _tasks.push({ task, completeCallback });
47 | }
48 |
49 | void RunWorker()
50 | {
51 | while (_running)
52 | {
53 | // Wait if there's no task for us
54 | _mutex.lock();
55 | if (_tasks.empty())
56 | {
57 | _mutex.unlock();
58 | std::this_thread::sleep_for(std::chrono::milliseconds(100));
59 | continue;
60 | }
61 |
62 | // Get next task
63 | std::pair task = _tasks.front();
64 | _tasks.pop();
65 | _mutex.unlock();
66 |
67 | // Run task
68 | TaskResult result = task.first();
69 |
70 | // Put result into completed tasks list
71 | std::lock_guard lock{ _mutex };
72 | _completedTasks.push_back({ result, task.second });
73 | }
74 | }
75 |
76 | void SpreadResults()
77 | {
78 | std::lock_guard lock{ _mutex };
79 |
80 | if (_completedTasks.empty())
81 | return;
82 |
83 | for (auto& task : _completedTasks)
84 | {
85 | task.second(task.first);
86 | }
87 |
88 | _completedTasks.clear();
89 | }
90 |
91 | private:
92 | std::vector _workers;
93 | std::size_t _numWorkers;
94 | bool _running = false;
95 |
96 | std::queue> _tasks;
97 | std::list> _completedTasks;
98 |
99 | std::recursive_mutex _mutex;
100 | };
101 |
--------------------------------------------------------------------------------
/module/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "Common.h"
2 | #include "CFunctions.h"
3 | #include "include/ILuaModuleManager.h"
4 | #include
5 | #include "Module.h"
6 |
7 | #ifndef WIN32
8 | #include "luaimports/luaimports.linux.h"
9 | #endif
10 |
11 | ILuaModuleManager10* pModuleManager = nullptr;
12 |
13 | // Initialisation function (module entrypoint)
14 | MTAEXPORT bool InitModule(ILuaModuleManager10* pManager, char* szModuleName, char* szAuthor, float* fVersion)
15 | {
16 | #ifndef WIN32
17 | ImportLua();
18 | #endif
19 |
20 | pModuleManager = pManager;
21 |
22 | // Set the module info
23 | std::memcpy(szModuleName, "Pathfind Module", MAX_INFO_LENGTH);
24 | std::memcpy(szAuthor, "Jusonex, StiviK", MAX_INFO_LENGTH);
25 | *fVersion = 1.0f;
26 |
27 | // Load module
28 | g_Module = new Module(pManager);
29 | g_Module->Start();
30 |
31 | return true;
32 | }
33 |
34 | MTAEXPORT void RegisterFunctions(lua_State* luaVM)
35 | {
36 | if (!pModuleManager || !luaVM)
37 | return;
38 |
39 | // Add lua vm to states list (to check validility)
40 | g_Module->AddLuaVM(luaVM);
41 |
42 | // Register functions
43 | pModuleManager->RegisterFunction(luaVM, "loadPathGraph", &CFunctions::LoadPathGraph);
44 | pModuleManager->RegisterFunction(luaVM, "unloadPathGraph", &CFunctions::UnloadPathGraph);
45 | pModuleManager->RegisterFunction(luaVM, "findShortestPathBetween", &CFunctions::FindShortestPathBetween);
46 | pModuleManager->RegisterFunction(luaVM, "isGraphLoaded", &CFunctions::IsGraphLoaded);
47 | pModuleManager->RegisterFunction(luaVM, "findNodeAt", &CFunctions::FindNodeAt);
48 | pModuleManager->RegisterFunction(luaVM, "getNodeNeighbors", &CFunctions::GetNodeNeighbors);
49 | }
50 |
51 | MTAEXPORT bool DoPulse()
52 | {
53 | g_Module->Process();
54 |
55 | return true;
56 | }
57 |
58 | MTAEXPORT bool ShutdownModule()
59 | {
60 | // Unload module
61 | delete g_Module;
62 |
63 | return true;
64 | }
65 |
66 | MTAEXPORT bool ResourceStopping(lua_State* luaVM)
67 | {
68 | // Invalidate lua vm by removing it from the valid list
69 | g_Module->RemoveLuaVM(luaVM);
70 |
71 | return true;
72 | }
73 |
74 | MTAEXPORT bool ResourceStopped(lua_State* luaVM)
75 | {
76 | return true;
77 | }
78 |
--------------------------------------------------------------------------------
/module/Module.cpp:
--------------------------------------------------------------------------------
1 | #include "Module.h"
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 | #include "include/ILuaModuleManager.h"
8 |
9 | Module* g_Module = nullptr;
10 |
11 | constexpr std::size_t kNumWorkers = 2;
12 |
13 | Module::Module(ILuaModuleManager* manager) : _moduleManager(manager), _jobManager(kNumWorkers), _lastGraphId(0)
14 | {
15 | }
16 |
17 | Module::~Module()
18 | {
19 | // Shutdown job manager threads
20 | _jobManager.Stop();
21 | }
22 |
23 | void Module::Start()
24 | {
25 | // Start job manager worker threads
26 | _jobManager.Start();
27 | }
28 |
29 | void Module::Process()
30 | {
31 | // Call complete callbacks on main thread
32 | _jobManager.SpreadResults();
33 | }
34 |
35 | GraphId Module::LoadGraph(const std::string& path)
36 | {
37 | // Make sure job manager is not running
38 | _jobManager.Stop();
39 |
40 | // Load graph
41 | auto startTime = std::chrono::system_clock::now();
42 | pathfind::GraphReader graphReader(path);
43 |
44 | // Find free graphId and add it
45 | AddGraph(++_lastGraphId, graphReader.Read());
46 |
47 | _moduleManager->Printf("Loaded graph! (Took %lldms)\n", std::chrono::duration_cast(std::chrono::system_clock::now() - startTime).count());
48 |
49 | // Start job manager again
50 | _jobManager.Start();
51 |
52 | return _lastGraphId;
53 | }
54 |
55 | void Module::UnloadGraph(GraphId graphId)
56 | {
57 | // Make sure job manager is not running
58 | _jobManager.Stop();
59 |
60 | // Delete graph reference
61 | RemoveGraph(graphId);
62 |
63 | // Start job manager again
64 | _jobManager.Start();
65 | }
--------------------------------------------------------------------------------
/module/Module.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 | #include "JobManager.h"
8 |
9 | struct lua_State;
10 | class ILuaModuleManager;
11 | using GraphId = size_t;
12 |
13 | class Module
14 | {
15 | public:
16 | Module(ILuaModuleManager* manager);
17 | ~Module();
18 |
19 | void Start();
20 | void Process();
21 |
22 | GraphId LoadGraph(const std::string& path);
23 | void UnloadGraph(GraphId graphId);
24 |
25 | inline void AddLuaVM(lua_State* luaVM) { _luaStates.insert(luaVM); }
26 | inline void RemoveLuaVM(lua_State* luaVM) { _luaStates.erase(luaVM); }
27 | inline bool HasLuaVM(lua_State* luaVM) { return _luaStates.find(luaVM) != _luaStates.end(); }
28 | inline pathfind::Graph* GetGraph(GraphId graphId) {
29 | if (_graphs.find(graphId) != _graphs.end()) {
30 | return _graphs.at(graphId).get();
31 | }
32 |
33 | return nullptr;
34 | }
35 | inline void AddGraph(GraphId graphId, std::unique_ptr graph) { _graphs[graphId] = std::move(graph); }
36 | inline void RemoveGraph(GraphId graphId) { _graphs.erase(graphId); }
37 |
38 | inline JobManager& GetJobManager() { return _jobManager; }
39 |
40 | private:
41 | ILuaModuleManager* _moduleManager;
42 | JobManager _jobManager;
43 | std::unordered_map> _graphs;
44 | std::unordered_set _luaStates;
45 | GraphId _lastGraphId;
46 | };
47 |
48 | extern Module* g_Module;
49 |
--------------------------------------------------------------------------------
/module/Utils.cpp:
--------------------------------------------------------------------------------
1 | #include "Utils.h"
2 |
3 | void Utils::GetNodeNeighbors(lua_State* luaVM, pathfind::GraphNode* startNode, int depth)
4 | {
5 | std::size_t index = 1;
6 |
7 | for (const auto& pair : startNode->edges)
8 | {
9 | pathfind::GraphNode* edgeNode = pair.first;
10 |
11 | lua_pushnumber(luaVM, index++);
12 | lua_newtable(luaVM);
13 | lua_pushnumber(luaVM, 1);
14 | lua_pushnumber(luaVM, edgeNode->id);
15 | lua_settable(luaVM, -3);
16 |
17 | lua_pushnumber(luaVM, 2);
18 | lua_pushnumber(luaVM, edgeNode->position.GetX());
19 | lua_settable(luaVM, -3);
20 |
21 | lua_pushnumber(luaVM, 3);
22 | lua_pushnumber(luaVM, edgeNode->position.GetY());
23 | lua_settable(luaVM, -3);
24 |
25 | lua_pushnumber(luaVM, 4);
26 | lua_pushnumber(luaVM, edgeNode->position.GetZ());
27 | lua_settable(luaVM, -3);
28 |
29 | lua_pushnumber(luaVM, 5);
30 | if (depth > 0) {
31 | lua_newtable(luaVM);
32 | GetNodeNeighbors(luaVM, edgeNode, depth - 1);
33 | }
34 | else {
35 | lua_pushboolean(luaVM, false);
36 | }
37 | lua_settable(luaVM, -3);
38 |
39 | lua_settable(luaVM, -3);
40 | }
41 | }
--------------------------------------------------------------------------------
/module/Utils.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "extra/CLuaArguments.h"
3 | #include
4 |
5 | class Utils
6 | {
7 |
8 | public:
9 | #ifdef DEBUG
10 | static const int MAX_NODE_DEPTH = std::numeric_limits::max();
11 | #else
12 | static const int MAX_NODE_DEPTH = 15;
13 | #endif
14 |
15 | static void GetNodeNeighbors(lua_State* luaVM, pathfind::GraphNode* node, int depth);
16 |
17 | };
--------------------------------------------------------------------------------
/module/extra/CLuaArgument.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/extra/CLuaArgument.cpp
--------------------------------------------------------------------------------
/module/extra/CLuaArgument.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/extra/CLuaArgument.h
--------------------------------------------------------------------------------
/module/extra/CLuaArguments.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/extra/CLuaArguments.cpp
--------------------------------------------------------------------------------
/module/extra/CLuaArguments.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/extra/CLuaArguments.h
--------------------------------------------------------------------------------
/module/include/ILuaModuleManager.h:
--------------------------------------------------------------------------------
1 | /*****************************************************************************
2 | *
3 | * PROJECT: Multi Theft Auto v1.0
4 | * LICENSE: See LICENSE in the top level directory
5 | * FILE: publicsdk/include/ILuaModuleManager.h
6 | * PURPOSE: Lua dynamic module interface
7 | *
8 | * Multi Theft Auto is available from http://www.multitheftauto.com/
9 | *
10 | *****************************************************************************/
11 |
12 | // INTERFACE for Lua dynamic modules
13 |
14 | #ifndef __ILUAMODULEMANAGER_H
15 | #define __ILUAMODULEMANAGER_H
16 |
17 | #define MAX_INFO_LENGTH 128
18 |
19 | extern "C"
20 | {
21 | #include "lua.h"
22 | #include "lualib.h"
23 | #include "lauxlib.h"
24 | }
25 | #include
26 |
27 | #ifndef __CChecksum_H
28 | class CChecksum
29 | {
30 | public:
31 | unsigned long ulCRC;
32 | unsigned char mD5 [16];
33 | };
34 | #endif
35 |
36 | /* Interface for modules until DP2.3 */
37 | class ILuaModuleManager
38 | {
39 | public:
40 | virtual void ErrorPrintf ( const char* szFormat, ... ) = 0;
41 | virtual void DebugPrintf ( lua_State * luaVM, const char* szFormat, ... ) = 0;
42 | virtual void Printf ( const char* szFormat, ... ) = 0;
43 |
44 | virtual bool RegisterFunction ( lua_State * luaVM, const char *szFunctionName, lua_CFunction Func ) = 0;
45 | virtual bool GetResourceName ( lua_State * luaVM, std::string &strName ) = 0;
46 | virtual CChecksum GetResourceMetaChecksum ( lua_State * luaVM ) = 0;
47 | virtual CChecksum GetResourceFileChecksum ( lua_State * luaVM, const char* szFile ) = 0;
48 | };
49 |
50 | /* Interface for modules until 1.0 */
51 | class ILuaModuleManager10 : public ILuaModuleManager
52 | {
53 | public:
54 | virtual unsigned long GetVersion ( ) = 0;
55 | virtual const char* GetVersionString ( ) = 0;
56 | virtual const char* GetVersionName ( ) = 0;
57 | virtual unsigned long GetNetcodeVersion ( ) = 0;
58 | virtual const char* GetOperatingSystemName ( ) = 0;
59 |
60 | virtual lua_State* GetResourceFromName ( const char* szResourceName ) = 0;
61 | virtual bool GetResourceName ( lua_State* luaVM, char* szName, size_t length ) = 0;
62 | virtual bool GetResourceFilePath ( lua_State* luaVM, const char* fileName, char* path, size_t length ) = 0;
63 | };
64 |
65 | #endif
66 |
--------------------------------------------------------------------------------
/module/include/lauxlib.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lauxlib.h,v 1.2 2007/08/14 19:23:35 toady Exp $
3 | ** Auxiliary functions for building Lua libraries
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lauxlib_h
9 | #define lauxlib_h
10 |
11 |
12 | #include
13 | #include
14 |
15 | #include "lua.h"
16 |
17 |
18 | #if defined(LUA_COMPAT_GETN)
19 | LUALIB_API int (luaL_getn) (lua_State *L, int t);
20 | LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
21 | #else
22 | #define luaL_getn(L,i) ((int)lua_objlen(L, i))
23 | #define luaL_setn(L,i,j) ((void)0) /* no op! */
24 | #endif
25 |
26 | #if defined(LUA_COMPAT_OPENLIB)
27 | #define luaI_openlib luaL_openlib
28 | #endif
29 |
30 |
31 | /* extra error code for `luaL_load' */
32 | #define LUA_ERRFILE (LUA_ERRERR+1)
33 |
34 |
35 | typedef struct luaL_Reg {
36 | const char *name;
37 | lua_CFunction func;
38 | } luaL_Reg;
39 |
40 |
41 |
42 | LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
43 | const luaL_Reg *l, int nup);
44 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
45 | const luaL_Reg *l);
46 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
47 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
48 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
49 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
50 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
51 | size_t *l);
52 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
53 | const char *def, size_t *l);
54 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
55 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
56 |
57 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
58 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
59 | lua_Integer def);
60 |
61 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
62 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
63 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
64 |
65 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
66 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
67 |
68 | LUALIB_API void (luaL_where) (lua_State *L, int lvl);
69 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
70 |
71 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
72 | const char *const lst[]);
73 |
74 | LUALIB_API int (luaL_ref) (lua_State *L, int t);
75 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
76 |
77 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
78 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
79 | const char *name);
80 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
81 |
82 | LUALIB_API lua_State *(luaL_newstate) (void);
83 |
84 |
85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
86 | const char *r);
87 |
88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
89 | const char *fname, int szhint);
90 |
91 |
92 |
93 |
94 | /*
95 | ** ===============================================================
96 | ** some useful macros
97 | ** ===============================================================
98 | */
99 |
100 | #define luaL_argcheck(L, cond,numarg,extramsg) \
101 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
102 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
103 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
104 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
105 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
106 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
107 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
108 |
109 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
110 |
111 | #define luaL_dofile(L, fn) \
112 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
113 |
114 | #define luaL_dostring(L, s) \
115 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
116 |
117 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
118 |
119 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
120 |
121 | /*
122 | ** {======================================================
123 | ** Generic Buffer manipulation
124 | ** =======================================================
125 | */
126 |
127 |
128 |
129 | typedef struct luaL_Buffer {
130 | char *p; /* current position in buffer */
131 | int lvl; /* number of strings in the stack (level) */
132 | lua_State *L;
133 | char buffer[LUAL_BUFFERSIZE];
134 | } luaL_Buffer;
135 |
136 | #define luaL_addchar(B,c) \
137 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
138 | (*(B)->p++ = (char)(c)))
139 |
140 | /* compatibility only */
141 | #define luaL_putchar(B,c) luaL_addchar(B,c)
142 |
143 | #define luaL_addsize(B,n) ((B)->p += (n))
144 |
145 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
146 | LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
147 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
148 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
149 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
150 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
151 |
152 |
153 | /* }====================================================== */
154 |
155 |
156 | /* compatibility with ref system */
157 |
158 | /* pre-defined references */
159 | #define LUA_NOREF (-2)
160 | #define LUA_REFNIL (-1)
161 |
162 | #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
163 | (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
164 |
165 | #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
166 |
167 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
168 |
169 |
170 | #define luaL_reg luaL_Reg
171 |
172 | #endif
173 |
174 |
175 |
--------------------------------------------------------------------------------
/module/include/lua.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $
3 | ** Lua - An Extensible Extension Language
4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5 | ** See Copyright Notice at the end of this file
6 | */
7 |
8 |
9 | #ifndef lua_h
10 | #define lua_h
11 |
12 | #include
13 | #include
14 |
15 |
16 | #include "luaconf.h"
17 |
18 |
19 | #define LUA_VERSION "Lua 5.1"
20 | #define LUA_RELEASE "Lua 5.1.5"
21 | #define LUA_VERSION_NUM 501
22 | #define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
23 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
24 |
25 |
26 | /* mark for precompiled code (`Lua') */
27 | #define LUA_SIGNATURE "\033Lua"
28 |
29 | /* option for multiple returns in `lua_pcall' and `lua_call' */
30 | #define LUA_MULTRET (-1)
31 |
32 |
33 | /*
34 | ** pseudo-indices
35 | */
36 | #define LUA_REGISTRYINDEX (-10000)
37 | #define LUA_ENVIRONINDEX (-10001)
38 | #define LUA_GLOBALSINDEX (-10002)
39 | #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
40 |
41 |
42 | /* thread status; 0 is OK */
43 | #define LUA_YIELD 1
44 | #define LUA_ERRRUN 2
45 | #define LUA_ERRSYNTAX 3
46 | #define LUA_ERRMEM 4
47 | #define LUA_ERRERR 5
48 |
49 |
50 | typedef struct lua_State lua_State;
51 |
52 | typedef int (*lua_CFunction) (lua_State *L);
53 |
54 | /*
55 | ** MTA Specific stuff written by Oli for pre C Function call hooking
56 | */
57 | typedef int (*lua_PreCallHook) ( lua_CFunction f, lua_State* L );
58 | LUA_API void lua_registerPreCallHook ( lua_PreCallHook f );
59 | typedef void (*lua_PostCallHook) ( lua_CFunction f, lua_State* L );
60 | LUA_API void lua_registerPostCallHook ( lua_PostCallHook f );
61 |
62 | // MTA Specific
63 | typedef int (*lua_UndumpHook) ( const char* p, size_t n );
64 | LUA_API void lua_registerUndumpHook ( lua_UndumpHook f );
65 |
66 | /*
67 | ** functions that read/write blocks when loading/dumping Lua chunks
68 | */
69 | typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
70 |
71 | typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
72 |
73 |
74 | /*
75 | ** prototype for memory-allocation functions
76 | */
77 | typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
78 |
79 |
80 | /*
81 | ** basic types
82 | */
83 | #define LUA_TNONE (-1)
84 |
85 | #define LUA_TNIL 0
86 | #define LUA_TBOOLEAN 1
87 | #define LUA_TLIGHTUSERDATA 2
88 | #define LUA_TNUMBER 3
89 | #define LUA_TSTRING 4
90 | #define LUA_TTABLE 5
91 | #define LUA_TFUNCTION 6
92 | #define LUA_TUSERDATA 7
93 | #define LUA_TTHREAD 8
94 |
95 |
96 |
97 | /* minimum Lua stack available to a C function */
98 | #define LUA_MINSTACK 50 // MTA change. Was 20
99 |
100 |
101 | /*
102 | ** generic extra include file
103 | */
104 | #if defined(LUA_USER_H)
105 | #include LUA_USER_H
106 | #endif
107 |
108 |
109 | /* type of numbers in Lua */
110 | typedef LUA_NUMBER lua_Number;
111 |
112 |
113 | /* type for integer functions */
114 | typedef LUA_INTEGER lua_Integer;
115 |
116 |
117 |
118 | /*
119 | ** state manipulation
120 | */
121 | LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
122 | LUA_API void (lua_close) (lua_State *L);
123 | LUA_API lua_State *(lua_newthread) (lua_State *L);
124 |
125 | LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
126 |
127 | // MTA Specific functions.
128 | // ChrML: Added function to get the main state from a lua state that is a coroutine
129 | LUA_API lua_State* (lua_getmainstate) (lua_State* L);
130 |
131 | /*
132 | ** basic stack manipulation
133 | */
134 | LUA_API int (lua_gettop) (lua_State *L);
135 | LUA_API void (lua_settop) (lua_State *L, int idx);
136 | LUA_API void (lua_pushvalue) (lua_State *L, int idx);
137 | LUA_API void (lua_remove) (lua_State *L, int idx);
138 | LUA_API void (lua_insert) (lua_State *L, int idx);
139 | LUA_API void (lua_replace) (lua_State *L, int idx);
140 | LUA_API int (lua_checkstack) (lua_State *L, int sz);
141 | LUA_API int (lua_getstackgap) (lua_State *L); // MTA addition
142 |
143 | LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
144 |
145 |
146 | /*
147 | ** access functions (stack -> C)
148 | */
149 |
150 | LUA_API int (lua_isnumber) (lua_State *L, int idx);
151 | LUA_API int (lua_isstring) (lua_State *L, int idx);
152 | LUA_API int (lua_iscfunction) (lua_State *L, int idx);
153 | LUA_API int (lua_isuserdata) (lua_State *L, int idx);
154 | LUA_API int (lua_type) (lua_State *L, int idx);
155 | LUA_API const char *(lua_typename) (lua_State *L, int tp);
156 |
157 | LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
158 | LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
159 | LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
160 |
161 | LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
162 | LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
163 | LUA_API lua_Integer (lua_tointegerW) (lua_State *L, int idx); // MTA Specific
164 | LUA_API int (lua_toboolean) (lua_State *L, int idx);
165 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
166 | LUA_API size_t (lua_objlen) (lua_State *L, int idx);
167 | LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
168 | LUA_API void *(lua_touserdata) (lua_State *L, int idx);
169 | LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
170 | LUA_API const void *(lua_topointer) (lua_State *L, int idx);
171 |
172 |
173 | /*
174 | ** push functions (C -> stack)
175 | */
176 | LUA_API void (lua_pushnil) (lua_State *L);
177 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
178 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
179 | LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l);
180 | LUA_API void (lua_pushstring) (lua_State *L, const char *s);
181 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
182 | va_list argp);
183 | LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
184 | LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
185 | LUA_API void (lua_pushboolean) (lua_State *L, int b);
186 | LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
187 | LUA_API int (lua_pushthread) (lua_State *L);
188 |
189 |
190 | /*
191 | ** get functions (Lua -> stack)
192 | */
193 | LUA_API void (lua_gettable) (lua_State *L, int idx);
194 | LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
195 | LUA_API void (lua_rawget) (lua_State *L, int idx);
196 | LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
197 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
198 | LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
199 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
200 | LUA_API void (lua_getfenv) (lua_State *L, int idx);
201 |
202 |
203 | /*
204 | ** set functions (stack -> Lua)
205 | */
206 | LUA_API void (lua_settable) (lua_State *L, int idx);
207 | LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
208 | LUA_API void (lua_rawset) (lua_State *L, int idx);
209 | LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
210 | LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
211 | LUA_API int (lua_setfenv) (lua_State *L, int idx);
212 |
213 |
214 | /*
215 | ** `load' and `call' functions (load and run Lua code)
216 | */
217 | LUA_API void (lua_call) (lua_State *L, int nargs, int nresults);
218 | LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
219 | LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
220 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
221 | const char *chunkname);
222 |
223 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
224 |
225 |
226 | /*
227 | ** coroutine functions
228 | */
229 | LUA_API int (lua_yield) (lua_State *L, int nresults);
230 | LUA_API int (lua_resume) (lua_State *L, int narg);
231 | LUA_API int (lua_status) (lua_State *L);
232 |
233 | /*
234 | ** garbage-collection function and options
235 | */
236 |
237 | #define LUA_GCSTOP 0
238 | #define LUA_GCRESTART 1
239 | #define LUA_GCCOLLECT 2
240 | #define LUA_GCCOUNT 3
241 | #define LUA_GCCOUNTB 4
242 | #define LUA_GCSTEP 5
243 | #define LUA_GCSETPAUSE 6
244 | #define LUA_GCSETSTEPMUL 7
245 |
246 | LUA_API int (lua_gc) (lua_State *L, int what, int data);
247 |
248 |
249 | /*
250 | ** miscellaneous functions
251 | */
252 |
253 | LUA_API int (lua_error) (lua_State *L);
254 |
255 | LUA_API int (lua_next) (lua_State *L, int idx);
256 |
257 | LUA_API void (lua_concat) (lua_State *L, int n);
258 |
259 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
260 | LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
261 |
262 |
263 |
264 | /*
265 | ** ===============================================================
266 | ** some useful macros
267 | ** ===============================================================
268 | */
269 |
270 | #define lua_pop(L,n) lua_settop(L, -(n)-1)
271 |
272 | #define lua_newtable(L) lua_createtable(L, 0, 0)
273 |
274 | #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
275 |
276 | #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
277 |
278 | #define lua_strlen(L,i) lua_objlen(L, (i))
279 |
280 | #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
281 | #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
282 | #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
283 | #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
284 | #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
285 | #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
286 | #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
287 | #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
288 |
289 | #define lua_pushliteral(L, s) \
290 | lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
291 |
292 | #define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
293 | #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
294 |
295 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
296 |
297 |
298 |
299 | /*
300 | ** compatibility macros and functions
301 | */
302 |
303 | #define lua_open() luaL_newstate()
304 |
305 | #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
306 |
307 | #define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0)
308 |
309 | #define lua_Chunkreader lua_Reader
310 | #define lua_Chunkwriter lua_Writer
311 |
312 |
313 | /* hack */
314 | LUA_API void lua_setlevel (lua_State *from, lua_State *to);
315 |
316 |
317 | /*
318 | ** {======================================================================
319 | ** Debug API
320 | ** =======================================================================
321 | */
322 |
323 |
324 | /*
325 | ** Event codes
326 | */
327 | #define LUA_HOOKCALL 0
328 | #define LUA_HOOKRET 1
329 | #define LUA_HOOKLINE 2
330 | #define LUA_HOOKCOUNT 3
331 | #define LUA_HOOKTAILRET 4
332 |
333 |
334 | /*
335 | ** Event masks
336 | */
337 | #define LUA_MASKCALL (1 << LUA_HOOKCALL)
338 | #define LUA_MASKRET (1 << LUA_HOOKRET)
339 | #define LUA_MASKLINE (1 << LUA_HOOKLINE)
340 | #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
341 |
342 | typedef struct lua_Debug lua_Debug; /* activation record */
343 |
344 |
345 | /* Functions to be called by the debuger in specific events */
346 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
347 |
348 |
349 | LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
350 | LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
351 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
352 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
353 | LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
354 | LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);
355 |
356 | LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
357 | LUA_API lua_Hook lua_gethook (lua_State *L);
358 | LUA_API int lua_gethookmask (lua_State *L);
359 | LUA_API int lua_gethookcount (lua_State *L);
360 |
361 |
362 | struct lua_Debug {
363 | int event;
364 | const char *name; /* (n) */
365 | const char *namewhat; /* (n) `global', `local', `field', `method' */
366 | const char *what; /* (S) `Lua', `C', `main', `tail' */
367 | const char *source; /* (S) */
368 | int currentline; /* (l) */
369 | int nups; /* (u) number of upvalues */
370 | int linedefined; /* (S) */
371 | int lastlinedefined; /* (S) */
372 | char short_src[LUA_IDSIZE]; /* (S) */
373 | /* private part */
374 | int i_ci; /* active function */
375 | };
376 |
377 | /* }====================================================================== */
378 |
379 |
380 | /******************************************************************************
381 | * Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved.
382 | *
383 | * Permission is hereby granted, free of charge, to any person obtaining
384 | * a copy of this software and associated documentation files (the
385 | * "Software"), to deal in the Software without restriction, including
386 | * without limitation the rights to use, copy, modify, merge, publish,
387 | * distribute, sublicense, and/or sell copies of the Software, and to
388 | * permit persons to whom the Software is furnished to do so, subject to
389 | * the following conditions:
390 | *
391 | * The above copyright notice and this permission notice shall be
392 | * included in all copies or substantial portions of the Software.
393 | *
394 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
395 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
396 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
397 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
398 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
399 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
400 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
401 | ******************************************************************************/
402 |
403 |
404 | #endif
405 |
--------------------------------------------------------------------------------
/module/include/luaconf.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $
3 | ** Configuration file for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lconfig_h
9 | #define lconfig_h
10 |
11 | #include
12 | #include
13 |
14 |
15 | /*
16 | ** ==================================================================
17 | ** Search for "@@" to find all configurable definitions.
18 | ** ===================================================================
19 | */
20 |
21 |
22 | /*
23 | @@ LUA_ANSI controls the use of non-ansi features.
24 | ** CHANGE it (define it) if you want Lua to avoid the use of any
25 | ** non-ansi feature or library.
26 | */
27 | #if defined(__STRICT_ANSI__)
28 | #define LUA_ANSI
29 | #endif
30 |
31 |
32 | #if !defined(LUA_ANSI) && defined(_WIN32)
33 | #define LUA_WIN
34 | #endif
35 |
36 | #if defined(LUA_USE_LINUX)
37 | #define LUA_USE_POSIX
38 | #define LUA_USE_DLOPEN /* needs an extra library: -ldl */
39 | #define LUA_USE_READLINE /* needs some extra libraries */
40 | #endif
41 |
42 | #if defined(LUA_USE_MACOSX)
43 | #define LUA_USE_POSIX
44 | #define LUA_DL_DYLD /* does not need extra library */
45 | #endif
46 |
47 |
48 |
49 | /*
50 | @@ LUA_USE_POSIX includes all functionallity listed as X/Open System
51 | @* Interfaces Extension (XSI).
52 | ** CHANGE it (define it) if your system is XSI compatible.
53 | */
54 | #if defined(LUA_USE_POSIX)
55 | #define LUA_USE_MKSTEMP
56 | #define LUA_USE_ISATTY
57 | #define LUA_USE_POPEN
58 | #define LUA_USE_ULONGJMP
59 | #endif
60 |
61 |
62 | /*
63 | @@ LUA_PATH and LUA_CPATH are the names of the environment variables that
64 | @* Lua check to set its paths.
65 | @@ LUA_INIT is the name of the environment variable that Lua
66 | @* checks for initialization code.
67 | ** CHANGE them if you want different names.
68 | */
69 | #define LUA_PATH "LUA_PATH"
70 | #define LUA_CPATH "LUA_CPATH"
71 | #define LUA_INIT "LUA_INIT"
72 |
73 |
74 | /*
75 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
76 | @* Lua libraries.
77 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
78 | @* C libraries.
79 | ** CHANGE them if your machine has a non-conventional directory
80 | ** hierarchy or if you want to install your libraries in
81 | ** non-conventional directories.
82 | */
83 | #if defined(_WIN32)
84 | /*
85 | ** In Windows, any exclamation mark ('!') in the path is replaced by the
86 | ** path of the directory of the executable file of the current process.
87 | */
88 | #define LUA_LDIR "!\\lua\\"
89 | #define LUA_CDIR "!\\"
90 | #define LUA_PATH_DEFAULT \
91 | ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
92 | LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
93 | #define LUA_CPATH_DEFAULT \
94 | ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
95 |
96 | #else
97 | #define LUA_ROOT "/usr/local/"
98 | #define LUA_LDIR LUA_ROOT "share/lua/5.1/"
99 | #define LUA_CDIR LUA_ROOT "lib/lua/5.1/"
100 | #define LUA_PATH_DEFAULT \
101 | "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
102 | LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua"
103 | #define LUA_CPATH_DEFAULT \
104 | "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
105 | #endif
106 |
107 |
108 | /*
109 | @@ LUA_DIRSEP is the directory separator (for submodules).
110 | ** CHANGE it if your machine does not use "/" as the directory separator
111 | ** and is not Windows. (On Windows Lua automatically uses "\".)
112 | */
113 | #if defined(_WIN32)
114 | #define LUA_DIRSEP "\\"
115 | #else
116 | #define LUA_DIRSEP "/"
117 | #endif
118 |
119 |
120 | /*
121 | @@ LUA_PATHSEP is the character that separates templates in a path.
122 | @@ LUA_PATH_MARK is the string that marks the substitution points in a
123 | @* template.
124 | @@ LUA_EXECDIR in a Windows path is replaced by the executable's
125 | @* directory.
126 | @@ LUA_IGMARK is a mark to ignore all before it when bulding the
127 | @* luaopen_ function name.
128 | ** CHANGE them if for some reason your system cannot use those
129 | ** characters. (E.g., if one of those characters is a common character
130 | ** in file/directory names.) Probably you do not need to change them.
131 | */
132 | #define LUA_PATHSEP ";"
133 | #define LUA_PATH_MARK "?"
134 | #define LUA_EXECDIR "!"
135 | #define LUA_IGMARK "-"
136 |
137 |
138 | /*
139 | @@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
140 | ** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
141 | ** machines, ptrdiff_t gives a good choice between int or long.)
142 | */
143 | #define LUA_INTEGER ptrdiff_t
144 |
145 |
146 | /*
147 | @@ LUA_API is a mark for all core API functions.
148 | @@ LUALIB_API is a mark for all standard library functions.
149 | ** CHANGE them if you need to define those functions in some special way.
150 | ** For instance, if you want to create one Windows DLL with the core and
151 | ** the libraries, you may want to use the following definition (define
152 | ** LUA_BUILD_AS_DLL to get it).
153 | */
154 | #if defined(LUA_BUILD_AS_DLL)
155 |
156 | #if defined(LUA_CORE) || defined(LUA_LIB)
157 | #define LUA_API __declspec(dllexport)
158 | #else
159 | #define LUA_API __declspec(dllimport)
160 | #endif
161 |
162 | #else
163 |
164 | #define LUA_API extern
165 |
166 | #endif
167 |
168 | /* more often than not the libs go together with the core */
169 | #define LUALIB_API LUA_API
170 |
171 |
172 | /*
173 | @@ LUAI_FUNC is a mark for all extern functions that are not to be
174 | @* exported to outside modules.
175 | @@ LUAI_DATA is a mark for all extern (const) variables that are not to
176 | @* be exported to outside modules.
177 | ** CHANGE them if you need to mark them in some special way. Elf/gcc
178 | ** (versions 3.2 and later) mark them as "hidden" to optimize access
179 | ** when Lua is compiled as a shared library.
180 | */
181 | #if defined(luaall_c)
182 | #define LUAI_FUNC static
183 | #define LUAI_DATA /* empty */
184 |
185 | #elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
186 | defined(__ELF__)
187 | #define LUAI_FUNC __attribute__((visibility("hidden"))) extern
188 | #define LUAI_DATA LUAI_FUNC
189 |
190 | #else
191 | #define LUAI_FUNC extern
192 | #define LUAI_DATA extern
193 | #endif
194 |
195 |
196 |
197 | /*
198 | @@ LUA_QL describes how error messages quote program elements.
199 | ** CHANGE it if you want a different appearance.
200 | */
201 | #define LUA_QL(x) "'" x "'"
202 | #define LUA_QS LUA_QL("%s")
203 |
204 |
205 | /*
206 | @@ LUA_IDSIZE gives the maximum size for the description of the source
207 | @* of a function in debug information.
208 | ** CHANGE it if you want a different size.
209 | */
210 | #define LUA_IDSIZE 60
211 |
212 |
213 | /*
214 | ** {==================================================================
215 | ** Stand-alone configuration
216 | ** ===================================================================
217 | */
218 |
219 | #if defined(lua_c) || defined(luaall_c)
220 |
221 | /*
222 | @@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
223 | @* is, whether we're running lua interactively).
224 | ** CHANGE it if you have a better definition for non-POSIX/non-Windows
225 | ** systems.
226 | */
227 | #if defined(LUA_USE_ISATTY)
228 | #include
229 | #define lua_stdin_is_tty() isatty(0)
230 | #elif defined(LUA_WIN)
231 | #include
232 | #include
233 | #define lua_stdin_is_tty() _isatty(_fileno(stdin))
234 | #else
235 | #define lua_stdin_is_tty() 1 /* assume stdin is a tty */
236 | #endif
237 |
238 |
239 | /*
240 | @@ LUA_PROMPT is the default prompt used by stand-alone Lua.
241 | @@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
242 | ** CHANGE them if you want different prompts. (You can also change the
243 | ** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
244 | */
245 | #define LUA_PROMPT "> "
246 | #define LUA_PROMPT2 ">> "
247 |
248 |
249 | /*
250 | @@ LUA_PROGNAME is the default name for the stand-alone Lua program.
251 | ** CHANGE it if your stand-alone interpreter has a different name and
252 | ** your system is not able to detect that name automatically.
253 | */
254 | #define LUA_PROGNAME "lua"
255 |
256 |
257 | /*
258 | @@ LUA_MAXINPUT is the maximum length for an input line in the
259 | @* stand-alone interpreter.
260 | ** CHANGE it if you need longer lines.
261 | */
262 | #define LUA_MAXINPUT 512
263 |
264 |
265 | /*
266 | @@ lua_readline defines how to show a prompt and then read a line from
267 | @* the standard input.
268 | @@ lua_saveline defines how to "save" a read line in a "history".
269 | @@ lua_freeline defines how to free a line read by lua_readline.
270 | ** CHANGE them if you want to improve this functionality (e.g., by using
271 | ** GNU readline and history facilities).
272 | */
273 | #if defined(LUA_USE_READLINE)
274 | #include
275 | #include
276 | #include
277 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
278 | #define lua_saveline(L,idx) \
279 | if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
280 | add_history(lua_tostring(L, idx)); /* add it to history */
281 | #define lua_freeline(L,b) ((void)L, free(b))
282 | #else
283 | #define lua_readline(L,b,p) \
284 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
285 | fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
286 | #define lua_saveline(L,idx) { (void)L; (void)idx; }
287 | #define lua_freeline(L,b) { (void)L; (void)b; }
288 | #endif
289 |
290 | #endif
291 |
292 | /* }================================================================== */
293 |
294 |
295 | /*
296 | @@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles
297 | @* as a percentage.
298 | ** CHANGE it if you want the GC to run faster or slower (higher values
299 | ** mean larger pauses which mean slower collection.) You can also change
300 | ** this value dynamically.
301 | */
302 | #define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */
303 |
304 |
305 | /*
306 | @@ LUAI_GCMUL defines the default speed of garbage collection relative to
307 | @* memory allocation as a percentage.
308 | ** CHANGE it if you want to change the granularity of the garbage
309 | ** collection. (Higher values mean coarser collections. 0 represents
310 | ** infinity, where each step performs a full collection.) You can also
311 | ** change this value dynamically.
312 | */
313 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
314 |
315 |
316 |
317 | /*
318 | @@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
319 | ** CHANGE it (define it) if you want exact compatibility with the
320 | ** behavior of setn/getn in Lua 5.0.
321 | */
322 | #undef LUA_COMPAT_GETN
323 |
324 | /*
325 | @@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.
326 | ** CHANGE it to undefined as soon as you do not need a global 'loadlib'
327 | ** function (the function is still available as 'package.loadlib').
328 | */
329 | #undef LUA_COMPAT_LOADLIB
330 |
331 | /*
332 | @@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
333 | ** CHANGE it to undefined as soon as your programs use only '...' to
334 | ** access vararg parameters (instead of the old 'arg' table).
335 | */
336 | #define LUA_COMPAT_VARARG
337 |
338 | /*
339 | @@ LUA_COMPAT_MOD controls compatibility with old math.mod function.
340 | ** CHANGE it to undefined as soon as your programs use 'math.fmod' or
341 | ** the new '%' operator instead of 'math.mod'.
342 | */
343 | #define LUA_COMPAT_MOD
344 |
345 | /*
346 | @@ LUA_COMPAT_LSTR controls compatibility with old long string nesting
347 | @* facility.
348 | ** CHANGE it to 2 if you want the old behaviour, or undefine it to turn
349 | ** off the advisory error when nesting [[...]].
350 | */
351 | #define LUA_COMPAT_LSTR 1
352 |
353 | /*
354 | @@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.
355 | ** CHANGE it to undefined as soon as you rename 'string.gfind' to
356 | ** 'string.gmatch'.
357 | */
358 | #define LUA_COMPAT_GFIND
359 |
360 | /*
361 | @@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'
362 | @* behavior.
363 | ** CHANGE it to undefined as soon as you replace to 'luaL_register'
364 | ** your uses of 'luaL_openlib'
365 | */
366 | #define LUA_COMPAT_OPENLIB
367 |
368 |
369 |
370 | /*
371 | @@ luai_apicheck is the assert macro used by the Lua-C API.
372 | ** CHANGE luai_apicheck if you want Lua to perform some checks in the
373 | ** parameters it gets from API calls. This may slow down the interpreter
374 | ** a bit, but may be quite useful when debugging C code that interfaces
375 | ** with Lua. A useful redefinition is to use assert.h.
376 | */
377 | #if defined(LUA_USE_APICHECK)
378 | /////////////////////////////////////////////////////////////////////////
379 | // MTA addition for testing if apicheck will function as expected, and generating more useful crash dumps
380 | #undef assert
381 | #define assert(_Expression) (void)( (!!(_Expression)) || ( *((int*)NULL) = 0) )
382 | LUA_API int luaX_is_apicheck_enabled();
383 | /////////////////////////////////////////////////////////////////////////
384 | #define luai_apicheck(L,o) { (void)L; assert(o); }
385 | #else
386 | #define luai_apicheck(L,o) { (void)L; }
387 | #endif
388 |
389 |
390 | /*
391 | @@ LUAI_BITSINT defines the number of bits in an int.
392 | ** CHANGE here if Lua cannot automatically detect the number of bits of
393 | ** your machine. Probably you do not need to change this.
394 | */
395 | /* avoid overflows in comparison */
396 | #if INT_MAX-20 < 32760
397 | #define LUAI_BITSINT 16
398 | #elif INT_MAX > 2147483640L
399 | /* int has at least 32 bits */
400 | #define LUAI_BITSINT 32
401 | #else
402 | #error "you must define LUA_BITSINT with number of bits in an integer"
403 | #endif
404 |
405 |
406 | /*
407 | @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
408 | @@ LUAI_INT32 is an signed integer with at least 32 bits.
409 | @@ LUAI_UMEM is an unsigned integer big enough to count the total
410 | @* memory used by Lua.
411 | @@ LUAI_MEM is a signed integer big enough to count the total memory
412 | @* used by Lua.
413 | ** CHANGE here if for some weird reason the default definitions are not
414 | ** good enough for your machine. (The definitions in the 'else'
415 | ** part always works, but may waste space on machines with 64-bit
416 | ** longs.) Probably you do not need to change this.
417 | */
418 | #if LUAI_BITSINT >= 32
419 | #define LUAI_UINT32 unsigned int
420 | #define LUAI_INT32 int
421 | #define LUAI_MAXINT32 INT_MAX
422 | #define LUAI_UMEM size_t
423 | #define LUAI_MEM ptrdiff_t
424 | #else
425 | /* 16-bit ints */
426 | #define LUAI_UINT32 unsigned long
427 | #define LUAI_INT32 long
428 | #define LUAI_MAXINT32 LONG_MAX
429 | #define LUAI_UMEM unsigned long
430 | #define LUAI_MEM long
431 | #endif
432 |
433 |
434 | /*
435 | @@ LUAI_MAXCALLS limits the number of nested calls.
436 | ** CHANGE it if you need really deep recursive calls. This limit is
437 | ** arbitrary; its only purpose is to stop infinite recursion before
438 | ** exhausting memory.
439 | */
440 | #define LUAI_MAXCALLS 20000
441 |
442 |
443 | /*
444 | @@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function
445 | @* can use.
446 | ** CHANGE it if you need lots of (Lua) stack space for your C
447 | ** functions. This limit is arbitrary; its only purpose is to stop C
448 | ** functions to consume unlimited stack space. (must be smaller than
449 | ** -LUA_REGISTRYINDEX)
450 | */
451 | #define LUAI_MAXCSTACK 8000
452 |
453 |
454 |
455 | /*
456 | ** {==================================================================
457 | ** CHANGE (to smaller values) the following definitions if your system
458 | ** has a small C stack. (Or you may want to change them to larger
459 | ** values if your system has a large C stack and these limits are
460 | ** too rigid for you.) Some of these constants control the size of
461 | ** stack-allocated arrays used by the compiler or the interpreter, while
462 | ** others limit the maximum number of recursive calls that the compiler
463 | ** or the interpreter can perform. Values too large may cause a C stack
464 | ** overflow for some forms of deep constructs.
465 | ** ===================================================================
466 | */
467 |
468 |
469 | /*
470 | @@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and
471 | @* syntactical nested non-terminals in a program.
472 | */
473 | #define LUAI_MAXCCALLS 200
474 |
475 |
476 | /*
477 | @@ LUAI_MAXVARS is the maximum number of local variables per function
478 | @* (must be smaller than 250).
479 | */
480 | #define LUAI_MAXVARS 200
481 |
482 |
483 | /*
484 | @@ LUAI_MAXUPVALUES is the maximum number of upvalues per function
485 | @* (must be smaller than 250).
486 | */
487 | #define LUAI_MAXUPVALUES 60
488 |
489 |
490 | /*
491 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
492 | */
493 | #define LUAL_BUFFERSIZE BUFSIZ
494 |
495 | /* }================================================================== */
496 |
497 |
498 |
499 |
500 | /*
501 | ** {==================================================================
502 | @@ LUA_NUMBER is the type of numbers in Lua.
503 | ** CHANGE the following definitions only if you want to build Lua
504 | ** with a number type different from double. You may also need to
505 | ** change lua_number2int & lua_number2integer.
506 | ** ===================================================================
507 | */
508 |
509 | #define LUA_NUMBER_DOUBLE
510 | #define LUA_NUMBER double
511 |
512 | /*
513 | @@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
514 | @* over a number.
515 | */
516 | #define LUAI_UACNUMBER double
517 |
518 |
519 | /*
520 | @@ LUA_NUMBER_SCAN is the format for reading numbers.
521 | @@ LUA_NUMBER_FMT is the format for writing numbers.
522 | @@ lua_number2str converts a number to a string.
523 | @@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
524 | @@ lua_str2number converts a string to a number.
525 | */
526 | #define LUA_NUMBER_SCAN "%lf"
527 | #define LUA_NUMBER_FMT "%.14g"
528 | #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
529 | #define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
530 | #define lua_str2number(s,p) strtod((s), (p))
531 |
532 |
533 | /*
534 | @@ The luai_num* macros define the primitive operations over numbers.
535 | */
536 | #if defined(LUA_CORE)
537 | #include
538 | #define luai_numadd(a,b) ((a)+(b))
539 | #define luai_numsub(a,b) ((a)-(b))
540 | #define luai_nummul(a,b) ((a)*(b))
541 | #define luai_numdiv(a,b) ((a)/(b))
542 | #define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
543 | #define luai_numpow(a,b) (pow(a,b))
544 | #define luai_numunm(a) (-(a))
545 | #define luai_numeq(a,b) ((a)==(b))
546 | #define luai_numlt(a,b) ((a)<(b))
547 | #define luai_numle(a,b) ((a)<=(b))
548 | #define luai_numisnan(a) (!luai_numeq((a), (a)))
549 | #endif
550 |
551 |
552 | /*
553 | @@ lua_number2int is a macro to convert lua_Number to int.
554 | @@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
555 | ** CHANGE them if you know a faster way to convert a lua_Number to
556 | ** int (with any rounding method and without throwing errors) in your
557 | ** system. In Pentium machines, a naive typecast from double to int
558 | ** in C is extremely slow, so any alternative is worth trying.
559 | */
560 |
561 | /* On a Pentium, resort to a trick */
562 | #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
563 | (defined(__i386) || defined (_M_IX86) || defined(__i386__))
564 |
565 | /* On a Microsoft compiler, use assembler */
566 | #if defined(_MSC_VER)
567 |
568 | #define lua_number2int(i,d) __asm fld d __asm fistp i
569 | #define lua_number2integer(i,n) lua_number2int(i, n)
570 |
571 | /* the next trick should work on any Pentium, but sometimes clashes
572 | with a DirectX idiosyncrasy */
573 | #else
574 |
575 | union luai_Cast { double l_d; long l_l; };
576 | #define lua_number2int(i,d) \
577 | { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
578 | #define lua_number2integer(i,n) lua_number2int(i, n)
579 |
580 | #endif
581 |
582 |
583 | /* this option always works, but may be slow */
584 | #else
585 | #define lua_number2int(i,d) ((i)=(int)(d))
586 | #define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
587 |
588 | #endif
589 |
590 | /* }================================================================== */
591 |
592 |
593 | /*
594 | @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
595 | ** CHANGE it if your system requires alignments larger than double. (For
596 | ** instance, if your system supports long doubles and they must be
597 | ** aligned in 16-byte boundaries, then you should add long double in the
598 | ** union.) Probably you do not need to change this.
599 | */
600 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
601 |
602 |
603 | /*
604 | @@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
605 | ** CHANGE them if you prefer to use longjmp/setjmp even with C++
606 | ** or if want/don't to use _longjmp/_setjmp instead of regular
607 | ** longjmp/setjmp. By default, Lua handles errors with exceptions when
608 | ** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
609 | ** and with longjmp/setjmp otherwise.
610 | */
611 | #if defined(__cplusplus)
612 | /* C++ exceptions */
613 | #define LUAI_THROW(L,c) throw(c)
614 | #define LUAI_TRY(L,c,a) try { a } catch(...) \
615 | { if ((c)->status == 0) (c)->status = -1; }
616 | #define luai_jmpbuf int /* dummy variable */
617 |
618 | #elif defined(LUA_USE_ULONGJMP)
619 | /* in Unix, try _longjmp/_setjmp (more efficient) */
620 | #define LUAI_THROW(L,c) _longjmp((c)->b, 1)
621 | #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
622 | #define luai_jmpbuf jmp_buf
623 |
624 | #else
625 | /* default handling with long jumps */
626 | #define LUAI_THROW(L,c) longjmp((c)->b, 1)
627 | #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
628 | #define luai_jmpbuf jmp_buf
629 |
630 | #endif
631 |
632 |
633 | /*
634 | @@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
635 | @* can do during pattern-matching.
636 | ** CHANGE it if you need more captures. This limit is arbitrary.
637 | */
638 | #define LUA_MAXCAPTURES 32
639 |
640 |
641 | /*
642 | @@ lua_tmpnam is the function that the OS library uses to create a
643 | @* temporary name.
644 | @@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.
645 | ** CHANGE them if you have an alternative to tmpnam (which is considered
646 | ** insecure) or if you want the original tmpnam anyway. By default, Lua
647 | ** uses tmpnam except when POSIX is available, where it uses mkstemp.
648 | */
649 | #if defined(loslib_c) || defined(luaall_c)
650 |
651 | #if defined(LUA_USE_MKSTEMP)
652 | #include
653 | #define LUA_TMPNAMBUFSIZE 32
654 | #define lua_tmpnam(b,e) { \
655 | strcpy(b, "/tmp/lua_XXXXXX"); \
656 | e = mkstemp(b); \
657 | if (e != -1) close(e); \
658 | e = (e == -1); }
659 |
660 | #else
661 | #define LUA_TMPNAMBUFSIZE L_tmpnam
662 | #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
663 | #endif
664 |
665 | #endif
666 |
667 |
668 | /*
669 | @@ lua_popen spawns a new process connected to the current one through
670 | @* the file streams.
671 | ** CHANGE it if you have a way to implement it in your system.
672 | */
673 | #if defined(LUA_USE_POPEN)
674 |
675 | #define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
676 | #define lua_pclose(L,file) ((void)L, (pclose(file) != -1))
677 |
678 | #elif defined(LUA_WIN)
679 |
680 | #define lua_popen(L,c,m) ((void)L, _popen(c,m))
681 | #define lua_pclose(L,file) ((void)L, (_pclose(file) != -1))
682 |
683 | #else
684 |
685 | #define lua_popen(L,c,m) ((void)((void)c, m), \
686 | luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
687 | #define lua_pclose(L,file) ((void)((void)L, file), 0)
688 |
689 | #endif
690 |
691 | /*
692 | @@ LUA_DL_* define which dynamic-library system Lua should use.
693 | ** CHANGE here if Lua has problems choosing the appropriate
694 | ** dynamic-library system for your platform (either Windows' DLL, Mac's
695 | ** dyld, or Unix's dlopen). If your system is some kind of Unix, there
696 | ** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for
697 | ** it. To use dlopen you also need to adapt the src/Makefile (probably
698 | ** adding -ldl to the linker options), so Lua does not select it
699 | ** automatically. (When you change the makefile to add -ldl, you must
700 | ** also add -DLUA_USE_DLOPEN.)
701 | ** If you do not want any kind of dynamic library, undefine all these
702 | ** options.
703 | ** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
704 | */
705 | #if defined(LUA_USE_DLOPEN)
706 | #define LUA_DL_DLOPEN
707 | #endif
708 |
709 | #if defined(LUA_WIN)
710 | #define LUA_DL_DLL
711 | #endif
712 |
713 |
714 | /*
715 | @@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
716 | @* (the data goes just *before* the lua_State pointer).
717 | ** CHANGE (define) this if you really need that. This value must be
718 | ** a multiple of the maximum alignment required for your machine.
719 | */
720 | #define LUAI_EXTRASPACE 0
721 |
722 |
723 | /*
724 | @@ luai_userstate* allow user-specific actions on threads.
725 | ** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
726 | ** extra when a thread is created/deleted/resumed/yielded.
727 | */
728 | #define luai_userstateopen(L) ((void)L)
729 | #define luai_userstateclose(L) ((void)L)
730 | #define luai_userstatethread(L,L1) ((void)L)
731 | #define luai_userstatefree(L) ((void)L)
732 | #define luai_userstateresume(L,n) ((void)L)
733 | #define luai_userstateyield(L,n) ((void)L)
734 |
735 |
736 | /*
737 | @@ LUA_INTFRMLEN is the length modifier for integer conversions
738 | @* in 'string.format'.
739 | @@ LUA_INTFRM_T is the integer type correspoding to the previous length
740 | @* modifier.
741 | ** CHANGE them if your system supports long long or does not support long.
742 | */
743 |
744 | #if defined(LUA_USELONGLONG)
745 |
746 | #define LUA_INTFRMLEN "ll"
747 | #define LUA_INTFRM_T long long
748 |
749 | #else
750 |
751 | #define LUA_INTFRMLEN "l"
752 | #define LUA_INTFRM_T long
753 |
754 | #endif
755 |
756 |
757 |
758 | /* =================================================================== */
759 |
760 | /*
761 | ** Local configuration. You can use this space to add your redefinitions
762 | ** without modifying the main part of the file.
763 | */
764 |
765 |
766 |
767 | #endif
768 |
769 |
--------------------------------------------------------------------------------
/module/include/lualib.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $
3 | ** Lua standard libraries
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lualib_h
9 | #define lualib_h
10 |
11 | #include "lua.h"
12 |
13 |
14 | /* Key to file-handle type */
15 | #define LUA_FILEHANDLE "FILE*"
16 |
17 |
18 | #define LUA_COLIBNAME "coroutine"
19 | LUALIB_API int (luaopen_base) (lua_State *L);
20 |
21 | #define LUA_TABLIBNAME "table"
22 | LUALIB_API int (luaopen_table) (lua_State *L);
23 |
24 | #define LUA_IOLIBNAME "io"
25 | LUALIB_API int (luaopen_io) (lua_State *L);
26 |
27 | #define LUA_OSLIBNAME "os"
28 | LUALIB_API int (luaopen_os) (lua_State *L);
29 |
30 | #define LUA_STRLIBNAME "string"
31 | LUALIB_API int (luaopen_string) (lua_State *L);
32 |
33 | #define LUA_MATHLIBNAME "math"
34 | LUALIB_API int (luaopen_math) (lua_State *L);
35 |
36 | #define LUA_DBLIBNAME "debug"
37 | LUALIB_API int (luaopen_debug) (lua_State *L);
38 |
39 | #define LUA_LOADLIBNAME "package"
40 | LUALIB_API int (luaopen_package) (lua_State *L);
41 |
42 |
43 | /* open all previous libraries */
44 | LUALIB_API void (luaL_openlibs) (lua_State *L);
45 |
46 | /* Additional luautf8 library */
47 | LUALIB_API int luaopen_utf8(lua_State *L);
48 |
49 | #ifndef lua_assert
50 | #define lua_assert(x) ((void)0)
51 | #endif
52 |
53 |
54 | #endif
55 |
--------------------------------------------------------------------------------
/module/lib/lua5.1.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/lib/lua5.1.lib
--------------------------------------------------------------------------------
/module/lib/lua5.1_64.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/module/lib/lua5.1_64.lib
--------------------------------------------------------------------------------
/module/luaimports/luaimports.linux.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008, Alberto Alonso
3 | *
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * * Redistributions of source code must retain the above copyright notice, this
10 | * list of conditions and the following disclaimer.
11 | * * Redistributions in binary form must reproduce the above copyright notice, this
12 | * list of conditions and the following disclaimer in the documentation and/or other
13 | * materials provided with the distribution.
14 | * * Neither the name of the mta-mysql nor the names of its contributors may be used
15 | * to endorse or promote products derived from this software without specific prior
16 | * written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | #include
31 | #include
32 | #include "ILuaModuleManager.h"
33 | #include "lauxlib.h"
34 | #include "luaconf.h"
35 | #include "lua.h"
36 | #include "lualib.h"
37 |
38 | /* This file extracts the lua function addresses from the server core to avoid lua gc crashes */
39 |
40 | extern ILuaModuleManager* pModuleManager;
41 |
42 | /*
43 | ** state manipulation
44 | */
45 | static void* plua_newstate = 0;
46 | static void* plua_close = 0;
47 | static void* plua_newthread = 0;
48 | static void* plua_atpanic = 0;
49 |
50 | /*
51 | ** basic stack manipulation
52 | */
53 | static void* plua_gettop = 0;
54 | static void* plua_settop = 0;
55 | static void* plua_pushvalue = 0;
56 | static void* plua_remove = 0;
57 | static void* plua_insert = 0;
58 | static void* plua_replace = 0;
59 | static void* plua_checkstack = 0;
60 | static void* plua_xmove = 0;
61 |
62 | /*
63 | ** access functions (stack -> C)
64 | */
65 | static void* plua_isnumber = 0;
66 | static void* plua_isstring = 0;
67 | static void* plua_iscfunction = 0;
68 | static void* plua_isuserdata = 0;
69 | static void* plua_type = 0;
70 | static void* plua_typename = 0;
71 | static void* plua_equal = 0;
72 | static void* plua_rawequal = 0;
73 | static void* plua_lessthan = 0;
74 | static void* plua_tonumber = 0;
75 | static void* plua_tointeger = 0;
76 | static void* plua_toboolean = 0;
77 | static void* plua_tolstring = 0;
78 | static void* plua_objlen = 0;
79 | static void* plua_tocfunction = 0;
80 | static void* plua_touserdata = 0;
81 | static void* plua_tothread = 0;
82 | static void* plua_topointer = 0;
83 |
84 | /*
85 | ** push functions (C -> stack)
86 | */
87 | static void* plua_pushnil = 0;
88 | static void* plua_pushnumber = 0;
89 | static void* plua_pushinteger = 0;
90 | static void* plua_pushlstring = 0;
91 | static void* plua_pushstring = 0;
92 | static void* plua_pushvfstring = 0;
93 | static void* plua_pushfstring = 0;
94 | static void* plua_pushcclosure = 0;
95 | static void* plua_pushboolean = 0;
96 | static void* plua_pushlightuserdata = 0;
97 | static void* plua_pushthread = 0;
98 |
99 | /*
100 | ** get functions (Lua -> stack)
101 | */
102 | static void* plua_gettable = 0;
103 | static void* plua_getfield = 0;
104 | static void* plua_rawget = 0;
105 | static void* plua_rawgeti = 0;
106 | static void* plua_createtable = 0;
107 | static void* plua_newuserdata = 0;
108 | static void* plua_getmetatable = 0;
109 | static void* plua_getfenv = 0;
110 |
111 | /*
112 | ** set functions (stack -> Lua)
113 | */
114 | static void* plua_settable = 0;
115 | static void* plua_setfield = 0;
116 | static void* plua_rawset = 0;
117 | static void* plua_rawseti = 0;
118 | static void* plua_setmetatable = 0;
119 | static void* plua_setfenv = 0;
120 |
121 | /*
122 | ** `load' and `call' functions (load and run Lua code)
123 | */
124 | static void* plua_call = 0;
125 | static void* plua_pcall = 0;
126 | static void* plua_cpcall = 0;
127 | static void* plua_load = 0;
128 | static void* plua_dump = 0;
129 |
130 | /*
131 | ** coroutine functions
132 | */
133 | static void* plua_yield = 0;
134 | static void* plua_resume = 0;
135 | static void* plua_status = 0;
136 |
137 | /*
138 | ** garbage-collection function and options
139 | */
140 | static void* plua_gc = 0;
141 |
142 |
143 | /*
144 | ** miscellaneous functions
145 | */
146 | static void* plua_error = 0;
147 | static void* plua_next = 0;
148 | static void* plua_concat = 0;
149 | static void* plua_getallocf = 0;
150 | static void* plua_setallocf = 0;
151 |
152 | /* Functions to be called by the debuger in specific events */
153 | static void* plua_getstack = 0;
154 | static void* plua_getinfo = 0;
155 | static void* plua_getlocal = 0;
156 | static void* plua_setlocal = 0;
157 | static void* plua_getupvalue = 0;
158 | static void* plua_setupvalue = 0;
159 | static void* plua_sethook = 0;
160 | static void* plua_gethook = 0;
161 | static void* plua_gethookmask = 0;
162 | static void* plua_gethookcount = 0;
163 |
164 | /*
165 | ** Lua auxlib
166 | */
167 | #if defined(LUA_COMPAT_GETN)
168 | static void* pluaL_getn = 0;
169 | static void* pluaL_setn = 0;
170 | #endif
171 | static void* pluaL_register = 0;
172 | static void* pluaL_getmetafield = 0;
173 | static void* pluaL_callmeta = 0;
174 | static void* pluaL_typerror = 0;
175 | static void* pluaL_argerror = 0;
176 | static void* pluaL_checklstring = 0;
177 | static void* pluaL_optlstring = 0;
178 | static void* pluaL_checknumber = 0;
179 | static void* pluaL_optnumber = 0;
180 | static void* pluaL_checkinteger = 0;
181 | static void* pluaL_optinteger = 0;
182 | static void* pluaL_checkstack = 0;
183 | static void* pluaL_checktype = 0;
184 | static void* pluaL_checkany = 0;
185 | static void* pluaL_newmetatable = 0;
186 | static void* pluaL_checkudata = 0;
187 | static void* pluaL_where = 0;
188 | static void* pluaL_error = 0;
189 | static void* pluaL_checkoption = 0;
190 | static void* pluaL_ref = 0;
191 | static void* pluaL_unref = 0;
192 | static void* pluaL_loadfile = 0;
193 | static void* pluaL_loadbuffer = 0;
194 | static void* pluaL_loadstring = 0;
195 | static void* pluaL_newstate = 0;
196 | static void* pluaL_gsub = 0;
197 | static void* pluaL_findtable = 0;
198 | static void* pluaL_buffinit = 0;
199 | static void* pluaL_prepbuffer = 0;
200 | static void* pluaL_addlstring = 0;
201 | static void* pluaL_addstring = 0;
202 | static void* pluaL_addvalue = 0;
203 | static void* pluaL_pushresult = 0;
204 | static void* plua_getmainstate = 0;
205 |
206 |
207 | #define SAFE_IMPORT(x) \
208 | p ## x = dlsym(dl, #x); \
209 | if (p ## x == 0) \
210 | { \
211 | pModuleManager->Printf("Unable to import " #x ": %s\n", dlerror()); \
212 | return false; \
213 | }
214 |
215 | #ifdef __cplusplus
216 | extern "C"
217 | #endif
218 | bool ImportLua()
219 | {
220 | #ifdef __x86_64
221 | void* dl = dlopen("x64/deathmatch.so", RTLD_NOW | RTLD_NOLOAD);
222 | #else
223 | void* dl = dlopen("mods/deathmatch/deathmatch.so", RTLD_NOW | RTLD_NOLOAD);
224 | #endif
225 | if (!dl)
226 | {
227 | pModuleManager->ErrorPrintf("Unable to open deathmatch.so: %s\n", dlerror());
228 | return false;
229 | }
230 |
231 | /*
232 | ** state manipulation
233 | */
234 | SAFE_IMPORT(lua_newstate);
235 | SAFE_IMPORT(lua_close);
236 | SAFE_IMPORT(lua_newthread);
237 | SAFE_IMPORT(lua_atpanic);
238 |
239 | /*
240 | ** basic stack manipulation
241 | */
242 | SAFE_IMPORT(lua_gettop);
243 | SAFE_IMPORT(lua_settop);
244 | SAFE_IMPORT(lua_pushvalue);
245 | SAFE_IMPORT(lua_remove);
246 | SAFE_IMPORT(lua_insert);
247 | SAFE_IMPORT(lua_replace);
248 | SAFE_IMPORT(lua_checkstack);
249 | SAFE_IMPORT(lua_xmove);
250 |
251 | /*
252 | ** access functions (stack -> C)
253 | */
254 | SAFE_IMPORT(lua_isnumber);
255 | SAFE_IMPORT(lua_isstring);
256 | SAFE_IMPORT(lua_iscfunction);
257 | SAFE_IMPORT(lua_isuserdata);
258 | SAFE_IMPORT(lua_type);
259 | SAFE_IMPORT(lua_typename);
260 | SAFE_IMPORT(lua_equal);
261 | SAFE_IMPORT(lua_rawequal);
262 | SAFE_IMPORT(lua_lessthan);
263 | SAFE_IMPORT(lua_tonumber);
264 | SAFE_IMPORT(lua_tointeger);
265 | SAFE_IMPORT(lua_toboolean);
266 | SAFE_IMPORT(lua_tolstring);
267 | SAFE_IMPORT(lua_objlen);
268 | SAFE_IMPORT(lua_tocfunction);
269 | SAFE_IMPORT(lua_touserdata);
270 | SAFE_IMPORT(lua_tothread);
271 | SAFE_IMPORT(lua_topointer);
272 |
273 | /*
274 | ** push functions (C -> stack)
275 | */
276 | SAFE_IMPORT(lua_pushnil);
277 | SAFE_IMPORT(lua_pushnumber);
278 | SAFE_IMPORT(lua_pushinteger);
279 | SAFE_IMPORT(lua_pushlstring);
280 | SAFE_IMPORT(lua_pushstring);
281 | SAFE_IMPORT(lua_pushvfstring);
282 | SAFE_IMPORT(lua_pushfstring);
283 | SAFE_IMPORT(lua_pushcclosure);
284 | SAFE_IMPORT(lua_pushboolean);
285 | SAFE_IMPORT(lua_pushlightuserdata);
286 | SAFE_IMPORT(lua_pushthread);
287 |
288 | /*
289 | ** get functions (Lua -> stack)
290 | */
291 | SAFE_IMPORT(lua_gettable);
292 | SAFE_IMPORT(lua_getfield);
293 | SAFE_IMPORT(lua_rawget);
294 | SAFE_IMPORT(lua_rawgeti);
295 | SAFE_IMPORT(lua_createtable);
296 | SAFE_IMPORT(lua_newuserdata);
297 | SAFE_IMPORT(lua_getmetatable);
298 | SAFE_IMPORT(lua_getfenv);
299 |
300 | /*
301 | ** set functions (stack -> Lua)
302 | */
303 | SAFE_IMPORT(lua_settable);
304 | SAFE_IMPORT(lua_setfield);
305 | SAFE_IMPORT(lua_rawset);
306 | SAFE_IMPORT(lua_rawseti);
307 | SAFE_IMPORT(lua_setmetatable);
308 | SAFE_IMPORT(lua_setfenv);
309 |
310 | /*
311 | ** `load' and `call' functions (load and run Lua code)
312 | */
313 | SAFE_IMPORT(lua_call);
314 | SAFE_IMPORT(lua_pcall);
315 | SAFE_IMPORT(lua_cpcall);
316 | SAFE_IMPORT(lua_load);
317 | SAFE_IMPORT(lua_dump);
318 |
319 | /*
320 | ** coroutine functions
321 | */
322 | SAFE_IMPORT(lua_yield);
323 | SAFE_IMPORT(lua_resume);
324 | SAFE_IMPORT(lua_status);
325 |
326 | /*
327 | ** garbage-collection function and options
328 | */
329 | SAFE_IMPORT(lua_gc);
330 |
331 |
332 | /*
333 | ** miscellaneous functions
334 | */
335 | SAFE_IMPORT(lua_error);
336 | SAFE_IMPORT(lua_next);
337 | SAFE_IMPORT(lua_concat);
338 | SAFE_IMPORT(lua_getallocf);
339 | SAFE_IMPORT(lua_setallocf);
340 |
341 | /* Functions to be called by the debuger in specific events */
342 | SAFE_IMPORT(lua_getstack);
343 | SAFE_IMPORT(lua_getinfo);
344 | SAFE_IMPORT(lua_getlocal);
345 | SAFE_IMPORT(lua_setlocal);
346 | SAFE_IMPORT(lua_getupvalue);
347 | SAFE_IMPORT(lua_setupvalue);
348 | SAFE_IMPORT(lua_sethook);
349 | SAFE_IMPORT(lua_gethook);
350 | SAFE_IMPORT(lua_gethookmask);
351 | SAFE_IMPORT(lua_gethookcount);
352 |
353 | /*
354 | ** Lua auxlib
355 | */
356 | #if defined(LUA_COMPAT_GETN)
357 | SAFE_IMPORT(luaL_getn);
358 | SAFE_IMPORT(luaL_setn);
359 | #endif
360 | SAFE_IMPORT(luaL_register);
361 | SAFE_IMPORT(luaL_getmetafield);
362 | SAFE_IMPORT(luaL_callmeta);
363 | SAFE_IMPORT(luaL_typerror);
364 | SAFE_IMPORT(luaL_argerror);
365 | SAFE_IMPORT(luaL_checklstring);
366 | SAFE_IMPORT(luaL_optlstring);
367 | SAFE_IMPORT(luaL_checknumber);
368 | SAFE_IMPORT(luaL_optnumber);
369 | SAFE_IMPORT(luaL_checkinteger);
370 | SAFE_IMPORT(luaL_optinteger);
371 | SAFE_IMPORT(luaL_checkstack);
372 | SAFE_IMPORT(luaL_checktype);
373 | SAFE_IMPORT(luaL_checkany);
374 | SAFE_IMPORT(luaL_newmetatable);
375 | SAFE_IMPORT(luaL_checkudata);
376 | SAFE_IMPORT(luaL_where);
377 | SAFE_IMPORT(luaL_error);
378 | SAFE_IMPORT(luaL_checkoption);
379 | SAFE_IMPORT(luaL_ref);
380 | SAFE_IMPORT(luaL_unref);
381 | SAFE_IMPORT(luaL_loadfile);
382 | SAFE_IMPORT(luaL_loadbuffer);
383 | SAFE_IMPORT(luaL_loadstring);
384 | SAFE_IMPORT(luaL_newstate);
385 | SAFE_IMPORT(luaL_gsub);
386 | SAFE_IMPORT(luaL_findtable);
387 | SAFE_IMPORT(luaL_buffinit);
388 | SAFE_IMPORT(luaL_prepbuffer);
389 | SAFE_IMPORT(luaL_addlstring);
390 | SAFE_IMPORT(luaL_addstring);
391 | SAFE_IMPORT(luaL_addvalue);
392 | SAFE_IMPORT(luaL_pushresult);
393 |
394 | SAFE_IMPORT(lua_getmainstate);
395 |
396 | return true;
397 | }
398 | #undef SAFE_IMPORT
399 |
400 | /** typedefs **/
401 |
402 | /*
403 | ** state manipulation
404 | */
405 | typedef lua_State *(*lua_newstate_t)(lua_Alloc f, void *ud);
406 | typedef void (*lua_close_t)(lua_State *ls);
407 | typedef lua_State *(*lua_newthread_t)(lua_State *ls);
408 |
409 | typedef lua_CFunction (*lua_atpanic_t)(lua_State *ls, lua_CFunction panicf);
410 |
411 |
412 | /*
413 | ** basic stack manipulation
414 | */
415 | typedef int (*lua_gettop_t)(lua_State *ls);
416 | typedef void (*lua_settop_t)(lua_State *ls, int idx);
417 | typedef void (*lua_pushvalue_t)(lua_State *ls, int idx);
418 | typedef void (*lua_remove_t)(lua_State *ls, int idx);
419 | typedef void (*lua_insert_t)(lua_State *ls, int idx);
420 | typedef void (*lua_replace_t)(lua_State *ls, int idx);
421 | typedef int (*lua_checkstack_t)(lua_State *ls, int sz);
422 |
423 | typedef void (*lua_xmove_t)(lua_State *from, lua_State *to, int n);
424 |
425 | /*
426 | ** access functions (stack -> C)
427 | */
428 |
429 | typedef int (*lua_isnumber_t)(lua_State *ls, int idx);
430 | typedef int (*lua_isstring_t)(lua_State *ls, int idx);
431 | typedef int (*lua_iscfunction_t)(lua_State *ls, int idx);
432 | typedef int (*lua_isuserdata_t)(lua_State *ls, int idx);
433 | typedef int (*lua_type_t)(lua_State *ls, int idx);
434 | typedef const char *(*lua_typename_t)(lua_State *ls, int tp);
435 |
436 | typedef int (*lua_equal_t)(lua_State *ls, int idx1, int idx2);
437 | typedef int (*lua_rawequal_t)(lua_State *ls, int idx1, int idx2);
438 | typedef int (*lua_lessthan_t)(lua_State *ls, int idx1, int idx2);
439 |
440 | typedef lua_Number (*lua_tonumber_t)(lua_State *ls, int idx);
441 | typedef lua_Integer (*lua_tointeger_t)(lua_State *ls, int idx);
442 | typedef int (*lua_toboolean_t)(lua_State *ls, int idx);
443 | typedef const char *(*lua_tolstring_t)(lua_State *ls, int idx, size_t *len);
444 | typedef size_t (*lua_objlen_t)(lua_State *ls, int idx);
445 | typedef lua_CFunction (*lua_tocfunction_t)(lua_State *ls, int idx);
446 | typedef void *(*lua_touserdata_t)(lua_State *ls, int idx);
447 | typedef lua_State *(*lua_tothread_t)(lua_State *ls, int idx);
448 | typedef const void *(*lua_topointer_t)(lua_State *ls, int idx);
449 |
450 |
451 | /*
452 | ** push functions (C -> stack)
453 | */
454 | typedef void (*lua_pushnil_t)(lua_State *ls);
455 | typedef void (*lua_pushnumber_t)(lua_State *ls, lua_Number n);
456 | typedef void (*lua_pushinteger_t)(lua_State *ls, lua_Integer n);
457 | typedef void (*lua_pushlstring_t)(lua_State *ls, const char *s, size_t l);
458 | typedef void (*lua_pushstring_t)(lua_State *ls, const char *s);
459 | typedef const char *(*lua_pushvfstring_t) (lua_State *ls, const char *fmt, va_list argp);
460 | typedef const char *(*lua_pushfstring_t)(lua_State *ls, const char *fmt, ...);
461 | typedef void (*lua_pushcclosure_t)(lua_State *ls, lua_CFunction fn, int n);
462 | typedef void (*lua_pushboolean_t)(lua_State *ls, int b);
463 | typedef void (*lua_pushlightuserdata_t)(lua_State *ls, void *p);
464 | typedef int (*lua_pushthread_t)(lua_State *ls);
465 |
466 |
467 | /*
468 | ** get functions (Lua -> stack)
469 | */
470 | typedef void (*lua_gettable_t)(lua_State *ls, int idx);
471 | typedef void (*lua_getfield_t)(lua_State *ls, int idx, const char *k);
472 | typedef void (*lua_rawget_t)(lua_State *ls, int idx);
473 | typedef void (*lua_rawgeti_t)(lua_State *ls, int idx, int n);
474 | typedef void (*lua_createtable_t)(lua_State *ls, int narr, int nrec);
475 | typedef void *(*lua_newuserdata_t)(lua_State *ls, size_t sz);
476 | typedef int (*lua_getmetatable_t)(lua_State *ls, int objindex);
477 | typedef void (*lua_getfenv_t)(lua_State *ls, int idx);
478 |
479 | /*
480 | ** set functions (stack -> Lua)
481 | */
482 | typedef void (*lua_settable_t)(lua_State *ls, int idx);
483 | typedef void (*lua_setfield_t)(lua_State *ls, int idx, const char *k);
484 | typedef void (*lua_rawset_t)(lua_State *ls, int idx);
485 | typedef void (*lua_rawseti_t)(lua_State *ls, int idx, int n);
486 | typedef int (*lua_setmetatable_t)(lua_State *ls, int objindex);
487 | typedef int (*lua_setfenv_t)(lua_State *ls, int idx);
488 |
489 |
490 | /*
491 | ** `load' and `call' functions (load and run Lua code)
492 | */
493 | typedef void (*lua_call_t)(lua_State *ls, int nargs, int nresults);
494 | typedef int (*lua_pcall_t)(lua_State *ls, int nargs, int nresults, int errfunc);
495 | typedef int (*lua_cpcall_t)(lua_State *ls, lua_CFunction func, void *ud);
496 | typedef int (*lua_load_t) (lua_State *ls, lua_Reader reader, void *dt, const char *chunkname);
497 | typedef int (*lua_dump_t)(lua_State *ls, lua_Writer writer, void *data);
498 |
499 |
500 | /*
501 | ** coroutine functions
502 | */
503 | typedef int (*lua_yield_t)(lua_State *ls, int nresults);
504 | typedef int (*lua_resume_t)(lua_State *ls, int narg);
505 | typedef int (*lua_status_t)(lua_State *ls);
506 |
507 | /*
508 | ** garbage-collection function and options
509 | */
510 | typedef int (*lua_gc_t)(lua_State *ls, int what, int data);
511 |
512 | /*
513 | ** miscellaneous functions
514 | */
515 | typedef int (*lua_error_t)(lua_State *ls);
516 | typedef int (*lua_next_t)(lua_State *ls, int idx);
517 | typedef void (*lua_concat_t)(lua_State *ls, int n);
518 | typedef lua_Alloc (*lua_getallocf_t) (lua_State *ls, void **ud);
519 | typedef void (*lua_setallocf_t) (lua_State *ls, lua_Alloc f, void *ud);
520 |
521 | /* Functions to be called by the debuger in specific events */
522 | typedef int (*lua_getstack_t) (lua_State *ls, int level, lua_Debug *ar);
523 | typedef int (*lua_getinfo_t) (lua_State *ls, const char *what, lua_Debug *ar);
524 | typedef const char *(*lua_getlocal_t) (lua_State *ls, const lua_Debug *ar, int n);
525 | typedef const char *(*lua_setlocal_t) (lua_State *ls, const lua_Debug *ar, int n);
526 | typedef const char *(*lua_getupvalue_t) (lua_State *ls, int funcindex, int n);
527 | typedef const char *(*lua_setupvalue_t) (lua_State *ls, int funcindex, int n);
528 |
529 | typedef int (*lua_sethook_t) (lua_State *ls, lua_Hook func, int mask, int count);
530 | typedef lua_Hook (*lua_gethook_t) (lua_State *ls);
531 | typedef int (*lua_gethookmask_t) (lua_State *ls);
532 | typedef int (*lua_gethookcount_t) (lua_State *ls);
533 |
534 | /*
535 | ** Lua auxlib
536 | */
537 | #if defined(LUA_COMPAT_GETN)
538 | typedef int (*luaL_getn_t) (lua_State *ls, int t);
539 | typedef void (*luaL_setn_t) (lua_State *ls, int t, int n);
540 | #endif
541 | typedef void (*luaL_register_t)(lua_State *ls, const char *libname, const luaL_Reg *l);
542 | typedef int (*luaL_getmetafield_t)(lua_State *ls, int obj, const char *e);
543 | typedef int (*luaL_callmeta_t)(lua_State *ls, int obj, const char *e);
544 | typedef int (*luaL_typerror_t)(lua_State *ls, int narg, const char *tname);
545 | typedef int (*luaL_argerror_t)(lua_State *ls, int numarg, const char *extramsg);
546 | typedef const char *(*luaL_checklstring_t)(lua_State *ls, int numArg, size_t *l);
547 | typedef const char *(*luaL_optlstring_t)(lua_State *ls, int numArg, const char *def, size_t *l);
548 | typedef lua_Number (*luaL_checknumber_t)(lua_State *ls, int numArg);
549 | typedef lua_Number (*luaL_optnumber_t)(lua_State *ls, int nArg, lua_Number def);
550 |
551 | typedef lua_Integer (*luaL_checkinteger_t)(lua_State *ls, int numArg);
552 | typedef lua_Integer (*luaL_optinteger_t)(lua_State *ls, int nArg, lua_Integer def);
553 |
554 | typedef void (*luaL_checkstack_t)(lua_State *ls, int sz, const char *msg);
555 | typedef void (*luaL_checktype_t)(lua_State *ls, int narg, int t);
556 | typedef void (*luaL_checkany_t)(lua_State *ls, int narg);
557 |
558 | typedef int (*luaL_newmetatable_t)(lua_State *ls, const char *tname);
559 | typedef void *(*luaL_checkudata_t)(lua_State *ls, int ud, const char *tname);
560 |
561 | typedef void (*luaL_where_t)(lua_State *ls, int lvl);
562 | typedef int (*luaL_error_t)(lua_State *ls, const char *fmt, ...);
563 |
564 | typedef int (*luaL_checkoption_t)(lua_State *ls, int narg, const char *def, const char *const lst[]);
565 |
566 | typedef int (*luaL_ref_t)(lua_State *ls, int t);
567 | typedef void (*luaL_unref_t)(lua_State *ls, int t, int ref);
568 |
569 | typedef int (*luaL_loadfile_t)(lua_State *ls, const char *filename);
570 | typedef int (*luaL_loadbuffer_t)(lua_State *ls, const char *buff, size_t sz, const char *name);
571 | typedef int (*luaL_loadstring_t)(lua_State *ls, const char *s);
572 |
573 | typedef lua_State *(*luaL_newstate_t)(void);
574 |
575 | typedef const char *(*luaL_gsub_t)(lua_State *ls, const char *s, const char *p, const char *r);
576 |
577 | typedef const char *(*luaL_findtable_t)(lua_State *ls, int idx, const char *fname, int szhint);
578 | typedef void (*luaL_buffinit_t)(lua_State *ls, luaL_Buffer *B);
579 | typedef char *(*luaL_prepbuffer_t)(luaL_Buffer *B);
580 | typedef void (*luaL_addlstring_t)(luaL_Buffer *B, const char *s, size_t l);
581 | typedef void (*luaL_addstring_t)(luaL_Buffer *B, const char *s);
582 | typedef void (*luaL_addvalue_t)(luaL_Buffer *B);
583 | typedef void (*luaL_pushresult_t)(luaL_Buffer *B);
584 |
585 | typedef lua_State* (*lua_getmainstate_t)(lua_State* L);
586 |
587 |
588 | /** functions **/
589 |
590 | #ifdef __cplusplus
591 | extern "C" {
592 | #endif
593 |
594 | /*
595 | ** state manipulation
596 | */
597 | #define LRET(f, ...) return ((f ## _t)p ## f)(__VA_ARGS__)
598 | #define LCALL(f, ...) ((f ## _t)p ## f)(__VA_ARGS__)
599 | lua_State *(lua_newstate) (lua_Alloc f, void *ud)
600 | {
601 | LRET(lua_newstate, f, ud);
602 | }
603 |
604 | void (lua_close) (lua_State *ls)
605 | {
606 | LRET(lua_close, ls);
607 | }
608 |
609 | lua_State *(lua_newthread) (lua_State *ls)
610 | {
611 | LRET(lua_newthread, ls);
612 | }
613 |
614 |
615 | lua_CFunction (lua_atpanic) (lua_State *ls, lua_CFunction panicf)
616 | {
617 | LRET(lua_atpanic, ls, panicf);
618 | }
619 |
620 |
621 |
622 | /*
623 | ** basic stack manipulation
624 | */
625 | int (lua_gettop) (lua_State *ls)
626 | {
627 | LRET(lua_gettop, ls);
628 | }
629 |
630 | void (lua_settop) (lua_State *ls, int idx)
631 | {
632 | LCALL(lua_settop, ls, idx);
633 | }
634 |
635 | void (lua_pushvalue) (lua_State *ls, int idx)
636 | {
637 | LCALL(lua_pushvalue, ls, idx);
638 | }
639 |
640 | void (lua_remove) (lua_State *ls, int idx)
641 | {
642 | LCALL(lua_remove, ls, idx);
643 | }
644 |
645 | void (lua_insert) (lua_State *ls, int idx)
646 | {
647 | LCALL(lua_insert, ls, idx);
648 | }
649 |
650 | void (lua_replace) (lua_State *ls, int idx)
651 | {
652 | LCALL(lua_replace, ls, idx);
653 | }
654 |
655 | int (lua_checkstack) (lua_State *ls, int sz)
656 | {
657 | LRET(lua_checkstack, ls, sz);
658 | }
659 |
660 |
661 | void (lua_xmove) (lua_State *from, lua_State *to, int n)
662 | {
663 | LCALL(lua_xmove, from, to, n);
664 | }
665 |
666 |
667 | /*
668 | ** access functions (stack -> C)
669 | */
670 |
671 | int (lua_isnumber) (lua_State *ls, int idx)
672 | {
673 | LRET(lua_isnumber, ls, idx);
674 | }
675 |
676 | int (lua_isstring) (lua_State *ls, int idx)
677 | {
678 | LRET(lua_isstring, ls, idx);
679 | }
680 |
681 | int (lua_iscfunction) (lua_State *ls, int idx)
682 | {
683 | LRET(lua_iscfunction, ls, idx);
684 | }
685 |
686 | int (lua_isuserdata) (lua_State *ls, int idx)
687 | {
688 | LRET(lua_isuserdata, ls, idx);
689 | }
690 |
691 | int (lua_type) (lua_State *ls, int idx)
692 | {
693 | LRET(lua_type, ls, idx);
694 | }
695 |
696 | const char *(lua_typename) (lua_State *ls, int tp)
697 | {
698 | LRET(lua_typename, ls, tp);
699 | }
700 |
701 |
702 | int (lua_equal) (lua_State *ls, int idx1, int idx2)
703 | {
704 | LRET(lua_equal, ls, idx1, idx2);
705 | }
706 |
707 | int (lua_rawequal) (lua_State *ls, int idx1, int idx2)
708 | {
709 | LRET(lua_rawequal, ls, idx1, idx2);
710 | }
711 |
712 | int (lua_lessthan) (lua_State *ls, int idx1, int idx2)
713 | {
714 | LRET(lua_lessthan, ls, idx1, idx2);
715 | }
716 |
717 |
718 | lua_Number (lua_tonumber) (lua_State *ls, int idx)
719 | {
720 | LRET(lua_tonumber, ls, idx);
721 | }
722 |
723 | lua_Integer (lua_tointeger) (lua_State *ls, int idx)
724 | {
725 | LRET(lua_tointeger, ls, idx);
726 | }
727 |
728 | int (lua_toboolean) (lua_State *ls, int idx)
729 | {
730 | LRET(lua_toboolean, ls, idx);
731 | }
732 |
733 | const char *(lua_tolstring) (lua_State *ls, int idx, size_t *len)
734 | {
735 | LRET(lua_tolstring, ls, idx, len);
736 | }
737 |
738 | size_t (lua_objlen) (lua_State *ls, int idx)
739 | {
740 | LRET(lua_objlen, ls, idx);
741 | }
742 |
743 | lua_CFunction (lua_tocfunction) (lua_State *ls, int idx)
744 | {
745 | LRET(lua_tocfunction, ls, idx);
746 | }
747 |
748 | void *(lua_touserdata) (lua_State *ls, int idx)
749 | {
750 | LRET(lua_touserdata, ls, idx);
751 | }
752 |
753 | lua_State *(lua_tothread) (lua_State *ls, int idx)
754 | {
755 | LRET(lua_tothread, ls, idx);
756 | }
757 |
758 | const void *(lua_topointer) (lua_State *ls, int idx)
759 | {
760 | LRET(lua_topointer, ls, idx);
761 | }
762 |
763 |
764 |
765 | /*
766 | ** push functions (C -> stack)
767 | */
768 | void (lua_pushnil) (lua_State *ls)
769 | {
770 | LCALL(lua_pushnil, ls);
771 | }
772 |
773 | void (lua_pushnumber) (lua_State *ls, lua_Number n)
774 | {
775 | LCALL(lua_pushnumber, ls, n);
776 | }
777 |
778 | void (lua_pushinteger) (lua_State *ls, lua_Integer n)
779 | {
780 | LCALL(lua_pushinteger, ls, n);
781 | }
782 |
783 | void (lua_pushlstring) (lua_State *ls, const char *s, size_t l)
784 | {
785 | LCALL(lua_pushlstring, ls, s, l);
786 | }
787 |
788 | void (lua_pushstring) (lua_State *ls, const char *s)
789 | {
790 | LCALL(lua_pushstring, ls, s);
791 | }
792 |
793 | const char *(lua_pushvfstring) (lua_State *ls, const char *fmt, va_list argp)
794 | {
795 | LRET(lua_pushvfstring, ls, fmt, argp);
796 | }
797 |
798 | const char *(lua_pushfstring) (lua_State *ls, const char *fmt, ...)
799 | {
800 | char buffer[1024];
801 | va_list vl;
802 |
803 | va_start(vl, fmt);
804 | vsnprintf(buffer, 1024, fmt, vl);
805 | va_end(vl);
806 |
807 | LRET(lua_pushfstring, ls, buffer);
808 | }
809 |
810 | void (lua_pushcclosure) (lua_State *ls, lua_CFunction fn, int n)
811 | {
812 | LCALL(lua_pushcclosure, ls, fn, n);
813 | }
814 |
815 | void (lua_pushboolean) (lua_State *ls, int b)
816 | {
817 | LCALL(lua_pushboolean, ls, b);
818 | }
819 |
820 | void (lua_pushlightuserdata) (lua_State *ls, void *p)
821 | {
822 | LCALL(lua_pushlightuserdata, ls, p);
823 | }
824 |
825 | int (lua_pushthread) (lua_State *ls)
826 | {
827 | LRET(lua_pushthread, ls);
828 | }
829 |
830 |
831 |
832 | /*
833 | ** get functions (Lua -> stack)
834 | */
835 | void (lua_gettable) (lua_State *ls, int idx)
836 | {
837 | LCALL(lua_gettable, ls, idx);
838 | }
839 |
840 | void (lua_getfield) (lua_State *ls, int idx, const char *k)
841 | {
842 | LCALL(lua_getfield, ls, idx, k);
843 | }
844 |
845 | void (lua_rawget) (lua_State *ls, int idx)
846 | {
847 | LCALL(lua_rawget, ls, idx);
848 | }
849 |
850 | void (lua_rawgeti) (lua_State *ls, int idx, int n)
851 | {
852 | LCALL(lua_rawgeti, ls, idx, n);
853 | }
854 |
855 | void (lua_createtable) (lua_State *ls, int narr, int nrec)
856 | {
857 | LCALL(lua_createtable, ls, narr, nrec);
858 | }
859 |
860 | void *(lua_newuserdata) (lua_State *ls, size_t sz)
861 | {
862 | LRET(lua_newuserdata, ls, sz);
863 | }
864 |
865 | int (lua_getmetatable) (lua_State *ls, int objindex)
866 | {
867 | LRET(lua_getmetatable, ls, objindex);
868 | }
869 |
870 | void (lua_getfenv) (lua_State *ls, int idx)
871 | {
872 | LCALL(lua_getfenv, ls, idx);
873 | }
874 |
875 |
876 | /*
877 | ** set functions (stack -> Lua)
878 | */
879 | void (lua_settable) (lua_State *ls, int idx)
880 | {
881 | LCALL(lua_settable, ls, idx);
882 | }
883 |
884 | void (lua_setfield) (lua_State *ls, int idx, const char *k)
885 | {
886 | LCALL(lua_setfield, ls, idx, k);
887 | }
888 |
889 | void (lua_rawset) (lua_State *ls, int idx)
890 | {
891 | LCALL(lua_rawset, ls, idx);
892 | }
893 |
894 | void (lua_rawseti) (lua_State *ls, int idx, int n)
895 | {
896 | LCALL(lua_rawseti, ls, idx, n);
897 | }
898 |
899 | int (lua_setmetatable) (lua_State *ls, int objindex)
900 | {
901 | LRET(lua_setmetatable, ls, objindex);
902 | }
903 |
904 | int (lua_setfenv) (lua_State *ls, int idx)
905 | {
906 | LRET(lua_setfenv, ls, idx);
907 | }
908 |
909 |
910 |
911 | /*
912 | ** `load' and `call' functions (load and run Lua code)
913 | */
914 | void (lua_call) (lua_State *ls, int nargs, int nresults)
915 | {
916 | LCALL(lua_call, ls, nargs, nresults);
917 | }
918 |
919 | int (lua_pcall) (lua_State *ls, int nargs, int nresults, int errfunc)
920 | {
921 | LRET(lua_pcall, ls, nargs, nresults, errfunc);
922 | }
923 |
924 | int (lua_cpcall) (lua_State *ls, lua_CFunction func, void *ud)
925 | {
926 | LRET(lua_cpcall, ls, func, ud);
927 | }
928 |
929 | int (lua_load) (lua_State *ls, lua_Reader reader, void *dt, const char *chunkname)
930 | {
931 | LRET(lua_load, ls, reader, dt, chunkname);
932 | }
933 |
934 | int (lua_dump) (lua_State *ls, lua_Writer writer, void *data)
935 | {
936 | LRET(lua_dump, ls, writer, data);
937 | }
938 |
939 |
940 |
941 | /*
942 | ** coroutine functions
943 | */
944 | int (lua_yield) (lua_State *ls, int nresults)
945 | {
946 | LRET(lua_yield, ls, nresults);
947 | }
948 |
949 | int (lua_resume) (lua_State *ls, int narg)
950 | {
951 | LRET(lua_resume, ls, narg);
952 | }
953 |
954 | int (lua_status) (lua_State *ls)
955 | {
956 | LRET(lua_status, ls);
957 | }
958 |
959 |
960 | /*
961 | ** garbage-collection function and options
962 | */
963 | int (lua_gc) (lua_State *ls, int what, int data)
964 | {
965 | LRET(lua_gc, ls, what, data);
966 | }
967 |
968 |
969 | /*
970 | ** miscellaneous functions
971 | */
972 |
973 | int (lua_error) (lua_State *ls)
974 | {
975 | LRET(lua_error, ls);
976 | }
977 |
978 |
979 | int (lua_next) (lua_State *ls, int idx)
980 | {
981 | LRET(lua_next, ls, idx);
982 | }
983 |
984 |
985 | void (lua_concat) (lua_State *ls, int n)
986 | {
987 | LCALL(lua_concat, ls, n);
988 | }
989 |
990 |
991 | lua_Alloc (lua_getallocf) (lua_State *ls, void **ud)
992 | {
993 | LRET(lua_getallocf, ls, ud);
994 | }
995 |
996 | void lua_setallocf (lua_State *ls, lua_Alloc f, void *ud)
997 | {
998 | LCALL(lua_setallocf, ls, f, ud);
999 | }
1000 |
1001 |
1002 | /* Functions to be called by the debuger in specific events */
1003 | int lua_getstack (lua_State *ls, int level, lua_Debug *ar)
1004 | {
1005 | LRET(lua_getstack, ls, level, ar);
1006 | }
1007 |
1008 | int lua_getinfo (lua_State *ls, const char *what, lua_Debug *ar)
1009 | {
1010 | LRET(lua_getinfo, ls, what, ar);
1011 | }
1012 |
1013 | const char *lua_getlocal (lua_State *ls, const lua_Debug *ar, int n)
1014 | {
1015 | LRET(lua_getlocal, ls, ar, n);
1016 | }
1017 |
1018 | const char *lua_setlocal (lua_State *ls, const lua_Debug *ar, int n)
1019 | {
1020 | LRET(lua_setlocal, ls, ar, n);
1021 | }
1022 |
1023 | const char *lua_getupvalue (lua_State *ls, int funcindex, int n)
1024 | {
1025 | LRET(lua_getupvalue, ls, funcindex, n);
1026 | }
1027 |
1028 | const char *lua_setupvalue (lua_State *ls, int funcindex, int n)
1029 | {
1030 | LRET(lua_setupvalue, ls, funcindex, n);
1031 | }
1032 |
1033 |
1034 | int lua_sethook (lua_State *ls, lua_Hook func, int mask, int count)
1035 | {
1036 | LRET(lua_sethook, ls, func, mask, count);
1037 | }
1038 |
1039 | lua_Hook lua_gethook (lua_State *ls)
1040 | {
1041 | LRET(lua_gethook, ls);
1042 | }
1043 |
1044 | int lua_gethookmask (lua_State *ls)
1045 | {
1046 | LRET(lua_gethookmask, ls);
1047 | }
1048 |
1049 | int lua_gethookcount (lua_State *ls)
1050 | {
1051 | LRET(lua_gethookcount, ls);
1052 | }
1053 |
1054 |
1055 | /**
1056 | ** Lua auxlib
1057 | **/
1058 | #if defined(LUA_COMPAT_GETN)
1059 | int (luaL_getn) (lua_State *ls, int t)
1060 | {
1061 | LRET(luaL_getn, ls, t);
1062 | }
1063 |
1064 | void (luaL_setn) (lua_State *ls, int t, int n)
1065 | {
1066 | LRET(luaL_setn, ls, t, n);
1067 | }
1068 | #endif
1069 |
1070 | void (luaL_register) (lua_State *ls, const char *libname, const luaL_Reg *l)
1071 | {
1072 | LCALL(luaL_register, ls, libname, l);
1073 | }
1074 |
1075 | int (luaL_getmetafield) (lua_State *ls, int obj, const char *e)
1076 | {
1077 | LRET(luaL_getmetafield, ls, obj, e);
1078 | }
1079 |
1080 | int (luaL_callmeta) (lua_State *ls, int obj, const char *e)
1081 | {
1082 | LRET(luaL_callmeta, ls, obj, e);
1083 | }
1084 |
1085 | int (luaL_typerror) (lua_State *ls, int narg, const char *tname)
1086 | {
1087 | LRET(luaL_typerror, ls, narg, tname);
1088 | }
1089 |
1090 | int (luaL_argerror) (lua_State *ls, int numarg, const char *extramsg)
1091 | {
1092 | LRET(luaL_argerror, ls, numarg, extramsg);
1093 | }
1094 |
1095 | const char *(luaL_checklstring) (lua_State *ls, int numArg, size_t *l)
1096 | {
1097 | LRET(luaL_checklstring, ls, numArg, l);
1098 | }
1099 |
1100 | const char *(luaL_optlstring) (lua_State *ls, int numArg, const char *def, size_t *l)
1101 | {
1102 | LRET(luaL_optlstring, ls, numArg, def, l);
1103 | }
1104 |
1105 | lua_Number (luaL_checknumber) (lua_State *ls, int numArg)
1106 | {
1107 | LRET(luaL_checknumber, ls, numArg);
1108 | }
1109 |
1110 | lua_Number (luaL_optnumber) (lua_State *ls, int nArg, lua_Number def)
1111 | {
1112 | LRET(luaL_optnumber, ls, nArg, def);
1113 | }
1114 |
1115 |
1116 | lua_Integer (luaL_checkinteger) (lua_State *ls, int numArg)
1117 | {
1118 | LRET(luaL_checkinteger, ls, numArg);
1119 | }
1120 |
1121 | lua_Integer (luaL_optinteger) (lua_State *ls, int nArg, lua_Integer def)
1122 | {
1123 | LRET(luaL_optinteger, ls, nArg, def);
1124 | }
1125 |
1126 |
1127 | void (luaL_checkstack) (lua_State *ls, int sz, const char *msg)
1128 | {
1129 | LRET(luaL_checkstack, ls, sz, msg);
1130 | }
1131 |
1132 | void (luaL_checktype) (lua_State *ls, int narg, int t)
1133 | {
1134 | LCALL(luaL_checktype, ls, narg, t);
1135 | }
1136 |
1137 | void (luaL_checkany) (lua_State *ls, int narg)
1138 | {
1139 | LCALL(luaL_checkany, ls, narg);
1140 | }
1141 |
1142 |
1143 | int (luaL_newmetatable) (lua_State *ls, const char *tname)
1144 | {
1145 | LRET(luaL_newmetatable, ls, tname);
1146 | }
1147 |
1148 | void *(luaL_checkudata) (lua_State *ls, int ud, const char *tname)
1149 | {
1150 | LRET(luaL_checkudata, ls, ud, tname);
1151 | }
1152 |
1153 |
1154 | void (luaL_where) (lua_State *ls, int lvl)
1155 | {
1156 | LCALL(luaL_where, ls, lvl);
1157 | }
1158 |
1159 | int (luaL_error) (lua_State *ls, const char *fmt, ...)
1160 | {
1161 | char buffer[1024];
1162 | va_list vl;
1163 |
1164 | va_start(vl, fmt);
1165 | vsnprintf(buffer, 1024, fmt, vl);
1166 | va_end(vl);
1167 |
1168 | LRET(luaL_error, ls, buffer);
1169 | }
1170 |
1171 |
1172 | int (luaL_checkoption) (lua_State *ls, int narg, const char *def, const char *const lst[])
1173 | {
1174 | LRET(luaL_checkoption, ls, narg, def, lst);
1175 | }
1176 |
1177 |
1178 | int (luaL_ref) (lua_State *ls, int t)
1179 | {
1180 | LRET(luaL_ref, ls, t);
1181 | }
1182 |
1183 | void (luaL_unref) (lua_State *ls, int t, int ref)
1184 | {
1185 | LCALL(luaL_unref, ls, t, ref);
1186 | }
1187 |
1188 |
1189 | int (luaL_loadfile) (lua_State *ls, const char *filename)
1190 | {
1191 | LRET(luaL_loadfile, ls, filename);
1192 | }
1193 |
1194 | int (luaL_loadbuffer) (lua_State *ls, const char *buff, size_t sz, const char *name)
1195 | {
1196 | LRET(luaL_loadbuffer, ls, buff, sz, name);
1197 | }
1198 |
1199 | int (luaL_loadstring) (lua_State *ls, const char *s)
1200 | {
1201 | LRET(luaL_loadstring, ls, s);
1202 | }
1203 |
1204 |
1205 | lua_State *(luaL_newstate) (void)
1206 | {
1207 | LRET(luaL_newstate);
1208 | }
1209 |
1210 |
1211 |
1212 | const char *(luaL_gsub) (lua_State *ls, const char *s, const char *p, const char *r)
1213 | {
1214 | LRET(luaL_gsub, ls, s, p, r);
1215 | }
1216 |
1217 |
1218 | const char *(luaL_findtable) (lua_State *ls, int idx, const char *fname, int szhint)
1219 | {
1220 | LRET(luaL_findtable, ls, idx, fname, szhint);
1221 | }
1222 |
1223 |
1224 | void (luaL_buffinit) (lua_State *ls, luaL_Buffer *B)
1225 | {
1226 | LCALL(luaL_buffinit, ls, B);
1227 | }
1228 |
1229 | char *(luaL_prepbuffer) (luaL_Buffer *B)
1230 | {
1231 | LRET(luaL_prepbuffer, B);
1232 | }
1233 |
1234 | void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l)
1235 | {
1236 | LCALL(luaL_addlstring, B, s, l);
1237 | }
1238 |
1239 | void (luaL_addstring) (luaL_Buffer *B, const char *s)
1240 | {
1241 | LCALL(luaL_addstring, B, s);
1242 | }
1243 |
1244 | void (luaL_addvalue) (luaL_Buffer *B)
1245 | {
1246 | LCALL(luaL_addvalue, B);
1247 | }
1248 |
1249 | void (luaL_pushresult) (luaL_Buffer *B)
1250 | {
1251 | LCALL(luaL_pushresult, B);
1252 | }
1253 |
1254 | lua_State* (lua_getmainstate) (lua_State* L)
1255 | {
1256 | LRET(lua_getmainstate, L);
1257 | }
1258 |
1259 | #ifdef __cplusplus
1260 | }
1261 | #endif
1262 |
1263 | #undef LRET
1264 | #undef LCALL
1265 |
1266 |
--------------------------------------------------------------------------------
/module/luaimports/luaimports.linux.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008, Alberto Alonso
3 | *
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * * Redistributions of source code must retain the above copyright notice, this
10 | * list of conditions and the following disclaimer.
11 | * * Redistributions in binary form must reproduce the above copyright notice, this
12 | * list of conditions and the following disclaimer in the documentation and/or other
13 | * materials provided with the distribution.
14 | * * Neither the name of the mta-mysql nor the names of its contributors may be used
15 | * to endorse or promote products derived from this software without specific prior
16 | * written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 |
31 | #if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
32 | #define ImportLua() true
33 | #else
34 | #ifdef __cplusplus
35 | extern "C"
36 | #endif
37 | bool ImportLua();
38 | #endif
39 |
--------------------------------------------------------------------------------
/module/premake5.lua:
--------------------------------------------------------------------------------
1 | project "module"
2 | language "C++"
3 | cppdialect "C++14"
4 | kind "SharedLib"
5 | targetname "ml_pathfind"
6 |
7 | includedirs { "include" }
8 | libdirs { "lib" }
9 |
10 | links { "pathfind" }
11 |
12 | vpaths {
13 | ["Headers/*"] = "**.h",
14 | ["Sources/*"] = "**.cpp",
15 | ["*"] = "premake5.lua"
16 | }
17 |
18 | files {
19 | "premake5.lua",
20 | "**.cpp",
21 | "**.h"
22 | }
23 |
24 | filter { "system:windows", "platforms:x86" }
25 | links { "lua5.1.lib" }
26 |
27 | filter { "system:windows", "platforms:x64" }
28 | links { "lua5.1_64.lib" }
29 |
30 | filter "system:not linux"
31 | excludes { "luaimports/luaimports.linux.h", "luaimports/luaimports.linux.cpp" }
32 |
--------------------------------------------------------------------------------
/pathfind/AStar.cpp:
--------------------------------------------------------------------------------
1 | #include "AStar.h"
2 | #include "Graph.h"
3 | #include
4 | #include
5 |
6 | namespace pathfind
7 | {
8 |
9 | AStar::AStar(Graph* graph, GraphNode* from, GraphNode* to) : _graph(graph), _from(from), _to(to)
10 | {
11 | }
12 |
13 | AStar::AStar(Graph* graph, const Vector3& from, const Vector3& to)
14 | : AStar(graph, graph->FindClosestNodeTo(from), graph->FindClosestNodeTo(to))
15 | {
16 | }
17 |
18 | AStarResult AStar::CalculateShortestPath()
19 | {
20 | // Add the node we start from to the to-traverse list
21 | _openSet.insert(_from);
22 |
23 | // Cache for calculated g scores (calculated costs to )
24 | _gScore[_from] = 0; // Costs for the from node are obviously 0
25 |
26 | // Cache for the calculated heuristics
27 | _fScore[_from] = HeuristicCostEstimate(_from, _to);
28 |
29 | // Traverse graph (and apply A*)
30 | while (!_openSet.empty())
31 | {
32 | GraphNode* current = GetMinimumInOpenSet();
33 | if (current == _to)
34 | return ReconstructPath();
35 |
36 | // We found a new node, so remove from open list and add to closed list
37 | _openSet.erase(current);
38 | _closedSet.insert(current);
39 |
40 | for (auto& neighbor : current->edges)
41 | {
42 | GraphNode* neighborNode = neighbor.first;
43 | std::size_t neighborDistance = neighbor.second;
44 |
45 | // If neighbor is already traversed, ignore this
46 | if (_closedSet.find(neighborNode) != _closedSet.end())
47 | continue;
48 |
49 | // Calculate tentative g score
50 | auto tentativeGScore = _gScore[current] + neighborDistance;
51 | if (_openSet.find(neighborNode) == _openSet.end())
52 | _openSet.insert(neighborNode);
53 | else if (tentativeGScore >= _gScore[neighborNode])
54 | continue;
55 |
56 | // We found the best path so far
57 | _cameFrom[neighborNode] = current;
58 | _gScore[neighborNode] = tentativeGScore;
59 | _fScore[neighborNode] = static_cast(_gScore[neighborNode]) + HeuristicCostEstimate(neighborNode, _to);
60 | }
61 | }
62 |
63 | return {};
64 | }
65 |
66 | float AStar::HeuristicCostEstimate(GraphNode* from, GraphNode* to)
67 | {
68 | return (to->position - from->position).Length();
69 | }
70 |
71 | GraphNode* AStar::GetMinimumInOpenSet()
72 | {
73 | GraphNode* minNode = nullptr;
74 | float minDist = std::numeric_limits::infinity();
75 |
76 | for (GraphNode* node : _openSet)
77 | {
78 | float fScore = _fScore[node];
79 |
80 | if (fScore < minDist)
81 | {
82 | minDist = fScore;
83 | minNode = node;
84 | }
85 | }
86 |
87 | return minNode;
88 | }
89 |
90 | std::vector AStar::ReconstructPath()
91 | {
92 | std::vector path;
93 |
94 | GraphNode* parent = _to;
95 | while (parent != _from)
96 | {
97 | path.push_back(parent);
98 | parent = _cameFrom[parent];
99 | }
100 |
101 | path.push_back(_from);
102 |
103 | // Reverse path
104 | std::reverse(path.begin(), path.end());
105 |
106 | return std::move(path);
107 | }
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/pathfind/AStar.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 | #include "Vector3.h"
6 |
7 | namespace pathfind
8 | {
9 | class Graph;
10 | struct GraphNode;
11 |
12 | using AStarResult = std::vector;
13 |
14 | class AStar
15 | {
16 | public:
17 | AStar(Graph* graph, GraphNode* from, GraphNode* to);
18 | AStar(Graph* graph, const Vector3& from, const Vector3& to);
19 |
20 | AStarResult CalculateShortestPath();
21 |
22 | protected:
23 | float HeuristicCostEstimate(GraphNode* from, GraphNode* to);
24 |
25 | GraphNode* GetMinimumInOpenSet();
26 |
27 | std::vector ReconstructPath();
28 |
29 | private:
30 | Graph* _graph;
31 | GraphNode* _to;
32 | GraphNode* _from;
33 |
34 | std::unordered_set _closedSet;
35 | std::unordered_set _openSet;
36 |
37 | std::unordered_map _gScore;
38 | std::unordered_map _fScore;
39 |
40 | std::unordered_map _cameFrom;
41 | };
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/pathfind/Graph.cpp:
--------------------------------------------------------------------------------
1 | #include "Graph.h"
2 | #include
3 |
4 | namespace pathfind
5 | {
6 |
7 | Graph::Graph()
8 | {
9 |
10 | }
11 |
12 | Graph::~Graph()
13 | {
14 |
15 | }
16 |
17 | GraphNode* Graph::FindClosestNodeTo(const Vector3& position)
18 | {
19 | // Iterate through all nodes and calculate the minimum distance
20 | // TODO: Optimize this with index structures
21 | GraphNode* closestNode = nullptr;
22 | float closestDist = std::numeric_limits::infinity();
23 |
24 | for (GraphNode& node : _nodes)
25 | {
26 | float dist = (node.position - position).LengthSquared();
27 | if (dist < closestDist)
28 | {
29 | closestNode = &node;
30 | closestDist = dist;
31 | }
32 | }
33 |
34 | return closestNode;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/pathfind/Graph.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include "Vector3.h"
5 |
6 | namespace pathfind
7 | {
8 | using GraphNodeId = std::size_t;
9 |
10 | struct GraphNode
11 | {
12 | GraphNodeId id;
13 | Vector3 position;
14 | std::vector> edges;
15 | };
16 |
17 | class Graph
18 | {
19 | friend class GraphReader;
20 |
21 | public:
22 | Graph();
23 | ~Graph();
24 |
25 | GraphNode* FindClosestNodeTo(const Vector3& position);
26 | inline GraphNode* GetNode(unsigned int id) {
27 | if (id >= _nodes.size()) {
28 | return nullptr;
29 | }
30 |
31 | return &_nodes[id];
32 | };
33 |
34 | private:
35 | std::vector _nodes;
36 | };
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/pathfind/GraphReader.cpp:
--------------------------------------------------------------------------------
1 | #include "GraphReader.h"
2 | #include
3 | #include
4 | #include "Graph.h"
5 |
6 | namespace pathfind
7 | {
8 |
9 | GraphReader::GraphReader(const std::string& path) : _path(path)
10 | {
11 | }
12 |
13 | std::unique_ptr GraphReader::Read()
14 | {
15 | // Load json file
16 | nlohmann::json json = nlohmann::json::parse(std::ifstream(_path));
17 |
18 | // Create graph from nodes
19 | auto graph = std::make_unique();
20 | for (auto& node : json)
21 | {
22 | graph->_nodes.push_back(GraphNode{ node["id"], Vector3(node["x"], node["y"], node["z"]), {} });
23 | }
24 |
25 | // Fill in edge pointers
26 | std::size_t i = 0;
27 | for (auto& node : json)
28 | {
29 | auto& edges = graph->_nodes[i++].edges;
30 |
31 | for (auto& edge : node["edges"])
32 | {
33 | auto edgeIndex = edge[0].get();
34 | auto distance = edge[1].get();
35 |
36 | edges.push_back({ &graph->_nodes[edgeIndex], distance });
37 | }
38 | }
39 |
40 | return graph;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/pathfind/GraphReader.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 |
5 | namespace pathfind
6 | {
7 |
8 | class Graph;
9 |
10 | class GraphReader
11 | {
12 | public:
13 | GraphReader(const std::string& path);
14 |
15 | std::unique_ptr Read();
16 |
17 | private:
18 | std::string _path;
19 | };
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/pathfind/Vector3.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | class Vector3
5 | {
6 | public:
7 | Vector3(float x, float y, float z) : _x(x), _y(y), _z(z)
8 | {}
9 |
10 | inline auto GetX() { return _x; }
11 | inline auto GetY() { return _y; }
12 | inline auto GetZ() { return _z; }
13 |
14 | inline Vector3 operator-(const Vector3& other) const
15 | {
16 | return Vector3(_x - other._x, _y - other._y, _z - other._z);
17 | }
18 |
19 | inline float Length()
20 | {
21 | return std::sqrt(_x * _x + _y * _y + _z * _z);
22 | }
23 |
24 | inline float LengthSquared()
25 | {
26 | return _x * _x + _y * _y + _z * _z;
27 | }
28 |
29 | private:
30 | float _x, _y, _z;
31 | };
32 |
--------------------------------------------------------------------------------
/pathfind/premake5.lua:
--------------------------------------------------------------------------------
1 | project "pathfind"
2 | language "C++"
3 | cppdialect "C++14"
4 | kind "StaticLib"
5 |
6 | vpaths {
7 | ["Headers/*"] = "**.h",
8 | ["Sources/*"] = "**.cpp",
9 | ["*"] = "premake5.lua"
10 | }
11 |
12 | files {
13 | "premake5.lua",
14 | "**.cpp",
15 | "**.h"
16 | }
17 |
--------------------------------------------------------------------------------
/premake5.lua:
--------------------------------------------------------------------------------
1 | solution "PathFind"
2 | configurations { "Debug", "Release" }
3 | location ( "Build" )
4 | startproject "test"
5 | targetdir "Bin/%{cfg.buildcfg}"
6 |
7 | platforms { "x86", "x64" }
8 | pic "On"
9 | symbols "On"
10 |
11 | includedirs { "vendor", "." }
12 |
13 | filter "system:windows"
14 | defines { "WINDOWS", "WIN32" }
15 |
16 | filter "configurations:Debug"
17 | defines { "DEBUG" }
18 |
19 | filter "configurations:Release"
20 | flags { "Optimize" }
21 |
22 | include "module"
23 | include "pathfind"
24 | include "test"
25 |
--------------------------------------------------------------------------------
/test/Main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | int main(int argc, char* argv[])
9 | {
10 | // Read graph from file
11 | std::cout << "Loading graph from json file..." << std::endl;
12 | pathfind::GraphReader graphReader("../test/sa_nodes.json");
13 | auto start = std::chrono::system_clock::now();
14 | std::unique_ptr graph = graphReader.Read();
15 | std::cout << "Done! (Took " << std::chrono::duration_cast(std::chrono::system_clock::now() - start).count() << "ms)" << std::endl;
16 |
17 | // Create instance of AStar
18 | start = std::chrono::system_clock::now();
19 | pathfind::AStar pathfind(graph.get(), Vector3(-2427.625, -2474.75, 35.75), Vector3(0.0f, 0.0f, 0.0f));
20 | std::cout << "Found a start node in " << std::chrono::duration_cast(std::chrono::system_clock::now() - start).count() << "micro s" << std::endl;
21 |
22 | // Run algorithm
23 | start = std::chrono::system_clock::now();
24 | std::vector path = pathfind.CalculateShortestPath();
25 | std::cout << "Found a route in " << std::chrono::duration_cast(std::chrono::system_clock::now() - start).count() << "ms" << std::endl;
26 |
27 | // Print path
28 | std::cout << "Calculating path: \n";
29 | for (pathfind::GraphNode* node : path)
30 | {
31 | std::cout << "Node " << node->id << " (" << node->position.GetX() << ", " << node->position.GetY() << ", " << node->position.GetZ() << ")\n";
32 | }
33 |
34 | if (path.empty())
35 | std::cout << "No path found";
36 |
37 | std::cout << std::endl;
38 |
39 | std::cin.get();
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/test/premake5.lua:
--------------------------------------------------------------------------------
1 | project "test"
2 | language "C++"
3 | cppdialect "C++14"
4 | kind "ConsoleApp"
5 |
6 | links { "pathfind" }
7 |
8 | vpaths {
9 | ["Headers/*"] = "**.h",
10 | ["Sources/*"] = "**.cpp",
11 | ["Test files"] = "**.json",
12 | ["*"] = "premake5.lua"
13 | }
14 |
15 | files {
16 | "premake5.lua",
17 | "**.cpp",
18 | "**.h",
19 |
20 | "testnodes1.json"
21 | }
22 |
--------------------------------------------------------------------------------
/test/testnodes1.json:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": [
3 | { "id": 0, "x": -2427.625, "y": -2474.75, "z": 35.75, "edges": [[1, 45], [2, 90]] },
4 | { "id": 1, "x": 0, "y": 0, "z": 0, "edges": [[0, 45]] },
5 | { "id": 2, "x": 0, "y": 0, "z": 0, "edges": [[0, 45]] }
6 | ]
7 | }
--------------------------------------------------------------------------------
/utils/premake5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/utils/premake5
--------------------------------------------------------------------------------
/utils/premake5-macos:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/utils/premake5-macos
--------------------------------------------------------------------------------
/utils/premake5.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eXo-OpenSource/ml_pathfind/60627b561d42cb17d898387a67665260c78e3fcf/utils/premake5.exe
--------------------------------------------------------------------------------