Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Lua packages #680

Closed
wants to merge 19 commits into from
Closed

Add support for Lua packages #680

wants to merge 19 commits into from

Conversation

qaisjp
Copy link
Contributor

@qaisjp qaisjp commented Oct 31, 2018

Work done by @darkdreamingdan.

This pull request is to record any remaining hurdles that need to be jumped before merging.

Personally, I am only happy with merging this if it's forward compatible with an expansion of this feature to support LuaRocks. Discussion about LuaRocks should be done in a separate issue.

Fixes #654

Similar design to before, but in C++: use require, and package.loaded is
enabled.

Pure Lua modules must be placed correctly in either:
<resource>/?.lua
<resource>/?/init.lua

Lua C Modules must be placed in:
/server/mods/deathmatch/modules/?.dll

To easily test or use modules for Windows: Download and install Lua for
Windows.  Copy contents of C:\Program Files (x86)\Lua\5.1\clibs into
/server/mods/deathmatch/modules.  Use e.g. require"lfs"

To-do:
* Port over to clientside (without C module support)
* More detailed error messages for Pure lua packages
* ACL support for C modules.
* Extensive testing
Fixed if someone naughty sets package as nil.
For each resource that wants to use a C module, a right needs to be
added in the following format:
<right name="module.<name>" access="true"></right>

For example,
<right name="module.lfs" access="true"></right>

A * wildcard works to grant a resource access to all modules.
@qaisjp qaisjp added the enhancement New feature or request label Oct 31, 2018
@darkdreamingdan
Copy link
Member

I think this should work with LuaRocks

@CrosRoad95
Copy link
Contributor

CrosRoad95 commented Nov 1, 2018

make require allow get lua files outside mta folder which is defined, example /var/mta on linux
this could be useful for hostings, could decrease amount of files and speed up scripting

hostings can control mtaserver.conf file and keep this path always in place, don't let user to change it
for security, mta while running could checking this path and if contain file like mta.txt with content allow this folder for require, if not, then this folder will not work for require

@CrosRoad95
Copy link
Contributor

CrosRoad95 commented Nov 1, 2018

