-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cs
More file actions
196 lines (154 loc) · 6.54 KB
/
Server.cs
File metadata and controls
196 lines (154 loc) · 6.54 KB
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
192
193
194
195
196
namespace Pluton.Rust
{
using Core;
using Objects;
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Server : Singleton<Server>, ISingleton
{
public bool Loaded = false;
public Dictionary<ulong, Player> Players;
public Dictionary<ulong, OfflinePlayer> OfflinePlayers;
public Dictionary<string, LoadOut> LoadOuts;
public DataStore serverData;
public static string server_message_name = "Pluton";
private float craftTimeScale = 1f;
public void Broadcast(string arg) => BroadcastFrom(server_message_name, arg);
public void BroadcastFrom(string name, string arg) => ConsoleNetwork.BroadcastToAllClients("chat.add", 0, String.Format("{0}: {1}", name.ColorText("fa5"), arg));
public void BroadcastFrom(ulong playerid, string arg) => ConsoleNetwork.BroadcastToAllClients("chat.add", playerid, arg, 1);
public Player FindPlayer(string s)
{
BasePlayer player = BasePlayer.Find(s);
if (player != null)
return new Player(player);
return null;
}
public Player FindPlayer(ulong steamid)
{
if (Players.ContainsKey(steamid))
return Players[steamid];
return FindPlayer(steamid.ToString());
}
public static Player GetPlayer(BasePlayer basePlayer)
{
try {
Player player = GetInstance().FindPlayer(basePlayer.userID);
if (player != null)
return player;
return new Player(basePlayer);
} catch (Exception ex) {
Logger.LogDebug("[Server] GetPlayer: " + ex.Message);
Logger.LogException(ex);
return null;
}
}
public void Initialize()
{
Instance.LoadOuts = new Dictionary<string, LoadOut>();
//Instance.Structures = new Dictionary<string, StructureRecorder.Structure>();
Instance.Players = new Dictionary<ulong, Player>();
Instance.OfflinePlayers = new Dictionary<ulong, OfflinePlayer>();
Instance.serverData = new DataStore("ServerData.ds");
Instance.serverData.Load();
Instance.LoadLoadouts();
//Instance.LoadStructures();
//Instance.ReloadBlueprints();
Instance.LoadOfflinePlayers();
Instance.CheckPluginsFolder();
}
public void CheckPluginsFolder()
{
string path = Singleton<Util>.Instance.GetPluginsFolder();
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
public float CraftingTimeScale {
get {
return craftTimeScale;
}
set {
craftTimeScale = value;
}
}
public void LoadLoadouts()
{
string path = Singleton<Util>.GetInstance().GetLoadoutFolder();
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
DirectoryInfo loadoutPath = new DirectoryInfo(path);
foreach (FileInfo file in loadoutPath.GetFiles()) {
if (file.Extension == ".ini") {
new LoadOut(file.Name.Replace(".ini", ""));
}
}
Logger.Log("[Server] " + LoadOuts.Count + " loadout loaded!");
}
public void LoadOfflinePlayers()
{
Hashtable ht = serverData.GetTable("OfflinePlayers");
if (ht != null) {
foreach (DictionaryEntry entry in ht) {
Instance.OfflinePlayers.Add(UInt64.Parse(entry.Key as string), entry.Value as OfflinePlayer);
}
} else {
Logger.LogWarning("[Server] No OfflinePlayers found!");
}
Logger.Log("[Server] " + Instance.OfflinePlayers.Count + " offlineplayer loaded!");
}
/*
public void LoadStructures()
{
string path = Util.GetStructuresFolder();
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
DirectoryInfo structuresPath = new DirectoryInfo(path);
Structures.Clear();
foreach (FileInfo file in structuresPath.GetFiles()) {
if (file.Extension.ToLower() == ".sps") {
using (FileStream stream = new FileStream(file.FullName, FileMode.Open)) {
BinaryFormatter formatter = new BinaryFormatter();
StructureRecorder.Structure structure = (StructureRecorder.Structure)formatter.Deserialize(stream);
Structures.Add(file.Name.Substring(0, file.Name.Length - 5), structure);
}
}
}
Logger.Log("[Server] " + Structures.Count.ToString() + " structure loaded!");
}
*/
public void Save()
{
OnShutdown();
foreach (Player p in Players.Values) {
OfflinePlayers.Remove(p.GameID);
}
}
public string SendCommand(string command, params object[] args) => ConsoleSystem.Run.Server.Normal(command, args);
public void OnShutdown()
{
foreach (Player player in Players.Values) {
if (serverData.ContainsKey("OfflinePlayers", player.SteamID)) {
OfflinePlayer op = serverData.Get("OfflinePlayers", player.SteamID) as OfflinePlayer;
op.Update(player);
OfflinePlayers[player.GameID] = op;
} else {
OfflinePlayer op = new OfflinePlayer(player);
OfflinePlayers.Add(player.GameID, op);
}
}
foreach (OfflinePlayer op in OfflinePlayers.Values) {
serverData.Add("OfflinePlayers", op.SteamID, op);
}
serverData.Save();
Singleton<Util>.Instance.SaveZones();
Singleton<Util>.Instance.ZoneStore.Save();
}
public List<Player> ActivePlayers => (from player in BasePlayer.activePlayerList
select GetPlayer(player)).ToList();
public List<Player> SleepingPlayers => (from player in BasePlayer.sleepingPlayerList
select GetPlayer(player)).ToList();
public int MaxPlayers => ConVar.Server.maxplayers;
}
}