-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnetServer.cs
134 lines (112 loc) · 3.78 KB
/
EnetServer.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
using UnityEngine;
using System.Collections;
using System;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.Linq;
namespace DarkRift.Server.Unity
{
public class EnetServer : MonoBehaviour
{
/// <summary>
/// The actual server.
/// </summary>
public DarkRiftServer Server { get; private set; }
[SerializeField]
[Tooltip("The configuration file to use.")]
TextAsset configuration;
[SerializeField]
[Tooltip("Indicates whether the server will be created in the OnEnable method.")]
bool createOnEnable = true;
[SerializeField]
[Tooltip("Indicates whether the server events will be routed through the dispatcher or just invoked.")]
bool eventsFromDispatcher = true;
[SerializeField]
[Tooltip("Specifies whether the server should receive messages in Update(), FixedUpdate() or manually")]
UpdateMode updateMode;
private EnetListenerPlugin enetListener;
void OnEnable()
{
//If createOnEnable is selected create a server
if (createOnEnable)
Create();
}
void Update()
{
if (updateMode == UpdateMode.Update)
{
ReceiveMessages();
}
}
void FixedUpdate()
{
if (updateMode == UpdateMode.FixedUpdate)
{
ReceiveMessages();
}
}
/// <summary>
/// Creates the server.
/// </summary>
public void Create()
{
Create(new NameValueCollection());
}
/// <summary>
/// Creates the server.
/// </summary>
public void Create(NameValueCollection variables)
{
if (Server != null)
throw new InvalidOperationException("The server has already been created! (Is CreateOnEnable enabled?)");
if (configuration != null)
{
//Create spawn data from config
ServerSpawnData spawnData = ServerSpawnData.CreateFromXml(XDocument.Parse(configuration.text), variables);
//Inaccessible from xml, set from inspector
spawnData.EventsFromDispatcher = eventsFromDispatcher;
//Unity is broken, work around it...
//This is an obsolete property but is still used if the user is using obsolete <server> tag properties
#pragma warning disable 0618
spawnData.Server.UseFallbackNetworking = true;
#pragma warning restore 0618
//Add types
spawnData.PluginSearch.PluginTypes.AddRange(UnityServerHelper.SearchForPlugins());
spawnData.PluginSearch.PluginTypes.Add(typeof(UnityConsoleWriter));
//Create server
Server = new DarkRiftServer(spawnData);
Server.Start();
enetListener = Server.NetworkListenerManager.GetNetworkListenersByType<EnetListenerPlugin>().First();
}
else
Debug.LogError("No configuration file specified!");
}
/// <summary>
/// Call this to manually receive messages
/// </summary>
public void ReceiveMessages()
{
if (Server != null)
{
enetListener?.ServerTick();
Server.ExecuteDispatcherTasks();
}
}
void OnDisable()
{
Close();
}
void OnApplicationQuit()
{
Close();
}
/// <summary>
/// Closes the server.
/// </summary>
public void Close()
{
if (Server != null)
Server.Dispose();
}
}
}