Skip to content

Commit d814645

Browse files
committed
key valied updated,and add exception catch on getUpdate
1 parent 879f782 commit d814645

File tree

3 files changed

+84
-35
lines changed

3 files changed

+84
-35
lines changed

CDK/CDKCommand.cs

+61-21
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,44 @@ private EKeyReemeResult RedeemCDK(UnturnedPlayer player, string CDK)
232232

233233
private bool KeyVailed(CDKData cdk)
234234
{
235+
if (Main.Instance.Configuration.Instance.ByPassKeyVailed)
236+
{
237+
return true;
238+
}
239+
240+
// 密钥验证逻辑
241+
// 首先判断载具栏是否为空字符串,如果不是,则尝试解析GUID 如果是空GUID则为无效KEY,反之尝试用尼尔森接口解析GUID,如果GUID无效,则视为无效KET
242+
// 然后尝试解析数字ID,如果不是数字ID,则视为无效KEY
243+
// 物品字符串验证逻辑为将物品字符串使用英文逗号分割,检查分割后的列表,如果存在非数字值,则视为无效KEY
244+
// 数量字符串验证逻辑为,将字符串使用英文逗号分割,如果物品和数量的长度不同,则视为无效KEY
245+
// 除此之外验证数量是否为无效值,例如超出最大值,或非数字
246+
if (!string.IsNullOrEmpty(cdk.Vehicle))
247+
{
248+
if (Guid.TryParse(cdk.Vehicle, out Guid result))
249+
{
250+
if (result == Guid.Empty)
251+
{
252+
Logger.LogError(string.Format("can't use a empty GUID in CDK:{0}", cdk.CDK));
253+
return false;
254+
}
255+
256+
var gid = Assets.find(result);
257+
if (gid == null)
258+
{
259+
Logger.LogError(string.Format("CDK: {0} use an invailed vehicle GUID",cdk.CDK));
260+
return false;
261+
}
262+
}
263+
else
264+
{
265+
if (!ushort.TryParse(cdk.Vehicle,out ushort vehicleID))
266+
{
267+
Logger.LogError(string.Format("CDK: {0} use an invailed vehicle id", cdk.CDK));
268+
return false;
269+
}
270+
}
271+
}
272+
235273
if (cdk.Amount == string.Empty && cdk.Items != string.Empty)
236274
{
237275
var list = cdk.Items.Split(',').ToList();
@@ -243,39 +281,41 @@ private bool KeyVailed(CDKData cdk)
243281
return false;
244282
}
245283
}
246-
return true;
247284
}
248-
List<string> listitem = cdk.Items.Split(',').ToList();
249-
List<string> listamount = cdk.Amount.Split(',').ToList();
250-
if (listitem.Count != 0 && listamount.Count != 0)
285+
286+
if (cdk.Amount != string.Empty && cdk.Items != string.Empty)
251287
{
252-
if (listitem.Count != listamount.Count)
253-
{
254-
Logger.LogError(String.Format("CDK:{0} Items and Amount Column length not equal! ", cdk.CDK));
255-
return false;
256-
}
257-
258-
for (int i = 0; i < listitem.Count; i++)
288+
List<string> listitem = cdk.Items.Split(',').ToList();
289+
List<string> listamount = cdk.Amount.Split(',').ToList();
290+
if (listitem.Count != 0 && listamount.Count != 0)
259291
{
260-
if (!ushort.TryParse(listitem[i], out ushort id))
292+
if (listitem.Count != listamount.Count)
261293
{
262-
Logger.LogError(String.Format("CDK:{0} has id in Items not a ushort!", cdk.CDK));
294+
Logger.LogError(String.Format("CDK:{0} Items and Amount Column length not equal! ", cdk.CDK));
263295
return false;
264296
}
265-
}
266297

267-
for (int i = 0; i < listamount.Count; i++)
268-
{
269-
if (!byte.TryParse(listamount[i], out byte am))
298+
for (int i = 0; i < listitem.Count; i++)
270299
{
271-
Logger.LogError(String.Format("CDK:{0} has amount in Amount not a byte. MAX 255!", cdk.CDK));
272-
return false;
300+
if (!ushort.TryParse(listitem[i], out ushort id))
301+
{
302+
Logger.LogError(String.Format("CDK:{0} has id in Items not a ushort!", cdk.CDK));
303+
return false;
304+
}
305+
}
306+
307+
for (int i = 0; i < listamount.Count; i++)
308+
{
309+
if (!byte.TryParse(listamount[i], out byte am))
310+
{
311+
Logger.LogError(String.Format("CDK:{0} has amount in Amount not a byte. MAX 255!", cdk.CDK));
312+
return false;
313+
}
273314
}
274315
}
275-
return true;
276316
}
277317

