Example #1
0
        public void addfille(PckEntry file)
        {
            Action <FolderTreeModel, IEnumerable <string>, PckEntry> ensureExists = null;

            ensureExists = (root, path, pckentry) =>
            {
                if (path.Any())
                {
                    var title = path.First();
                    var child = root.Rows.Where(x => x.Title == title).SingleOrDefault();
                    if (child == null)
                    {
                        child = new FolderTreeModel()
                        {
                            Title = title,
                            Rows  = new List <FolderTreeModel>(),
                        };
                        if (Path.HasExtension(title))
                        {
                            child.File = pckentry;
                        }
                        root.Rows.Add(child);
                    }
                    ensureExists(child, path.Skip(1), pckentry);
                }
            };
            string[] splitedPath = file.filePath.Split(Path.DirectorySeparatorChar);
            ensureExists(root, splitedPath, file);
        }
Example #2
0
        public bool ExtractSfx()
        {
            try
            {
                string     sfx        = Directory.GetParent(Path.GetDirectoryName(ElementEditorPath)).FullName + Path.DirectorySeparatorChar + "sfx.pck";
                PackHelper gfxManager = getManager("sfx.pck", sfx, null, false, false);
                if (gfxManager == null)
                {
                    return(true);
                }
                foreach (string str in this.sfx)
                {
                    List <PckEntry> dd = gfxManager.getDirectory(Path.GetDirectoryName(str), true, Path.GetFileName(str));
                    for (int i = 0; i < dd.Count; i++)
                    {
                        PckEntry curGfx  = dd[i];
                        string   dirpath = Path.GetDirectoryName(Path.Combine(saveDirectory, curGfx.fullP));
                        string   subpath = Path.Combine(saveDirectory, curGfx.fullP);
                        string   fileEx  = Path.GetExtension(subpath).Replace(".", "").ToLower();
                        if (!Directory.Exists(dirpath))
                        {
                            Directory.CreateDirectory(dirpath);
                        }

                        File.WriteAllBytes(subpath, curGfx.bytes);
                    }
                }
            }
            catch { }
            return(true);
        }
Example #3
0
 public PckEntry GetFile(PckEntry table, bool isRaw = false, bool returnOnlyRaw = false)
 {
     if (table.bytes != null)
     {
         return(table);
     }
     MakeStreams();
     byte[] buffer = new byte[table.compressedSize];
     table.fullP = table.filePath;
     br.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);
     buffer = br.ReadBytes(table.compressedSize);
     br.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);
     if (isRaw)
     {
         table.bytesCompresed = buffer;
         if (returnOnlyRaw)
         {
             return(table);
         }
     }
     if (table.compressedSize < table.decompressedSize)
     {
         MemoryStream  ms  = new MemoryStream();
         ZOutputStream zos = new ZOutputStream(ms);
         CopyStream(new MemoryStream(buffer), zos, table.compressedSize);
         table.bytes = ms.ToArray();
         return(table);
     }
     else
     {
         table.bytes = buffer;
         return(table);
     }
 }
Example #4
0
        public byte[] getChunk2(string packPath, PckEntry table)
        {
            if (table.bytes != null)
            {
                return(table.bytes);
            }

            byte[] buffer = new byte[table.compressedSize];
            table.fullP = table.filePath;
            br.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);
            buffer = br.ReadBytes(table.compressedSize);
            br.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);

            if (table.compressedSize < table.decompressedSize)
            {
                MemoryStream  ms  = new MemoryStream();
                ZOutputStream zos = new ZOutputStream(ms);
                CopyStream(new MemoryStream(buffer), zos, table.compressedSize);
                return(ms.ToArray());
            }
            else
            {
                return(buffer);
            }
        }
