Example #1
0
        public override PluginApplication CompileResourceScriptPlugin()
        {
            // New projects may have no C# scripts. In this case a dummy plugin instance that does nothing will be
            // returned. Once new scripts appear - plugin will be reloaded properly.
            var plugin = new RuntimeCompiledScriptPluginApplication(Context.Instance);

            plugin.SetSlaveAssembly(CsCompiler.FindAndCompile("", null));
            return(plugin);
        }
Example #2
0
        public override PluginApplication CompileResourceScriptPlugin()
        {
            var  scriptRsrcs     = new StringList();
            var  scriptCodes     = new List <string>();
            var  sourceFiles     = new List <string>();
            bool compileFromText = false;

            Context.Instance.Cache.Scan(scriptRsrcs, "", "*.cs", Urho3D.ScanFiles, true);
            foreach (string fileName in scriptRsrcs)
            {
                using (var file = Context.Instance.Cache.GetFile(fileName))
                {
                    // Gather both paths and code text here. If scripts are packaged we must compile them from
                    // text form. However if we are running a development version of application we prefer to
                    // compile scripts directly from file because then we get proper error locations.
                    compileFromText |= file.IsPackaged();
                    scriptCodes.Add(file.ReadString());
                    if (!compileFromText)
                    {
                        string path = Context.Instance.Cache.GetResourceFileName(fileName);
                        path = Urho3D.GetAbsolutePath(path);
                        path = Urho3D.GetNativePath(path);
                        sourceFiles.Add(path);
                    }
                }
            }

            if (sourceFiles.Count == 0)
            {
                return(null);
            }

            var csc = new CSharpCodeProvider();
            var compileParameters = new CompilerParameters(new[] // TODO: User may need to extend this list
            {
                "mscorlib.dll",
                "System.dll",
                "System.Core.dll",
                "System.Data.dll",
                "System.Drawing.dll",
                "System.Numerics.dll",
                "System.Runtime.Serialization.dll",
                "System.Xml.dll",
                "System.Xml.Linq.dll",
                "Urho3DNet.dll",
            })
            {
                GenerateExecutable    = false,
                GenerateInMemory      = true,
                TreatWarningsAsErrors = false,
            };

            CompilerResults results;

            if (compileFromText)
            {
                results = csc.CompileAssemblyFromSource(compileParameters, scriptCodes.ToArray());
            }
            else
            {
                results = csc.CompileAssemblyFromFile(compileParameters, sourceFiles.ToArray());
            }

            if (results.Errors.HasErrors)
            {
                foreach (CompilerError error in results.Errors)
                {
                    string resourceName = error.FileName;
                    foreach (string resourceDir in Context.Instance.Cache.ResourceDirs)
                    {
                        if (resourceName.StartsWith(resourceDir))
                        {
                            resourceName = resourceName.Substring(resourceDir.Length);
                            break;
                        }
                    }

                    string message = $"{resourceName}:{error.Line}:{error.Column}: {error.ErrorText}";
                    if (error.IsWarning)
                    {
                        Log.Warning(message);
                    }
                    else
                    {
                        Log.Error(message);
                    }
                }

                return(null);
            }

            // New projects may have no C# scripts. In this case a dummy plugin instance that does nothing will be
            // returned. Once new scripts appear - plugin will be reloaded properly.
            var plugin = new RuntimeCompiledScriptPluginApplication(Context.Instance);

            plugin.SetHostAssembly(results.CompiledAssembly);
            return(plugin);
        }