Exemplo n.º 1
0
        public static Assembly FindAndCompile(string scanPath, IEnumerable <string> extraReferences)
        {
            var scriptRsrcs = new StringList();
            var scriptCodes = new List <string>();
            var sourceFiles = new List <string>();

            Context.Instance.ResourceCache.Scan(scriptRsrcs, scanPath, "*.cs", Urho3D.ScanFiles, true);
            foreach (string fileName in scriptRsrcs)
            {
                using (File file = Context.Instance.ResourceCache.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.
                    if (file.IsPackaged)
                    {
                        scriptCodes.Add(file.ReadString());
                    }
                    else
                    {
                        string path = Context.Instance.ResourceCache.GetResourceFileName(fileName);
                        path = Urho3D.GetAbsolutePath(path);
                        path = Urho3D.GetNativePath(path);
                        sourceFiles.Add(path);
                    }
                }
            }
            return(Compile(sourceFiles, scriptCodes, extraReferences));
        }
Exemplo n.º 2
0
        public static void ParseArguments(Assembly program, string[] args)
        {
            int argc = args.Length + 1;                 // args + executable path
            var argv = new string[args.Length + 2];     // args + executable path + null

            argv[0] = new Uri(program.Location).LocalPath;
            args.CopyTo(argv, 1);
            Urho3D.ParseArgumentsInternal(argc, argv);
        }
Exemplo n.º 3
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);
        }