Sirius902 / LuaBackend

A standalone script interpreter for PC games. Compatible with LuaEngine.
GNU General Public License v3.0
41 stars 5 forks source link

Optimize reading/writing #9

Closed MainMemory closed 1 year ago

MainMemory commented 2 years ago

I've noticed that you initialize a protect_lock on every read and write operation, which results in two calls to VirtualProtect every time. Instead, you could look up all of the segments in the EXE and change their protections on startup, which would allow you to perform read/write operations without any additional function calls, like Panacea does in this code.

Sirius902 commented 1 year ago

Implemented in a4454551bd74006b40177be376062251f408f2c8. Thanks for this issue! I recall trying to make this optimization earlier but I couldn't figure out exactly how to do it. One concern I have is some scripts blindly write to invalid addresses like address 0, which is always a crash regardless of changed permissions as far as I know at least for 0, but for now I put in a check that prevents writing to / reading from address 0 specifically. I'm hoping that's good enough, it seems to work fine on my end. Once I can mess with it a bit more and address some other issues I'll make a release with this change.

Sirius902 commented 1 year ago

@MainMemory Oh also I think the Panacea code might need a slight modification, it needed to be section[i].VirtualAddress for me instead of section->VirtualAddress otherwise only the first section's permissions are changed. https://github.com/Sirius902/LuaBackend/blob/a4454551bd74006b40177be376062251f408f2c8/src/main_dll.cpp#L119