Example #5
0
        public void addFilesToTable(string Directory, string FilePaths)
        {
            PckEntry data = table.Values.FirstOrDefault(x => x.filePath.ToLower().Equals(Directory));

            if (data != null)
            {
                data.filePath         = Directory;
                data.bytes            = File.ReadAllBytes(FilePaths);
                data.bytesCompresed   = PCKZlib.Compress(data.bytes, CompressionLevel);
                data.fileoffset       = (uint)0;
                data.compressedSize   = data.bytesCompresed.Length;
                data.decompressedSize = data.bytes.Length;
                table[data.id]        = data;
                modified = true;
            }
            else
            {
                data                  = new PckEntry(version);
                data.filePath         = Directory;
                data.bytes            = File.ReadAllBytes(FilePaths);
                data.bytesCompresed   = PCKZlib.Compress(data.bytes, CompressionLevel);
                data.fileoffset       = (uint)0;
                data.compressedSize   = data.bytesCompresed.Length;
                data.decompressedSize = data.bytes.Length;
                data.id               = table.Count;
                table.Add(data.id, data);
                directoryStructure.addfille(data);
                modified = true;
            }
        }
Example #6
0
        public void ParseSkyFile(PckEntry entry)
        {
            try
            {
                gfx = new List <string>();
                sfx = new List <string>();
                atk = new List <string>();
                const Int32 BufferSize = 128;
                using (var streamReader = new StreamReader(new MemoryStream(entry.bytes), Encoding.GetEncoding("GB2312"), true, BufferSize))
                {
                    String line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        if (line.ToLower().StartsWith("fxfilepath"))
                        {
                            string fileName = line.Split(':')[1].TrimStart();
                            if (fileName.Length > 3)
                            {
                                string fileExt = Path.GetExtension(fileName).Replace(".", "").ToLower();
                                switch (fileExt)
                                {
                                case "gfx":
                                    gfx.Add(("gfx" + Path.DirectorySeparatorChar + fileName).Replace("/", "\\"));
                                    break;

                                case "ogg":
                                case "wav":
                                    sfx.Add(("sfx" + Path.DirectorySeparatorChar + fileName.Replace("/", "\\")));
                                    break;
                                }
                            }
                        }
                        if (line.ToLower().StartsWith("atkpath"))
                        {
                            string fileName = line.Split(':')[1].TrimStart();
                            if (fileName.Length > 3)
                            {
                                atk.Add(("gfx" + Path.DirectorySeparatorChar + "skillattack" + Path.DirectorySeparatorChar + fileName.Replace("/", "\\")));
                            }
                        }
                    }
                }
            }catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Example #7
0
        private void ReadTable(string path)
        {
            psm     = new PackStructureManager(Path.GetFileNameWithoutExtension(path));
            streams = new List <Stream>();
            if (File.Exists(path))
            {
                streams.Add(new FileStream(path, FileMode.Open, FileAccess.Read));
            }
            string pkx = path.Replace(".pck", ".pkx");

            if (File.Exists(pkx))
            {
                streams.Add(new FileStream(pkx, FileMode.Open, FileAccess.Read));
            }
            stream = new CombinationStream(streams);

            BinaryReader br = new BinaryReader(stream);

            br.BaseStream.Seek(-8, SeekOrigin.End);
            int entryCount = br.ReadInt32();

            br.BaseStream.Seek(-272, SeekOrigin.End);
            long fileTableOffset = (long)((ulong)((uint)(br.ReadUInt32() ^ (ulong)KEY_1)));

            br.BaseStream.Seek(fileTableOffset, SeekOrigin.Begin);
            PckEntry[] table = new PckEntry[entryCount];
            for (int a = 0; a < entryCount; ++a)
            {
                int entrySize = br.ReadInt32() ^ KEY_1;
                entrySize = br.ReadInt32() ^ KEY_2;
                byte[] buffer = new byte[entrySize];
                buffer = br.ReadBytes(entrySize);
                if (entrySize < 276)
                {
                    table[a] = readTableEntry(buffer, true);
                }
                else
                {
                    table[a] = readTableEntry(buffer, false);
                }
                string name = table[a].filePath.ToLower();
                files[name] = table[a];
                psm.addfille(files[name]);
            }

            initiated = true;
        }
