Exemplo n.º 1
0
        private object CSharpEval(string cs)
        {
            var c  = new CSharpCodeProvider();
            var cp = new CompilerParameters();

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try {
                    var location = assembly.Location;
                    if (!string.IsNullOrEmpty(location))
                    {
                        cp.ReferencedAssemblies.Add(location);
                    }
                }
                catch (NotSupportedException) {
                }
            }

            cp.ReferencedAssemblies.Add(typeof(DeveloperConsole).Assembly.Location);

            foreach (var s in DeveloperConsole.RegisteredScripts)
            {
                cp.ReferencedAssemblies.Add(s.Key.GetType().Assembly.Location);
            }

            cp.CompilerOptions  = "/t:library";
            cp.GenerateInMemory = true;

            var code = "using System;\n";

            code += "using System.Drawing;\n";
            code += "using System.Windows.Forms;\n";
            code += "using GTA;\n";
            code += "using GTA.Math;\n";
            code += "using GTA.Native;\n";
            code += "using DeveloperConsole;\n";

            code += "namespace DeveloperConsoleCodeEval{ \n";
            code += "public class DeveloperConsoleCodeEval{ \n";
            code += "public object EvalCode(){\n";
            code += "Player PLAYER = Game.Player; \n";
            code += "Ped PED = PLAYER.Character;\n";
            code += "int MP_ID = Function.Call<int>(Hash.NETWORK_GET_PLAYER_INDEX, PED.Handle);\n";

            code += cs + ";\n";
            code += "return null; \n";
            code += "} \n";
            code += "} \n";
            code += "}\n";

            var cr = c.CompileAssemblyFromSource(cp, code);

            if (cr.Errors.Count > 0)
            {
                _console.PrintError("C# ERROR: " + cr.Errors[0].ErrorText + " at " + cr.Errors[0].Line + ":" +
                                    cr.Errors[0].Column);
                return(null);
            }

            var a = cr.CompiledAssembly;
            var o = a.CreateInstance("DeveloperConsoleCodeEval.DeveloperConsoleCodeEval");

            if (o != null)
            {
                var t  = o.GetType();
                var mi = t.GetMethod("EvalCode");
                var s  = mi.Invoke(o, null);
                return(s);
            }
            return(null);
        }