private void AddAssemblyReference(IVsaEngine engine, string filename)
        {
            IVsaReferenceItem item;

            item = (IVsaReferenceItem)engine.Items.CreateItem(filename, VsaItemType.Reference, VsaItemFlag.None);
            item.AssemblyName = filename;
        }
Example #2
0
        /// <summary>
        /// Sets the script from a string
        /// </summary>
        /// <param name="scriptText">Script text</param>
        /// <param name="language">Script language</param>
        public void SetScript(string scriptText)
        {
            _isInitialized = false;
            // Make sure the last allocated engine is stopped.
            if (_engine != null)
            {
                _engine.Close();
            }

            _scriptText = scriptText;

            // Initialize globale vars
            _globalObs.Clear();

            _engine = new Microsoft.VisualBasic.Vsa.VsaEngine();

            // Initialize Engine
            _engine.RootMoniker = "nScriptHost://VSAScript/Instance" + NewInstanceID.ToString();
            _engine.Site        = this;
            _engine.InitNew();
            _engine.RootNamespace = "__Script__";

            _references.Clear();
            _globalObItems.Clear();

            AddDefaultReferences();

            // Set the script code.
            IVsaCodeItem item = _engine.Items.CreateItem("Code", VsaItemType.Code, VsaItemFlag.None) as IVsaCodeItem;

            item.SourceText = _scriptText;
        }
Example #3
0
		public void Dispose()
		{
			if (engine == null)
				return;
			engine.Close();
			engine = null;
		}
Example #4
0
File: jsc.cs Project: ydunk/masters
 private void AddSourceFile(IVsaEngine engine, string fileName, int codepage, bool fForceCodepage){
   // We don't use the filename as the item name because filenames are allowed to contain
   // things that identifiers aren't (like commas and periods).
   string itemName = "$SourceFile_" + this.codeItemCounter++;
   IVsaCodeItem item = (IVsaCodeItem)engine.Items.CreateItem(itemName, VsaItemType.Code, VsaItemFlag.None);
   item.SetOption("codebase", fileName);
   item.SourceText = this.ReadFile(fileName, codepage, fForceCodepage);
 }
Example #5
0
 public void ClearScript()
 {
     this.Ready = false;
     this._ClearCachedMethodPointers();
     if (this._engine != null)
     {
         try
         {
             this._engine.Close();
         }
         catch (Exception)
         {
         }
         this._engine = null;
     }
 }
 private void AddAssemblyReference(IVsaEngine engine, string filename){
   IVsaReferenceItem item;
   item = (IVsaReferenceItem)engine.Items.CreateItem(filename, VsaItemType.Reference, VsaItemFlag.None);
   item.AssemblyName = filename;
 }
