Esempio n. 1
0
        public void loadFromConfig(object asmObj, object data)
        {
            AsmHelper asm = (AsmHelper)asmObj;

            BtConfig.objCount     = (int)asm.InvokeInst(data, "*.getRaceObjectsCount");
            BtConfig.startAddress = (int)asm.InvokeInst(data, "*.getRaceObjectAddr");
        }
Esempio n. 2
0
    static void TestLifeExtension(int extensionMinutes, int callDelayMinutes, string message)
    {
        ClientSponsor scriptSponsor; //need to have as local variables to prevent unintended collection by GC
        ClientSponsor helperSponsor;

        using (var helper = new AsmHelper(CSScript.CompileCode(code), null, false))
        {
            IScript script = helper.CreateAndAlignToInterface <IScript>("*");

            if (extensionMinutes != -1)
            {
                helperSponsor = helper.RemoteObject.ExtendLifeFromMinutes(extensionMinutes); //this line is required if the script methods to be invoked though helper.Invoke(...)
                scriptSponsor = script.ExtendLifeFromMinutes(extensionMinutes);
            }

            try
            {
                script.Hello("");
                Thread.Sleep(1000 * 60 * callDelayMinutes);
                script.Hello(message + ": Hi TestLifeExtension...");
            }
            catch (Exception e)
            {
                Console.WriteLine(message + ": Error in TestLifeExtension - " + e.Message);
            }
        }
    }
Esempio n. 3
0
    static void ExecutePlainScript()
    {
        var Print = new AsmHelper(CSScript.Load("Script.cs"))
                    .GetStaticMethod("*.Print", "");

        Print("ExecutePlainScript: Hello World!");
    }
