-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
416 lines (327 loc) · 14.8 KB
/
Utils.cs
File metadata and controls
416 lines (327 loc) · 14.8 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using Microsoft.Xna.Framework.Input;
using ProgressLock.Enums;
using ProgressLock.Models.Entries;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.Config;
using tModPorter;
using static ProgressLock.ProgressLockWorld;
namespace ProgressLock
{
public class Utils
{
public static string FormatTimeSpan(TimeSpan ts)
{
// 判断游戏客户端是否为中文
bool isChinese = Language.ActiveCulture.Name.StartsWith("zh", StringComparison.OrdinalIgnoreCase);
if (ts <= TimeSpan.Zero)
return isChinese ? "0秒" : "0s";
var sb = new StringBuilder();
if (ts.Days > 0)
sb.Append(isChinese ? $"{ts.Days}天" : $"{ts.Days}d");
if (ts.Hours > 0)
sb.Append(isChinese ? $" {ts.Hours}小时" : $" {ts.Hours}h");
if (ts.Minutes > 0)
sb.Append(isChinese ? $" {ts.Minutes}分钟" : $" {ts.Minutes}m");
if (ts.Seconds > 0)
sb.Append(isChinese ? $" {ts.Seconds}秒" : $" {ts.Seconds}s");
return sb.ToString().Trim();
}
public static string GetNpcProgressInfo(Player player, string pageStr)
{
string GetMsg(string key, params object[] args) => Language.GetTextValue($"Mods.ProgressLock.ProgressInfo.{key}", args);
StringBuilder sb = new StringBuilder();
var config = ProgressLockConfig.Config;
sb.AppendLine(GetMsg("NpcHeader"));
sb.AppendLine(GetMsg("FirstTime", config.FirstTime));
if (config.NpcEntries == null || config.NpcEntries.Count == 0)
{
sb.AppendLine(GetMsg("AllUnlocked"));
return sb.ToString();
}
if (!DateTime.TryParse(config.FirstTime, out DateTime baseTime))
{
return "Error: Invalid FirstTime format in config.";
}
int pageSize = 10;
int totalCount = config.NpcEntries.Count;
int maxPage = (int)Math.Ceiling((double)totalCount / pageSize);
if (!int.TryParse(pageStr, out int page) || page < 1) page = 1;
if (page > maxPage) page = maxPage;
var currentPageEntries = config.NpcEntries
.Skip((page - 1) * pageSize)
.Take(pageSize);
int entryIndex = (page - 1) * pageSize + 1;
DateTime now = DateTime.Now;
foreach (var entry in currentPageEntries)
{
// 过滤掉未加载的项
if (entry.DefinitionList == null || entry.DefinitionList.Count == 0 || entry.DefinitionList[0].IsUnloaded)
{
continue;
}
string npcName = Lang.GetNPCNameValue(entry.DefinitionList[0].Type);
DateTime unlockDate = baseTime.AddSeconds(entry.UnlockTimeSec);
if (entry.LockMode == LockMode.ManuallyLocked)
{
sb.AppendLine($"[{entryIndex}] {npcName}: {GetMsg("ManuallyLocked")}");
}
else if (entry.LockMode == LockMode.ManuallyUnlocked || (entry.LockMode == LockMode.Automatic && now >= unlockDate))
{
sb.AppendLine($"[{entryIndex}] {npcName}: {GetMsg("Unlocked")}");
}
else // 锁定状态(倒计时)
{
string timeLeft = FormatTimeSpan(unlockDate - now);
string dateStr = unlockDate.ToString("MM-dd HH:mm:ss");
sb.AppendLine($"[{entryIndex}] {npcName}: {GetMsg("UnlockCountdown", timeLeft, dateStr)}");
}
entryIndex++;
}
// 6. 页脚
sb.AppendLine($"======== {page} / {maxPage} ========");
return sb.ToString();
}
public static string GetEventProgressInfo(Player player, string pageStr)
{
string GetMsg(string key, params object[] args) => Language.GetTextValue($"Mods.ProgressLock.ProgressInfo.{key}", args);
StringBuilder sb = new StringBuilder();
var config = ProgressLockConfig.Config;
// 基础头部信息
sb.AppendLine(GetMsg("EventHeader"));
sb.AppendLine(GetMsg("FirstTime", config.FirstTime));
if (config.EventEntries == null || config.EventEntries.Count == 0)
{
sb.AppendLine(GetMsg("AllUnlocked"));
return sb.ToString();
}
if (!DateTime.TryParse(config.FirstTime, out DateTime baseTime))
{
return "Error: Invalid FirstTime format in config.";
}
// 计算分页逻辑
int pageSize = 10;
int totalCount = config.EventEntries.Count;
int maxPage = (int)Math.Ceiling((double)totalCount / pageSize);
if (!int.TryParse(pageStr, out int page) || page < 1) page = 1;
if (page > maxPage) page = maxPage;
var currentPageEntries = config.EventEntries
.Skip((page - 1) * pageSize)
.Take(pageSize);
int entryIndex = (page - 1) * pageSize + 1;
DateTime now = DateTime.Now;
foreach (var entry in currentPageEntries)
{
string eventName = Language.GetTextValue($"Mods.ProgressLock.Configs.VanillaEvent.{entry.Name}.Label");
DateTime unlockDate = baseTime.AddSeconds(entry.UnlockTimeSec);
// 使用新的 LockMode 枚举进行判断
if (entry.LockMode == LockMode.ManuallyLocked)
{
sb.AppendLine($"[{entryIndex}] {eventName}: {GetMsg("ManuallyLocked")}");
}
else if (entry.LockMode == LockMode.ManuallyUnlocked || (entry.LockMode == LockMode.Automatic && now >= unlockDate))
{
sb.AppendLine($"[{entryIndex}] {eventName}: {GetMsg("Unlocked")}");
}
else
{
string timeLeft = FormatTimeSpan(unlockDate - now);
string dateStr = unlockDate.ToString("MM-dd HH:mm:ss");
sb.AppendLine($"[{entryIndex}] {eventName}: {GetMsg("UnlockCountdown", timeLeft, dateStr)}");
}
entryIndex++;
}
sb.AppendLine($"======== {page} / {maxPage} ========");
return sb.ToString();
}
/// <summary>
/// 切换指定 NPC 的锁定状态
/// </summary>
/// <param name="player">玩家对象</param>
/// <param name="name">NPC 名称或别名</param>
/// <param name="currentMode">输出参数:切换后的锁定模式。如果返回值为 false,此值无意义</param>
/// <returns>是否找到并成功切换。true=已找到并更新, false=未找到匹配的 NPC</returns>
public static bool ToggleNpcLock(Player player, string name, out LockMode currentMode)
{
var entries = ProgressLockConfig.Config.NpcEntries;
currentMode = LockMode.Automatic; // 默认值
if (entries == null) return false;
foreach (var entry in entries)
{
bool matchAlias = entry.Alias.Any(a => a.Equals(name, StringComparison.OrdinalIgnoreCase));
bool matchNPC = entry.DefinitionList.Any(def =>
!def.IsUnloaded && (
Lang.GetNPCNameValue(def.Type).Equals(name, StringComparison.OrdinalIgnoreCase) ||
def.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
);
if (matchAlias || matchNPC)
{
// 切换逻辑:Automatic -> ManuallyLocked -> ManuallyUnlocked -> 循环回到 Automatic
entry.LockMode = entry.LockMode switch
{
LockMode.Automatic => LockMode.ManuallyLocked,
LockMode.ManuallyLocked => LockMode.ManuallyUnlocked,
LockMode.ManuallyUnlocked => LockMode.Automatic,
_ => LockMode.Automatic
};
currentMode = entry.LockMode;
ProgressLockConfig.Config.SaveChanges();
return true;
}
}
return false;
}
/// <summary>
/// 切换指定事件的锁定状态
/// </summary>
/// <param name="player">玩家对象</param>
/// <param name="name">事件名称、内部枚举名或别名</param>
/// <param name="currentMode">输出参数:切换后的锁定模式</param>
/// <returns>是否找到并成功切换</returns>
public static bool ToggleEventLock(Player player, string name, out LockMode currentMode)
{
var entries = ProgressLockConfig.Config.EventEntries;
currentMode = LockMode.Automatic; // 初始化
if (entries == null) return false;
foreach (var entry in entries)
{
//检查别名列表
bool matchAlias = entry.Alias != null && entry.Alias.Any(a => a.Equals(name, StringComparison.OrdinalIgnoreCase));
//检查枚举名
bool matchEnumName = entry.Name.ToString().Equals(name, StringComparison.OrdinalIgnoreCase);
//检查本地化显示的标签
string label = Language.GetTextValue($"Mods.ProgressLock.Configs.VanillaEvent.{entry.Name}.Label");
bool matchLocalization = label.Equals(name, StringComparison.OrdinalIgnoreCase);
// 只要匹配其中任意一项
if (matchAlias || matchEnumName || matchLocalization)
{
// 切换逻辑:Automatic -> ManuallyLocked -> ManuallyUnlocked -> 循环
entry.LockMode = entry.LockMode switch
{
LockMode.Automatic => LockMode.ManuallyLocked,
LockMode.ManuallyLocked => LockMode.ManuallyUnlocked,
LockMode.ManuallyUnlocked => LockMode.Automatic,
_ => LockMode.Automatic
};
currentMode = entry.LockMode;
// 保存修改后的配置
ProgressLockConfig.Config.SaveChanges();
return true;
}
}
return false;
}
//PreAI()内调用,判断 NPC 是否解锁
public static bool IsUnlocked(NPC npc, out LockStatus status)
{
var config = ProgressLockConfig.Config;
// 提前解析时间,避免高频解析字符串带来的性能损耗
if (!DateTime.TryParse(config.FirstTime, out DateTime baseTime))
{
status = LockStatus.Other;
return true;
}
DateTime now = DateTime.Now;
foreach (var entry in config.NpcEntries)
{
// 匹配 NPC 类型
if (entry.DefinitionList != null && entry.DefinitionList.Any(def => def.Type == npc.type))
{
// 1. 优先处理手动覆盖状态
if (entry.LockMode == LockMode.ManuallyLocked)
{
status = LockStatus.IsManuallyLocked;
return false;
}
if (entry.LockMode == LockMode.ManuallyUnlocked)
{
status = LockStatus.IsManuallyUnlocked;
return true;
}
// 2. 自动模式判断
DateTime unlockTime = baseTime.AddSeconds(entry.UnlockTimeSec);
if (now >= unlockTime)
{
status = LockStatus.Unlocked;
return true;
}
else
{
status = LockStatus.NotTimeYet;
return false;
}
}
}
status = LockStatus.Unlocked;
return true;
}
//PreUpdateWorld()内调用,判断 事件 是否解锁
public static bool IsUnlocked(out LockStatus status, out EventEntry matchedEntry)
{
matchedEntry = null;
var config = ProgressLockConfig.Config;
if (!DateTime.TryParse(config.FirstTime, out DateTime baseTime))
{
status = LockStatus.Other;
return true;
}
DateTime now = DateTime.Now;
foreach (var entry in config.EventEntries)
{
if (entry.Match())
{
matchedEntry = entry;
// 1. 优先处理手动覆盖状态
if (entry.LockMode == LockMode.ManuallyLocked)
{
status = LockStatus.IsManuallyLocked;
return false;
}
if (entry.LockMode == LockMode.ManuallyUnlocked)
{
status = LockStatus.IsManuallyUnlocked;
return true;
}
// 2. 自动模式判断
DateTime unlockTime = baseTime.AddSeconds(entry.UnlockTimeSec);
if (now >= unlockTime)
{
status = LockStatus.Unlocked;
return true;
}
else
{
status = LockStatus.NotTimeYet;
return false;
}
}
}
status = LockStatus.Unlocked;
return true;
}
public static void StopNPC(NPC npc)
{
npc.active = false;
NetMessage.SendData(
MessageID.SyncNPC,
-1, // 所有客户端
-1, // 不排除自己
null, // 文本参数通常为空
npc.whoAmI // 发送哪一个 NPC
);
}
public static string GetMentionMsg(string key, params object[] args)
{
return Language.GetTextValue($"Mods.ProgressLock.Mention.{key}", args);
}
}
}