Skip to content

Commit

Permalink
("Optionally") remove all Monocle and Celeste references from Server …
Browse files Browse the repository at this point in the history
…& Shared (#154)

* check the description of the PR ( #154) if anything is unclear about this squash'd merge.
  • Loading branch information
RedFlames authored Sep 6, 2024
1 parent b68bf9e commit 405a7e5
Show file tree
Hide file tree
Showing 25 changed files with 545 additions and 96 deletions.
9 changes: 8 additions & 1 deletion CelesteNet.Client/CelesteNetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ public CelesteNetClient(CelesteNetClientSettings settings, CelesteNetClientOptio
Logger.Log(LogLevel.DEV, "lifecycle", $"CelesteNetClient created");

Data = new();

Logger.Log(LogLevel.INF, "data", "Rescanning all data types");
Data.IDToDataType.Clear();
Data.DataTypeToID.Clear();

Data.RescanDataTypes(CelesteNetClientModule.GetTypes());

Data.RegisterHandlersIn(this);

_ReadyEvent = new(false);

// Find connection features
List<IConnectionFeature> conFeatures = new();
foreach (Type type in CelesteNetUtils.GetTypes()) {
foreach (Type type in CelesteNetClientModule.GetTypes()) {
try {
if (!typeof(IConnectionFeature).IsAssignableFrom(type) || type.IsAbstract)
continue;
Expand Down
32 changes: 32 additions & 0 deletions CelesteNet.Client/CelesteNetClientModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,38 @@ public void Stop() {
}
}

public static Type[] GetTypes() {
if (Everest.Modules.Count != 0)
return _GetTypes();

Type[] typesPrev = _GetTypes();
Retry:
Type[] types = _GetTypes();
if (typesPrev.Length != types.Length) {
typesPrev = types;
goto Retry;
}
return types;
}

private static IEnumerable<Assembly> _GetAssemblies()
=> (Everest.Modules?.Select(m => m.GetType().Assembly) ?? new Assembly[0])
.Concat(AppDomain.CurrentDomain.GetAssemblies())
.Distinct();

private static Type[] _GetTypes()
=> _GetAssemblies().SelectMany(_GetTypes).ToArray();

private static IEnumerable<Type> _GetTypes(Assembly asm) {
try {
return asm.GetTypes();
} catch (ReflectionTypeLoadException e) {
#pragma warning disable CS8619 // Compiler thinks this could be <Type?> even though we check for t != null
return e.Types.Where(t => t != null);
#pragma warning restore CS8619
}
}

public void ResetReconnectPenalty() {
Logger.Log(LogLevel.INF, "reconnect-attempt", $"CelesteNetClientModule Start: Resetting reconnect delay from {ReconnectWaitTime} seconds to 0... (started {ReconnectDelayingSince})");
ReconnectWaitTime = 0;
Expand Down
20 changes: 18 additions & 2 deletions CelesteNet.Client/Components/CelesteNetMainComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,15 @@ public void Handle(CelesteNetConnection con, DataDashTrail trail) {
if (trail.Server) {
TrailManager.Add(
trail.Position,
trail.Sprite?.ToImage(),
trail.Sprite == null ? null :
new(GFX.Game[trail.Sprite.AtlasPath]) {
Position = trail.Sprite.Position,
Origin = trail.Sprite.Origin,
Scale = trail.Sprite.Scale,
Rotation = trail.Sprite.Rotation,
Color = trail.Sprite.Color,
Effects = trail.Sprite.Effects
},
ghost?.Hair,
trail.Scale,
trail.Color,
Expand Down Expand Up @@ -1126,7 +1134,15 @@ public void SendDashTrail(Vector2 position, Image sprite, PlayerHair hair, Vecto
Client?.Send(new DataDashTrail {
Player = Client.PlayerInfo,
Position = position,
Sprite = new(sprite),
Sprite = new DataPartImage {
AtlasPath = sprite.Texture.AtlasPath,
Position = sprite.Position,
Origin = sprite.Origin,
Scale = sprite.Scale,
Rotation = sprite.Rotation,
Color = sprite.Color,
Effects = sprite.Effects
},
Scale = scale,
Color = color,
Depth = depth,
Expand Down
19 changes: 9 additions & 10 deletions CelesteNet.Server.ChatModule/ChatSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Celeste.Mod.CelesteNet.Server.Chat {
public class ChatSettings : CelesteNetServerModuleSettings {
Expand All @@ -26,13 +25,13 @@ public class ChatSettings : CelesteNetServerModuleSettings {
public bool FilterOnlyGlobalAndMainChat { get; set; } = true;
public bool FilterPrivateChannelNames { get; set; } = false;

public Color ColorBroadcast { get; set; } = Calc.HexToColor("#00adee");
public Color ColorServer { get; set; } = Calc.HexToColor("#9e24f5");
public Color ColorError { get; set; } = Calc.HexToColor("#c71585");
public Color ColorCommand { get; set; } = Calc.HexToColor("#2e31f1");
public Color ColorCommandReply { get; set; } = Calc.HexToColor("#e39dcc");
public Color ColorWhisper { get; set; } = Calc.HexToColor("#888888");
public Color ColorLogEmote { get; set; } = Calc.HexToColor("#bbbb88");
public Color ColorBroadcast { get; set; } = ColorHelpers.HexToColor("#00adee");
public Color ColorServer { get; set; } = ColorHelpers.HexToColor("#9e24f5");
public Color ColorError { get; set; } = ColorHelpers.HexToColor("#c71585");
public Color ColorCommand { get; set; } = ColorHelpers.HexToColor("#2e31f1");
public Color ColorCommandReply { get; set; } = ColorHelpers.HexToColor("#e39dcc");
public Color ColorWhisper { get; set; } = ColorHelpers.HexToColor("#888888");
public Color ColorLogEmote { get; set; } = ColorHelpers.HexToColor("#bbbb88");

public string MessageGreeting { get; set; } = "Welcome {player}, to <insert server name here>!";
public string MessageMOTD { get; set; } =
Expand Down
10 changes: 4 additions & 6 deletions CelesteNet.Server.ChatModule/SpamContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Celeste.Mod.CelesteNet.DataTypes;
using Monocle;
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Celeste.Mod.CelesteNet.DataTypes;

namespace Celeste.Mod.CelesteNet.Server.Chat
{
namespace Celeste.Mod.CelesteNet.Server.Chat {
public class SpamContext : IDisposable {

public readonly ChatModule Chat;
Expand Down Expand Up @@ -47,7 +45,7 @@ public class SpamTimeout {
public DateTime End =>
Start + TimeSpan.FromSeconds(
Spam.Chat.Settings.SpamTimeout +
Spam.Chat.Settings.SpamTimeoutAdd * Calc.Clamp(Count - Spam.Chat.Settings.SpamCount, 0, Spam.Chat.Settings.SpamCountMax)
Spam.Chat.Settings.SpamTimeoutAdd * CalcHelpers.Clamp(Count - Spam.Chat.Settings.SpamCount, 0, Spam.Chat.Settings.SpamCountMax)
);

public TimeSpan Timeout => End - DateTime.UtcNow;
Expand Down
6 changes: 2 additions & 4 deletions CelesteNet.Server.FrontendModule/WSCMDs/WSCMDChatEdit.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Celeste.Mod.CelesteNet.DataTypes;
using Celeste.Mod.CelesteNet.Server.Chat;
using Monocle;

namespace Celeste.Mod.CelesteNet.Server.Control
{
namespace Celeste.Mod.CelesteNet.Server.Control {
public class WSCMDChatEdit : WSCMD {
public override bool MustAuth => true;
public override object? Run(dynamic? input) {
Expand All @@ -15,7 +13,7 @@ public class WSCMDChatEdit : WSCMD {
return null;

if (input.Color != null)
msg.Color = Calc.HexToColor((string) input.Color);
msg.Color = ColorHelpers.HexToColor((string) input.Color);

if (input.Text != null)
msg.Text = (string) input.Text;
Expand Down
12 changes: 5 additions & 7 deletions CelesteNet.Server.FrontendModule/WSCMDs/WSCMDChatX.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Celeste.Mod.CelesteNet.DataTypes;
using System;
using System.Linq;
using Celeste.Mod.CelesteNet.DataTypes;
using Celeste.Mod.CelesteNet.Server.Chat;
using Monocle;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace Celeste.Mod.CelesteNet.Server.Control
{
namespace Celeste.Mod.CelesteNet.Server.Control {
public class WSCMDChatX : WSCMD {
public override bool MustAuth => true;
public override object? Run(dynamic? input) {
Expand All @@ -27,7 +25,7 @@ public class WSCMDChatX : WSCMD {
};

if (!string.IsNullOrEmpty(color))
msg.Color = Calc.HexToColor(color!);
msg.Color = ColorHelpers.HexToColor(color!);

if (targets != null && targets.Count > 0)
{
Expand Down
1 change: 1 addition & 0 deletions CelesteNet.Server/CelesteNet.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<AssemblyName>CelesteNet.Server</AssemblyName>
<RootNamespace>Celeste.Mod.CelesteNet.Server</RootNamespace>
<OutputType>Exe</OutputType>
Expand Down
1 change: 0 additions & 1 deletion CelesteNet.Server/CelesteNetPlayerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Threading;
using Celeste.Mod.CelesteNet.DataTypes;
using Monocle;

namespace Celeste.Mod.CelesteNet.Server {
public class CelesteNetPlayerSession : IDisposable {
Expand Down
7 changes: 7 additions & 0 deletions CelesteNet.Server/CelesteNetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public CelesteNetServer(CelesteNetServerSettings settings) {
RegisterModule(file);

Data = new();

Logger.Log(LogLevel.INF, "data", "Rescanning all data types");
Data.IDToDataType.Clear();
Data.DataTypeToID.Clear();

Data.RescanDataTypes(CelesteNetUtils.GetTypes());

Data.RegisterHandlersIn(this);

PacketDumper = new(this);
Expand Down
125 changes: 125 additions & 0 deletions CelesteNet.Shared/CelesteCompat/CelesteAudio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Celeste.Mod.CelesteNet {

[Serializable]
public class CelesteAudioState {

// NOTE: If there are things missing from this that are needed Server-side (or in Shared),
// check Celeste.AudioState and Everest/Celeste.Mod.mm/Patches/AudioState.cs to add it here

public static string[] LayerParameters = new string[10] { "layer0", "layer1", "layer2", "layer3", "layer4", "layer5", "layer6", "layer7", "layer8", "layer9" };

public CelesteAudioTrackState Music = new CelesteAudioTrackState();

public CelesteAudioTrackState Ambience = new CelesteAudioTrackState();

public float? AmbienceVolume;

public CelesteAudioState() {
}

public CelesteAudioState(CelesteAudioTrackState music, CelesteAudioTrackState ambience) {
if (music != null) {
Music = music.Clone();
}

if (ambience != null) {
Ambience = ambience.Clone();
}
}
}

[Serializable]
public class CelesteAudioTrackState {

// NOTE: If there are things missing from this that are needed Server-side (or in Shared),
// check Celeste.AudioTrackState and potentially an Everest patch (currently there is none)

[XmlIgnore]
private string ev;

public List<CelesteMEP> Parameters = new List<CelesteMEP>();

[XmlAttribute]
public string Event {
get {
return ev;
}
set {
if (ev != value) {
ev = value;
Parameters.Clear();
}
}
}

[XmlIgnore]
public int Progress {
get {
return (int)GetParam("progress");
}
set {
Param("progress", value);
}
}

public CelesteAudioTrackState() {
}

public CelesteAudioTrackState(string ev) {
Event = ev;
}

public CelesteAudioTrackState Param(string key, float value) {
foreach (CelesteMEP parameter in Parameters) {
if (parameter.Key != null && parameter.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)) {
parameter.Value = value;
return this;
}
}

Parameters.Add(new CelesteMEP(key, value));
return this;
}

public float GetParam(string key) {
foreach (CelesteMEP parameter in Parameters) {
if (parameter.Key != null && parameter.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)) {
return parameter.Value;
}
}

return 0f;
}

public CelesteAudioTrackState Clone() {
CelesteAudioTrackState audioTrackState = new CelesteAudioTrackState();
audioTrackState.Event = Event;
foreach (CelesteMEP parameter in Parameters) {
audioTrackState.Parameters.Add(new CelesteMEP(parameter.Key, parameter.Value));
}

return audioTrackState;
}
}

[Serializable]
public class CelesteMEP {
[XmlAttribute]
public string Key;

[XmlAttribute]
public float Value;

public CelesteMEP() {
}

public CelesteMEP(string key, float value) {
Key = key;
Value = value;
}
}
}
40 changes: 40 additions & 0 deletions CelesteNet.Shared/CelesteCompat/CelesteEntityID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Xml.Serialization;

namespace Celeste.Mod.CelesteNet {
[Serializable]
public struct CelesteEntityID {
public static readonly CelesteEntityID None = new CelesteEntityID("null", -1);

[XmlIgnore]
public string Level;

[XmlIgnore]
public int ID;

[XmlAttribute]
public string Key {
get {
return Level + ":" + ID;
}
set {
string[] array = value.Split(':');
Level = array[0];
ID = int.Parse(array[1]);
}
}

public CelesteEntityID(string level, int entityID) {
Level = level;
ID = entityID;
}

public override string ToString() {
return Key;
}

public override int GetHashCode() {
return Level.GetHashCode() ^ ID;
}
}
}
Loading

0 comments on commit 405a7e5

Please sign in to comment.