Exemple #1
0
        private void Process()
        {
            // Validate all files are in the same directory.
            var allFiles    = this.nonBlockingTextBox.Lines.Union(this.blockingTextBox.Lines).Union(this.threatTextBox.Lines).Where(f => f != "+" && f != "-");
            var directories = allFiles.Select(f => new FileInfo(f).Directory.ToString()).Distinct();

            if (directories.Count() > 1)
            {
                throw new Exception("All input files should be in the same directory");
            }

            // Validate no files are called the same as the intermediate files.
            var directory          = directories.First();
            var nonBlockingFile    = directory + "\\_nonblocking_gs.png";
            var blockingFile       = directory + "\\_blocking_gs.png";
            var threatFile         = directory + "\\_threat_gs.png";
            var nonBlockingFileOld = directory + "\\_nonblocking_gs_old.png";
            var blockingFileOld    = directory + "\\_blocking_gs_old.png";
            var threatFileOld      = directory + "\\_threat_gs_old.png";

            var allIntermediate   = new[] { nonBlockingFile, blockingFile, threatFile, nonBlockingFileOld, blockingFileOld, threatFileOld };
            var intermediateInfos = allIntermediate.Select(f => new FileInfo(f).FullName);

            if (allFiles.Any(f => intermediateInfos.Contains(new FileInfo(f).FullName)))
            {
                throw new Exception("One input file called like the intermediate ouputs");
            }

            // Create all collections.
            var nonBlockingAll = new List <MyBitmap>();
            var blockingAll    = new List <MyBitmap>();
            var threatAll      = new List <MyBitmap>();
            var nonBlockingOld = new List <MyBitmap>();
            var blockingOld    = new List <MyBitmap>();
            var threatOld      = new List <MyBitmap>();

            Action <string[], List <MyBitmap>, List <MyBitmap> > createCollections = (lines, all, old) =>
            {
                var minusFound = false;
                var plusFound  = false;
                foreach (var line in lines)
                {
                    if (line == "-")
                    {
                        minusFound = true;
                        continue;
                    }

                    if (minusFound)
                    {
                        if (line == "+")
                        {
                            throw new Exception("+ after -");
                        }

                        old.Add(MyBitmap.FromFileWithParams(line));
                    }
                    else
                    {
                        if (line == "+")
                        {
                            plusFound = true;
                            continue;
                        }

                        if (!plusFound)
                        {
                            old.Add(MyBitmap.FromFileWithParams(line));
                        }

                        all.Add(MyBitmap.FromFileWithParams(line));
                    }
                }
            };

            createCollections(this.nonBlockingTextBox.Lines, nonBlockingAll, nonBlockingOld);
            createCollections(this.blockingTextBox.Lines, blockingAll, blockingOld);
            createCollections(this.threatTextBox.Lines, threatAll, threatOld);

            // Create intermediate files.
            Action <IEnumerable <MyBitmap>, string> createIntermediate = (bitmaps, targetFile) =>
            {
                if (!bitmaps.Any())
                {
                    return;
                }

                foreach (var bitmap in bitmaps)
                {
                    bitmap.MakeNesGreyscale();
                }

                var result = CreateSingleBitmap(bitmaps);
                result.ToBitmap().Save(targetFile);
            };

            createIntermediate(nonBlockingAll, nonBlockingFile);
            createIntermediate(blockingAll, blockingFile);
            createIntermediate(threatAll, threatFile);
            createIntermediate(nonBlockingOld, nonBlockingFileOld);
            createIntermediate(blockingOld, blockingFileOld);
            createIntermediate(threatOld, threatFileOld);

            // Validate level files.
            var lvlFiles = this.lvlFilesTextBox.Lines.Where(l => !string.IsNullOrWhiteSpace(l)).ToArray();

            foreach (var lvl in lvlFiles)
            {
                if (!File.Exists(lvl))
                {
                    throw new Exception($"Lvl {lvl} doesn't exist");
                }
            }

            // Process everything.
            BackgroundConfig config;
            List <MyBitmap>  sprites;
            Dictionary <string, MyBitmap> tilesWithImages;

            GetConfigAndSprites(nonBlockingFile, blockingFile, threatFile, true, out config, out sprites, out tilesWithImages);

            if (lvlFiles.Any())
            {
                Dictionary <string, MyBitmap> tilesWithImagesOld = null;
                BackgroundConfig ignore1;
                List <MyBitmap>  ignore2;
                GetConfigAndSprites(nonBlockingFileOld, blockingFileOld, threatFileOld, false, out ignore1, out ignore2, out tilesWithImagesOld);
                ProcessLvlFiles(lvlFiles, tilesWithImages, tilesWithImagesOld);
            }

            GenerateChrImageAndSaveFiles(config, sprites);

            if (File.Exists(nonBlockingFileOld))
            {
                File.Delete(nonBlockingFileOld);
            }

            if (File.Exists(blockingFileOld))
            {
                File.Delete(blockingFileOld);
            }

            if (File.Exists(threatFileOld))
            {
                File.Delete(threatFileOld);
            }
        }