-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceManager.cs
165 lines (140 loc) · 6.25 KB
/
ReferenceManager.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using ThunderRoad;
namespace SpellFramework
{
public partial class ReferenceManager : Form
{
DataGridViewCellEventArgs eventArgs;
Type fieldType;
int category;
//List<CatalogData> allowedValues = new List<CatalogData>();
string fieldValue = "";
string targetSource = "";
AutoCompleteStringCollection autoCompSrc = new AutoCompleteStringCollection();
public static event EventHandler ReferenceComplete;
protected virtual void OnReferenceComplete(EventArgs e)
{
EventHandler handler = ReferenceComplete;
handler?.Invoke(this, e);
}
public ReferenceManager(FieldInfo fieldInfo, DataGridViewCellEventArgs e)
{
FormClosing += ReferenceManager_FormClosing;
fieldValue = fieldInfo.GetValue(ModConfiguration.current.instancedType).ToString();
fieldInfo.GetValue(ModConfiguration.current.instancedType);
string fieldTypeName = Spell.dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex - 2].Value as string;
fieldTypeName = fieldTypeName.Replace("Id", "Data");
Catalog.Category categorylocal = Catalog.Category.Brain;
foreach (var en in Enum.GetValues(typeof(Catalog.Category)))
{
var index = fieldTypeName.IndexOf(en.ToString() + "Data");
if (index < 0) continue;
var split = fieldTypeName.Substring(index);
fieldType = ModConfiguration.generatedTypes.First(x => x.Name == split);
category = (int)Enum.Parse(typeof(Catalog.Category), en.ToString());
categorylocal = (Catalog.Category)en;
break;
}
if (fieldType == null)
{
fieldType = ModConfiguration.generatedTypes.First(x => x.Name == "ItemPhysic");
category = (int)Catalog.Category.Item;
}
foreach (var data in ModConfiguration.data[category])
{
//var dota = ModConfiguration.ConvertDynamic(data, ModConfiguration.generatedTypes.FirstOrDefault(x => x.Name.Contains(categorylocal.ToString())));
autoCompSrc.Add(fieldType.GetField("id").GetValue(data));
//allowedValues.Add(data);
if (fieldType.GetField("id").GetValue(data) != null) targetSource = "Default";
}
foreach (var data in ModConfiguration.current.modData[category])
{
autoCompSrc.Add(fieldType.GetField("id").GetValue(data));
//allowedValues.Add(data);
if (fieldType.GetField("id").GetValue(data) != null) targetSource = ModConfiguration.current.modName;
}
if (string.IsNullOrEmpty(targetSource))
{
targetSource = "Default";
}
//allowedValues.ForEach(x => autoCompSrc.Add(x.id));
InitializeComponent();
if (targetSource != "Default")
{
labelDefault.Visible = false;
}
else
{
labelDefault.Visible = true;
}
fieldValueLabel.Text = fieldValue + " (" + fieldType.Name + ")";
var copyField = fieldValue.Replace("Fire", ModConfiguration.current.modName);
copyRenameText.Text = copyField;
eventArgs = e;
ReferenceText.AutoCompleteCustomSource = autoCompSrc;
ReferenceText.AutoCompleteMode = AutoCompleteMode.Suggest;
ReferenceText.AutoCompleteSource = AutoCompleteSource.CustomSource;
//category = (Catalog.Category)Enum.Parse(typeof(Catalog.Category), fieldTypeName.Replace("Data", ""));
}
private void ReferenceManager_FormClosing(object sender, FormClosingEventArgs e)
{
OnReferenceComplete(null);
}
private void ReferenceManager_Load(object sender, EventArgs e)
{
}
private void fieldValueLabel_Click(object sender, EventArgs e)
{
}
private void copyButton_Click(object sender, EventArgs e)
{
dynamic result;
if (targetSource == "Default")
{
string val = Spell.dataGrid.Rows[eventArgs.RowIndex].Cells[eventArgs.ColumnIndex].Value as string;
//fieldType.GetField("id").GetValue();
result = ModConfiguration.data[category].FirstOrDefault(x => x.GetType().GetField("id").GetValue(x) == val);
}
else
{
throw new NotImplementedException();
}
//result = ModConfiguration.current.fieldInfoList.FirstOrDefault(x => x.FieldType == typeof(string) && ((string)x.GetValue(ModConfiguration.current.instancedType)) == fieldValue);
//var obj = ModConfiguration.FindAndCopy(targetSource);
ModConfiguration.current.CreateData(result, fieldType, copyRenameText.Text, category);
ChangeReference(copyRenameText.Text);
this.Hide();
}
public new void Hide()
{
OnReferenceComplete(null);
base.Hide();
}
private void changeRefButton_Click(object sender, EventArgs e)
{
string val = ReferenceText.Text;
if (!autoCompSrc.Contains(val))
{
Debug.Log(val + " is not a valid input.");
return;
}
ChangeReference(val);
this.Hide();
}
private void ChangeReference(string refName)
{
string val = refName;
var result = ModConfiguration.current.fieldInfoList.FirstOrDefault(x => x.FieldType == typeof(string) && ((string)x.GetValue(ModConfiguration.current.instancedType)) == fieldValue);
ModConfiguration.current.fieldInfoList.FirstOrDefault(x => x == result).SetValue(ModConfiguration.current.instancedType, ReferenceText.Text);
Spell.dataGrid.Rows[eventArgs.RowIndex].Cells[eventArgs.ColumnIndex].Value = refName;
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Hide();
}
}
}