-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDynmap.cs
266 lines (230 loc) · 11.2 KB
/
Dynmap.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Rocket.API;
using Rocket.API.Collections;
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Timers;
using UnityEngine;
namespace dynmap.core
{
public class DynmapConfiguration : IRocketPluginConfiguration
{
//Konfigurační soubor
public string PrivateKey;
public int syncInterval;
public string WebCoreAddress;
public void LoadDefaults()
{
PrivateKey = "MySecretPrivateKey";
syncInterval = 5000;
WebCoreAddress = "http://localhost";
}
}
public class Dynmap : RocketPlugin<DynmapConfiguration>
{
//Definice proměnných
public static Dynmap Instance;
public List<CSteamID> Nicks = new List<CSteamID>();
public Timer myTimer;
public string directory = Directory.GetCurrentDirectory();
public string[] maps;
public string sendMaps;
public string data = string.Empty;
public string url = string.Empty;
public string encoded = string.Empty;
public string output = string.Empty;
public string map = string.Empty;
public string[] uploadMaps;
public string PrivateKey;
public string postData;
public bool firstrun = true;
public bool shutdown = false;
public string characterName;
public int rotation;
public string playerStatus;
protected override void Load()
{
//Načtení privátního klíče a složky Maps
PrivateKey = Configuration.Instance.PrivateKey;
maps = Directory.GetDirectories(directory + @"/../../../Maps");
//Vypsání map na serveru
foreach (string splitMap in maps)
{
var map = splitMap.Split('\\');
int length = map.Length;
sendMaps += map[length - 1] + ";";
}
//Odeslání map serveru do PHP skriptu
var mapUrl = Configuration.Instance.WebCoreAddress + "/dynmap-core.php?user=server&maps=" + Uri.EscapeDataString(sendMaps);
string postData = "&privatekey=" + PrivateKey;
var post = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(mapUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = post.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(post, 0, post.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
output = reader.ReadToEnd();
}
//Deaktivuje plugin, pokud se PrivateKey neshoduje
if(output == "Error.PrivateKeyNotMatch")
{
Logger.LogError("Priavte keys in dynmap-config.php and in Dynmap.configuration.xml doesn't match!");
Logger.LogError("Unloading plugin!");
return;
}
//Příjem názvů map, které se ještě nenacházejí na serveru
var sentMessage = false;
uploadMaps = output.Split(';');
for (var o = 0; o < uploadMaps.Length; o++)
{
if (sentMessage == false) { Logger.LogWarning("Uploading map files to the server! This may take some time!"); sentMessage = true; };
if (uploadMaps[o] != string.Empty)
{
//Generování TransferID
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rndNumber = new byte[20];
rng.GetBytes(rndNumber);
string TransferID = Convert.ToBase64String(rndNumber);
//TransferID
TransferID = TransferID.Remove(TransferID.Length - 1);
//Odeslání TransferID na server
url = Configuration.Instance.WebCoreAddress + "/dynmap-core.php?user=server";
string TransferIDdata = "TransferID=" + Uri.EscapeDataString(TransferID) + "&privatekey=" + PrivateKey;
var postTransferID = Encoding.ASCII.GetBytes(TransferIDdata);
CookieContainer cookies = new CookieContainer();
HttpWebRequest requestTransferID = (HttpWebRequest)WebRequest.Create(url);
requestTransferID.Method = "POST";
requestTransferID.ContentType = "application/x-www-form-urlencoded";
requestTransferID.ContentLength = TransferIDdata.Length;
requestTransferID.CookieContainer = cookies;
using (var stream = requestTransferID.GetRequestStream())
{
stream.Write(postTransferID, 0, postTransferID.Length);
}
HttpWebResponse responseTransferID = (HttpWebResponse)requestTransferID.GetResponse();
using (Stream stream = responseTransferID.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
output = reader.ReadToEnd();
}
//Nahrání souborů map na server
System.Net.WebClient Client = new System.Net.WebClient ();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile(Configuration.Instance.WebCoreAddress + "/dynmap-core.php?user=server&do=uploadfile&TransferID=" + Uri.EscapeDataString(TransferID) + "&mapname=" + uploadMaps[o], "POST", directory + @"/../../../Maps/" + uploadMaps[o] + @"/Map.png");
String s = System.Text.Encoding.UTF8.GetString (result,0,result.Length);
if (s == "Error.UploadDone")
{
Logger.LogWarning("Uploaded " + uploadMaps[o]);
}
else if (s == "1Error.UploadFailed")
{
Logger.LogError("Uploading " + uploadMaps[o] + " failed because the uploaded file exceeds the upload_max_filesize directive in php.ini.");
Logger.LogError("See http://php.net/manual/en/ini.core.php#ini.upload-max-filesize for further information!");
}
else
{
Logger.LogError("Uploading " + uploadMaps[o] + " failed!");
}
}
if (o == uploadMaps.Length - 1) { Logger.LogWarning("Uploading done!");};
}
//Časovač odesílající data o pozici na server
myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(callFunc);
myTimer.Interval = Configuration.Instance.syncInterval;
myTimer.Enabled = true;
//Přidání hráčů do listu při načtení pluginu
int f = 0;
foreach (SDG.Unturned.SteamPlayer plr in SDG.Unturned.Provider.Players)
{
UnturnedPlayer unturnedPlayer = UnturnedPlayer.FromSteamPlayer(plr);
Nicks.Add(unturnedPlayer.CSteamID);
f++;
}
//Přidání hráčů do listu při připojení na server
U.Events.OnPlayerConnected += (UnturnedPlayer player) =>
{
Nicks.Add(player.CSteamID);
};
//Odebrání hráčů z listu při odpojení ze serveru
U.Events.OnPlayerDisconnected += (UnturnedPlayer player) =>
{
Nicks.Remove(player.CSteamID);
};
U.Events.OnShutdown += () =>
{
shutdown = true;
ShowCords();
};
}
//Zjištění souřadnic
private void callFunc(object sender, EventArgs e)
{
ShowCords();
}
private void ShowCords()
{
int count = Nicks.Count;
data = string.Empty;
url = string.Empty;
encoded = string.Empty;
//Pro každého hráče zjistí jméno, CSteamID a pozici
for (int i = 1; i <= count; i++)
{
UnturnedPlayer player = UnturnedPlayer.FromCSteamID(Nicks[i - 1]);
characterName = player.CharacterName.Replace(";", ";").Replace("[", "[").Replace("]", "]").Replace("=", "=");
rotation = Convert.ToInt32(player.Rotation);
if (player.IsAdmin == true) { playerStatus = "admin"; } else if (player.IsPro == true) { playerStatus = "pro"; } else { playerStatus = "player"; }
UnturnedChat.Say(player, player.Position + "=Position");
if (player.Features.VanishMode == false) { data = data + "[Charactername=" + characterName + ";CSteamID=" + player.CSteamID + ";Position=" + player.Position + ";Rotation=" + rotation + ";PlayerStatus=" + playerStatus + "]"; };
}
//Odešle data na server
if (data != string.Empty || firstrun == true)
{
url = Configuration.Instance.WebCoreAddress + "/dynmap-core.php?user=server";
postData = "map=" + SDG.Unturned.Provider.map + "&data=" + Uri.EscapeDataString(data) + "&privatekey=" + PrivateKey;
if (shutdown == true) { postData = "map=" + SDG.Unturned.Provider.map + "&privatekey=" + PrivateKey; };
var post = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = post.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(post, 0, post.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
output = reader.ReadToEnd();
}
//Deaktivuje plugin, pokud se PrivateKey neshoduje
if (output == "Error.PrivateKeyNotMatch")
{
Logger.LogError("Priavte keys in dynmap-config.php and in Dynmap.configuration.xml doesn't match!");
Logger.LogError("Unloading plugin!");
myTimer.Enabled = false;
}
if (data != string.Empty) { firstrun = true; } else { firstrun = false; };
}
}
}
}