コード例 #1
0
        static int Main(string[] args)
        {
            ParserResult <CommandLineArguments> options = null;

            try
            {
                options = Parser.Default.ParseArguments <CommandLineArguments>(args);
                if (!IsValid(options))
                {
                    return(-2);
                }
            }
            catch
            {
                Console.WriteLine();
                Console.WriteLine("Failed to parse command line arguments. See valid command line arguments below:");

                var h = new HelpText {
                    AddDashesToOption = true, AdditionalNewLineAfterOption = true
                };

                h.AddOptions(new CommandLineArguments());
                Console.WriteLine(h.ToString());

                return(-3);
            }

            RedirectConsoleOutput(options.Value.OutputFile);

            if (!ReadResponseFile(options.Value))
            {
                return(-4);
            }

            try
            {
                options.Value.ProjectPath = Path.GetFullPath(options.Value.ProjectPath);

                // We should ignore scripts in assets/WebGLTemplates
                var ignoredPathsRegex = new Regex(string.Format("assets{0}{0}webgltemplates{0}{0}", Path.DirectorySeparatorChar), RegexOptions.Compiled | RegexOptions.IgnoreCase);

                var editorSubFolder        = String.Format("{0}Editor{0}", Path.DirectorySeparatorChar);
                var pluginsEditorSubFolder = String.Format("{0}Plugins{0}Editor{0}", Path.DirectorySeparatorChar);
                var pluginSubFolder        = String.Format("{0}Plugins{0}", Path.DirectorySeparatorChar);

                var allFiles = Directory.GetFiles(Path.Combine(options.Value.ProjectPath, "Assets"), "*.js", SearchOption.AllDirectories).Where(path => !ignoredPathsRegex.IsMatch(path));
                var filter   = new Regex(string.Format(@"{0}|{1}|{2}", editorSubFolder, pluginSubFolder, pluginsEditorSubFolder).Replace("\\", "\\\\"), RegexOptions.Compiled);

                var runtimeScripts = allFiles.Where(scriptPath => !filter.IsMatch(scriptPath)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var editorScripts = allFiles.Where(scriptPath => scriptPath.Contains(editorSubFolder) && !scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var pluginScripts = allFiles.Where(scriptPath => scriptPath.Contains(pluginSubFolder) && !scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var pluginEditorScritps = allFiles.Where(scriptPath => scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();

                if (!ValidateAssemblyReferences(options))
                {
                    return(-1);
                }

                if (options.Value.DumpScripts)
                {
                    DumpScripts("Runtime", runtimeScripts);
                    DumpScripts("Editor", editorScripts);
                    DumpScripts("Plugin", pluginScripts);
                    DumpScripts("Plugin/Editor", pluginEditorScritps);
                }

                var converter = new UnityScript2CSharpConverter(options.Value.IgnoreErrors, options.Value.SkipComments, options.Value.ShowOrphanComments);

                var referencedSymbols = new List <SymbolInfo>();
                var errors            = new HashSet <CompilerError>(new CompilerErrorComparer());

                ConvertScripts(AssemblyType.Runtime, runtimeScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.Editor, editorScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.RuntimePlugins, pluginScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.EditorPlugins, pluginEditorScritps, converter, options.Value, referencedSymbols, errors);

                ReportConversionFinished(runtimeScripts.Length + editorScripts.Length + pluginScripts.Length, options.Value, referencedSymbols, errors);
            }
            catch (Exception ex)
            {
                using (WithConsoleColors.SetTo(ConsoleColor.DarkRed, ConsoleColor.Black))
                {
                    Console.WriteLine(ex.Message);
                    if (options.Value.Verbose)
                    {
                        Console.WriteLine();
                        Console.WriteLine(ex.StackTrace);
                    }

                    if (!options.Value.IgnoreErrors)
                    {
                        Console.WriteLine("Consider running converter with '-i' option.");
                    }
                }

                return(-5);
            }

            return(0);
        }