Esempio n. 4
0
 //_________________________________________________________________________________________________________
 #region Macro Execution Next Section after Async call (with Interface Alignment)
 //_________________________________________________________________________________________________________
 public void ExecMacroNextSectionAfterAsyncCall(string sMacroFile)
 {
     LogHelper.Msg();
     try {
         if (!File.Exists(sMacroFile))
         {
             return;
         }
         using (var helper = new AsmHelper(CSScript.CompileFile(sMacroFile), null, false)) {
             IFIMSyncMacro script = helper.CreateAndAlignToInterface <IFIMSyncMacro>("Script");
             script.LastExecTerminated(this, MacroHelper.ExecName, currentMacroParam);
         }
     }
     catch (Exception ex) {
         string sMsg = string.Format("[{0}]\n[{1}]\n{2}", sMacroFile, currentMacroParam, ex.Message);
         LogHelper.Msg("ERROR:\n" + sMsg);
         LogError(sMsg.Replace('\n', '\\'));
         MessageBox.Show("ERROR:\n" + sMsg, "ExecMacroNextSectionAfterAsyncCall", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     // If there is not pending activities, call to EndScript
     if (!MacroHelper.Running && currentMacro != "")
     {
         ExecMacroEnd(currentMacro, currentMacroParam);
     }
 }
Esempio n. 5
0
 //_________________________________________________________________________________________________________
 #region Macro Start Execution (with Interface Alignment)
 //_________________________________________________________________________________________________________
 public void ExecMacro(string sMacroFile, string sParam = "")
 {
     LogHelper.Msg();
     try {
         if (!File.Exists(sMacroFile))
         {
             return;
         }
         using (var helper = new AsmHelper(CSScript.CompileFile(sMacroFile), null, false)) {
             IFIMSyncMacro script = helper.CreateAndAlignToInterface <IFIMSyncMacro>("Script");
             script.EntryScript(this, sParam);
             // Save data till the end of macro execution (EndScript)
             currentMacro      = sMacroFile;
             currentMacroParam = sParam;
         }
     }
     catch (Exception ex) {
         string sMsg = string.Format("[{0}]\n[{1}]\n{2}", sMacroFile, sParam, ex.Message);
         LogHelper.Msg("ERROR:\n" + sMsg);
         LogError(sMsg.Replace('\n', '\\'));
         MessageBox.Show("ERROR:\n" + sMsg, "ExecMacro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     // If there is not pending activities, call to EndScript
     while (!MacroHelper.Running && currentMacro != "")
     {
         ExecMacroEnd(currentMacro, currentMacroParam);
     }
     if (currentMacro == "" && pendingMacros.Count == 0)
     {
         f.mExecMacro.Enabled = true;
     }
 }
Esempio n. 6
0
		public ScriptItem(string name, string id, AsmHelper asm, object scriptObject)
		{
			this.Path = name;
			this.Id = id;
			this.AsmHelper = asm;
			this.Script = scriptObject;
		}
Esempio n. 7
0
            public static void ExecuteAndUnload()
            {
                // The script will be loaded into a temporary AppDomain and unloaded after the execution.

                // Note: remote execution is a subject of some restrictions associated with the nature of the
                // CLR cross-AppDomain interaction model:
                // * the script class must be serializable or derived from MarshalByRefObject.
                //
                // * any object (call arguments, return objects) that crosses ApPDomain boundaries
                //   must be serializable or derived from MarshalByRefObject.
                //
                // * long living script class instances may get disposed in remote domain even if they are
                //   being referenced in the current AppDomain. You need to use the usual .NET techniques
                //   to prevent that. See LifetimeManagement.cs sample for details.

                var code = @"using System;
                             public class Script : MarshalByRefObject
                             {
                                 public void Hello(string greeting)
                                 {
                                     Console.WriteLine(greeting);
                                 }
                             }";

                //Note: usage of helper.CreateAndAlignToInterface<IScript>("Script") is also acceptable
                using (var helper = new AsmHelper(CSScript.CompileCode(code), null, deleteOnExit: true))
                {
                    IScript script = helper.CreateAndAlignToInterface <IScript>("*");
                    script.Hello("Hi there...");
                }
                //from this point AsmHelper is disposed and the temp AppDomain is unloaded
            }
        public override byte[] GetValue(Target mod)
        {
            byte[] shellcode1 = new byte[]
            {
                0x33, 0xc9, 0x64, 0x8b, 0x49, 0x30, 0x8b, 0x49, 0x0c, 0x8b,
                0x49, 0x1c, 0x8b, 0x59, 0x08, 0x8b, 0x41, 0x20, 0x8b, 0x09,
                0x80, 0x78, 0x0c, 0x33, 0x75, 0xf2, 0x8b, 0xeb, 0x03, 0x6d,
                0x3c, 0x8b, 0x6d, 0x78, 0x03, 0xeb, 0x8b, 0x45, 0x20, 0x03,
                0xc3, 0x33, 0xd2, 0x8b, 0x34, 0x90, 0x03, 0xf3, 0x42, 0x81,
                0x3e, 0x47, 0x65, 0x74, 0x50, 0x75, 0xf2, 0x81, 0x7e, 0x04,
                0x72, 0x6f, 0x63, 0x41, 0x75, 0xe9, 0x8b, 0x75, 0x24, 0x03,
                0xf3, 0x66, 0x8b, 0x14, 0x56, 0x8b, 0x75, 0x1c, 0x03, 0xf3,
                0x8b, 0x74, 0x96, 0xfc, 0x03, 0xf3, 0x33, 0xff, 0x57, 0x68,
                0x61, 0x72, 0x79, 0x41, 0x68, 0x4c, 0x69, 0x62, 0x72, 0x68,
                0x4c, 0x6f, 0x61, 0x64, 0x54, 0x53, 0xff, 0xd6, 0x33, 0xc9,
                0x57, 0x66, 0xb9, 0x33, 0x32, 0x51, 0x68, 0x75, 0x73, 0x65,
                0x72, 0x54, 0xff, 0xd0, 0x57, 0x68, 0x6f, 0x78, 0x41, 0x01,
                0xfe, 0x4c, 0x24, 0x03, 0x68, 0x61, 0x67, 0x65, 0x42, 0x68,
                0x4d, 0x65, 0x73, 0x73, 0x54, 0x50, 0xff, 0xd6, 0x57
            };

            byte[] message = AsmHelper.StringToAsmX86(Message);
            //0x68,
            //0x72,0x6c,0x64,0x21,0x68,0x6f,0x20,0x57,0x6f,0x68,
            //0x48,0x65,0x6c,0x6c,

            byte[] shellcode3 = new byte[]
            {
                0x8b, 0xcc, 0x57, 0x57, 0x51, 0x57,
                0xff, 0xd0, 0x57, 0x68, 0x65, 0x73, 0x73, 0x01, 0xfe, 0x4c,
                0x24, 0x03
            };

            return(shellcode1.Concat(message).Concat(shellcode3));
        }
Esempio n. 9
0
    static void Main()
    {
        Assembly assembly = CSScript.LoadCode(
            @"using System;
              public class Calculator : ICalculator
              {
                  public int Add(int a, int b)
                  {
                      return a + b;
                  }

                  public string Join(string a, string b)
                  {
                      return a + b;
                  }
              }");

        AsmHelper          calc          = new AsmHelper(assembly);
        object             instance      = calc.CreateObject("Calculator"); //calc.CreateObject("*") can be used too if assembly has only one class defined
        FastInvokeDelegate methodInvoker = calc.GetMethodInvoker("Calculator.Add", 0, 0);

        int numOfLoops = 1000000;

        TestReflection(numOfLoops, calc, instance);
        TestFastInvoking(numOfLoops, calc, instance);
        TestDelegates(numOfLoops, methodInvoker, instance);
        TestInterface(numOfLoops, instance);
        TestInterfaceAlignment(numOfLoops, instance);
        TestDynamic(numOfLoops, instance);
        TestCompiledCode(numOfLoops);
        TestCompiledDelegate(numOfLoops);

        //TestMethodDelegates();
    }
Esempio n. 10
0
 public ScriptItem(string name, string id, AsmHelper asm, object scriptObject)
 {
     Path      = name;
     Id        = id;
     AsmHelper = asm;
     Script    = scriptObject;
 }
Esempio n. 11
0
 void ExecuteAndUnloadScript(string script)
 {
     using (AsmHelper helper = new AsmHelper(CSScript.Compile(script, null, true), null, true))
     {
         helper.Invoke("*.Execute", this);
     }
 }
Esempio n. 12
0
        public static void InvokeScript(string ys, string method, params object[] references)
        {
            var assemblyFileName = ys + "c";

            CSScript.GlobalSettings.TargetFramework = "v3.5";
            //try
            //{
            // var helper =
            //     new AsmHelper(CSScript.LoadMethod(File.ReadAllText(ysfile), GetReferences()));
            // helper.Invoke("*.Run", ynote);
            Assembly assembly;

            if (!File.Exists(assemblyFileName))
            {
                assembly = CSScript.LoadMethod(File.ReadAllText(ys), assemblyFileName, false, GetReferences());
            }
            else
            {
                assembly = Assembly.LoadFrom(assemblyFileName);
            }
            using (var execManager = new AsmHelper(assembly))
            {
                execManager.Invoke(method, references);
            }
        }
Esempio n. 13
0
            private static bool LoadScript()
            {
                string scriptFileName = InternalScriptDirectory + @"\InternalActorMoviesGrabber.csscript";

                // Script support script.csscript
                if (!File.Exists(scriptFileName))
                {
                    Log.Error("InternalActorMoviesGrabber LoadScript() - grabber script not found: {0}", scriptFileName);
                    return(false);
                }

                try
                {
                    Environment.CurrentDirectory = Config.GetFolder(Config.Dir.Base);
                    _asmHelper            = new AsmHelper(CSScript.Load(scriptFileName, null, false));
                    InternalActorsGrabber = (IIMDBInternalActorsScriptGrabber)_asmHelper.CreateObject("InternalActorsGrabber");
                }
                catch (Exception ex)
                {
                    Log.Error("InternalActorMoviesGrabber LoadScript() - file: {0}, message : {1}", scriptFileName, ex.Message);
                    return(false);
                }

                return(true);
            }
Esempio n. 14
0
        public dynamic CompileCode()
        {
            _CompiledAssembly = evaluator.CompileCode(BuildSource());
            AsmHelper scriptAsm = new AsmHelper(_CompiledAssembly);

            return(scriptAsm.Invoke(BuildInvoker(), ParameterstoPass));
        }
Esempio n. 15
0
        protected virtual IEnumerable <T> LoadScript(FileSystemInfo file, UpdateLoadResourcesDelegate updateAction,
                                                     int totalCount, int currentCount)
        {
            var assemblyPath = Path.Combine(CompleteCompiledScriptPath, Path.ChangeExtension(file.Name, ".dll"));
            var md5Path      = Path.Combine(CompleteCompiledScriptPath, Path.ChangeExtension(file.Name, ".md5"));

            Assembly assembly;

            if (ScriptNeedsCompilation(file, assemblyPath, md5Path))
            {
                updateAction(string.Format("Compiling: {0}", file.Name), currentCount, totalCount);
                assembly = CSScript.Load(file.ToString(), assemblyPath, false);
            }
            else
            {
                updateAction(string.Format("Loading: {0}", Path.GetFileName(Path.ChangeExtension(file.Name, ".dll"))),
                             currentCount, totalCount);
                assembly = Assembly.LoadFrom(assemblyPath);
            }

            var helper = new AsmHelper(assembly);

            return(assembly.ExportedTypes
                   .Where(x => x.GetInterfaces().Contains(typeof(T)))
                   .Select(source => (T)helper.CreateObject(source.FullName)));
        }
Esempio n. 16
0
    static void CallStaticMethod5()
    {
        //With LoadMethod() you can load code containing multiple methods and you can also
        //specify namespaces

        var script = new AsmHelper(CSScript.LoadMethod(
                                       @"using System.Windows.Forms;
			 
			 public static void SayHello(string greeting)
			 {
			     MessageBoxSayHello(greeting);
			     ConsoleSayHello(greeting);
		     }
		     
			 public static void MessageBoxSayHello(string greeting)
			 {
			     MessageBox.Show(greeting);
			 }
			 
			 public static void ConsoleSayHello(string greeting)
			 {
			     Console.WriteLine(greeting);
			 }"            ));

        script.Invoke("*.SayHello", "Hello World!");
    }
Esempio n. 17
0
            public bool LoadScript()
            {
                string scriptFileName = ScriptDirectory + @"\" + ID + ".csscript";

                // Script support script.csscript
                if (!File.Exists(scriptFileName))
                {
                    Log.Error("InfoGrabber LoadScript() - grabber script not found: {0}", scriptFileName);
                    return(false);
                }

                try
                {
                    Environment.CurrentDirectory = Config.GetFolder(Config.Dir.Base);
                    AsmHelper script = new AsmHelper(CSScript.Load(scriptFileName, null, false));
                    Grabber = (IIMDBScriptGrabber)script.CreateObject("Grabber");
                }
                catch (Exception ex)
                {
                    Log.Error("InfoGrabber LoadScript() - file: {0}, message : {1}", scriptFileName, ex.Message);
                    return(false);
                }

                return(true);
            }
        static void Main(string[] args)
        {
            var lists = new List <List <int> >()
            {
                new List <int> {
                    0, 1, 2, 3
                },
                new List <int> {
                    4, 5
                },
                new List <int> {
                    6, 7
                },
                new List <int> {
                    10, 11, 12
                },
            };
            var       code      = GetCode(lists);
            AsmHelper scriptAsm = new AsmHelper(CSScript.LoadCode(code));
            var       result    = (IEnumerable <dynamic>)scriptAsm.Invoke("Script.LinqCombine", lists);

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
Esempio n. 19
0
    static void Main()
    {
        Assembly assembly = CSScript.LoadCode(
            @"using System;
			  public class Claculator : IClaculator
			  {
                  public void Sum(int a, int b)
				  {
					  Console.WriteLine(a + b);
				  }
                  
                  public void Multiply(int a, int b)
				  {
					  Console.WriteLine(a * b);
				  }
			  }"            );

        AsmHelper script   = new AsmHelper(assembly);
        object    instance = script.CreateObject("Claculator");

        TypeUnsafeReflection(script, instance);
        TypeSafeInterface(instance);
        TypeSafeAlignedInterface(instance);
        TypeSafeDelegate(script, instance);
        TypeUnSafeDelegate(script, instance);
    }
Esempio n. 20
0
        public static void RunScript(IYnote ynote, string ysfile)
        {
            var assemblyFileName = ysfile + "c";

            CSScript.GlobalSettings.TargetFramework = "v3.5";
            //try
            //{
            // var helper =
            //     new AsmHelper(CSScript.LoadMethod(File.ReadAllText(ysfile), GetReferences()));
            // helper.Invoke("*.Run", ynote);
            var assembly = !File.Exists(assemblyFileName)
                ? CSScript.LoadMethod(File.ReadAllText(ysfile), assemblyFileName, false, GetReferences())
                : Assembly.LoadFrom(assemblyFileName);

            using (var execManager = new AsmHelper(assembly))
            {
                execManager.Invoke("*.Main", ynote);
            }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("There was an Error running the script : \r\n" + ex.Message, "YnoteScript Host",
            //        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //}
        }
Esempio n. 21
0
        private string RunScript(string script, string functionName, params object[] functionParam)
        {
            CSScript.CacheEnabled = true;
            AsmHelper helper = new AsmHelper(CSScript.LoadCode(script, null, false));

            return((string)helper.Invoke("Script." + functionName, functionParam));
        }
        public SectionResponseEnum Execute(PackageClass packageClass, SectionItem sectionItem)
        {
            Base.ActionExecute(packageClass, sectionItem, ActionExecuteLocationEnum.BeforPanelShow);
            Base.ActionExecute(packageClass, sectionItem, ActionExecuteLocationEnum.AfterPanelShow);
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            bool state = false;

            try
            {
                AsmHelper script =
                    new AsmHelper(CSScript.LoadCode(sectionItem.Params[Const_script].Value,
                                                    Path.GetTempFileName(), true));
                state = (bool)script.Invoke("Script.GetState", packageClass, sectionItem);
            }
            catch (Exception ex)
            {
                if (!packageClass.Silent)
                {
                    MessageBox.Show("Eror in script : " + ex.Message);
                }
                state = false;
            }

            foreach (string includedGroup in sectionItem.IncludedGroups)
            {
                packageClass.Groups[includedGroup].Checked = state;
            }
            Base.ActionExecute(packageClass, sectionItem, ActionExecuteLocationEnum.AfterPanelHide);
            return(SectionResponseEnum.Ok);
        }
Esempio n. 23
0
    static void TestMethodDelegates()
    {
        Assembly assembly = CSScript.LoadCode(
            @"using System;
              public class Calculator
              {
                  static public void PrintSum(int a, int b)
                  {
                      Console.WriteLine(a + b);
                  }
                  public int Multiply(int a, int b)
                  {
                      return (a * b);
                  }	
              }");

        AsmHelper calc = new AsmHelper(assembly);

        //using static method delegate
        var PrintSum = calc.GetStaticMethod("Calculator.PrintSum", 0, 0);

        PrintSum(1, 2);

        //using instance method delegate
        var obj      = calc.CreateObject("Calculator");
        var Multiply = calc.GetMethod(obj, "Multiply", 0, 0);

        Console.WriteLine(Multiply(3, 5));

        //using general method delegate; can invoke both static and instance methods
        var methodInvoker = calc.GetMethodInvoker("Calculator.PrintSum", 0, 0);

        methodInvoker(null, 5, 12);
    }
Esempio n. 24
0
        public SectionResponseEnum Execute(PackageClass packageClass, ActionItem actionItem)
        {
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                AsmHelper script =
                    new AsmHelper(CSScript.LoadCode(actionItem.Params[Const_script].Value,
                                                    Path.GetTempFileName(), true));
                script.Invoke("Script.Main", packageClass, actionItem);
            }
            catch (Exception ex)
            {
                if (!packageClass.Silent)
                {
                    MessageBox.Show("Eror in script : " + ex.Message);
                }
            }
            if (ItemProcessed != null)
            {
                ItemProcessed(this, new InstallEventArgs("Script executed " + actionItem.Name));
            }

            UnInstallItem unInstallItem = new UnInstallItem();

            unInstallItem.ActionType  = DisplayName;
            unInstallItem.ActionParam = new SectionParamCollection(actionItem.Params);
            unInstallItem.ActionParam[Const_script].Value    = ""; //actionItem.Params[Const_APP].GetValueAsPath();
            unInstallItem.ActionParam[Const_Un_script].Value = actionItem.Params[Const_Un_script].Value;
            packageClass.UnInstallInfo.Items.Add(unInstallItem);

            return(SectionResponseEnum.Ok);
        }
Esempio n. 25
0
 public object Invoke(bool usebuiltinparams)
 {
     try
     {
         _CompiledAssembly = CSScript.LoadCode(BuildSource());
         AsmHelper helper = new AsmHelper(CSScript.LoadCode(BuildSource(), null, false));
         if ((usebuiltinparams) && (ParameterstoPass != null))
         {
             return(helper.Invoke(BuildInvoker(), ParameterstoPass));
         }
         else
         {
             return(helper.Invoke(BuildInvoker()));
         }
     }
     catch (CompilerException e)
     {
         Errors = (List <string>)e.Data["Errors"];
         return(Errors);
     }
     catch (Exception e)
     {
         Errors.Add(e.Message);
         return(Errors);
     }
 }
Esempio n. 26
0
        public dynamic DoEval(string Text, bool Preprocess, out string Error)
        {
            if (Preprocess)
            {
                Host.Preprocess(ref Text);
            }

            string imports = Host.GetImports();

            try
            {
                System.Reflection.Assembly assembly = CSScript.LoadCode(imports + @"
                        public class Eval
                        { static public dynamic Expr() { return " + Text + " } }");
                var     Expr = new AsmHelper(assembly).GetStaticMethod();
                dynamic ret  = Expr();
                Error = "";
                return(ret);
            }
            catch (Exception ex)
            {
                Error = Host.ProcessError(ex.Message);
                return(null);
            }
        }
Esempio n. 27
0
 void ExecuteAndUnloadScript(string script)
 {
     using (AsmHelper helper = new AsmHelper(CSScript.Compile(script, Path.GetFullPath("ExternalAsm.dll")), null, true))
     {
         helper.Invoke("*.Execute");
     }
 }
Esempio n. 28
0
    static void Main()
    {
        CSScript.GlobalSettings.UseAlternativeCompiler = Path.GetFullPath("CSSCodeProvider.dll"); //compiler that understands JS/VB.NET/C++ and classless-C#

        var script = new AsmHelper(CSScript.Load("Hello.js"));

        script.Invoke("*.Main");
    }
Esempio n. 29
0
    static void TypeSafeDelegate(AsmHelper script, object instance)
    {
        FastInvokeDelegate sumInvoker = script.GetMethodInvoker("Claculator.Sum", 0, 0);        //type unsafe delegate
        Action <int, int>  Sum        = delegate(int a, int b) { sumInvoker(instance, a, b); }; //type safe delegate

        Sum(1, 2);
        Sum(4, 7);
    }
Esempio n. 30
0
		public static T TryAlignToInterface<T>(this AsmHelper helper, object obj) where T : class
		{
			try {
				return helper.AlignToInterface<T>(obj);
			} catch {
				return null;
			}
		}