I made some tests, serverside ( shared code, i don't think there are difference between client and server ).

Checks for incorrect behaviour

  1. simply require("module")
    i think, should show only path from mta root directory
    image

  2. while require myself filename = "s.lua" and require "s". I don't think this is issue, but could information you can't require myself
    image

  3. I created 3 files text.lua in each of possible according to pattern:
    print("test in ", "D:/mtasa require test/InstallFiles/server/mods/deathmatch/resources/t")
    where second argument is current pattern ( from screenshot above ) and properly loaded only first occur

longText = string.rep("a",2^24)
require(longText) -- just spam in console, and a bit lag
require("../../../../../asdf")

image

i was suprised when i discovered this

require("ŕşôíŠ%v=Ü6ÇŘ˙˙śeDL×�`�­��ëGF–±Öş|5ľxuóľ�") -- https://pastebin.com/eA1xHZQr

when you trying require this monster, windows produce this sound:
https://www.youtube.com/watch?v=qlUFWSiOXpM

setTimer(function()
  for k=1,100 do
    randomString = {}
    for i=1,40 do
    randomString[#randomString + 1] = string.char ( math.random(255) )
    end
    randomString = table.concat(randomString)
    require(randomString)
  end
end,50,0)

nothing special, just make server logs spam
i ran this for few minutes, crash doesn't appear, server working normally after stopping resource

  1. Test while required script has error
    image

  2. Creating elements in required resource working. I restarted resource many times, and always print same thing

test.lua

a = createElement("test")
return a
rrr = require "test"

iprint(rrr, getElementsByType("test"))

image

Tests in practice usage

rrr = require "test"

iprint(rrr, rrr.func(2,2))

where test is

randomModule = {}

randomModule.int = 1
randomModule.float = math.pi
randomModule.string = "hi"
randomModule.table = {a = 3, [1] = 9, asdf = "asdf"}
randomModule.func = function(a,b) return a+b end

return randomModule

image

  1. Traceback working

test.lua

function traceback ()
  local level = 1
  while true do
    local info = debug.getinfo(level, "Sl")
    if not info then break end
    if info.what == "C" then   -- is a C function?
      print(level, "C function")
    else   -- a Lua function
      print(string.format("[%s]:%d",
                          info.short_src, info.currentline))
    end
    level = level + 1
  end
end

return traceback

t.lua

rrr = require "test"

iprint(rrr())

image

Bugs, issues

  1. Infinity loop wrong source information

s.lua

print ("blabla")

rrr = require "test" -- contain infinity loop

print("bla bla")

image

  1. require myself should be disabled.

  2. While incorrect file is passed, only path from server root should be showned Improved warnings for lua-package #683

Copy link
Contributor

@patrikjuvonen patrikjuvonen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of what needs fixing and/or double-checking:

  • Infinite loop error should output some more useful trace
  • Error out require() if trying to require the same script file

Other than that seems fine with #683 merged into this.
I will test LuaRocks with this soon and add any further notes on this review.

CrosRoad95 and others added 2 commits February 20, 2019 20:33
* Improved warnings

Previous looks like shit

* fix conflicts

* fix conflicts

* Fix indentation

* Tweak location text

* fix path

* fixed `nil` return in require

* Fix code formatting
@patrikjuvonen patrikjuvonen added this to In progress in Lua modules via automation Feb 24, 2019
@patrikjuvonen patrikjuvonen moved this from In progress to To do in Lua modules Feb 24, 2019
@patrikjuvonen patrikjuvonen moved this from To do to In progress in Lua modules Feb 24, 2019
@botder botder added this to the Backlog milestone Mar 4, 2019
@CrosRoad95
Copy link
Contributor

any plans to marge?

@qaisjp
Copy link
Contributor Author

qaisjp commented Apr 13, 2019

@patrikjuvonen — I have investigated requiring the same file.

Test code:

s = io.read("*l")
if s == "yes" then
    print("YUP")
    require("test")
end
print(s, s:len())
  • Lua 5.1 (MTA's Lua, but not tested in MTA) errors out after the second time you enter "yes" (so this file is run a total of two files, once on initial load, and once after the first require)
  • Lua 5.3.5 simply repeatedly says "YUP" (after entering "yes")

I think we should just do the latter.

Here is the commit in Lua that removes this error message.

The Lua specification also does not define what should happen if require requires itself.

@qaisjp
Copy link
Contributor Author

qaisjp commented Apr 13, 2019

Also, this is how 5.3.5 behaves for infinite loops:

cat test.lua
require "test"
print "hello"lua test.lua
lua: C stack overflow
stack traceback:
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	...
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	./test.lua:1: in main chunk
	[C]: in function 'require'
	test.lua:1: in main chunk
	[C]: in ?

@patrikjuvonen
Copy link
Contributor

Right now requiring the same file crashes the server.

@patrikjuvonen patrikjuvonen changed the title Lua packages WIP: Lua packages May 12, 2019
@qaisjp qaisjp marked this pull request as draft April 9, 2020 16:10
@qaisjp qaisjp changed the title WIP: Lua packages Add support for Lua packages Apr 9, 2020
@buepas
Copy link
Contributor

buepas commented Jun 8, 2020

Since the development of this PR has halted (has it?), I think it helps if there is an up-to-date todolist of what has to be done in order to complete this PR.
To-dos so far:

  • More detailed error messages for Pure lua packages (?)
  • Extensive testing
  • Infinite loop error should output some more useful trace

@qaisjp qaisjp closed this Dec 29, 2020
Lua modules automation moved this from In progress to Done Dec 29, 2020
@patrikjuvonen patrikjuvonen reopened this Dec 29, 2020
Lua modules automation moved this from Done to In progress Dec 29, 2020
Lua modules automation moved this from In progress to Done Dec 29, 2020
@patrikjuvonen patrikjuvonen moved this from Done to To do in Lua modules Dec 29, 2020
@patrikjuvonen
Copy link
Contributor

A new PR should be created by someone who wants to author this enhancement to the end. Remember to link to this, gather necessary information from here and elsewhere to the new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Lua modules
  
To do
Development

Successfully merging this pull request may close these issues.

More math functions for mta
7 participants