コード例 #1
0
ファイル: EasyPatch.cs プロジェクト: Dormanil/KPT
        private async Task PatchFileDeprecated(EmbeddedFileAccessor file, IEnumerable <ZipArchiveEntry> prebuiltFiles)
        {
            using (System.IO.StreamWriter logFile = new System.IO.StreamWriter(logFilePath, true))
            {
                logFile.WriteLine("Beginning work on file: " + file.name);
            }

            string fileName = Path.Combine(txtOutputPath.Text, file.name);

            DirectoryGuard.CheckDirectory(fileName);

            var prebuiltFile = prebuiltFiles.SingleOrDefault(fileEntry => String.Equals(fileEntry.FullName.Substring(prebuiltFiles.First().FullName.IndexOf("PSP_GAME")), file.name.Replace("\\", "/"), StringComparison.OrdinalIgnoreCase));

            if (prebuiltFile == null)
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                file.dataStream.CopyTo(fs);
                fs.Close();
            }
            else
            {
                prebuiltFile.ExtractToFile(fileName);
            }

            using (System.IO.StreamWriter logFile = new System.IO.StreamWriter(logFilePath, true))
            {
                logFile.WriteLine("Completed work on file: " + file.name);
            }

            this.filesProcessed++;

            //Update Progress Bar
            PatchProgressBar.Value = (int)((this.filesProcessed / this.totalFiles) * 100);
        }
コード例 #2
0
        public static bool DumpISO(string isoFileName)
        {
            var    isoReader    = new ISOReader();
            string isoOutputDir = Path.Combine(rootDir, extractedISODir);

            if (!isoReader.OpenISOStream(isoFileName, isoOutputDir))
            {
                return(false); // ISOReader itself should take care of showing a detailed error message
            }

            DirectoryGuard.CheckDirectory(isoOutputDir);

            var isoFiles = isoReader.GetGenerator();

            foreach (var file in isoFiles)
            {
                string fileName = Path.Combine(isoOutputDir, file.name);

                DirectoryGuard.CheckDirectory(fileName);

                FileStream fs = new FileStream(fileName, FileMode.Create);

                file.dataStream.CopyTo(fs);

                fs.Close();
            }

            isoReader.CloseISOStream();

            return(true);
        }
コード例 #3
0
ファイル: EasyPatch.cs プロジェクト: Dormanil/KPT
        private async Task PatchGameDeprecated()
        {
            ZipArchive patchDirectory = ZipFile.OpenRead(txtPatchPath.Text);

            var isoReader = new ISOReader();

            if (!isoReader.OpenISOStream(txtIsoPath.Text, txtOutputPath.Text))
            {
                return; // ISOReader itself should take care of showing a detailed error message
            }

            DirectoryGuard.CheckDirectory(txtOutputPath.Text);

            IEnumerable <ZipArchiveEntry> prebuiltFiles = patchDirectory.Entries.Where(patchEntry => patchEntry.Name.EndsWith(".cpk") || patchEntry.Name.EndsWith(".SFO"));

            var isoFiles = isoReader.GetGenerator();

            try
            {
                filesProcessed = 0;
                totalFiles     = (double)isoFiles.Count();

                //First We Dump the Iso and make list of files in need of patching.

                List <Task> tasks = isoFiles.Select(file => PatchFileDeprecated(file, prebuiltFiles)).ToList();

                await Task.WhenAll(tasks);

                MessageBox.Show("That was easy.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an issue. Please send the log located at " + logFilePath + " to Spud and yell at him to fix things.");
                using (System.IO.StreamWriter logFile = new System.IO.StreamWriter(logFilePath, true))
                {
                    logFile.WriteLine("Exception hit:\n" + ex.ToString());
                }
            }
            finally
            {
                isoReader.CloseISOStream();

                //Re-Enable text boxes
                txtIsoPath.Enabled    = true;
                btnIsoPath.Enabled    = true;
                txtPatchPath.Enabled  = true;
                btnPatchPath.Enabled  = true;
                txtOutputPath.Enabled = true;
                btnOutputPath.Enabled = true;

                System.Timers.Timer renablePatchTier = new System.Timers.Timer(2000);
                renablePatchTier.Elapsed  += ReenablePatchButton;
                renablePatchTier.AutoReset = false;
                renablePatchTier.Enabled   = true;
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: shindouj/KPT
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            ActiveEncodings.Initalize();
            DirectoryGuard.Initalize();

            Application.Run(new Form1());
            Environment.Exit(0);
        }
コード例 #5
0
        /// <summary>
        /// Go through a directory and split it into an "Original" directory with a copy of the original files and an "Editble" directory with processed files that can be edited by users
        /// </summary>
        /// <param name="sourceDirectoryPath">The path of the directory to be processed</param>
        /// <param name="targetDirectoryPath">The path of the directory to store the processed contents</param>
        /// <returns>True is succesfully processed, false if processing failed</returns>
        public bool ProcessDirectory(string sourceDirectoryPath)
        {
            string rootDir = sourceDirectoryPath;                                       // not compliant with the ProjectFolder structure

            sourceDirectoryPath = Path.Combine(rootDir, ProjectFolder.extractedISODir); // now that we know it is an inited directory we start working with the ISO files

            if (!IsExpectedISODirectory(sourceDirectoryPath))
            {
                return(false);
            }

            List <FileLocationMeta> fileList = new List <FileLocationMeta>();

            if (!PopulateFileList(sourceDirectoryPath, fileList))
            {
                return(false);
            }

            string editableFilesDirectory = Path.Combine(ProjectFolder.GetRootDir(), ProjectFolder.editableGameFiesDir);

            if (!DebugSettings.SKIP_ORIGINAL_DIRECTORY_COPYING) // needs to test if dir already exists / handle exception
            {
                string targetDir = Path.Combine(rootDir, ProjectFolder.repackedGameFilesDir);
                DirectoryGuard.CheckDirectory(targetDir);
                Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(sourceDirectoryPath, targetDir, true); // i would like "the program has not crashed" progress box but i'm not sure it can be done if things are done this way. hmm. perhaps writing a recursive copying function would be best after all.
            }

            foreach (var file in fileList)
            {
                string fileExtension = Path.GetExtension(file.fileName);
                fileExtension = fileExtension.ToLower();
                switch (fileExtension)
                {
                case ".cpk":
                    FilterCPKFile(file, sourceDirectoryPath, rootDir);
                    break;

                default:
                    break;
                }
            }

            return(true);
        }
コード例 #6
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);
        }