Commit edcd0708 authored by mercury233's avatar mercury233

cache cards database

parent b72f75f3
...@@ -88,14 +88,6 @@ ...@@ -88,14 +88,6 @@
LINK_MARKER_TOP_RIGHT = 0x100 LINK_MARKER_TOP_RIGHT = 0x100
} }
[StructLayout(LayoutKind.Sequential)]
public struct Text
{
public string name;
public string text;
public string[] desc;
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Data public struct Data
{ {
...@@ -108,6 +100,8 @@ ...@@ -108,6 +100,8 @@
public Race race; public Race race;
public int attack; public int attack;
public int defence; public int defence;
public string name;
public string text;
public bool isType(Type typ) public bool isType(Type typ)
{ {
......
...@@ -18,6 +18,8 @@ namespace ImgGen ...@@ -18,6 +18,8 @@ namespace ImgGen
private static Bitmap[] bLinkNums = new Bitmap[8]; private static Bitmap[] bLinkNums = new Bitmap[8];
private static Bitmap[] bLinkMarkers = new Bitmap[9]; private static Bitmap[] bLinkMarkers = new Bitmap[9];
private static Dictionary<int, Data> cardDatas = new Dictionary<int, Data>();
private static SQLiteConnection conn; private static SQLiteConnection conn;
private static object locker = new object(); private static object locker = new object();
...@@ -149,68 +151,49 @@ namespace ImgGen ...@@ -149,68 +151,49 @@ namespace ImgGen
if (i == 5) continue; if (i == 5) continue;
bLinkMarkers[i - 1] = new Bitmap($"./textures/link_marker_on_{i}.png"); bLinkMarkers[i - 1] = new Bitmap($"./textures/link_marker_on_{i}.png");
} }
conn.Open();
SQLiteCommand command = new SQLiteCommand(conn);
command.CommandText = "SELECT * FROM datas JOIN texts on datas.id = texts.id";
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Data data = new Data();
data.code = (int)(long)reader["id"];
data.alias = (int)(long)reader["alias"];
data.setcode = (int)(long)reader["setcode"];
data.type = (Type)(long)reader["type"];
data.attack = (int)(long)reader["atk"];
data.defence = (int)(long)reader["def"];
data.level = (int)(long)reader["level"];
data.race = (Race)(long)reader["race"];
data.attribute = (Attribute)(long)reader["attribute"];
data.name = (string)reader["name"];
data.text = (string)reader["desc"];
cardDatas.Add(data.code, data);
}
reader.Close();
command.Dispose();
conn.Close();
} }
public static Bitmap GetImage(int code) public static Bitmap GetImage(int code)
{ {
lock (locker) Data data;
if(cardDatas.ContainsKey(code))
{ {
Data data = new Data(); data = cardDatas[code];
Text text = new Text(); }
else
{
Console.WriteLine($"Card {code} not found!");
data = new Data();
data.code = code; data.code = code;
text.name = "???"; data.name = "???";
text.text = "???"; data.text = "???";
SQLiteCommand command = null;
SQLiteDataReader reader = null;
try
{
conn.Open();
command = new SQLiteCommand(conn);
command.CommandText = $"SELECT * FROM datas WHERE id={code}";
reader = command.ExecuteReader();
if (reader.Read())
{
data.code = (int)(long)reader["id"];
data.alias = (int)(long)reader["alias"];
data.setcode = (int)(long)reader["setcode"];
data.type = (Type)(long)reader["type"];
data.attack = (int)(long)reader["atk"];
data.defence = (int)(long)reader["def"];
data.level = (int)(long)reader["level"];
data.race = (Race)(long)reader["race"];
data.attribute = (Attribute)(long)reader["attribute"];
}
reader.Close();
reader = null; // for Exception -> finally GC
command.CommandText = $"SELECT * FROM texts WHERE id={code}";
reader = command.ExecuteReader();
if (reader.Read())
{
text.name = (string)reader["name"];
text.text = (string)reader["desc"];
}
reader.Close();
return DrawCard(data, text);
}
catch (Exception e)
#if DEBUG
when (false)
#endif
{
Console.WriteLine($"Error when parsing {code} - {e}");
return null;
}
finally
{
reader?.Close();
command?.Dispose();
conn.Close();
}
} }
return DrawCard(data);
} }
private static string FormatCardDesc(string r) private static string FormatCardDesc(string r)
...@@ -607,7 +590,7 @@ namespace ImgGen ...@@ -607,7 +590,7 @@ namespace ImgGen
graphics.ResetTransform(); graphics.ResetTransform();
} }
private static Bitmap DrawCard(Data data, Text text) private static Bitmap DrawCard(Data data)
{ {
Bitmap bitmap = new Bitmap(400, 580); Bitmap bitmap = new Bitmap(400, 580);
Graphics graphics = Graphics.FromImage(bitmap); Graphics graphics = Graphics.FromImage(bitmap);
...@@ -618,8 +601,8 @@ namespace ImgGen ...@@ -618,8 +601,8 @@ namespace ImgGen
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
string name = FormatCardName(text.name); string name = FormatCardName(data.name);
string desc = FormatCardDesc(text.text); string desc = FormatCardDesc(data.text);
DrawPicture(graphics, data); DrawPicture(graphics, data);
DrawTemplate(graphics, data); DrawTemplate(graphics, data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment