-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
771a1c3
commit 049bc82
Showing
21 changed files
with
1,234 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\template.targets" /> | ||
<ItemGroup> | ||
<ProjectReference Include="..\StatusTxtMgr\StatusTxtMgr.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Terraria; | ||
using TerrariaApi.Server; | ||
|
||
namespace Demo | ||
{ | ||
[ApiVersion(2, 1)] | ||
public class StatusTxtMgrDemo : TerrariaPlugin | ||
{ | ||
#region Plugin Infos | ||
public override string Name => "Status Text Manager Demo"; | ||
public override Version Version => new Version("1.0.0"); | ||
public override string Author => "LaoSparrow (Team CNS)"; | ||
public override string Description => "Status Text Manager Demo"; | ||
#endregion | ||
|
||
#region Initialize / Dispose | ||
public StatusTxtMgrDemo(Main game) : base(game) | ||
{ | ||
Order = 1; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
/* | ||
* 参数 <updateInterval> 为 <handler> 被调用的间隔. | ||
* <handler> 会在以下几种情况被调用: | ||
* AccumulateTickCount % UpdateInterval == 0 | ||
* "AccumulateTickCount" 每秒平均60次 | ||
* | ||
* 当 <updateInterval> == 5 | ||
* | ||
* 0 1 2 3 4 5 6 7 ... | ||
* * - - - - * - - ... | ||
* ^ ^ | ||
* | ||
* <handler> 会在第 0, 5 ... Tick 被调用 | ||
* | ||
* | ||
* 例子: | ||
* | ||
* ``` StatusTxtMgr.Hooks.StatusTextUpdate.Register(OnStatusTextUpdate, --> 15 <--); ``` | ||
* <handler> 每 0.25秒 调用一次 ( 15 / 60 = 0.25 ) | ||
* | ||
* ``` StatusTxtMgr.Hooks.StatusTextUpdate.Register(OnStatusTextUpdate, --> 180 <--); ``` | ||
* <handler> 每 3秒 调用一次 ( 180 / 60 = 3 ) | ||
*/ | ||
|
||
StatusTxtMgr.StatusTxtMgr.Hooks.StatusTextUpdate.Register(OnStatusTextUpdate, 120); // update every 2s | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
StatusTxtMgr.StatusTxtMgr.Hooks.StatusTextUpdate.Deregister(OnStatusTextUpdate); | ||
} | ||
base.Dispose(disposing); | ||
} | ||
#endregion | ||
|
||
#region Hooks | ||
private ulong callCount = 0; | ||
private void OnStatusTextUpdate(StatusTxtMgr.StatusTextUpdateEventArgs args) | ||
{ | ||
// 示例 | ||
var sb = args.statusTextBuilder; | ||
sb.AppendLine("Hello World"); | ||
sb.AppendLine("Hello Player " + args.tsplayer.Name); | ||
sb.AppendLine("Call Count: " + ++callCount); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\template.targets" /> | ||
<ItemGroup> | ||
<ProjectReference Include="..\StatusTxtMgr\StatusTxtMgr.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Terraria; | ||
using TerrariaApi.Server; | ||
|
||
namespace Demo | ||
{ | ||
[ApiVersion(2, 1)] | ||
public class StatusTxtMgrDemo2 : TerrariaPlugin | ||
{ | ||
#region Plugin Infos | ||
public override string Name => "Status Text Manager Demo2"; | ||
public override Version Version => new Version("1.0.0"); | ||
public override string Author => "LaoSparrow (Team CNS)"; | ||
public override string Description => "Status Text Manager Demo2"; | ||
#endregion | ||
|
||
#region Initialize / Dispose | ||
public StatusTxtMgrDemo2(Main game) : base(game) | ||
{ | ||
Order = 1; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
StatusTxtMgr.StatusTxtMgr.Hooks.StatusTextUpdate.Register(OnStatusTextUpdate, 30); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
StatusTxtMgr.StatusTxtMgr.Hooks.StatusTextUpdate.Deregister(OnStatusTextUpdate); | ||
} | ||
base.Dispose(disposing); | ||
} | ||
#endregion | ||
|
||
#region Hooks | ||
private ulong callCount = 0; | ||
private void OnStatusTextUpdate(StatusTxtMgr.StatusTextUpdateEventArgs args) | ||
{ | ||
var sb = args.statusTextBuilder; | ||
sb.AppendLine("Hello World2"); | ||
sb.AppendLine("Hello Player " + args.tsplayer.Name); | ||
sb.AppendLine("Call Count: " + ++callCount); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# StatusTxtMgr Demo | ||
|
||
示例在 `Demo` 项目中 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.2.32616.157 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatusTxtMgr", "StatusTxtMgr\StatusTxtMgr.csproj", "{926F4C18-1AE0-4907-B813-AF607BE56292}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{5F1E128A-7B8E-4D24-8EF3-C2CE323CF37E}" | ||
ProjectSection(ProjectDependencies) = postProject | ||
{926F4C18-1AE0-4907-B813-AF607BE56292} = {926F4C18-1AE0-4907-B813-AF607BE56292} | ||
EndProjectSection | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo2", "Demo2\Demo2.csproj", "{32EECD79-5D8F-4D55-8C10-99B96A44BF99}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{926F4C18-1AE0-4907-B813-AF607BE56292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{926F4C18-1AE0-4907-B813-AF607BE56292}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{926F4C18-1AE0-4907-B813-AF607BE56292}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{926F4C18-1AE0-4907-B813-AF607BE56292}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{5F1E128A-7B8E-4D24-8EF3-C2CE323CF37E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5F1E128A-7B8E-4D24-8EF3-C2CE323CF37E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5F1E128A-7B8E-4D24-8EF3-C2CE323CF37E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5F1E128A-7B8E-4D24-8EF3-C2CE323CF37E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{32EECD79-5D8F-4D55-8C10-99B96A44BF99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{32EECD79-5D8F-4D55-8C10-99B96A44BF99}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{32EECD79-5D8F-4D55-8C10-99B96A44BF99}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{32EECD79-5D8F-4D55-8C10-99B96A44BF99}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {CF683196-9739-4880-A931-8467BE490B23} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using StatusTxtMgr.SettingsModel; | ||
|
||
namespace StatusTxtMgr | ||
{ | ||
public class STMSettings | ||
{ | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
public Utils.LogLevel LogLevel = Utils.LogLevel.INFO; | ||
public List<IStatusTextSetting> StatusTextSettings = new(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace StatusTxtMgr.SettingsModel | ||
{ | ||
public class HandlerInfoOverride : IStatusTextSetting | ||
{ | ||
[JsonProperty] | ||
public static string TypeName => "handler_info_override"; | ||
public string PluginName { get; set; } | ||
public bool Enabled { get; set; } | ||
public ulong UpdateInterval { get; set; } | ||
|
||
public void ProcessHandlers(List<StatusTextUpdateHandlerItem> handlers, List<IStatusTextUpdateHandler> processedHandlers, int settingsIdx) | ||
{ | ||
var handlerMatched = handlers.Find(h => h.AssemblyName == PluginName); | ||
if (handlerMatched == null) | ||
return; | ||
|
||
handlers.Remove(handlerMatched); | ||
if (Enabled) | ||
{ | ||
if (UpdateInterval > 0) | ||
handlerMatched.UpdateInterval = UpdateInterval; | ||
processedHandlers.Add(handlerMatched); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Newtonsoft.Json; | ||
using StatusTxtMgr.Utils.Attrs; | ||
using StatusTxtMgr.Utils.JsonConverters; | ||
|
||
namespace StatusTxtMgr.SettingsModel | ||
{ | ||
[JsonConverter(typeof(InterfaceConcreteConverter))] | ||
[Implements(typeof(StaticText), typeof(HandlerInfoOverride))] | ||
public interface IStatusTextSetting | ||
{ | ||
// 实际上还需要加一个 | ||
// public static string TypeName => "handler_info_override"; | ||
// 供 Converter 使用 | ||
|
||
/// <summary> | ||
/// 令 Handler 处理 Handler List | ||
/// </summary> | ||
/// <param name="handlers">由插件而来未经处理的 Handlers</param> | ||
/// <param name="processedHandlers">最终交由 HandlerList 依序调用的 Handlers</param> | ||
/// <param name="settingsIdx">实例在 Setting 中的 index</param> | ||
void ProcessHandlers(List<StatusTextUpdateHandlerItem> handlers, List<IStatusTextUpdateHandler> processedHandlers, int settingsIdx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Newtonsoft.Json; | ||
using TShockAPI; | ||
|
||
namespace StatusTxtMgr.SettingsModel | ||
{ | ||
public class StaticText : IStatusTextSetting, IStatusTextUpdateHandler | ||
{ | ||
[JsonProperty] | ||
public static string TypeName => "static_text"; | ||
public string Text { get; set; } | ||
|
||
public void ProcessHandlers(List<StatusTextUpdateHandlerItem> handlers, List<IStatusTextUpdateHandler> processedHandlers, int settingsIdx) => | ||
processedHandlers.Add(this); | ||
|
||
|
||
public bool Invoke(TSPlayer tsplr, bool forceUpdate = false) => forceUpdate; | ||
|
||
public string GetPlrST(TSPlayer tsplr) => Text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using StatusTxtMgr.Utils; | ||
using System.Reflection; | ||
using System.Text; | ||
using Terraria; | ||
using TShockAPI; | ||
|
||
namespace StatusTxtMgr | ||
{ | ||
public interface IStatusTextUpdateHandler | ||
{ | ||
bool Invoke(TSPlayer tsplr, bool forceUpdate = false); | ||
string GetPlrST(TSPlayer tsplr); | ||
} | ||
|
||
|
||
public delegate void StatusTextUpdateDelegate(StatusTextUpdateEventArgs args); | ||
|
||
public class StatusTextUpdateEventArgs | ||
{ | ||
public TSPlayer tsplayer { get; set; } | ||
public StringBuilder statusTextBuilder { get; set; } | ||
} | ||
|
||
public class StatusTextUpdateHandlerItem : IStatusTextUpdateHandler | ||
{ | ||
public StatusTextUpdateDelegate UpdateDelegate; | ||
public ulong UpdateInterval = 60; | ||
public string AssemblyName; | ||
|
||
private StringBuilder?[] plrSBs = new StringBuilder[Main.maxPlayers]; | ||
|
||
public StatusTextUpdateHandlerItem(StatusTextUpdateDelegate updateDelegate, ulong updateInterval = 60) | ||
{ | ||
UpdateDelegate = updateDelegate ?? throw new ArgumentNullException(nameof(updateDelegate)); | ||
UpdateInterval = updateInterval > 0 ? updateInterval : throw new ArgumentException("cannot be 0", nameof(updateInterval)); | ||
AssemblyName = updateDelegate.Method.DeclaringType?.Assembly.GetName().Name ?? ""; | ||
} | ||
|
||
public bool Invoke(TSPlayer tsplr, bool forceUpdate = false) | ||
{ | ||
try | ||
{ | ||
// 检查对应玩家是否需要更新 Status Text | ||
if (forceUpdate || (Utils.Common.TickCount + (ulong)tsplr.Index) % UpdateInterval == 0) | ||
{ | ||
var updateDelegate = UpdateDelegate; | ||
var args = new StatusTextUpdateEventArgs() { tsplayer = tsplr, statusTextBuilder = plrSBs.AcquirePlrSB(tsplr) }; | ||
updateDelegate(args); | ||
return true; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.Warn($"Exception occur while invoking delegate of '{AssemblyName}' in StatusTextUpdateHandler.Invoke, Ex: {ex}"); | ||
} | ||
return false; | ||
} | ||
|
||
// 获取当前 Handler 对对应玩家的 Status Text | ||
public string GetPlrST(TSPlayer tsplr) => plrSBs[tsplr.Index]?.ToString() ?? ""; | ||
} | ||
} |
Oops, something went wrong.