예제 #1
0
        static int Main(string[] args)
        {
            bool   errorInUsage     = false;
            bool   mgaIsGiven       = true;
            bool   generateTemplate = false;
            bool   formattedOutput  = false;
            string outputDirectory  = Path.Combine(Environment.CurrentDirectory, "output");
            string statFileName     = "sample_stats.json";
            string inputFilename    = string.Empty;

            Directory.CreateDirectory(outputDirectory);

            // verify the input arguments
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    switch (args[i].Substring(1))
                    {
                    case "p":
                    case "-prettyPrint":
                        formattedOutput = true;
                        break;

                    case "t":
                    case "-template":
                        generateTemplate = true;
                        break;

                    default:
                        errorInUsage = true;
                        break;
                    }
                }
                else
                {
                    inputFilename = args[i];

                    if (i < args.Length - 1)
                    {
                        Console.WriteLine("Arguments after filename are ignored.");
                    }

                    break;
                }
            }

            if (File.Exists(inputFilename))
            {
                if (Path.GetExtension(inputFilename).ToLowerInvariant() == ".mga")
                {
                    mgaIsGiven = true;
                }
                else if (Path.GetExtension(inputFilename).ToLowerInvariant() == ".xme")
                {
                    mgaIsGiven = false;
                }
                else
                {
                    errorInUsage = true;
                }
            }
            else
            {
                errorInUsage = true;
            }


            if (generateTemplate)
            {
                Statistics.Statistics stats = Process(null);
                SerializeStats(stats, Path.Combine(outputDirectory, statFileName), formattedOutput);
                return(0);
            }

            if (errorInUsage)
            {
                Console.WriteLine("Usage: {0} [arguments] inputfilename.[mga|xme]", Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location));
                Console.WriteLine("");
                Console.WriteLine("Arguments:");
                Console.WriteLine("  -p --prettyPrint   Indents the output json file.");
                Console.WriteLine("  -t --template      Generates a sample json file with random numbers.");
                return(1);
            }


            // copy the file to a temp directory
            string tempXmeFile = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(inputFilename) + ".xme");
            string tempMgaFile = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(inputFilename) + ".mga");

            statFileName = Path.GetFileNameWithoutExtension(inputFilename) + "_stat.json";

            MgaProject project = new MgaProject();

            if (mgaIsGiven)
            {
                if (inputFilename != tempMgaFile)
                {
                    Console.WriteLine("Copying MGA file to temp directory: {0}", tempMgaFile);
                    File.Copy(inputFilename, tempMgaFile, true);
                }

                Console.WriteLine("Opening project");
                int    mgaversion;
                string paradigmName;
                string paradigmVersion;
                object paradigmGUID;
                bool   ro_mode;

                project.QueryProjectInfo("MGA=" + tempMgaFile, out mgaversion, out paradigmName, out paradigmVersion, out paradigmGUID, out ro_mode);

                project.OpenEx("MGA=" + tempMgaFile, paradigmName, true);

                Console.WriteLine("Saving project as xme: {0}", tempXmeFile);
                // export to xme
                GME.MGA.Parser.MgaDumper dumper = new GME.MGA.Parser.MgaDumper();
                dumper.DumpProject(project, tempXmeFile);
            }
            else
            {
                if (inputFilename != tempXmeFile)
                {
                    Console.WriteLine("Copying XME file to temp directory: {0}", tempXmeFile);
                    File.Copy(inputFilename, tempXmeFile, true);
                }

                GME.MGA.Parser.MgaParser parser = new GME.MGA.Parser.MgaParser();
                string paradigmName;

                parser.GetXMLParadigm(tempXmeFile, out paradigmName);

                Console.WriteLine("Creating project");
                project.CreateEx("MGA=" + tempMgaFile, paradigmName, null);

                // import xme file
                Console.WriteLine("Importing project");

                parser.ParseProject(project, tempXmeFile);

                Console.WriteLine("Saving project as mga: {0}", tempMgaFile);
                project.Save("MGA=" + tempMgaFile);
            }

            if (project == null)
            {
                throw new Exception("Project is null.");
            }


            // process the project
            var statistics = Process(project);

            // read in file stats
            FileInfo mgaInfo = new FileInfo(tempMgaFile);
            FileInfo xmeInfo = new FileInfo(tempXmeFile);

            statistics.MgaSizeInBytes = mgaInfo.Length;
            statistics.XmeSizeInBytes = xmeInfo.Length;

            // serialize the object
            SerializeStats(statistics, Path.Combine(outputDirectory, statFileName), formattedOutput);


            return(0);
        }