Example #1
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 #2
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))));
        }