Exemple #1
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);
        }
Exemple #2
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;
		}
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #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;
		}
Exemple #7
0
		public static bool ConvertJPEG2(PakRecord record, out byte[] dest, ImageFormat format)
		{
			string filename = Path.GetFileName(record.FileName);
			string src = Path.Combine(TempDirectory, filename);

			if (!Directory.Exists(TempDirectory))
				Directory.CreateDirectory(TempDirectory);

			File.WriteAllBytes(src, record.Buffer);
			bool successful = ConvertJPEG2(src, out dest, format);
			File.Delete(src);

			return successful;
		}