aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/OpenSim.Region/Scenes/scripting/CSharpScriptEngine.cs
blob: d5622b8cd8ec0be87f384e9ee4d5d82ff29617e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Text;

// Compilation stuff
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace OpenSim.Scripting
{
    public class CSharpScriptEngine : IScriptCompiler
    {
        public string fileExt()
        {
            return ".cs";
        }

        private Dictionary<string,IScript> LoadDotNetScript(ICodeCompiler compiler, string filename)
        {
            CompilerParameters compilerParams = new CompilerParameters();
            CompilerResults compilerResults;
            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory = true;
            compilerParams.IncludeDebugInformation = false;
            compilerParams.ReferencedAssemblies.Add("OpenSim.Region.dll");
            compilerParams.ReferencedAssemblies.Add("OpenSim.Framework.dll");
            compilerParams.ReferencedAssemblies.Add("libsecondlife.dll");
            compilerParams.ReferencedAssemblies.Add("System.dll");

            compilerResults = compiler.CompileAssemblyFromFile(compilerParams, filename);

            if (compilerResults.Errors.Count > 0)
            {
                OpenSim.Framework.Console.MainLog.Instance.Error("Compile errors");
                foreach (CompilerError error in compilerResults.Errors)
                {
                    OpenSim.Framework.Console.MainLog.Instance.Error(error.Line.ToString() + ": " + error.ErrorText.ToString());
                }
            }
            else
            {
                Dictionary<string,IScript> scripts = new Dictionary<string,IScript>();

                foreach (Type pluginType in compilerResults.CompiledAssembly.GetExportedTypes())
                {
                    Type testInterface = pluginType.GetInterface("IScript", true);

                    if (testInterface != null)
                    {
                        IScript script = (IScript)compilerResults.CompiledAssembly.CreateInstance(pluginType.ToString());

                        string scriptName = "C#/" + script.getName();
                        Console.WriteLine("Script: " + scriptName + " loaded.");

                        if (!scripts.ContainsKey(scriptName))
                        {
                            scripts.Add(scriptName, script);
                        }
                        else
                        {
                            scripts[scriptName] = script;
                        }
                    }
                }
                return scripts;
            }
            return null;
        }

        public Dictionary<string,IScript> compile(string filename)
        {
            CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
            return LoadDotNetScript(csharpProvider.CreateCompiler(), filename);
        }
    }
}