Пример #1
0
        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.DefaultExt      = ".dat";
            dialog.CheckFileExists = true;
            dialog.Filter          = "DAT files (*.dat)|*.dat";

            string fileName;

            if (dialog.ShowDialog(this) == true)
            {
                fileName = dialog.FileName;
            }
            else
            {
                return;
            }

            this.RunBusyAction(disp =>
            {
                try
                {
                    var dat = DatFile.FromFile(fileName);
                    disp(() => this.DatFile = dat);
                }
                catch (Exception ex)
                {
                    disp(() => MessageBox.Show(this, fileName + "\n" + ex.Message, this.Title, MessageBoxButton.OK, MessageBoxImage.Error));
                }
            });
        }
        private void LoadResdataPlanets(string fileName)
        {
            this._backdropModels.Clear();

            string basePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(fileName), System.IO.Path.GetFileNameWithoutExtension(fileName));

            var lines = XwaHooksConfig.GetFileLines(basePath + "_Resdata.txt");

            if (lines.Count == 0)
            {
                lines = XwaHooksConfig.GetFileLines(basePath + ".ini", "Resdata");
            }

            foreach (string line in XwaHooksConfig.GetFileLines(AppSettings.WorkingDirectory + "Resdata.txt"))
            {
                lines.Add(line);
            }

            var planetGroupIds = AppSettings.ExePlanets.Select(t => t.DataIndex1).ToList();

            foreach (string line in lines)
            {
                DatFile dat = DatFile.FromFile(AppSettings.WorkingDirectory + line);

                foreach (DatImage image in dat.Images)
                {
                    if (!planetGroupIds.Contains(image.GroupId))
                    {
                        continue;
                    }

                    string key = image.GroupId + ", " + image.ImageId;

                    if (this._backdropModels.ContainsKey(key))
                    {
                        continue;
                    }

                    this._backdropModels.Add(key, new BackdropModel(image));
                }
            }
        }
Пример #3
0
        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Title           = "Select a folder";
            dialog.DefaultExt      = ".dat";
            dialog.CheckFileExists = true;
            dialog.Filter          = "DAT files (*.dat)|*.dat";

            string directory;

            if (dialog.ShowDialog(this) == true)
            {
                directory = System.IO.Path.GetDirectoryName(dialog.FileName);
            }
            else
            {
                return;
            }

            this.DataContext = null;

            this.RunBusyAction(disp =>
            {
                try
                {
                    var datFiles = System.IO.Directory.EnumerateFiles(directory, "*.DAT")
                                   .Select(file => DatFile.FromFile(file))
                                   .ToDictionary(
                        t => System.IO.Path.GetFileNameWithoutExtension(t.FileName).ToUpperInvariant(),
                        t => t);

                    disp(() => this.DataContext = datFiles);
                }
                catch (Exception ex)
                {
                    disp(() => MessageBox.Show(this, ex.Message, this.Title, MessageBoxButton.OK, MessageBoxImage.Error));
                }
            });
        }