Example #8
0
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            //Extract file
            if (lockedItemDrop)
            {
                return;
            }
            if (selectedPck == null)
            {
                return;
            }
            List <PckEntry> selectedFiles = new List <PckEntry>();
            string          pckName       = selectedPck.fiullFilePath;

            if (openedPckFies != null)
            {
                lockedItemDrop = true;
                foreach (ListViewItem item in FileList.SelectedItems)
                {
                    int index = item.Index;
                    if (FileList.Items[index].Tag is PckEntry)
                    {
                        PckEntry file = (PckEntry)FileList.Items[index].Tag;
                        if (file != null)
                        {
                            byte[] bytes = openedPckFies.getChunk(file.packPath, file);
                            if (bytes != null)
                            {
                                file.memory = new MemoryStream(openedPckFies.getChunk(file.packPath, file));
                                selectedFiles.Add(file);
                            }
                        }
                    }
                }
                DataDragObject dataDragObject = new DataDragObject(selectedFiles.ToArray());
                dataDragObject.SetData(NativeMethods.CFSTR_FILEDESCRIPTORW, null);
                dataDragObject.SetData(NativeMethods.CFSTR_FILECONTENTS, null);
                dataDragObject.SetData(NativeMethods.CFSTR_PERFORMEDDROPEFFECT, null);
                DoDragDrop(dataDragObject, DragDropEffects.Copy);
                lockedItemDrop = false;
            }
        }
