public bool Save(string fileName)
            {
                if ((fileName == null) || (fileName[0] == '\0'))
                {
                    // no filename to save it as.
                    return(false);
                }

                #region "Create Data Block, Byte By Byte For MD5 Encoding"
                // Create Macro File First before I start writing anything
                byte[] data = new byte[7600];
                int    cnt  = 0;
                #region Loop Through All The Macros In "this" Macro Set
                for (int mcrnum = 0; mcrnum < 20; mcrnum++)
                {
                    // 4 byte null header
                    data[cnt++] = 0;
                    data[cnt++] = 0;
                    data[cnt++] = 0;
                    data[cnt++] = 0;
                    #region "Loop Through The Series Of Lines... 6 Lines (61 Bytes Each)"
                    for (int mcrline = 0; mcrline < 6; mcrline++)
                    {
                        #region "Loop Through One Line Byte By Byte For 61 Bytes"
                        byte[] s = this.FFXIEncoding.GetBytes(this.Macros[mcrnum].Line[mcrline]); //.ToCharArray());
                        for (int linecnt = 0; linecnt < 61; linecnt++)
                        {
                            if ((s.Length == 0) || ((linecnt + 1) > s.Length) || (linecnt == 60))
                            {
                                data[cnt++] = 0;
                            }
                            else
                            {
                                data[cnt++] = s[linecnt];
                            }
                        }
                        #endregion
                    }
                    #endregion
                    byte[] t = this.FFXIEncoding.GetBytes(this.Macros[mcrnum].Name);
                    #region "Loop Through The Name Byte By Byte For 9 Bytes"
                    for (int namecnt = 0; namecnt < 9; namecnt++)
                    {
                        if ((namecnt + 1) > t.Length)
                        {
                            data[cnt++] = 0;
                        }
                        else
                        {
                            data[cnt++] = t[namecnt];
                        }
                    }
                    #endregion
                    data[cnt++] = 0; // final null byte
                }
                #endregion

                if (cnt != 7600)
                {
                    LogMessage.Log("CMacroFile.Save(): Data has only " + cnt + " bytes out of 7600. Not Saving!");
                    return(false);
                }
                #endregion

                #region "Create MD5 Hash From Data Block"
                // This is one implementation of the abstract class MD5.

                MD5 md5 = new MD5CryptoServiceProvider();
                this.MD5Digest = md5.ComputeHash(data);
                if (this.MD5Digest.Length != 16)
                {
                    MessageBox.Show("MD5 Hash has only " + this.MD5Digest.Length + " bytes instead of 16.");
                    return(false);
                }
                #endregion

                #region "Open The Binary File For Creation/Overwriting, Proceed To Write Everything"
                FileStream fs = null;
                try
                {
                    if (!Directory.Exists(Path.GetDirectoryName(fileName).TrimEnd('\\')))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(fileName).TrimEnd('\\'));
                    }

                    fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite);
                }
                //catch (DirectoryNotFoundException e)
                //{
                //    String[] fPath = fileName.Split('\\');
                //    Array.Resize(ref fPath, fPath.Length - 1);
                //    String pathtocreate = String.Empty;
                //    foreach (String x in fPath)
                //        pathtocreate += x + "\\";
                //    pathtocreate = pathtocreate.Trim('\\');
                //    LogMessage.Log("{0}: {1} not found, attempting to create.", e.Message, pathtocreate);
                //    try
                //    {
                //        Directory.CreateDirectory(pathtocreate);
                //        fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite);
                //        LogMessage.Log("..Success");
                //    }
                //    catch (Exception ex)
                //    {
                //        fs = null;
                //        LogMessage.Log("..Failed, {0} & {1}", e.Message, ex.Message);
                //        return false;
                //    }
                //}
                catch (Exception e)
                {
                    LogMessage.Log("Error: {0}", e.Message);
                    return(false);
                }
                BinaryWriter BR = null;
                if (fs != null)
                {
                    BR = new BinaryWriter(fs);
                }
                if ((fs != null) && (BR != null))    // Write the header to the new file
                {
                    BR.Write((ulong)1);              // Write the first of eight bytes with 0x01 then 7 0x00's
                    BR.Write(this.MD5Digest, 0, 16); // Write MD5 hash
                    BR.Write(data, 0, 7600);
                    BR.Close();
                    fs.Close();
                }
                else
                {
                    return(false);
                }
                #endregion
                this.Changed = false;
                return(true);
            }
