Skip to content

Commit 5741ee5

Browse files
committed
[+] ServerNotice, ServerAnnouncement -> Enhancement
1 parent 7d2a67f commit 5741ee5

File tree

4 files changed

+111
-47
lines changed

4 files changed

+111
-47
lines changed

AquaMai.Mods/UX/ServerAnnouncement.cs renamed to AquaMai.Mods/Enhancement/ServerAnnouncement.cs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using AquaMai.Config.Attributes;
44
using AquaMai.Core.Helpers;
5+
using AquaMai.Mods.Types;
56
using DB;
67
using HarmonyLib;
78
using JetBrains.Annotations;
@@ -11,10 +12,11 @@
1112
using UnityEngine;
1213
using UnityEngine.Networking;
1314

14-
namespace AquaMai.Mods.UX;
15+
namespace AquaMai.Mods.Enhancement;
1516

1617
[ConfigSection(
1718
defaultOn: true,
19+
exampleHidden: true,
1820
en: """
1921
Show announcement from compatible server
2022
(no side effects for other servers, no extra requests made)
@@ -25,19 +27,15 @@ Show announcement from compatible server
2527
""")]
2628
public static class ServerAnnouncement
2729
{
28-
private class ServerAnnouncementEntry
30+
private class ServerAnnouncementEntry : ConditionalMessage
2931
{
3032
[CanBeNull] public string title = null;
3133
[CanBeNull] public string announcement = null;
3234
[CanBeNull] public string imageUrl = null;
3335
public float imageSizeFactor = 9;
3436
public bool showOnIdle = false;
3537
public bool showOnUserLogin = false;
36-
public string[] locales = [];
37-
[CanBeNull] public string minimumAquaMaiVersion = null;
38-
[CanBeNull] public string maximumAquaMaiVersion = null;
39-
public int minimumGameVersion = 0;
40-
public int maximumGameVersion = 0;
38+
public bool persistent = false;
4139
}
4240

4341
private class ServerAnnouncementData
@@ -64,7 +62,7 @@ private static Variant OnNetPacketComplete(string api, Variant _, Variant respon
6462
ServerAnnouncementEntry chosenAnnouncement = null;
6563
foreach (var entry in serverAnnouncementData.entries)
6664
{
67-
if (ShouldShowAnnouncement(entry))
65+
if (entry.ShouldShow())
6866
{
6967
chosenAnnouncement = entry;
7068
break;
@@ -92,43 +90,6 @@ private static Variant OnNetPacketComplete(string api, Variant _, Variant respon
9290
return null;
9391
}
9492

95-
private static bool ShouldShowAnnouncement(ServerAnnouncementEntry announcement)
96-
{
97-
if (announcement.locales != null && announcement.locales.Length != 0 && !announcement.locales.Contains(General.locale))
98-
{
99-
MelonLogger.Msg($"[ServerAnnouncement] Now showing announcement: Language {General.locale} not in {JSON.Dump(announcement.locales)}");
100-
return false;
101-
}
102-
103-
var aquaMaiVersion = new System.Version(Core.BuildInfo.Version);
104-
if (announcement.minimumAquaMaiVersion != null && aquaMaiVersion < new System.Version(announcement.minimumAquaMaiVersion))
105-
{
106-
MelonLogger.Msg($"[ServerAnnouncement] Now showing announcement: AquaMai version {aquaMaiVersion} < {announcement.minimumAquaMaiVersion}");
107-
return false;
108-
}
109-
110-
if (announcement.maximumAquaMaiVersion != null && aquaMaiVersion > new System.Version(announcement.maximumAquaMaiVersion))
111-
{
112-
MelonLogger.Msg($"[ServerAnnouncement] Now showing announcement: AquaMai version {aquaMaiVersion} > {announcement.maximumAquaMaiVersion}");
113-
return false;
114-
}
115-
116-
var gameVersion = GameInfo.GameVersion;
117-
if (announcement.minimumGameVersion != 0 && gameVersion < announcement.minimumGameVersion)
118-
{
119-
MelonLogger.Msg($"[ServerAnnouncement] Now showing announcement: Game version {gameVersion} < {announcement.minimumGameVersion}");
120-
return false;
121-
}
122-
123-
if (announcement.maximumGameVersion != 0 && gameVersion > announcement.maximumGameVersion)
124-
{
125-
MelonLogger.Msg($"[ServerAnnouncement] Now showing announcement: Game version {gameVersion} > {announcement.maximumGameVersion}");
126-
return false;
127-
}
128-
129-
return true;
130-
}
131-
13293
private class DownloadTexture : MonoBehaviour
13394
{
13495
private void Start()
@@ -163,7 +124,7 @@ private IEnumerator GetTexture()
163124
private static void AdvertiseProcessOnStart()
164125
{
165126
if (_announcement == null || !_announcement.showOnIdle) return;
166-
MessageHelper.ShowMessage(_announcement.announcement, title: _announcement.title, sprite: _sprite, size: WindowSizeID.LargeVerticalPostImage);
127+
MessageHelper.ShowMessage(_announcement.announcement, title: _announcement.title, sprite: _sprite, size: WindowSizeID.LargeVerticalPostImage, retain: _announcement.persistent);
167128
}
168129

169130
[HarmonyPatch(typeof(EntryProcess), "OnStart")]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Linq;
2+
using AquaMai.Config.Attributes;
3+
using AquaMai.Core.Helpers;
4+
using AquaMai.Mods.Types;
5+
using JetBrains.Annotations;
6+
using MelonLoader;
7+
using MelonLoader.TinyJSON;
8+
9+
namespace AquaMai.Mods.Enhancement;
10+
11+
[ConfigSection(
12+
defaultOn: true,
13+
exampleHidden: true,
14+
en: """
15+
Show extra information from compatible server
16+
(no side effects for other servers, no extra requests made)
17+
""",
18+
zh: """
19+
在用户登录之类的时候,显示来自兼容服务器的额外信息
20+
(对其他服务器无副作用,不会发出额外请求)
21+
""")]
22+
public class ServerNotice
23+
{
24+
private const string FieldName = "_aquaMaiServerNotice";
25+
26+
private class ServerNoticeEntry : ConditionalMessage
27+
{
28+
[CanBeNull] public string title = null;
29+
[CanBeNull] public string content = null;
30+
}
31+
32+
private class ServerNoticeData
33+
{
34+
public ServerNoticeEntry[] entries = [];
35+
}
36+
37+
public static void OnBeforePatch()
38+
{
39+
NetPacketHook.OnNetPacketComplete += OnNetPacketComplete;
40+
}
41+
42+
private static Variant OnNetPacketComplete(string api, Variant request, Variant response)
43+
{
44+
if (response is not ProxyObject obj) return null;
45+
var serverNoticeJson = obj.Keys.Contains(FieldName) ? obj[FieldName] : null;
46+
if (serverNoticeJson == null) return null;
47+
48+
var data = serverNoticeJson.Make<ServerNoticeData>();
49+
var entry = data?.entries.FirstOrDefault(it => it.ShouldShow());
50+
if (entry == null) return null;
51+
52+
MelonLogger.Msg($"[ServerNotice] {entry.title}: {entry.content}");
53+
MessageHelper.ShowMessage(entry.content, title: entry.title);
54+
return null;
55+
}
56+
}

AquaMai.Mods/General.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ public enum SectionNameOrder
3838
Tweaks_TimeSaving,
3939
UX,
4040
Utils,
41-
Fancy
41+
Fancy,
42+
Enhancement,
4243
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Linq;
2+
using AquaMai.Core.Helpers;
3+
using JetBrains.Annotations;
4+
5+
namespace AquaMai.Mods.Types;
6+
7+
public abstract class ConditionalMessage
8+
{
9+
public string[] locales = [];
10+
[CanBeNull] public string minimumAquaMaiVersion = null;
11+
[CanBeNull] public string maximumAquaMaiVersion = null;
12+
public int minimumGameVersion = 0;
13+
public int maximumGameVersion = 0;
14+
15+
public bool ShouldShow()
16+
{
17+
if (locales != null && locales.Length != 0 && !locales.Contains(General.locale))
18+
{
19+
return false;
20+
}
21+
22+
var aquaMaiVersion = new System.Version(Core.BuildInfo.Version);
23+
if (minimumAquaMaiVersion != null && aquaMaiVersion < new System.Version(minimumAquaMaiVersion))
24+
{
25+
return false;
26+
}
27+
28+
if (maximumAquaMaiVersion != null && aquaMaiVersion > new System.Version(maximumAquaMaiVersion))
29+
{
30+
return false;
31+
}
32+
33+
var gameVersion = GameInfo.GameVersion;
34+
if (minimumGameVersion != 0 && gameVersion < minimumGameVersion)
35+
{
36+
return false;
37+
}
38+
39+
if (maximumGameVersion != 0 && gameVersion > maximumGameVersion)
40+
{
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)