Open ds1-e opened 5 months ago
Idea: Allow <script src=":resourceName/pathToScript.lua" type="client"/>
to load scripts from other resources using the MTA file path style?
But also, why is require from Lua disabled? Clearly PZomboid managed to have it.
But also, why is require from Lua disabled? Clearly PZomboid managed to have it.
Security reasons
But also, why is require from Lua disabled? Clearly PZomboid managed to have it.
Security reasons
but what about server side scripts tho
Idea: Allow
<script src=":resourceName/pathToScript.lua" type="client"/>
to load scripts from other resources using the MTA file path style?
There's three more reasons why i've mentioned keep it as close as possible to original:
Thank you for the clarification. Yeah, screw the meta.xml importing. require
is powerful, and has been tested on other platforms. We can make it secure and use it.
Speaking of modifying Lua, Luau exists... #2815
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta>
...
<include resource="library" >
<script src="server.lua" type="server" />
<script src="client.lua" type="client" />
</include>
</meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta> ... <include resource="library" > <script src="server.lua" type="server" /> <script src="client.lua" type="client" /> </include> </meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
What if other resource would have the same library name (resouce="library"
)?
Could there be more than 1 include
field in 1 meta then?
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta> ... <include resource="library" > <script src="server.lua" type="server" /> <script src="client.lua" type="client" /> </include> </meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
I like this. It's similar to what I suggested above having <script src=":resourceName/..."
but this is better because the include tag already works for making sure the included resource starts before the meta.xml's resource, thus ensuring the included scripts belong to a running resource.
My question is: are the scripts included from another resource executed again in the LuaVM of the resource which is including them, or they are only ran once in the original resource?
What if other resource would have the same library name (
resouce="library"
)? Could there be more than 1include
field in 1 meta then?
There can be as many includes in the meta.xml as you want. See wiki https://wiki.multitheftauto.com/wiki/Meta.xml
What if other resource would have the same library name (
resouce="library"
)? Could there be more than 1include
field in 1 meta then?There can be as many includes in the meta.xml as you want. See wiki https://wiki.multitheftauto.com/wiki/Meta.xml
I meant like, restricting include
so there would be only 1 include
per resource
Why would there need to be a maximum of 1 include? What if I wanna import multiple scripts from different libraries?
Why would there need to be a maximum of 1 include? What if I wanna import multiple scripts from different libraries?
Ah, my bad. I thought it will define a library for the require
function
<meta>
...
<include resource="library">
<script src='...' />
</include>
</meta>
require "library"
-- ...
I don't think tederis implemented "require" on his fork, but rather the meta.xml include scripts which I support.
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta> ... <include resource="library" > <script src="server.lua" type="server" /> <script src="client.lua" type="client" /> </include> </meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
What if other resource would have the same library name (
resouce="library"
)? Could there be more than 1include
field in 1 meta then?
As was already said, there could be as many includes as you want. The only limitation here is inability of cross inclusion when resources are referencing each others.
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta> ... <include resource="library" > <script src="server.lua" type="server" /> <script src="client.lua" type="client" /> </include> </meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
I like this. It's similar to what I suggested above having
<script src=":resourceName/..."
but this is better because the include tag already works for making sure the included resource starts before the meta.xml's resource, thus ensuring the included scripts belong to a running resource.My question is: are the scripts included from another resource executed again in the LuaVM of the resource which is including them, or they are only ran once in the original resource?
Included scripts are run in the same moment as native scripts do. That's guaranteed by an order of the resources startup.
@tederis I'm a fan of your method. Do you think this is a better alternative to reimplementing Lua require?
There's an another mechanism of scripts inclusion that we're using on a custom fork:
<meta> ... <include resource="library" > <script src="server.lua" type="server" /> <script src="client.lua" type="client" /> </include> </meta>
It's just an extension of the resource inclusion mechanism. Easy to do and perfectly fit into the existing system.
As i've mentioned on Discord, require imho has superior way over it, because it allows to import code to the place where it was called, not only to the whole resource. Regarding more security aspect, there could be a flag in meta.xml which will mark resource possible to require from other resources.
I've got some more insights on how it works, much thanks goes to @Niventill who elaborated on it. Main post got updated with MTA example, and require example usages, so please re-read it.
One of @TracerDS concerns was that _G would get polluted, in require you can easily solve it (see mathutils example). When it comes to meta.xml approach, it gets tricky, because code would be loaded to each script of target resource. Well, perhaps you could add importTo attribute:
<meta>
...
<include resource="library" >
<script src="server.lua" importTo="serverA.lua" type="server" />
<script src="client.lua" importTo="clientB.lua" type="client" />
</include>
</meta>
Yes, but it makes things a bit complicated, and you probably can see in which direction it is going. Not a convenient one, for sure. I believe there was a reason why require stayed nearly the same (excluding obvious security changes), on all the aforementioned platforms.
@tederis I think it is fair to say that point goes in favor of require.
@tederis I'm a fan of your method. Do you think this is a better alternative to reimplementing Lua require?
For my 15 years of experience in MTA, I'm inclined to say that require
is excessive. I usually expect a global(for the entire VM) import in roughly 9 cases of 10 (again, from my experience). To prevent _G
from pollution a library script can be wrapped into a table, that's it. I'm not against require
, it's just impractical for me. But it can be an alternative to what I said, perhaps. I rather worried about implementation difficulties. My approach relies on the existing system which guarantees that a library script will be loaded before the dependent one. On the other side, require
will require(sorry for the pun) significant work to make client scripts work correctly.
On the other side,
require
will require(sorry for the pun) significant work to make client scripts work correctly.
Moreover, something similar to a regular require
can be done using file functions and loadstring.
Is your feature request related to a problem? Please describe.
I propose to recreate Lua's require and keep it as close as possible to original (default require is disabled since ever). It would be good idea to keep consistency with it, especially for those people who knew Lua before coming to MTA (it is common and widely used tool outside of it). For instance, this is how it is utilized in Project Zomboid:
Example taken directly from MTA: You have a dxDraw/utility function which gets used in few other resources. In order to not copy it over and over, and to keep codebase clean, you decide to create utility resource which will contain it. There are few ways to make use of it:
You need to add this long ass line to every resource, where utility function gets called.
Which leads us to the point, where there should be native support for it. Now, this is a moment when require steps in:
dxutils:
resourceA:
You might want to keep things more organized, or simply not pollute _G, so:
mathutils:
resourceB:
If you want to only access certain function, why not:
or:
It is also worth to mention that require isn't replacement to exports, but rather great addition. Since both of them have different purpose.
Describe the solution you'd like
requireLua / luaRequire
Describe alternatives you've considered
loadstring
Additional context
3128
Security Policy