Example #9
0
        public void SaveTextures(PckEntry entry)
        {
            try
            {
                string      gfx        = Directory.GetParent(Path.GetDirectoryName(ElementEditorPath)).FullName + Path.DirectorySeparatorChar + "gfx.pck";
                const Int32 BufferSize = 128;
                using (var streamReader = new StreamReader(new MemoryStream(entry.bytes), Encoding.GetEncoding("GB2312"), true, BufferSize))
                {
                    String line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        if (line.ToLower().StartsWith("texfile"))
                        {
                            string fileName = line.Split(':')[1].TrimStart();
                            if (fileName.Length > 3)
                            {
                                PackHelper aa = getManager("gfx.pck", gfx, null);
                                if (aa != null)
                                {
                                    List <PckEntry> dd = aa.getDirectory("gfx" + Path.DirectorySeparatorChar + "textures" + Path.DirectorySeparatorChar + Path.GetDirectoryName(fileName), true, Path.GetFileName(fileName));
                                    for (int i = 0; i < dd.Count; i++)
                                    {
                                        PckEntry curGfx  = dd[i];
                                        string   dirpath = Path.GetDirectoryName(Path.Combine(saveDirectory, curGfx.fullP));
                                        string   subpath = Path.Combine(saveDirectory, curGfx.fullP);
                                        string   fileEx  = Path.GetExtension(subpath).Replace(".", "").ToLower();
                                        if (!Directory.Exists(dirpath))
                                        {
                                            Directory.CreateDirectory(dirpath);
                                        }

                                        File.WriteAllBytes(subpath, curGfx.bytes);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
Example #10
0
        public PckEntry readTableEntry(byte[] buffer, bool compressed)
        {
            PckEntry     fte = new PckEntry();
            MemoryStream ms  = new MemoryStream(buffer);

            if (compressed)
            {
                byte[]        buf = new byte[276];
                ZOutputStream zos = new ZOutputStream(new MemoryStream(buf));
                CopyStream(new MemoryStream(buffer), zos, 276);
                buffer = buf;
            }
            BinaryReader br = new BinaryReader(new MemoryStream(buffer));

            fte.filePath         = Encoding.GetEncoding("GB2312").GetString(br.ReadBytes(260)).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries)[0].Replace("/", "\\");
            fte.fullP            = string.Empty;
            fte.fileoffset       = br.ReadUInt32();
            fte.decompressedSize = br.ReadInt32();
            fte.compressedSize   = br.ReadInt32();
            return(fte);
        }
Example #11
0
        private void UnpackDirectoryOrFiles(object sender, EventArgs e)
        {
            if (lockedItemDrop || selectedPck == null || selectedtree == null)
            {
                return;
            }
            using (var fbd = new FolderBrowserDialog())
            {
                progress_bar("Extracting...", 0, 0);
                DialogResult result = fbd.ShowDialog();
                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    string pckName  = selectedPck.fiullFilePath;
                    string savePath = fbd.SelectedPath;
                    if (openedPckFies != null)
                    {
                        List <PckEntry> filesToSave = new List <PckEntry>();
                        // this.Cursor = Cursors.WaitCursor;
                        foreach (ListViewItem item in FileList.SelectedItems)
                        {
                            int index = item.Index;
                            if (FileList.Items[index].Tag is PckEntry)
                            {
                                PckEntry file = (PckEntry)FileList.Items[index].Tag;
                                if (Path.HasExtension(file.filePath))
                                {
                                    filesToSave.AddRange(openedPckFies.getDirectory(file.filePath, true, Path.GetFileName(file.filePath)));
                                }
                                else
                                {
                                    filesToSave.AddRange(openedPckFies.getDirectory(file.filePath));
                                }
                            }
                            else
                            {
                                if (FileList.Items[index].Tag is FolderTreeModel)
                                {
                                    FolderTreeModel file = (FolderTreeModel)FileList.Items[index].Tag;

                                    filesToSave.AddRange(openedPckFies.getDirectory(textBox1.Text + Path.DirectorySeparatorChar + file.Title));
                                }
                            }
                        }
                        if (filesToSave.Count > 0)
                        {
                            foreach (PckEntry item in filesToSave)
                            {
                                string dirpath = Path.GetDirectoryName(Path.Combine(savePath, item.fullP));
                                string subpath = Path.Combine(savePath, item.fullP);
                                string fileEx  = Path.GetExtension(subpath).Replace(".", "").ToLower();
                                if (!Directory.Exists(dirpath))
                                {
                                    Directory.CreateDirectory(dirpath);
                                }

                                File.WriteAllBytes(subpath, item.bytes);
                            }
                        }
                        // this.Cursor = Cursors.Default;
                    }
                }
            }
            progress_bar("Ready", 0, 0);
        }
Example #12
0
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lockedItemDrop || selectedPck == null || selectedtree == null)
            {
                return;
            }
            string pckName = selectedPck.fiullFilePath;

            if (openedPckFies != null)
            {
                this.FileList.SuspendLayout();
                this.FileList.BeginUpdate();
                // this.Cursor = Cursors.WaitCursor;
                List <int> ids = new List <int>();
                foreach (ListViewItem item in FileList.SelectedItems)
                {
                    int index = item.Index;
                    if (FileList.Items[index].Tag is PckEntry)
                    {
                        PckEntry file = (PckEntry)FileList.Items[index].Tag;
                        ids.Add(file.id);
                        foreach (FolderTreeModel row in selectedtree.Rows)
                        {
                            if (row.Title == Path.GetFileName(file.filePath))
                            {
                                selectedtree.Rows.Remove(row);
                                break;
                            }
                        }
                        FileList.Items.RemoveAt(index);
                    }
                    else if (FileList.Items[index].Tag is FolderTreeModel)
                    {
                        FolderTreeModel file  = (FolderTreeModel)FileList.Items[index].Tag;
                        List <PckEntry> files = openedPckFies.directoryStructure.getAllFIles(file);
                        foreach (PckEntry row in files)
                        {
                            ids.Add(row.id);
                        }
                        FileList.Items.RemoveAt(index);
                        selectedtree.Rows.Remove(file);
                    }
                }
                openedPckFies.deleteFile(ids.ToArray());
                if (selectedtree.Rows == null || selectedtree.Rows != null && selectedtree.Rows.Count == 0)
                {
                    selectedtree = selectedPck.root;
                }


                FileList.Items.Clear();
                ListViewItem lvi = new ListViewItem();
                lvi.Text       = "..";
                lvi.ImageIndex = _iconListManager.AddFileId("d", "d", true);
                FileList.Items.Add(lvi);
                //folderDeeplist[folderDeep - 1] = selectedtree;
                foreach (FolderTreeModel row in selectedtree.Rows.OrderBy(o => o.File != null).ToList())
                {
                    lvi      = new ListViewItem();
                    lvi.Text = row.Title;
                    if (row.File != null)
                    {
                        lvi.ImageIndex = _iconListManager.AddFileId(Path.GetExtension(row.Title), row.Title, false);
                        double compresRatio = ConvertSize(row.File.decompressedSize, "MB") - ConvertSize(row.File.compressedSize, "MB") / ConvertSize(row.File.decompressedSize, "MB");
                        lvi.SubItems.Add(SizeSuffix(row.File.compressedSize));
                        lvi.SubItems.Add(SizeSuffix(row.File.decompressedSize));
                        lvi.SubItems.Add((compresRatio * 100).ToString("F").Substring(1) + " %");
                        lvi.Tag = row.File;
                    }
                    else
                    {
                        lvi.ImageIndex = _iconListManager.AddFileId(row.Title, row.Title, true);
                        lvi.SubItems.Add("-");
                        lvi.SubItems.Add("-");
                        lvi.SubItems.Add("-");
                        lvi.Tag = row;
                    }

                    FileList.Items.Add(lvi);
                }
                this.FileList.ResumeLayout();
                this.FileList.EndUpdate();
                textBox1.Text = "";
                foreach (FolderTreeModel list in folderDeeplist.Values)
                {
                    textBox1.AppendText(list.Title + Path.DirectorySeparatorChar);
                }
                string ddd = textBox1.Text;
                textBox1.Text = ddd.TrimEnd(Path.DirectorySeparatorChar);
            }
        }
Example #13
0
        public void unpack(bool isThread = false, Action callAfterCompleate = null)
        {
            // Task<bool>[] taskList = {
            //    Task.Run(() =>
            // {
            MakeStreams();

            LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(Environment.ProcessorCount / 2);
            List <Task> tasks = new List <Task>();

            // Create a TaskFactory and pass it our custom scheduler.
            TaskFactory             factory = new TaskFactory(lcts);
            CancellationTokenSource cts     = new CancellationTokenSource();

            new Thread(() =>
            {
                try
                {
                    var listOfTasks = new List <Task>();
                    if (table.Count > 0 && path != null && path.Length > 0)
                    {
                        string pathx = path + ".files\\";
                        if (Directory.Exists(pathx))
                        {
                            try
                            {
                                Directory.Delete(pathx, true);
                            }
                            catch { }
                        }

                        PckEntry[] files = table.Values.ToArray();
                        for (int i = 0; i < files.Length; i++)
                        {
                            PckEntry pck = files[i];
                            CreateDir(pathx, pck.filePath);
                            if (pck.bytes == null)
                            {
                                byte[] data = getChunk2("", pck);
                                if (data != null)
                                {
                                    Task t1 = factory.StartNew((o) => {
                                        progress_bar("Unpacking..", (int)i, files.Length);
                                        File.WriteAllBytes(pathx + pck.filePath, data);
                                    }, i, cts.Token);
                                    tasks.Add(t1);
                                }
                                else
                                {
                                }
                            }
                            else
                            {
                                if (pck.bytes != null)
                                {
                                    File.WriteAllBytes(pathx + pck.filePath, pck.bytes);
                                }
                            }
                        }
                        Task.WaitAll(tasks.ToArray());
                        Thread.Sleep(500);
                        cts.Dispose();
                    }
                    if (callAfterCompleate != null)
                    {
                        callAfterCompleate.Invoke();
                    }
                }
                catch
                {
                }
            }).Start();

            //   })
            // };
            // Task.WaitAll(taskList);
        }
Example #14
0
        public int ReWrite()
        {
            string       pathx  = path + ".files\\";
            DialogResult result = DialogResult.No;

            if (Directory.Exists(pathx))
            {
                result = MessageBox.Show("Found files on disk do you want to rewrite from files?", "Question", MessageBoxButtons.YesNo);
            }
            #region AAAAA
            if (result == DialogResult.No)
            {
                LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(Environment.ProcessorCount / 2);
                List <Task>             tasks             = new List <Task>();
                TaskFactory             factory           = new TaskFactory(lcts);
                CancellationTokenSource cts = new CancellationTokenSource();
                MakeStreams();
                if (path != null && path.Length > 0)
                {
                    PckEntry[] values = table.Values.ToArray();
                    int        count  = values.Length;
                    for (int i = 0; i < values.Length; i++)
                    {
                        if (values[i].bytesCompresed == null)
                        {
                            PckEntry pck  = values[i];
                            byte[]   data = getChunk2("", pck);
                            progress_bar("Reading files ....", i, count);
                            Task t1 = factory.StartNew((o) =>
                            {
                                progress_bar("Compressing files ....", (int)o, count);
                                pck.bytesCompresed = PCKZlib.Compress(data, CompressionLevel);
                            }, i, cts.Token);
                            tasks.Add(t1);
                        }
                    }
                    progress_bar("Please Wait", 0, 0);
                    Task.WaitAll(tasks.ToArray());
                    Thread.Sleep(500);
                    cts.Dispose();
                    initialCount = count;
                    this.Dispose();
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    if (File.Exists(path.Replace(".pck", ".pkx")))
                    {
                        File.Delete(path.Replace(".pck", ".pkx"));
                    }

                    PCKStream stream = new PCKStream(path);
                    stream.WriteInt32(stream.key.FSIG_1);
                    stream.WriteInt32(0);
                    stream.WriteInt32(stream.key.FSIG_2);
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        fte.setVersion(version);

                        progress_bar("Writing files ....", i, count);
                        fte.fileoffset = (uint)stream.Position;
                        stream.WriteBytes(fte.bytesCompresed);
                    }
                    uint         fileTableOffset_tmp = (uint)stream.Position;
                    MemoryStream FileTable           = new MemoryStream();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        fte.setVersion(version);
                        byte[] buffer = fte.Write(CompressionLevel);
                        lock (FileTable)
                        {
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4);
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4);
                            FileTable.Write(buffer, 0, buffer.Length);
                        }
                        progress_bar("Writing table...", i, count);
                    }
                    stream.WriteBytes(FileTable.ToArray());
                    FileTable.Flush();
                    FileTable.Dispose();
                    stream.WriteInt32(ASIG_1);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1));
                    if (version > 2)
                    {
                        stream.WriteInt32(-1);
                    }
                    stream.WriteInt32(0);
                    NameChar namechar = new NameChar(252);
                    namechar.DefaultEncoding = Encoding.Default;
                    namechar.Name            = "Angelica File Package, writed by JHS Software.";
                    stream.WriteBytes(namechar.EncodedName);
                    stream.WriteInt32(ASIG_2);
                    if (version > 2)
                    {
                        stream.WriteInt32(512);
                    }
                    stream.WriteInt32(table.Count);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.Seek(4, SeekOrigin.Begin);
                    stream.WriteUInt32((uint)stream.GetLenght());
                    stream.Dispose();
                    progress_bar("Clearing memory...", 0, 0);
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        table[fte.id].bytes          = null;
                        table[fte.id].bytesCompresed = null;
                        table[fte.id].memory         = null;
                        progress_bar("Clearing memory...", i, count);
                    }
                    GC.Collect();
                    progress_bar("Ready", 0, 0);
                }
            }
            else
            #endregion
            #region FROM DISK
            {
                directoryStructure = null;
                GC.Collect();
                modified           = false;
                directoryStructure = new PackStructureManager(Path.GetFileNameWithoutExtension(path));
                table = new SortedList <int, PckEntry>();
                string[] files = Directory.GetFiles(pathx, "*", SearchOption.AllDirectories);

                int count = files.Length;
                initialCount = count;
                this.Dispose();
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                if (File.Exists(path.Replace(".pck", ".pkx")))
                {
                    File.Delete(path.Replace(".pck", ".pkx"));
                }

                PCKStream stream = new PCKStream(path);
                stream.WriteInt32(stream.key.FSIG_1);
                stream.WriteInt32(0);
                stream.WriteInt32(stream.key.FSIG_2);
                for (int i = 0; i < files.Length; i++)
                {
                    string   file  = files[i].Replace(pathx, "").Replace("/", "\\");
                    string   pathr = Path.Combine(pathx, file);
                    PckEntry data  = new PckEntry(version);
                    data.filePath = file;
                    byte[] datab          = File.ReadAllBytes(pathr);
                    byte[] bytesCompresed = PCKZlib.Compress(datab, CompressionLevel);
                    data.compressedSize   = bytesCompresed.Length;
                    data.decompressedSize = datab.Length;
                    data.id = i;
                    progress_bar("Writing files ....", i, count);
                    data.fileoffset = (uint)stream.Position;
                    stream.WriteBytes(bytesCompresed);
                    table[i] = data;
                    directoryStructure.addfille(table[i]);
                }
                progress_bar("Please Wait", 0, 0);
                Application.DoEvents();
                uint         fileTableOffset_tmp = (uint)stream.Position;
                MemoryStream FileTable           = new MemoryStream();
                progress_bar("Finalizing Pack...", 0, 0);
                for (int i = 0; i < table.Count; i++)
                {
                    PckEntry fte    = table[i];
                    byte[]   buffer = fte.Write(CompressionLevel);
                    lock (FileTable)
                    {
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4);
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4);
                        FileTable.Write(buffer, 0, buffer.Length);
                    }
                    progress_bar("Finalizing Pack...", i, count);
                }
                stream.WriteBytes(FileTable.ToArray());
                FileTable.Flush();
                FileTable.Dispose();
                stream.WriteInt32(ASIG_1);
                stream.WriteInt16(version);
                stream.WriteInt16(2);
                stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1));
                if (version > 2)
                {
                    stream.WriteInt32(-1);
                }
                stream.WriteInt32(0);
                NameChar namechar = new NameChar(252);
                namechar.DefaultEncoding = Encoding.Default;
                namechar.Name            = "Angelica File Package, writed by JHS Software.";
                stream.WriteBytes(namechar.EncodedName);
                stream.WriteInt32(ASIG_2);
                if (version > 2)
                {
                    stream.WriteInt32(512);
                }
                stream.WriteInt32(table.Count);
                stream.WriteInt16(version);
                stream.WriteInt16(2);
                stream.Seek(4, SeekOrigin.Begin);
                stream.WriteUInt32((uint)stream.GetLenght());
                stream.Dispose();
                progress_bar("Clearing memory...", 0, 0);
                for (int i = 0; i < table.Count; i++)
                {
                    table[i].bytes          = null;
                    table[i].bytesCompresed = null;
                    table[i].memory         = null;
                    progress_bar("Clearing memory...", i, count);
                }
                GC.Collect();
                progress_bar("Ready", 0, 0);
            }
            #endregion
            return(0);//SUccess
        }
