从文件中的偏移量加载Library

发布于 2024-11-11 07:24:31 字数 411 浏览 7 评论 0原文

我正在编写一个可编写脚本的游戏引擎,我有大量的类来执行各种任务。引擎的大小正在快速增长,因此我想到将大型可执行文件拆分为 dll 模块,以便仅包含游戏编写者实际使用的组件。当用户编译他们的游戏(也就是说他们的脚本)时,我希望正确的 dll 成为最终可执行文件的一部分。我已经有相当多的覆盖数据,所以我想我可以将 dll 存储为该块的一部分。我的问题归结为:

是否有可能欺骗 LoadLibrary 以某个偏移量开始读取文件?这将使我不必将 dll 提取到不干净的临时文件中,或者完全放弃自动包含 dll 并简单地指示我的用户将 dll 与他们的游戏一起打包。

最初,我想过采用“从内存加载 dll”的方法,但出于可移植性的考虑而拒绝了它,而且只是因为它看起来是一个可怕的黑客攻击。

有什么想法吗?

亲切的问候,

菲利普·本尼法尔

I am writing a scriptable game engine, for which I have a large number of classes that perform various tasks. The size of the engine is growing rapidly, and so I thought of splitting the large executable up into dll modules so that only the components that the game writer actually uses can be included. When the user compiles their game (which is to say their script), I want the correct dll's to be part of the final executable. I already have quite a bit of overlay data, so I figured I might be able to store the dll's as part of this block. My question boils down to this:

Is it possible to trick LoadLibrary to start reading the file at a certain offset? That would save me from having to either extract the dll into a temporary file which is not clean, or alternatively scrapping the automatic inclusion of dll's altogether and simply instructing my users to package the dll's along with their games.

Initially I thought of going for the "load dll from memory" approach but rejected it on grounds of portability and simply because it seems like such a horrible hack.

Any thoughts?

Kind regards,

Philip Bennefall

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朮生 2024-11-18 07:24:31

你正在试图解决一个不存在的问题。加载 DLL 实际上不需要任何物理内存。 Windows 为 DLL 内容创建内存映射文件。仅当您的程序调用该代码时,才会加载 DLL 中的代码。除了保留的内存页之外,未使用的代码不需要任何系统资源。在 32 位操作系统上,您有 20 亿字节的内存。你必须编写很多代码来消耗它们,50兆字节的机器代码已经是一个非常大的程序了。

内存映射也是您无法使 LoadLibrary() 执行您想做的事情的原因。不存在您需要这样做的现实场景。

查看链接器的 /DELAYLOAD 选项以提高启动性能。

You are trying to solve a problem that doesn't exist. Loading a DLL doesn't actually require any physical memory. Windows creates a memory mapped file for the DLL content. Code from the DLL only ever gets loaded when your program calls that code. Unused code doesn't require any system resources beyond reserved memory pages. You have 2 billion bytes worth of that on a 32-bit operating system. You have to write a lot of code to consume them all, 50 megabytes of machine code is already a very large program.

The memory mapping is also the reason you cannot make LoadLibrary() do what you want to do. There is no realistic scenario where you need to.

Look into the linker's /DELAYLOAD option to improve startup performance.

暖树树初阳… 2024-11-18 07:24:31

我认为该任务的每个解决方案都是“可怕的黑客”,仅此而已。

我看到的最简单的方法是创建您自己的虚拟驱动器,该驱动器提供自定义文件系统,并破解从一个真实文件(库的编译)到多个单独的 DLL 的系统访问路径。例如 TrueCrypt 就是这样做的(它是开源的)。并且您可以使用 LoadLibrary 函数而不进行任何更改。

但我认为唯一正确的方法是改变你的任务并且不要使用这种方法。我认为你需要使用结构、指针等创建自己的脚本解释器和编译器。

最主要的是我不明白您从使用库中得到的好处。我认为当前任何已编译的代码都不会那么重,并且可能打包得很好。任何其他资源都可以在第一次调用时动态加载。您所需要做的就是以正确的方式组织脚本引擎所有组件的工作周期。

I think every solution for that task is "horrible hack" and nothing more.

Simplest way that I see is create your own virtual drive that present custom filesystem and hacks system access path from one real file (compilation of your libraries) to multiple separate DLL-s. For example like TrueCrypt does (it's open-source). And than you may use LoadLibrary function without changes.

But only right way I see is change your task and don't use this approach. I think you need to create your own script interpreter and compiler, using structures, pointers and so on.

The main thing is that I don't understand your benefit from use of libraries. I think any compiled code in current time does not weigh so much and may be packed very good. Any other resources may be loaded dynamically at first call. All you need to do is to organize the working cycles of all components of the script engine in right way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文