Example #7
0
        private bool _LoadScript(string sScriptFilename)
        {
            if (!CONFIG.bLoadScript)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(sScriptFilename))
            {
                return(false);
            }
            if (!File.Exists(sScriptFilename))
            {
                return(false);
            }
            _scriptCount++;
            if (this._BeforeRulesCompile != null)
            {
                this._BeforeRulesCompile(sScriptFilename);
            }
            foreach (object obj2 in htMenuScripts.Keys)
            {
                ((MenuItem)obj2).Parent.MenuItems.RemoveAt(((MenuItem)obj2).Index);
            }
            htMenuScripts.Clear();
            foreach (PreferenceBag.PrefWatcher watcher in this.listWeaklyHeldWatchers)
            {
                FiddlerApplication.Prefs.RemoveWatcher(watcher);
            }
            this.listWeaklyHeldWatchers.Clear();
            StreamReader reader = new StreamReader(new FileStream(sScriptFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8, true);
            string       str    = reader.ReadToEnd();

            reader.Close();
            try
            {
                this._engine             = new VsaEngine();
                this._engine.RootMoniker = "fiddler://script/" + _scriptCount;
                this._engine.Site        = this.objVSASite;
                this._engine.InitNew();
                this._engine.RootNamespace     = "Fiddler.ScriptNamespace";
                this._engine.GenerateDebugInfo = false;
            }
            catch (EntryPointNotFoundException)
            {
                FiddlerApplication.DoNotifyUser("Unable to initialize FiddlerScript. This typically indicates that you are attempting to run Fiddler on .NET Framework v4.\n\nYour machine may have the unsupported OnlyUseLatestCLR registry key set.", "Unsupported configuration", MessageBoxIcon.Hand);
                this.Ready = false;
                return(false);
            }
            IVsaItems items = this._engine.Items;

            foreach (string str2 in CONFIG.sScriptReferences.Split(new char[] { ';' }))
            {
                if (str2.Trim().Length > 0)
                {
                    IVsaReferenceItem item = (IVsaReferenceItem)items.CreateItem(str2, VsaItemType.Reference, VsaItemFlag.None);
                    item.AssemblyName = str2;
                }
            }
            IVsaGlobalItem item2 = (IVsaGlobalItem)items.CreateItem("FiddlerObject", VsaItemType.AppGlobal, VsaItemFlag.None);

            item2.TypeString = "Fiddler.FiddlerScript";
            item2            = (IVsaGlobalItem)items.CreateItem("FiddlerScript", VsaItemType.AppGlobal, VsaItemFlag.None);
            item2.TypeString = "Fiddler.FiddlerScript";
            IVsaCodeItem item3 = (IVsaCodeItem)items.CreateItem("Handlers", VsaItemType.Code, VsaItemFlag.None);

            item3.SourceText = str;
            if (!this._engine.Compile())
            {
                this.Ready = false;
                return(false);
            }
            this._engine.Run();
            this.Ready = true;
            this._typeScriptHandlers = this._engine.Assembly.GetType("Fiddler.ScriptNamespace.Handlers");
            if (this._typeScriptHandlers == null)
            {
                this._ClearCachedMethodPointers();
            }
            else
            {
                this._ExtractMethodPointersFromScript();
                foreach (FieldInfo info in this._typeScriptHandlers.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    if (info.FieldType == typeof(bool))
                    {
                        RulesOption customAttribute = (RulesOption)Attribute.GetCustomAttribute(info, typeof(RulesOption));
                        if (customAttribute != null)
                        {
                            this.CreateRulesMenuItem(info, customAttribute);
                        }
                    }
                    else if (info.FieldType == typeof(string))
                    {
                        RulesString oRule = (RulesString)Attribute.GetCustomAttribute(info, typeof(RulesString));
                        if (oRule != null)
                        {
                            RulesStringValue[] customAttributes = (RulesStringValue[])Attribute.GetCustomAttributes(info, typeof(RulesStringValue));
                            if ((customAttributes != null) && (customAttributes.Length > 0))
                            {
                                Array.Sort <RulesStringValue>(customAttributes);
                                this.CreateRulesMenuForStrings(info, oRule, customAttributes);
                            }
                        }
                    }
                }
                bool flag = true;
                foreach (MethodInfo info2 in this._typeScriptHandlers.GetMethods())
                {
                    MenuItem    item4;
                    ToolsAction action = (ToolsAction)Attribute.GetCustomAttribute(info2, typeof(ToolsAction));
                    if (action != null)
                    {
                        item4 = new MenuItem(action.Name);
                        htMenuScripts.Add(item4, info2);
                        item4.Click += new EventHandler(this.HandleScriptToolsClick);
                        FiddlerApplication._frmMain.mnuTools.MenuItems.Add(item4);
                    }
                    ContextAction action2 = (ContextAction)Attribute.GetCustomAttribute(info2, typeof(ContextAction));
                    if (action2 != null)
                    {
                        if (flag)
                        {
                            item4 = new MenuItem("-");
                            htMenuScripts.Add(item4, null);
                            FiddlerApplication._frmMain.mnuSessionContext.MenuItems.Add(0, item4);
                            flag = false;
                        }
                        item4 = new MenuItem(action2.Name);
                        htMenuScripts.Add(item4, info2);
                        item4.Click += new EventHandler(this.HandleScriptToolsClick);
                        FiddlerApplication._frmMain.mnuSessionContext.MenuItems.Add(0, item4);
                    }
                    BindUIColumn column = (BindUIColumn)Attribute.GetCustomAttribute(info2, typeof(BindUIColumn));
                    if (column != null)
                    {
                        getColumnStringDelegate delFn = (getColumnStringDelegate)Delegate.CreateDelegate(typeof(getColumnStringDelegate), info2);
                        FiddlerApplication._frmMain.lvSessions.AddBoundColumn(column._colName, column._iColWidth, delFn);
                    }
                    QuickLinkMenu menu = (QuickLinkMenu)Attribute.GetCustomAttribute(info2, typeof(QuickLinkMenu));
                    if (menu != null)
                    {
                        QuickLinkItem[] array = (QuickLinkItem[])Attribute.GetCustomAttributes(info2, typeof(QuickLinkItem));
                        if ((array != null) && (array.Length > 0))
                        {
                            Array.Sort <QuickLinkItem>(array);
                            this.CreateQuickLinkMenu(menu.Name, info2, menu.Name, array);
                        }
                    }
                }
                MenuExt.ReadRegistry(FiddlerApplication._frmMain.mnuTools, htMenuScripts);
                try
                {
                    MethodInfo method = this._typeScriptHandlers.GetMethod("Main");
                    if (method != null)
                    {
                        method.Invoke(null, null);
                    }
                }
                catch (Exception exception)
                {
                    FiddlerApplication.DoNotifyUser(string.Concat(new object[] { "There was a problem with your FiddlerScript.\n\n", exception.Message, "\n", exception.StackTrace, "\n\n", exception.InnerException }), "JScript main() failed.");
                }
            }
            if (this._AfterRulesCompile != null)
            {
                this._AfterRulesCompile();
            }
            return(true);
        }
