Skip to content

Instantly share code, notes, and snippets.

@codemonkey85
Last active August 29, 2015 14:01
Show Gist options
  • Save codemonkey85/fae64baac79cf30bc3ee to your computer and use it in GitHub Desktop.
Save codemonkey85/fae64baac79cf30bc3ee to your computer and use it in GitHub Desktop.
Using MemoryMappedFile and DragDrop to swap Pokémon between applications
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.MemoryMappedFiles;
using System.IO;
using System.Runtime.InteropServices;
using PKMDS_CS;
namespace MemMapTest
{
public partial class frmMemMapTest : Form
{
PKMDS.Pokemon frmpkm = new PKMDS.Pokemon();
// Reference:
//http://blogs.msdn.com/b/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx
public frmMemMapTest()
{
try
{
// Open the veekun DB - needed for PKMDS_CS.Pokemon properties
PKMDS.SQL.OpenDB("veekun-pokedex.sqlite");
}
catch (Exception ex)
{
}
InitializeComponent();
}
private void frmMemMapTest_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
// Close the veekun DB
PKMDS.SQL.CloseDB();
}
catch (Exception ex)
{
}
}
private void frmMemMapTest_Load(object sender, EventArgs e)
{
// Need to do this for PictureBox, which normally doesn't expose AllowDrop
((Control)(pbSprite)).AllowDrop = true;
// Grab a sample Pokemon from a file on disk; the other client should use a different one of course
frmpkm = PKMDS.ReadPokemonFile("Feraligatr.pkm");
lblData.Text = frmpkm.SpeciesID.ToString();
pbSprite.Image = frmpkm.Sprite;
}
private void pbSprite_DragEnter(object sender, DragEventArgs e)
{
if (e.Data != null)
{
e.Effect = DragDropEffects.Move;
}
}
private void pbSprite_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)(sender);
DataObject data = new DataObject();
// Create the DragDrop data from our Pokemon and send it on its way
data = new DataObject(DataFormats.Serializable, frmpkm);
pb.DoDragDrop(data, DragDropEffects.Move);
// If the MemoryMappedFile already exists, open it; otherwise create it
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
PKMDS.Pokemon otherpkm = new PKMDS.Pokemon();
for (int i = 0; i < Marshal.SizeOf(otherpkm); i++)
{
// You can't do this on nullable types
// Good thing I expose the raw data of my Pokemon
FileMap.Read<byte>(i, out otherpkm.Data[i]);
}
// Once again, I'm glad I expose the raw data of my Pokemon
frmpkm.Data = otherpkm.Data;
lblData.Text = frmpkm.SpeciesID.ToString();
pbSprite.Image = frmpkm.Sprite;
}
}
private void pbSprite_DragDrop(object sender, DragEventArgs e)
{
if (e.Data != null)
{
PictureBox pb = (PictureBox)(sender);
// Grab the Pokemon from the DragDrop data
PKMDS.Pokemon otherpkm = (PKMDS.Pokemon)e.Data.GetData(DataFormats.Serializable);
// If the MemoryMappedFile already exists, open it; otherwise create it
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
for (int i = 0; i < Marshal.SizeOf(frmpkm); i++)
{
// One last hurrah for exposing my Pokemon as a byte array
FileMap.Write<byte>(i, ref frmpkm.Data[i]);
}
}
frmpkm.Data = otherpkm.Data;
lblData.Text = frmpkm.SpeciesID.ToString();
pbSprite.Image = frmpkm.Sprite;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment