This repository was archived by the owner on May 4, 2024. It is now read-only.
forked from Core2002/PlayerDataBackupTool-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
360 lines (325 loc) · 12.4 KB
/
Form1.cs
File metadata and controls
360 lines (325 loc) · 12.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Driver;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace PlayerDataBackupTool_CSharp
{
public partial class Form1 : Form
{
MongoClient client;
public static ConfigPojo cfg;
public static Form1 Singleton;
public static SetConfigForm setConfigForm;
public Dictionary<string, string> dic;
public IMongoCollection<BsonDocument> getColl()
{
return client.GetDatabase(cfg.mongodb_database).GetCollection<BsonDocument>(cfg.mongodb_collection);
}
public Form1()
{
Singleton = this;
setConfigForm = new SetConfigForm();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
feflashdate();
timer1.Interval = 24 * 60 * 60 * 1000 / 100;
timer1.Start();
}
private void listPlayer_SelectedIndexChanged(object sender, EventArgs e)
{
listTime.Items.Clear();
if (listPlayer.SelectedItem == null)
return;
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
var res = getColl().Find(new BsonDocument("player_uuid", listPlayer.SelectedItem.ToString())).FirstOrDefault();
var json = JObject.Parse(res.ToJson(jsonWriterSettings)).ToString();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<PlayerInvDataPojo>(json);
foreach (var a in data.data.Keys)
listTime.Items.Add(a);
var k = listPlayer.SelectedItem.ToString();
if (dic.ContainsKey(k))
{
label2.Text = $"> {dic[k]}";
listPlayer.Text = k;
return;
}
else
{
textBox1.Text = "";
label2.Text = ">";
}
}
public void BackUp()
{
if (!feflashdate())
return;
string[] paths = Directory.GetFiles(cfg.world_playerdata_path);
var t = DateTime.Now.ToString();
foreach (var item in paths)
{
if (Path.GetExtension(item).ToLower() == ".dat")
{
string filename = Path.GetFileName(item);
string extension = Path.GetExtension(item);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item);
var res = getColl().Find(new BsonDocument("player_uuid", fileNameWithoutExtension));
if (res.Count() > 0)
{
var r = FromBson<PlayerInvDataPojo>(res.FirstOrDefault().ToBson());
r.data.Add(t, FileToBase64Str(item));
getColl().FindOneAndUpdate(new BsonDocument("player_uuid", r.player_uuid), r.ToBsonDocument());
}
else
{
var pojo = new PlayerInvDataPojo();
pojo.player_uuid = fileNameWithoutExtension;
pojo.data.Add(t, FileToBase64Str(item));
getColl().InsertOne(pojo.ToBsonDocument());
}
}
}
if (!feflashdate())
return;
}
private void button1_Click(object sender, EventArgs e)
{
BackUp();
MessageBox.Show("备份完毕", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 文件转为base64编码
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string FileToBase64Str(string filePath)
{
string base64Str = string.Empty;
try
{
using (FileStream filestream = new FileStream(filePath, FileMode.Open))
{
byte[] bt = new byte[filestream.Length];
//调用read读取方法
filestream.Read(bt, 0, bt.Length);
base64Str = Convert.ToBase64String(bt);
filestream.Close();
}
return base64Str;
}
catch (Exception)
{
return base64Str;
}
}
/// <summary>
/// 文件base64解码
/// </summary>
/// <param name="base64Str">文件base64编码</param>
/// <param name="outPath">生成文件路径</param>
public void Base64ToOriFile(string base64Str, string outPath)
{
var contents = Convert.FromBase64String(base64Str);
using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
fs.Write(contents, 0, contents.Length);
fs.Flush();
}
}
public static string ToBson<T>(T value)
{
using (MemoryStream ms = new MemoryStream())
using (BsonDataWriter datawriter = new BsonDataWriter(ms))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(datawriter, value);
return Convert.ToBase64String(ms.ToArray());
}
}
public static T FromBson<T>(byte[] data)
{
//byte[] data = Convert.FromBase64String(base64data);
using (MemoryStream ms = new MemoryStream(data))
using (BsonDataReader reader = new BsonDataReader(ms))
{
JsonSerializer serializer = new JsonSerializer();
return serializer.Deserialize<T>(reader);
}
}
private void button2_Click(object sender, EventArgs e)
{
var i = listTime.SelectedItem;
if (i == null)
{
MessageBox.Show("你需要选择要还原的时间点", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var uuid = listPlayer.SelectedItem.ToString();
var time = i.ToString();
var res = getColl().Find(new BsonDocument("player_uuid", uuid));
if (res.Count() > 0)
{
var r = FromBson<PlayerInvDataPojo>(res.FirstOrDefault().ToBson());
string base64data;
if (r.data.TryGetValue(time, out base64data))
{
var path = cfg.world_playerdata_path + uuid + ".dat";
Base64ToOriFile(base64data, path);
string name = "[未知玩家]";
if (dic.ContainsKey(uuid))
{
name = dic[uuid];
}
MessageBox.Show($"玩家{name},UUID:{uuid}\r\n还原到 {time} 成功\r\n路径:{path}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("还原失败->数据损坏", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("还原失败->玩家数据未备份", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
feflashdate();
}
private void button3_Click(object sender, EventArgs e)
{
if (!feflashdate())
return;
if ((int)MessageBox.Show("确定需要删库跑路吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == 1)
{
if ((int)MessageBox.Show("确定需要删库跑路吗?", "警告(确认2/3)", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == 1)
{
if ((int)MessageBox.Show("确定需要删库跑路吗?", "警告(确认3/3)", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == 1)
{
getColl().DeleteMany(new BsonDocument());
feflashdate();
MessageBox.Show("数据已删除", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
}
private void button4_Click(object sender, EventArgs e)
{
feflashdate();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
refalshsearch();
}
public void refalshsearch()
{
foreach (var k in dic.Keys)
{
if (dic.ContainsValue(textBox1.Text) && textBox1.Text.Equals(dic[k]))
{
label1.Text = $"[{dic[k]}]({k})";
listPlayer.Text = k;
return;
}
else
{
label1.Text = "查无此人";
}
}
}
public bool feflashdate()
{
try
{
cfg = Path2Pojo<ConfigPojo>(@"config.json");
client = new MongoClient(cfg.mongodb_uri);
listPlayer.Items.Clear();
getColl().Find(new BsonDocument()).ToList().ForEach(r =>
{
if (r.Contains("player_uuid"))
listPlayer.Items.Add(r.GetValue("player_uuid"));
});
string text = File.ReadAllText(cfg.uuid2name_path);
object obj = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
JObject js = obj as JObject;
dic = js.ToObject<Dictionary<string, string>>();
listTime.Items.Clear();
if (listPlayer.SelectedItem == null)
{
label2.Text = ">";
textBox1.Text = "";
return true;
}
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
var res = getColl().Find(new BsonDocument("player_uuid", listPlayer.SelectedItem.ToString())).FirstOrDefault();
var json = JObject.Parse(res.ToJson(jsonWriterSettings)).ToString();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<PlayerInvDataPojo>(json);
foreach (var a in data.data.Keys)
listTime.Items.Add(a);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "配置填写错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (!setConfigForm.Visible)
{
setConfigForm.ShowDialog();
return false;
}
}
return true;
}
private void label1_Click(object sender, EventArgs e)
{
refalshsearch();
}
public T Path2Pojo<T>(string path)
{
return new JavaScriptSerializer().Deserialize<T>(File.ReadAllText(path));
}
private void button5_Click(object sender, EventArgs e)
{
if (!setConfigForm.Visible)
{
setConfigForm.ShowDialog();
}
}
bool AutoBackUp { get; set; }
private void progressBar1_Click(object sender, EventArgs e)
{
AutoBackUp = !AutoBackUp;
if (AutoBackUp)
{
progressBar1.Value = 0;
timer1.Enabled = true;
MessageBox.Show("已开启自动备份");
}
else
{
progressBar1.Value = 0;
timer1.Enabled = false;
MessageBox.Show("已关闭自动备份");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (AutoBackUp)
{
if (progressBar1.Value != 100)
progressBar1.Value += 1;
else
progressBar1.Value = 0;
if (progressBar1.Value == 100)
{
BackUp();
}
}
}
}
}