278-
return false;
318+
return true;
279319
}
280320

281321
private void ExecuteUconomy(UnturnedPlayer player, CDKData cdkdata)

CDK/Config.cs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Config : IRocketPluginConfiguration
2121
public string DatabaseRedeemLogTableName;
2222
public int DatabasePort;
2323
public bool EnableRichText;
24+
public bool ByPassKeyVailed;
2425

2526
public void LoadDefaults()
2627
{
@@ -35,6 +36,7 @@ public void LoadDefaults()
3536
DatabaseRedeemLogTableName = "redeemedlog";
3637
DatabasePort = 3306;
3738
EnableRichText = false;
39+
ByPassKeyVailed = false;
3840
//CDKs = new List<CDK>() { new CDK() { Key = "Key", Items = "100,1000,1500",XP = 111 , RedeemPermission = "Test",GrantPermissionGroup="VIP1" ,MaxCount = 100,CurrentCount = 0,Vehicle = 1,Money = 1000 } };
3941
}
4042
}

CDK/Main.cs

+21-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.IO;
1818
using Rocket.Core.Logging;
1919
using Newtonsoft.Json.Linq;
20+
using Logger = Rocket.Core.Logging.Logger;
2021

2122
namespace CDK
2223
{
@@ -70,21 +71,27 @@ private void PlayerConnect(UnturnedPlayer player)
7071

7172
private void CheckUpdate()
7273
{
73-
74-
string dlstring = "https://api.github.com/repos/zeng-github01/CDKey-CodeReward/releases/latest";
75-
WebClient webClient = new WebClient();
76-
webClient.Headers.Add("user-agent",USER_AGENT);
77-
string jsonstring = webClient.DownloadString(dlstring);
78-
var json = JObject.Parse(jsonstring);
79-
Version version = new Version(json["tag_name"].ToString());
80-
Version crv = Assembly.GetName().Version;
81-
if(version > crv)
74+
try
8275
{
83-
var changelog = json["body"].ToString();
84-
Rocket.Core.Logging.Logger.Log(String.Format("New Update {0} has been released",version.ToString()),ConsoleColor.Green);
85-
Rocket.Core.Logging.Logger.LogWarning(String.Format("Changelog: {0}",changelog));
86-
Rocket.Core.Logging.Logger.LogWarning($"{Name} has been unload");
87-
Rocket.Core.Logging.Logger.Log("Go to " + "https://github.com/zeng-github01/CDKey-CodeReward/releases/latest "+"to get latest update", ConsoleColor.Yellow);
76+
string dlstring = "https://api.github.com/repos/zeng-github01/CDKey-CodeReward/releases/latest";
77+
WebClient webClient = new WebClient();
78+
webClient.Headers.Add("user-agent", USER_AGENT);
79+
string jsonstring = webClient.DownloadString(dlstring);
80+
var json = JObject.Parse(jsonstring);
81+
Version version = new Version(json["tag_name"].ToString());
82+
Version crv = Assembly.GetName().Version;
83+
if (version > crv)
84+
{
85+
var changelog = json["body"].ToString();
86+
Logger.Log(String.Format("New Update {0} has been released", version.ToString()), ConsoleColor.Green);
87+
Logger.LogWarning(String.Format("Changelog: {0}", changelog));
88+
Logger.LogWarning($"{Name} has been unload");
89+
Logger.Log("Go to " + "https://github.com/zeng-github01/CDKey-CodeReward/releases/latest " + "to get latest update", ConsoleColor.Yellow);
90+
}
91+
}
92+
catch (WebException ex)
93+
{
94+
Logger.LogException(ex, ex.Message);
8895
}
8996
}
9097
}

0 commit comments

Comments
 (0)