Example #2
0
        private void bttOpen_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlgOpenFile = new Microsoft.Win32.OpenFileDialog();
            dlgOpenFile.Multiselect = false;
            dlgOpenFile.DefaultExt  = ".lqps";
            dlgOpenFile.Filter      = "Application extension (.lqps)|*.lqps";

            FileStream   fiStream = null;
            BinaryReader BR       = null;
            List <int[]> Sloaded  = null;
            string       file     = "";
            bool         binOk    = true;

            try
            {
                if (dlgOpenFile.ShowDialog() == true)
                {
                    // --- file Loading ---
                    file = dlgOpenFile.FileName;

                    fiStream = new FileStream(file, FileMode.Open);

                    BR = new BinaryReader(fiStream);

                    int SolNum = BR.ReadInt32();
                    int length = BR.ReadInt32();

                    Sloaded = new List <int[]>(SolNum);
                    int[] Solution;

                    for (int i = 0; i < SolNum; i++)
                    {
                        Solution = new int[length];

                        for (int j = 0; j < length; j++)
                        {
                            Solution[j] = BR.ReadInt32();
                        }

                        Sloaded.Add(Solution);
                    }
                }
                else
                {
                    binOk = false;  // nothing was selected
                }
            }
            catch (Exception exc)
            {
                binOk = false;
                MessageBox.Show(exc.ToString(), "Issues during loading", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            finally
            {
                if (BR != null)
                {
                    BR.Close();
                }
                if (fiStream != null)
                {
                    fiStream.Close();
                }
            }

            if (binOk)
            {
                file = System.IO.Path.GetFileNameWithoutExtension(file);

                Solutions = Sloaded;

                Length   = Solutions[0].Length;
                NumTotal = Math.Pow(Length, Length);

                txtInfo.Text = string.Format("{0}", file);

                DrawRangeSpreading();
            }
        }
Example #3
0
        public static MacroFolder Load(string PathName, string FolderName)
        {
            FileStream DATFile = null;

            if (File.Exists(PathName))
            {
                try { DATFile = new FileStream(PathName, FileMode.Open, FileAccess.Read, FileShare.Read); }
                catch { }
            }
            BinaryReader BR = null;

            if (DATFile != null)
            {
                try {
                    BR = new BinaryReader(DATFile, Encoding.ASCII);
                    if (BR.BaseStream.Length != 7624 || BR.ReadUInt32() != 1)
                    {
                        BR.Close();
                        BR = null;
                    }
                    if (BR != null)
                    {
                        BR.ReadUInt32();                     // Unknown - sometimes zero, sometimes 0x80000000
                        byte[] StoredMD5 = BR.ReadBytes(16); // MD5 Checksum of the rest of the data
#if VerifyChecksum
                        {
                            byte[] Data = BR.ReadBytes(7600);
                            BR.BaseStream.Seek(-7600, SeekOrigin.Current);
                            MD5    Hash        = new MD5CryptoServiceProvider();
                            byte[] ComputedMD5 = Hash.ComputeHash(Data);
                            for (int i = 0; i < 16; ++i)
                            {
                                if (StoredMD5[i] != ComputedMD5[i])
                                {
                                    string Message = String.Format("MD5 Checksum failure for {0}:\n- Stored Hash  :", PathName);
                                    for (int j = 0; j < 16; ++j)
                                    {
                                        Message += String.Format(" {0:X2}", StoredMD5[j]);
                                    }
                                    Message += "\n- Computed Hash:";
                                    for (int j = 0; j < 16; ++j)
                                    {
                                        Message += String.Format(" {0:X2}", ComputedMD5[j]);
                                    }
                                    Message += '\n';
                                    MessageBox.Show(null, Message, "Warning");
                                    break;
                                }
                            }
                        }
#endif
                    }
                } catch { }
            }
            MacroSet MS = new MacroSet(FolderName);
            MS.FileName_ = PathName;
            MS.Folders.Add(new MacroFolder("Top Bar (Control)"));
            MS.Folders.Add(new MacroFolder("Bottom Bar (Alt)"));
            Encoding E = new FFXIEncoding();
            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    MS.Folders[i].Macros.Add((BR != null) ? Macro.ReadFromDATFile(BR, E) : new Macro());
                }
            }
            MS.LockTree();
            if (BR != null)
            {
                BR.Close();
            }
            return(MS);
        }