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

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

            GenerateFileListFiltered(Path.Combine(ProjectFolder.GetRootDir(), ProjectFolder.buildScriptsDir), scriptFiles, filter);

            foreach (var script in scriptFiles)
            {
                GIMBuildObject buildInstructions = new GIMBuildObject();
                buildInstructions.DeserializeFromDisk(script);

                string pngFileLocation    = Path.Combine(ProjectFolder.GetRootDir(), buildInstructions.originalFileLocation);
                string targetFileLocation = Path.Combine(ProjectFolder.GetRootDir(), buildInstructions.targetFileLocation);

                counter++;

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

                    worker.ReportProgress((int)progress);
                }

                if (worker != null)
                {
                    if (worker.WorkerSupportsCancellation && worker.CancellationPending)
                    {
                        MessageBox.Show("Load images cancelled");
                        return;
                    }
                }

                string checksum;

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

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

                    br.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    continue; // this is primarily here to catch the odd "file that is supposed to exist in build script does not exist in image dir" problem. again, it is ideally something we will write to a log file at some point. user needs to be notified in some way.
                }

                if (checksum == buildInstructions.checksumValue)
                {
                    continue;
                }

                if (!ImageHandler.ConvertImage(pngFileLocation, targetFileLocation))
                {
                    continue;
                }
            }

            MessageBox.Show("Images loaded!");
        }
Beispiel #2
0
        private CPKBuildObject FilterCPKFile(FileLocationMeta file, string sourceDirectoryPath, string targetDirectoryPath)
        {
            var cpkFile  = new CPK(new Tools()); // this function gets a bit confusings since file, cpkFile and embeddedFile are all thrown around - i will need to fix that
            var filePath = Path.Combine(sourceDirectoryPath, file.subPath, file.fileName);

            if (!cpkFile.ReadCPK(filePath, ActiveEncodings.currentEncoding))
            {
                string errorMessage = string.Format("Unknown error while attempting to open {0}.", filePath);
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // this could be replaced with a custom form that allows the user to skip all errors or a simple "errors while opening X files" after the files are done being read. though, the later option would require a small restructuring of the code.
                Environment.Exit(1);
            }

            int realFileCount = 0;

            foreach (var embeddedFile in cpkFile.FileTable)
            {
                if (embeddedFile.FileType.ToString() == "FILE")
                {
                    realFileCount += 1;
                }
            }

            if (realFileCount == 0)
            {
                string errorMessage = string.Format("CPK file {0} was empty.", filePath); // i am not sure this should be a fatal error - i will attempt to come back to it once i have a more complete picture of how the build system works and thus have a better idea of how to handle such an eventuality
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(1);
            }

            CPKBuildObject cpkBuildInstructions = new CPKBuildObject();

            string originalFileLocation = Path.Combine(ProjectFolder.extractedISODir, file.subPath, file.fileName);

            cpkBuildInstructions.SetOriginalFileLocation(originalFileLocation);
            string targetFileLocation = Path.Combine(ProjectFolder.repackedGameFilesDir, file.subPath, file.fileName);

            cpkBuildInstructions.SetTargetFileLocation(targetFileLocation);


            if (realFileCount > 1) // if there is more than one file in the CPK we move the files within it to their own directory
            {
                string newSubDir  = Path.GetFileNameWithoutExtension(file.fileName);
                string newSubPath = Path.Combine(file.subPath, newSubDir);
                file.subPath = newSubPath;
            }

            foreach (var embeddedFile in cpkFile.FileTable)
            {
                var cpkMeta = new CPKEmbeddedFileMeta();

                if (embeddedFile.FileType != "FILE")
                {
                    continue; // skip headers etc.
                }

                if (FileParser.IsParseable(embeddedFile.FileName.ToString())) // use this to determine whether to unpack or not, not save location
                {
                    file.switchPath = editableDirectory;
                }
                else
                {
                    file.switchPath = rawDirectory;
                }

                string targetFileAbsolutePath = Path.Combine(targetDirectoryPath, ProjectFolder.unpackedGameFilesDir, file.subPath, embeddedFile.FileName.ToString());
                DirectoryGuard.CheckDirectory(targetFileAbsolutePath);

                byte[] fileAsBytes = GrabCPKData(filePath, embeddedFile);

                if (DebugSettings.ATTEMPT_DECOMPRESSION)
                {
                    if (fileAsBytes.Length >= 8) // 8 = length of "CRILAYLA"
                    {
                        byte[] crilaylaCheck = new byte[8];
                        Array.Copy(fileAsBytes, 0, crilaylaCheck, 0, 8);
                        string crilaylaString = Encoding.ASCII.GetString(crilaylaCheck);

                        if (crilaylaString == "CRILAYLA")
                        {
                            byte[] decompressedBytes = cpkFile.DecompressCRILAYLA(fileAsBytes, fileAsBytes.Length);
                            fileAsBytes = decompressedBytes;
                        }
                    }
                }

                if (DebugSettings.ALLOW_FILE_WRITES)
                {
                    FileStream   fs = new FileStream(targetFileAbsolutePath, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(fileAsBytes);

                    bw.Close();
                    fs.Close();
                }

                if (DebugSettings.COPY_UNPACKED_FILES)
                {
                    string secondTargetPath = Path.Combine(targetDirectoryPath, ProjectFolder.reassembledGameFilesDir, file.subPath, embeddedFile.FileName.ToString());
                    DirectoryGuard.CheckDirectory(secondTargetPath);

                    FileStream   fs = new FileStream(secondTargetPath, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(fileAsBytes);

                    bw.Close();
                    fs.Close();
                }

                string relativeFilePath = Path.Combine(file.subPath, embeddedFile.FileName.ToString());
                uint   fileID           = (uint)embeddedFile.ID;

                cpkMeta.filePath      = Path.Combine(ProjectFolder.reassembledGameFilesDir, relativeFilePath);
                cpkMeta.fileName      = embeddedFile.FileName.ToString();
                cpkMeta.checksumType  = Checksum.MD5;
                cpkMeta.checksumValue = Checksums.GetMD5(fileAsBytes);
                cpkMeta.ID            = fileID;

                cpkBuildInstructions.AddFile(fileID, cpkMeta);
            }

            cpkBuildInstructions.SerializeToDisk(Path.Combine(targetDirectoryPath, ProjectFolder.buildScriptsDir));

            return(cpkBuildInstructions);
        }
Beispiel #3
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);
                }
            }
        }