GameSaveProgress / Forms / MainForm.cs

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 GameSave;
using System.Security.Permissions;
using System.IO;
using System.Reflection;

namespace GameSaveProgress
{
	public partial class MainForm : Form
	{
		AGameSave opengame;
		//IEnumerable<Type> GameList;
		Dictionary<string, Type> dictList;
		string selectedgame;

		private Type SelectedGameType {  get { return dictList[selectedgame]; } }
		private string SelectedGameExt { get { return dictList[selectedgame].GetProperty("GetExtension", BindingFlags.Static | BindingFlags.Public).GetValue(null, null).ToString(); } }
		private string SelectedGameInitialDir { get { return dictList[selectedgame].GetProperty("GetDirectory", BindingFlags.Static | BindingFlags.Public).GetValue(null, null).ToString(); } }

		public MainForm()
		{
			InitializeComponent();
			InitGUI();
		}

		private void InitGUI()
		{
			Text = Program.Title;
			Icon = Properties.Resources.gsp;

			IEnumerable<Type> GameList = AppDomain
					.CurrentDomain
					.GetAssemblies()
					.SelectMany(assembly => assembly.GetTypes())
					.Where(type => (typeof(AGameSave).IsAssignableFrom(type) && !type.IsAbstract));

			dictList = new Dictionary<string, Type>();
			foreach (Type game in GameList)
			{
				string gamename = game.GetProperty("GetGameName", BindingFlags.Static | BindingFlags.Public).GetValue(null, null).ToString();
				dictList.Add(gamename.ToString(), game);
				lbGameSelection.Items.Add(gamename);
			}			
		}

		private bool OpenSaveGameFile(string path)
		{
			lock (this)
			{
				try
				{
					opengame = (AGameSave)Activator.CreateInstance(SelectedGameType);
					bool isLoaded = opengame.Init(path);
					
					if (isLoaded)
					{
						return true;
					}
				}
				catch (Exception e)
				{
					MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			return false;
		}


		private void MainForm_Load(object sender, EventArgs e)
		{

		}
		
		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void btLoad_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog ofd = new OpenFileDialog())
			{
				ofd.InitialDirectory = SelectedGameInitialDir; //todo: fix


				ofd.Filter = String.Format("{0} (*.{1})|*.{1}", selectedgame, /*SelectedGameExt*/ "eep"); //todo: fix
				ofd.FilterIndex = 1;
				ofd.RestoreDirectory = true;

				if (ofd.ShowDialog() == DialogResult.OK)
				{					
					if (OpenSaveGameFile(ofd.FileName))
					{
						panel1.Controls.Remove(lbGameSelection);
						panel1.Controls.Remove(btLoad);

						Text = string.Format("{0} - {1}", Program.Title, Path.GetFileName(ofd.FileName));
						panel1 = opengame.GameUI(panel1);
						opengame.Parse();
					}
				}
			}
		}

		private void lbGameSelection_SelectedIndexChanged(object sender, EventArgs e)
		{			
			selectedgame = lbGameSelection.SelectedItem.ToString();
			Console.WriteLine("Clicked: " + "\"" + lbGameSelection.SelectedItem + "\" \"" + SelectedGameType.ToString() + "\"");
			btLoad.Enabled = true;
		}
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.