Esempio n. 1
0
        private static void SingleImport(string imgDir, string outputDir,
                                         List <ImageInfo> importedList, bool filterDate)
        {
            int count = 0;
            Dictionary <string, int> errors = new Dictionary <string, int>();

            errors.Add("errorPack", 0);    errors.Add("errorImgs", 0);
            errors.Add("noN2DPack", 0);    errors.Add("noN2DImgs", 0);
            errors.Add("noModPack", 0);    errors.Add("noModImgs", 0);
            errors.Add("inListPack", 0);    errors.Add("inListImgs", 0);
            errors.Add("noSuffPack", 0);    errors.Add("noSuffImgs", 0);

            // Search image: Group of images with same prefix ordered by frame index.
            Dictionary <string, SortedList <int, ImageInfo> > imageGroups =
                SearchImages(imgDir, outputDir);

            // Import!
            Console.WriteLine("Starting importing...");
            foreach (string relative in imageGroups.Keys)
            {
                // Get paths
                IList <ImageInfo> infos    = imageGroups[relative].Values;
                string            outFile  = outputDir + infos[0].RelativeNewPack;
                string            oriFile  = outputDir + infos[0].RelativePack;
                string[]          imgs     = infos.Select(i => i.AbsoluteImage).ToArray();
                int[]             frameIdx = infos.Select(i => i.FrameIndex).ToArray();
                //string[] internalNames = infos.Select(i => i.InternalName).ToArray();

                // If don't match the filter, skip
                if (filterDate)
                {
                    DateTime referenceDate = File.GetLastWriteTime(outFile);
                    if (!imgs.Any(f => File.GetLastWriteTime(f) > referenceDate))
                    {
                        Console.WriteLine("|+ Skipped (date filter) {0}", relative);
                        errors["noModPack"]++;
                        errors["noModImgs"] += imgs.Length;
                        continue;
                    }
                }

                // Check if it has been already imported
                if (importedList.Any(i => i.RelativeImage == relative))
                {
                    Console.WriteLine("|+ Skipped (already imported) {0}", relative);
                    errors["inListPack"]++;
                    errors["inListImgs"] += imgs.Length;
                    continue;
                }

                // If original file does not exist, skip
                // Odd way to import manually images and skip them here
                if (!File.Exists(oriFile) && File.Exists(outFile))
                {
                    Console.WriteLine("|+ Skipped (manual mode) {0}", relative);
                    errors["noN2DPack"]++;
                    errors["noN2DImgs"] += imgs.Length;
                    continue;
                }

                // If the original file does not exists AND there isn't any manual import
                if (!File.Exists(oriFile))
                {
                    Console.WriteLine("|+ Skipped (invalid suffix) {0}", relative);
                    errors["noSuffPack"]++;
                    errors["noSuffImgs"] += imgs.Length;
                    continue;
                }

                // Try to import
                Console.Write("|-Importing {0,-45} {1,2} | ", relative, imgs.Length);
                try {
                    Npck ori = new Npck(oriFile);
                    Npck npck;
                    if (ori.IsSprite)
                    {
                        npck = NpckFactory.FromSpriteImage(imgs, frameIdx, ori);
                    }
                    else if (ori.IsBackground && imgs.Length == 1)
                    {
                        npck = NpckFactory.FromBackgroundImage(imgs[0], ori);
                    }
                    else if (ori.IsTexture)
                    {
                        npck = NpckFactory.ChangeTextureImages(imgs, frameIdx, ori);
                    }
                    else
                    {
                        throw new FormatException("Image format not supported");
                    }

                    npck.Write(outFile);
                    npck.CloseAll();
                    ori.CloseAll();
                } catch (Exception ex) {
                    Console.WriteLine("Error: {0}", ex.Message);
                                        #if DEBUG
                    Console.WriteLine(ex.ToString());
                                        #endif
                    errors["errorPack"]++;
                    errors["errorImgs"] += imgs.Length;
                    continue;
                }

                count += imgs.Length;
                importedList.AddRange(infos);
                Console.WriteLine("Successfully");
            }

            Console.WriteLine();
            Console.WriteLine("# Statistics #");
            Console.WriteLine("\tErrors in {0} packages ({1} images)",
                              errors["errorPack"], errors["errorImgs"]);
            Console.WriteLine("\tNo N2D file found for {0} packages ({1} images)",
                              errors["noN2DPack"], errors["noN2DImgs"]);
            Console.WriteLine("\tInvalid file suffix for {0} packages ({1} images)",
                              errors["noSuffPack"], errors["noSuffImgs"]);
            Console.WriteLine("\tFilter skipped {0} packages ({1} images)",
                              errors["noModPack"], errors["noModImgs"]);
            Console.WriteLine("\tAlready imported {0} packages ({1} images)",
                              errors["inListPack"], errors["inListImgs"]);
            Console.WriteLine("\tImported {0} images successfully!", count);
        }
