Example #1
0
        public void DumpImages(object sender, EventArgs e)
        {
            if (sender is BackgroundWorker)
            {
                worker = sender as BackgroundWorker;
            }
            else
            {
                worker = null;
            }

            List <string> imageFiles = new List <string>();
            string        filter     = ".gim";
            int           counter    = 0; // used to add a number to each image name, just in case disambiguation is required

            GenerateFileListFiltered(Path.Combine(ProjectFolder.GetRootDir(), ProjectFolder.unpackedGameFilesDir), imageFiles, filter);

            foreach (var file in imageFiles)
            {
                string fileName              = Path.GetFileNameWithoutExtension(file);
                string targetFilePath        = Path.Combine(ImageHandler.GetImagesDir(), fileName);
                string targetFilePathPostFix = string.Format("_{0}.png", counter.ToString());
                targetFilePath += targetFilePathPostFix;

                counter++;



                if (worker != null)
                {
                    if (worker.WorkerSupportsCancellation && worker.CancellationPending)
                    {
                        return;
                    }
                }

                if (!ImageHandler.ConvertImage(file, targetFilePath))
                {
                    continue;
                }

                var gimBuildInstructions = new GIMBuildObject();
                gimBuildInstructions.originalFileLocation = Path.Combine(ProjectFolder.editableGameFiesDir, ImageHandler.imagesDir, Path.GetFileName(targetFilePath));
                gimBuildInstructions.targetFileLocation   = Path.Combine(ProjectFolder.reassembledGameFilesDir, ProjectFolder.GetSubPath(file, Path.Combine(ProjectFolder.GetRootDir(), ProjectFolder.unpackedGameFilesDir))); // this is a bad line
                gimBuildInstructions.checksumType         = Checksum.MD5;

                try
                {
                    FileStream   fs = new FileStream(targetFilePath, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);

                    byte[] imageData = br.ReadBytes((int)br.BaseStream.Length);
                    gimBuildInstructions.checksumValue = Checksums.GetMD5(imageData);

                    br.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    continue;
                }

                gimBuildInstructions.SerializeToDisk(Path.Combine(ProjectFolder.GetRootDir(), ProjectFolder.buildScriptsDir, Path.GetFileName(targetFilePath)));

                if (worker != null)
                {
                    double progress = ((double)counter / (double)imageFiles.Count) * 100;

                    worker.ReportProgress((int)progress);
                }
            }
        }