/
TypeBind.cs
89 lines (81 loc) · 2.39 KB
/
TypeBind.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
namespace DSLImplementation.UserInterface {
public class TypeBind {
private readonly Type type;
private readonly Dictionary<string,string> bindingtable;
private readonly int index = -0x01;
public Type Type {
get {
return type;
}
}
public Dictionary<string, string> Bindingtable {
get {
return bindingtable;
}
}
public int Index {
get {
return index;
}
}
public TypeBind (Type type) {
this.type = type;
this.bindingtable = null;
}
public TypeBind (Type type, int index) : this(type) {
this.index = index;
}
public TypeBind (Type type, Dictionary<string,string> bindingtable) : this(type) {
this.bindingtable = bindingtable;
}
public TypeBind (Type type, int index, Dictionary<string,string> bindingtable) : this(type,index) {
this.bindingtable = bindingtable;
}
public TypeBind (Type type, params string[] bindingtable) {
this.type = type;
if (bindingtable.Length > 0x00) {
this.bindingtable = new Dictionary<string, string> ();
for (int i = 0x00; i < bindingtable.Length; i += 0x02) {
this.bindingtable.Add (bindingtable [i], bindingtable [i + 0x01]);
}
} else {
this.bindingtable = null;
}
}
public TypeBind (Type type, int index, params string[] bindingtable) : this(type,bindingtable) {
this.index = index;
}
public static bool Match (TypeBind tb, int index, IPuzzlePiece ipp) {
return (ipp != null && tb.type.IsAssignableFrom(ipp.GetType()) && (tb.index == -0x01 || tb.index == index));
}
public bool Match (IPuzzlePiece ipp, int index) {
return Match(this,index,ipp);
}
public static bool MatchAndBind (TypeBind tb, int index, IPuzzlePiece ipp, Dictionary<string,object> tobind) {
if(Match(tb,index,ipp)) {
if(tb.bindingtable != null && tb.bindingtable.Count > 0x00) {
if(!(ipp is IKeyValueTablePuzzlePiece<string,object>)) {
return false;
}
KeyValueTable<string,object> conv = ((IKeyValueTablePuzzlePiece<string,object>) ipp).Table;
foreach(KeyValuePair<string,string> entry in tb.bindingtable) {
Console.WriteLine("Bind key {0} of {1}",entry.Key,ipp);
try {
tobind.Add(entry.Value,conv[entry.Key]);
}
catch {
return false;
}
}
}
return true;
}
return false;
}
public static implicit operator TypeBind (Type t) {
return new TypeBind(t);
}
}
}