Ejemplo n.º 1
0
        /// <summary>
        /// Load supported .NET scripts from the /scripts directory.
        /// </summary>
        private static void LoadScripts()
        {
            _scripts = new List<ModScript>();
            var dllFiles = Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.dll");
            var sourceFiles = Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.cs")
                .Concat(Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.vb"));

            var compiledScripts = 0;

            foreach (var file in dllFiles)
            {
                if (!IsValidAssembly(file)) continue;
                var script = new ModScript(file);
                _scripts.Add(script);
                compiledScripts++;
                Log.Print(NFSScriptLoader.INFO_TAG, $"Loaded {script.File}");
            }
            foreach (var file in sourceFiles)
            {
                if (!CompileScript(file))
                {
                    Log.Print(NFSScriptLoader.ERROR_TAG, $"Error compiling {file}!! Will abort.");
                    NFSScriptLoader.CriticalError($"Could not compile script at {file}");
                }
                
                compiledScripts++;
            }

            Log.Print(NFSScriptLoader.INFO_TAG, $"{compiledScripts} scripts are loaded.");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load supported .NET scripts from the /scripts directory.
        /// </summary>
        private static void LoadScripts()
        {
            scripts = new List <ModScript>();
            string[] dllFiles = Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.dll");
            string[] csFiles  = Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.cs");
            string[] vbFiles  = Directory.GetFiles(NFSScriptLoader.SCRIPTS_FOLDER, "*.vb");

            int compiledScripts = 0;

            for (int i = 0; i < dllFiles.Length; i++)
            {
                if (IsValidAssembly(dllFiles[i]))
                {
                    ModScript script = new ModScript(dllFiles[i]);
                    scripts.Add(script);
                    compiledScripts++;
                    Log.Print(NFSScriptLoader.INFO_TAG, string.Format("{0} {1}", "Loaded", script.File));
                }
            }
            for (int i = 0; i < csFiles.Length; i++)
            {
                CompileScript(csFiles[i]);
                compiledScripts++;
            }
            for (int i = 0; i < vbFiles.Length; i++)
            {
                CompileScript(vbFiles[i]);
                compiledScripts++;
            }

            Log.Print(NFSScriptLoader.INFO_TAG, string.Format("{0} scripts are loaded.", compiledScripts));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Compile script by file.
        /// </summary>
        /// <param name="file"></param>
        private static bool CompileScript(string file)
        {
            var sourceFile = new FileInfo(file);
            CodeDomProvider provider = null;
            var csc = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });

            // TODO: maybe allow custom references like CS-Script
            var parameters = new CompilerParameters(new[]
                {"mscorlib.dll", "System.Core.dll", "System.Windows.Forms.dll", "NFSScript.dll"})
            {
                GenerateExecutable = false,
                GenerateInMemory = true
            };

            // TODO this doesn't support C#6 and above
            // we can fix that with this CodeDom provider from nuget:
            // https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/
            // then we can do this:
            // new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
            
            // TODO perhaps add support for F#
            // that comes in the form of another custom package
            // https://github.com/fsprojects/FSharp.Compiler.CodeDom
            
            // since we're at it, why not PowerShell?
            // https://github.com/adamdriscoll/PowerShellCodeDomProvider
            
            switch (sourceFile.Extension.ToUpperInvariant())
            {
                case ".CS":
                    provider = new CSharpCodeProvider();
                    break;
                case ".VB":
                    provider = new VBCodeProvider();
                    break;
                case ".JS":
                    provider = new JScriptCodeProvider();
                    break;
            }

            if (provider == null) return false;
            
            var results = provider.CompileAssemblyFromFile(parameters, file);
            if (results.Errors.HasErrors)
            {
                foreach (CompilerError r in results.Errors)
                {
                    Log.Print(NFSScriptLoader.ERROR_TAG, r.ToString());
                }
                return false;
            }
            var ass = results.CompiledAssembly;
            var script = new ModScript(ass, Path.GetFileName(file));
            _scripts.Add(script);
            Log.Print(NFSScriptLoader.INFO_TAG, $"Loaded {script.File}");

            return true;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Compile script by file.
        /// </summary>
        /// <param name="file"></param>
        private static bool CompileScript(string file)
        {
            FileInfo        sourceFile = new FileInfo(file);
            CodeDomProvider provider   = null;
            var             csc        = new CSharpCodeProvider(new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            });

            var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "System.Windows.Forms.dll", "NFSScript.dll" });

            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory   = true;

            if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
            {
                provider = CodeDomProvider.CreateProvider("CSharp");
            }
            else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
            {
                provider = CodeDomProvider.CreateProvider("VisualBasic");
            }

            if (provider != null)
            {
                CompilerResults results = provider.CompileAssemblyFromFile(parameters, file);
                if (results.Errors.HasErrors)
                {
                    foreach (CompilerError r in results.Errors)
                    {
                        Log.Print(NFSScriptLoader.ERROR_TAG, r.ToString());
                    }
                    return(false);
                }
                else
                {
                    Assembly  ass    = results.CompiledAssembly;
                    ModScript script = new ModScript(ass, Path.GetFileName(file));
                    scripts.Add(script);
                    Log.Print(NFSScriptLoader.INFO_TAG, string.Format("{0} {1}", "Loaded", script.File));

                    return(true);
                }
            }

            return(false);
        }