This repository was archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathIDConverter.cs
More file actions
239 lines (196 loc) · 9.04 KB
/
IDConverter.cs
File metadata and controls
239 lines (196 loc) · 9.04 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace DevPro_CardManager
{
public sealed partial class IDConverter : Form
{
//string[] definition
//[0] = Old card ID
//[1] = New card ID
public IDConverter()
{
InitializeComponent();
TopLevel = false;
Dock = DockStyle.Fill;
Visible = true;
NewId.Enter += NewIDInput_Enter;
NewId.Leave += NewIDInput_Leave;
UpdateCardsList.DrawItem += NewIDList_DrawItem;
UpdateCardsList.KeyDown += DeleteItem;
}
private void DeleteItem(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
var list = (ListBox)sender;
if (list.SelectedIndex != -1)
list.Items.RemoveAt(list.SelectedIndex);
}
}
private void AddButton_Click(object sender, EventArgs e)
{
List<string[]> updateCards = UpdateCardsList.Items.OfType<string[]>().ToList();
if (SearchBox.List.SelectedIndex == -1)
{
MessageBox.Show("No card selected.", "Error!", MessageBoxButtons.OK);
return;
}
int newId;
if (!Int32.TryParse(NewId.Text, out newId))
{
MessageBox.Show("New Id is invalid.", "Error!", MessageBoxButtons.OK);
return;
}
if (CardManager.ContainsCard(newId) || updateCards.Exists(x => x[1] == newId.ToString(CultureInfo.InvariantCulture)))
{
MessageBox.Show("New Id is already been used.", "Error!", MessageBoxButtons.OK);
return;
}
int selectedCardId = Convert.ToInt32(SearchBox.List.SelectedItem.ToString());
if (updateCards.Exists(x => x[0] == selectedCardId.ToString(CultureInfo.InvariantCulture)))
{
MessageBox.Show("Card already in list to be changed", "Error!", MessageBoxButtons.OK);
return;
}
var cardToUpdate = new string[2];
cardToUpdate[0] = selectedCardId.ToString(CultureInfo.InvariantCulture);
cardToUpdate[1] = newId.ToString(CultureInfo.InvariantCulture);
UpdateCardsList.Items.Add(cardToUpdate);
}
private void ConvertButton_Click(object sender, EventArgs e)
{
ConvertButton.Enabled = false;
bool updateCdb = cdbchk.Checked;
bool updateScript = patchchk.Checked;
bool updateImage = imagechk.Checked;
List<string[]> updateCards = UpdateCardsList.Items.OfType<string[]>().ToList();
if (patchchk.Checked)
{
if (!Directory.Exists("DevPatch"))
Directory.CreateDirectory("DevPatch");
if (!Directory.Exists("DevPatch\\script"))
Directory.CreateDirectory("DevPatch\\script");
if (!Directory.Exists("DevPatch\\pics"))
Directory.CreateDirectory("DevPatch\\pics");
if (!Directory.Exists("DevPatch\\pics\\thumbnail"))
Directory.CreateDirectory("DevPatch\\pics\\thumbnail");
}
string str = "cards.cdb";
foreach (var updateCard in updateCards)
{
if (updateCdb)
{
if (!File.Exists(str))
{
MessageBox.Show("cards.cdb not found.");
return;
}
int cardid = Int32.Parse(updateCard[0]);
int newid = Int32.Parse(updateCard[1]);
CardManager.RenameKey(cardid, newid);
CardInfos card = CardManager.GetCard(newid);
card.Id = newid;
if (chkremovepre.Checked)
card.Ot = card.Ot & 0x03;
CardManager.UpdateOrAddCard(card);
var connection = new SQLiteConnection("Data Source=" + str);
connection.Open();
SQLiteCommands.UpdateCardId(updateCard[0], updateCard[1], connection);
if(chkremovepre.Checked)
SQLiteCommands.UpdateCardOt(updateCard[1], card.Ot.ToString(), connection);
connection.Close();
}
if (updateImage)
{
string mainDir = Directory.GetCurrentDirectory(); ;
const string picFolderName = "pics";
const string tumbnailFolderName = "pics\\thumbnail";
string picName = updateCard[0] + ".jpg";
string newPicName = updateCard[1] + ".jpg";
string imagePath = Path.Combine(mainDir, picFolderName, picName);
string newImagePath = Path.Combine(mainDir, picFolderName, newPicName);
string thumbnailImagePath = Path.Combine(mainDir, tumbnailFolderName, picName);
string newthumbnailImagePath = Path.Combine(mainDir, tumbnailFolderName, newPicName);
if (File.Exists(imagePath) && !File.Exists(newImagePath))
File.Move(imagePath, newImagePath);
if (File.Exists(thumbnailImagePath) && !File.Exists(newthumbnailImagePath))
File.Move(thumbnailImagePath, newthumbnailImagePath);
if (patchchk.Checked)
{
if(File.Exists(newImagePath))
File.Copy(newImagePath, Path.Combine("DevPatch\\pics", newPicName), true);
if (File.Exists(newthumbnailImagePath))
File.Copy(newthumbnailImagePath, Path.Combine("DevPatch\\pics\\thumbnail", newPicName), true);
}
}
if (updateScript)
{
string mainDir = Directory.GetCurrentDirectory(); ;
const string scriptFolderName = "script";
string scriptName = "c" + updateCard[0] + ".lua";
string newScriptName = "c" + updateCard[1] + ".lua";
string scriptPath = Path.Combine(mainDir, scriptFolderName, scriptName);
string newScriptPath = Path.Combine(mainDir, scriptFolderName, newScriptName);
if (File.Exists(scriptPath))
{
File.Move(scriptPath, newScriptPath);
//needs testing id replacing
string scriptFile = File.ReadAllText(newScriptPath);
scriptFile = scriptFile.Replace(updateCard[0], updateCard[1]);
File.WriteAllText(newScriptPath, scriptFile);
if (patchchk.Checked)
if(File.Exists(newScriptPath))
File.Copy(newScriptPath, Path.Combine("DevPatch\\script", newScriptName), true);
}
}
}
if (patchchk.Checked)
File.Copy(str, "DevPatch\\cards.cdb", true);
UpdateCardsList.Items.Clear();
MessageBox.Show("Complete.");
ConvertButton.Enabled = true;
}
#region Form Desgin
private void NewIDInput_Enter(object sender, EventArgs e)
{
if (NewId.Text == "New ID")
{
NewId.Text = "";
NewId.ForeColor = SystemColors.WindowText;
}
}
private void NewIDInput_Leave(object sender, EventArgs e)
{
if (NewId.Text == "")
{
NewId.Text = "New ID";
NewId.ForeColor = SystemColors.WindowFrame;
}
}
private void NewIDList_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int index = e.Index;
if (index >= 0 && index < UpdateCardsList.Items.Count)
{
var data = (string[])UpdateCardsList.Items[index];
Graphics g = e.Graphics;
CardInfos card = CardManager.GetCard(Int32.Parse(data[0]));
g.FillRectangle((selected) ? new SolidBrush(Color.Blue) : new SolidBrush(Color.White), e.Bounds);
// Print text
g.DrawString((card.Name == "" ? "???" : card.Name) + " - " + data[0] + " > " + data[1], e.Font, (selected) ? Brushes.White : Brushes.Black,
UpdateCardsList.GetItemRectangle(index).Location);
}
e.DrawFocusRectangle();
}
#endregion
}
}