Example #15
0
        /// <summary>
        /// Returns 0 success
        /// </summary>
        /// <returns></returns>
        public int Update()
        {
            if (path != null && path.Length > 0)
            {
                if (initialCount > table.Count)
                {
                    return(ReWrite());
                }
                if (!modified)
                {
                    DialogResult res = JMessageBox.Show(PackView.Instance, "The archive has not been modified do you wish to rewirte it?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (res == DialogResult.Yes)
                    {
                        return(ReWrite());
                    }
                    else
                    {
                        return(0);
                    }
                }
                this.Dispose();
                PCKStream stream = new PCKStream(path);

                if (stream != null)
                {
                    stream.Seek(fileTableOffset, SeekOrigin.Begin);
                    //Write new files
                    PckEntry[] values = table.Values.ToArray();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        if (fte.bytesCompresed != null)
                        {
                            fte.fileoffset = (uint)stream.Position;
                            stream.WriteBytes(fte.bytesCompresed);
                        }
                    }
                    uint fileTableOffset_tmp = (uint)stream.Position;
                    //RewriteTable
                    progress_bar("Finalizing Pack...", 0, 0);
                    MemoryStream FileTable = new MemoryStream();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte    = values[i];
                        byte[]   buffer = fte.Write(CompressionLevel);
                        lock (FileTable)
                        {
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4);
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4);
                            FileTable.Write(buffer, 0, buffer.Length);
                        }
                        progress_bar("Finalizing Pack...", i, values.Length);
                    }
                    stream.WriteBytes(FileTable.ToArray());
                    FileTable.Flush();
                    FileTable.Dispose();
                    stream.WriteInt32(ASIG_1);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1));
                    if (version > 2)
                    {
                        stream.WriteInt32(-1);
                    }
                    stream.WriteInt32(0);
                    NameChar namechar = new NameChar(252);
                    namechar.DefaultEncoding = Encoding.Default;
                    namechar.Name            = "Angelica File Package, writed by JHS Software.";
                    stream.WriteBytes(namechar.EncodedName);
                    stream.WriteInt32(ASIG_2);
                    if (version > 2)
                    {
                        stream.WriteInt32(512);
                    }
                    stream.WriteInt32(table.Count);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.Seek(4, SeekOrigin.Begin);
                    stream.WriteUInt32((uint)stream.GetLenght());
                    stream.Dispose();
                    progress_bar("Clearing memory...", 0, 0);
                    foreach (PckEntry fte in table.Values)
                    {
                        table[fte.id].bytes          = null;
                        table[fte.id].bytesCompresed = null;
                        table[fte.id].memory         = null;
                    }
                    GC.Collect();
                    return(0);//SUccess
                }
                else
                {
                    return(3);//CANNOT OPEN STREAM;
                }
            }
            else
            {
                if (path == null)
                {
                    return(1); //PATH NULL
                }
                else if (path != null && path.Length == 0)
                {
                    return(1); //PATH NULL
                }
                else
                {
                    return(2);// NOT MODIFIED
                }
            }
        }
