-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatEntryPoint.cs
192 lines (162 loc) · 7.74 KB
/
ChatEntryPoint.cs
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
using System.Text; //location of encoding/decoding.
using Sandbox.ModAPI; //location of MyAPIGateway.
using VRage.Game.Components; //location of MySessionComponentBase.
using System.Threading.Tasks;
using System;
using SETextToSpeechMod.LookUpTables;
using SETextToSpeechMod.Processing;
namespace SETextToSpeechMod
{
[MySessionComponentDescriptor (MyUpdateOrder.BeforeSimulation)] //adds an attribute tag telling the game to run my script.
public class ChatEntryPoint : MySessionComponentBase
{
const ushort packet_ID = 60452; //the convention is to use the last 4-5 digits of your steam mod as packet ID
bool initialised;
private readonly bool debugging;
private Encoding encode = Encoding.Unicode; //encoding is necessary to convert message into correct format.
private SoundPlayer soundPlayer;
public OutputManager OutputManager { get; private set; }
private AttendanceManager attendanceManager = AttendanceManager.GetSingleton();
private Commands commands = Commands.GetSingleton();
public ChatEntryPoint(){}
public ChatEntryPoint (bool isDebugging)
{
debugging = isDebugging;
}
public override void UpdateBeforeSimulation()
{
if (initialised == false)
{
Initialise();
}
OutputManager.Run();
}
public void Initialise() //this wouldnt work as a constructor because some assets arent available during load time.
{
initialised = true;
soundPlayer = new SoundPlayer (debugging, true);
OutputManager = new OutputManager (soundPlayer, debugging);
if (debugging == false)
{
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; //subscribes my method to the MessageEntered event.
MyAPIGateway.Multiplayer.RegisterMessageHandler (packet_ID, OnReceivedPacket); //registers a multiplayer packet receiver.
AttendanceManager.UpdatePlayers();
MyAPIGateway.Utilities.ShowMessage ("TextToSpeechMod:", "If you find a broken word, please tell the designer.");
}
}
public void OnMessageEntered (string messageText, ref bool sendToOthers) //event handler method will run when this client posts a chat message.
{
string noEscapes = string.Format (@"{0}", messageText);
string fixedCase = noEscapes.ToUpper(); //capitalize all letters of the input sentence so that comparison is made easier.
ExecuteCommandIfValid (fixedCase);
string signatureBuild = OutputManager.LocalPlayersVoice.ToString();
int leftoverSpace = PossibleOutputs.AutoSignatureSize - OutputManager.LocalPlayersVoice.ToString().Length;
for (int i = 0; i < leftoverSpace; i++)
{
signatureBuild += " ";
}
fixedCase = signatureBuild + fixedCase;
byte[] ConvertedToPacket = encode.GetBytes (fixedCase);
if (MyAPIGateway.Multiplayer.MultiplayerActive)
{
for (int i = 0; i < attendanceManager.Players.Count; i++)
{
if (attendanceManager.PlayersMuteStatuses[attendanceManager.Players[i].DisplayName] == false)
{
MyAPIGateway.Multiplayer.SendMessageTo(packet_ID, ConvertedToPacket, attendanceManager.Players[i].SteamUserId, true); //everyone will get this trigger including you.
}
}
}
else
{
OnReceivedPacket (ConvertedToPacket);
}
}
private void ExecuteCommandIfValid (string upperCaseSentence)
{
for (int i = 0; i < commands.VoiceCollection.Length; i++)
{
if (upperCaseSentence.Contains (commands.VoiceCollection[i]))
{
OutputManager.LocalPlayersVoice = PossibleOutputs.Collection[i];
return;
}
}
string[] choppedCommand = upperCaseSentence.Split (' ');
if (choppedCommand.Length >= commands.MUTING_MIN_SIZE)
{
int startOfNameIndex = commands.MUTING_MIN_SIZE - 1;
string interpretedInput = choppedCommand[startOfNameIndex];
for (int i = startOfNameIndex; i < choppedCommand.Length; i++)
{
interpretedInput += " " + choppedCommand[i];
}
if (upperCaseSentence.Contains (commands.MUTE_PLAYER))
{
attendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, true);
}
else if (upperCaseSentence.Contains (commands.UNMUTE_PLAYER))
{
attendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, false);
}
else if (upperCaseSentence.Contains (commands.CHANGE_VOLUME))
{
float attemptedConversion;
if (float.TryParse (interpretedInput, out attemptedConversion))
{
soundPlayer.UpdateVolume (attemptedConversion);
}
else
{
MyAPIGateway.Utilities.ShowMessage ("", "Could not determine a valid volume from: " + interpretedInput);
}
}
}
}
public void OnReceivedPacket (byte[] receivedPacket) //action type method which handles the received packets from other players.
{
string decoded = encode.GetString (receivedPacket);
string signature = ExtractSignatureFromPacket (ref decoded);
Sentence inputSentence = new Sentence(decoded);
if (decoded.Length > OutputManager.MAX_LETTERS && //letter limit for mental health concerns.
debugging == false)
{
MyAPIGateway.Utilities.ShowMessage (OutputManager.MAX_LETTERS.ToString(), " LETTER LIMIT REACHED");
}
else
{
OutputManager.CreateNewSpeech (signature, inputSentence);
}
}
/// <summary>
/// Gets the serialized header within a transmission packet.
/// Dont blame me if your string is destroyed when it doesn't contain a signature!
/// </summary>
/// <param name="packet"></param>
/// <returns></returns>
private string ExtractSignatureFromPacket (ref string packet)
{
char[] dividedMessage = packet.ToCharArray();
char[] signatureChars = new char[PossibleOutputs.AutoSignatureSize];
for (int i = 0; i < signatureChars.Length; i++)
{
signatureChars[i] = dividedMessage[i];
}
string voiceSignature = new string (signatureChars);
packet = packet.Remove (0, PossibleOutputs.AutoSignatureSize);
return voiceSignature;
}
protected override void UnloadData() //will run when the session closes to prevent my assets from doubling up.
{
initialised = false;
MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered;
MyAPIGateway.Multiplayer.UnregisterMessageHandler (packet_ID, OnReceivedPacket);
OutputManager.DisposeOfUnsafe();
}
public void Dispose()
{
initialised = false;
OutputManager.DisposeOfUnsafe();
}
}
}