Commit f137400f authored by mercury233's avatar mercury233

improve memory alloc by JoyJ

parent df92db27
...@@ -18,9 +18,6 @@ namespace ImgGen ...@@ -18,9 +18,6 @@ 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 Dictionary<int, Bitmap> cardImages = new Dictionary<int, Bitmap>();
private static SQLiteConnection conn; private static SQLiteConnection conn;
private static object locker = new object(); private static object locker = new object();
...@@ -151,35 +148,23 @@ namespace ImgGen ...@@ -151,35 +148,23 @@ namespace ImgGen
} }
public static Bitmap GetImage(int code) public static Bitmap GetImage(int code)
{
if (!cardImages.ContainsKey(code))
{
LoadCard(code);
}
return cardImages[code];
}
private static int LoadCard(int code)
{ {
lock (locker) lock (locker)
{ {
if (cardDatas.ContainsKey(code))
{
return 0;
}
Data data = new Data(); Data data = new Data();
Text text = new Text(); Text text = new Text();
data.code = code; data.code = code;
text.name = "???"; text.name = "???";
text.text = "???"; text.text = "???";
SQLiteCommand command = null;
SQLiteDataReader reader = null;
try try
{ {
conn.Open(); conn.Open();
SQLiteCommand command = new SQLiteCommand(conn); command = new SQLiteCommand(conn);
SQLiteDataReader reader;
command.CommandText = $"SELECT * FROM datas WHERE id={code}"; command.CommandText = $"SELECT * FROM datas WHERE id={code}";
reader = command.ExecuteReader(); reader = command.ExecuteReader();
if (reader.Read()) if (reader.Read())
{ {
data.code = reader.GetInt32(0); data.code = reader.GetInt32(0);
...@@ -196,28 +181,27 @@ namespace ImgGen ...@@ -196,28 +181,27 @@ namespace ImgGen
command.CommandText = $"SELECT * FROM texts WHERE id={code}"; command.CommandText = $"SELECT * FROM texts WHERE id={code}";
reader = command.ExecuteReader(); reader = command.ExecuteReader();
if (reader.Read()) if (reader.Read())
{ {
text.name = reader.GetString(1); text.name = reader.GetString(1);
text.text = reader.GetString(2); text.text = reader.GetString(2);
} }
reader.Close(); reader.Close();
return DrawCard(data, text);
} }
catch catch (Exception e)
{ {
Console.WriteLine($"Error when parsing {code} - {e}");
return null;
} }
finally finally
{ {
reader?.Close();
command?.Dispose();
conn.Close(); conn.Close();
} }
cardDatas.Add(code, data);
if (!cardImages.ContainsKey(code))
{
Bitmap bitmap;
bitmap = DrawCard(data, text);
cardImages.Add(code, bitmap);
}
return 0;
} }
} }
...@@ -249,7 +233,7 @@ namespace ImgGen ...@@ -249,7 +233,7 @@ namespace ImgGen
private static void DrawPicture(Graphics graphics, Data data) private static void DrawPicture(Graphics graphics, Data data)
{ {
Bitmap image; Bitmap image = null;
string filename = "./pico/" + data.code.ToString() + ".png"; string filename = "./pico/" + data.code.ToString() + ".png";
if (!File.Exists(filename)) if (!File.Exists(filename))
filename = "./pico/" + data.code.ToString() + ".jpg"; filename = "./pico/" + data.code.ToString() + ".jpg";
...@@ -259,11 +243,7 @@ namespace ImgGen ...@@ -259,11 +243,7 @@ namespace ImgGen
} }
catch (Exception e) catch (Exception e)
{ {
#if DEBUG Console.WriteLine($"Error when parsing {data.code} - {e}");
Console.WriteLine($"Error parsing [{data.code}] : {e.Message}");
#else
Console.WriteLine($"Error parsing [{data.code}] : {e}");
#endif
return; return;
} }
if (data.isType(Type.TYPE_PENDULUM)) if (data.isType(Type.TYPE_PENDULUM))
...@@ -290,6 +270,7 @@ namespace ImgGen ...@@ -290,6 +270,7 @@ namespace ImgGen
else else
graphics.DrawImage(image, dest); graphics.DrawImage(image, dest);
} }
image?.Dispose();
} }
private static void DrawTemplate(Graphics graphics, Data data) private static void DrawTemplate(Graphics graphics, Data data)
...@@ -359,6 +340,7 @@ namespace ImgGen ...@@ -359,6 +340,7 @@ namespace ImgGen
template = new Bitmap(bTemplates[8]); template = new Bitmap(bTemplates[8]);
} }
graphics.DrawImage(template, 0, 0, 400, 580); graphics.DrawImage(template, 0, 0, 400, 580);
template.Dispose();
} }
private static void DrawStars(Graphics graphics, Data data) private static void DrawStars(Graphics graphics, Data data)
...@@ -660,7 +642,7 @@ namespace ImgGen ...@@ -660,7 +642,7 @@ namespace ImgGen
DrawSpellTrapType(graphics, data); DrawSpellTrapType(graphics, data);
DrawSpellTrapEffect(graphics, desc); DrawSpellTrapEffect(graphics, desc);
} }
graphics.Dispose();
return bitmap; return bitmap;
} }
......
...@@ -39,7 +39,9 @@ namespace ImgGen ...@@ -39,7 +39,9 @@ namespace ImgGen
List<string> files = new List<string>(); List<string> files = new List<string>();
files.AddRange(Directory.GetFiles("./pico", "*.png")); files.AddRange(Directory.GetFiles("./pico", "*.png"));
files.AddRange(Directory.GetFiles("./pico", "*.jpg")); files.AddRange(Directory.GetFiles("./pico", "*.jpg"));
bool generateLarge = System.Configuration.ConfigurationManager.AppSettings["GenerateLarge"] != "False"; // true if AppSettings null
bool generateSmall = System.Configuration.ConfigurationManager.AppSettings["GenerateSmall"] == "True"; bool generateSmall = System.Configuration.ConfigurationManager.AppSettings["GenerateSmall"] == "True";
bool generateThumb = System.Configuration.ConfigurationManager.AppSettings["GenerateThumb"] == "True";
if (generateLarge) if (generateLarge)
Directory.CreateDirectory("./picn"); Directory.CreateDirectory("./picn");
if (generateSmall) if (generateSmall)
...@@ -53,20 +55,35 @@ namespace ImgGen ...@@ -53,20 +55,35 @@ namespace ImgGen
{ {
code = int.Parse(Path.GetFileNameWithoutExtension(str)); code = int.Parse(Path.GetFileNameWithoutExtension(str));
} }
catch (Exception) catch
{ {
continue; continue;
} }
string fileName = code.ToString() + ".jpg"; string fileName = code.ToString() + ".jpg";
Console.WriteLine($"Generating {fileName}"); Console.WriteLine($"Generating {fileName}");
Bitmap image = DataManager.GetImage(code); Bitmap image = DataManager.GetImage(code);
if (image == null)
{
Console.WriteLine($"[{code}] generation failed");
continue;
}
if (generateLarge) if (generateLarge)
{
image.Save("./picn/" + fileName, encoderInfo, encoderParams); image.Save("./picn/" + fileName, encoderInfo, encoderParams);
}
if (generateSmall) if (generateSmall)
DataManager.Zoom(image, 177, 254).Save("./pics/" + fileName, encoderInfo, encoderParams); {
Bitmap bmp = DataManager.Zoom(image, 177, 254);
bmp.Save("./pics/" + fileName, encoderInfo, encoderParams);
bmp.Dispose();
}
if (generateThumb) if (generateThumb)
DataManager.Zoom(image, 44, 64).Save("./pics/thumbnail/" + fileName, encoderInfo, encoderParams); {
image.Dispose(); Bitmap bmp = DataManager.Zoom(image, 44, 64);
bmp.Save("./pics/thumbnail/" + fileName, encoderInfo, encoderParams);
bmp.Dispose();
}
image?.Dispose();
} }
} }
} }
......
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