Jump to content

  • Log In with Google      Sign In   
  • Create Account


[.net] Easy Scripting in .NET


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
73 replies to this topic

#1 Shinkage   Members   -  Reputation: 595

Like
0Likes
Like

Posted 16 August 2004 - 09:04 AM

For all those who haven't experimented with it, the Microsoft.CSharp namespace in the .NET BCL provides access to a fully functioning C# compiler. Using this it only takes a few simple steps to add powerful scripting support to any project. For those interested, I'll demonstrate the simplest way to get such a system up and running with only a few lines of code. First we'll want access to the namespaces where all this functionality resides.
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
Next up we need to create the objects that will actually do the work of compiling our scripts for us.
CSharpCodeProvider	codeProvider = new CSharpCodeProvider();
ICodeCompiler		codeCompiler = codeProvider->CreateCompiler();
With that ready, the compilation parameters need to be set up so the compile knows exactly what we want made. Most of these options are pretty straightforward and obvious. Pay close attention to the ReferencedAssemblies property of the compiler parameters however. This is where we tell the compiler which assemblies we want our script to have access to. For example, if the script is going to be doing any works with forms, it'll need to reference "System.Windows.Forms.dll."
CompilerParameters	params = new CompilerParameters();
params.GenerateExecutable = false;
params.GenerateInMemory = true;
params.IncludeDebugInformation = false;
params.TreatWarningsAsErrors = false;
params.ReferencedAssemblies.Add( "System.dll" );
params.ReferencedAssemblies.Add( "System.Windows.Forms.dll" );
CompilerResults		results = codeCompiler.CompileAssemblyFromFile( params, "MyScript.cs" );
At this point if the compiler met with any errors they can be found in the results.Errors member. Otherwise, we now have a new assembly loaded into memory and ready for use. Using the System.Reflection namespace, any types or methods declared in the source file can be discovered and used. Let's assume that the source file looks like the following:
// MyScript.cs

using System;
using System.Windows.Forms;

class Script
{
	public static void Main()
	{
		MessageBox.Show( "This is a script!" );
	}
}
Given that source after compilation by the preceding steps, the following will be sufficient to call the static Main method.
results.CompiledAssembly.GetType( "Script").GetMethod( "Main" ).Invoke( null, null );
Of course that only covers the basics, but there you have it.

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 3951

Like
0Likes
Like

Posted 16 August 2004 - 09:20 AM

An excellent example.

For those who are going to implement such a setup however, I would sugest using another AppDomain and giving it restricted security settings, so that it's harder for malicious scripts to be used.

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 17 August 2004 - 02:04 AM

Great.

So how do we do that? ;)

#4 strahan   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 August 2004 - 02:47 AM

I heard that managed DirectX also supports scripting.

Which would be better to use for a game?

Thanks.

#5 VizOne   Members   -  Reputation: 590

Like
0Likes
Like

Posted 17 August 2004 - 04:17 AM

The managed DX scripting example uses exactly this technique.


Here a code snippet how to setup a "sandbox"-AppDomain

public class ScriptRunner : MarshalByRefObject // make class remotable
{
ICodeCompiler compiler;
CompilerParameters param;

public static ScriptRunner CreateInSeparateDomain()
{
AppDomain dom = AppDomain.CreateDomain( "sandbox" );
return dom.CreateInstanceAndUnwrap( typeof( ScriptRunner ).Assembly.GetName().Name,
typeof( ScriptRunner ).FullName ) as ScriptRunner;
}

public ScriptRunner()
{
string parameters = "";
// C# compiler
compiler = new CSharpCodeProvider().CreateCompiler();
// parameters = "/debug+"; // uncomment for debugging

// uncomment for JScript.NET
//compiler = new JScriptCodeProvider().CreateCompiler();
//parameters ="/debug+ /versionsafe+ ";

param = new CompilerParameters();
param.CompilerOptions = parameters;
param.GenerateExecutable = false;
param.GenerateInMemory = true;
param.IncludeDebugInformation = true;
param.ReferencedAssemblies.Add( "Scripting.dll" ); // whatever you need here

// set policy
PolicyLevel level = PolicyLevel.CreateAppDomainLevel();
PermissionSet permissions = new PermissionSet( PermissionState.None );
// uncomment all permissions you need
// (never allow "Assertion"...)
// which flags are required minimally also depends
// on .NET runtime Version
SecurityPermissionFlag permissionFlags =
// SecurityPermissionFlag.Assertion |
// SecurityPermissionFlag.BindingRedirects |
// SecurityPermissionFlag.ControlAppDomain |
// SecurityPermissionFlag.ControlDomainPolicy |
// SecurityPermissionFlag.ControlEvidence |
// SecurityPermissionFlag.ControlPolicy |
// SecurityPermissionFlag.ControlThread |
// SecurityPermissionFlag.ControlPrincipal |
// SecurityPermissionFlag.Infrastructure |
// SecurityPermissionFlag.RemotingConfiguration |
// SecurityPermissionFlag.SerializationFormatter |
// SecurityPermissionFlag.Infrastructure |
SecurityPermissionFlag.SkipVerification |
SecurityPermissionFlag.UnmanagedCode|
SecurityPermissionFlag.Execution;

permissions.AddPermission( new SecurityPermission( permissionFlags ) );

// allow reflection
permissions.AddPermission( new ReflectionPermission( ReflectionPermissionFlag.AllFlags ) );

PolicyStatement policy = new PolicyStatement( permissions, PolicyStatementAttribute.Exclusive );
CodeGroup group = new UnionCodeGroup( new AllMembershipCondition(), policy );
level.RootCodeGroup = group;
AppDomain.CurrentDomain.SetAppDomainPolicy( level );
}
}

