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); }
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); }
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); }