|
5 | 5 | using UnityEngine;
|
6 | 6 | using MLAPI.Data.Transports;
|
7 | 7 | using MLAPI.NetworkingManagerComponents.Core;
|
| 8 | +using MLAPI.MonoBehaviours.Core; |
8 | 9 |
|
9 | 10 | namespace MLAPI.Data
|
10 | 11 | {
|
@@ -137,6 +138,143 @@ public class NetworkConfig
|
137 | 138 | /// If your logic uses the NetwokrTime, this should probably be turned off. If however it's needed to maximize accuracy, this is recommended to be turned on
|
138 | 139 | /// </summary>
|
139 | 140 | public bool EnableTimeResync = false;
|
| 141 | + [TextArea] |
| 142 | + public string ConfigData = string.Empty; |
| 143 | + |
| 144 | + internal string ToBase64() |
| 145 | + { |
| 146 | + using (BitWriter writer = BitWriter.Get()) |
| 147 | + { |
| 148 | + writer.WriteUShort(ProtocolVersion); |
| 149 | + writer.WriteBits((byte)Transport, 5); |
| 150 | + |
| 151 | + writer.WriteUShort((ushort)Channels.Count); |
| 152 | + for (int i = 0; i < Channels.Count; i++) |
| 153 | + { |
| 154 | + writer.WriteString(Channels[i].Name); |
| 155 | + writer.WriteBool(Channels[i].Encrypted); |
| 156 | + writer.WriteBits((byte)Channels[i].Type, 5); |
| 157 | + } |
| 158 | + |
| 159 | + writer.WriteUShort((ushort)MessageTypes.Count); |
| 160 | + for (int i = 0; i < MessageTypes.Count; i++) |
| 161 | + { |
| 162 | + writer.WriteString(MessageTypes[i].Name); |
| 163 | + writer.WriteBool(MessageTypes[i].Passthrough); |
| 164 | + } |
| 165 | + |
| 166 | + writer.WriteUShort((ushort)RegisteredScenes.Count); |
| 167 | + for (int i = 0; i < RegisteredScenes.Count; i++) |
| 168 | + { |
| 169 | + writer.WriteString(RegisteredScenes[i]); |
| 170 | + } |
| 171 | + |
| 172 | + writer.WriteUShort((ushort)NetworkedPrefabs.Count); |
| 173 | + for (int i = 0; i < NetworkedPrefabs.Count; i++) |
| 174 | + { |
| 175 | + writer.WriteBool(NetworkedPrefabs[i].playerPrefab); |
| 176 | + writer.WriteString(NetworkedPrefabs[i].name); |
| 177 | + } |
| 178 | + |
| 179 | + writer.WriteInt(MessageBufferSize); |
| 180 | + writer.WriteInt(ReceiveTickrate); |
| 181 | + writer.WriteInt(MaxReceiveEventsPerTickRate); |
| 182 | + writer.WriteInt(SendTickrate); |
| 183 | + writer.WriteInt(EventTickrate); |
| 184 | + writer.WriteInt(MaxConnections); |
| 185 | + writer.WriteInt(ConnectPort); |
| 186 | + writer.WriteString(ConnectAddress); |
| 187 | + writer.WriteInt(ClientConnectionBufferTimeout); |
| 188 | + writer.WriteBool(ConnectionApproval); |
| 189 | + writer.WriteInt(SecondsHistory); |
| 190 | + writer.WriteBool(HandleObjectSpawning); |
| 191 | + writer.WriteBool(EnableEncryption); |
| 192 | + writer.WriteBool(SignKeyExchange); |
| 193 | + writer.WriteBool(AllowPassthroughMessages); |
| 194 | + writer.WriteBool(EnableSceneSwitching); |
| 195 | + writer.WriteBool(EnableTimeResync); |
| 196 | + |
| 197 | + return Convert.ToBase64String(writer.Finalize()); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + internal void FromBase64(string base64) |
| 202 | + { |
| 203 | + byte[] binary = Convert.FromBase64String(base64); |
| 204 | + using (BitReader reader = BitReader.Get(binary)) |
| 205 | + { |
| 206 | + ProtocolVersion = reader.ReadUShort(); |
| 207 | + Transport = (DefaultTransport)reader.ReadBits(5); |
| 208 | + |
| 209 | + ushort channelCount = reader.ReadUShort(); |
| 210 | + Channels.Clear(); |
| 211 | + for (int i = 0; i < channelCount; i++) |
| 212 | + { |
| 213 | + Channel channel = new Channel() |
| 214 | + { |
| 215 | + Name = reader.ReadString(), |
| 216 | + Encrypted = reader.ReadBool(), |
| 217 | + Type = (ChannelType)reader.ReadBits(5) |
| 218 | + }; |
| 219 | + Channels.Add(channel); |
| 220 | + } |
| 221 | + |
| 222 | + ushort messageTypeCount = reader.ReadUShort(); |
| 223 | + MessageTypes.Clear(); |
| 224 | + for (int i = 0; i < messageTypeCount; i++) |
| 225 | + { |
| 226 | + MessageType messageType = new MessageType() |
| 227 | + { |
| 228 | + Name = reader.ReadString(), |
| 229 | + Passthrough = reader.ReadBool() |
| 230 | + }; |
| 231 | + MessageTypes.Add(messageType); |
| 232 | + } |
| 233 | + |
| 234 | + ushort sceneCount = reader.ReadUShort(); |
| 235 | + RegisteredScenes.Clear(); |
| 236 | + for (int i = 0; i < sceneCount; i++) |
| 237 | + { |
| 238 | + RegisteredScenes.Add(reader.ReadString()); |
| 239 | + } |
| 240 | + |
| 241 | + ushort networkedPrefabsCount = reader.ReadUShort(); |
| 242 | + NetworkedPrefabs.Clear(); |
| 243 | + GameObject root = new GameObject("MLAPI: Dummy prefabs"); |
| 244 | + for (int i = 0; i < networkedPrefabsCount; i++) |
| 245 | + { |
| 246 | + bool playerPrefab = reader.ReadBool(); |
| 247 | + string prefabName = reader.ReadString(); |
| 248 | + GameObject dummyPrefab = new GameObject("REPLACEME: " + prefabName + "(Dummy prefab)", typeof(NetworkedObject)); |
| 249 | + dummyPrefab.GetComponent<NetworkedObject>().NetworkedPrefabName = prefabName; |
| 250 | + dummyPrefab.transform.SetParent(root.transform); //This is just here to not ruin your hierarchy |
| 251 | + NetworkedPrefab networkedPrefab = new NetworkedPrefab() |
| 252 | + { |
| 253 | + playerPrefab = playerPrefab, |
| 254 | + prefab = dummyPrefab |
| 255 | + }; |
| 256 | + NetworkedPrefabs.Add(networkedPrefab); |
| 257 | + } |
| 258 | + |
| 259 | + MessageBufferSize = reader.ReadInt(); |
| 260 | + ReceiveTickrate = reader.ReadInt(); |
| 261 | + MaxReceiveEventsPerTickRate = reader.ReadInt(); |
| 262 | + SendTickrate = reader.ReadInt(); |
| 263 | + EventTickrate = reader.ReadInt(); |
| 264 | + MaxConnections = reader.ReadInt(); |
| 265 | + ConnectPort = reader.ReadInt(); |
| 266 | + ConnectAddress = reader.ReadString(); |
| 267 | + ClientConnectionBufferTimeout = reader.ReadInt(); |
| 268 | + ConnectionApproval = reader.ReadBool(); |
| 269 | + SecondsHistory = reader.ReadInt(); |
| 270 | + HandleObjectSpawning = reader.ReadBool(); |
| 271 | + EnableEncryption = reader.ReadBool(); |
| 272 | + SignKeyExchange = reader.ReadBool(); |
| 273 | + AllowPassthroughMessages = reader.ReadBool(); |
| 274 | + EnableSceneSwitching = reader.ReadBool(); |
| 275 | + EnableTimeResync = reader.ReadBool(); |
| 276 | + } |
| 277 | + } |
140 | 278 |
|
141 | 279 | private byte[] ConfigHash = null;
|
142 | 280 | /// <summary>
|
|
0 commit comments