コード例 #1
0
        public void using_CompilerOptions()
        {
            // Note if you give AssemblyFile the name with the extension .dll xUnit runtime will lock the file simply
            // because it was present in the local dir. So hide the assembly by dropping the file extension and giving the name "using_CompilerOptions".

            var info = new CompileInfo
            {
                CompilerOptions = "-define:test",
                AssemblyFile    = nameof(using_CompilerOptions).GetFullPath()
            };

            Assembly asm = CSScript.CodeDomEvaluator.CompileCode(
                @"using System;
                      public class Script
                      {
                          public int Sum(int a, int b)
                          {
                              #if test
                                  return -(a+b);
                              #else
                                  return a+b;
                              #endif
                          }
                      }",
                info);

            dynamic script = asm.CreateObject("*");
            var     result = script.Sum(7, 3);

            Assert.Equal(-10, result);
        }
コード例 #2
0
        public void CompileCode_CompileInfo()
        {
            // Note if you give AssemblyFile the name with the extension .dll xUnit runtime will lock the file simply
            // because it was present in the local dir. So hide the assembly by dropping the file extension.

            var asm_file = GetTempFileName(nameof(CompileCode_InmemAsmLocation));

            var info = new CompileInfo {
                AssemblyFile = asm_file
            };

            Assembly asm = evaluator.CompileCode(@"using System;
                                                   public class Script
                                                   {
                                                       public int Sum(int a, int b)
                                                       {
                                                           return a+b;
                                                       }
                                                   }",
                                                 info);

            dynamic script = asm.CreateObject("*");
            var     result = script.Sum(7, 3);

            Assert.Equal(10, result);
            Assert.Equal(asm_file, asm.Location());
        }