// add code for compiling and running scripts
}





Regards
Andre

#6 evolutional   Moderators   -  Reputation: 905

Like
0Likes
Like

Posted 17 August 2004 - 04:27 AM

Very cool post. I've been wanting to look into C# scripting for a while but didn't know where to start. Rating++ for you, sir.

#7 VizOne   Members   -  Reputation: 590

Like
0Likes
Like

Posted 17 August 2004 - 10:18 PM

A note: it is important to create the compiler in the sandbox domain. Else the assembly created would automatically bleed into the main domain. This also means that you must set the permissions AFTER the compiler was created, as the compiler requires some special permissions.

Regards,
Andre

#8 machine_gun_man   Members   -  Reputation: 127

Like
0Likes
Like

Posted 19 August 2004 - 05:46 AM

this is interesting...

I'm just wondering tho...

how might you use this within an html browser?


<html>
<head>
<title>c#</title>
</head>
<body>
<script language="c#">
// MyScript.cs

using System;
using System.Windows.Forms;

class Script
{
public static void Main()
{
MessageBox.Show( "This is a script!" );
}
}
</script>
</body>
</html>




or


<html>
<head>
<title>c#</title>
</head>
<body>
<script language="c#" src="MyScript.cs">
</script>
</body>
</html>



I find this cool as a matter of fact...
I might like to create an enhanced version of vbscript using visual basic .net that easily implements directx coponents.

of course you could achieve directx by using com in a script refering to directAnimation, but that part of directx is dead and why use the simple scripting languages when you can be proud of making your own? Who doesn't want to feel accomplished?

#9 machine_gun_man   Members   -  Reputation: 127

Like
0Likes
Like

Posted 19 August 2004 - 06:08 AM

wait a sec...

your not talking a bout the scripting I was thinking of...

dang... that c# code is not meant for the "script" box I guess...

you could of course go make .net apps by means of asp pages, but it adds some overhead if youre trying to create one from scratch

ie: without visual studio.net

any possible way to have c# go into the script tag with simplicity... aka the standard html+script approach.

thanx...

#10 Influenza   Members   -  Reputation: 162

Like
0Likes
Like

Posted 20 August 2004 - 11:15 AM

C# scripts in web pages are parsed by the web server and execute through ASP.NET before the web page data is sent to the user. They don't act the same as javascript blocks, which are interpreted and run on the user's web client. So no, you can't use C# inside a web page without using ASP.NET.

This is a very good introductory walktrough. I'd read about the CSharp namespace, but never looked into using it. I'd better look again!

#11 Etnu   GDNet+   -  Reputation: 880

Like
0Likes
Like

Posted 22 August 2004 - 07:05 AM

Yeah, the sample that comes with the DX9c SDK shows how to use C# scripts within native C++ code by using an intermediate managed C++ layer. It works amazingly well.

#12 deathtrap   Members   -  Reputation: 364

Like
0Likes
Like

Posted 23 August 2004 - 12:46 PM

So... exactly what are the uses of this? All i see is that you're compiling source code inside your source code.

#13 JesseT   Banned   -  Reputation: 402

Like
0Likes
Like

Posted 15 December 2004 - 01:27 PM

Quote:
Original post by deathtrap
So... exactly what are the uses of this? All i see is that you're compiling source code inside your source code.


LOL

#14 joew   Crossbones+   -  Reputation: 3162

Like
0Likes
Like

Posted 16 December 2004 - 08:45 AM

Quote:
Original post by deathtrap
So... exactly what are the uses of this? All i see is that you're compiling source code inside your source code.


ROFLMAO! :)

#15 Nypyren   Crossbones+   -  Reputation: 3412

Like
0Likes
Like

Posted 16 December 2004 - 08:53 AM

It's good to note that you can use a string in memory rather than a file on disk for your compile source.

