C# 脚本和安全
在我的游戏(也是用 C# 编写的)中,我想包含某种自定义支持。 速度是一个非常令人担忧的问题,因为我计划将游戏玩法的主要部分转移到脚本中。 因此,我选择使用 C# 来编写游戏内事件和内容的脚本。 用户还应该能够用 C# 为游戏编写脚本和插件。 (顺便说一句,我知道 C# 不是脚本语言。)
我将为用户提供一个(静态)类,其中包含与游戏交互所需的所有函数。 用户还可以提供代码作为源(在这种情况下游戏将对其进行编译)。 编译的用户“脚本”在某些情况下也可以/将会传输给其他玩家。 例如,用户可以在他的家中或其他地方构建脚本陷阱。
这是我的问题:
安全
如何确保所提供的代码只能调用所提供的 API(类)的内容?为了防止脚本作弊、恶意活动...
有没有办法在某种虚拟机或低信任环境中运行编译后的代码?
速度
这种方法足够快吗? (每隔一帧调用多达 100 个自定义程序集的函数)。 有什么建议吗?
也许可以通过将所有用户内容编译成一个大程序集或其他东西来获得速度优势? ??
尺寸
如何使编译后的代码体积较小(除了压缩之外)? 因为玩家可能需要下载数十个脚本...
有没有办法去掉不是绝对必要的东西?像调试信息或类名之类的...
运行时编译对我来说相当新(这就是我在这里问的原因)。因此,如果能指出一些主要的初学者错误和/或安全问题,那就太好了。
编辑:
只是为了澄清: 用户插件/脚本或任何您想调用的内容都将是一个类(也用 C# 编写),它必须实现特定的函数,如“GetAddonInfo”、“Init”、“Update”... 就像从我的抽象“Addon”类派生的普通 C# 类一样。
In my game (that is also written in C#) I want to include some kind of customization support.
Speed is a great concern as I plan to offload major parts of the gameplay to scripts.
As a result I choose to use C# for scripting ingame events and stuff.
Users should also be able to write scripts and addons for the game in C#.
(I know that C# is NOT a scripting language btw.)
I will provide the users with a (static) class that contains all the functions needed to interact with the game.
The users will also be able to provide the code as source (the game will compile it in that case).
Compiled user "scripts" can / will also be transmitted to other players in some cases.
For example a user can build in scripted traps in his home or whatever.
Here are my questions:
Security
How can I ensure that the provided code is forced to ONLY call stuff of the provided API (class) ? To prevent cheating with scripts, malicious activities...
Is there a way to maybe run the compiled code(s) in a sort of VM or low-trust environment ??
Speed
Will this approach be fast enough ? (Calling functions from up to 100 custom assemblys every or every second frame).
Any tips on this?Maybe it's possible to get a speed advantage by compiling all user content into one big assembly or whatever ??
Size
How can I make the compiled code small in size (besides compressing it) ?
Because players might need to download tens of scripts...Is there a way to strip stuff that isn't absolute necessary ? Like debug information or classnames and the likes...
Runtime compilation is fairly new to me (That's why I ask here). So it would be nice to have some major beginner mistakes and / or security concerns pointed out.
Edit:
Just for clarification:
A user plugin/script or whatever you would like to call it will be a class (also written in C#) that has to implement specific functions like "GetAddonInfo", "Init", "Update" ...
Just like a normal C# class that is derived from my abstract "Addon" class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想看看旧的 Microsoft 示例项目的重做
http://terrarium2.codeplex.com/
它基本上可以完成您想要的一切。
将代码编译成 DLL 并在环境中运行它们
检查允许的 API 调用。
版本
2 被重写以利用 Web 服务和许多其他功能。从你的问题来看,你可能对 Terrarium 1 更感兴趣。但是,我似乎无法找到它,所以你可能不得不“适应”更复杂的 2.0 版本。
You want to take a look at this rework of an old Microsoft example project
http://terrarium2.codeplex.com/
It basically does everything you want.
Compiles code into DLLs and runs them in the environemnt
Checks for allowed API calls.
etc.
Version 2 was re-written to make use of web services and a number of other features. From your question it sounds like you might be more interested in Terrarium 1. However, I can't seem to track it down so you may have to "settle" for the more complicated 2.0 release.
查看 MEF 和 MAF——它们都是旨在将 C# 和 VB 脚本添加到现有应用程序的框架。您可以为脚本创建应用程序域,然后使用代码访问安全性来严格限制这些脚本可以执行的操作。
这个问题可能会有所帮助。
在 MEF 和 MAF (System.AddIn) 之间进行选择
Look into MEF and MAF -- they are both frameworks designed to add C# and VB scripting to existing applications. You can create an appdomain for your scripts and then use code access security to severely restrict what those scripts can do.
This question might help.
Choosing between MEF and MAF (System.AddIn)