/// <summary> /// Opens a loaded bundle from the given path on disk /// </summary> /// <param name="savePath">The path to load the bundle from</param> public void LoadBundleFromFile(string savePath) { PixelariaFile file = PixelariaSaverLoader.LoadFileFromDisk(savePath); if (file == null) { MessageBox.Show(Resources.ErrorLoadingFile, Resources.Error_AlertTile, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Dispose of the current bundle if it's present if (CurrentBundle != null) { CloseBundle(CurrentBundle); } Bundle newBundle = file.LoadedBundle; newBundle.SaveFile = savePath; LoadBundle(newBundle); // Store the file now CurrentRecentFileList.StoreFile(savePath); _mainForm.UpdateRecentFilesList(); }
public void TestFileLoaderSaver() { Bundle bundle = BundleGenerator.GenerateTestBundle(0); Stream stream = new MemoryStream(); PixelariaFile originalFile = new PixelariaFile(bundle, stream); PixelariaFileSaver.Save(originalFile); // Test if the memory stream is now filled Assert.IsTrue(stream.Length > 0, "After a call to PixelariaFileSaver.Save(), the pixelaria file's stream should not be empty"); // Bring the bundle back with a PixelariaFileLoader PixelariaFile newFile = new PixelariaFile(new Bundle(""), stream); stream.Position = 0; PixelariaFileLoader.Load(newFile); Assert.AreEqual(originalFile.LoadedBundle, newFile.LoadedBundle, "After persisting a file to a stream and loading it back up again, the bundles must be equal"); // Save the bundle a few more times to test resilience of the save/load process newFile.CurrentStream.Position = 0; PixelariaFileLoader.Load(newFile); newFile.CurrentStream.Position = 0; PixelariaFileSaver.Save(newFile); Assert.IsTrue( Utilities.ByteArrayCompare(((MemoryStream)newFile.CurrentStream).GetBuffer(), ((MemoryStream)originalFile.CurrentStream).GetBuffer()), "Two streams that represent the same Pixelaria File should be bitwise equal"); Assert.AreEqual(originalFile.LoadedBundle, newFile.LoadedBundle, "After persisting a file to a stream and loading it back up again, the bundles must be equal"); }
/// <summary> /// Closes the given bundle from the controller /// </summary> /// <param name="bundle">The bundle to close</param> public void CloseBundle(Bundle bundle) { PixelariaFile file = GetPixelariaFileByBundle(bundle); file?.Dispose(); bundle.Dispose(); }
/// <summary> /// Reads a block from the given stream object /// </summary> /// <param name="stream">The stream to read the block from</param> /// <param name="file">The PixelariaFile to use when loading the block</param> /// <returns>A block read from the given stream</returns> public static FileBlock FromStream(Stream stream, PixelariaFile file) { // Save the current stream position long offset = stream.Position; // Reader to read the ID from the stream BinaryReader reader = new BinaryReader(stream); FileBlock block = CreateBlockById(reader.ReadInt16()); // Rewind the stream and read the block now stream.Position = offset; block.owningFile = file; block.LoadFromStream(stream); return(block); }