Esempio n. 2
0
        private static void MultiImport(string baseDir, string outputDir,
                                        List <ImageInfo> importedList, string xml, bool filterDate)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return;
            }

            XDocument doc = XDocument.Load(xml);

            foreach (XElement entry in doc.Root.Elements("Pack"))
            {
                // Get mode
                string mode = entry.Element("Mode").Value;

                // Get paths
                bool             existImages = true;
                List <string>    images      = new List <string>();
                List <ImageInfo> infos       = new List <ImageInfo>();
                foreach (XElement ximg in entry.Element("Images").Elements("Image"))
                {
                    string frame = (ximg.Attribute("frame") != null) ?
                                   "_" + ximg.Attribute("frame").Value : "";

                    ImageInfo info = new ImageInfo();
                    info.AbsoluteImage = Path.Combine(baseDir, ximg.Value) + frame + M + ".png";
                    info.Filename      = Path.GetFileNameWithoutExtension(ximg.Value);
                    info.PackExtension = (mode != "TextureWithPalette") ? ".n2d" : ".n3d";
                    info.RelativePath  = Path.GetDirectoryName(ximg.Value) + Path.DirectorySeparatorChar;
                    infos.Add(info);

                    images.Add(info.AbsoluteImage);

                    if (!File.Exists(info.AbsoluteImage))
                    {
                        existImages = false;
                        break;
                    }
                }

                // Import
                Console.Write("|-Importing {0,-45} | ", infos[0].RelativeImage);
                for (int i = 1; i < infos.Count; i++)
                {
                    Console.WriteLine();
                    Console.Write("            {0,-45} | ", infos[i].RelativeImage);
                }

                // Images still not translated
                if (!existImages)
                {
                    Console.WriteLine("Skipped: Images not found");
                    continue;
                }

                // If don't match the filter, skip
                if (filterDate)
                {
                    DateTime referenceDate = File.GetLastWriteTime(outputDir + infos[0].RelativeNewPack);
                    if (!images.Any(f => File.GetLastWriteTime(f) > referenceDate))
                    {
                        Console.WriteLine("Skipped: date filter");
                        continue;
                    }
                }

                Npck   originalPack = new Npck(outputDir + infos[0].RelativePack);
                Npck[] packs        = null;
                if (mode == "SharePalette")
                {
                    packs = NpckFactory.FromBackgroundImageSharePalette(images.ToArray(), originalPack);
                }
                else if (mode == "ChangePalette")
                {
                    packs = new Npck[1] {
                        NpckFactory.FromBackgroundImage(images[0], originalPack, true)
                    }
                }
                ;
                else if (mode == "ShareImage")
                {
                    packs = NpckFactory.FromBackgroundImageShareImage(images.ToArray(), originalPack);
                }
                else if (mode == "SharePaletteChangeDepth")
                {
                    packs = NpckFactory.FromBackgroundImageSharePaletteChangeDepth(images.ToArray(), originalPack, true);
                }
                else if (mode == "TextureWithPalette")
                {
                    // Get frames
                    string     frame  = entry.Element("Images").Elements("Image").First().Attribute("frame").Value;
                    List <int> frames = new List <int>()
                    {
                        Convert.ToInt32(frame)
                    };

                    // Create palette
                    XElement[] xcolors = entry.Element("Palette").Elements("Color").ToArray();
                    Color[]    colors  = new Color[xcolors.Length];
                    for (int i = 0; i < colors.Length; i++)
                    {
                        colors[i]       = new Color();
                        colors[i].Red   = Convert.ToInt32(xcolors[i].Attribute("red").Value);
                        colors[i].Green = Convert.ToInt32(xcolors[i].Attribute("green").Value);
                        colors[i].Blue  = Convert.ToInt32(xcolors[i].Attribute("blue").Value);
                    }
                    Palette palette = new Palette(colors);

                    // Generate pack file
                    packs    = new Npck[1];
                    packs[0] = NpckFactory.ChangeTextureImages(images.ToArray(), frames.ToArray(), palette, originalPack);
                }
                else
                {
                    throw new FormatException(string.Format("Unsopported mode \"{0}\"", mode));
                }

                // Write output
                originalPack.CloseAll();
                for (int i = 0; i < infos.Count; i++)
                {
                    packs[i].Write(outputDir + infos[i].RelativeNewPack);
                    packs[i].CloseAll();
                }

                importedList.AddRange(infos);
                Console.WriteLine("Successfully");
            }
        }