コード例 #1
0
ファイル: HinanawiTenshi.cs プロジェクト: tirr-c/th105Edit
        public void Open(string Path)
        {
            if (m_stream != null)
            {
                m_stream.Close();
            }
            m_stream = new FileStream(Path, FileMode.Open);
            m_entries.Clear();
            byte[] buf = new byte[4];
            m_stream.Read(buf, 0, 2);
            m_list_count = (short)(buf[0] + (buf[1] << 8));
            m_stream.Read(buf, 0, 4);
            m_list_length = (uint)(buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24));

            TenshiRandomGenerator mt = new TenshiRandomGenerator(m_list_length + 6);

            byte[] list_buf = new byte[m_list_length];
            m_stream.Read(list_buf, 0, (int)m_list_length);
            byte key = key_base, delta = key_delta;

            for (uint i = 0; i < m_list_length; i++)
            {
                list_buf[i] ^= (byte)(mt.NextInt() & 0xff);
                list_buf[i] ^= key;
                key         += delta;
                delta       += key_ddelta;
            }

            uint c = 0;

            for (short i = 0; i < m_list_count; i++)
            {
                uint off, len;
                off = (uint)(list_buf[c] + (list_buf[c + 1] << 8) + (list_buf[c + 2] << 16) + (list_buf[c + 3] << 24));
                c  += 4;
                len = (uint)(list_buf[c] + (list_buf[c + 1] << 8) + (list_buf[c + 2] << 16) + (list_buf[c + 3] << 24));
                c  += 4;
                byte entry_len = list_buf[c];
                c++;
                byte[] entry_buf = new byte[entry_len];
                Array.Copy(list_buf, c, entry_buf, 0, entry_len);
                c += entry_len;
                string entry_name = ShiftJIS.GetString(entry_buf);
                m_entries.Add(new TenshiEntry(m_stream, entry_name, (long)off, (long)len));
            }
        }
コード例 #2
0
ファイル: HinanawiTenshi.cs プロジェクト: tirr-c/th105Edit
        public void Save(string Path, SaveFileCallback callback = null)
        {
            FileStream save_stream = new FileStream(Path, FileMode.Create);

            try
            {
                ushort entry_count = (ushort)m_entries.Count;
                uint   entry_len   = 0;

                // 엔트리 리스트 길이 계산
                foreach (TenshiEntry i in m_entries)
                {
                    entry_len += (uint)i.EntryLength;
                }
                save_stream.Write(LittleEndian.ToEndian(entry_count), 0, 2);
                save_stream.Write(LittleEndian.ToEndian(entry_len), 0, 4);
                // 엔트리 리스트 구성
                uint                  offset            = 6 + entry_len;
                MemoryStream          entry_list_stream = new MemoryStream((int)entry_len);
                TenshiRandomGenerator rand = new TenshiRandomGenerator(6 + entry_len);
                foreach (TenshiEntry i in m_entries)
                {
                    entry_list_stream.Write(LittleEndian.ToEndian(offset), 0, 4);
                    entry_list_stream.Write(LittleEndian.ToEndian((uint)i.Length), 0, 4);
                    entry_list_stream.WriteByte((byte)ShiftJIS.GetByteCount(i.Entry));
                    entry_list_stream.Write(ShiftJIS.GetBytes(i.Entry), 0, ShiftJIS.GetByteCount(i.Entry));
                    i.NewOffset = offset;
                    offset     += (uint)i.Length;
                }
                // 리스트 암호화
                {
                    byte[] buf = entry_list_stream.GetBuffer();
                    byte   key = key_base, delta = key_delta;
                    for (uint i = 0; i < entry_len; i++)
                    {
                        buf[i] ^= key;
                        key    += delta;
                        delta  += key_ddelta;
                        buf[i] ^= (byte)(rand.NextInt() & 0xff);
                    }
                    save_stream.Write(buf, 0, (int)entry_len);
                    entry_list_stream.Dispose();
                }
                // 나 쓴다 데이터
                ushort count = 0;
                if (m_buf_queue == null)
                {
                    m_buf_queue = new Queue <byte[]>();
                }
                else
                {
                    m_buf_queue.Clear();
                }
                Thread th = new Thread(new ThreadStart(CryptThread));
                th.Start();
                for (ushort i = 0; i < entry_count; i++)
                {
                    while (m_buf_queue.Count <= 0)
                    {
                        ;
                    }
                    byte[] buf = m_buf_queue.Dequeue();
                    save_stream.Write(buf, 0, buf.Length);
                    if (callback != null)
                    {
                        callback(++count, entry_count);
                    }
                }
                m_stream.Close();
            }
            catch
            {
            }
            finally
            {
                save_stream.Close();
            }
        }