Skip to content

Commit c2b0412

Browse files
committed
Fix a issue with Loading and Saving Databases
1 parent 7da68f3 commit c2b0412

14 files changed

+56
-23
lines changed

CLib.dll

0 Bytes
Binary file not shown.

CLibCompression.dll

0 Bytes
Binary file not shown.

CLibCompression_x64.dll

0 Bytes
Binary file not shown.

CLibDataBaseEditor.exe

0 Bytes
Binary file not shown.

CLibDatabase.dll

1 KB
Binary file not shown.

CLibDatabase_x64.dll

1 KB
Binary file not shown.

CLibLogging.dll

0 Bytes
Binary file not shown.

CLibLogging_x64.dll

0 Bytes
Binary file not shown.

CLibSocket.dll

0 Bytes
Binary file not shown.

CLibSocket_x64.dll

0 Bytes
Binary file not shown.

CLib_x64.dll

0 Bytes
Binary file not shown.

extensions/CLib/CLib/Debugger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Debugger()
1717
InitializeComponent();
1818

1919
#if Debug
20-
this.Show()
20+
this.Show();
2121
#else
2222
this.Hide();
2323
#endif

extensions/CLib/CLibDataBaseEditor/Form1.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void SaveFile(string filePath)
109109
#region Convertion
110110
private JSONNode ConvertToJson()
111111
{
112-
JSONNode json = JSON.Parse("{}");
112+
JSONNode json = new JSONObject();
113113
foreach (KeyValuePair<string, string> item in database)
114114
{
115115
json.Add(item.Key, new JSONString(item.Value));

extensions/CLib/CLibDatabase/DllEntry.cs

+54-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using System.Runtime.InteropServices;
88
using System.Text;
9+
using System.Xml.Linq;
910
using SimpleJSON;
1011

1112
namespace CLibDatabase
@@ -14,7 +15,7 @@ public class DllEntry
1415
{
1516
private static string loadedDatabase = "";
1617

17-
private static readonly string databaseFolder =
18+
private static string databaseFolder =
1819
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CLibDatabase");
1920

2021
private static Dictionary<string, string> database = new Dictionary<string, string>();
@@ -65,6 +66,14 @@ private static string GetVersion()
6566

6667
return "0.0.0.0";
6768
}
69+
[DllExport("SetExportPath")]
70+
public static string SetExportPath(string path)
71+
{
72+
databaseFolder = path;
73+
if (!Directory.Exists(databaseFolder))
74+
Directory.CreateDirectory(databaseFolder);
75+
return "true";
76+
}
6877

6978
[DllExport("KeyExists")]
7079
public static string KeyExists(string key)
@@ -97,10 +106,10 @@ public static string Load(string filename)
97106
{
98107
if (loadedDatabase == filename)
99108
return "true";
100-
109+
101110
using (FileStream fs = File.OpenRead(Path.Combine(databaseFolder, filename + ".clibdata")))
102111
{
103-
GZipStream cmp = new GZipStream(fs, CompressionLevel.Optimal);
112+
GZipStream cmp = new GZipStream(fs, CompressionMode.Decompress);
104113
using (BinaryReader reader = new BinaryReader(cmp))
105114
{
106115
int count = reader.ReadInt32();
@@ -121,11 +130,10 @@ public static string Load(string filename)
121130
[DllExport("Save")]
122131
public static string Save(string filename)
123132
{
124-
using (FileStream fs = File.OpenWrite(Path.Combine(
125-
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CLibDatabase",
126-
filename + ".clibdata")))
133+
string path = Path.Combine(databaseFolder, filename + ".clibdata");
134+
using (FileStream fs = File.OpenWrite(path))
127135
{
128-
GZipStream dcmp = new GZipStream(fs, CompressionMode.Decompress);
136+
GZipStream dcmp = new GZipStream(fs, CompressionLevel.Optimal);
129137

130138
using (BinaryWriter writer = new BinaryWriter(dcmp))
131139
{
@@ -136,14 +144,13 @@ public static string Save(string filename)
136144
writer.Write(pair.Value);
137145
}
138146
}
139-
140-
return "true";
147+
return $"File Exported to {path}";
141148
}
142149
}
143150

144151
private static JSONNode ConvertToJson()
145152
{
146-
JSONNode json = JSON.Parse("{}");
153+
JSONNode json = new JSONObject();
147154
foreach (KeyValuePair<string, string> item in database) json.Add(item.Key, item.Value);
148155
return json;
149156
}
@@ -154,6 +161,23 @@ private static void ConvertToDictionary(JSONNode json)
154161
foreach (KeyValuePair<string, JSONNode> item in json.Linq) database.Add(item.Key, item.Value.Value);
155162
}
156163

164+
private static void ConvertToDictionary(XContainer xml)
165+
{
166+
database.Clear();
167+
foreach (XElement item in xml.Elements())
168+
{
169+
database.Add(item.Name.LocalName, item.Value);
170+
}
171+
}
172+
private static XDocument ConvertToXML()
173+
{
174+
XDocument xml = new XDocument();
175+
foreach (KeyValuePair<string, string> item in database)
176+
{
177+
xml.Add(item.Key, new JSONString(item.Value));
178+
}
179+
return xml;
180+
}
157181
#region Import/Export
158182

159183
[DllExport("ExportJson")]
@@ -163,26 +187,30 @@ public static string ExportJson(string filename)
163187
StringBuilder exportStringBuilder = new StringBuilder();
164188
json.WriteToStringBuilder(exportStringBuilder, 0, 4, JSONTextMode.Indent);
165189
File.WriteAllText(
166-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CLibDatabase",
167-
filename + ".json"), exportStringBuilder.ToString());
190+
Path.Combine(databaseFolder, filename + ".json"), exportStringBuilder.ToString());
168191
return "true";
169192
}
170193

