<p>I have a little game written in C#. It uses a database as back-end. It's 
a <a href="http://en.wikipedia.org/wiki/Collectible_card_game" rel="noreferrer">trading card game</a>, and I wanted to implement the function of the cards as a script.</p>

<p>What I mean is that I essentially have an interface, <code>ICard</code>, which a card class implements (<code>public class Card056: ICard</code>) and which contains a function that is called by the game.</p>

<p>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).</p>

<p>Is that possible? Register a class from a source file and then instantiate it, etc.</p>

<pre><code>ICard Cards[current] = new MyGame.CardLibrary.Card056();
Cards[current].OnEnterPlay(ref currentGameState);
</code></pre>

<p>The language is C# but extra bonus if it's possible to write the script in any .NET language.</p>

[{'answer_id': 269, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, I thought about that, but I soon figured out that another Domain-Specific-Language (DSL) would be a bit too much.</p>\n\n<p>Essentially, they need to interact with my gamestate in possibly unpredictable ways. For example, a card could have a rule "When this cards enter play, all your undead minions gain +3 attack against flying enemies, except when the enemy is blessed". As trading card games are turn based, the GameState Manager will fire OnStageX events and let the cards modify other cards or the GameState in whatever way the card needs.</p>\n\n<p>If I try to create a DSL, I have to implement a rather large feature set and possibly constantly update it, which shifts the maintenance work to another part without actually removing it.</p>\n\n<p>That\'s why I wanted to stay with a "real" .NET language to essentially be able to just fire the event and let the card manipulate the gamestate in whatever way (within the limits of the code access security).</p>\n'}, {'answer_id': 307, 'author': 'Leon Bambrick', 'author_id': 49, 'author_profile': 'https://Stackoverflow.com/users/49', 'pm_score': 6, 'selected': True, 'text': '<p><a href="https://www.codeproject.com/Articles/8656/C-Script-The-Missing-Puzzle-Piece" rel="nofollow noreferrer">Oleg Shilo\'s C# Script solution (at The Code Project</a>) really is a great introduction to providing script abilities in your application.</p>\n\n<p>A different approach would be to consider a language that is specifically built for scripting, such as <a href="https://en.wikipedia.org/wiki/IronRuby" rel="nofollow noreferrer">IronRuby</a>, <a href="https://en.wikipedia.org/wiki/IronPython" rel="nofollow noreferrer">IronPython</a>, or <a href="https://en.wikipedia.org/wiki/Lua_%28programming_language%29" rel="nofollow noreferrer">Lua</a>.</p>\n\n<p>IronPython and IronRuby are both available today.</p>\n\n<p>For a guide to embedding IronPython read\n<a href="https://blogs.msdn.microsoft.com/jmstall/2005/09/01/how-to-embed-ironpython-script-support-in-your-existing-app-in-10-easy-steps/" rel="nofollow noreferrer">How to embed IronPython script support in your existing app in 10 easy steps</a>.</p>\n\n<p>Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- <a href="http://www.codeplex.com/Nua" rel="nofollow noreferrer">http://www.codeplex.com/Nua</a></p>\n\n<p>That codebase is a great read if you want to learn about building a compiler in .NET.</p>\n\n<p>A different angle altogether is to try <a href="https://en.wikipedia.org/wiki/PowerShell" rel="nofollow noreferrer">PowerShell</a>. There are numerous examples of embedding PowerShell into an application -- here\'s a thorough project on the topic: \n<a href="http://code.msdn.microsoft.com/PowerShellTunnel/Wiki/View.aspx?title=PowerShellTunnel%20Reference" rel="nofollow noreferrer" title="PowerShell Tunnel">Powershell Tunnel</a></p>\n'}, {'answer_id': 344, 'author': 'Eric Haskins', 'author_id': 100, 'author_profile': 'https://Stackoverflow.com/users/100', 'pm_score': 3, 'selected': False, 'text': '<p>You might be able to use IronRuby for that. </p>\n\n<p>Otherwise I\'d suggest you have a directory where you place precompiled assemblies. Then you could have a reference in the DB to the assembly and class, and use reflection to load the proper assemblies at runtime.</p>\n\n<p>If you really want to compile at run-time you could use the CodeDOM, then you could use reflection to load the dynamic assembly. <a href="https://learn.microsoft.com/dotnet/api/microsoft.csharp.csharpcodeprovider" rel="nofollow noreferrer">Microsoft documentation article which might help</a>.</p>\n'}, {'answer_id': 359, 'author': 'Jesse Ezell', 'author_id': 119, 'author_profile': 'https://Stackoverflow.com/users/119', 'pm_score': 3, 'selected': False, 'text': "<p>You could use any of the DLR languages, which provide a way to really easily host your own scripting platform. However, you don't have to use a scripting language for this. You could use C# and compile it with the C# code provider. As long as you load it in its own AppDomain, you can load and unload it to your heart's content.</p>\n"}, {'answer_id': 3637, 'author': 'Nathan', 'author_id': 541, 'author_profile': 'https://Stackoverflow.com/users/541', 'pm_score': 3, 'selected': False, 'text': '<p>If you don\'t want to use the DLR you can <a href="http://docs.codehaus.org/display/BOO/Boo+as+an+embedded+scripting+language" rel="nofollow noreferrer">use Boo (which has an interpreter)</a> or you could consider <a href="https://archive.codeplex.com/?p=scriptdotnet" rel="nofollow noreferrer">the Script.NET (S#) project on CodePlex</a>. With the Boo solution you can choose between compiled scripts or using the interpreter, and Boo makes a nice scripting language, has a flexible syntax and an extensible language via its open compiler architecture. Script.NET looks nice too, though, and you could easily extend that language as well as its an open source project and uses a very friendly Compiler Generator (<a href="https://github.com/IronyProject/" rel="nofollow noreferrer">Irony.net</a>).</p>\n'}, {'answer_id': 7217, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>The main application that my division sells does something very similar to provide client customisations (which means that I can\'t post any source). We have a C# application that loads dynamic VB.NET scripts (although any .NET language could be easily supported - VB was chosen because the customisation team came from an ASP background).</p>\n\n<p>Using .NET\'s CodeDom we compile the scripts from the database, using the VB <code>CodeDomProvider</code> (annoyingly it defaults to .NET 2, if you want to support 3.5 features you need to pass a dictionary with "CompilerVersion" = "v3.5" to its constructor). Use the <code>CodeDomProvider.CompileAssemblyFromSource</code> method to compile it (you can pass settings to force it to compile in memory only.</p>\n\n<p>This would result in hundreds of assemblies in memory, but you could put all the dynamic classes\' code together into a single assembly, and recompile the whole lot when any change. This has the advantage that you could add a flag to compile on disk with a <a href="http://en.wikipedia.org/wiki/Program_database" rel="nofollow noreferrer">PDB</a> for when you\'re testing, allowing you to debug through the dynamic code.</p>\n'}, {'answer_id': 79013, 'author': 'harningt', 'author_id': 12713, 'author_profile': 'https://Stackoverflow.com/users/12713', 'pm_score': 3, 'selected': False, 'text': '<p>I\'d suggest using <a href="http://luaforge.net/projects/luainterface/" rel="noreferrer">LuaInterface</a> as it has fully implemented Lua where it appears that Nua is not complete and likely does not implement some very useful functionality (coroutines, etc).</p>\n\n<p>If you want to use some of the outside prepacked Lua modules, I\'d suggest using something along the lines of 1.5.x as opposed to the 2.x series that builds fully managed code and cannot expose the necessary C API.</p>\n'}, {'answer_id': 4289753, 'author': 'Eric Falsken', 'author_id': 192536, 'author_profile': 'https://Stackoverflow.com/users/192536', 'pm_score': 2, 'selected': False, 'text': '<p>The next version of .NET (5.0?) has had a lot of talk about opening the "compiler as a service" which would make things like direct script evaluation possible.</p>\n'}, {'answer_id': 11527418, 'author': 'Kat Lim Ruiz', 'author_id': 915865, 'author_profile': 'https://Stackoverflow.com/users/915865', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m using LuaInterface1.3 + Lua 5.0 for a NET&nbsp;1.1 application.</p>\n\n<p>The issue with Boo is that every time you parse/compile/eval your code on the fly, it creates a set of boo classes so you will get memory leaks.</p>\n\n<p>Lua in the other hand, does not do that, so it\'s very very stable and works wonderful (I can pass objects from C# to Lua and backwards).</p>\n\n<p>So far I haven\'t put it in PROD yet, but seems very promising.</p>\n\n<p><strong>I did have memory leaks issues in PROD using LuaInterface + Lua 5.0</strong>, therefore I used Lua 5.2 and linked directly into C# with DllImport. <strong>The memory leaks were inside the LuaInterface library.</strong></p>\n\n<p>Lua 5.2: from <a href="http://luabinaries.sourceforge.net" rel="nofollow noreferrer">http://luabinaries.sourceforge.net</a> and <a href="http://sourceforge.net/projects/luabinaries/files/5.2/Windows%20Libraries/Dynamic/lua-5.2_Win32_dll7_lib.zip/download" rel="nofollow noreferrer">http://sourceforge.net/projects/luabinaries/files/5.2/Windows%20Libraries/Dynamic/lua-5.2_Win32_dll7_lib.zip/download</a></p>\n\n<p>Once I did this, all my memory leaks were gone and the application was very stable.</p>\n'}]
['https://Stackoverflow.com/questions/260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']