예제 #1
0
        /*
         *  MoveTypeToNamespace retrieves the type matching typeFullName and moves it from the original namespace
         *  into the target namespace.
         *
         *  Returns true if the type was found and moved. False if the type was not found.
         */
        public static bool MovePublicTypeToNamespace(this IRProgram program, string typeFullName,
                                                     IRNamespace targetNamespace, StringComparison casing = StringComparison.Ordinal)
        {
            bool        found      = false;
            IRNamespace sourceNS   = null;
            IRClass     foundClass = null;
            IREnum      foundEnum  = null;

            // Locate the type - could be class or enum.
            foreach (var ns in program.Namespaces)
            {
                foundClass = ns.Classes.FirstOrDefault(c => c.FullName.Equals(typeFullName, casing));
                foundEnum  = ns.Enums.FirstOrDefault(e => e.FullName.Equals(typeFullName, casing));

                // If found, remove it from the parent namespace.
                if (foundClass != null || foundEnum != null)
                {
                    // We don't move private types!
                    if (foundClass?.IsPrivate == true || foundEnum?.IsPrivate == true)
                    {
                        throw new IRMoveException("This method does not move private types!");
                    }

                    ns.Classes.Remove(foundClass);
                    ns.Enums.Remove(foundEnum);
                    found    = true;
                    sourceNS = ns;
                    break;
                }
            }

            // Add the type to the target namespace.
            if (found)
            {
                if (foundClass != null)
                {
                    targetNamespace.Classes.Add(foundClass);
                    foundClass.UpdateTypeReferences(targetNamespace, sourceNS);
                    // And move all private types under the new namespace.
                    foundClass.MovePrivateTypesToNamespace(targetNamespace, sourceNS);
                }
                else if (foundEnum != null)
                {
                    targetNamespace.Enums.Add(foundEnum);
                    foundEnum.UpdateTypeReferences(targetNamespace, sourceNS);
                }
            }

            return(found);
        }
예제 #2
0
        /*
         *  GetCreateNamespace looks through the program for the namespace object matching the
         *  given fullname.
         *  If no namespace object is found, it is created and inserted into program.
         */
        public static IRNamespace GetCreateNamespace(this IRProgram program, string nsFullName)
        {
            var targetNS = program.Namespaces.FirstOrDefault(ns => ns.FullName.Equals(nsFullName,
                                                                                      StringComparison.OrdinalIgnoreCase));

            if (targetNS == null)
            {
                // Namespace names should always be lowercased!
                targetNS = new IRNamespace(nsFullName.ToLower(), null);
                program.Namespaces.Add(targetNS);
            }

            return(targetNS);
        }
예제 #3
0
        static int Main(string[] args)
        {
            // Setup new logger
            Log = new Logger();

            Log.Info("Launched proto-extractor");

            // Parse commands
            Options opts = null;

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(_opts => opts = _opts);
            if (opts == null)
            {
                return(-1);
            }

            // Update logger with command line parameters.
            Log.SetParams(opts);

            // Setup decompiler
            var analyzer = new CSAnalyzer();

            //Set the library path.
            if (!Directory.Exists(opts.LibraryPath))
            {
                Console.WriteLine("The library path does not exist! Exiting..");

                Log.Exception("Library path was not found");
                Environment.Exit(-1);
            }
            else
            {
                analyzer.SetLibraryPath(opts.LibraryPath);
            }
            // Set input files.
            analyzer.InputFiles = new List <string>(opts.InputFileNames);

            // Analyze
            analyzer.Parse();

            // Fetch the root for program inspection
            IR.IRProgram program = analyzer.GetRoot();

            //************************************************************
            try
            {
                //*----- Lowercase short- and fullnames of all namespacs -----*//
                var lcProcessor = new LowerCaseNamespaces(program);
                program = lcProcessor.Process();

                if (opts.ManualPackagingFile.Length > 0)
                {
                    //*----- Manually move matching namespaces into another -----*//
                    var manualPackager = new ManualPackager(program, opts.ManualPackagingFile);
                    program = manualPackager.Process();
                }

                if (opts.ResolveCircDependancies)
                {
                    //*----- Searches and resolves circular dependancies -----*//
                    var depAnalyzer = new DependancyAnalyzer(program);
                    program = depAnalyzer.Process();
                }

                if (opts.AutomaticPackaging)
                {
                    //*----- Uses longest substring matching to group namespaces into common packages -----*//
                    var nsPackager = new AutoPackager(program);
                    program = nsPackager.Process();
                }

                if (opts.ResolveCollisions)
                {
                    //*----- Searches and resolves name collisions of various types -----*//
                    var ncAnalyzer = new NameCollisionAnalyzer(program);
                    program = ncAnalyzer.Process();
                }

                //************************************************************
            }
            catch (Exception e)
            {
                Log.Exception("Exception occurred while processing!", e);
                Environment.Exit(-8);
            }

            // Setup compiler
            DefaultProtoCompiler compiler = null;

            if (opts.Proto3Syntax == true)
            {
                compiler = new Proto3Compiler(program);
            }
            else
            {
                compiler = new Proto2Compiler(program);
            }

            if (!Directory.Exists(opts.OutDirectory))
            {
                // Generate full path for directory.
                string fullDirPath = Path.GetFullPath(opts.OutDirectory);
                // Create directory.
                Directory.CreateDirectory(fullDirPath);
                Log.Info("Created output directory: {0}", fullDirPath);
                // Update options.
                opts.OutDirectory = fullDirPath;
            }
            compiler.SetOutputPath(opts.OutDirectory);

            // Write output
            // All paths for created files are lowercased!
            compiler.Compile();

            Log.Info("Finished extracting");

            return(0);
        }