aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim.Scripting/EmbeddedJVM/OpenSimJVM.cs
blob: b47bb503f10aecb6ff2ed4802a8d100ab3bdc6b7 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using OpenSim.Framework;
using OpenSim.Framework.Interfaces;
using OpenSim.Framework.Utilities;

namespace OpenSim.Scripting.EmbeddedJVM
{
    public class OpenSimJVM : IScriptEngine
    {
        private List<Thread> _threads = new List<Thread>();
        private BlockingQueue<CompileInfo> CompileScripts = new BlockingQueue<CompileInfo>();
        private MainMemory _mainMemory;
        private System.Threading.Thread compileThread;

        public OpenSimJVM()
        {

        }

        public bool Init(IScriptAPI api)
        {
            Console.WriteLine("Creating OpenSim JVM scripting engine");
            _mainMemory = new MainMemory();
            Thread.GlobalMemory = this._mainMemory;
            Thread.OpenSimScriptAPI = api;
            compileThread = new System.Threading.Thread(new ThreadStart(CompileScript));
            compileThread.IsBackground = true;
            compileThread.Start();
            return true;
        }

        public string GetName()
        {
            return "OpenSimJVM";
        }

        public void LoadScript(string script, string scriptName, uint entityID)
        {
            Console.WriteLine("OpenSimJVM - loading new script: " + scriptName);
            CompileInfo comp = new CompileInfo();
            comp.entityId = entityID;
            comp.script = script;
            comp.scriptName = scriptName;
            this.CompileScripts.Enqueue(comp);
        }

        public void CompileScript()
        {
            while (true)
            {
                CompileInfo comp = this.CompileScripts.Dequeue();
                string script = comp.script;
                string scriptName = comp.scriptName;
                uint entityID = comp.entityId;
                try
                {
                    //need to compile the script into a java class file

                    //first save it to a java source file
                    TextWriter tw = new StreamWriter(scriptName + ".java");
                    tw.WriteLine(script);
                    tw.Close();

                    //now compile
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("javac.exe", "*.java");
                   // psi.RedirectStandardOutput = true;
                    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    psi.UseShellExecute = false;

                    System.Diagnostics.Process javacomp;
                    javacomp = System.Diagnostics.Process.Start(psi);
                    javacomp.WaitForExit();

                    
                    //now load in class file
                    ClassRecord class1 = new ClassRecord();
                    class1.LoadClassFromFile(scriptName + ".class");
                    class1.PrintToConsole();
                    //Console.WriteLine();
                    this._mainMemory.MethodArea.Classes.Add(class1);
                    class1.AddMethodsToMemory(this._mainMemory.MethodArea);

                    Thread newThread = new Thread();
                    this._threads.Add(newThread);
                    newThread.EntityId = entityID;
                    newThread.currentClass = class1;

                    //now delete the created files
                    System.IO.File.Delete(scriptName + ".java");
                    System.IO.File.Delete(scriptName + ".class");
                    //this.OnFrame();
                }
                catch (Exception e)
                {
                    Console.WriteLine("exception");
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine(e.Message);
                }
            }
        }

        public void OnFrame()
        {
            for (int i = 0; i < this._threads.Count; i++)
            {
                if (!this._threads[i].running)
                {
                    this._threads[i].StartMethod("OnFrame");
                    bool run = true;
                    while (run)
                    {
                        run = this._threads[i].Excute();
                    }
                }
            }
        }

        private class CompileInfo
        {
            public string script;
            public string scriptName;
            public uint entityId;

            public CompileInfo()
            {

            }
        }
    }
}