Example #8
0
 private void AddSourceFile(IVsaEngine engine, string fileName, int codepage){
   // We don't use the filename as the item name because filenames are allowed to contain
   // things that identifiers aren't (like commas and periods).
   string itemName = "$SourceFile_" + this.codeItemCounter++;
   IVsaCodeItem item = (IVsaCodeItem)engine.Items.CreateItem(itemName, VsaItemType.Code, VsaItemFlag.None);
   item.SetOption("codebase", fileName);
   item.SourceText = ReadFile(fileName, codepage);
 }
        private void GetFromEngine(IVsaEngine _engine)
        {
            // set the root moniker used by the engine (ex: "MyApp://MyVsaEngine/Instance#X")
            //rootMoniker = _engine.RootMoniker;

            /*
             * fails when engine is a jscript engine...
             * wtf?
             * it works when it's a vbscript engine?
             * */
            //if (_language == VsaScriptingLanguages.VBScript)
                // set the name used by the engine (ex: "MyVsaEngine")
              //  _engine.Name = name;

            // set the site used by the engine to ourself as we are the host and will need to communicate with the engine
            //_engine.Site = this;

            // readies the engine to add new code items
            //_engine.InitNew();

            // set the root namespace used by the engine
            //_engine.RootNamespace = rootNamespace;

            // set the debug flag used by the engine
            //_engine.GenerateDebugInfo = generateDebugInfo;
        }
 public void SetEngine(IVsaEngine _engine0)
 {
     _engine = _engine0;
 }
 public VsaScriptingHost(IVsaEngine _engine0)
 {
     // create a new hashtable for storing global items so that we can look them up as needed
     _globalItemLookupTable = new Hashtable();
     SetEngine( _engine0);
     // from an engine aligned to the language specified (currently only JScript and VBScript are supported)
     GetFromEngine(_engine0);
 }
 /// <summary>a
 /// Initializes a new instance of the VsaScriptingHost class
 /// </summary>
 /// <param name="language">The language used by this scripting host</param>
 /// <param name="name">The name of the engine used by the scripting host</param>
 /// <param name="rootMoniker">The root moniker of the engine used by the scripting host</param>
 /// <param name="rootNamespace">The root namespace of the engine used by the scripting host</param>
 /// <param name="generateDebugInfo">A flag that indicates whether the engine should generate debugging information</param>
 public VsaScriptingHost(VsaScriptingLanguages language, string name, string rootMoniker, string rootNamespace, bool generateDebugInfo)
 {
     // create a new hashtable for storing global items so that we can look them up as needed
     _globalItemLookupTable = new Hashtable();
     _language = language;
     // create an engine aligned to the language specified (currently only JScript and VBScript are supported)
     _engine = ((object)this.CreateLanguageSpecificEngine(language)) as IVsaEngine;
     _engine.RootMoniker = rootMoniker;
     SetEngine(_engine);
     SetUpEngine(name, rootMoniker, rootNamespace, generateDebugInfo);           
 }