171194
[DllExport("ExportJsonBinary")]
172195
public static string ExportJsonBinary(string filename)
173196
{
174197
JSONNode json = ConvertToJson();
175-
json.SaveToCompressedFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
176-
"CLibDatabase", filename + ".bson"));
198+
json.SaveToCompressedFile(Path.Combine(databaseFolder, filename + ".bson"));
199+
return "true";
200+
}
201+
202+
[DllExport("ExportXml")]
203+
public static string ExportXml(string filePath)
204+
{
205+
XDocument xml = ConvertToXML();
206+
xml.Save(filePath, SaveOptions.OmitDuplicateNamespaces);
177207
return "true";
178208
}
179209

180210
[DllExport("ImportJson")]
181211
public static string ImportJson(string filename)
182212
{
183-
string jsonStr = File.ReadAllText(Path.Combine(
184-
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CLibDatabase",
185-
filename + ".json"));
213+
string jsonStr = File.ReadAllText(Path.Combine(databaseFolder, filename + ".json"));
186214
JSONNode json = JSON.Parse(jsonStr);
187215
ConvertToDictionary(json);
188216
return "true";
@@ -191,18 +219,23 @@ public static string ImportJson(string filename)
191219
[DllExport("ImportJsonBinary")]
192220
public static string ImportJsonBinary(string filename)
193221
{
194-
JSONNode json = JSONNode.LoadFromCompressedFile(Path.Combine(
195-
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CLibDatabase",
196-
filename + ".bson"));
222+
JSONNode json = JSONNode.LoadFromCompressedFile(Path.Combine(databaseFolder, filename + ".bson"));
197223
ConvertToDictionary(json);
198224
return "true";
199225
}
200226

227+
public static string ImportXml(string filePath)
228+
{
229+
XDocument xml = XDocument.Load(filePath);
230+
ConvertToDictionary(xml);
231+
return "true";
232+
}
233+
201234
#endregion Import/Export
202235

203236
~DllEntry()
204237
{
205238
Save(loadedDatabase);
206239
}
207240
}
208-
}
241+
}

0 commit comments

Comments
 (0)