Beispiel #1
0
        public bool CompileIngameScript(MyStringId id, StringBuilder errors)
        {
            if (!MyFakes.ENABLE_SCRIPTS)
            {
                return(false);
            }
            Assembly assembly;
            bool     success;

            success = IlCompiler.Compile(new string[] { InGameScriptsCode[id].ToString() }, out assembly, false);
            if (success)
            {
                var scriptType = typeof(MyIngameScript);
                if (InGameScripts.ContainsKey(id))
                {
                    InGameScripts.Remove(id);
                }
                foreach (var type in assembly.GetTypes())
                {
                    if (scriptType.IsAssignableFrom(type))
                    {
                        InGameScripts.Add(id, type);
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        private static ObjectReader GetDelegate(Type type, FieldInfo[] fields, Serializer serializer)
        {
            var c                       = new IlCompiler <ObjectReader>();
            var stream                  = c.Parameter <Stream>("stream");
            var session                 = c.Parameter <DeserializerSession>("session");
            var newExpression           = c.NewObject(type);
            var target                  = c.Variable <object>("target");
            var assignNewObjectToTarget = c.WriteVar(target, newExpression);

            c.Emit(assignNewObjectToTarget);

            var size         = c.Constant(16);
            var buffer       = c.Variable <byte[]>(DefaultCodeGenerator.PreallocatedByteBuffer);
            var bufferValue  = c.Call(typeof(DeserializerSession).GetMethod("GetBuffer"), session, size);
            var assignBuffer = c.WriteVar(buffer, bufferValue);

            c.Emit(assignBuffer);

            var typedTarget = c.CastOrUnbox(target, type);

            foreach (var field in fields)
            {
                var s    = serializer.GetSerializerByType(field.FieldType);
                var read = s.EmitReadValue(c, stream, session, field);

                var assignReadToField = c.WriteField(field, typedTarget, read);
                c.Emit(assignReadToField);
            }
            c.Emit(target);

            var readAllFields = c.Compile();

            return(readAllFields);
        }
Beispiel #3
0
        public void CanCreateEmptyMethod()
        {
            var c = new IlCompiler <Action>();
            var a = c.Compile();

            a();
        }
Beispiel #4
0
        public void CanCreateEmptyMethodWithArguments()
        {
            var c = new IlCompiler <Action <bool> >();
            var a = c.Compile();

            a(true);
        }
        public bool CompileIngameScript(MyStringId id, StringBuilder errors)
        {
#if XB1 // XB1_ALLINONEASSEMBLY
            System.Diagnostics.Debug.Assert(false, "XB1 TODO?");
            return(false);
#else // !XB1
            // TODO: Not in use. Remove when Roslyn scripts are activated

            if (!MyFakes.ENABLE_SCRIPTS)
            {
                return(false);
            }
            Assembly assembly;
            bool     success;
            success = IlCompiler.Compile(new string[] { InGameScriptsCode[id].ToString() }, out assembly, false);
            if (success)
            {
                var scriptType = typeof(MyIngameScript);
                if (InGameScripts.ContainsKey(id))
                {
                    InGameScripts.Remove(id);
                }
                foreach (var type in assembly.GetTypes())
                {
                    if (scriptType.IsAssignableFrom(type))
                    {
                        InGameScripts.Add(id, type);
                        return(true);
                    }
                }
            }
            return(false);
#endif // !XB1
        }
Beispiel #6
0
        public void CanCreateObject()
        {
            var c   = new IlCompiler <Func <Dummy> >();
            var obj = c.NewObject(typeof(Dummy));

            c.Emit(obj);
            var a = c.Compile();

            a();
        }
Beispiel #7
0
        public void CanReturnConstantString()
        {
            var c = new IlCompiler <Func <string> >();
            var b = c.Constant("hello");

            c.Emit(b);
            var a   = c.Compile();
            var res = a();

            Assert.Equal("hello", res);
        }
Beispiel #8
0
        public void CanCreateEmptyMethodWithReturnType()
        {
            var c = new IlCompiler <Func <bool> >();
            var b = c.Constant(true);

            c.Emit(b);
            var a   = c.Compile();
            var res = a();

            Assert.Equal(true, res);
        }
Beispiel #9
0
        public void CanCallInstanceMethodOnParameter()
        {
            var c     = new IlCompiler <Action <Dummy> >();
            var param = c.Parameter <Dummy>("dummy");

            c.EmitCall(SetBool, param);
            var a     = c.Compile();
            var dummy = new Dummy();

            a(dummy);
            Assert.Equal(true, dummy.BoolField);
        }
Beispiel #10
0
        public void CanCallStaticMethodUsingParameter()
        {
            var c     = new IlCompiler <Action <Dummy> >();
            var param = c.Parameter <Dummy>("dummy");

            c.EmitStaticCall(SetStatic, param);
            var a     = c.Compile();
            var dummy = new Dummy();

            a(dummy);
            Assert.Equal(true, dummy.BoolField);
        }
Beispiel #11
0
        public void CanCreateObjectAndStoreInVar()
        {
            var c        = new IlCompiler <Action>();
            var variable = c.Variable <Dummy>("dummy");
            var obj      = c.NewObject(typeof(Dummy));
            var assign   = c.WriteVar(variable, obj);

            c.Emit(assign);
            var a = c.Compile();

            a();
        }
Beispiel #12
0
        public void CanStoreBoolInField()
        {
            var c     = new IlCompiler <Action>();
            var True  = c.Constant(true);
            var obj   = c.NewObject(typeof(Dummy));
            var write = c.WriteField(BoolField, obj, True);

            c.Emit(write);
            var a = c.Compile();

            a();
        }
Beispiel #13
0
        public void CanModifyParameter()
        {
            var c     = new IlCompiler <Action <Dummy> >();
            var param = c.Parameter <Dummy>("dummy");
            var write = c.WriteField(BoolField, param, c.Constant(true));

            c.Emit(write);
            var a     = c.Compile();
            var dummy = new Dummy();

            a(dummy);
            Assert.Equal(true, dummy.BoolField);
        }
Beispiel #14
0
        public void CanCastToAndFromObject()
        {
            var c = new IlCompiler <Action>();

            var True        = c.Constant(true);
            var boxedBool   = c.Convert(True, typeof(object));
            var unboxedBool = c.CastOrUnbox(boxedBool, typeof(bool));

            var obj   = c.NewObject(typeof(Dummy));
            var write = c.WriteField(BoolField, obj, unboxedBool);

            c.Emit(write);
            var a = c.Compile();

            a();
        }
Beispiel #15
0
        private bool CallScriptInternal(string message)
        {
            Assembly ass;

            if (IlCompiler.Buffer.Length > 0)
            {
                if (IlCompiler.Compile(new string[] { IlCompiler.Buffer.ToString() }, out ass, true))
                {
                    var retval = ass.GetType("wrapclass").GetMethod("run").Invoke(null, null);
                    if (!string.IsNullOrEmpty(message))
                    {
                        Sandbox.Game.Gui.MyHud.Chat.ShowMessage("returned", retval.ToString());
                    }
                    return(true);

                    IlCompiler.Buffer.Clear();
                }
                else
                {
                    IlCompiler.Buffer.Clear();
                    return(false);
                }
            }
            var parts = message.Split(Separators, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length < 3)
            {
                MyAPIGateway.Utilities.ShowNotification("Not enought parameters for script please provide following paramaters : Sriptname Classname MethodName", 5000);
                return(false);
            }
            if (!Scripts.ContainsKey(MyStringId.TryGet(parts[1])))
            {
                string availableScripts = "";
                foreach (var scriptName in Scripts)
                {
                    availableScripts += scriptName.Key + "\n";
                }
                MyAPIGateway.Utilities.ShowMissionScreen("Script not found", "", "Available scripts:", availableScripts);
                return(false);
            }
            ass = Scripts[MyStringId.Get(parts[1])];
            var type = ass.GetType(parts[2]);

            if (type == null)
            {
                string availableScripts = "";
                var    types            = ass.GetTypes();
                foreach (var scriptType in types)
                {
                    availableScripts += scriptType.FullName + "\n";
                }
                MyAPIGateway.Utilities.ShowMissionScreen("Class not found", "", "Available classes:", availableScripts);
                return(false);
            }
            var method = type.GetMethod(parts[3]);

            if (method == null)
            {
                string availableScripts = "";
                var    types            = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
                foreach (var scriptType in types)
                {
                    availableScripts += scriptType.Name + "\n";
                }
                MyAPIGateway.Utilities.ShowMissionScreen("Method not found", "", "Available methods:", availableScripts);
                return(false);
            }
            var           paramInfos = method.GetParameters();
            List <object> parameters = new List <object>();

            for (int i = 4; i < paramInfos.Length + 4 && i < parts.Length; i++)
            {
                var paramType = paramInfos[i - 4].ParameterType;
                var parseMet  = paramType.GetMethod("TryParse", new Type[] { typeof(System.String), paramType.MakeByRefType() });
                if (parseMet != null)
                {
                    var output = Activator.CreateInstance(paramType);
                    var args   = new object[] { parts[i], output };
                    var par    = parseMet.Invoke(null, args);
                    parameters.Add(args[1]);
                }
                else
                {
                    parameters.Add(parts[i]);
                }
            }
            if (paramInfos.Length == parameters.Count)
            {
                var retval = method.Invoke(null, parameters.ToArray());
                if (retval != null)
                {
                    Sandbox.Game.Gui.MyHud.Chat.ShowMessage("return value", retval.ToString());
                }
                Sandbox.Game.Gui.MyHud.Chat.ShowMessage("Call", "Success");
                return(true);
            }
            return(false);
        }