Пример #4
0
        public static bool LoadDATFile([MarshalAs(UnmanagedType.LPStr)] string sDatFileName)
        {
            // First, let's check if this DAT file is already loaded:
            if (m_DATFile != null && m_DATFile.FileName.ToLower().Equals(sDatFileName.ToLower()))
            {
                if (m_Verbose)
                {
                    Trace.WriteLine("[DBG] [C#] DAT File " + sDatFileName + " already loaded");
                }
                return(true);
            }

            if (m_Verbose)
            {
                Trace.WriteLine("[DBG] [C#] Loading File: " + sDatFileName);
            }
            try
            {
                m_DATImage = null; // Release any previous instances
                m_DATFile  = null; // Release any previous instances
                m_DATFile  = DatFile.FromFile(sDatFileName);
                if (m_DATFile == null)
                {
                    if (m_Verbose)
                    {
                        Trace.WriteLine("[DBG] [C#] Failed when loading: " + sDatFileName);
                    }
                    return(false);
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine("[DBG] [C#] Exception " + e + " when opening " + sDatFileName);
                m_DATFile = null;
            }
            return(true);
        }
Пример #5
0
        /*
         * Main entry point. Call this with a valid DAT file name to create a ZIP file.
         * Returns true if the conversion was successful.
         */
        public static bool ConvertDATFile(string sDATFileName)
        {
            DatFile m_DATFile = null;
            bool    bResult   = false;

            try
            {
                m_DATFile = DatFile.FromFile(sDATFileName);

                // Generate a new temporary path to make the conversion
                m_sTempDirectory = Path.Combine(Path.GetDirectoryName(sDATFileName),
                                                Path.GetFileNameWithoutExtension(sDATFileName)) + "_dat_to_zip\\";

                // If the temporary directory exists, delete it
                if (Directory.Exists(m_sTempDirectory))
                {
                    Directory.Delete(m_sTempDirectory, true);
                }
                // Create the temporary directory
                Directory.CreateDirectory(m_sTempDirectory);

                // Iterate over each group and dump all its images
                foreach (var Group in m_DATFile.Groups)
                {
                    string sSubDir = Path.Combine(m_sTempDirectory, Group.GroupId.ToString());
                    Directory.CreateDirectory(sSubDir);
                    Console.WriteLine("Processing Group: " + Group.GroupId);

                    // Flip and dump each image in the current group
                    foreach (var Image in Group.Images)
                    {
                        // For some reason, we still have to flip the images upside down before saving
                        // them.
                        // Convert all images to Format25, that way the flipping algorithm is the same for
                        // all formats.
                        bool bHasAlpha = false;
                        Image.ConvertToFormat25();
                        short  W = Image.Width;
                        short  H = Image.Height;
                        byte[] data = Image.GetImageData();
                        byte[] data_out = new byte[data.Length];
                        UInt32 OfsOut = 0, OfsIn = 0, RowStride = (UInt32)W * 4, RowOfs = (UInt32)(H - 1) * RowStride;
                        //Console.Write("Image: " + Group.GroupId + "-" + Image.ImageId + ", (W,H): (" + W + ", " + H + "), ");
                        //Console.WriteLine("Format: " + Image.Format + ", ColorCount: " + Image.ColorsCount);

                        for (int y = 0; y < H; y++)
                        {
                            OfsIn = RowOfs; // Flip the image
                            for (int x = 0; x < W; x++)
                            {
                                data_out[OfsOut + 0] = data[OfsIn + 0]; // B
                                data_out[OfsOut + 1] = data[OfsIn + 1]; // G
                                data_out[OfsOut + 2] = data[OfsIn + 2]; // R
                                data_out[OfsOut + 3] = data[OfsIn + 3]; // A
                                if (data_out[OfsOut + 3] < 255)
                                {
                                    bHasAlpha = true;
                                }
                                OfsIn  += 4;
                                OfsOut += 4;
                            }
                            // Flip the image and prevent underflows:
                            RowOfs -= RowStride;
                            if (RowOfs < 0)
                            {
                                RowOfs = 0;
                            }
                        }
                        Console.WriteLine("\tSaving Image: " + Group.GroupId + "-" + Image.ImageId);
                        //Console.WriteLine("\tOutput: " + Path.Combine(sSubDir, Image.ImageId.ToString()) + ".jpg");

                        // The gamma correction is applied in FromByteArray
                        Bitmap new_image = FromByteArray(data_out, W, H);
                        if (m_bConvertToJpeg && !bHasAlpha)
                        {
                            Console.WriteLine("Image " + Group.GroupId + "-" + Image.ImageId + " has no transparency. Saving as Jpeg");
                            new_image.Save(Path.Combine(sSubDir, Image.ImageId.ToString()) + ".jpg", ImageFormat.Jpeg);
                        }
                        else
                        {
                            new_image.Save(Path.Combine(sSubDir, Image.ImageId.ToString()) + ".png", ImageFormat.Png);
                        }
                        new_image.Dispose(); new_image = null;

                        data = null; data_out = null;
                    }
                }

                // ZIP the new directories into a new ZIP file
                MakeZIPFile(m_sTempDirectory, sDATFileName);

                // Erase the temporary directory
                if (!m_bKeepTempDirectory)
                {
                    Directory.Delete(m_sTempDirectory, true);
                }
                else
                {
                    Console.WriteLine("Kept temporary directory: " + m_sTempDirectory);
                }
                bResult = true;
            }
            catch (Exception e)
            {
                m_sLastError = e.StackTrace;
                Console.WriteLine("Error " + e + " when processing: " + sDATFileName);
                Console.WriteLine(m_sLastError);
                bResult = false;
            }
            finally
            {
                m_DATFile = null;
            }
            return(bResult);
        }