Example #16
0
        private void ReadTable()
        {
            isReading    = true;
            initialCount = 0;
            int maxCount   = 100;
            int cnow       = 0;
            int entryCount = 0;

            try
            {
                br.BaseStream.Seek(-4, SeekOrigin.End);
                version = br.ReadInt16();
                br.BaseStream.Seek(-8, SeekOrigin.End);
                entryCount = br.ReadInt32();
                if (maxCount > 1000)
                {
                    maxCount = entryCount / 100;
                }
                else
                {
                    maxCount = entryCount / 10;
                }

                if (maxCount <= 1)
                {
                    maxCount = 1;
                }
                br.BaseStream.Seek(version == 2 ? -272 : -280, SeekOrigin.End);

                fileTableOffset = (long)((ulong)((uint)(br.ReadUInt32() ^ (ulong)KEY_1)));
                br.BaseStream.Seek(fileTableOffset, SeekOrigin.Begin);
                table = new SortedList <int, PckEntry>();
                for (int a = 0; a < entryCount; ++a)
                {
                    if (progress_bar != null && cnow > maxCount)
                    {
                        progress_bar("Reading files ....", a, entryCount);
                        cnow = 0;
                    }
                    cnow++;
                    int entrySize = br.ReadInt32() ^ KEY_1;
                    entrySize = br.ReadInt32() ^ KEY_2;
                    byte[] buffer = new byte[entrySize];
                    buffer      = br.ReadBytes(entrySize);
                    table[a]    = new PckEntry(buffer, version);
                    table[a].id = a;
                    directoryStructure.addfille(table[a]);
                }
            }
            catch { }
            disposed     = false;
            initiated    = true;
            initialCount = table.Count();
            if (progress_bar != null)
            {
                progress_bar("Reading files ....", entryCount, entryCount);
            }
            if (ReadTableIsDone != null)
            {
                ReadTableIsDone.Invoke();
            }

            isReading = false;
            this.Dispose();
        }