コード例 #3
0
ファイル: samples.cs プロジェクト: kesshei/cs-script
    public static void ScriptReferencingScript()
    {
        var info = new CompileInfo {
            RootClass = "printer_script"
        };

        var printer_asm = CSScript.Evaluator
                          .CompileCode(
            @"using System;
                                        public class Printer
                                        {
                                            public void Print() =>
                                                Console.WriteLine(""Printing..."");
                                        }", info);

        dynamic script = CSScript.Evaluator
                         .ReferenceAssembly(printer_asm)
                         .LoadMethod(@"using static printer_script;
                                               void Test()
                                               {
                                                   new Printer().Print();
                                               }");

        script.Test();
    }
コード例 #4
0
        public void CompileCode_InmemAsmLocation()
        {
            if (evaluator is RoslynEvaluator) // Roslyn cannot work with C# files (but in memory streams)
            {
                return;                       // So asm location does not make sense.
            }
            var asm_file = GetTempFileName(nameof(CompileCode_InmemAsmLocation));

            var info = new CompileInfo {
                AssemblyFile = asm_file, PreferLoadingFromFile = false
            };

            Assembly asm = evaluator.CompileCode(@"using System;
                                                    public class Script
                                                    {
                                                        public int Sum(int a, int b)
                                                        {
                                                            return a+b;
                                                        }
                                                    }",
                                                 info);

            var css_injected_location = asm.Location();
            var clr_standard_location = asm.Location;

            Assert.Equal(asm_file, css_injected_location);
            Assert.Equal("", clr_standard_location);
        }
コード例 #5
0
        public void Issue_185_Referencing()
        {
            CSScript.EvaluatorConfig.DebugBuild = true;

            var root_class_name = $"script_{System.Guid.NewGuid()}".Replace("-", "");

            var info = new CompileInfo {
                RootClass = root_class_name, PreferLoadingFromFile = true
            };

            try
            {
                var printer_asm = CSScript.RoslynEvaluator
                                  .CompileCode(@"using System;
                                                 public class Printer
                                                 {
                                                     public void Print() => Console.Write(""Printing..."");
                                                 }", info);

                dynamic script = CSScript.RoslynEvaluator
                                 .ReferenceAssembly(printer_asm)
                                 .LoadMethod($"using static {root_class_name};" + @"
                                               void Test()
                                               {
                                                   new Printer().Print();
                                               }");
                script.Test();
            }
            finally
            {
                info.AssemblyFile.FileDelete(rethrow: false);
            }
        }
コード例 #6
0
        public CompileInfo Compile(string javaScriptFileName){

            var compileInfo         = new CompileInfo();
            var info                = Runner.Execute(this.JavaExe, this.GetJavaCommandLine(javaScriptFileName));
            compileInfo.ErrorLevel  = info.ErrorLevel;
            compileInfo.Output      = info.Output;
            compileInfo.ErrorOutput = info.ErrorOutput;
            return compileInfo;
        }
コード例 #7
0
        public CompileInfo Compile(string javaScriptFileName)
        {
            var compileInfo = new CompileInfo();
            var info        = Runner.Execute(this.JavaExe, this.GetJavaCommandLine(javaScriptFileName));

            compileInfo.ErrorLevel  = info.ErrorLevel;
            compileInfo.Output      = info.Output;
            compileInfo.ErrorOutput = info.ErrorOutput;
            return(compileInfo);
        }
コード例 #8
0
        public void referencing_script_types_from_another_script()
        {
            CSScript.EvaluatorConfig.ReferenceDomainAssemblies = false; // to avoid an accidental referencing
            CSScript.EvaluatorConfig.DebugBuild = true;

            var info = new CompileInfo {
                RootClass = "script_a", AssemblyFile = "script_a_asm2"
            };

            try
            {
                var code2 = @"using System;
                      using System.Collections.Generic;
                      using System.Linq;

                      public class Utils
                      {
                          static void Main(string[] args)
                          {
                              var x = new List<int> {1, 2, 3, 4, 5};
                              var y = Enumerable.Range(0, 5);

                              x.ForEach(Console.WriteLine);
                              var z = y.First();
                              Console.WriteLine(z);
                          }
                      }";

                CSScript.RoslynEvaluator
                .With(e => e.IsCachingEnabled = false)         // required to not interfere with xUnit
                .CompileCode(code2, info);

                dynamic script = CSScript.RoslynEvaluator
                                 .With(e => e.IsCachingEnabled = false)
                                 // .With(e => e.ReferenceDomainAssemblies = false)

                                 .ReferenceAssembly(info.AssemblyFile)
                                 .CompileMethod(@"using static script_a;
                                                  Utils Test()
                                                  {
                                                      return new Utils();
                                                  }")
                                 .CreateObject("*");
                object utils = script.Test();

                Assert.Equal("script_a+Utils", utils.GetType().ToString());
            }
            finally
            {
                info.AssemblyFile.FileDelete(rethrow: false);
            }
        }
コード例 #9
0
        public void referencing_script_types_from_another_script()
        {
            CSScript.EvaluatorConfig.DebugBuild = true;
            CSScript.EvaluatorConfig.ReferenceDomainAssemblies = false;

            var info = new CompileInfo {
                AssemblyFile = "utils_asm"
            };

            try
            {
                var utils_code = @"using System;
                               using System.Collections.Generic;
                               using System.Linq;

                               public class Utils
                               {
                                   public class Printer { }

                                   static void Main(string[] args)
                                   {
                                       var x = new List<int> {1, 2, 3, 4, 5};
                                       var y = Enumerable.Range(0, 5);

                                       x.ForEach(Console.WriteLine);
                                       var z = y.First();
                                       Console.WriteLine(z);
                                   }
                               }";

                var asm = CSScript.CodeDomEvaluator
                          .CompileCode(utils_code, info);

                dynamic script = CSScript.CodeDomEvaluator
                                 .ReferenceAssembly(info.AssemblyFile)
                                 .CompileMethod(@"public Utils NewUtils() => new Utils();
                                                      public Utils.Printer NewPrinter() => new Utils.Printer();")
                                 .CreateObject("*");

                object utils   = script.NewUtils();
                object printer = script.NewPrinter();

                Assert.Equal("Utils", utils.GetType().ToString());
                Assert.Equal("Utils+Printer", printer.GetType().ToString());
            }
            finally
            {
                info.AssemblyFile.FileDelete(rethrow: false); // assembly is locked so only showing the intention
            }
        }
コード例 #10
0
ファイル: samples.cs プロジェクト: kesshei/cs-script
    public static void CompileCode()
    {
        var info = new CompileInfo {
            RootClass = "printer_script", AssemblyFile = "script.dll"
        };

        var printer_asm = CSScript.Evaluator
                          .CompileCode(
            @"using System;
                                        public class Printer
                                        {
                                            public static void Print() =>
                                                Console.WriteLine(""Printing..."");
                                        }", info);

        printer_asm
        .GetType("printer_script+Printer")
        .GetMethod("Print")
        .Invoke(null, null);
    }
コード例 #11
0
        public void CompileCode_CompileInfo_CodeDom()
        {
            var info = new CompileInfo {
                RootClass = "test"
            };

            var ex = Assert.Throws <CSScriptException>(() =>
            {
                Assembly asm = evaluator.CompileCode(@"using System;
                                                               public class Script
                                                               {
                                                                   public int Sum(int a, int b)
                                                                   {
                                                                       return a+b;
                                                                   }
                                                               }",
                                                     info);
            });

            Assert.StartsWith("CompileInfo.RootClass property should only be used with Roslyn evaluator", ex.Message);
        }
コード例 #12
0
        public override void Read(AssetReader reader)
        {
            if (IsSerialized(reader.Version))
            {
                ReadNamedObject(reader);

                ParsedForm.Read(reader);
                Platforms = reader.ReadArray((t) => (GPUPlatform)t);
                if (IsDoubleArray(reader.Version))
                {
                    uint[][] offsets             = reader.ReadUInt32ArrayArray();
                    uint[][] compressedLengths   = reader.ReadUInt32ArrayArray();
                    uint[][] decompressedLengths = reader.ReadUInt32ArrayArray();
                    byte[]   compressedBlob      = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, offsets, compressedLengths, decompressedLengths, compressedBlob);
                }
                else
                {
                    uint[] offsets             = reader.ReadUInt32Array();
                    uint[] compressedLengths   = reader.ReadUInt32Array();
                    uint[] decompressedLengths = reader.ReadUInt32Array();
                    byte[] compressedBlob      = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, offsets, compressedLengths, decompressedLengths, compressedBlob);
                }
            }
            else
            {
                base.Read(reader);

                if (HasBlob(reader.Version))
                {
                    uint   decompressedSize = reader.ReadUInt32();
                    byte[] compressedBlob   = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, 0, (uint)compressedBlob.Length, decompressedSize, compressedBlob);
                }

                if (HasFallback(reader.Version))
                {
                    Fallback.Read(reader);
                }
                if (HasDefaultProperties(reader.Version))
                {
                    DefaultProperties.Read(reader);
                }
                if (HasStaticProperties(reader.Version))
                {
                    StaticProperties.Read(reader);
                }
            }

            if (HasDependencies(reader.Version))
            {
                Dependencies = reader.ReadAssetArray <PPtr <Shader> >();
            }
            if (HasNonModifiableTextures(reader.Version))
            {
                NonModifiableTextures = new Dictionary <string, PPtr <Texture> >();
                NonModifiableTextures.Read(reader);
            }
            if (HasShaderIsBaked(reader.Version))
            {
                ShaderIsBaked = reader.ReadBoolean();
                reader.AlignStream();
            }

#if UNIVERSAL
            if (HasErrors(reader.Version, reader.Flags))
            {
                Errors = reader.ReadAssetArray <ShaderError>();
            }
            if (HasDefaultTextures(reader.Version, reader.Flags))
            {
                DefaultTextures = new Dictionary <string, PPtr <Texture> >();
                DefaultTextures.Read(reader);
            }
            if (HasCompileInfo(reader.Version, reader.Flags))
            {
                CompileInfo.Read(reader);
            }
#endif
        }
コード例 #13
0
ファイル: Shader.cs プロジェクト: nguyenhoainam91/UtinyRipper
        public override void Read(AssetReader reader)
        {
            if (IsSerialized(reader.Version))
            {
                ReadNamedObject(reader);

                ParsedForm.Read(reader);

                Platforms = reader.ReadArray((t) => (GPUPlatform)t);
                uint[] offsets             = reader.ReadUInt32Array();
                uint[] compressedLengths   = reader.ReadUInt32Array();
                uint[] decompressedLengths = reader.ReadUInt32Array();
                byte[] compressedBlob      = reader.ReadByteArray();
                reader.AlignStream();

                SubProgramBlobs = new ShaderSubProgramBlob[Platforms.Length];
                using (MemoryStream memStream = new MemoryStream(compressedBlob))
                {
                    for (int i = 0; i < Platforms.Length; i++)
                    {
                        uint offset             = offsets[i];
                        uint compressedLength   = compressedLengths[i];
                        uint decompressedLength = decompressedLengths[i];

                        memStream.Position = offset;
                        byte[] decompressedBuffer = new byte[decompressedLength];
                        using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream, (int)compressedLength))
                        {
                            lz4Stream.ReadBuffer(decompressedBuffer, 0, decompressedBuffer.Length);
                        }

                        using (MemoryStream blobMem = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(blobMem, EndianType.LittleEndian, reader.Version, reader.Platform, reader.Flags))
                            {
                                ShaderSubProgramBlob blob = new ShaderSubProgramBlob();
                                blob.Read(blobReader);
                                SubProgramBlobs[i] = blob;
                            }
                        }
                    }
                }
            }
            else
            {
                base.Read(reader);

                if (IsEncoded(reader.Version))
                {
                    uint decompressedSize = reader.ReadUInt32();
                    int  comressedSize    = reader.ReadInt32();
                    if (comressedSize > 0 && decompressedSize > 0)
                    {
                        byte[] subProgramBlob = new byte[comressedSize];
                        reader.ReadBuffer(subProgramBlob, 0, comressedSize);

                        byte[] decompressedBuffer = new byte[decompressedSize];
                        using (MemoryStream memStream = new MemoryStream(subProgramBlob))
                        {
                            using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream))
                            {
                                lz4Stream.ReadBuffer(decompressedBuffer, 0, decompressedBuffer.Length);
                            }
                        }

                        using (MemoryStream memStream = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(memStream, EndianType.LittleEndian, reader.Version, reader.Platform, reader.Flags))
                            {
                                SubProgramBlob.Read(blobReader);
                            }
                        }
                    }
                    reader.AlignStream();
                }

                if (HasFallback(reader.Version))
                {
                    Fallback.Read(reader);
                }
                if (HasDefaultProperties(reader.Version))
                {
                    DefaultProperties.Read(reader);
                }
                if (HasStaticProperties(reader.Version))
                {
                    StaticProperties.Read(reader);
                }
            }

            if (HasDependencies(reader.Version))
            {
                Dependencies = reader.ReadAssetArray <PPtr <Shader> >();
            }
            if (HasNonModifiableTextures(reader.Version))
            {
                NonModifiableTextures = new Dictionary <string, PPtr <Texture> >();
                NonModifiableTextures.Read(reader);
            }
            if (HasShaderIsBaked(reader.Version))
            {
                ShaderIsBaked = reader.ReadBoolean();
                reader.AlignStream();
            }

#if UNIVERSAL
            if (HasErrors(reader.Version, reader.Flags))
            {
                Errors = reader.ReadAssetArray <ShaderError>();
            }
            if (HasDefaultTextures(reader.Version, reader.Flags))
            {
                DefaultTextures = new Dictionary <string, PPtr <Texture> >();
                DefaultTextures.Read(reader);
            }
            if (HasCompileInfo(reader.Version, reader.Flags))
            {
                CompileInfo.Read(reader);
            }
#endif
        }