Beispiel #1
0
        void SaveTo(string file, bool encode)
        {
            dialogs1_SelectedChanging(null);
            dialogs1_SelectedChangingTL(null);

            if (encode)
            {
                (Dialog.CurrentImageContainer as jzip.Container).SaveDialogData(file, dialogs1.dlg, (src, dst) =>
                {
                    string pwd_p = PasswordRequest();
                    if (pwd_p == null)
                    {
                        return;
                    }

                    using (System.IO.Stream sfs = new FileStream(src, FileMode.Open, FileAccess.Read))
                        using (System.IO.Stream sp = new FileStream(dst, FileMode.Create, FileAccess.Write))
                        {
                            sp.Write(new byte[] { (byte)0x9e, (byte)0x3f }, 0, 2);
                            sp.Write(BitConverter.GetBytes(sfs.Length), 0, 4);

                            DialogMaker.SecurityStream sec = new DialogMaker.SecurityStream(sp, pwd_p);

                            sfs.CopyTo(sec);
                            sec.Close();

                            sp.Close();
                            sfs.Close();
                        }
                });
            }
            else
            {
                (Dialog.CurrentImageContainer as jzip.Container).SaveDialogData(file, dialogs1.dlg);
            }
            LastLoadedJson    = file;
            LastLoadedEncoded = encode;

            dialogs1.Changed = false;
            this.Text        = "DMPRO \"" + System.IO.Path.GetFileName(file) + "\"";
            OpenFileHistory.DHistory.Push(file);
        }
Beispiel #2
0
        public void LoadJsFile(string path)
        {
            loading = true;
            updateStatusLabel(); this.Refresh();
            string json    = string.Empty;
            bool   encoded = false;

            try
            {
                string workingFile = null;
                using (System.IO.Stream sp = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    int a = sp.ReadByte(); int b = sp.ReadByte();

                    if (encoded = (a == 0xae && (b == 0xfe || b == 0xfa)) || (a == 0x9e && b == 0x3f))
                    {
                        int fixedLength = 0;
                        if (b == 0xfa || (a == 0x9e && b == 0x3f))
                        {
                            byte[] jslength = new byte[4];
                            if (sp.Read(jslength, 0, 4) != 4)
                            {
                                throw new Exception("file corrupted.");
                            }
                            fixedLength = BitConverter.ToInt32(jslength, 0);
                            sp.Seek(6, SeekOrigin.Begin);
                        }
                        else if (b == 0xfe)
                        {
                            fixedLength = ((int)sp.Length) - 2;
                            sp.Seek(2, SeekOrigin.Begin);
                        }

                        string pwd_p = PasswordRequest();
                        if (pwd_p == null)
                        {
                            return;
                        }

                        DialogMaker.SecurityStream sec = new DialogMaker.SecurityStream(sp, pwd_p);
                        if (!(a == 0x9e && b == 0x3f))//not zip encoded
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                sec.CopyTo(ms);
                                json = Encoding.UTF8.GetString(ms.GetBuffer(), 0, fixedLength);
                            }


                            sec.Close();

                            sec = null;
                            //!!!
                            LastLoadedEncoded = true;
                        }
                        else
                        {
                            workingFile = Path.GetTempFileName();
                            using (FileStream fs = new FileStream(workingFile, FileMode.Create))
                            {
                                sec.CopyTo(fs);
                                fs.Flush();
                                fs.SetLength(fixedLength);
                            }
                            LastLoadedEncoded = true;
                        }
                    }
                    else if ((a == 'P' && b == 'K'))
                    {
                        workingFile = Path.GetTempFileName();
                        sp.Seek(0, SeekOrigin.Begin);
                        using (FileStream fs = new FileStream(workingFile, FileMode.Create))
                        {
                            sp.CopyTo(fs);
                        }
                    }
                    else
                    {
                        sp.Seek(0, SeekOrigin.Begin);
                        StreamReader sr = new StreamReader(sp, Encoding.UTF8);
                        json = sr.ReadToEnd();
                        sr.Close();
                        //!!!!
                        LastLoadedEncoded = false;
                    }
                }
                //string json = sr.ReadToEnd();//System.IO.File.ReadAllText(path, Encoding.UTF8);
                //sr.Close();
                //!!!
                LastLoadedJson = path;

                //Dialog dlg = Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(Dialog)) as Dialog;
                //dialogs1.Clear();
                jzip.Container container = new jzip.Container(workingFile);
                Dialog         dlg       = new Dialog(container);
                if (workingFile == null)
                {
                    Newtonsoft.Json.JsonConvert.PopulateObject(json, dlg);
                }
                else
                {
                    container.ExtractLinkedData(dlg);
                }
                foreach (var item in dlg.phrases)
                {
                    if (item._attributes == null)
                    {
                        item._attributes = new byte[attributesForPhrase];
                    }
                }

                dialogs1.dlg            = new DBacklog(dlg);
                dialogs1.imageContainer = container;
                this.Text        = "DMPRO \"" + System.IO.Path.GetFileName(path) + "\"";
                dialogs1.Changed = false;
                OpenFileHistory.DHistory.Push(path);

                this.Refresh();
            }
            catch (Exception exc)
            {
                if (!string.IsNullOrEmpty(json))
                {
                    string failOverPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + "_failover_" + Path.GetExtension(path));
                    if (encoded)
                    {
                        try
                        {
                            File.WriteAllText(failOverPath, json, Encoding.UTF8);
                        }
                        catch
                        {
                            failOverPath = Path.Combine(Path.GetFileNameWithoutExtension(path) + "_failover_" + Path.GetExtension(path));
                            File.WriteAllText(failOverPath, json, Encoding.UTF8);
                        }
                    }
                    else
                    {
                        failOverPath = null;
                    }
                    System.Windows.Forms.MessageBox.Show(
                        string.Format("Error parsing saved dialog.\r\n{0} \r\n json saved as: {1}",
                                      exc.Message, failOverPath ?? "not saved"),
                        "open file error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show(
                        string.Format("Error occured while loading: {0}",
                                      exc.Message),
                        "open file error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            finally
            {
                json    = null;
                loading = false;
                updateStatusLabel();
            }
            GC.Collect();
            System.Threading.Thread.Sleep(100);
            GC.Collect();
            System.Threading.Thread.Sleep(100);
        }