├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── pawn.json ├── pawno └── include │ └── YALP.inc └── plugins ├── YALP.def ├── YALP.sln ├── YALP.vcxproj ├── YALP.vcxproj.filters ├── lib ├── lua │ ├── lapi.cpp │ ├── lapi.h │ ├── lauxlib.cpp │ ├── lauxlib.h │ ├── lbaselib.cpp │ ├── lbitlib.cpp │ ├── lcode.cpp │ ├── lcode.h │ ├── lcorolib.cpp │ ├── lctype.cpp │ ├── lctype.h │ ├── ldblib.cpp │ ├── ldebug.cpp │ ├── ldebug.h │ ├── ldo.cpp │ ├── ldo.h │ ├── ldump.cpp │ ├── lfunc.cpp │ ├── lfunc.h │ ├── lgc.cpp │ ├── lgc.h │ ├── linit.cpp │ ├── liolib.cpp │ ├── llex.cpp │ ├── llex.h │ ├── llimits.h │ ├── lmathlib.cpp │ ├── lmem.cpp │ ├── lmem.h │ ├── loadlib.cpp │ ├── lobject.cpp │ ├── lobject.h │ ├── lopcodes.cpp │ ├── lopcodes.h │ ├── loslib.cpp │ ├── lparser.cpp │ ├── lparser.h │ ├── lprefix.h │ ├── lstate.cpp │ ├── lstate.h │ ├── lstring.cpp │ ├── lstring.h │ ├── lstrlib.cpp │ ├── ltable.cpp │ ├── ltable.h │ ├── ltablib.cpp │ ├── ltm.cpp │ ├── ltm.h │ ├── lua.h │ ├── lua.hpp │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.cpp │ ├── lundump.h │ ├── lutf8lib.cpp │ ├── lvm.cpp │ ├── lvm.h │ ├── lzio.cpp │ └── lzio.h ├── sdk │ ├── amx │ │ ├── amx.h │ │ ├── getch.h │ │ └── sclinux.h │ ├── amxplugin.cpp │ ├── plugin.h │ └── plugincommon.h └── subhook │ ├── subhook.c │ ├── subhook.h │ ├── subhook_linux.h │ ├── subhook_private.h │ ├── subhook_unix.h │ ├── subhook_windows.h │ └── subhook_x86.h ├── makefile └── src ├── amx ├── amxutils.cpp ├── amxutils.h ├── fileutils.cpp ├── fileutils.h ├── loader.cpp └── loader.h ├── fixes └── linux.h ├── hooks.cpp ├── hooks.h ├── lua ├── interop.cpp ├── interop.h ├── interop │ ├── file.cpp │ ├── file.h │ ├── memory.cpp │ ├── memory.h │ ├── native.cpp │ ├── native.h │ ├── public.cpp │ ├── public.h │ ├── pubvar.cpp │ ├── pubvar.h │ ├── result.cpp │ ├── result.h │ ├── sleep.cpp │ ├── sleep.h │ ├── string.cpp │ ├── string.h │ ├── tags.cpp │ └── tags.h ├── lualibs.h ├── remote.cpp ├── remote.h ├── timer.cpp └── timer.h ├── lua_adapt.cpp ├── lua_adapt.h ├── lua_api.cpp ├── lua_api.h ├── lua_utils.cpp ├── lua_utils.h ├── main.cpp ├── main.h ├── natives.cpp ├── natives.h └── utils ├── id_set_pool.h ├── linear_pool.h ├── optional.h └── shared_id_set_pool.h /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pwn linguist-language=Pawn 2 | *.inc linguist-language=Pawn 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Linux stuff 2 | *.so 3 | *.o 4 | 5 | ## Ignore Visual Studio temporary files, build results, and 6 | ## files generated by popular Visual Studio add-ons. 7 | 8 | # User-specific files 9 | *.suo 10 | *.user 11 | *.sln.docstates 12 | *.db 13 | *.opendb 14 | *.amx 15 | rus.txt 16 | 17 | # Build results 18 | [Dd]ebug/ 19 | [Dd]ebugPublic/ 20 | [Rr]elease/ 21 | x64/ 22 | build/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | 27 | # MSTest test Results 28 | [Tt]est[Rr]esult*/ 29 | [Bb]uild[Ll]og.* 30 | 31 | #NUNIT 32 | *.VisualState.xml 33 | TestResult.xml 34 | 35 | # Build Results of an ATL Project 36 | [Dd]ebugPS/ 37 | [Rr]eleasePS/ 38 | dlldata.c 39 | 40 | *_i.c 41 | *_p.c 42 | *_i.h 43 | *.ilk 44 | *.meta 45 | *.obj 46 | *.pch 47 | *.pdb 48 | *.pgc 49 | *.pgd 50 | *.rsp 51 | *.sbr 52 | *.tlb 53 | *.tli 54 | *.tlh 55 | *.tmp 56 | *.tmp_proj 57 | *.log 58 | *.vspscc 59 | *.vssscc 60 | .builds 61 | *.pidb 62 | *.svclog 63 | *.scc 64 | 65 | # Chutzpah Test files 66 | _Chutzpah* 67 | 68 | # Visual C++ cache files 69 | ipch/ 70 | *.aps 71 | *.ncb 72 | *.opensdf 73 | *.sdf 74 | *.cachefile 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | *.vspx 80 | 81 | # TFS 2012 Local Workspace 82 | $tf/ 83 | 84 | # Guidance Automation Toolkit 85 | *.gpState 86 | 87 | # ReSharper is a .NET coding add-in 88 | _ReSharper*/ 89 | *.[Rr]e[Ss]harper 90 | *.DotSettings.user 91 | 92 | # JustCode is a .NET coding addin-in 93 | .JustCode 94 | 95 | # TeamCity is a build add-in 96 | _TeamCity* 97 | 98 | # DotCover is a Code Coverage Tool 99 | *.dotCover 100 | 101 | # NCrunch 102 | *.ncrunch* 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | 133 | # NuGet Packages Directory 134 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 135 | #packages/* 136 | ## TODO: If the tool you use requires repositories.config, also uncomment the next line 137 | #!packages/repositories.config 138 | 139 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 140 | # This line needs to be after the ignore of the build folder (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file to a newer 167 | # Visual Studio version. Backup files are not needed, because we have git ;-) 168 | _UpgradeReport_Files/ 169 | Backup*/ 170 | UpgradeLog*.XML 171 | UpgradeLog*.htm 172 | 173 | # SQL Server files 174 | *.mdf 175 | *.ldf 176 | 177 | # Business Intelligence projects 178 | *.rdl.data 179 | *.bim.layout 180 | *.bim_*.settings 181 | 182 | # Microsoft Fakes 183 | FakesAssemblies/ 184 | 185 | # ========================= 186 | # Windows detritus 187 | # ========================= 188 | 189 | # Windows image file caches 190 | Thumbs.db 191 | ehthumbs.db 192 | 193 | # Folder config file 194 | Desktop.ini 195 | 196 | # Recycle Bin used on file shares 197 | $RECYCLE.BIN/ 198 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 IllidanS4 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 | YALP v1.1 2 | ========== 3 | 4 | _YALP_, short for _Yet Another Lua Plugin_, aims to be a simple yet extendable SA-MP plugin allowing to use [Lua](https://www.lua.org/) for SA-MP server programming. 5 | 6 | Compared to older Lua plugins, _YALP_ doesn't export any of the SA-MP natives or callbacks directly. Instead, it exposes a set of functions to interact with the server via a virtual filterscript, mimicking calls to AMX functions. The actual server API can be ported entirely with Lua, which removes the need to update the plugin for new versions of SA-MP and maintain all functions, and also allows the use of other plugins. 7 | 8 | ## Installation 9 | Download the latest [release](//github.com/IllidanS4/YALP/releases/latest) for your platform to the "plugins" directory and add "YALP" (or "YALP.so" on Linux) to the `plugins` line in server.cfg. 10 | 11 | Include [YALP.inc](pawno/include/YALP.inc) to create and control Lua machines on-the-fly. 12 | 13 | ## Building 14 | Use Visual Studio to build the project on Windows, or `make` on Linux. 15 | 16 | ## Example 17 | ```lua 18 | local interop = require "interop" 19 | local native = interop.native 20 | 21 | local commands = {} 22 | 23 | function commands.lua(playerid, params) 24 | -- no need to import native functions, simply call them! 25 | native.SendClientMessage(playerid, -1, "Hello from Lua!") 26 | return true 27 | end 28 | 29 | function commands.setpos(playerid, params) 30 | -- even a function from any plugin can be used 31 | local fail, x, y, z = interop.vacall(native.sscanf, interop.asboolean, params, "fff")(0.0, 0.0, 0.0) 32 | -- return values can be easily specified 33 | if fail then 34 | return native.SendClientMessage(playerid, -1, "Wrong arguments!") 35 | end 36 | native.SetPlayerPos(playerid, x, y, z) 37 | return true 38 | end 39 | 40 | function interop.public.OnPlayerCommandText(playerid, cmdtext) 41 | -- properly convert values to Lua (YALP cannot determine their types) 42 | playerid = interop.asinteger(playerid) 43 | cmdtext = interop.asstring(cmdtext) 44 | 45 | -- take advantage of everything Lua has to offer 46 | local ret 47 | cmdtext:gsub("^/([^ ]+) ?(.*)$", function(cmd, params) 48 | local handler = commands[string.lower(cmd)] 49 | if handler then 50 | ret = handler(playerid, params) 51 | end 52 | end) 53 | return ret 54 | end 55 | ``` 56 | 57 | ## Credits 58 | * [Zeex](//github.com/Zeex) for creating [subhook](//github.com/Zeex/subhook), the backbone of this plugin. 59 | * The creators of Lua. -------------------------------------------------------------------------------- /pawn.json: -------------------------------------------------------------------------------- 1 | { 2 | "user": "IllidanS4", 3 | "repo": "YALP", 4 | "include_path": "pawno/include", 5 | "dependencies": ["sampctl/pawn-stdlib"], 6 | "resources": [ 7 | { 8 | "name": "YALP.dll", 9 | "platform": "windows", 10 | "archive": false 11 | }, 12 | { 13 | "name": "YALP.so", 14 | "platform": "linux", 15 | "archive": false 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /pawno/include/YALP.inc: -------------------------------------------------------------------------------- 1 | /** YALP v1.1.1 by IllidanS4 **/ 2 | //github.com/IllidanS4/YALP 3 | 4 | #if defined _inc_YALP 5 | #undef _inc_YALP 6 | #endif 7 | #if defined _YALP_included 8 | #endinput 9 | #endif 10 | #define _YALP_included 11 | 12 | #include 13 | #include 14 | 15 | enum lua_lib (<<= 1) 16 | { 17 | lua_lib_base = 1, 18 | lua_lib_package, 19 | lua_lib_coroutine, 20 | lua_lib_table, 21 | lua_lib_io, 22 | lua_lib_os, 23 | lua_lib_string, 24 | lua_lib_math, 25 | lua_lib_utf8, 26 | lua_lib_debug, 27 | lua_lib_interop, 28 | lua_lib_timer, 29 | lua_lib_remote, 30 | } 31 | 32 | const lua_lib:lua_baselibs = lua_lib_base | lua_lib_coroutine | lua_lib_table | lua_lib_string | lua_lib_math; 33 | const lua_lib:lua_newlibs = lua_lib_interop | lua_lib_timer | lua_lib_remote; 34 | 35 | enum lua_load_mode (<<= 1) 36 | { 37 | lua_load_text = 1, 38 | lua_load_binary, 39 | } 40 | 41 | native Lua:lua_newstate(lua_lib:load=lua_baselibs, lua_lib:preload=lua_newlibs, memlimit=-1); 42 | native bool:lua_dostring(Lua:L, const str[]); 43 | native bool:lua_close(Lua:L); 44 | native lua_status:lua_load(Lua:L, const reader[], data, bufsize=-1, chunkname[]=""); 45 | 46 | const LUA_MULTRET = -1; 47 | 48 | const lua_status:LUA_OK = lua_status:0; 49 | const lua_status:LUA_YIELD = lua_status:1; 50 | const lua_status:LUA_ERRRUN = lua_status:2; 51 | const lua_status:LUA_ERRSYNTAX = lua_status:3; 52 | const lua_status:LUA_ERRMEM = lua_status:4; 53 | const lua_status:LUA_ERRGCMM = lua_status:5; 54 | const lua_status:LUA_ERRERR = lua_status:6; 55 | const lua_status:LUA_ERRFILE = lua_status:7; 56 | 57 | native lua_status:lua_pcall(Lua:L, nargs, nresults, errfunc=0); 58 | native lua_call(Lua:L, nargs, nresults); 59 | native lua_stackdump(Lua:L, depth=-1); 60 | native lua_tostring(Lua:L, idx, buffer[], size=sizeof(buffer), bool:pack=false); 61 | native lua_bind(Lua:L); 62 | native lua_pushpfunction(Lua:L, const f[]); 63 | native lua_status:lua_loadstream(Lua:L, File:file, const chunkname[], lua_load_mode:mode=lua_load_text); 64 | native LuaLoader:lua_loader(Lua:L, const chunkname[], lua_load_mode:mode=lua_load_text); 65 | native lua_status:lua_write(LuaLoader:stream, const data[], size=-1); 66 | 67 | stock lua_status:lua_loadfile(Lua:L, const name[], lua_load_mode:mode=lua_load_text) 68 | { 69 | new File:file = fopen(name, io_read); 70 | if(!file) return LUA_ERRFILE; 71 | new chunkname[64] = "@"; 72 | strcat(chunkname, name); 73 | new lua_status:err = lua_loadstream(L, file, chunkname, mode); 74 | fclose(file); 75 | return err; 76 | } 77 | 78 | stock lua_status:lua_dofile(Lua:L, const name[], lua_load_mode:mode=lua_load_text) 79 | { 80 | new lua_status:err = lua_loadfile(L, name, mode); 81 | if(err != LUA_OK) return err; 82 | return lua_pcall(L, 0, -1, 0); 83 | } 84 | 85 | const lua_type:LUA_TNONE = lua_type:-1; 86 | const lua_type:LUA_TNIL = lua_type:0; 87 | const lua_type:LUA_TBOOLEAN = lua_type:1; 88 | const lua_type:LUA_TLIGHTUSERDATA = lua_type:2; 89 | const lua_type:LUA_TNUMBER = lua_type:3; 90 | const lua_type:LUA_TSTRING = lua_type:4; 91 | const lua_type:LUA_TTABLE = lua_type:5; 92 | const lua_type:LUA_TFUNCTION = lua_type:6; 93 | const lua_type:LUA_TUSERDATA = lua_type:7; 94 | const lua_type:LUA_TTHREAD = lua_type:8; 95 | 96 | native lua_tointeger(Lua:L, idx); 97 | native Float:lua_tonumber(Lua:L, idx); 98 | native lua_pop(Lua:L, n); 99 | native lua_type:lua_gettable(Lua:L, idx); 100 | native lua_type:lua_getfield(Lua:L, idx, const k[]); 101 | native lua_type:lua_getglobal(Lua:L, const name[]); 102 | native lua_settable(Lua:L, idx); 103 | native lua_setfield(Lua:L, idx, const k[]); 104 | native lua_setglobal(Lua:L, const name[]); 105 | native lua_len(Lua:L, idx); 106 | native lua_pushstring(Lua:L, const s[]); 107 | native lua_pushfstring(Lua:L, const fmt[], ...); 108 | native Pointer:lua_pushuserdata(Lua:L, const data[], size=sizeof(data)); 109 | native lua_getuserdata(Lua:L, idx, data[], size=sizeof(data)); 110 | native lua_setuserdata(Lua:L, idx, const data[], size=sizeof(data)); 111 | 112 | //Directly ported 113 | native Lua:lua_newthread(Lua:L); 114 | 115 | native lua_version(Lua:L); 116 | 117 | native lua_absindex(Lua:L, idx); 118 | native lua_gettop(Lua:L); 119 | native lua_settop(Lua:L, idx); 120 | native lua_pushvalue(Lua:L, idx); 121 | native lua_rotate(Lua:L, idx, n); 122 | native lua_copy(Lua:L, fromidx, toidx); 123 | native bool:lua_checkstack(Lua:L, n); 124 | 125 | native lua_xmove(Lua:from, Lua:to, n); 126 | 127 | native bool:lua_isnumber(Lua:L, idx); 128 | native bool:lua_isstring(Lua:L, idx); 129 | native bool:lua_iscfunction(Lua:L, idx); 130 | native bool:lua_isinteger(Lua:L, idx); 131 | native bool:lua_isuserdata(Lua:L, idx); 132 | native lua_type:lua_type(Lua:L, idx); 133 | 134 | native bool:lua_toboolean(Lua:L, idx); 135 | native lua_rawlen(Lua:L, idx); 136 | native Pointer:lua_touserdata(Lua:L, idx); 137 | native L:lua_tothread(Lua:L, idx); 138 | native Pointer:lua_topointer(Lua:L, idx); 139 | 140 | native bool:lua_rawequal(Lua:L, idx1, idx2); 141 | 142 | native lua_pushnil(Lua:L); 143 | native lua_pushnumber(Lua:L, Float:n); 144 | native lua_pushinteger(Lua:L, n); 145 | native lua_pushboolean(Lua:L, bool:b); 146 | native lua_pushlightuserdata(Lua:L, {_,Pointer}:p); 147 | native lua_pushthread(Lua:L); 148 | 149 | native lua_type:lua_rawget(Lua:L, idx); 150 | native lua_type:lua_rawgeti(Lua:L, idx, n); 151 | native lua_type:lua_rawgetp(Lua:L, idx, Pointer:p); 152 | 153 | native lua_createtable(Lua:L, narr, nrec); 154 | native Pointer:lua_newuserdata(Lua:L, size); 155 | native bool:lua_getmetatable(Lua:L, objindex); 156 | native lua_type:lua_getuservalue(Lua:L, idx); 157 | 158 | native lua_rawset(Lua:L, idx); 159 | native lua_rawseti(Lua:L, idx, n); 160 | native lua_rawsetp(Lua:L, idx, Pointer:p); 161 | native lua_setmetatable(Lua:L, objindex); 162 | native lua_setuservalue(Lua:L, idx); 163 | 164 | native lua_status:lua_resume(Lua:L, Lua:from, narg); 165 | native lua_status:lua_status(Lua:L); 166 | 167 | const lua_gc_command:LUA_GCSTOP = lua_gc_command:0; 168 | const lua_gc_command:LUA_GCRESTART = lua_gc_command:1; 169 | const lua_gc_command:LUA_GCCOLLECT = lua_gc_command:2; 170 | const lua_gc_command:LUA_GCCOUNT = lua_gc_command:3; 171 | const lua_gc_command:LUA_GCCOUNTB = lua_gc_command:4; 172 | const lua_gc_command:LUA_GCSTEP = lua_gc_command:5; 173 | const lua_gc_command:LUA_GCSETPAUSE = lua_gc_command:6; 174 | const lua_gc_command:LUA_GCSETSTEPMUL = lua_gc_command:7; 175 | const lua_gc_command:LUA_GCISRUNNING = lua_gc_command:9; 176 | 177 | native lua_gc(Lua:L, lua_gc_command:what, data); 178 | 179 | native bool:lua_next(Lua:L, idx); 180 | 181 | const lua_arith_op:LUA_OPADD = lua_arith_op:0; 182 | const lua_arith_op:LUA_OPSUB = lua_arith_op:1; 183 | const lua_arith_op:LUA_OPMUL = lua_arith_op:2; 184 | const lua_arith_op:LUA_OPMOD = lua_arith_op:3; 185 | const lua_arith_op:LUA_OPPOW = lua_arith_op:4; 186 | const lua_arith_op:LUA_OPDIV = lua_arith_op:5; 187 | const lua_arith_op:LUA_OPIDIV = lua_arith_op:6; 188 | const lua_arith_op:LUA_OPBAND = lua_arith_op:7; 189 | const lua_arith_op:LUA_OPBOR = lua_arith_op:8; 190 | const lua_arith_op:LUA_OPBXOR = lua_arith_op:9; 191 | const lua_arith_op:LUA_OPSHL = lua_arith_op:10; 192 | const lua_arith_op:LUA_OPSHR = lua_arith_op:11; 193 | const lua_arith_op:LUA_OPUNM = lua_arith_op:12; 194 | const lua_arith_op:LUA_OPBNOT = lua_arith_op:13; 195 | 196 | native lua_arith(Lua:L, lua_arith_op:op); 197 | 198 | const lua_rel_op:LUA_OPEQ = lua_rel_op:0; 199 | const lua_rel_op:LUA_OPLT = lua_rel_op:1; 200 | const lua_rel_op:LUA_OPLE = lua_rel_op:2; 201 | 202 | native bool:lua_compare(Lua:L, idx1, idx2, lua_rel_op:op); 203 | -------------------------------------------------------------------------------- /plugins/YALP.def: -------------------------------------------------------------------------------- 1 | LIBRARY "YALP" 2 | 3 | EXPORTS 4 | Supports 5 | Load 6 | Unload 7 | AmxLoad 8 | AmxUnload 9 | ProcessTick -------------------------------------------------------------------------------- /plugins/YALP.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 15 3 | VisualStudioVersion = 15.0.27004.2002 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{F221DB48-3DA9-4033-AC26-AC0A8652C605}") = "YALP", "YALP.vcxproj", "{6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Any CPU = Debug|Any CPU 10 | Debug|Win32 = Debug|Win32 11 | Release|Any CPU = Release|Any CPU 12 | Release|Win32 = Release|Win32 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Debug|Any CPU.ActiveCfg = Debug|Win32 16 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Debug|Win32.Build.0 = Debug|Win32 18 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Release|Any CPU.ActiveCfg = Release|Win32 19 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Release|Win32.ActiveCfg = Release|Win32 20 | {6B21A77F-6E43-43E2-954D-C00D2DBBE9FE}.Release|Win32.Build.0 = Release|Win32 21 | EndGlobalSection 22 | GlobalSection(SolutionProperties) = preSolution 23 | HideSolutionNode = FALSE 24 | EndGlobalSection 25 | GlobalSection(ExtensibilityGlobals) = postSolution 26 | SolutionGuid = {7AB21584-7362-4B5B-BD21-A501920662DB} 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /plugins/lib/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /plugins/lib/lua/lbitlib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 24 | 25 | 26 | /* number of bits to consider in a number */ 27 | #if !defined(LUA_NBITS) 28 | #define LUA_NBITS 32 29 | #endif 30 | 31 | 32 | /* 33 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 34 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 35 | ** number of bits in a lua_Unsigned.) 36 | */ 37 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 38 | 39 | 40 | /* macro to trim extra bits */ 41 | #define trim(x) ((x) & ALLONES) 42 | 43 | 44 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 45 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 46 | 47 | 48 | 49 | static lua_Unsigned andaux (lua_State *L) { 50 | int i, n = lua_gettop(L); 51 | lua_Unsigned r = ~(lua_Unsigned)0; 52 | for (i = 1; i <= n; i++) 53 | r &= checkunsigned(L, i); 54 | return trim(r); 55 | } 56 | 57 | 58 | static int b_and (lua_State *L) { 59 | lua_Unsigned r = andaux(L); 60 | pushunsigned(L, r); 61 | return 1; 62 | } 63 | 64 | 65 | static int b_test (lua_State *L) { 66 | lua_Unsigned r = andaux(L); 67 | lua_pushboolean(L, r != 0); 68 | return 1; 69 | } 70 | 71 | 72 | static int b_or (lua_State *L) { 73 | int i, n = lua_gettop(L); 74 | lua_Unsigned r = 0; 75 | for (i = 1; i <= n; i++) 76 | r |= checkunsigned(L, i); 77 | pushunsigned(L, trim(r)); 78 | return 1; 79 | } 80 | 81 | 82 | static int b_xor (lua_State *L) { 83 | int i, n = lua_gettop(L); 84 | lua_Unsigned r = 0; 85 | for (i = 1; i <= n; i++) 86 | r ^= checkunsigned(L, i); 87 | pushunsigned(L, trim(r)); 88 | return 1; 89 | } 90 | 91 | 92 | static int b_not (lua_State *L) { 93 | lua_Unsigned r = ~checkunsigned(L, 1); 94 | pushunsigned(L, trim(r)); 95 | return 1; 96 | } 97 | 98 | 99 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 100 | if (i < 0) { /* shift right? */ 101 | i = -i; 102 | r = trim(r); 103 | if (i >= LUA_NBITS) r = 0; 104 | else r >>= i; 105 | } 106 | else { /* shift left */ 107 | if (i >= LUA_NBITS) r = 0; 108 | else r <<= i; 109 | r = trim(r); 110 | } 111 | pushunsigned(L, r); 112 | return 1; 113 | } 114 | 115 | 116 | static int b_lshift (lua_State *L) { 117 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); 118 | } 119 | 120 | 121 | static int b_rshift (lua_State *L) { 122 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 123 | } 124 | 125 | 126 | static int b_arshift (lua_State *L) { 127 | lua_Unsigned r = checkunsigned(L, 1); 128 | lua_Integer i = luaL_checkinteger(L, 2); 129 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 130 | return b_shift(L, r, -i); 131 | else { /* arithmetic shift for 'negative' number */ 132 | if (i >= LUA_NBITS) r = ALLONES; 133 | else 134 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 135 | pushunsigned(L, r); 136 | return 1; 137 | } 138 | } 139 | 140 | 141 | static int b_rot (lua_State *L, lua_Integer d) { 142 | lua_Unsigned r = checkunsigned(L, 1); 143 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 144 | r = trim(r); 145 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 146 | r = (r << i) | (r >> (LUA_NBITS - i)); 147 | pushunsigned(L, trim(r)); 148 | return 1; 149 | } 150 | 151 | 152 | static int b_lrot (lua_State *L) { 153 | return b_rot(L, luaL_checkinteger(L, 2)); 154 | } 155 | 156 | 157 | static int b_rrot (lua_State *L) { 158 | return b_rot(L, -luaL_checkinteger(L, 2)); 159 | } 160 | 161 | 162 | /* 163 | ** get field and width arguments for field-manipulation functions, 164 | ** checking whether they are valid. 165 | ** ('luaL_error' called without 'return' to avoid later warnings about 166 | ** 'width' being used uninitialized.) 167 | */ 168 | static int fieldargs (lua_State *L, int farg, int *width) { 169 | lua_Integer f = luaL_checkinteger(L, farg); 170 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 171 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 172 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 173 | if (f + w > LUA_NBITS) 174 | luaL_error(L, "trying to access non-existent bits"); 175 | *width = (int)w; 176 | return (int)f; 177 | } 178 | 179 | 180 | static int b_extract (lua_State *L) { 181 | int w; 182 | lua_Unsigned r = trim(checkunsigned(L, 1)); 183 | int f = fieldargs(L, 2, &w); 184 | r = (r >> f) & mask(w); 185 | pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static int b_replace (lua_State *L) { 191 | int w; 192 | lua_Unsigned r = trim(checkunsigned(L, 1)); 193 | lua_Unsigned v = trim(checkunsigned(L, 2)); 194 | int f = fieldargs(L, 3, &w); 195 | lua_Unsigned m = mask(w); 196 | r = (r & ~(m << f)) | ((v & m) << f); 197 | pushunsigned(L, r); 198 | return 1; 199 | } 200 | 201 | 202 | static const luaL_Reg bitlib[] = { 203 | {"arshift", b_arshift}, 204 | {"band", b_and}, 205 | {"bnot", b_not}, 206 | {"bor", b_or}, 207 | {"bxor", b_xor}, 208 | {"btest", b_test}, 209 | {"extract", b_extract}, 210 | {"lrotate", b_lrot}, 211 | {"lshift", b_lshift}, 212 | {"replace", b_replace}, 213 | {"rrotate", b_rrot}, 214 | {"rshift", b_rshift}, 215 | {NULL, NULL} 216 | }; 217 | 218 | 219 | 220 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 221 | luaL_newlib(L, bitlib); 222 | return 1; 223 | } 224 | 225 | 226 | #else /* }{ */ 227 | 228 | 229 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 230 | return luaL_error(L, "library 'bit32' has been deprecated"); 231 | } 232 | 233 | #endif /* } */ 234 | -------------------------------------------------------------------------------- /plugins/lib/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | /* get (pointer to) instruction of given 'expdesc' */ 44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 45 | 46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 47 | 48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 49 | 50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 51 | 52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 74 | LUAI_FUNC int luaK_jump (FuncState *fs); 75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 80 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 84 | expdesc *v2, int line); 85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 86 | 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /plugins/lib/lua/lcorolib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argcheck(L, co, 1, "thread expected"); 24 | return co; 25 | } 26 | 27 | 28 | static int auxresume (lua_State *L, lua_State *co, int narg) { 29 | int status; 30 | if (!lua_checkstack(co, narg)) { 31 | lua_pushliteral(L, "too many arguments to resume"); 32 | return -1; /* error flag */ 33 | } 34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 35 | lua_pushliteral(L, "cannot resume dead coroutine"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg); 40 | if (status == LUA_OK || status == LUA_YIELD) { 41 | int nres = lua_gettop(co); 42 | if (!lua_checkstack(L, nres + 1)) { 43 | lua_pop(co, nres); /* remove results anyway */ 44 | lua_pushliteral(L, "too many results to resume"); 45 | return -1; /* error flag */ 46 | } 47 | lua_xmove(co, L, nres); /* move yielded values */ 48 | return nres; 49 | } 50 | else { 51 | lua_xmove(co, L, 1); /* move error message */ 52 | return -1; /* error flag */ 53 | } 54 | } 55 | 56 | 57 | static int luaB_coresume (lua_State *L) { 58 | lua_State *co = getco(L); 59 | int r; 60 | r = auxresume(L, co, lua_gettop(L) - 1); 61 | if (r < 0) { 62 | lua_pushboolean(L, 0); 63 | lua_insert(L, -2); 64 | return 2; /* return false + error message */ 65 | } 66 | else { 67 | lua_pushboolean(L, 1); 68 | lua_insert(L, -(r + 1)); 69 | return r + 1; /* return true + 'resume' returns */ 70 | } 71 | } 72 | 73 | 74 | static int luaB_auxwrap (lua_State *L) { 75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 76 | int r = auxresume(L, co, lua_gettop(L)); 77 | if (r < 0) { 78 | if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ 79 | luaL_where(L, 1); /* add extra info */ 80 | lua_insert(L, -2); 81 | lua_concat(L, 2); 82 | } 83 | return lua_error(L); /* propagate error */ 84 | } 85 | return r; 86 | } 87 | 88 | 89 | static int luaB_cocreate (lua_State *L) { 90 | lua_State *NL; 91 | luaL_checktype(L, 1, LUA_TFUNCTION); 92 | NL = lua_newthread(L); 93 | lua_pushvalue(L, 1); /* move function to top */ 94 | lua_xmove(L, NL, 1); /* move function from L to NL */ 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_cowrap (lua_State *L) { 100 | luaB_cocreate(L); 101 | lua_pushcclosure(L, luaB_auxwrap, 1); 102 | return 1; 103 | } 104 | 105 | 106 | static int luaB_yield (lua_State *L) { 107 | return lua_yield(L, lua_gettop(L)); 108 | } 109 | 110 | 111 | static int luaB_costatus (lua_State *L) { 112 | lua_State *co = getco(L); 113 | if (L == co) lua_pushliteral(L, "running"); 114 | else { 115 | switch (lua_status(co)) { 116 | case LUA_YIELD: 117 | lua_pushliteral(L, "suspended"); 118 | break; 119 | case LUA_OK: { 120 | lua_Debug ar; 121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 122 | lua_pushliteral(L, "normal"); /* it is running */ 123 | else if (lua_gettop(co) == 0) 124 | lua_pushliteral(L, "dead"); 125 | else 126 | lua_pushliteral(L, "suspended"); /* initial state */ 127 | break; 128 | } 129 | default: /* some error occurred */ 130 | lua_pushliteral(L, "dead"); 131 | break; 132 | } 133 | } 134 | return 1; 135 | } 136 | 137 | 138 | static int luaB_yieldable (lua_State *L) { 139 | lua_pushboolean(L, lua_isyieldable(L)); 140 | return 1; 141 | } 142 | 143 | 144 | static int luaB_corunning (lua_State *L) { 145 | int ismain = lua_pushthread(L); 146 | lua_pushboolean(L, ismain); 147 | return 2; 148 | } 149 | 150 | 151 | static const luaL_Reg co_funcs[] = { 152 | {"create", luaB_cocreate}, 153 | {"resume", luaB_coresume}, 154 | {"running", luaB_corunning}, 155 | {"status", luaB_costatus}, 156 | {"wrap", luaB_cowrap}, 157 | {"yield", luaB_yield}, 158 | {"isyieldable", luaB_yieldable}, 159 | {NULL, NULL} 160 | }; 161 | 162 | 163 | 164 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 165 | luaL_newlib(L, co_funcs); 166 | return 1; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /plugins/lib/lua/lctype.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /plugins/lib/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /plugins/lib/lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 24 | const TValue *p2); 25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 26 | const TValue *p2, 27 | const char *msg); 28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 29 | const TValue *p2); 30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 31 | const TValue *p2); 32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 34 | TString *src, int line); 35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 36 | LUAI_FUNC void luaG_traceexec (lua_State *L); 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /plugins/lib/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } 26 | 27 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 29 | 30 | 31 | 32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 34 | 35 | 36 | /* type of protected functions, to be ran by 'runprotected' */ 37 | typedef void (*Pfunc) (lua_State *L, void *ud); 38 | 39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 40 | const char *mode); 41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 46 | ptrdiff_t oldtop, ptrdiff_t ef); 47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, 48 | int nres); 49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 52 | LUAI_FUNC void luaD_inctop (lua_State *L); 53 | 54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /plugins/lib/lua/ldump.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through DumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 36 | 37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 38 | 39 | 40 | static void DumpBlock (const void *b, size_t size, DumpState *D) { 41 | if (D->status == 0 && size > 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define DumpVar(x,D) DumpVector(&x,1,D) 50 | 51 | 52 | static void DumpByte (int y, DumpState *D) { 53 | lu_byte x = (lu_byte)y; 54 | DumpVar(x, D); 55 | } 56 | 57 | 58 | static void DumpInt (int x, DumpState *D) { 59 | DumpVar(x, D); 60 | } 61 | 62 | 63 | static void DumpNumber (lua_Number x, DumpState *D) { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpInteger (lua_Integer x, DumpState *D) { 69 | DumpVar(x, D); 70 | } 71 | 72 | 73 | static void DumpString (const TString *s, DumpState *D) { 74 | if (s == NULL) 75 | DumpByte(0, D); 76 | else { 77 | size_t size = tsslen(s) + 1; /* include trailing '\0' */ 78 | const char *str = getstr(s); 79 | if (size < 0xFF) 80 | DumpByte(cast_int(size), D); 81 | else { 82 | DumpByte(0xFF, D); 83 | DumpVar(size, D); 84 | } 85 | DumpVector(str, size - 1, D); /* no need to save '\0' */ 86 | } 87 | } 88 | 89 | 90 | static void DumpCode (const Proto *f, DumpState *D) { 91 | DumpInt(f->sizecode, D); 92 | DumpVector(f->code, f->sizecode, D); 93 | } 94 | 95 | 96 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 97 | 98 | static void DumpConstants (const Proto *f, DumpState *D) { 99 | int i; 100 | int n = f->sizek; 101 | DumpInt(n, D); 102 | for (i = 0; i < n; i++) { 103 | const TValue *o = &f->k[i]; 104 | DumpByte(ttype(o), D); 105 | switch (ttype(o)) { 106 | case LUA_TNIL: 107 | break; 108 | case LUA_TBOOLEAN: 109 | DumpByte(bvalue(o), D); 110 | break; 111 | case LUA_TNUMFLT: 112 | DumpNumber(fltvalue(o), D); 113 | break; 114 | case LUA_TNUMINT: 115 | DumpInteger(ivalue(o), D); 116 | break; 117 | case LUA_TSHRSTR: 118 | case LUA_TLNGSTR: 119 | DumpString(tsvalue(o), D); 120 | break; 121 | default: 122 | lua_assert(0); 123 | } 124 | } 125 | } 126 | 127 | 128 | static void DumpProtos (const Proto *f, DumpState *D) { 129 | int i; 130 | int n = f->sizep; 131 | DumpInt(n, D); 132 | for (i = 0; i < n; i++) 133 | DumpFunction(f->p[i], f->source, D); 134 | } 135 | 136 | 137 | static void DumpUpvalues (const Proto *f, DumpState *D) { 138 | int i, n = f->sizeupvalues; 139 | DumpInt(n, D); 140 | for (i = 0; i < n; i++) { 141 | DumpByte(f->upvalues[i].instack, D); 142 | DumpByte(f->upvalues[i].idx, D); 143 | } 144 | } 145 | 146 | 147 | static void DumpDebug (const Proto *f, DumpState *D) { 148 | int i, n; 149 | n = (D->strip) ? 0 : f->sizelineinfo; 150 | DumpInt(n, D); 151 | DumpVector(f->lineinfo, n, D); 152 | n = (D->strip) ? 0 : f->sizelocvars; 153 | DumpInt(n, D); 154 | for (i = 0; i < n; i++) { 155 | DumpString(f->locvars[i].varname, D); 156 | DumpInt(f->locvars[i].startpc, D); 157 | DumpInt(f->locvars[i].endpc, D); 158 | } 159 | n = (D->strip) ? 0 : f->sizeupvalues; 160 | DumpInt(n, D); 161 | for (i = 0; i < n; i++) 162 | DumpString(f->upvalues[i].name, D); 163 | } 164 | 165 | 166 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { 167 | if (D->strip || f->source == psource) 168 | DumpString(NULL, D); /* no debug info or same source as its parent */ 169 | else 170 | DumpString(f->source, D); 171 | DumpInt(f->linedefined, D); 172 | DumpInt(f->lastlinedefined, D); 173 | DumpByte(f->numparams, D); 174 | DumpByte(f->is_vararg, D); 175 | DumpByte(f->maxstacksize, D); 176 | DumpCode(f, D); 177 | DumpConstants(f, D); 178 | DumpUpvalues(f, D); 179 | DumpProtos(f, D); 180 | DumpDebug(f, D); 181 | } 182 | 183 | 184 | static void DumpHeader (DumpState *D) { 185 | DumpLiteral(LUA_SIGNATURE, D); 186 | DumpByte(LUAC_VERSION, D); 187 | DumpByte(LUAC_FORMAT, D); 188 | DumpLiteral(LUAC_DATA, D); 189 | DumpByte(sizeof(int), D); 190 | DumpByte(sizeof(size_t), D); 191 | DumpByte(sizeof(Instruction), D); 192 | DumpByte(sizeof(lua_Integer), D); 193 | DumpByte(sizeof(lua_Number), D); 194 | DumpInteger(LUAC_INT, D); 195 | DumpNumber(LUAC_NUM, D); 196 | } 197 | 198 | 199 | /* 200 | ** dump Lua function as precompiled chunk 201 | */ 202 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 203 | int strip) { 204 | DumpState D; 205 | D.L = L; 206 | D.writer = w; 207 | D.data = data; 208 | D.strip = strip; 209 | D.status = 0; 210 | DumpHeader(&D); 211 | DumpByte(f->sizeupvalues, &D); 212 | DumpFunction(f, NULL, &D); 213 | return D.status; 214 | } 215 | 216 | -------------------------------------------------------------------------------- /plugins/lib/lua/lfunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure (lua_State *L, int n) { 26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 27 | CClosure *c = gco2ccl(o); 28 | c->nupvalues = cast_byte(n); 29 | return c; 30 | } 31 | 32 | 33 | LClosure *luaF_newLclosure (lua_State *L, int n) { 34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 35 | LClosure *c = gco2lcl(o); 36 | c->p = NULL; 37 | c->nupvalues = cast_byte(n); 38 | while (n--) c->upvals[n] = NULL; 39 | return c; 40 | } 41 | 42 | /* 43 | ** fill a closure with new closed upvalues 44 | */ 45 | void luaF_initupvals (lua_State *L, LClosure *cl) { 46 | int i; 47 | for (i = 0; i < cl->nupvalues; i++) { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | uv->refcount = 1; 50 | uv->v = &uv->u.value; /* make it closed */ 51 | setnilvalue(uv->v); 52 | cl->upvals[i] = uv; 53 | } 54 | } 55 | 56 | 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { 58 | UpVal **pp = &L->openupval; 59 | UpVal *p; 60 | UpVal *uv; 61 | lua_assert(isintwups(L) || L->openupval == NULL); 62 | while (*pp != NULL && (p = *pp)->v >= level) { 63 | lua_assert(upisopen(p)); 64 | if (p->v == level) /* found a corresponding upvalue? */ 65 | return p; /* return it */ 66 | pp = &p->u.open.next; 67 | } 68 | /* not found: create a new upvalue */ 69 | uv = luaM_new(L, UpVal); 70 | uv->refcount = 0; 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 72 | uv->u.open.touched = 1; 73 | *pp = uv; 74 | uv->v = level; /* current value lives in the stack */ 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 76 | L->twups = G(L)->twups; /* link it to the list */ 77 | G(L)->twups = L; 78 | } 79 | return uv; 80 | } 81 | 82 | 83 | void luaF_close (lua_State *L, StkId level) { 84 | UpVal *uv; 85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) { 86 | lua_assert(upisopen(uv)); 87 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 88 | if (uv->refcount == 0) /* no references? */ 89 | luaM_free(L, uv); /* free upvalue */ 90 | else { 91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 92 | uv->v = &uv->u.value; /* now current value lives here */ 93 | luaC_upvalbarrier(L, uv); 94 | } 95 | } 96 | } 97 | 98 | 99 | Proto *luaF_newproto (lua_State *L) { 100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 101 | Proto *f = gco2p(o); 102 | f->k = NULL; 103 | f->sizek = 0; 104 | f->p = NULL; 105 | f->sizep = 0; 106 | f->code = NULL; 107 | f->cache = NULL; 108 | f->sizecode = 0; 109 | f->lineinfo = NULL; 110 | f->sizelineinfo = 0; 111 | f->upvalues = NULL; 112 | f->sizeupvalues = 0; 113 | f->numparams = 0; 114 | f->is_vararg = 0; 115 | f->maxstacksize = 0; 116 | f->locvars = NULL; 117 | f->sizelocvars = 0; 118 | f->linedefined = 0; 119 | f->lastlinedefined = 0; 120 | f->source = NULL; 121 | return f; 122 | } 123 | 124 | 125 | void luaF_freeproto (lua_State *L, Proto *f) { 126 | luaM_freearray(L, f->code, f->sizecode); 127 | luaM_freearray(L, f->p, f->sizep); 128 | luaM_freearray(L, f->k, f->sizek); 129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 130 | luaM_freearray(L, f->locvars, f->sizelocvars); 131 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 132 | luaM_free(L, f); 133 | } 134 | 135 | 136 | /* 137 | ** Look for n-th local variable at line 'line' in function 'func'. 138 | ** Returns NULL if not found. 139 | */ 140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 141 | int i; 142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 143 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 144 | local_number--; 145 | if (local_number == 0) 146 | return getstr(f->locvars[i].varname); 147 | } 148 | } 149 | return NULL; /* not found */ 150 | } 151 | 152 | -------------------------------------------------------------------------------- /plugins/lib/lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | /* 33 | ** Upvalues for Lua closures 34 | */ 35 | struct UpVal { 36 | TValue *v; /* points to stack or to its own value */ 37 | lu_mem refcount; /* reference counter */ 38 | union { 39 | struct { /* (when open) */ 40 | UpVal *next; /* linked list */ 41 | int touched; /* mark to avoid cycles with dead threads */ 42 | } open; 43 | TValue value; /* the value (when closed) */ 44 | } u; 45 | }; 46 | 47 | #define upisopen(up) ((up)->v != &(up)->u.value) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 58 | int pc); 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /plugins/lib/lua/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | /* 105 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 106 | ** allows some adjustments to be done only when needed. macro 107 | ** 'condchangemem' is used only for heavy tests (forcing a full 108 | ** GC cycle on every opportunity) 109 | */ 110 | #define luaC_condGC(L,pre,pos) \ 111 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 112 | condchangemem(L,pre,pos); } 113 | 114 | /* more often than not, 'pre'/'pos' are empty */ 115 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 116 | 117 | 118 | #define luaC_barrier(L,p,v) ( \ 119 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 120 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 121 | 122 | #define luaC_barrierback(L,p,v) ( \ 123 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 124 | luaC_barrierback_(L,p) : cast_void(0)) 125 | 126 | #define luaC_objbarrier(L,p,o) ( \ 127 | (isblack(p) && iswhite(o)) ? \ 128 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 129 | 130 | #define luaC_upvalbarrier(L,uv) ( \ 131 | (iscollectable((uv)->v) && !upisopen(uv)) ? \ 132 | luaC_upvalbarrier_(L,uv) : cast_void(0)) 133 | 134 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 135 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 136 | LUAI_FUNC void luaC_step (lua_State *L); 137 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 138 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 139 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 142 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 145 | 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /plugins/lib/lua/linit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {"_G", luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | #if defined(LUA_COMPAT_BITLIB) 54 | {LUA_BITLIBNAME, luaopen_bit32}, 55 | #endif 56 | {NULL, NULL} 57 | }; 58 | 59 | 60 | LUALIB_API void luaL_openlibs (lua_State *L) { 61 | const luaL_Reg *lib; 62 | /* "require" functions from 'loadedlibs' and set results to global table */ 63 | for (lib = loadedlibs; lib->func; lib++) { 64 | luaL_requiref(L, lib->name, lib->func, 1); 65 | lua_pop(L, 1); /* remove lib */ 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /plugins/lib/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | } LexState; 73 | 74 | 75 | LUAI_FUNC void luaX_init (lua_State *L); 76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 77 | TString *source, int firstchar); 78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 79 | LUAI_FUNC void luaX_next (LexState *ls); 80 | LUAI_FUNC int luaX_lookahead (LexState *ls); 81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /plugins/lib/lua/lmem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ 89 | if (g->version) { /* is state fully built? */ 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | } 93 | if (newblock == NULL) 94 | luaD_throw(L, LUA_ERRMEM); 95 | } 96 | lua_assert((nsize == 0) == (newblock == NULL)); 97 | g->GCdebt = (g->GCdebt + nsize) - realosize; 98 | return newblock; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /plugins/lib/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /plugins/lib/lua/lopcodes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /plugins/lib/lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum { 26 | VVOID, /* when 'expdesc' describes the last expression a list, 27 | this kind means an empty list (so, no expression) */ 28 | VNIL, /* constant nil */ 29 | VTRUE, /* constant true */ 30 | VFALSE, /* constant false */ 31 | VK, /* constant in 'k'; info = index of constant in 'k' */ 32 | VKFLT, /* floating constant; nval = numerical float value */ 33 | VKINT, /* integer constant; nval = numerical integer value */ 34 | VNONRELOC, /* expression has its value in a fixed register; 35 | info = result register */ 36 | VLOCAL, /* local variable; info = local register */ 37 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 38 | VINDEXED, /* indexed variable; 39 | ind.vt = whether 't' is register or upvalue; 40 | ind.t = table register or upvalue; 41 | ind.idx = key's R/K index */ 42 | VJMP, /* expression is a test/comparison; 43 | info = pc of corresponding jump instruction */ 44 | VRELOCABLE, /* expression can put result in any register; 45 | info = instruction pc */ 46 | VCALL, /* expression is a function call; info = instruction pc */ 47 | VVARARG /* vararg expression; info = instruction pc */ 48 | } expkind; 49 | 50 | 51 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 52 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 53 | 54 | typedef struct expdesc { 55 | expkind k; 56 | union { 57 | lua_Integer ival; /* for VKINT */ 58 | lua_Number nval; /* for VKFLT */ 59 | int info; /* for generic use */ 60 | struct { /* for indexed variables (VINDEXED) */ 61 | short idx; /* index (R/K) */ 62 | lu_byte t; /* table (register or upvalue) */ 63 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 64 | } ind; 65 | } u; 66 | int t; /* patch list of 'exit when true' */ 67 | int f; /* patch list of 'exit when false' */ 68 | } expdesc; 69 | 70 | 71 | /* description of active local variable */ 72 | typedef struct Vardesc { 73 | short idx; /* variable index in stack */ 74 | } Vardesc; 75 | 76 | 77 | /* description of pending goto statements and label statements */ 78 | typedef struct Labeldesc { 79 | TString *name; /* label identifier */ 80 | int pc; /* position in code */ 81 | int line; /* line where it appeared */ 82 | lu_byte nactvar; /* local level where it appears in current block */ 83 | } Labeldesc; 84 | 85 | 86 | /* list of labels or gotos */ 87 | typedef struct Labellist { 88 | Labeldesc *arr; /* array */ 89 | int n; /* number of entries in use */ 90 | int size; /* array size */ 91 | } Labellist; 92 | 93 | 94 | /* dynamic structures used by the parser */ 95 | typedef struct Dyndata { 96 | struct { /* list of active local variables */ 97 | Vardesc *arr; 98 | int n; 99 | int size; 100 | } actvar; 101 | Labellist gt; /* list of pending gotos */ 102 | Labellist label; /* list of active labels */ 103 | } Dyndata; 104 | 105 | 106 | /* control of blocks */ 107 | struct BlockCnt; /* defined in lparser.c */ 108 | 109 | 110 | /* state needed to generate code for a given function */ 111 | typedef struct FuncState { 112 | Proto *f; /* current function header */ 113 | struct FuncState *prev; /* enclosing function */ 114 | struct LexState *ls; /* lexical state */ 115 | struct BlockCnt *bl; /* chain of current blocks */ 116 | int pc; /* next position to code (equivalent to 'ncode') */ 117 | int lasttarget; /* 'label' of last 'jump label' */ 118 | int jpc; /* list of pending jumps to 'pc' */ 119 | int nk; /* number of elements in 'k' */ 120 | int np; /* number of elements in 'p' */ 121 | int firstlocal; /* index of first local var (in Dyndata array) */ 122 | short nlocvars; /* number of elements in 'f->locvars' */ 123 | lu_byte nactvar; /* number of active local variables */ 124 | lu_byte nups; /* number of upvalues */ 125 | lu_byte freereg; /* first free register */ 126 | } FuncState; 127 | 128 | 129 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 130 | Dyndata *dyd, const char *name, int firstchar); 131 | 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /plugins/lib/lua/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /plugins/lib/lua/lstring.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | #define MEMERRMSG "not enough memory" 26 | 27 | 28 | /* 29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 30 | ** compute its hash 31 | */ 32 | #if !defined(LUAI_HASHLIMIT) 33 | #define LUAI_HASHLIMIT 5 34 | #endif 35 | 36 | 37 | /* 38 | ** equality for long strings 39 | */ 40 | int luaS_eqlngstr (TString *a, TString *b) { 41 | size_t len = a->u.lnglen; 42 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); 43 | return (a == b) || /* same instance or... */ 44 | ((len == b->u.lnglen) && /* equal length and ... */ 45 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 46 | } 47 | 48 | 49 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 50 | unsigned int h = seed ^ cast(unsigned int, l); 51 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 52 | for (; l >= step; l -= step) 53 | h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); 54 | return h; 55 | } 56 | 57 | 58 | unsigned int luaS_hashlongstr (TString *ts) { 59 | lua_assert(ts->tt == LUA_TLNGSTR); 60 | if (ts->extra == 0) { /* no hash? */ 61 | ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); 62 | ts->extra = 1; /* now it has its hash */ 63 | } 64 | return ts->hash; 65 | } 66 | 67 | 68 | /* 69 | ** resizes the string table 70 | */ 71 | void luaS_resize (lua_State *L, int newsize) { 72 | int i; 73 | stringtable *tb = &G(L)->strt; 74 | if (newsize > tb->size) { /* grow table if needed */ 75 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 76 | for (i = tb->size; i < newsize; i++) 77 | tb->hash[i] = NULL; 78 | } 79 | for (i = 0; i < tb->size; i++) { /* rehash */ 80 | TString *p = tb->hash[i]; 81 | tb->hash[i] = NULL; 82 | while (p) { /* for each node in the list */ 83 | TString *hnext = p->u.hnext; /* save next */ 84 | unsigned int h = lmod(p->hash, newsize); /* new position */ 85 | p->u.hnext = tb->hash[h]; /* chain it */ 86 | tb->hash[h] = p; 87 | p = hnext; 88 | } 89 | } 90 | if (newsize < tb->size) { /* shrink table if needed */ 91 | /* vanishing slice should be empty */ 92 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 93 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 94 | } 95 | tb->size = newsize; 96 | } 97 | 98 | 99 | /* 100 | ** Clear API string cache. (Entries cannot be empty, so fill them with 101 | ** a non-collectable string.) 102 | */ 103 | void luaS_clearcache (global_State *g) { 104 | int i, j; 105 | for (i = 0; i < STRCACHE_N; i++) 106 | for (j = 0; j < STRCACHE_M; j++) { 107 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ 108 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ 109 | } 110 | } 111 | 112 | 113 | /* 114 | ** Initialize the string table and the string cache 115 | */ 116 | void luaS_init (lua_State *L) { 117 | global_State *g = G(L); 118 | int i, j; 119 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 120 | /* pre-create memory-error message */ 121 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 122 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ 123 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ 124 | for (j = 0; j < STRCACHE_M; j++) 125 | g->strcache[i][j] = g->memerrmsg; 126 | } 127 | 128 | 129 | 130 | /* 131 | ** creates a new string object 132 | */ 133 | static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { 134 | TString *ts; 135 | GCObject *o; 136 | size_t totalsize; /* total size of TString object */ 137 | totalsize = sizelstring(l); 138 | o = luaC_newobj(L, tag, totalsize); 139 | ts = gco2ts(o); 140 | ts->hash = h; 141 | ts->extra = 0; 142 | getstr(ts)[l] = '\0'; /* ending 0 */ 143 | return ts; 144 | } 145 | 146 | 147 | TString *luaS_createlngstrobj (lua_State *L, size_t l) { 148 | TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); 149 | ts->u.lnglen = l; 150 | return ts; 151 | } 152 | 153 | 154 | void luaS_remove (lua_State *L, TString *ts) { 155 | stringtable *tb = &G(L)->strt; 156 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 157 | while (*p != ts) /* find previous element */ 158 | p = &(*p)->u.hnext; 159 | *p = (*p)->u.hnext; /* remove element from its list */ 160 | tb->nuse--; 161 | } 162 | 163 | 164 | /* 165 | ** checks whether short string exists and reuses it or creates a new one 166 | */ 167 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 168 | TString *ts; 169 | global_State *g = G(L); 170 | unsigned int h = luaS_hash(str, l, g->seed); 171 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; 172 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ 173 | for (ts = *list; ts != NULL; ts = ts->u.hnext) { 174 | if (l == ts->shrlen && 175 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 176 | /* found! */ 177 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 178 | changewhite(ts); /* resurrect it */ 179 | return ts; 180 | } 181 | } 182 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { 183 | luaS_resize(L, g->strt.size * 2); 184 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 185 | } 186 | ts = createstrobj(L, l, LUA_TSHRSTR, h); 187 | memcpy(getstr(ts), str, l * sizeof(char)); 188 | ts->shrlen = cast_byte(l); 189 | ts->u.hnext = *list; 190 | *list = ts; 191 | g->strt.nuse++; 192 | return ts; 193 | } 194 | 195 | 196 | /* 197 | ** new string (with explicit length) 198 | */ 199 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 200 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 201 | return internshrstr(L, str, l); 202 | else { 203 | TString *ts; 204 | if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) 205 | luaM_toobig(L); 206 | ts = luaS_createlngstrobj(L, l); 207 | memcpy(getstr(ts), str, l * sizeof(char)); 208 | return ts; 209 | } 210 | } 211 | 212 | 213 | /* 214 | ** Create or reuse a zero-terminated string, first checking in the 215 | ** cache (using the string address as a key). The cache can contain 216 | ** only zero-terminated strings, so it is safe to use 'strcmp' to 217 | ** check hits. 218 | */ 219 | TString *luaS_new (lua_State *L, const char *str) { 220 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ 221 | int j; 222 | TString **p = G(L)->strcache[i]; 223 | for (j = 0; j < STRCACHE_M; j++) { 224 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ 225 | return p[j]; /* that is it */ 226 | } 227 | /* normal route */ 228 | for (j = STRCACHE_M - 1; j > 0; j--) 229 | p[j] = p[j - 1]; /* move out last element */ 230 | /* new element is first in the list */ 231 | p[0] = luaS_newlstr(L, str, strlen(str)); 232 | return p[0]; 233 | } 234 | 235 | 236 | Udata *luaS_newudata (lua_State *L, size_t s) { 237 | Udata *u; 238 | GCObject *o; 239 | if (s > MAX_SIZE - sizeof(Udata)) 240 | luaM_toobig(L); 241 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); 242 | u = gco2u(o); 243 | u->len = s; 244 | u->metatable = NULL; 245 | setuservalue(L, u, luaO_nilobject); 246 | return u; 247 | } 248 | 249 | -------------------------------------------------------------------------------- /plugins/lib/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | 17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 18 | #define sizeudata(u) sizeludata((u)->len) 19 | 20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21 | (sizeof(s)/sizeof(char))-1)) 22 | 23 | 24 | /* 25 | ** test whether a string is a reserved word 26 | */ 27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 28 | 29 | 30 | /* 31 | ** equality for short strings, which are always internalized 32 | */ 33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 34 | 35 | 36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_clearcache (global_State *g); 41 | LUAI_FUNC void luaS_init (lua_State *L); 42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /plugins/lib/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | /* 22 | ** writable version of 'gkey'; allows updates to individual fields, 23 | ** but not to the whole (which has incompatible type) 24 | */ 25 | #define wgkey(n) (&(n)->i_key.nk) 26 | 27 | #define invalidateTMcache(t) ((t)->flags = 0) 28 | 29 | 30 | /* true when 't' is using 'dummynode' as its hash part */ 31 | #define isdummy(t) ((t)->lastfree == NULL) 32 | 33 | 34 | /* allocated size for hash nodes */ 35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 36 | 37 | 38 | /* returns the key, given the value of a table entry */ 39 | #define keyfromval(v) \ 40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 41 | 42 | 43 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 44 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 45 | TValue *value); 46 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 47 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 48 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 49 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 50 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 51 | LUAI_FUNC Table *luaH_new (lua_State *L); 52 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 53 | unsigned int nhsize); 54 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 55 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 56 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 57 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /plugins/lib/lua/ltm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 30 | "no value", 31 | "nil", "boolean", udatatypename, "number", 32 | "string", "table", "function", udatatypename, "thread", 33 | "proto" /* this last case is used for tests only */ 34 | }; 35 | 36 | 37 | void luaT_init (lua_State *L) { 38 | static const char *const luaT_eventname[] = { /* ORDER TM */ 39 | "__index", "__newindex", 40 | "__gc", "__mode", "__len", "__eq", 41 | "__add", "__sub", "__mul", "__mod", "__pow", 42 | "__div", "__idiv", 43 | "__band", "__bor", "__bxor", "__shl", "__shr", 44 | "__unm", "__bnot", "__lt", "__le", 45 | "__concat", "__call" 46 | }; 47 | int i; 48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 51 | } 52 | } 53 | 54 | 55 | /* 56 | ** function to be used with macro "fasttm": optimized for absence of 57 | ** tag methods 58 | */ 59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 60 | const TValue *tm = luaH_getshortstr(events, ename); 61 | lua_assert(event <= TM_EQ); 62 | if (ttisnil(tm)) { /* no tag method? */ 63 | events->flags |= cast_byte(1u<metatable; 75 | break; 76 | case LUA_TUSERDATA: 77 | mt = uvalue(o)->metatable; 78 | break; 79 | default: 80 | mt = G(L)->mt[ttnov(o)]; 81 | } 82 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); 83 | } 84 | 85 | 86 | /* 87 | ** Return the name of the type of an object. For tables and userdata 88 | ** with metatable, use their '__name' metafield, if present. 89 | */ 90 | const char *luaT_objtypename (lua_State *L, const TValue *o) { 91 | Table *mt; 92 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 93 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { 94 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 95 | if (ttisstring(name)) /* is '__name' a string? */ 96 | return getstr(tsvalue(name)); /* use it as type name */ 97 | } 98 | return ttypename(ttnov(o)); /* else use standard type name */ 99 | } 100 | 101 | 102 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 103 | const TValue *p2, TValue *p3, int hasres) { 104 | ptrdiff_t result = savestack(L, p3); 105 | StkId func = L->top; 106 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 107 | setobj2s(L, func + 1, p1); /* 1st argument */ 108 | setobj2s(L, func + 2, p2); /* 2nd argument */ 109 | L->top += 3; 110 | if (!hasres) /* no result? 'p3' is third argument */ 111 | setobj2s(L, L->top++, p3); /* 3rd argument */ 112 | /* metamethod may yield only when called from Lua code */ 113 | if (isLua(L->ci)) 114 | luaD_call(L, func, hasres); 115 | else 116 | luaD_callnoyield(L, func, hasres); 117 | if (hasres) { /* if has result, move it to its place */ 118 | p3 = restorestack(L, result); 119 | setobjs2s(L, p3, --L->top); 120 | } 121 | } 122 | 123 | 124 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 125 | StkId res, TMS event) { 126 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 127 | if (ttisnil(tm)) 128 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 129 | if (ttisnil(tm)) return 0; 130 | luaT_callTM(L, tm, p1, p2, res, 1); 131 | return 1; 132 | } 133 | 134 | 135 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 136 | StkId res, TMS event) { 137 | if (!luaT_callbinTM(L, p1, p2, res, event)) { 138 | switch (event) { 139 | case TM_CONCAT: 140 | luaG_concaterror(L, p1, p2); 141 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 142 | case TM_BAND: case TM_BOR: case TM_BXOR: 143 | case TM_SHL: case TM_SHR: case TM_BNOT: { 144 | lua_Number dummy; 145 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 146 | luaG_tointerror(L, p1, p2); 147 | else 148 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 149 | } 150 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 151 | default: 152 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 153 | } 154 | } 155 | } 156 | 157 | 158 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 159 | TMS event) { 160 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 161 | return -1; /* no metamethod */ 162 | else 163 | return !l_isfalse(L->top); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /plugins/lib/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | 55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 56 | 57 | 58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 59 | 60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 62 | TMS event); 63 | LUAI_FUNC void luaT_init (lua_State *L); 64 | 65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 66 | const TValue *p2, TValue *p3, int hasres); 67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 68 | StkId res, TMS event); 69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 70 | StkId res, TMS event); 71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 72 | const TValue *p2, TMS event); 73 | 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /plugins/lib/lua/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /plugins/lib/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 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 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_BITLIBNAME "bit32" 39 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 40 | 41 | #define LUA_MATHLIBNAME "math" 42 | LUAMOD_API int (luaopen_math) (lua_State *L); 43 | 44 | #define LUA_DBLIBNAME "debug" 45 | LUAMOD_API int (luaopen_debug) (lua_State *L); 46 | 47 | #define LUA_LOADLIBNAME "package" 48 | LUAMOD_API int (luaopen_package) (lua_State *L); 49 | 50 | 51 | /* open all previous libraries */ 52 | LUALIB_API void (luaL_openlibs) (lua_State *L); 53 | 54 | 55 | 56 | #if !defined(lua_assert) 57 | #define lua_assert(x) ((void)0) 58 | #endif 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /plugins/lib/lua/lundump.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstring.h" 23 | #include "lundump.h" 24 | #include "lzio.h" 25 | 26 | 27 | #if !defined(luai_verifycode) 28 | #define luai_verifycode(L,b,f) /* empty */ 29 | #endif 30 | 31 | 32 | typedef struct { 33 | lua_State *L; 34 | ZIO *Z; 35 | const char *name; 36 | } LoadState; 37 | 38 | 39 | static l_noret error(LoadState *S, const char *why) { 40 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); 41 | luaD_throw(S->L, LUA_ERRSYNTAX); 42 | } 43 | 44 | 45 | /* 46 | ** All high-level loads go through LoadVector; you can change it to 47 | ** adapt to the endianness of the input 48 | */ 49 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 50 | 51 | static void LoadBlock (LoadState *S, void *b, size_t size) { 52 | if (luaZ_read(S->Z, b, size) != 0) 53 | error(S, "truncated"); 54 | } 55 | 56 | 57 | #define LoadVar(S,x) LoadVector(S,&x,1) 58 | 59 | 60 | static lu_byte LoadByte (LoadState *S) { 61 | lu_byte x; 62 | LoadVar(S, x); 63 | return x; 64 | } 65 | 66 | 67 | static int LoadInt (LoadState *S) { 68 | int x; 69 | LoadVar(S, x); 70 | return x; 71 | } 72 | 73 | 74 | static lua_Number LoadNumber (LoadState *S) { 75 | lua_Number x; 76 | LoadVar(S, x); 77 | return x; 78 | } 79 | 80 | 81 | static lua_Integer LoadInteger (LoadState *S) { 82 | lua_Integer x; 83 | LoadVar(S, x); 84 | return x; 85 | } 86 | 87 | 88 | static TString *LoadString (LoadState *S) { 89 | size_t size = LoadByte(S); 90 | if (size == 0xFF) 91 | LoadVar(S, size); 92 | if (size == 0) 93 | return NULL; 94 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ 95 | char buff[LUAI_MAXSHORTLEN]; 96 | LoadVector(S, buff, size); 97 | return luaS_newlstr(S->L, buff, size); 98 | } 99 | else { /* long string */ 100 | TString *ts = luaS_createlngstrobj(S->L, size); 101 | LoadVector(S, getstr(ts), size); /* load directly in final place */ 102 | return ts; 103 | } 104 | } 105 | 106 | 107 | static void LoadCode (LoadState *S, Proto *f) { 108 | int n = LoadInt(S); 109 | f->code = luaM_newvector(S->L, n, Instruction); 110 | f->sizecode = n; 111 | LoadVector(S, f->code, n); 112 | } 113 | 114 | 115 | static void LoadFunction(LoadState *S, Proto *f, TString *psource); 116 | 117 | 118 | static void LoadConstants (LoadState *S, Proto *f) { 119 | int i; 120 | int n = LoadInt(S); 121 | f->k = luaM_newvector(S->L, n, TValue); 122 | f->sizek = n; 123 | for (i = 0; i < n; i++) 124 | setnilvalue(&f->k[i]); 125 | for (i = 0; i < n; i++) { 126 | TValue *o = &f->k[i]; 127 | int t = LoadByte(S); 128 | switch (t) { 129 | case LUA_TNIL: 130 | setnilvalue(o); 131 | break; 132 | case LUA_TBOOLEAN: 133 | setbvalue(o, LoadByte(S)); 134 | break; 135 | case LUA_TNUMFLT: 136 | setfltvalue(o, LoadNumber(S)); 137 | break; 138 | case LUA_TNUMINT: 139 | setivalue(o, LoadInteger(S)); 140 | break; 141 | case LUA_TSHRSTR: 142 | case LUA_TLNGSTR: 143 | setsvalue2n(S->L, o, LoadString(S)); 144 | break; 145 | default: 146 | lua_assert(0); 147 | } 148 | } 149 | } 150 | 151 | 152 | static void LoadProtos (LoadState *S, Proto *f) { 153 | int i; 154 | int n = LoadInt(S); 155 | f->p = luaM_newvector(S->L, n, Proto *); 156 | f->sizep = n; 157 | for (i = 0; i < n; i++) 158 | f->p[i] = NULL; 159 | for (i = 0; i < n; i++) { 160 | f->p[i] = luaF_newproto(S->L); 161 | LoadFunction(S, f->p[i], f->source); 162 | } 163 | } 164 | 165 | 166 | static void LoadUpvalues (LoadState *S, Proto *f) { 167 | int i, n; 168 | n = LoadInt(S); 169 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); 170 | f->sizeupvalues = n; 171 | for (i = 0; i < n; i++) 172 | f->upvalues[i].name = NULL; 173 | for (i = 0; i < n; i++) { 174 | f->upvalues[i].instack = LoadByte(S); 175 | f->upvalues[i].idx = LoadByte(S); 176 | } 177 | } 178 | 179 | 180 | static void LoadDebug (LoadState *S, Proto *f) { 181 | int i, n; 182 | n = LoadInt(S); 183 | f->lineinfo = luaM_newvector(S->L, n, int); 184 | f->sizelineinfo = n; 185 | LoadVector(S, f->lineinfo, n); 186 | n = LoadInt(S); 187 | f->locvars = luaM_newvector(S->L, n, LocVar); 188 | f->sizelocvars = n; 189 | for (i = 0; i < n; i++) 190 | f->locvars[i].varname = NULL; 191 | for (i = 0; i < n; i++) { 192 | f->locvars[i].varname = LoadString(S); 193 | f->locvars[i].startpc = LoadInt(S); 194 | f->locvars[i].endpc = LoadInt(S); 195 | } 196 | n = LoadInt(S); 197 | for (i = 0; i < n; i++) 198 | f->upvalues[i].name = LoadString(S); 199 | } 200 | 201 | 202 | static void LoadFunction (LoadState *S, Proto *f, TString *psource) { 203 | f->source = LoadString(S); 204 | if (f->source == NULL) /* no source in dump? */ 205 | f->source = psource; /* reuse parent's source */ 206 | f->linedefined = LoadInt(S); 207 | f->lastlinedefined = LoadInt(S); 208 | f->numparams = LoadByte(S); 209 | f->is_vararg = LoadByte(S); 210 | f->maxstacksize = LoadByte(S); 211 | LoadCode(S, f); 212 | LoadConstants(S, f); 213 | LoadUpvalues(S, f); 214 | LoadProtos(S, f); 215 | LoadDebug(S, f); 216 | } 217 | 218 | 219 | static void checkliteral (LoadState *S, const char *s, const char *msg) { 220 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 221 | size_t len = strlen(s); 222 | LoadVector(S, buff, len); 223 | if (memcmp(s, buff, len) != 0) 224 | error(S, msg); 225 | } 226 | 227 | 228 | static void fchecksize (LoadState *S, size_t size, const char *tname) { 229 | if (LoadByte(S) != size) 230 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); 231 | } 232 | 233 | 234 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 235 | 236 | static void checkHeader (LoadState *S) { 237 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ 238 | if (LoadByte(S) != LUAC_VERSION) 239 | error(S, "version mismatch in"); 240 | if (LoadByte(S) != LUAC_FORMAT) 241 | error(S, "format mismatch in"); 242 | checkliteral(S, LUAC_DATA, "corrupted"); 243 | checksize(S, int); 244 | checksize(S, size_t); 245 | checksize(S, Instruction); 246 | checksize(S, lua_Integer); 247 | checksize(S, lua_Number); 248 | if (LoadInteger(S) != LUAC_INT) 249 | error(S, "endianness mismatch in"); 250 | if (LoadNumber(S) != LUAC_NUM) 251 | error(S, "float format mismatch in"); 252 | } 253 | 254 | 255 | /* 256 | ** load precompiled chunk 257 | */ 258 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { 259 | LoadState S; 260 | LClosure *cl; 261 | if (*name == '@' || *name == '=') 262 | S.name = name + 1; 263 | else if (*name == LUA_SIGNATURE[0]) 264 | S.name = "binary string"; 265 | else 266 | S.name = name; 267 | S.L = L; 268 | S.Z = Z; 269 | checkHeader(&S); 270 | cl = luaF_newLclosure(L, LoadByte(&S)); 271 | setclLvalue(L, L->top, cl); 272 | luaD_inctop(L); 273 | cl->p = luaF_newproto(L); 274 | LoadFunction(&S, cl->p, NULL); 275 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 276 | luai_verifycode(L, buff, cl->p); 277 | return cl; 278 | } 279 | 280 | -------------------------------------------------------------------------------- /plugins/lib/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 30 | void* data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /plugins/lib/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | #define tonumber(o,n) \ 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 42 | 43 | #define tointeger(o,i) \ 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 45 | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 47 | 48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 49 | 50 | 51 | /* 52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, 53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, 54 | ** return 0 (meaning it will have to check metamethod) with 'slot' 55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). 56 | ** 'f' is the raw get function to use. 57 | */ 58 | #define luaV_fastget(L,t,k,slot,f) \ 59 | (!ttistable(t) \ 60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 62 | !ttisnil(slot))) /* result not nil? */ 63 | 64 | /* 65 | ** standard implementation for 'gettable' 66 | */ 67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \ 68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 69 | else luaV_finishget(L,t,k,v,slot); } 70 | 71 | 72 | /* 73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil, 74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise, 75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or 76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro 77 | ** returns true, there is no need to 'invalidateTMcache', because the 78 | ** call is not creating a new entry. 79 | */ 80 | #define luaV_fastset(L,t,k,slot,f,v) \ 81 | (!ttistable(t) \ 82 | ? (slot = NULL, 0) \ 83 | : (slot = f(hvalue(t), k), \ 84 | ttisnil(slot) ? 0 \ 85 | : (luaC_barrierback(L, hvalue(t), v), \ 86 | setobj2t(L, cast(TValue *,slot), v), \ 87 | 1))) 88 | 89 | 90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \ 91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 92 | luaV_finishset(L,t,k,v,slot); } 93 | 94 | 95 | 96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); 101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 102 | StkId val, const TValue *slot); 103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 104 | StkId val, const TValue *slot); 105 | LUAI_FUNC void luaV_finishOp (lua_State *L); 106 | LUAI_FUNC void luaV_execute (lua_State *L); 107 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /plugins/lib/lua/lzio.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /plugins/lib/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /plugins/lib/sdk/amx/getch.h: -------------------------------------------------------------------------------- 1 | /* Extremely inefficient but portable POSIX getch(), see getch.c */ 2 | #ifndef GETCH_H 3 | #define GETCH_H 4 | 5 | #if defined __cplusplus 6 | extern "C" { 7 | #endif 8 | int getch(void); 9 | int kbhit(void); 10 | 11 | #if defined __cplusplus 12 | } 13 | #endif 14 | 15 | #endif /* GETCH_H */ 16 | -------------------------------------------------------------------------------- /plugins/lib/sdk/amx/sclinux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Things needed to compile under linux. 3 | * 4 | * Should be reworked totally to use GNU's 'configure' 5 | */ 6 | #ifndef SCLINUX_H 7 | #define SCLINUX_H 8 | 9 | /* getchar() is not a 'cool' replacement for MSDOS getch: Linux/unix depends on the features activated or not about the 10 | * controlling terminal's tty. This means that ioctl(2) calls must be performed, for instance to have the controlling 11 | * terminal tty's in 'raw' mode, if we want to be able to fetch a single character. This also means that everything must 12 | * be put back correctly when the function ends. See GETCH.C for an implementation. 13 | * 14 | * For interactive use of SRUN/SDBG if would be much better to use GNU's readline package: the user would be able to 15 | * have a complete emacs/vi like line editing system. 16 | */ 17 | #include "getch.h" 18 | 19 | #define stricmp(a,b) strcasecmp(a,b) 20 | #define strnicmp(a,b,c) strncasecmp(a,b,c) 21 | 22 | /* 23 | * WinWorld wants '\'. Unices do not. 24 | */ 25 | #define DIRECTORY_SEP_CHAR '/' 26 | #define DIRECTORY_SEP_STR "/" 27 | 28 | /* 29 | * SC assumes that a computer is Little Endian unless told otherwise. It uses 30 | * (and defines) the macros BYTE_ORDER and BIG_ENDIAN. 31 | * For Linux, we must overrule these settings with those defined in glibc. 32 | */ 33 | #if !defined __BYTE_ORDER 34 | # include 35 | #endif 36 | 37 | #if defined __OpenBSD__ || defined __FreeBSD__ 38 | # define __BYTE_ORDER BYTE_ORDER 39 | # define __LITTLE_ENDIAN LITTLE_ENDIAN 40 | # define __BIG_ENDIAN BIG_ENDIAN 41 | #endif 42 | 43 | #if !defined __BYTE_ORDER 44 | # error "Can't figure computer byte order (__BYTE_ORDER macro not found)" 45 | #endif 46 | 47 | #endif /* SCLINUX_H */ 48 | -------------------------------------------------------------------------------- /plugins/lib/sdk/plugin.h: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------- 2 | // 3 | // SA-MP Multiplayer Modification For GTA:SA 4 | // Copyright 2004-2009 SA-MP Team 5 | // 6 | //---------------------------------------------------------- 7 | 8 | #include "plugincommon.h" 9 | #include "amx/amx.h" 10 | 11 | //---------------------------------------------------------- 12 | // EOF 13 | -------------------------------------------------------------------------------- /plugins/lib/sdk/plugincommon.h: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------- 2 | // 3 | // SA-MP Multiplayer Modification For GTA:SA 4 | // Copyright 2004-2009 SA-MP Team 5 | // 6 | //---------------------------------------------------------- 7 | 8 | #pragma once 9 | 10 | //---------------------------------------------------------- 11 | 12 | #define SAMP_PLUGIN_VERSION 0x0200 13 | 14 | //---------------------------------------------------------- 15 | 16 | #ifdef __cplusplus 17 | #define PLUGIN_EXTERN_C extern "C" 18 | #else 19 | #define PLUGIN_EXTERN_C 20 | #endif 21 | 22 | #if defined __linux__ || defined __FreeBSD__ || defined __OpenBSD__ 23 | #ifndef __GNUC__ 24 | #pragma message "Warning: Not using a GNU compiler." 25 | #endif 26 | #define PLUGIN_CALL 27 | #ifndef SAMPSVR 28 | // Compile code with -fvisibility=hidden to hide non-exported functions. 29 | #define PLUGIN_EXPORT PLUGIN_EXTERN_C __attribute__((visibility("default"))) 30 | #else 31 | #define PLUGIN_EXPORT PLUGIN_EXTERN_C 32 | #endif 33 | #elif defined _WIN32 34 | #ifndef _MSC_VER 35 | #pragma message "Warning: Not using a VC++ compiler." 36 | #endif 37 | #define PLUGIN_CALL __stdcall 38 | #define PLUGIN_EXPORT PLUGIN_EXTERN_C 39 | #else 40 | #error "Unknown operating system." 41 | #endif 42 | 43 | //---------------------------------------------------------- 44 | 45 | enum SUPPORTS_FLAGS 46 | { 47 | SUPPORTS_VERSION = SAMP_PLUGIN_VERSION, 48 | SUPPORTS_VERSION_MASK = 0xffff, 49 | SUPPORTS_AMX_NATIVES = 0x10000, 50 | SUPPORTS_PROCESS_TICK = 0x20000 51 | }; 52 | 53 | //---------------------------------------------------------- 54 | 55 | enum PLUGIN_DATA_TYPE 56 | { 57 | // For some debugging 58 | PLUGIN_DATA_LOGPRINTF = 0x00, // void (*logprintf)(char* format, ...) 59 | PLUGIN_DATA_NETGAME = 0xE1, // CNetGame* GetNetGame(); 60 | PLUGIN_DATA_RAKSERVER = 0xE2, // RakServerInterface* PluginGetRakServer() 61 | PLUGIN_DATA_LOADFSCRIPT = 0xE3, // bool LoadFilterscriptFromMemory(char* pFileName, char* pFileData) 62 | PLUGIN_DATA_UNLOADFSCRIPT = 0xE5, // bool UnloadFilterScript(char* pFileName) 63 | PLUGIN_DATA_CONSOLE = 0xE4, // CConsole* GetConsole(); 64 | 65 | // AMX 66 | PLUGIN_DATA_AMX_EXPORTS = 0x10, // void* AmxFunctionTable[] (see PLUGIN_AMX_EXPORT) 67 | PLUGIN_DATA_CALLPUBLIC_FS = 0x11, // int (*AmxCallPublicFilterScript)(char *szFunctionName) 68 | PLUGIN_DATA_CALLPUBLIC_GM = 0x12, // int (*AmxCallPublicGameMode)(char *szFunctionName) 69 | 70 | }; 71 | 72 | //---------------------------------------------------------- 73 | 74 | enum PLUGIN_AMX_EXPORT 75 | { 76 | PLUGIN_AMX_EXPORT_Align16 = 0, 77 | PLUGIN_AMX_EXPORT_Align32 = 1, 78 | PLUGIN_AMX_EXPORT_Align64 = 2, 79 | PLUGIN_AMX_EXPORT_Allot = 3, 80 | PLUGIN_AMX_EXPORT_Callback = 4, 81 | PLUGIN_AMX_EXPORT_Cleanup = 5, 82 | PLUGIN_AMX_EXPORT_Clone = 6, 83 | PLUGIN_AMX_EXPORT_Exec = 7, 84 | PLUGIN_AMX_EXPORT_FindNative = 8, 85 | PLUGIN_AMX_EXPORT_FindPublic = 9, 86 | PLUGIN_AMX_EXPORT_FindPubVar = 10, 87 | PLUGIN_AMX_EXPORT_FindTagId = 11, 88 | PLUGIN_AMX_EXPORT_Flags = 12, 89 | PLUGIN_AMX_EXPORT_GetAddr = 13, 90 | PLUGIN_AMX_EXPORT_GetNative = 14, 91 | PLUGIN_AMX_EXPORT_GetPublic = 15, 92 | PLUGIN_AMX_EXPORT_GetPubVar = 16, 93 | PLUGIN_AMX_EXPORT_GetString = 17, 94 | PLUGIN_AMX_EXPORT_GetTag = 18, 95 | PLUGIN_AMX_EXPORT_GetUserData = 19, 96 | PLUGIN_AMX_EXPORT_Init = 20, 97 | PLUGIN_AMX_EXPORT_InitJIT = 21, 98 | PLUGIN_AMX_EXPORT_MemInfo = 22, 99 | PLUGIN_AMX_EXPORT_NameLength = 23, 100 | PLUGIN_AMX_EXPORT_NativeInfo = 24, 101 | PLUGIN_AMX_EXPORT_NumNatives = 25, 102 | PLUGIN_AMX_EXPORT_NumPublics = 26, 103 | PLUGIN_AMX_EXPORT_NumPubVars = 27, 104 | PLUGIN_AMX_EXPORT_NumTags = 28, 105 | PLUGIN_AMX_EXPORT_Push = 29, 106 | PLUGIN_AMX_EXPORT_PushArray = 30, 107 | PLUGIN_AMX_EXPORT_PushString = 31, 108 | PLUGIN_AMX_EXPORT_RaiseError = 32, 109 | PLUGIN_AMX_EXPORT_Register = 33, 110 | PLUGIN_AMX_EXPORT_Release = 34, 111 | PLUGIN_AMX_EXPORT_SetCallback = 35, 112 | PLUGIN_AMX_EXPORT_SetDebugHook = 36, 113 | PLUGIN_AMX_EXPORT_SetString = 37, 114 | PLUGIN_AMX_EXPORT_SetUserData = 38, 115 | PLUGIN_AMX_EXPORT_StrLen = 39, 116 | PLUGIN_AMX_EXPORT_UTF8Check = 40, 117 | PLUGIN_AMX_EXPORT_UTF8Get = 41, 118 | PLUGIN_AMX_EXPORT_UTF8Len = 42, 119 | PLUGIN_AMX_EXPORT_UTF8Put = 43, 120 | }; 121 | 122 | //---------------------------------------------------------- 123 | // EOF 124 | -------------------------------------------------------------------------------- /plugins/lib/subhook/subhook.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012-2015 Zeex 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 17 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 | * POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include "subhook.h" 27 | #include "subhook_private.h" 28 | 29 | SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_src(subhook_t hook) { 30 | return hook->src; 31 | } 32 | 33 | SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_dst(subhook_t hook) { 34 | return hook->dst; 35 | } 36 | 37 | SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_trampoline(subhook_t hook) { 38 | return hook->trampoline; 39 | } 40 | 41 | SUBHOOK_EXPORT int SUBHOOK_API subhook_is_installed(subhook_t hook) { 42 | return hook->installed; 43 | } 44 | 45 | #if defined SUBHOOK_WINDOWS 46 | #include "subhook_windows.h" 47 | #elif defined SUBHOOK_UNIX 48 | #include "subhook_unix.h" 49 | #endif 50 | 51 | #if defined SUBHOOK_X86 || defined SUBHOOK_X86_64 52 | #include "subhook_x86.h" 53 | #endif 54 | -------------------------------------------------------------------------------- /plugins/lib/subhook/subhook_linux.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012-2015 Zeex 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 17 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 | * POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | void *subhook_unprotect(void *address, size_t size) { 31 | long pagesize; 32 | 33 | pagesize = sysconf(_SC_PAGESIZE); 34 | address = (void *)((long)address & ~(pagesize - 1)); 35 | 36 | if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) 37 | return NULL; 38 | 39 | return address; 40 | } 41 | -------------------------------------------------------------------------------- /plugins/lib/subhook/subhook_private.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012-2015 Zeex 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 17 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 | * POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #ifndef SUBHOOK_PRIVATE_H 27 | #define SUBHOOK_PRIVATE_H 28 | 29 | #include 30 | 31 | struct subhook_struct { 32 | int installed; 33 | void *src; 34 | void *dst; 35 | subhook_options_t options; 36 | void *code; 37 | void *trampoline; 38 | size_t jmp_size; 39 | size_t trampoline_size; 40 | size_t trampoline_len; 41 | }; 42 | 43 | void *subhook_unprotect(void *address, size_t size); 44 | 45 | #endif /* SUBHOOK_PRIVATE_H */ 46 | -------------------------------------------------------------------------------- /plugins/lib/subhook/subhook_unix.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012-2015 Zeex 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 17 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 | * POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | void *subhook_unprotect(void *address, size_t size) { 31 | long pagesize; 32 | 33 | pagesize = sysconf(_SC_PAGESIZE); 34 | address = (void *)((long)address & ~(pagesize - 1)); 35 | 36 | if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) { 37 | return NULL; 38 | } 39 | 40 | return address; 41 | } 42 | -------------------------------------------------------------------------------- /plugins/lib/subhook/subhook_windows.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012-2015 Zeex 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 17 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 | * POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | 29 | void *subhook_unprotect(void *address, size_t size) { 30 | DWORD old; 31 | 32 | if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &old) == 0) { 33 | return NULL; 34 | } 35 | 36 | return address; 37 | } 38 | -------------------------------------------------------------------------------- /plugins/makefile: -------------------------------------------------------------------------------- 1 | GPP = g++ -D _GLIBCXX_USE_CXX11_ABI=0 -m32 -std=c++11 -Ilib -Isrc -fno-stack-protector 2 | GCC = gcc -D _GLIBCXX_USE_CXX11_ABI=0 -m32 -Ilib -Isrc -fno-stack-protector 3 | LINK = $(GPP) -Wl,-z,defs -lstdc++ 4 | PP_OUTFILE = "./YALP.so" 5 | 6 | COMPILE_FLAGS = -c -O3 -fPIC -w -DLINUX -pthread -fno-operator-names 7 | 8 | YALP = -D YALP $(COMPILE_FLAGS) 9 | 10 | all: YALP 11 | 12 | clean: 13 | -rm -f *~ *.o *.so 14 | 15 | static: GPP = g++ -D _GLIBCXX_USE_CXX11_ABI=0 -m32 -std=c++11 -Ilib -Isrc -fno-stack-protector -static-libgcc -static-libstdc++ 16 | static: GCC = gcc -D _GLIBCXX_USE_CXX11_ABI=0 -m32 -Ilib -Isrc -fno-stack-protector -static-libgcc -static-libstdc++ 17 | static: LINK = $(GPP) -Wl,-z,defs 18 | static: all 19 | 20 | YALP: clean 21 | $(GPP) $(YALP) ./lib/sdk/*.cpp 22 | $(GCC) $(YALP) ./lib/subhook/*.c 23 | $(GPP) $(YALP) ./lib/lua/*.cpp 24 | $(GPP) $(YALP) ./src/amx/*.cpp 25 | $(GPP) $(YALP) ./src/lua/interop/*.cpp 26 | $(GPP) $(YALP) ./src/lua/*.cpp 27 | $(GPP) $(YALP) ./src/*.cpp 28 | $(LINK) -pthread -shared -o $(PP_OUTFILE) *.o 29 | -------------------------------------------------------------------------------- /plugins/src/amx/amxutils.cpp: -------------------------------------------------------------------------------- 1 | #include "amxutils.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #ifdef _WIN32 8 | #define bswap32 _byteswap_ulong 9 | #else 10 | #define bswap32 __builtin_bswap32 11 | #endif 12 | 13 | const char *amx::StrError(int errnum) 14 | { 15 | static const char *messages[] = { 16 | /* AMX_ERR_NONE */ "(none)", 17 | /* AMX_ERR_EXIT */ "Forced exit", 18 | /* AMX_ERR_ASSERT */ "Assertion failed", 19 | /* AMX_ERR_STACKERR */ "Stack/heap collision (insufficient stack size)", 20 | /* AMX_ERR_BOUNDS */ "Array index out of bounds", 21 | /* AMX_ERR_MEMACCESS */ "Invalid memory access", 22 | /* AMX_ERR_INVINSTR */ "Invalid instruction", 23 | /* AMX_ERR_STACKLOW */ "Stack underflow", 24 | /* AMX_ERR_HEAPLOW */ "Heap underflow", 25 | /* AMX_ERR_CALLBACK */ "No (valid) native function callback", 26 | /* AMX_ERR_NATIVE */ "Native function failed", 27 | /* AMX_ERR_DIVIDE */ "Divide by zero", 28 | /* AMX_ERR_SLEEP */ "(sleep mode)", 29 | /* 13 */ "(reserved)", 30 | /* 14 */ "(reserved)", 31 | /* 15 */ "(reserved)", 32 | /* AMX_ERR_MEMORY */ "Out of memory", 33 | /* AMX_ERR_FORMAT */ "Invalid/unsupported P-code file format", 34 | /* AMX_ERR_VERSION */ "File is for a newer version of the AMX", 35 | /* AMX_ERR_NOTFOUND */ "File or function is not found", 36 | /* AMX_ERR_INDEX */ "Invalid index parameter (bad entry point)", 37 | /* AMX_ERR_DEBUG */ "Debugger cannot run", 38 | /* AMX_ERR_INIT */ "AMX not initialized (or doubly initialized)", 39 | /* AMX_ERR_USERDATA */ "Unable to set user data field (table full)", 40 | /* AMX_ERR_INIT_JIT */ "Cannot initialize the JIT", 41 | /* AMX_ERR_PARAMS */ "Parameter error", 42 | /* AMX_ERR_DOMAIN */ "Domain error, expression result does not fit in range", 43 | /* AMX_ERR_GENERAL */ "General error (unknown or unspecific error)", 44 | }; 45 | if(errnum < 0 || (size_t)errnum >= sizeof(messages) / sizeof(*messages)) 46 | return "(unknown)"; 47 | return messages[errnum]; 48 | } 49 | 50 | std::string amx::GetString(const cell *source, size_t size, bool cstring) 51 | { 52 | if(source == nullptr) return {}; 53 | 54 | std::string str; 55 | if(static_cast(*source) > UNPACKEDMAX) 56 | { 57 | cell c = 0; 58 | int i = sizeof(cell) - 1; 59 | while(true) 60 | { 61 | if(i == sizeof(cell) - 1) 62 | { 63 | c = *source++; 64 | if(size-- == 0) 65 | { 66 | break; 67 | } 68 | } 69 | char ch = c >> (i * 8); 70 | if(cstring && ch == '\0') break; 71 | str.push_back(ch); 72 | i = (i + sizeof(cell) - 1) % sizeof(cell); 73 | } 74 | }else{ 75 | cell c; 76 | while(true) 77 | { 78 | c = *source++; 79 | if(size-- == 0) 80 | { 81 | break; 82 | } 83 | if(cstring && c == 0) break; 84 | str.push_back(c); 85 | } 86 | } 87 | return str; 88 | } 89 | 90 | void amx::SetString(cell *dest, const char *source, size_t len, bool pack) 91 | { 92 | if(!pack) 93 | { 94 | while(len-- > 0) 95 | { 96 | unsigned char c = *source++; 97 | if(c == '\0') 98 | { 99 | *dest++ = 0xFFFF00; 100 | }else{ 101 | *dest++ = c; 102 | } 103 | } 104 | *dest = 0; 105 | }else{ 106 | dest[len / sizeof(cell)] = 0; 107 | std::memcpy(dest, source, len); 108 | len = (len + sizeof(cell) - 1) / sizeof(cell); 109 | 110 | while(len-- > 0) 111 | { 112 | *dest = bswap32(*dest); 113 | dest++; 114 | } 115 | } 116 | } 117 | 118 | constexpr cell STKMARGIN = 16 * sizeof(cell); 119 | 120 | bool amx::MemCheck(AMX *amx, size_t size) 121 | { 122 | return (cell)size >= 0 && amx->hea + (cell)size + STKMARGIN <= amx->stk; 123 | } 124 | 125 | std::unordered_map> handle_map; 126 | 127 | std::shared_ptr amx::GetHandle(AMX *amx) 128 | { 129 | auto it = handle_map.find(amx); 130 | if(it != handle_map.end()) 131 | { 132 | return it->second; 133 | } 134 | return handle_map[amx] = std::make_shared(amx); 135 | } 136 | 137 | void amx::RemoveHandle(AMX *amx) 138 | { 139 | handle_map.erase(amx); 140 | } 141 | -------------------------------------------------------------------------------- /plugins/src/amx/amxutils.h: -------------------------------------------------------------------------------- 1 | #ifndef AMXUTILS_H_INCLUDED 2 | #define AMXUTILS_H_INCLUDED 3 | 4 | #include "sdk/amx/amx.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace amx 11 | { 12 | class Extra 13 | { 14 | protected: 15 | AMX *_amx; 16 | 17 | public: 18 | Extra(AMX *amx) : _amx(amx) 19 | { 20 | 21 | } 22 | 23 | virtual ~Extra() = default; 24 | }; 25 | 26 | class Instance 27 | { 28 | AMX *_amx; 29 | std::unordered_map> extras; 30 | 31 | public: 32 | Instance() : _amx(nullptr) 33 | { 34 | 35 | } 36 | 37 | explicit Instance(AMX *amx) : _amx(amx) 38 | { 39 | 40 | } 41 | 42 | Instance(const Instance &obj) = delete; 43 | Instance &operator=(const Instance &obj) = delete; 44 | 45 | template 46 | ExtraType &get_extra() 47 | { 48 | std::type_index key = typeid(ExtraType); 49 | 50 | auto it = extras.find(key); 51 | if(it == extras.end()) 52 | { 53 | it = extras.insert(std::make_pair(key, std::unique_ptr(new ExtraType(_amx)))).first; 54 | } 55 | return static_cast(*it->second); 56 | } 57 | 58 | template 59 | bool has_extra() const 60 | { 61 | std::type_index key = typeid(ExtraType); 62 | 63 | return extras.find(key) != extras.end(); 64 | } 65 | 66 | template 67 | bool remove_extra() 68 | { 69 | std::type_index key = typeid(ExtraType); 70 | 71 | auto it = extras.find(key); 72 | if(it != extras.end()) 73 | { 74 | extras.erase(it); 75 | return true; 76 | } 77 | return false; 78 | } 79 | 80 | AMX *get() 81 | { 82 | return _amx; 83 | } 84 | 85 | operator AMX*() 86 | { 87 | return _amx; 88 | } 89 | }; 90 | 91 | const char *StrError(int error); 92 | std::string GetString(const cell *source, size_t size, bool cstring); 93 | void SetString(cell *dest, const char *source, size_t len, bool pack); 94 | bool MemCheck(AMX *amx, size_t size); 95 | std::shared_ptr GetHandle(AMX *amx); 96 | void RemoveHandle(AMX *amx); 97 | } 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /plugins/src/amx/fileutils.cpp: -------------------------------------------------------------------------------- 1 | #include "fileutils.h" 2 | #include "lua_utils.h" 3 | 4 | #ifdef _WIN32 5 | #include "amx/amxutils.h" 6 | #include "subhook/subhook.h" 7 | #include "subhook/subhook_private.h" 8 | #include 9 | #include 10 | #include 11 | #define WIN32_LEAN_AND_MEAN 12 | #include 13 | #include 14 | #else 15 | #include 16 | #endif 17 | #include 18 | 19 | #ifdef _WIN32 20 | struct file_extra : amx::Extra 21 | { 22 | AMX_NATIVE fseek = nullptr; 23 | AMX_NATIVE ftemp = nullptr; 24 | 25 | file_extra(AMX *amx) : amx::Extra(amx) 26 | { 27 | 28 | } 29 | }; 30 | #endif 31 | 32 | #ifdef _WIN32 33 | template 34 | class subhook_guard; 35 | 36 | template 37 | class subhook_guard 38 | { 39 | typedef Ret(__stdcall func_type)(Args...); 40 | 41 | subhook_t hook; 42 | 43 | static void *traverse(void *src) 44 | { 45 | auto dst = subhook_read_dst(src); 46 | if(dst != nullptr) 47 | { 48 | return traverse(dst); // something else is already hooking this function 49 | } 50 | return src; 51 | } 52 | 53 | public: 54 | subhook_guard(func_type *src, func_type *dst) : hook(subhook_new(traverse(reinterpret_cast(src)), reinterpret_cast(dst), {})) 55 | { 56 | subhook_install(hook); 57 | } 58 | 59 | Ret trampoline(Args...args) 60 | { 61 | auto ptr = subhook_get_trampoline(hook); 62 | if(ptr) 63 | { 64 | return reinterpret_cast(ptr)(std::forward(args)...); 65 | } 66 | 67 | auto src = subhook_get_src(hook); 68 | auto dst = subhook_read_dst(src); 69 | auto olddst = subhook_get_dst(hook); 70 | if(dst != olddst) 71 | { 72 | hook->dst = dst; 73 | subhook_remove(hook); 74 | auto ret = reinterpret_cast(src)(std::forward(args)...); 75 | subhook_install(hook); 76 | hook->dst = olddst; 77 | return ret; 78 | }else if(!dst) 79 | { 80 | return reinterpret_cast(src)(std::forward(args)...); 81 | }else{ 82 | subhook_remove(hook); 83 | auto ret = reinterpret_cast(src)(std::forward(args)...); 84 | subhook_install(hook); 85 | return ret; 86 | } 87 | } 88 | 89 | ~subhook_guard() 90 | { 91 | subhook_remove(hook); 92 | subhook_free(hook); 93 | } 94 | }; 95 | 96 | static thread_local struct { 97 | subhook_guard *hook; 98 | HANDLE handle; 99 | } sfp_info; 100 | 101 | DWORD WINAPI HookSetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) 102 | { 103 | auto &info = sfp_info; 104 | info.handle = hFile; 105 | return info.hook->trampoline(hFile, lDistanceToMove, lpDistanceToMoveHigh, dwMoveMethod); 106 | } 107 | #endif 108 | 109 | bool amx::FileLoad(cell value, AMX *amx, FILE *&f) 110 | { 111 | #ifdef _WIN32 112 | auto &info = sfp_info; 113 | info.handle = nullptr; 114 | { 115 | subhook_guard guard(&SetFilePointer, &HookSetFilePointer); 116 | info.hook = &guard; 117 | cell params[] = {3 * sizeof(cell), value, 0, 1}; 118 | amx::GetHandle(amx)->get_extra().fseek(amx, params); 119 | } 120 | 121 | HANDLE hfile; 122 | if(info.handle == nullptr || !DuplicateHandle(GetCurrentProcess(), info.handle, GetCurrentProcess(), &hfile, 0, TRUE, DUPLICATE_SAME_ACCESS)) 123 | { 124 | return false; 125 | } 126 | 127 | int fd = _open_osfhandle(reinterpret_cast(hfile), _O_RDWR); 128 | if(fd != -1) 129 | { 130 | f = _fdopen(fd, "r+"); 131 | if(!f) 132 | { 133 | _close(fd); 134 | } 135 | }else{ 136 | CloseHandle(hfile); 137 | } 138 | #else 139 | int fd = fileno(reinterpret_cast(value)); 140 | if(fd == -1) 141 | { 142 | return false; 143 | } 144 | fd = dup(fd); 145 | 146 | if(fd != -1) 147 | { 148 | int flags = fcntl(fd, F_GETFL); 149 | const char *access = (flags & O_RDWR) ? "r+" : ((flags & O_WRONLY) ? "w" : "r"); 150 | f = fdopen(fd, access); 151 | if(!f) 152 | { 153 | close(fd); 154 | } 155 | } 156 | #endif 157 | return true; 158 | } 159 | 160 | #ifdef _WIN32 161 | static thread_local struct { 162 | subhook_guard *hook; 163 | HANDLE handle; 164 | } cf_info; 165 | 166 | HANDLE WINAPI HookCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) 167 | { 168 | auto &info = cf_info; 169 | if(info.handle) 170 | { 171 | return info.handle; 172 | } 173 | return info.hook->trampoline(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 174 | } 175 | #endif 176 | 177 | cell amx::FileStore(FILE *file, AMX *amx) 178 | { 179 | cell value; 180 | #ifdef _WIN32 181 | int fd = _fileno(file); 182 | if(fd == -1) 183 | { 184 | return 0; 185 | } 186 | HANDLE hfile = reinterpret_cast(_get_osfhandle(fd)); 187 | if(hfile == nullptr || !DuplicateHandle(GetCurrentProcess(), hfile, GetCurrentProcess(), &hfile, 0, TRUE, DUPLICATE_SAME_ACCESS)) 188 | { 189 | return 0; 190 | } 191 | 192 | auto &info = cf_info; 193 | info.handle = hfile; 194 | { 195 | subhook_guard guard(&CreateFileA, &HookCreateFileA); 196 | info.hook = &guard; 197 | cell params[] = {0}; 198 | value = amx::GetHandle(amx)->get_extra().ftemp(amx, params); 199 | } 200 | #else 201 | int fd = fileno(file); 202 | if(fd == -1) 203 | { 204 | return 0; 205 | } 206 | fd = dup(fd); 207 | 208 | if(fd == -1) 209 | { 210 | return 0; 211 | } 212 | int flags = fcntl(fd, F_GETFL); 213 | const char *access = (flags & O_RDWR) ? "r+" : ((flags & O_WRONLY) ? "w" : "r"); 214 | value = reinterpret_cast(fdopen(fd, access)); 215 | if(!value) 216 | { 217 | close(fd); 218 | } 219 | #endif 220 | return value; 221 | } 222 | 223 | void amx::RegisterNatives(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number) 224 | { 225 | #ifdef _WIN32 226 | for(int i = 0; nativelist[i].name != nullptr && (i < number || number == -1); i++) 227 | { 228 | if(!std::strcmp(nativelist[i].name, "fseek")) 229 | { 230 | amx::GetHandle(amx)->get_extra().fseek = nativelist[i].func; 231 | }else if(!std::strcmp(nativelist[i].name, "ftemp")) 232 | { 233 | amx::GetHandle(amx)->get_extra().ftemp = nativelist[i].func; 234 | } 235 | } 236 | #endif 237 | } 238 | -------------------------------------------------------------------------------- /plugins/src/amx/fileutils.h: -------------------------------------------------------------------------------- 1 | #ifndef FILEUTILS_H_INCLUDED 2 | #define FILEUTILS_H_INCLUDED 3 | 4 | #include "sdk/amx/amx.h" 5 | #include "lua/lualibs.h" 6 | #include 7 | 8 | namespace amx 9 | { 10 | bool FileLoad(cell value, AMX *amx, FILE *&f); 11 | cell FileStore(FILE *file, AMX *amx); 12 | 13 | void RegisterNatives(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number); 14 | } 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /plugins/src/amx/loader.cpp: -------------------------------------------------------------------------------- 1 | #include "loader.h" 2 | #include "sdk/plugincommon.h" 3 | 4 | extern void **ppData; 5 | 6 | AMX *last_amx; 7 | std::function init_func; 8 | 9 | bool LoadFilterScriptFromMemory(const char* pFileName, const char* pFileData) 10 | { 11 | return reinterpret_cast(ppData[PLUGIN_DATA_LOADFSCRIPT])(const_cast(pFileName), const_cast(pFileData)); 12 | } 13 | 14 | bool UnloadFilterScript(const char* pFileName) 15 | { 16 | return reinterpret_cast(ppData[PLUGIN_DATA_UNLOADFSCRIPT])(const_cast(pFileName)); 17 | } 18 | 19 | bool amx::loader::Init(AMX *amx, void *program) 20 | { 21 | if(init_func && last_amx == nullptr) 22 | { 23 | last_amx = amx; 24 | amx->flags |= AMX_FLAG_RELOC; 25 | init_func(amx, program); 26 | return true; 27 | } 28 | return false; 29 | } 30 | 31 | AMX *amx::LoadProgram(const char *name, const char *program, std::function &&init) 32 | { 33 | last_amx = nullptr; 34 | init_func = std::move(init); 35 | bool ok = LoadFilterScriptFromMemory(name, program); 36 | AMX *amx = last_amx; 37 | last_amx = nullptr; 38 | init_func = nullptr; 39 | if(ok) 40 | { 41 | return amx; 42 | } 43 | return nullptr; 44 | } 45 | 46 | struct AMX_FAKE_HEADER : public AMX_HEADER 47 | { 48 | uint16_t namelength; 49 | }; 50 | 51 | AMX *amx::LoadNew(const char *name, int32_t heapspace, uint16_t namelength, std::function &&init) 52 | { 53 | AMX_FAKE_HEADER hdr{}; 54 | hdr.magic = AMX_MAGIC; 55 | hdr.file_version = 7; 56 | hdr.amx_version = MIN_AMX_VERSION; 57 | hdr.cod = hdr.dat = hdr.hea = hdr.size = sizeof(hdr); 58 | hdr.stp = hdr.hea + heapspace; 59 | hdr.defsize = sizeof(AMX_FUNCSTUBNT); 60 | hdr.nametable = reinterpret_cast(&hdr.namelength) - reinterpret_cast(&hdr); 61 | hdr.namelength = namelength; 62 | hdr.publics = hdr.natives = hdr.libraries = hdr.pubvars = hdr.tags = hdr.nametable; 63 | return LoadProgram(name, reinterpret_cast(&hdr), std::move(init)); 64 | } 65 | 66 | bool amx::Unload(const char *name) 67 | { 68 | return UnloadFilterScript(name); 69 | } 70 | -------------------------------------------------------------------------------- /plugins/src/amx/loader.h: -------------------------------------------------------------------------------- 1 | #ifndef LOADER_H_INCLUDED 2 | #define LOADER_H_INCLUDED 3 | 4 | #include "sdk/amx/amx.h" 5 | #include 6 | 7 | namespace amx 8 | { 9 | namespace loader 10 | { 11 | bool Init(AMX *amx, void *program); 12 | } 13 | AMX *LoadProgram(const char *name, const char *program, std::function &&init); 14 | AMX *LoadNew(const char *name, int32_t heapspace, uint16_t namelength, std::function &&init); 15 | bool Unload(const char *name); 16 | } 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /plugins/src/fixes/linux.h: -------------------------------------------------------------------------------- 1 | #ifndef LINUX_H_INCLUDED 2 | #define LINUX_H_INCLUDED 3 | 4 | #ifndef _WIN32 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace std { 11 | template struct _Unique_if { 12 | typedef unique_ptr _Single_object; 13 | }; 14 | 15 | template struct _Unique_if { 16 | typedef unique_ptr _Unknown_bound; 17 | }; 18 | 19 | template struct _Unique_if { 20 | typedef void _Known_bound; 21 | }; 22 | 23 | template 24 | typename _Unique_if::_Single_object 25 | make_unique(Args&&... args) { 26 | return unique_ptr(new T(std::forward(args)...)); 27 | } 28 | 29 | template 30 | typename _Unique_if::_Unknown_bound 31 | make_unique(size_t n) { 32 | typedef typename remove_extent::type U; 33 | return unique_ptr(new U[n]()); 34 | } 35 | 36 | template 37 | typename _Unique_if::_Known_bound 38 | make_unique(Args&&...) = delete; 39 | } 40 | 41 | #endif 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /plugins/src/hooks.cpp: -------------------------------------------------------------------------------- 1 | #include "hooks.h" 2 | #include "main.h" 3 | #include "lua_api.h" 4 | #include "lua/interop.h" 5 | #include "lua/interop/native.h" 6 | #include "lua/interop/public.h" 7 | #include "lua/interop/pubvar.h" 8 | #include "lua/interop/tags.h" 9 | #include "amx/loader.h" 10 | #include "amx/fileutils.h" 11 | 12 | #include "sdk/amx/amx.h" 13 | #include "sdk/plugincommon.h" 14 | #include "subhook/subhook.h" 15 | 16 | extern void *pAMXFunctions; 17 | 18 | template 19 | class amx_hook_func; 20 | 21 | template 22 | class amx_hook_func 23 | { 24 | public: 25 | typedef Ret hook_ftype(Ret(*)(Args...), Args...); 26 | 27 | typedef Ret AMXAPI handler_ftype(Args...); 28 | 29 | template 30 | static Ret AMXAPI handler(Args... args) 31 | { 32 | return Handler(reinterpret_cast(subhook_get_trampoline(Hook)), args...); 33 | } 34 | }; 35 | 36 | template 37 | class amx_hook; 38 | 39 | template 40 | class amx_hook_var 41 | { 42 | friend class amx_hook; 43 | 44 | static subhook_t hook; 45 | }; 46 | 47 | template 48 | subhook_t amx_hook_var::hook; 49 | 50 | template 51 | class amx_hook 52 | { 53 | typedef amx_hook_var var; 54 | 55 | public: 56 | template ::hook_ftype *Func> 57 | struct ctl 58 | { 59 | static void load() 60 | { 61 | typename amx_hook_func::handler_ftype *hookfn = &amx_hook_func::template handler; 62 | 63 | var::hook = subhook_new(reinterpret_cast(((FType*)pAMXFunctions)[Index]), reinterpret_cast(hookfn), {}); 64 | subhook_install(var::hook); 65 | } 66 | 67 | static void unload() 68 | { 69 | subhook_remove(var::hook); 70 | subhook_free(var::hook); 71 | } 72 | 73 | static void install() 74 | { 75 | subhook_install(var::hook); 76 | } 77 | 78 | static void uninstall() 79 | { 80 | subhook_remove(var::hook); 81 | } 82 | 83 | static FType orig() 84 | { 85 | if(subhook_is_installed(var::hook)) 86 | { 87 | return reinterpret_cast(subhook_get_trampoline(var::hook)); 88 | }else{ 89 | return ((FType*)pAMXFunctions)[Index]; 90 | } 91 | } 92 | }; 93 | }; 94 | 95 | #define AMX_HOOK_FUNC(Func, ...) Func(decltype(&::Func) _base_func, __VA_ARGS__) noexcept 96 | #define base_func _base_func 97 | #define amx_Hook(Func) amx_hook::ctl 98 | 99 | namespace hooks 100 | { 101 | int AMX_HOOK_FUNC(amx_Init, AMX *amx, void *program) 102 | { 103 | int ret = base_func(amx, program); 104 | if(ret == AMX_ERR_NONE) 105 | { 106 | amx::loader::Init(amx, program); 107 | } 108 | return ret; 109 | } 110 | 111 | int AMX_HOOK_FUNC(amx_Exec, AMX *amx, cell *retval, int index) 112 | { 113 | int result; 114 | if(lua::interop::amx_exec(amx, retval, index, result)) 115 | { 116 | return result; 117 | } 118 | int error = base_func(amx, retval, index); 119 | if(error == AMX_ERR_SLEEP) 120 | { 121 | return lua::bind(amx, retval, index); 122 | } 123 | return error; 124 | } 125 | 126 | int AMX_HOOK_FUNC(amx_Register, AMX *amx, const AMX_NATIVE_INFO *nativelist, int number) 127 | { 128 | int ret = base_func(amx, nativelist, number); 129 | lua::interop::amx_register_natives(amx, nativelist, number); 130 | amx::RegisterNatives(amx, nativelist, number); 131 | return ret; 132 | } 133 | 134 | int AMX_HOOK_FUNC(amx_FindPublic, AMX *amx, const char *funcname, int *index) 135 | { 136 | int error; 137 | if(lua::interop::amx_find_public(amx, funcname, index, error)) 138 | { 139 | return error; 140 | } 141 | 142 | return base_func(amx, funcname, index); 143 | } 144 | 145 | int AMX_HOOK_FUNC(amx_GetPublic, AMX *amx, int index, char *funcname) 146 | { 147 | if(lua::interop::amx_get_public(amx, index, funcname)) 148 | { 149 | return AMX_ERR_NONE; 150 | } 151 | 152 | return base_func(amx, index, funcname); 153 | } 154 | 155 | int AMX_HOOK_FUNC(amx_NumPublics, AMX *amx, int *number) 156 | { 157 | if(lua::interop::amx_num_publics(amx, number)) 158 | { 159 | return AMX_ERR_NONE; 160 | } 161 | 162 | return base_func(amx, number); 163 | } 164 | 165 | int AMX_HOOK_FUNC(amx_FindPubVar, AMX *amx, const char *varname, cell *amx_addr) 166 | { 167 | int error; 168 | if(lua::interop::amx_find_pubvar(amx, varname, amx_addr, error)) 169 | { 170 | return error; 171 | } 172 | 173 | return base_func(amx, varname, amx_addr); 174 | } 175 | 176 | int AMX_HOOK_FUNC(amx_GetPubVar, AMX *amx, int index, char *varname, cell *amx_addr) 177 | { 178 | if(lua::interop::amx_get_pubvar(amx, index, varname, amx_addr)) 179 | { 180 | return AMX_ERR_NONE; 181 | } 182 | 183 | return base_func(amx, index, varname, amx_addr); 184 | } 185 | 186 | int AMX_HOOK_FUNC(amx_NumPubVars, AMX *amx, int *number) 187 | { 188 | if(lua::interop::amx_num_pubvars(amx, number)) 189 | { 190 | return AMX_ERR_NONE; 191 | } 192 | 193 | return base_func(amx, number); 194 | } 195 | 196 | int AMX_HOOK_FUNC(amx_FindTagId, AMX *amx, cell tag_id, char *tagname) 197 | { 198 | if(lua::interop::amx_find_tag_id(amx, tag_id, tagname)) 199 | { 200 | return AMX_ERR_NONE; 201 | } 202 | 203 | return base_func(amx, tag_id, tagname); 204 | } 205 | 206 | int AMX_HOOK_FUNC(amx_GetTag, AMX *amx, int index, char *tagname, cell *tag_id) 207 | { 208 | if(lua::interop::amx_get_tag(amx, index, tagname, tag_id)) 209 | { 210 | return AMX_ERR_NONE; 211 | } 212 | 213 | return base_func(amx, index, tagname, tag_id); 214 | } 215 | 216 | int AMX_HOOK_FUNC(amx_NumTags, AMX *amx, int *number) 217 | { 218 | if(lua::interop::amx_num_tags(amx, number)) 219 | { 220 | return AMX_ERR_NONE; 221 | } 222 | 223 | return base_func(amx, number); 224 | } 225 | 226 | int AMX_HOOK_FUNC(amx_GetAddr, AMX *amx, cell amx_addr, cell **phys_addr) 227 | { 228 | if(lua::interop::amx_get_addr(amx, amx_addr, phys_addr)) 229 | { 230 | return AMX_ERR_NONE; 231 | } 232 | 233 | return base_func(amx, amx_addr, phys_addr); 234 | } 235 | } 236 | 237 | void hooks::load() 238 | { 239 | amx_Hook(Init)::load(); 240 | amx_Hook(Register)::load(); 241 | amx_Hook(Exec)::load(); 242 | amx_Hook(FindPublic)::load(); 243 | amx_Hook(GetPublic)::load(); 244 | amx_Hook(NumPublics)::load(); 245 | amx_Hook(FindPubVar)::load(); 246 | amx_Hook(GetPubVar)::load(); 247 | amx_Hook(NumPubVars)::load(); 248 | amx_Hook(FindTagId)::load(); 249 | amx_Hook(GetTag)::load(); 250 | amx_Hook(NumTags)::load(); 251 | amx_Hook(GetAddr)::load(); 252 | } 253 | 254 | void hooks::unload() 255 | { 256 | amx_Hook(Init)::unload(); 257 | amx_Hook(Register)::unload(); 258 | amx_Hook(Exec)::unload(); 259 | amx_Hook(FindPublic)::unload(); 260 | amx_Hook(GetPublic)::unload(); 261 | amx_Hook(NumPublics)::unload(); 262 | amx_Hook(FindPubVar)::unload(); 263 | amx_Hook(GetPubVar)::unload(); 264 | amx_Hook(NumPubVars)::unload(); 265 | amx_Hook(FindTagId)::unload(); 266 | amx_Hook(GetTag)::unload(); 267 | amx_Hook(NumTags)::unload(); 268 | amx_Hook(GetAddr)::unload(); 269 | } 270 | -------------------------------------------------------------------------------- /plugins/src/hooks.h: -------------------------------------------------------------------------------- 1 | #ifndef HOOKS_H_INCLUDED 2 | #define HOOKS_H_INCLUDED 3 | 4 | namespace hooks 5 | { 6 | void load(); 7 | void unload(); 8 | } 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /plugins/src/lua/interop.cpp: -------------------------------------------------------------------------------- 1 | #include "interop.h" 2 | #include "lua_utils.h" 3 | #include "lua_api.h" 4 | #include "amx/amxutils.h" 5 | #include "amx/loader.h" 6 | #include "interop/native.h" 7 | #include "interop/public.h" 8 | #include "interop/pubvar.h" 9 | #include "interop/memory.h" 10 | #include "interop/string.h" 11 | #include "interop/result.h" 12 | #include "interop/file.h" 13 | #include "interop/tags.h" 14 | #include "interop/sleep.h" 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | std::unordered_map> amx_map; 22 | 23 | class amx_info 24 | { 25 | public: 26 | lua_State *L; 27 | std::string fs_name; 28 | int self = 0; 29 | AMX *amx = nullptr; 30 | 31 | amx_info(lua_State *L) : L(L) 32 | { 33 | 34 | } 35 | 36 | ~amx_info() 37 | { 38 | amx_map.erase(amx); 39 | if(amx && !fs_name.empty() && amx::Unload(fs_name.c_str())) 40 | { 41 | amx = nullptr; 42 | } 43 | } 44 | }; 45 | 46 | int forward(lua_State *L) 47 | { 48 | luaL_checktype(L, 1, LUA_TFUNCTION); 49 | lua_pushvalue(L, lua_upvalueindex(1)); 50 | lua_getfield(L, lua_upvalueindex(1), "#lua"); 51 | int numresults = lua::numresults(L); 52 | lua_pushinteger(L, numresults); 53 | lua_rotate(L, 1, 3); 54 | int nups = lua::packupvals(L, 4, lua_gettop(L) - 3); 55 | lua_pushcclosure(L, [](lua_State *L) 56 | { 57 | int res = (int)lua_tointeger(L, lua_upvalueindex(3)); 58 | lua_pushnil(L); 59 | lua_replace(L, lua_upvalueindex(3)); 60 | 61 | lua_pushvalue(L, lua_upvalueindex(2)); 62 | lua_setfield(L, lua_upvalueindex(1), "#lua"); 63 | int num = lua::unpackupvals(L, 4); 64 | lua_call(L, num - 1, res); 65 | 66 | if(res == LUA_MULTRET || res > 0) 67 | { 68 | if(res != LUA_MULTRET) 69 | { 70 | lua_settop(L, res); 71 | }else{ 72 | res = lua_gettop(L); 73 | } 74 | lua_createtable(L, res + 1, 0); 75 | lua_insert(L, 1); 76 | lua_pushinteger(L, res); 77 | lua_rawseti(L, 1, 1); 78 | while(res > 0) 79 | { 80 | lua_rawseti(L, 1, 1 + res--); 81 | } 82 | lua_replace(L, lua_upvalueindex(4)); 83 | } 84 | return 0; 85 | }, nups + 3); 86 | if(numresults != 0) 87 | { 88 | lua_pushvalue(L, -1); 89 | } 90 | lua_setfield(L, lua_upvalueindex(1), "#lua"); 91 | 92 | auto amx = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(2))); 93 | int index, error; 94 | error = amx_FindPublic(amx, "#lua", &index); 95 | if(error != AMX_ERR_NONE) 96 | { 97 | return lua::amx_error(L, error); 98 | } 99 | cell retval; 100 | error = amx_Exec(amx, &retval, index); 101 | if(error != AMX_ERR_NONE) 102 | { 103 | return lua::amx_error(L, error, retval); 104 | } 105 | if(numresults == 0) 106 | { 107 | return 0; 108 | } 109 | lua_getupvalue(L, -1, 4); 110 | if(lua_istable(L, -1)) 111 | { 112 | int table = lua_absindex(L, -1); 113 | lua_rawgeti(L, table, 1); 114 | int num = (int)lua_tointeger(L, -1); 115 | if(numresults != LUA_MULTRET && numresults < num) 116 | { 117 | num = numresults; 118 | } 119 | luaL_checkstack(L, num, nullptr); 120 | for(int i = 1; i <= num; i++) 121 | { 122 | lua_rawgeti(L, table, 1 + i); 123 | } 124 | return num; 125 | }else{ 126 | lua_pushlightuserdata(L, reinterpret_cast(retval)); 127 | return 1; 128 | } 129 | } 130 | 131 | int lua::interop::loader(lua_State *L) 132 | { 133 | auto ptr = std::make_shared(lua::mainthread(L)); 134 | amx_info &info = *ptr; 135 | lua::pushuserdata(L, ptr); 136 | auto loader = [&](AMX *amx, void *program) 137 | { 138 | std::unordered_map tagcache; 139 | 140 | int nlen; 141 | amx_NameLength(amx, &nlen); 142 | 143 | auto tagname = reinterpret_cast(alloca(nlen + 1)); 144 | int numtags; 145 | amx_NumTags(amx, &numtags); 146 | for(int i = 0; i < numtags; i++) 147 | { 148 | cell tag_id; 149 | amx_GetTag(amx, i, tagname, &tag_id); 150 | 151 | tagcache[tag_id] = tagname; 152 | } 153 | 154 | info.amx = amx; 155 | info.self = luaL_ref(L, LUA_REGISTRYINDEX); 156 | amx_map[amx] = ptr; 157 | 158 | lua_newtable(L); 159 | 160 | lua_pushinteger(L, std::numeric_limits::min()); 161 | lua_setfield(L, -2, "cellmin"); 162 | lua_pushinteger(L, std::numeric_limits::max()); 163 | lua_setfield(L, -2, "cellmax"); 164 | 165 | lua_newtable(L); 166 | lua_setfield(L, -2, "public"); 167 | 168 | init_sleep(L, amx); 169 | init_native(L, amx); 170 | init_public(L, amx); 171 | init_pubvar(L, amx); 172 | init_memory(L, amx); 173 | init_string(L, amx); 174 | init_result(L, amx); 175 | init_file(L, amx); 176 | init_tags(L, amx, tagcache); 177 | 178 | lua_getfield(L, -1, "public"); 179 | lua_pushlightuserdata(L, amx); 180 | lua_pushcclosure(L, forward, 2); 181 | lua_setfield(L, -2, "forward"); 182 | 183 | lua::pushstring(L, "#lua"); 184 | lua_setfield(L, -2, "loopback"); 185 | }; 186 | 187 | AMX *amx = lua::bound_amx(L); 188 | if(amx) 189 | { 190 | loader(amx, nullptr); 191 | }else{ 192 | info.fs_name = "?luafs_"; 193 | info.fs_name.append(std::to_string(reinterpret_cast(&info))); 194 | amx = amx::LoadNew(info.fs_name.c_str(), 8192, sNAMEMAX, std::move(loader)); 195 | if(!amx) 196 | { 197 | lua_pop(L, 1); 198 | return 0; 199 | } 200 | } 201 | 202 | return 1; 203 | } 204 | 205 | bool lua::interop::amx_get_addr(AMX *amx, cell amx_addr, cell **phys_addr) 206 | { 207 | return amx_get_param_addr(amx, amx_addr, phys_addr) || amx_get_pubvar_addr(amx, amx_addr, phys_addr); 208 | } 209 | 210 | void lua::interop::amx_unload(AMX *amx) 211 | { 212 | auto it = amx_map.find(amx); 213 | if(it != amx_map.end()) 214 | { 215 | if(auto lock = it->second.lock()) 216 | { 217 | lock->amx = nullptr; 218 | lua_close(lock->L); 219 | lua::cleanup(lock->L); 220 | }else{ 221 | amx_map.erase(it); 222 | } 223 | } 224 | amx_unregister_natives(amx); 225 | } 226 | -------------------------------------------------------------------------------- /plugins/src/lua/interop.h: -------------------------------------------------------------------------------- 1 | #ifndef INTEROP_H_INCLUDED 2 | #define INTEROP_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | int loader(lua_State *L); 12 | void amx_unload(AMX *amx); 13 | bool amx_get_addr(AMX *amx, cell amx_addr, cell **phys_addr); 14 | } 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/file.cpp: -------------------------------------------------------------------------------- 1 | #include "file.h" 2 | #include "lua_utils.h" 3 | #include "amx/fileutils.h" 4 | #include "native.h" 5 | 6 | template 7 | int asfile(lua_State *L) 8 | { 9 | auto ptr = lua::checklightudata(L, 1); 10 | if(!ptr) 11 | { 12 | lua_pushnil(L); 13 | return 1; 14 | } 15 | 16 | if(luaL_getmetatable(L, LUA_FILEHANDLE) != LUA_TTABLE) 17 | { 18 | return luaL_error(L, "the io package is not loaded"); 19 | } 20 | int mtindex = lua_absindex(L, -1); 21 | 22 | FILE *f; 23 | #ifdef _WIN32 24 | auto amx = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); 25 | if(!amx::FileLoad(reinterpret_cast(ptr), amx, f)) 26 | #else 27 | if(!amx::FileLoad(reinterpret_cast(ptr), nullptr, f)) 28 | #endif 29 | { 30 | return lua::argerror(L, 1, "invalid file handle"); 31 | } 32 | 33 | if(DestroyOriginal) 34 | { 35 | #ifdef _WIN32 36 | auto fclose = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(2))); 37 | cell params[] = {sizeof(cell), reinterpret_cast(ptr)}; 38 | 39 | fclose(amx, params); 40 | #else 41 | fclose(reinterpret_cast(ptr)); 42 | #endif 43 | } 44 | 45 | if(!f) 46 | { 47 | lua_pushnil(L); 48 | return 1; 49 | } 50 | 51 | auto &file = lua::newuserdata(L); 52 | lua_pushvalue(L, mtindex); 53 | lua_setmetatable(L, -2); 54 | file.f = f; 55 | file.closef = [](lua_State *L) 56 | { 57 | auto &file = lua::touserdata(L, 1); 58 | int res = fclose(file.f); 59 | return luaL_fileresult(L, res == 0, nullptr); 60 | }; 61 | return 1; 62 | } 63 | 64 | int tofile(lua_State *L) 65 | { 66 | luaL_checkudata(L, 1, LUA_FILEHANDLE); 67 | auto &file = lua::touserdata(L, 1); 68 | #ifdef _WIN32 69 | auto amx = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); 70 | cell value = amx::FileStore(file.f, amx); 71 | #else 72 | cell value = amx::FileStore(file.f, nullptr); 73 | #endif 74 | if(value == 0) 75 | { 76 | lua_pushnil(L); 77 | return 1; 78 | } 79 | lua_pushlightuserdata(L, reinterpret_cast(value)); 80 | return 1; 81 | } 82 | 83 | int closefile(lua_State *L) 84 | { 85 | auto file = lua::checklightudata(L, 1); 86 | #ifdef _WIN32 87 | auto amx = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); 88 | auto fclose = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(2))); 89 | cell params[] = {sizeof(cell), reinterpret_cast(file)}; 90 | 91 | fclose(amx, params); 92 | #else 93 | fclose(reinterpret_cast(file)); 94 | #endif 95 | return 0; 96 | } 97 | 98 | void lua::interop::init_file(lua_State *L, AMX *amx) 99 | { 100 | int table = lua_absindex(L, -1); 101 | 102 | #ifdef _WIN32 103 | auto fclose = reinterpret_cast(find_native(amx, "fclose")); 104 | auto fseek = reinterpret_cast(find_native(amx, "fseek")); 105 | auto ftemp = reinterpret_cast(find_native(amx, "ftemp")); 106 | 107 | lua_pushlightuserdata(L, amx); 108 | lua_pushlightuserdata(L, fclose); 109 | lua_pushcclosure(L, asfile, 2); 110 | lua_setfield(L, table, "asfile"); 111 | 112 | lua_pushlightuserdata(L, amx); 113 | lua_pushcclosure(L, asfile, 1); 114 | lua_setfield(L, table, "asnewfile"); 115 | 116 | lua_pushlightuserdata(L, amx); 117 | lua_pushlightuserdata(L, fclose); 118 | lua_pushcclosure(L, closefile, 2); 119 | lua_setfield(L, table, "closefile"); 120 | 121 | lua_pushlightuserdata(L, amx); 122 | lua_pushcclosure(L, tofile, 1); 123 | lua_setfield(L, table, "tofile"); 124 | #else 125 | lua_pushcfunction(L, asfile); 126 | lua_setfield(L, table, "asfile"); 127 | 128 | lua_pushcfunction(L, asfile); 129 | lua_setfield(L, table, "asnewfile"); 130 | 131 | lua_pushcfunction(L, tofile); 132 | lua_setfield(L, table, "tofile"); 133 | 134 | lua_pushcfunction(L, closefile); 135 | lua_setfield(L, table, "closefile"); 136 | #endif 137 | } 138 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/file.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_H_INCLUDED 2 | #define FILE_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_file(lua_State *L, AMX *amx); 12 | } 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMORY_H_INCLUDED 2 | #define MEMORY_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_memory(lua_State *L, AMX *amx); 12 | } 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/native.h: -------------------------------------------------------------------------------- 1 | #ifndef NATIVE_H_INCLUDED 2 | #define NATIVE_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_native(lua_State *L, AMX *amx); 12 | void amx_register_natives(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number); 13 | bool amx_get_param_addr(AMX *amx, cell amx_addr, cell **phys_addr); 14 | void amx_unregister_natives(AMX *amx); 15 | AMX_NATIVE find_native(AMX *amx, const char *native); 16 | } 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/public.h: -------------------------------------------------------------------------------- 1 | #ifndef PUBLIC_H_INCLUDED 2 | #define PUBLIC_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_public(lua_State *L, AMX *amx); 12 | bool amx_find_public(AMX *amx, const char *funcname, int *index, int &error); 13 | bool amx_get_public(AMX *amx, int index, char *funcname); 14 | bool amx_num_publics(AMX *amx, int *number); 15 | bool amx_exec(AMX *amx, cell *retval, int index, int &result); 16 | } 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/pubvar.cpp: -------------------------------------------------------------------------------- 1 | #include "pubvar.h" 2 | #include "lua_utils.h" 3 | #include "lua_api.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | static std::unordered_map> amx_map; 10 | static std::unordered_map> addr_map; 11 | 12 | struct amx_pubvar_info 13 | { 14 | AMX *amx; 15 | 16 | lua_State *L; 17 | int self; 18 | int pubvartable; 19 | int pubvarlist; 20 | 21 | amx_pubvar_info(lua_State *L, AMX *amx) : L(L), amx(amx) 22 | { 23 | 24 | } 25 | 26 | ~amx_pubvar_info() 27 | { 28 | if(amx) 29 | { 30 | amx_map.erase(amx); 31 | amx = nullptr; 32 | } 33 | } 34 | }; 35 | 36 | void lua::interop::init_pubvar(lua_State *L, AMX *amx) 37 | { 38 | int table = lua_absindex(L, -1); 39 | 40 | auto info = std::make_shared(lua::mainthread(L), amx); 41 | amx_map[amx] = info; 42 | lua::pushuserdata(L, info); 43 | 44 | lua_getfield(L, table, "public"); 45 | info->pubvartable = luaL_ref(L, LUA_REGISTRYINDEX); 46 | 47 | lua_newtable(L); 48 | info->pubvarlist = luaL_ref(L, LUA_REGISTRYINDEX); 49 | 50 | info->self = luaL_ref(L, LUA_REGISTRYINDEX); 51 | } 52 | 53 | bool getpubvar(lua_State *L, const char *name, int index, int &error, void *&buf) 54 | { 55 | if(lua_rawgeti(L, LUA_REGISTRYINDEX, index) == LUA_TTABLE) 56 | { 57 | error = lua::pgetfield(L, -1, name); 58 | size_t length; 59 | bool isconst; 60 | if(error != LUA_OK || !(buf = lua::tobuffer(L, -1, length, isconst))) 61 | { 62 | if(error != LUA_OK) 63 | { 64 | lua::report_error(L, error); 65 | } 66 | lua_pop(L, 2); 67 | return false; 68 | } 69 | lua_remove(L, -2); 70 | return true; 71 | } 72 | error = LUA_OK; 73 | lua_pop(L, 1); 74 | return false; 75 | } 76 | 77 | bool getpubvarlist(lua_State *L, int index) 78 | { 79 | if(lua_rawgeti(L, LUA_REGISTRYINDEX, index) == LUA_TTABLE) 80 | { 81 | return true; 82 | } 83 | lua_pop(L, 1); 84 | return false; 85 | } 86 | 87 | bool lua::interop::amx_find_pubvar(AMX *amx, const char *varname, cell *amx_addr, int &error) 88 | { 89 | if(amx_addr) 90 | { 91 | auto it = amx_map.find(amx); 92 | if(it != amx_map.end()) 93 | { 94 | if(auto info = it->second.lock()) 95 | { 96 | auto L = info->L; 97 | lua::stackguard guard(L); 98 | if(!lua_checkstack(L, 4)) 99 | { 100 | error = AMX_ERR_MEMORY; 101 | return true; 102 | } 103 | if(getpubvarlist(L, info->pubvarlist)) 104 | { 105 | int index = 0; 106 | if(lua_getfield(L, -1, varname) == LUA_TNUMBER) 107 | { 108 | index = (int)lua_tointeger(L, -1); 109 | } 110 | lua_pop(L, 1); 111 | int lerror; 112 | void *buf; 113 | if(getpubvar(L, varname, info->pubvartable, lerror, buf)) 114 | { 115 | auto hdr = (AMX_HEADER*)amx->base; 116 | auto data = (amx->data != NULL) ? amx->data : amx->base + (int)hdr->dat; 117 | cell addr = *amx_addr = reinterpret_cast(buf) - data; 118 | auto lock = addr_map[addr].lock(); 119 | if(!lock) 120 | { 121 | lock = std::make_shared(addr); 122 | addr_map[addr] = lock; 123 | } 124 | if(index) 125 | { 126 | if(lua_rawgeti(L, -2, index) == LUA_TTABLE) 127 | { 128 | lua_insert(L, -2); 129 | lua_rawseti(L, -2, 1); 130 | lua::pushuserdata(L, std::move(lock)); 131 | lua_rawseti(L, -2, 3); 132 | lua_pop(L, 2); 133 | error = AMX_ERR_NONE; 134 | return true; 135 | } 136 | lua_pop(L, 1); 137 | }else{ 138 | lua_createtable(L, 3, 0); 139 | lua_insert(L, -2); 140 | lua_rawseti(L, -2, 1); 141 | lua_pushstring(L, varname); 142 | lua_rawseti(L, -2, 2); 143 | lua::pushuserdata(L, std::move(lock)); 144 | lua_rawseti(L, -2, 3); 145 | int index = luaL_ref(L, -2); 146 | lua_pushinteger(L, index); 147 | lua_setfield(L, -2, varname); 148 | lua_pop(L, 1); 149 | error = AMX_ERR_NONE; 150 | return true; 151 | } 152 | lua_pop(L, 1); 153 | }else if(lerror != LUA_OK) 154 | { 155 | error = AMX_ERR_GENERAL; 156 | return true; 157 | } 158 | if(index) 159 | { 160 | if(lua_rawgeti(L, -1, index) == LUA_TTABLE) 161 | { 162 | lua_pushnil(L); 163 | lua_rawseti(L, -2, 1); 164 | lua_pushnil(L); 165 | lua_rawseti(L, -2, 3); 166 | } 167 | lua_pop(L, 1); 168 | } 169 | lua_pop(L, 1); 170 | } 171 | error = AMX_ERR_INDEX; 172 | return true; 173 | } 174 | } 175 | } 176 | return false; 177 | } 178 | 179 | bool lua::interop::amx_get_pubvar(AMX *amx, int index, char *varname, cell *amx_addr) 180 | { 181 | if(varname || amx_addr) 182 | { 183 | auto it = amx_map.find(amx); 184 | if(it != amx_map.end()) 185 | { 186 | if(auto info = it->second.lock()) 187 | { 188 | auto L = info->L; 189 | lua::stackguard guard(L); 190 | if(!lua_checkstack(L, 4)) 191 | { 192 | return false; 193 | } 194 | if(getpubvarlist(L, info->pubvarlist)) 195 | { 196 | if(lua_rawgeti(L, -1, index + 1) == LUA_TTABLE) 197 | { 198 | if(amx_addr) 199 | { 200 | if(lua_rawgeti(L, -1, 3) == LUA_TUSERDATA) 201 | { 202 | *amx_addr = *lua::touserdata>(L, -1); 203 | } 204 | lua_pop(L, 1); 205 | } 206 | if(varname) 207 | { 208 | if(lua_rawgeti(L, -1, 2) == LUA_TSTRING) 209 | { 210 | auto str = lua_tostring(L, -1); 211 | std::strcpy(varname, str); 212 | } 213 | lua_pop(L, 1); 214 | lua_pop(L, 2); 215 | } 216 | return true; 217 | } 218 | lua_pop(L, 2); 219 | } 220 | } 221 | } 222 | } 223 | return false; 224 | } 225 | 226 | bool lua::interop::amx_num_pubvars(AMX *amx, int *number) 227 | { 228 | if(number) 229 | { 230 | auto it = amx_map.find(amx); 231 | if(it != amx_map.end()) 232 | { 233 | if(auto info = it->second.lock()) 234 | { 235 | auto L = info->L; 236 | lua::stackguard guard(L); 237 | if(!lua_checkstack(L, 4)) 238 | { 239 | return false; 240 | } 241 | if(getpubvarlist(L, info->pubvarlist)) 242 | { 243 | *number = (int)lua_rawlen(L, -1); 244 | lua_pop(L, 1); 245 | return true; 246 | } 247 | } 248 | } 249 | } 250 | return false; 251 | } 252 | 253 | bool lua::interop::amx_get_pubvar_addr(AMX *amx, cell amx_addr, cell **phys_addr) 254 | { 255 | if(phys_addr) 256 | { 257 | auto it = addr_map.find(amx_addr); 258 | if(it != addr_map.end()) 259 | { 260 | auto lock = it->second.lock(); 261 | if(!lock) 262 | { 263 | addr_map.erase(it); 264 | return false; 265 | } 266 | 267 | auto hdr = (AMX_HEADER*)amx->base; 268 | auto data = (amx->data != NULL) ? amx->data : amx->base + (int)hdr->dat; 269 | *phys_addr = reinterpret_cast(data + amx_addr); 270 | return true; 271 | } 272 | } 273 | return false; 274 | } 275 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/pubvar.h: -------------------------------------------------------------------------------- 1 | #ifndef PUBVAR_H_INCLUDED 2 | #define PUBVAR_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_pubvar(lua_State *L, AMX *amx); 12 | bool amx_find_pubvar(AMX *amx, const char *varname, cell *amx_addr, int &error); 13 | bool amx_get_pubvar(AMX *amx, int index, char *varname, cell *amx_addr); 14 | bool amx_num_pubvars(AMX *amx, int *number); 15 | bool amx_get_pubvar_addr(AMX *amx, cell amx_addr, cell **phys_addr); 16 | } 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/result.cpp: -------------------------------------------------------------------------------- 1 | #include "result.h" 2 | #include "lua_utils.h" 3 | 4 | static int asnone(lua_State *L) 5 | { 6 | lua::checklightudata(L, 1); 7 | return 0; 8 | } 9 | 10 | static int asnil(lua_State *L) 11 | { 12 | lua::checklightudata(L, 1); 13 | lua_pushnil(L); 14 | return 1; 15 | } 16 | 17 | static int asinteger(lua_State *L) 18 | { 19 | auto ptr = lua::checklightudata(L, 1); 20 | lua_pushinteger(L, reinterpret_cast(ptr)); 21 | return 1; 22 | } 23 | 24 | static int asuinteger(lua_State *L) 25 | { 26 | auto ptr = lua::checklightudata(L, 1); 27 | lua_pushinteger(L, reinterpret_cast(ptr)); 28 | return 1; 29 | } 30 | 31 | static int asboolean(lua_State *L) 32 | { 33 | auto ptr = lua::checklightudata(L, 1); 34 | lua_pushboolean(L, !!ptr); 35 | return 1; 36 | } 37 | 38 | static int asfloat(lua_State *L) 39 | { 40 | auto ptr = lua::checklightudata(L, 1); 41 | lua_pushnumber(L, amx_ctof(reinterpret_cast(ptr))); 42 | return 1; 43 | } 44 | 45 | static int asoffset(lua_State *L) 46 | { 47 | auto ptr = lua::checklightudata(L, 1); 48 | size_t ofs = reinterpret_cast(ptr); 49 | if(ofs % sizeof(cell) != 0) 50 | { 51 | return luaL_argerror(L, 1, "not a valid offset"); 52 | } 53 | lua_pushinteger(L, ofs / sizeof(cell) + 1); 54 | return 1; 55 | } 56 | 57 | static int ashandle(lua_State *L) 58 | { 59 | auto ptr = lua::checklightudata(L, 1); 60 | if(!ptr) 61 | { 62 | lua_pushnil(L); 63 | return 1; 64 | } 65 | lua_pushlightuserdata(L, ptr); 66 | return 1; 67 | } 68 | 69 | void lua::interop::init_result(lua_State *L, AMX *amx) 70 | { 71 | int table = lua_absindex(L, -1); 72 | 73 | lua_pushcfunction(L, asnone); 74 | lua_setfield(L, table, "asnone"); 75 | lua_pushcfunction(L, asnil); 76 | lua_setfield(L, table, "asnil"); 77 | lua_pushcfunction(L, asinteger); 78 | lua_setfield(L, table, "asinteger"); 79 | lua_pushcfunction(L, asuinteger); 80 | lua_setfield(L, table, "asuinteger"); 81 | lua_pushcfunction(L, asboolean); 82 | lua_setfield(L, table, "asboolean"); 83 | lua_pushcfunction(L, asfloat); 84 | lua_setfield(L, table, "asfloat"); 85 | lua_pushcfunction(L, asoffset); 86 | lua_setfield(L, table, "asoffset"); 87 | lua_pushcfunction(L, ashandle); 88 | lua_setfield(L, table, "ashandle"); 89 | } 90 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/result.h: -------------------------------------------------------------------------------- 1 | #ifndef RESULT_H_INCLUDED 2 | #define RESULT_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_result(lua_State *L, AMX *amx); 12 | } 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/sleep.cpp: -------------------------------------------------------------------------------- 1 | #include "sleep.h" 2 | #include "lua_utils.h" 3 | #include "amx/amxutils.h" 4 | 5 | #include 6 | 7 | int sleep(lua_State *L) 8 | { 9 | luaL_checktype(L, 1, LUA_TFUNCTION); 10 | cell value; 11 | if(lua_islightuserdata(L, 2)) 12 | { 13 | return lua::amx_sleep(L, 2, 1); 14 | }else if(lua_isinteger(L, 2)) 15 | { 16 | value = (cell)lua_tointeger(L, 2); 17 | }else if(lua::isnumber(L, 2)) 18 | { 19 | float num = (float)lua_tonumber(L, 2); 20 | value = amx_ftoc(num); 21 | }else if(lua_isboolean(L, 2)) 22 | { 23 | value = (cell)lua_toboolean(L, 2); 24 | }else{ 25 | if(lua_isfunction(L, 2)) 26 | { 27 | lua_pushvalue(L, 2); 28 | switch(lua_pcall(L, 0, 1, 0)) 29 | { 30 | case LUA_OK: 31 | return 1; 32 | default: 33 | if(lua_istable(L, -1)) 34 | { 35 | if(lua_getfield(L, -1, "__amxerr") == LUA_TNUMBER) 36 | { 37 | if(lua_tointeger(L, -1) == AMX_ERR_SLEEP) 38 | { 39 | lua_pushvalue(L, 1); 40 | lua_setfield(L, -3, "__cont"); 41 | } 42 | } 43 | lua_pop(L, 1); 44 | } 45 | return lua_error(L); 46 | } 47 | } 48 | return lua::argerrortype(L, 2, "primitive type or function"); 49 | } 50 | lua_pushlightuserdata(L, reinterpret_cast(value)); 51 | return lua::amx_sleep(L, -1, 1); 52 | } 53 | 54 | void lua::interop::init_sleep(lua_State *L, AMX *amx) 55 | { 56 | int table = lua_absindex(L, -1); 57 | lua_pushcfunction(L, sleep); 58 | lua_setfield(L, table, "sleep"); 59 | } 60 | 61 | void lua::interop::handle_sleep(lua_State *L, AMX *amx, int contlist) 62 | { 63 | if(lua_getfield(L, -1, "__retval") == LUA_TLIGHTUSERDATA) 64 | { 65 | amx->pri = reinterpret_cast(lua_touserdata(L, -1)); 66 | } 67 | lua_pop(L, 1); 68 | 69 | if(lua_rawgeti(L, LUA_REGISTRYINDEX, contlist) == LUA_TTABLE) 70 | { 71 | if(lua_getfield(L, -2, "__cont") == LUA_TFUNCTION) 72 | { 73 | lua_createtable(L, 2, 0); 74 | lua_insert(L, -2); 75 | lua_rawseti(L, -2, 1); 76 | int cookie = std::rand(); 77 | lua_pushinteger(L, cookie); 78 | lua_rawseti(L, -2, 2); 79 | amx->alt = cookie; 80 | amx->cip = luaL_ref(L, -2); 81 | }else{ 82 | lua_pop(L, 1); 83 | } 84 | } 85 | lua_pop(L, 1); 86 | } 87 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/sleep.h: -------------------------------------------------------------------------------- 1 | #ifndef SLEEP_H_INCLUDED 2 | #define SLEEP_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_sleep(lua_State *L, AMX *amx); 12 | void handle_sleep(lua_State *L, AMX *amx, int contlist); 13 | } 14 | } 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/string.cpp: -------------------------------------------------------------------------------- 1 | #include "string.h" 2 | #include "lua_utils.h" 3 | #include "amx/amxutils.h" 4 | 5 | int getstring(lua_State *L) 6 | { 7 | size_t blen; 8 | bool isconst; 9 | auto ptr = reinterpret_cast(lua::tobuffer(L, 1, blen, isconst)); 10 | if(!ptr) 11 | { 12 | return lua::argerrortype(L, 1, "buffer type"); 13 | } 14 | ptrdiff_t offset = lua::checkoffset(L, 2); 15 | lua_Integer len; 16 | if(lua_isnil(L, 3) || lua_isnone(L, 3)) 17 | { 18 | len = -1; 19 | }else{ 20 | len = luaL_checkinteger(L, 3); 21 | } 22 | 23 | if(offset < 0 || (size_t)offset >= blen) 24 | { 25 | lua_pushnil(L); 26 | return 1; 27 | } 28 | 29 | ptr += offset; 30 | blen -= offset; 31 | 32 | auto buf = reinterpret_cast(ptr); 33 | blen /= sizeof(cell); 34 | if(len >= 0 && len < blen) 35 | { 36 | blen = (size_t)len; 37 | } 38 | 39 | 40 | std::string str = amx::GetString(buf, blen, true); 41 | lua_pushlstring(L, str.data(), str.size()); 42 | return 1; 43 | } 44 | 45 | int setstring(lua_State *L) 46 | { 47 | size_t blen; 48 | bool isconst; 49 | auto ptr = reinterpret_cast(lua::tobuffer(L, 1, blen, isconst)); 50 | if(!ptr) 51 | { 52 | return lua::argerrortype(L, 1, "buffer type"); 53 | } 54 | size_t slen; 55 | auto str = luaL_checklstring(L, 2, &slen); 56 | ptrdiff_t offset = lua::checkoffset(L, 3); 57 | lua_Integer len; 58 | if(lua_isnil(L, 4) || lua_isnone(L, 4)) 59 | { 60 | len = -1; 61 | }else{ 62 | len = luaL_checkinteger(L, 4); 63 | } 64 | 65 | bool pack = luaL_opt(L, lua::checkboolean, 5, true); 66 | 67 | if(offset < 0 || (size_t)offset >= blen) 68 | { 69 | return 0; 70 | } 71 | 72 | ptr += offset; 73 | blen -= offset; 74 | 75 | auto buf = reinterpret_cast(ptr); 76 | blen /= sizeof(cell); 77 | if(len >= 0 && len < blen) 78 | { 79 | blen = (size_t)len; 80 | } 81 | 82 | if(pack) 83 | { 84 | blen *= sizeof(cell); 85 | } 86 | if(slen >= blen) 87 | { 88 | slen = blen - 1; 89 | } 90 | 91 | amx::SetString(buf, str, slen, pack); 92 | return 0; 93 | } 94 | 95 | int asstring(lua_State *L) 96 | { 97 | auto amx = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); 98 | auto ptr = lua::checklightudata(L, 1); 99 | cell *addr; 100 | int len; 101 | if(amx_GetAddr(amx, reinterpret_cast(ptr), &addr) != AMX_ERR_NONE || amx_StrLen(addr, &len) != AMX_ERR_NONE) 102 | { 103 | lua_pushnil(L); 104 | return 1; 105 | } 106 | 107 | std::string str = amx::GetString(addr, (size_t)len, len < 0 ? true : false); 108 | lua_pushlstring(L, str.data(), str.size()); 109 | return 1; 110 | } 111 | 112 | int tocellstring(lua_State *L) 113 | { 114 | size_t length; 115 | const char *str = luaL_checklstring(L, 1, &length); 116 | lua_pushvalue(L, lua_upvalueindex(1)); 117 | lua_pushinteger(L, length + 1); 118 | lua_pushboolean(L, false); 119 | lua_call(L, 2, 1); 120 | auto ptr = reinterpret_cast(lua_touserdata(L, -1)); 121 | amx::SetString(ptr, str, length, false); 122 | return 1; 123 | } 124 | 125 | void lua::interop::init_string(lua_State *L, AMX *amx) 126 | { 127 | int table = lua_absindex(L, -1); 128 | 129 | lua_pushcfunction(L, getstring); 130 | lua_setfield(L, table, "getstring"); 131 | 132 | lua_pushcfunction(L, setstring); 133 | lua_setfield(L, table, "setstring"); 134 | 135 | lua_pushlightuserdata(L, amx); 136 | lua_pushcclosure(L, asstring, 1); 137 | lua_setfield(L, table, "asstring"); 138 | 139 | lua_getfield(L, table, "newbuffer"); 140 | lua_pushcclosure(L, tocellstring, 1); 141 | lua_setfield(L, table, "tocellstring"); 142 | } 143 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/string.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_H_INCLUDED 2 | #define STRING_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | namespace lua 8 | { 9 | namespace interop 10 | { 11 | void init_string(lua_State *L, AMX *amx); 12 | } 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /plugins/src/lua/interop/tags.h: -------------------------------------------------------------------------------- 1 | #ifndef TAGS_H_INCLUDED 2 | #define TAGS_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | #include 8 | #include 9 | 10 | namespace lua 11 | { 12 | namespace interop 13 | { 14 | void init_tags(lua_State *L, AMX *amx, const std::unordered_map &init); 15 | bool amx_find_tag_id(AMX *amx, cell tag_id, char *tagname); 16 | bool amx_get_tag(AMX *amx, int index, char *tagname, cell *tag_id); 17 | bool amx_num_tags(AMX *amx, int *number); 18 | } 19 | } 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /plugins/src/lua/lualibs.h: -------------------------------------------------------------------------------- 1 | #include "lua/lua.h" 2 | #include "lua/lualib.h" 3 | #include "lua/lauxlib.h" 4 | -------------------------------------------------------------------------------- /plugins/src/lua/remote.h: -------------------------------------------------------------------------------- 1 | #ifndef REMOTE_H_INCLUDED 2 | #define REMOTE_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | 6 | namespace lua 7 | { 8 | namespace remote 9 | { 10 | int loader(lua_State *L); 11 | void close(); 12 | } 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /plugins/src/lua/timer.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMER_H_INCLUDED 2 | #define TIMER_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | 6 | namespace lua 7 | { 8 | namespace timer 9 | { 10 | int loader(lua_State *L); 11 | void close(); 12 | void tick(); 13 | bool pushyielded(lua_State *L, lua_State *from); 14 | } 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /plugins/src/lua_adapt.cpp: -------------------------------------------------------------------------------- 1 | #include "lua_adapt.h" 2 | #include "lua_api.h" 3 | #include "main.h" 4 | 5 | int lua::atpanic(lua_State *L) 6 | { 7 | auto str = lua_tostring(L, -1); 8 | lua::report_error(L, lua_status(L)); 9 | throw lua::panic_error(str, lua_status(L)); 10 | } 11 | 12 | bool lua::check_params(AMX *amx, cell *params, cell needed) 13 | { 14 | if(params[0] >= needed * static_cast(sizeof(cell))) return true; 15 | logprintf("[YALP] not enough arguments (%d expected, got %d)", needed, params[0] / static_cast(sizeof(cell))); 16 | amx_RaiseError(amx, AMX_ERR_PARAMS); 17 | return false; 18 | } 19 | -------------------------------------------------------------------------------- /plugins/src/lua_adapt.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA_ADAPT_H_INCLUDED 2 | #define LUA_ADAPT_H_INCLUDED 3 | 4 | #include "main.h" 5 | #include "lua/lualibs.h" 6 | #include "sdk/amx/amx.h" 7 | 8 | #include 9 | 10 | namespace lua 11 | { 12 | class panic_error : public std::runtime_error 13 | { 14 | public: 15 | int code; 16 | 17 | panic_error(const char *what, int code) : std::runtime_error(what), code(code) 18 | { 19 | 20 | } 21 | }; 22 | 23 | int atpanic(lua_State *L); 24 | 25 | template 26 | struct adapt_return 27 | { 28 | 29 | }; 30 | 31 | template <> 32 | struct adapt_return 33 | { 34 | static cell convert(int arg) 35 | { 36 | return arg; 37 | } 38 | }; 39 | 40 | template <> 41 | struct adapt_return 42 | { 43 | static cell convert(size_t arg) 44 | { 45 | return static_cast(arg); 46 | } 47 | }; 48 | 49 | template <> 50 | struct adapt_return 51 | { 52 | static cell convert(lua_Integer arg) 53 | { 54 | return static_cast(arg); 55 | } 56 | }; 57 | 58 | template <> 59 | struct adapt_return 60 | { 61 | static cell convert(lua_Number arg) 62 | { 63 | float fval = static_cast(arg); 64 | return amx_ftoc(fval); 65 | } 66 | }; 67 | 68 | template <> 69 | struct adapt_return 70 | { 71 | static cell convert(void *arg) 72 | { 73 | return reinterpret_cast(arg); 74 | } 75 | }; 76 | 77 | template <> 78 | struct adapt_return 79 | { 80 | static cell convert(const void *arg) 81 | { 82 | return reinterpret_cast(arg); 83 | } 84 | }; 85 | 86 | template <> 87 | struct adapt_return 88 | { 89 | static cell convert(lua_State *arg) 90 | { 91 | return reinterpret_cast(arg); 92 | } 93 | }; 94 | 95 | template <> 96 | struct adapt_return 97 | { 98 | static cell convert(const lua_Number *arg) 99 | { 100 | return static_cast(*arg); 101 | } 102 | }; 103 | 104 | template 105 | struct adapt_arg 106 | { 107 | 108 | }; 109 | 110 | template <> 111 | struct adapt_arg 112 | { 113 | static int convert(cell arg) 114 | { 115 | return arg; 116 | } 117 | }; 118 | 119 | template <> 120 | struct adapt_arg 121 | { 122 | static size_t convert(cell arg) 123 | { 124 | return arg; 125 | } 126 | }; 127 | 128 | template <> 129 | struct adapt_arg 130 | { 131 | static lua_Integer convert(cell arg) 132 | { 133 | return arg; 134 | } 135 | }; 136 | 137 | template <> 138 | struct adapt_arg 139 | { 140 | static lua_Number convert(cell arg) 141 | { 142 | return amx_ctof(arg); 143 | } 144 | }; 145 | 146 | template <> 147 | struct adapt_arg 148 | { 149 | static void *convert(cell arg) 150 | { 151 | return reinterpret_cast(arg); 152 | } 153 | }; 154 | 155 | template <> 156 | struct adapt_arg 157 | { 158 | static const void *convert(cell arg) 159 | { 160 | return reinterpret_cast(arg); 161 | } 162 | }; 163 | 164 | template <> 165 | struct adapt_arg 166 | { 167 | static lua_State *convert(cell arg) 168 | { 169 | return reinterpret_cast(arg); 170 | } 171 | }; 172 | 173 | template 174 | struct adapt 175 | { 176 | 177 | }; 178 | 179 | bool check_params(AMX *amx, cell *params, cell needed); 180 | 181 | template 182 | struct adapt 183 | { 184 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 185 | { 186 | if(!check_params(amx, params, 1)) return 0; 187 | auto L = reinterpret_cast(params[1]); 188 | Func(L); 189 | return 0; 190 | } 191 | }; 192 | 193 | template 194 | struct adapt 195 | { 196 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 197 | { 198 | if(!check_params(amx, params, 1)) return 0; 199 | auto L = reinterpret_cast(params[1]); 200 | return adapt_return::convert(Func(L)); 201 | } 202 | }; 203 | 204 | template 205 | struct adapt 206 | { 207 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 208 | { 209 | if(!check_params(amx, params, 2)) return 0; 210 | auto L = reinterpret_cast(params[1]); 211 | Func(L, adapt_arg::convert(params[2])); 212 | return 0; 213 | } 214 | }; 215 | 216 | template 217 | struct adapt 218 | { 219 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 220 | { 221 | if(!check_params(amx, params, 2)) return 0; 222 | auto L = reinterpret_cast(params[1]); 223 | return adapt_return::convert(Func(L, adapt_arg::convert(params[2]))); 224 | } 225 | }; 226 | 227 | template 228 | struct adapt 229 | { 230 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 231 | { 232 | if(!check_params(amx, params, 3)) return 0; 233 | auto L = reinterpret_cast(params[1]); 234 | Func(L, adapt_arg::convert(params[2]), adapt_arg::convert(params[3])); 235 | return 0; 236 | } 237 | }; 238 | 239 | template 240 | struct adapt 241 | { 242 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 243 | { 244 | if(!check_params(amx, params, 3)) return 0; 245 | auto L = reinterpret_cast(params[1]); 246 | return adapt_return::convert(Func(L, adapt_arg::convert(params[2]), adapt_arg::convert(params[3]))); 247 | } 248 | }; 249 | 250 | template 251 | struct adapt 252 | { 253 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 254 | { 255 | if(!check_params(amx, params, 4)) return 0; 256 | auto L = reinterpret_cast(params[1]); 257 | Func(L, adapt_arg::convert(params[2]), adapt_arg::convert(params[3]), adapt_arg::convert(params[4])); 258 | return 0; 259 | } 260 | }; 261 | 262 | template 263 | struct adapt 264 | { 265 | static cell AMX_NATIVE_CALL native(AMX *amx, cell *params) 266 | { 267 | if(!check_params(amx, params, 4)) return 0; 268 | auto L = reinterpret_cast(params[1]); 269 | return adapt_return::convert(Func(L, adapt_arg::convert(params[2]), adapt_arg::convert(params[3]), adapt_arg::convert(params[4]))); 270 | } 271 | }; 272 | } 273 | 274 | #endif 275 | -------------------------------------------------------------------------------- /plugins/src/lua_api.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA_API_H_INCLUDED 2 | #define LUA_API_H_INCLUDED 3 | 4 | #include "main.h" 5 | #include "lua/lualibs.h" 6 | 7 | namespace lua 8 | { 9 | void initlibs(lua_State *L, int load, int preload); 10 | cell init_bind(lua_State *L, AMX *amx); 11 | int bind(AMX *amx, cell *retval, int index); 12 | void report_error(lua_State *L, int error); 13 | AMX *bound_amx(lua_State *L); 14 | void process_tick(); 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /plugins/src/lua_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA_UTILS_H_INCLUDED 2 | #define LUA_UTILS_H_INCLUDED 3 | 4 | #include "lua/lualibs.h" 5 | #include 6 | #include 7 | 8 | #if __GNUG__ && __GNUC__ < 5 9 | #define is_trivially_constructible(T) __has_trivial_constructor(T) 10 | #define is_trivially_destructible(T) __has_trivial_destructor(T) 11 | #else 12 | #define is_trivially_constructible(T) std::is_trivially_constructible::value 13 | #define is_trivially_destructible(T) std::is_trivially_destructible::value 14 | #endif 15 | 16 | namespace lua 17 | { 18 | template 19 | struct mt_ctor 20 | { 21 | bool operator()(lua_State *L) 22 | { 23 | return false; 24 | } 25 | }; 26 | 27 | int amx_error(lua_State *L, int error); 28 | int amx_error(lua_State *L, int error, int retval); 29 | int amx_sleep(lua_State *L, int value, int cont); 30 | 31 | template 32 | struct _udata 33 | { 34 | typedef Type &type; 35 | 36 | static Type &to(lua_State *L, int idx) 37 | { 38 | return *reinterpret_cast(lua_touserdata(L, idx)); 39 | } 40 | 41 | static Type &check(lua_State *L, int idx, const char *tname) 42 | { 43 | return *reinterpret_cast(luaL_checkudata(L, idx, tname)); 44 | } 45 | }; 46 | 47 | template 48 | struct _udata 49 | { 50 | typedef Type *type; 51 | 52 | static Type *to(lua_State *L, int idx) 53 | { 54 | return reinterpret_cast(lua_touserdata(L, idx)); 55 | } 56 | 57 | static Type *check(lua_State *L, int idx, const char *tname) 58 | { 59 | return reinterpret_cast(luaL_checkudata(L, idx, tname)); 60 | } 61 | }; 62 | 63 | template 64 | typename _udata::type touserdata(lua_State *L, int idx) 65 | { 66 | return _udata::to(L, idx); 67 | } 68 | 69 | template 70 | typename _udata::type checkudata(lua_State *L, int idx, const char *tname) 71 | { 72 | return _udata::check(L, idx, tname); 73 | } 74 | 75 | template 76 | Type &newuserdata(lua_State *L) 77 | { 78 | auto data = reinterpret_cast(lua_newuserdata(L, sizeof(Type))); 79 | if(!is_trivially_constructible(Type)) 80 | { 81 | new (data) Type(); 82 | } 83 | bool hasmt = mt_ctor()(L); 84 | if(!is_trivially_destructible(Type)) 85 | { 86 | if(!hasmt) 87 | { 88 | lua_createtable(L, 0, 2); 89 | hasmt = true; 90 | } 91 | lua_pushboolean(L, false); 92 | lua_setfield(L, -2, "__metatable"); 93 | lua_pushcfunction(L, [](lua_State *L) { 94 | lua::touserdata(L, -1).~Type(); 95 | return 0; 96 | }); 97 | lua_setfield(L, -2, "__gc"); 98 | } 99 | if(hasmt) 100 | { 101 | lua_setmetatable(L, -2); 102 | } 103 | return *data; 104 | } 105 | 106 | template 107 | void pushuserdata(lua_State *L, const Type &val) 108 | { 109 | newuserdata(L) = val; 110 | } 111 | 112 | template ::value>::type> 113 | void pushuserdata(lua_State *L, Type &&val) 114 | { 115 | newuserdata::type>(L) = std::move(val); 116 | } 117 | 118 | void *testudata(lua_State *L, int ud, int mt); 119 | 120 | void *tobuffer(lua_State *L, int idx, size_t &length, bool &isconst); 121 | 122 | ptrdiff_t checkoffset(lua_State *L, int idx); 123 | 124 | void *checklightudata(lua_State *L, int idx); 125 | 126 | typedef std::function Reader; 127 | 128 | int load(lua_State *L, const lua::Reader &reader, const char *chunkname, const char *mode); 129 | 130 | typedef std::function CFunction; 131 | 132 | void pushcfunction(lua_State *L, CFunction &&fn); 133 | 134 | int pgetfield(lua_State *L, int idx, const char *k); 135 | 136 | int psetfield(lua_State *L, int idx, const char *k); 137 | 138 | short numresults(lua_State *L); 139 | short numresults(lua_State *L, int level); 140 | 141 | lua_State *mainthread(lua_State *L); 142 | 143 | template 144 | const char *pushstring(lua_State *L, const char(&s)[Len]) 145 | { 146 | return lua_pushlstring(L, s, Len - 1); 147 | } 148 | 149 | bool checkboolean(lua_State *L, int arg); 150 | 151 | int tailcall(lua_State *L, int n); 152 | int tailcall(lua_State *L, int n, int r); 153 | 154 | typedef std::function KFunction; 155 | 156 | int pcallk(lua_State *L, int nargs, int nresults, int errfunc, KFunction &&k); 157 | 158 | int callk(lua_State *L, int nargs, int nresults, KFunction &&k); 159 | 160 | bool isnumber(lua_State *L, int idx); 161 | 162 | bool isstring(lua_State *L, int idx); 163 | 164 | template 165 | int argerror(lua_State *L, int arg, const char *format, Args&&... args) 166 | { 167 | return luaL_argerror(L, arg, lua_pushfstring(L, format, std::forward(args)...)); 168 | } 169 | 170 | int argerrortype(lua_State *L, int arg, const char *expected); 171 | 172 | int pgettable(lua_State *L, int idx); 173 | 174 | int psettable(lua_State *L, int idx); 175 | 176 | int pcompare(lua_State *L, int idx1, int idx2, int op); 177 | 178 | int parith(lua_State *L, int op); 179 | 180 | int pconcat(lua_State *L, int n); 181 | 182 | int plen(lua_State *L, int idx); 183 | 184 | typedef std::function Alloc; 185 | 186 | void setallocf(lua_State *L, Alloc &&f); 187 | void cleanup(lua_State *L); 188 | 189 | bool active(lua_State *L); 190 | 191 | int tailyield(lua_State *L, int n); 192 | 193 | class jumpguard 194 | { 195 | lua_State *L; 196 | void *jmp; 197 | void *gljmp; 198 | 199 | public: 200 | jumpguard(lua_State *L); 201 | ~jumpguard(); 202 | }; 203 | 204 | int error(lua_State *L); 205 | 206 | class stackguard 207 | { 208 | lua_State *L; 209 | int top; 210 | 211 | public: 212 | stackguard(lua_State *L, int off=0); 213 | ~stackguard(); 214 | }; 215 | 216 | template 217 | const char *pushliteral(lua_State *L, const char(&s)[Size]) 218 | { 219 | return lua_pushlstring(L, s, Size - 1); 220 | } 221 | 222 | template 223 | int loadbufferx(lua_State *L, const char(&buff)[Size], const char *name, const char *mode) 224 | { 225 | return luaL_loadbufferx(L, buff, Size - 1, name, mode); 226 | } 227 | 228 | int packupvals(lua_State *L, int start, int num); 229 | 230 | int unpackupvals(lua_State *L, int start); 231 | } 232 | 233 | #endif 234 | -------------------------------------------------------------------------------- /plugins/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "hooks.h" 3 | #include "natives.h" 4 | #include "amx/amxutils.h" 5 | #include "lua_api.h" 6 | #include "lua/interop.h" 7 | #include "lua/timer.h" 8 | #include "lua/remote.h" 9 | #include "amx/fileutils.h" 10 | 11 | #include "sdk/amx/amx.h" 12 | #include "sdk/plugincommon.h" 13 | 14 | logprintf_t logprintf; 15 | extern void *pAMXFunctions; 16 | void **ppData; 17 | 18 | PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() 19 | { 20 | return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES | SUPPORTS_PROCESS_TICK; 21 | } 22 | 23 | PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) 24 | { 25 | ::ppData = ppData; 26 | pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS]; 27 | logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF]; 28 | 29 | hooks::load(); 30 | 31 | logprintf(" YALP v1.1.1 loaded"); 32 | logprintf(" Created by IllidanS4"); 33 | return true; 34 | } 35 | 36 | PLUGIN_EXPORT void PLUGIN_CALL Unload() 37 | { 38 | lua::timer::close(); 39 | lua::remote::close(); 40 | hooks::unload(); 41 | 42 | logprintf(" YALP v1.1.1 unloaded"); 43 | } 44 | 45 | PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx) 46 | { 47 | RegisterNatives(amx); 48 | return AMX_ERR_NONE; 49 | } 50 | 51 | PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx) 52 | { 53 | lua::interop::amx_unload(amx); 54 | amx::RemoveHandle(amx); 55 | return AMX_ERR_NONE; 56 | } 57 | 58 | PLUGIN_EXPORT void PLUGIN_CALL ProcessTick() 59 | { 60 | lua::process_tick(); 61 | lua::timer::tick(); 62 | } 63 | -------------------------------------------------------------------------------- /plugins/src/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | #include "sdk/amx/amx.h" 5 | #include 6 | 7 | typedef void(*logprintf_t)(const char* format, ...); 8 | extern logprintf_t logprintf; 9 | 10 | template 11 | void logerror(AMX *amx, const char *format, Args&&... args) 12 | { 13 | logprintf(format, std::forward(args)...); 14 | amx_RaiseError(amx, AMX_ERR_NATIVE); 15 | } 16 | 17 | template 18 | void logwarn(AMX *amx, const char *format, Args&&... args) 19 | { 20 | logprintf(format, std::forward(args)...); 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /plugins/src/natives.h: -------------------------------------------------------------------------------- 1 | #ifndef NATIVES_H_INCLUDED 2 | #define NATIVES_H_INCLUDED 3 | 4 | #include "main.h" 5 | #include "sdk/amx/amx.h" 6 | 7 | #define optparam(idx, optvalue) (params[0] / sizeof(cell) < idx ? optvalue : params[idx]) 8 | #define amx_OptStrParam(amx,idx,result,default) \ 9 | do { \ 10 | if (params[0] / sizeof(cell) < idx) { result = default; break; } \ 11 | cell *amx_cstr_; int amx_length_; \ 12 | amx_GetAddr((amx), (params[idx]), &amx_cstr_); \ 13 | amx_StrLen(amx_cstr_, &amx_length_); \ 14 | if (amx_length_ > 0 && \ 15 | ((result) = (char*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) \ 16 | amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_ + 1); \ 17 | else (result) = NULL; \ 18 | } while (0) 19 | 20 | int RegisterNatives(AMX *amx); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /plugins/src/utils/id_set_pool.h: -------------------------------------------------------------------------------- 1 | #ifndef ID_SET_POOL_H_INCLUDED 2 | #define ID_SET_POOL_H_INCLUDED 3 | 4 | #include "fixes/linux.h" 5 | #include "sdk/amx/amx.h" 6 | #include 7 | #include 8 | #include 9 | 10 | namespace aux 11 | { 12 | template 13 | class id_set_pool 14 | { 15 | std::unordered_set data; 16 | 17 | Type *add(Type *value) 18 | { 19 | data.insert(value); 20 | return value; 21 | } 22 | 23 | typedef typename std::unordered_set::iterator iterator; 24 | typedef typename std::unordered_set::const_iterator const_iterator; 25 | 26 | public: 27 | Type *add(std::unique_ptr &&value) 28 | { 29 | return add(value.release()); 30 | } 31 | 32 | Type *add() 33 | { 34 | return add(new Type()); 35 | } 36 | 37 | Type *add(Type&& value) 38 | { 39 | return add(new Type(std::move(value))); 40 | } 41 | 42 | size_t size() const 43 | { 44 | return data.size(); 45 | } 46 | 47 | void clear() 48 | { 49 | for(auto ptr : data) 50 | { 51 | delete ptr; 52 | } 53 | data.clear(); 54 | } 55 | 56 | iterator begin() 57 | { 58 | return data.begin(); 59 | } 60 | 61 | iterator end() 62 | { 63 | return data.end(); 64 | } 65 | 66 | bool remove(Type *value) 67 | { 68 | auto it = data.find(value); 69 | if(it != data.end()) 70 | { 71 | delete value; 72 | data.erase(it); 73 | return true; 74 | } 75 | return false; 76 | } 77 | 78 | bool get_by_id(cell id, Type *&value) 79 | { 80 | value = reinterpret_cast(id); 81 | if(data.find(value) != data.end()) 82 | { 83 | return true; 84 | } 85 | return false; 86 | } 87 | 88 | cell get_id(const Type *value) const 89 | { 90 | return reinterpret_cast(value); 91 | } 92 | 93 | bool contains(const Type *value) const 94 | { 95 | return data.find(const_cast(value)) != data.cend(); 96 | } 97 | 98 | iterator find(const Type *value) 99 | { 100 | return data.find(const_cast(value)); 101 | } 102 | 103 | const_iterator find(const Type *value) const 104 | { 105 | return data.find(const_cast(value)); 106 | } 107 | 108 | iterator erase(iterator it) 109 | { 110 | auto ptr = *it; 111 | delete ptr; 112 | return data.erase(it); 113 | } 114 | 115 | std::unique_ptr extract(iterator it) 116 | { 117 | Type *ptr = *it; 118 | data.erase(it); 119 | return std::unique_ptr(ptr); 120 | } 121 | 122 | id_set_pool() 123 | { 124 | 125 | } 126 | 127 | id_set_pool(id_set_pool &&obj) : data(std::move(obj.data)) 128 | { 129 | obj.data.clear(); 130 | } 131 | 132 | id_set_pool &operator=(id_set_pool &&obj) 133 | { 134 | if(this != &obj) 135 | { 136 | data = std::move(obj.data); 137 | obj.data.clear(); 138 | } 139 | return *this; 140 | } 141 | 142 | ~id_set_pool() 143 | { 144 | clear(); 145 | } 146 | }; 147 | } 148 | 149 | #endif 150 | -------------------------------------------------------------------------------- /plugins/src/utils/linear_pool.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_POOL_H_INCLUDED 2 | #define LINEAR_POOL_H_INCLUDED 3 | 4 | #include "fixes/linux.h" 5 | #include 6 | #include 7 | #include 8 | 9 | namespace aux 10 | { 11 | template 12 | class linear_pool 13 | { 14 | std::vector> data; 15 | 16 | size_t add(Type *value) 17 | { 18 | for(size_t i = 0; i < data.size(); i++) 19 | { 20 | if(data[i] == nullptr) 21 | { 22 | data[i] = std::unique_ptr(value); 23 | return i; 24 | } 25 | } 26 | size_t index = data.size(); 27 | data.push_back(std::unique_ptr(value)); 28 | return index; 29 | } 30 | 31 | public: 32 | size_t add() 33 | { 34 | return add(new Type()); 35 | } 36 | 37 | size_t add(Type&& value) 38 | { 39 | return add(new Type(std::move(value))); 40 | } 41 | 42 | size_t size() const 43 | { 44 | return data.size(); 45 | } 46 | 47 | bool remove(size_t index) 48 | { 49 | if(index >= data.size() || data[index] == nullptr) return false; 50 | if(index == data.size() - 1) 51 | { 52 | data.erase(data.begin() + index); 53 | }else{ 54 | data[index] = nullptr; 55 | } 56 | return true; 57 | } 58 | 59 | Type *get(size_t index) 60 | { 61 | if(index >= data.size() || data[index] == nullptr) return nullptr; 62 | return data[index].get(); 63 | } 64 | 65 | void clear() 66 | { 67 | data.clear(); 68 | } 69 | 70 | linear_pool() 71 | { 72 | 73 | } 74 | 75 | linear_pool(const linear_pool &obj) 76 | { 77 | for(auto &ptr : obj.data) 78 | { 79 | if(ptr == nullptr) 80 | { 81 | data.push_back(nullptr); 82 | }else{ 83 | data.push_back(std::make_unique(*ptr)); 84 | } 85 | } 86 | } 87 | 88 | linear_pool &operator=(const linear_pool &obj) 89 | { 90 | if(this != &obj) 91 | { 92 | data.clear(); 93 | for(auto &ptr : obj.data) 94 | { 95 | if(ptr == nullptr) 96 | { 97 | data.push_back(nullptr); 98 | }else{ 99 | data.push_back(std::make_unique(*ptr)); 100 | } 101 | } 102 | } 103 | return *this; 104 | } 105 | }; 106 | } 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /plugins/src/utils/optional.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTIONAL_H_INCLUDED 2 | #define OPTIONAL_H_INCLUDED 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace aux 11 | { 12 | template 13 | class optional; 14 | 15 | template 16 | optional make_optional(Args&&... args); 17 | 18 | template 19 | class optional 20 | { 21 | bool present; 22 | union { 23 | char dummy; 24 | T data; 25 | }; 26 | public: 27 | constexpr optional() : present(false), dummy() 28 | { 29 | 30 | } 31 | 32 | constexpr optional(std::nullptr_t) : present(false), dummy() 33 | { 34 | 35 | } 36 | 37 | constexpr optional(const T &obj) : present(true), data(obj) 38 | { 39 | 40 | } 41 | 42 | constexpr optional(T &&obj) : present(true), data(std::move(obj)) 43 | { 44 | 45 | } 46 | 47 | optional(const optional &other) : present(other.present), dummy() 48 | { 49 | if (present) 50 | { 51 | data = other.data; 52 | } 53 | } 54 | 55 | optional(optional &&other) : present(other.present), dummy() 56 | { 57 | if (present) 58 | { 59 | data = std::move(other.data); 60 | } 61 | } 62 | 63 | ~optional() 64 | { 65 | if (present) 66 | { 67 | data.~T(); 68 | } 69 | } 70 | 71 | void reset() 72 | { 73 | if (present) 74 | { 75 | data.~T(); 76 | present = false; 77 | } 78 | } 79 | 80 | template 81 | T& emplace(Args&&... args) 82 | { 83 | if (present) 84 | { 85 | data.~T(); 86 | } 87 | present = true; 88 | new (&data) T(std::forward(args)...); 89 | return data; 90 | } 91 | 92 | constexpr T value_or(T other) const 93 | { 94 | return present ? data : other; 95 | } 96 | 97 | constexpr T &value() 98 | { 99 | if (!present) throw std::logic_error("Optional object does not have a value."); 100 | return data; 101 | } 102 | 103 | constexpr T &operator*() 104 | { 105 | return data; 106 | } 107 | 108 | constexpr bool has_value() const 109 | { 110 | return present; 111 | } 112 | 113 | optional& operator=(std::nullptr_t) 114 | { 115 | reset(); 116 | return *this; 117 | } 118 | 119 | optional& operator=(const optional& other) 120 | { 121 | if (this != &other) 122 | { 123 | if (!other.present) 124 | { 125 | reset(); 126 | return *this; 127 | } 128 | else { 129 | present = true; 130 | data = other.data; 131 | } 132 | } 133 | return *this; 134 | } 135 | 136 | optional& operator=(optional&& other) 137 | { 138 | if (this != &other) 139 | { 140 | if (!other.present) 141 | { 142 | return *this = nullptr; 143 | } 144 | else { 145 | present = true; 146 | data = std::move(other.data); 147 | } 148 | } 149 | return *this; 150 | } 151 | 152 | constexpr bool operator==(std::nullptr_t) const 153 | { 154 | return present; 155 | } 156 | 157 | constexpr explicit operator bool() const 158 | { 159 | return present; 160 | } 161 | 162 | template 163 | constexpr typename std::conditional::type>::value, void, aux::optional::type>>::type map(Function func) 164 | { 165 | using ret_t = typename std::conditional::type>::value, void, aux::optional::type>>::type; 166 | return present ? ret_t(func(data)) : ret_t(); 167 | } 168 | 169 | template 170 | constexpr typename std::conditional::type>::value, void, aux::optional::type>>::type map(Function func) const 171 | { 172 | using ret_t = typename std::conditional::type>::value, void, aux::optional::type>>::type; 173 | return present ? ret_t(func(data)) : ret_t(); 174 | } 175 | 176 | template 177 | constexpr void decide(Function1 present, Function2 notpresent) 178 | { 179 | present ? present(data) : notpresent(); 180 | } 181 | 182 | template 183 | friend optional make_optional(Args&&... args); 184 | private: 185 | struct init_args_t {}; 186 | 187 | template 188 | constexpr optional(init_args_t, Args&&... args) : present(true), data(std::forward(args)...) 189 | { 190 | 191 | } 192 | }; 193 | 194 | template 195 | inline optional make_optional(Args&&... args) 196 | { 197 | return optional(optional::init_args_t(), std::forward(args)...); 198 | } 199 | } 200 | 201 | #endif 202 | -------------------------------------------------------------------------------- /plugins/src/utils/shared_id_set_pool.h: -------------------------------------------------------------------------------- 1 | #ifndef SHARED_ID_SET_POOL_H_INCLUDED 2 | #define SHARED_ID_SET_POOL_H_INCLUDED 3 | 4 | #include "fixes/linux.h" 5 | #include "sdk/amx/amx.h" 6 | #include 7 | #include 8 | 9 | namespace aux 10 | { 11 | template 12 | class shared_id_set_pool 13 | { 14 | std::unordered_map> data; 15 | 16 | std::shared_ptr add(Type *value) 17 | { 18 | return data[value] = std::shared_ptr(value); 19 | } 20 | 21 | public: 22 | std::shared_ptr add() 23 | { 24 | return add(new Type()); 25 | } 26 | 27 | std::shared_ptr add(Type&& value) 28 | { 29 | return add(new Type(std::move(value))); 30 | } 31 | 32 | size_t size() const 33 | { 34 | return data.size(); 35 | } 36 | 37 | bool remove(Type *value) 38 | { 39 | auto it = data.find(value); 40 | if(it != data.end()) 41 | { 42 | data.erase(it); 43 | return true; 44 | } 45 | return false; 46 | } 47 | 48 | void clear() 49 | { 50 | data.clear(); 51 | } 52 | 53 | bool get_by_id(cell id, Type *&value) 54 | { 55 | value = reinterpret_cast(id); 56 | if(data.find(value) != data.end()) 57 | { 58 | return true; 59 | } 60 | return false; 61 | } 62 | 63 | bool get_by_id(cell id, std::shared_ptr &value) 64 | { 65 | auto it = data.find(reinterpret_cast(id)); 66 | if(it != data.end()) 67 | { 68 | value = it->second; 69 | return true; 70 | } 71 | return false; 72 | } 73 | 74 | cell get_id(const Type *value) const 75 | { 76 | return reinterpret_cast(value); 77 | } 78 | 79 | cell get_id(const std::shared_ptr &value) const 80 | { 81 | return reinterpret_cast(value.get()); 82 | } 83 | 84 | bool contains(const Type *value) const 85 | { 86 | return data.find(const_cast(value)) != data.cend(); 87 | } 88 | 89 | std::shared_ptr get(Type *value) 90 | { 91 | auto it = data.find(value); 92 | if(it != data.end()) 93 | { 94 | return it->second; 95 | } 96 | return {}; 97 | } 98 | 99 | shared_id_set_pool() 100 | { 101 | 102 | } 103 | 104 | shared_id_set_pool(shared_id_set_pool &&obj) : data(std::move(obj.data)) 105 | { 106 | obj.data.clear(); 107 | } 108 | 109 | shared_id_set_pool &operator=(shared_id_set_pool &&obj) 110 | { 111 | if(this != &obj) 112 | { 113 | data = std::move(obj.data); 114 | obj.data.clear(); 115 | } 116 | return *this; 117 | } 118 | }; 119 | } 120 | 121 | #endif 122 | --------------------------------------------------------------------------------