向 .NET 应用程序添加脚本功能 [英] Adding scripting functionality to .NET applications
问题描述
我有一个用 C# 编写的小游戏.它使用数据库作为后端.它是一个交易纸牌游戏,我想把纸牌的功能作为脚本来实现.>
我的意思是我本质上有一个接口,ICard
,它是一个卡片类实现的(public class Card056: ICard
),它包含一个被调用的函数通过游戏.
现在,为了使东西可维护/可修改,我希望将每张卡的类作为数据库中的源代码,并在第一次使用时对其进行编译.因此,当我必须添加/更改卡片时,我只需将其添加到数据库并告诉我的应用程序进行刷新,无需任何程序集部署(特别是因为我们将讨论每张卡 1 个程序集,这意味着数百个程序集).
这可能吗?从源文件注册一个类,然后实例化它,等等.
ICard Cards[current] = new MyGame.CardLibrary.Card056();Cards[current].OnEnterPlay(ref currentGameState);
语言是 C#,但如果可以用任何 .NET 语言编写脚本,则额外奖励.
Oleg Shilo 的 C# 脚本解决方案(在代码项目中)确实很好地介绍了在您的应用程序中提供脚本功能.
另一种方法是考虑专门为脚本编写的语言,例如 IronRuby、IronPython 或 Lua.
IronPython 和 IronRuby 现已上市.
有关嵌入 IronPython 的指南,请阅读如何通过 10 个简单的步骤将 IronPython 脚本支持嵌入到您现有的应用程序中.
Lua 是游戏中常用的脚本语言.有一个用于 .NET 的 Lua 编译器,可从 CodePlex 获得 -- http://www.codeplex.com/Nua
如果您想了解如何在 .NET 中构建编译器,那么该代码库非常适合阅读.
完全不同的角度是尝试 PowerShell.有许多将 PowerShell 嵌入应用程序的示例——这里有一个关于该主题的完整项目:Powershell 隧道
I have a little game written in C#. It uses a database as back-end. It's a trading card game, and I wanted to implement the function of the cards as a script.
What I mean is that I essentially have an interface, ICard
, which a card class implements (public class Card056: ICard
) and which contains a function that is called by the game.
Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies).
Is that possible? Register a class from a source file and then instantiate it, etc.
ICard Cards[current] = new MyGame.CardLibrary.Card056();
Cards[current].OnEnterPlay(ref currentGameState);
The language is C# but extra bonus if it's possible to write the script in any .NET language.
Oleg Shilo's C# Script solution (at The Code Project) really is a great introduction to providing script abilities in your application.
A different approach would be to consider a language that is specifically built for scripting, such as IronRuby, IronPython, or Lua.
IronPython and IronRuby are both available today.
For a guide to embedding IronPython read How to embed IronPython script support in your existing app in 10 easy steps.
Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- http://www.codeplex.com/Nua
That codebase is a great read if you want to learn about building a compiler in .NET.
A different angle altogether is to try PowerShell. There are numerous examples of embedding PowerShell into an application -- here's a thorough project on the topic: Powershell Tunnel
这篇关于向 .NET 应用程序添加脚本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!