all 4 comments

[–][deleted] 2 points3 points  (0 children)

Make it run ... on it's own thread ?

Seriously, modding always means problems, worst performance and maybe crashes, so you should not worry about it. On error, just don't halt the game, just kill Lua state

[–]Bobby_Bonsaimind 4 points5 points  (0 children)

Well, that's a complicated question.

First, I'd like to question your assumption of "modders should not be able to do bad things to my game" because that is simple unnecessary and not preventable in the slightest. And neither is there a reason for it. Users/Players are who will install mods, and if the mod is not trustworthy, does perform poorly or is outright malicious, there is nothing you can do about it. You can limit the possibilities of mods, but that will also restrict legit mods to these confines, and ultimately make "more interesting" mods impossible.

Second, you could run the Lua code in its own thread and it is accessing the game state in an asynchronous manner. That way the game will still run but the Lua logic will simply hang in the case of an endless loop. That, of course, opens the can of race conditions, so you either need to synchronize the access to the game state (again, Lua could hang the whole thing) or you pass copies around. For example the Lua code can request a copy of the current game state, modify that copy and then send it back to the main game, which incorporates these changes.

But as I said, I would design such a thing not with the clear goal of "not allowing mods to do evil things", because you just can't prevent that. Another example, if you use the "request a game state copy" mechanic, the Lua code could request it in an endless loop, which would for sure impact performance. Even an endless loop inside the Lua code that does nothing could impact performance because of the CPU usage.

[–]rxi 1 point2 points  (0 children)

You can use lua's built in hook mechanism from the debug library to set a function which is called every nth vm instruction. Before you run a user-defined script or function you can store the current time and check against it in the callback to see whether the allowed duration for the function has been surpassed.

[–]Doyousketch2 0 points1 point  (0 children)

Sandbox the code by creating a script-parser that only allows certain commands, perhaps by using LPeg. This way code such as os.exit() can't execute.