Reloading Scripts

293 views
Skip to first unread message

tyhtuj

unread,
Feb 1, 2012, 6:54:25 PM2/1/12
to CS-Script
I have a map editor that I can edit cs-scripts in, and then run the
map. I want to be able to dynamically edit them and reload the
scripts, however, whenever I try, I get an error "Access to the path
'...\CSSCRIPT\Cache\-467734178\Player.txt.compiled' is denied." I
realize why this is, and I tried following the example in the "Samples
\Modifying script without restart", however, it doesn't seem to work
still.

Here is the code:


public IPlayer LoadPlayerScript(string Path)
{
string ClassName = WSU.Util.GetClassName(Path); //ignore
this

using (AsmHelper helper = new
AsmHelper(CSScript.Compile(Path), null, true))
{
return
helper.CreateAndAlignToInterface<IPlayer>(ClassName);//I have tried
using * as well
}


When I use this version of the function, and I try to use the IPlayer
interface, I get the error "AppDomainUnhandledException: The target
application domain has been unloaded." Any ideas? Thank you.



}

tyhtuj

unread,
Feb 1, 2012, 6:56:19 PM2/1/12
to CS-Script
I forgot to mention, the class in my script derives from
MarshalByRefObject.

Oleg Shilo

unread,
Feb 1, 2012, 11:13:55 PM2/1/12
to CS-Script
This happens because you are unloading your script assembly with
"using" statement.

All examples demonstrate how to load script, call some of its methods
and unload it. What you wand is a bit different.

From your code I understood that you want to load the script and keep
it loaded until some point in the future. If this is correct then you
need to delay unloading the script (currently you unloading it
immediately after the loading). Thus you need to remove "using"
statement, keep reference to the AsmHelper instance and call
helper.Dispose() just before loading the new (updated) script.

yuet...@gmail.com

unread,
Jan 31, 2013, 9:50:19 AM1/31/13
to cs-s...@googlegroups.com
Hi,
 
I try what you suggested, however when I called CreateAndAlignToInterface, first it throw an exception about script class is not mark as Serializable, so then I mark my script class as serializable and then it throw another exception about it cannot find the assembly Script.cs. Any idea?
 
Thanks

Oleg Shilo

unread,
Feb 1, 2013, 12:13:04 AM2/1/13
to cs-s...@googlegroups.com
A few point abut your problem:
 - Arguably the better way to make an object serializable to inherit it from MarshalByRefObject
 - The assembly Script.cs is most likely your host assembly (hard to tell without the actual code). You will need to pass the location of the host assembly to  the AlignToInterface().

However I strongly recommend that you have a look at the latest (1 hour ago) CS-Script release v3.5. It finally delivers "compiler as service" solution which allows script hosting without any need for the extra AppDomain or any serialization. It also allows reloading already loaded scripts in the same AppDomain without any memory leaks. 

You can find the details here: http://csscript.net/Help/evaluator.html

From now on CSScript.Evaluator ("compiler as service") is a recommended hosting approach.

Thus you may want to reconsider your hosting model and migrate to CSScript.Evaluator. Migration should be simple as the Evaluator API is consistent with the existing hosting API (CodeDOM).

Regards
Oleg Shilo

--
You received this message because you are subscribed to the Google Groups "CS-Script" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cs-script+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted
Message has been deleted

Woodstock

unread,
Feb 1, 2013, 9:46:10 PM2/1/13
to cs-s...@googlegroups.com, osh...@gmail.com
There is also another limitation (rather a quality) of the hosting with "compiler as service". Debugging of the script code is impossible. This is because of the very nature of the "compiler as service" engine, which does not save the script code to the file and in result there is no possibility for the debugger to map the IL instruction to the C# statement. 

I wonder for Evaluator.LoadFile("my_cs_file.cs") is it possible to also call csc.exe to generate only my_cs_file.pdb file if we are in debug mode? would it make debugging work? I guess evaluator is trying to improve on script reloading, and one of the main usage scenario is for debugging these scripts. So having a full debug support (at least for Evaluator.LoadFile) would be very nice. For non-debug mode, we can skip the generation of .pdb file (and call to csc.exe) all together.

Thanks

Oleg Shilo

unread,
Feb 1, 2013, 10:08:36 PM2/1/13
to cs-s...@googlegroups.com
There will be slight one off performance cost for the Evaluator initialization. 

Otherwise the compilation with Evaluator seems to be slightly faster then CodeDOM. The actual execution speed of the compiled script in the same-AppDomain scenario is identical for CodeDOM and Evaluator. 

>Would there be any performance overhead compared to the extra AppDomain?
With the Evaluator you will immediately have the performance benefit (not the overhead) when you are not using extra AppDomain. This benefit is actually present in both CodeDOM and Evaluator cases however with Evaluator you have it for free but with CodeDOM you have to have a memory leak.

Cheers,
Oleg

On Sat, Feb 2, 2013 at 1:21 PM, Woodstock <tse....@gmail.com> wrote:
That's very exciting, Oleg!

Would there be any performance overhead compared to the extra AppDomain? I assume it will be faster than serialization. Okay, let me read up on the link.

Thanks!

Woodstock

unread,
Mar 7, 2013, 8:36:25 PM3/7/13
to cs-s...@googlegroups.com, osh...@gmail.com
Can Evaluator use .pdb files? If so we can compile the script to .pdb and have Evaluator use it for debugging purposes? Sorry, obviously I don't know much about CodeDOM vs Evaluator.

Oleg Shilo

unread,
Mar 7, 2013, 8:42:18 PM3/7/13
to cs-s...@googlegroups.com
Unfortunately no. I tried this with no luck. I also tried to set the Mono compiled settings generate DBG symbols with no success.I think the problem is not the inclusion of the debug symbols or generation of the "map" file (pdb) but rather the fact that mono never creates/saves the source code file so the pdb cannot point to it.

l A few people on the Mono forum have actually asked how to debug the evaluated cod and unfortunately their posts are still not unanswered.
Reply all
Reply to author
Forward
0 new messages