forked from DatZach/CharacterEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
124 lines (102 loc) · 3.07 KB
/
Database.cs
File metadata and controls
124 lines (102 loc) · 3.07 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
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using zlib;
namespace CharacterEditor
{
public class Database
{
private SQLiteConnection connection;
public void Load(string filename)
{
string connectionString = String.Format("Data Source={0};Version=3", filename);
connection = new SQLiteConnection(connectionString);
connection.Open();
}
public byte[] ReadCharacterBlob(int index)
{
byte[] stream = ReadBlobByIndex(index);
byte[] decompressedStream;
DecompressData(stream, out decompressedStream);
return decompressedStream;
}
public bool WriteCharacterBlob(int index, byte[] data)
{
byte[] compressedStream;
CompressData(data, out compressedStream);
return WriteBlobByIndex(index, compressedStream);
}
public bool WriteBlobByIndex(int index, byte[] data)
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = String.Format("UPDATE `blobs` SET `value`=@blobData WHERE `key`='{0}'", index);
command.Parameters.Add(new SQLiteParameter("@blobData", DbType.Binary)
{
Value = data
});
return command.ExecuteNonQuery() == 1;
}
}
public byte[] ReadBlobByIndex(int index)
{
return ReadBlobByKey(index.ToString("G"));
}
public byte[] ReadBlobByKey(string index)
{
DataTable table = new DataTable();
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = String.Format("SELECT `value` FROM `blobs` WHERE `key`='{0}'", index);
SQLiteDataReader reader = command.ExecuteReader();
table.Load(reader);
reader.Close();
}
if (table.Rows.Count == 0)
return null;
return (byte[])table.Rows[0].ItemArray.First();
}
private static void CompressData(byte[] inData, out byte[] outData)
{
using (MemoryStream outMemoryStream = new MemoryStream())
{
using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
{
using (Stream inMemoryStream = new MemoryStream(inData))
{
CopyStream(inMemoryStream, outZStream);
outZStream.finish();
outData = outMemoryStream.ToArray();
}
}
}
}
private static void DecompressData(byte[] inData, out byte[] outData)
{
using (MemoryStream outMemoryStream = new MemoryStream())
{
using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))
{
using (Stream inMemoryStream = new MemoryStream(inData))
{
CopyStream(inMemoryStream, outZStream);
outZStream.finish();
outData = outMemoryStream.ToArray();
}
}
}
}
private static void CopyStream(Stream input, Stream output)
{
// You may be tempted to change this to 2048, don't, zlib doesn't like it
const int bufferLength = 2000;
byte[] buffer = new byte[bufferLength];
int len;
while ((len = input.Read(buffer, 0, bufferLength)) > 0)
output.Write(buffer, 0, len);
output.Flush();
}
}
}