neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
466 stars 76 forks source link

[Question] Is there a reliable way to get the full path of the Lua file that called a C# method? #159

Open AppeazeTheCheese opened 1 year ago

AppeazeTheCheese commented 1 year ago

I am currently working on a program that allows addons written in Lua to be added to it. These addons can be shared by anyone. The idea is I would like to limit file access for these scripts so they can only read and write files in their respective addon folder as a way to prevent people from creating and sharing malicious scripts. In order for this to be done, I need to be able to get which Lua file called the current method. I tried creating a new StackTrace() because that's how you would accomplish this with normal C#, but as I expected this didn't yield anything useful. I also tried creating a new LuaRuntimeException() and looking at the StackTrace property in there, but this doesn't seem to be populated until later.

neolithos commented 1 year ago

You need to overwrite LuaCompileOptions and take a look to the SandBox-method: https://github.com/neolithos/neolua/blob/157df5c2d42da847a554ff56f4af718c39246181/NeoLua/Lua.cs#L79-L129

madmagic007 commented 1 year ago

But is there a direct way to get the path of the file that contains the lua code that ran a specific method? Im trying to achieve the same but for completely different reason.

Im trying to make an auto script updater. My use case, the user will be able to download "modules", each as their own file and they can specify and update url. During loading of the lua file, my code should be able to find and possibly replace the file that contained the script.

neolithos commented 1 year ago

No, most information is thrown away due performance reason. But you can had this with your own compiler hook. To add it before.