Esempio n. 1
0
 public PakRecord(PakCollection collection, string filename, DateTime filetime)
 {
     mCollection = collection;
     mFileName   = filename;
     mFileTime   = filetime;
     mBuffer     = null;
 }
Esempio n. 2
0
 public PakRecord(PakCollection collection)
 {
     mCollection = collection;
     mFileName   = String.Empty;
     mFileTime   = DateTime.MinValue;
     mBuffer     = null;
 }
Esempio n. 3
0
        private Image GetBackground(PakCollection collection, string levelFilename)
        {
            string filename = "levels\\" + levelFilename;
            string jp2      = Path.ChangeExtension(filename, ".jp2");
            string jpg      = Path.ChangeExtension(filename, ".jpg");
            string png      = Path.ChangeExtension(filename, ".png");

            PakRecord record;

            record = collection.GetRecord(jp2);
            if (record != null)
            {
                byte[] buffer;
                OpenJPEG.ConvertJPEG2(record, out buffer, ImageFormat.Jpeg);
                return(Image.FromStream(new MemoryStream(buffer)));
            }
            else
            {
                record = collection.GetRecord(jpg);
                if (record != null)
                {
                    return(GetImageFromBuffer(record.Buffer));
                }

                record = collection.GetRecord(png);
                if (record != null)
                {
                    return(GetImageFromBuffer(record.Buffer));
                }
            }

            return(null);
        }
Esempio n. 4
0
        public bool Open(string path)
        {
            bool successful = false;

            try {
                string pakFilename = Path.GetFileNameWithoutExtension(path);

                PakCollection pakFile = new PakCollection();
                pakFile.Open(path);

                PakRecord cfgRecord = GetCFGRecord(pakFilename, pakFile);
                if (cfgRecord == null)
                {
                    throw new InvalidDataException("Unable to find a cfg file in the level pack.");
                }

                ParseCFG(UTF8Encoding.ASCII.GetString(cfgRecord.Buffer));

                //Load levels
                foreach (LevelInfo linfo in mLevelInfos)
                {
                    byte[] buffer = pakFile.GetRecord("levels\\" + linfo.Filename + ".dat").Buffer;

                    LevelReader levelReader = new LevelReader(buffer);
                    Level       level       = levelReader.Read();
                    levelReader.Dispose();

                    if (level == null)
                    {
                        MessageBox.Show("Unable to open " + linfo.Name, "Open Level Pack", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        continue;
                    }

                    mLevels.Add(level);
                    level.Info       = linfo;
                    level.Background = GetBackground(pakFile, linfo.Filename);
                }

                //Load any images
                foreach (PakRecord record in pakFile)
                {
                    if (record.FileName.StartsWith("levels\\"))
                    {
                        continue;
                    }

                    if (Path.GetExtension(record.FileName) == ".png")
                    {
                        mImages.Add(record.FileName, GetImageFromBuffer(record.Buffer));
                    }
                }

                successful = true;
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Open Level Pack", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(successful);
        }
Esempio n. 5
0
        public static PakRecord FromFile(PakCollection collection, string filename)
        {
            PakRecord record = new PakRecord(collection);

            record.FileName = Path.GetFileName(filename);
            record.FileTime = File.GetLastWriteTime(filename);
            record.Buffer   = File.ReadAllBytes(filename);
            return(record);
        }
Esempio n. 6
0
        public bool Save(string path)
        {
            bool successful = false;

            try {
                string pakFilename = Path.GetFileNameWithoutExtension(path);

                PakCollection pakFile = new PakCollection();

                //CFG
                PakRecord cfgRecord = new PakRecord(pakFile, pakFilename + ".cfg", DateTime.Now);
                cfgRecord.Buffer = Encoding.UTF8.GetBytes(WriteCFG());
                pakFile.Records.Add(cfgRecord);

                //Levels
                foreach (Level lvl in mLevels)
                {
                    PakRecord datRecord = new PakRecord(pakFile, String.Format("levels\\{0}.dat", lvl.Info.Filename), DateTime.Now);
                    datRecord.Buffer = GetLevelData(lvl);

                    if (lvl.Background != null)
                    {
                        PakRecord bgImageRecord = new PakRecord(pakFile, String.Format("levels\\{0}.png", lvl.Info.Filename), DateTime.Now);
                        bgImageRecord.Buffer = GetImageData(lvl.Background);
                        pakFile.Records.Add(bgImageRecord);
                    }

                    PakRecord tbRecord = new PakRecord(pakFile, String.Format("levels\\cached_thumbnails\\{0}.png", lvl.Info.Filename), DateTime.Now);
                    tbRecord.Buffer = GetImageData(lvl.GetThumbnail());

                    pakFile.Records.Add(datRecord);

                    pakFile.Records.Add(tbRecord);
                }

                //Images
                foreach (string s in mImages.Keys)
                {
                    PakRecord iRecord = new PakRecord(pakFile, s, DateTime.Now);
                    iRecord.Buffer = GetImageData(mImages[s]);
                    pakFile.Records.Add(iRecord);
                }

                pakFile.Save(path);

                successful = true;
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Save Level Pack", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(successful);
        }
Esempio n. 7
0
        private PakRecord GetCFGRecord(string pakFilename, PakCollection pakFile)
        {
            PakRecord cfgRecord = pakFile.GetRecord(pakFilename + ".cfg");

            if (cfgRecord != null)
            {
                return(cfgRecord);
            }

            //Missing cfg or named wrong
            PakRecord[] records = pakFile.GetRecords(String.Empty);
            foreach (PakRecord record in records)
            {
                if (Path.GetExtension(record.FileName) == ".cfg")
                {
                    return(record);
                }
            }

            return(null);
        }