Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
fmSelectGrammars.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.ComponentModel;
16 using System.Data;
17 using System.Drawing;
18 using System.Linq;
19 using System.Text;
20 using System.Windows.Forms;
21 using System.Reflection;
22 using Irony.Parsing;
23 using System.IO;
24 
25 namespace Irony.GrammarExplorer {
26  public partial class fmSelectGrammars : Form {
27  public fmSelectGrammars() {
28  InitializeComponent();
29  }
30 
31  public static GrammarItemList SelectGrammars(string assemblyPath, GrammarItemList loadedGrammars) {
32  var fromGrammars = LoadGrammars(assemblyPath);
33  if (fromGrammars == null)
34  return null;
35  //fill the listbox and show the form
37  var listbox = form.lstGrammars;
38  listbox.Sorted = false;
39  foreach(GrammarItem item in fromGrammars) {
40  listbox.Items.Add(item);
41  if (!ContainsGrammar(loadedGrammars, item))
42  listbox.SetItemChecked(listbox.Items.Count - 1, true);
43  }
44  listbox.Sorted = true;
45 
46  if (form.ShowDialog() != DialogResult.OK) return null;
47  GrammarItemList result = new GrammarItemList();
48  for (int i = 0; i < listbox.Items.Count; i++) {
49  if (listbox.GetItemChecked(i)) {
50  var item = listbox.Items[i] as GrammarItem;
51  item._loading = false;
52  result.Add(item);
53  }
54  }
55  return result;
56  }
57 
58  private static GrammarItemList LoadGrammars(string assemblyPath) {
59  Assembly asm = null;
60  try
61  {
62  asm = Assembly.LoadFrom(assemblyPath);
63  // enforce loading every time, even if assembly name is not changed
64  //asm = Assembly.Load(File.ReadAllBytes(assemblyPath));
65  } catch (Exception ex) {
66  MessageBox.Show("Failed to load assembly: " + ex.Message);
67  return null;
68  }
69  var types = asm.GetTypes();
70  var grammars = new GrammarItemList();
71  foreach (Type t in types) {
72  if (!t.IsSubclassOf(typeof(Parsing.Grammar))) continue;
73  grammars.Add(new GrammarItem(t, assemblyPath));
74  }
75  if (grammars.Count == 0) {
76  MessageBox.Show("No classes derived from Irony.Grammar were found in the assembly.");
77  return null;
78  }
79  return grammars;
80  }
81 
82  private static bool ContainsGrammar(GrammarItemList items, GrammarItem item) {
83  foreach (var listItem in items)
84  if (listItem.TypeName == item.TypeName && listItem.Location == item.Location)
85  return true;
86  return false;
87  }
88 
89  private void btnCheckUncheck_Click(object sender, EventArgs e) {
90  bool check = sender == btnCheckAll;
91  for (int i = 0; i < lstGrammars.Items.Count; i++)
92  lstGrammars.SetItemChecked(i, check);
93  }
94 
95  }//class
96 }
static GrammarItemList SelectGrammars(string assemblyPath, GrammarItemList loadedGrammars)