Example #1
0
        private void RunForFile(Stream currentInputStream, string currentInputPath, string currentOutputPath, bool keepOutputPath)
        {
            if (Mode == GxExpanderMode.Unpack && GetDotExtension(currentInputPath) == "lz")
            {
                // Unpack .LZ file
                using (Stream outputStream = new MemoryStream())
                {
                    Lz.Unpack(currentInputStream, outputStream, Game);
                    outputStream.Position = 0;

                    // Need to continue unpacking for .arc.lz
                    RunForFile(outputStream, DotExtToCommaExt(currentInputPath),
                               !keepOutputPath ? DotExtToCommaExt(currentOutputPath) : currentOutputPath, keepOutputPath);
                }
            }
            else if (Mode == GxExpanderMode.Pack && GetCommaExtension(currentInputPath) == "lz")
            {
                // Pack .LZ file
                using (Stream outputStream = new MemoryStream())
                {
                    Lz.Pack(currentInputStream, outputStream, Game);
                    outputStream.Position = 0;

                    RunForFile(outputStream, CommaExtToDotExt(currentInputPath),
                               !keepOutputPath ? CommaExtToDotExt(currentOutputPath) : currentOutputPath, keepOutputPath);
                }
            }
            else if (Mode == GxExpanderMode.Unpack && GetDotExtension(currentInputPath) == "arc")
            {
                // Unpack .ARC container

                ArcContainer arc = new ArcContainer(currentInputStream);
                arc.Extract(!keepOutputPath ? DotExtToCommaExt(currentOutputPath) : currentOutputPath);

                // No need to continue unpacking inside .arc files
            }
            else
            {
                // Copy the file as-is without change
                using (FileStream currentOutputStream = File.Create(currentOutputPath))
                {
                    currentInputStream.CopyTo(currentOutputStream);
                }
            }
        }
Example #2
0
        private void RunForDirectory(DirectoryInfo currentInputDirectory, string currentInputPath, string currentOutputPath, bool keepOutputPath)
        {
            if (Mode == GxExpanderMode.Pack && GetCommaExtension(currentInputPath) == "arc")
            {
                // Pack directory to .ARC file
                using (MemoryStream outputStream = new MemoryStream())
                {
                    ArcContainer arc = new ArcContainer(currentInputDirectory.FullName);
                    arc.Save(outputStream);
                    outputStream.Position = 0;

                    // Need to continue packing for ,lz,arc
                    RunForFile(outputStream, CommaExtToDotExt(currentInputPath),
                               !keepOutputPath ? CommaExtToDotExt(currentOutputPath) : currentOutputPath, keepOutputPath);
                }
            }
            else
            {
                // Continue recursively packing/unpacking the directory

                // Recreate the directory on the output
                Directory.CreateDirectory(currentOutputPath);

                // Iterate recursively through all files in the directory
                foreach (FileSystemInfo currentSubInput in currentInputDirectory.GetFileSystemInfos())
                {
                    RunRecursive(currentSubInput, Path.Combine(currentInputPath, currentSubInput.Name),
                                 Path.Combine(currentOutputPath, currentSubInput.Name), false);

                    // Allow the user to cancel the process
                    if (BackgroundWorker != null && BackgroundWorker.CancellationPending)
                    {
                        return;
                    }
                }
            }
        }
Example #3
0
        private void backgroundWorkerRunTests_DoWork(object sender, DoWorkEventArgs e)
        {
            RunTestsWorkerParams args = (RunTestsWorkerParams)e.Argument;

            // Find the list of files to process in the input directory
            string[] fileNames = Directory.EnumerateFiles(args.inputPath, "*.*", SearchOption.AllDirectories).Where(fn =>
                                                                                                                    (args.runLzTest && fn.EndsWith(".lz")) ||
                                                                                                                    (args.runArcTest && fn.EndsWith(".arc")) ||
                                                                                                                    (args.runTplTest && fn.EndsWith(".tpl")) ||
                                                                                                                    (args.runGmaTest && fn.EndsWith(".gma"))).ToArray();

            // Process each file
            int nErrors = 0;

            for (int i = 0; i < fileNames.Length; i++)
            {
                string fileName = fileNames[i];

                byte[] originalFileData = File.ReadAllBytes(fileName);

                try
                {
                    // Load and resave the file using the appropiate class
                    MemoryStream originalStream = new MemoryStream(originalFileData);
                    MemoryStream resavedStream  = new MemoryStream();

                    if (args.runLzTest && fileName.EndsWith(".lz"))
                    {
                        MemoryStream decodedStream = new MemoryStream();
                        Lz.Unpack(originalStream, decodedStream, args.game);
                        decodedStream.Position = 0;
                        Lz.Pack(decodedStream, resavedStream, args.game);
                    }
                    else if (args.runArcTest && fileName.EndsWith(".arc"))
                    {
                        ArcContainer arc = new ArcContainer(originalStream);
                        arc.Save(resavedStream);
                    }
                    else if (args.runTplTest && fileName.EndsWith(".tpl"))
                    {
                        Tpl tpl = new Tpl(originalStream, args.game);
                        tpl.Save(resavedStream, args.game);
                    }
                    else if (args.runGmaTest && fileName.EndsWith(".gma"))
                    {
                        Gma gma = new Gma(originalStream, args.game);
                        gma.Save(resavedStream, args.game);
                    }

                    // Check that the resaved file is equal to the original file
                    byte[] resavedFileData = resavedStream.ToArray();

                    if (!ByteArrayUtils.ByteArrayDataEquals(originalFileData, resavedFileData))
                    {
                        if (args.saveDiffErrors)
                        {
                            File.WriteAllBytes(fileName + "-error", resavedFileData);
                        }

                        throw new Exception("The resaved file is different than the original file.\n");
                    }
                }
                catch (Exception ex)
                {
                    txtLog.Invoke(new Action(() => txtLog.AppendText(
                                                 string.Format("{0}: {1}\n", fileName, ex.Message))));
                    nErrors++;
                }

                backgroundWorkerRunTests.ReportProgress(i * 100 / fileNames.Length);
                if (backgroundWorkerRunTests.CancellationPending)
                {
                    return;
                }
            }

            backgroundWorkerRunTests.ReportProgress(100);
            txtLog.Invoke(new Action(() => txtLog.AppendText(
                                         string.Format("Test finished. {0} tested, {1} errors.\n", fileNames.Length, nErrors))));
        }