FWIW - I'm using this type of scripting in a binary file editor. I allow the "scripts" to use the public classes/members of my own editor .exe by adding my .exe itself to the reference list.

I use the scripts mainly for loading/saving different file formats, but I'm thinking about letting them do some more advanced stuff adding on to the user interface.

#16 dug   Members   -  Reputation: 160

Like
0Likes
Like

Posted 18 December 2004 - 12:42 PM

Here is how to dynamically compile a boo script ( http://boo.codehaus.org/ ), you can sandbox the resulting assembly before running it too if need be.


// (compile with references to Boo.dll and Boo.Lang.Compiler.dll)
using System;
using System.IO;
using System.Threading;
using System.Reflection;
using Boo.Lang.Compiler;
using Boo.Lang.Compiler.IO;
using Boo.Lang.Compiler.Pipelines;


namespace BooRunner
{
class App
{
[STAThread]
static int Main(string[] args)
{
string booscript = "print \"hello\"";

return RunScript(booscript);
}

static int RunScript(string script)
{
BooCompiler compiler = new BooCompiler();
compiler.Parameters.Input.Add(new StringInput("<stdin>", script));
//or to run files:
//compiler.Parameters.Input.Add(new FileInput(Path.GetFullPath(filepath)));

compiler.Parameters.Pipeline = new CompileToMemory();

//or to compile to a dll:
//compiler.Parameters.Pipeline = new CompileToFile();
//compiler.Parameters.OutputAssembly = "Script.dll";

CompilerContext result = compiler.Run();

if (result.Errors.Count > 0)
{
foreach (CompilerError error in result.Errors)
{
Console.WriteLine(error.ToString(true));
}
return -1;
}
try
{
result.GeneratedAssemblyEntryPoint.Invoke(null, new object[1]);
}
catch (TargetInvocationException x)
{
Console.WriteLine(x.InnerException.ToString());
return -1;
}
return 0;

}
}
}



#17 posti   Members   -  Reputation: 122

Like
0Likes
Like

Posted 01 May 2005 - 11:23 AM

You can also embed mono in your project and use C# scripting via mono. Making your code little more platform independant.

http://www.mono-project.com/Embedding_Mono
http://www.go-mono.com/embedded-api.html

Probably possible to run mono on game consoles too. So if you plan on porting your game to consoles or other os than windows you should use it.


#18 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 December 2005 - 02:21 PM

Quote:
Original post by VizOne
The managed DX scripting example uses exactly this technique.


Here a code snippet how to setup a "sandbox"-AppDomain
*** Source Snippet Removed ***

Regards
Andre



Hello Andre,
Im using VB.NET 2005. Ive been unable to get the sandboxed version to work, although I have no problems with compiling and invoking scripts from the current appdomain.

With the sandboxed version, the call to myprovider.CompileAssemblyFromSource throws an exception that it cannot find the temporary binary file it's created. I even tried specifying an output file and i see it being created but it still complains with filenotfoundexception.

I'm thinking maybe it has something to do with how i'm trying to load the assembly. I'll keep trying, but if you have any more tips, it'd be much appreciated.
-Hypnotron


#19 Rob Loach   Moderators   -  Reputation: 1500

Like
0Likes
Like

Posted 05 December 2005 - 04:11 PM

Washu wrote up an excellent article on Code Access Permissions which I suggest you should most definately read.

Also, a nice tutorial was just recently published here on Gamedev: Using Lua with C#. It goes over using the LuaInterface in .NET. Lua should be mentioned as it's been in the game industry for years.

I'd also like to make a self plug of my GakScript Scripting Engine which nicely wraps the CodeDom and adds multiple language plugin interfaces. Very easy to use and comes with a few simple examples of its use.

[Edited by - Rob Loach on June 4, 2006 5:11:48 PM]

#20 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 December 2005 - 04:55 PM

Quote:
Original post by Rob Loach
Washu wrote up an excellent article on Code Access Permissions which I suggest you should most definately read.

Also, a nice tutorial was just recently published here on Gamedev: Using Lua with C#. It goes over using the LuaInterface in .NET. Lua should be mentioned as it's been in the game industry for years.

I'd also like to make a self plug of my GakScript Scripting Engine which nicely wraps the CodeDom and adds multiple language plugin interfaces. Very easy to use and comes with a few simple examples of its use.


Hello Rob. Thanks for the reply.

I understand the importance of code security, but with regards to loading scripts within the same appDomain (as I believe the article you link to suggests) it doesnt deal with the issue of script bloat. That is, you cannot unload assemblies from an AppDomain. However,you _can_ unload entire AppDomains so if you load scripts in an AppDomain you can free memory by unloading that domain.

Am I off base here?
-Hypnotron




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS