forked from Buttys/DevProCardManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardManager.cs
More file actions
128 lines (110 loc) · 3.7 KB
/
CardManager.cs
File metadata and controls
128 lines (110 loc) · 3.7 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
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
namespace DevPro_CardManager
{
public static class CardManager
{
private static Dictionary<int, CardInfos> CardData = new Dictionary<int, CardInfos>();
private static Dictionary<int, CDBData> loadedCDB = new Dictionary<int, CDBData>();
private static void RenameKey<TKey, TValue>(this IDictionary<TKey, TValue> dic,
TKey fromKey, TKey toKey)
{
TValue value = dic[fromKey];
dic.Remove(fromKey);
dic[toKey] = value;
}
public static bool LoadCDB(string dir, bool overwrite,bool clearData = false)
{
if (!File.Exists(dir))
return false;
if (clearData)
{
CardData.Clear();
loadedCDB.Clear();
}
SQLiteConnection connection = new SQLiteConnection("Data Source=" + dir);
List<string[]> datas = new List<string[]>();
List<string[]> texts = new List<string[]>();
try
{
connection.Open();
datas = SQLiteCommands.LoadData(connection);
texts = SQLiteCommands.LoadText(connection);
connection.Close();
}
catch(Exception)
{
connection.Close();
return false;
}
int cdbsource = loadedCDB.Count + 1;
loadedCDB.Add(cdbsource, new CDBData(clearData ? "Master": Path.GetFileNameWithoutExtension(dir), dir));
foreach (string[] row in datas)
{
if (overwrite)
CardManager.UpdateOrAddCard(new CardInfos(row, cdbsource));
else
{
if (!CardManager.ContainsCard(Int32.Parse(row[0])))
CardManager.UpdateOrAddCard(new CardInfos(row, cdbsource));
}
}
foreach (string[] row in texts)
{
if (CardManager.ContainsCard(Int32.Parse(row[0])))
CardManager.GetCard(Int32.Parse(row[0])).SetCardText(row);
}
return true;
}
public static CardInfos GetCard(int id)
{
if (CardData.ContainsKey(id))
return CardData[id];
return null;
}
public static bool RemoveCard(int id)
{
if (CardData.ContainsKey(id))
return CardData.Remove(id);
return false;
}
public static void UpdateOrAddCard(CardInfos card)
{
if (ContainsCard(card.Id))
CardData[card.Id] = card;
else
CardData.Add(card.Id, card);
}
public static bool ContainsCard(int id)
{
return CardData.ContainsKey(id);
}
public static void RenameKey(int fromkey,int tokey)
{
CardData.RenameKey(fromkey, tokey);
}
public static Dictionary<int, CardInfos>.KeyCollection GetKeys()
{
return CardData.Keys;
}
public static int Count
{
get { return CardData.Count; }
}
public static string GetDatabaseDir(int source)
{
if (loadedCDB.ContainsKey(source))
return loadedCDB[source].Dir;
return null;
}
public static string[] GetDatabaseNames()
{
List<string> keys = new List<string>();
foreach (int key in loadedCDB.Keys)
keys.Add(loadedCDB[key].Name);
return keys.ToArray();
}
}
}