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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
|
#include "StarUniverseServer.hpp"
#include "StarFile.hpp"
#include "StarLogging.hpp"
#include "StarJsonExtra.hpp"
#include "StarEncode.hpp"
#include "StarRoot.hpp"
#include "StarCommandProcessor.hpp"
#include "StarTeamManager.hpp"
#include "StarChatProcessor.hpp"
#include "StarConfiguration.hpp"
#include "StarVersioningDatabase.hpp"
#include "StarAssets.hpp"
#include "StarTcp.hpp"
#include "StarCelestialDatabase.hpp"
#include "StarWorldTemplate.hpp"
#include "StarSecureRandom.hpp"
#include "StarSha256.hpp"
#include "StarSky.hpp"
#include "StarAiDatabase.hpp"
#include "StarBiomeDatabase.hpp"
#include "StarUniverseServerLuaBindings.hpp"
namespace Star {
UniverseServer::UniverseServer(String const& storageDir)
: Thread("UniverseServer"),
m_workerPool("UniverseServerWorkerPool"),
m_clients(MinClientConnectionId, MaxClientConnectionId) {
String const LockFile = "universe.lock";
m_storageDirectory = storageDir;
if (!File::isDirectory(m_storageDirectory)) {
Logger::info("UniverseServer: Creating universe storage directory");
File::makeDirectory(m_storageDirectory);
}
auto& root = Root::singleton();
auto assets = root.assets();
auto configuration = root.configuration();
if (auto assetsDigestOverride = configuration->get("serverOverrideAssetsDigest").optString()) {
m_assetsDigest = hexDecode(*assetsDigestOverride);
Logger::info("UniverseServer: Overriding assets digest as '{}'", *assetsDigestOverride);
} else {
m_assetsDigest = assets->digest();
}
startLuaScripts();
m_commandProcessor = make_shared<CommandProcessor>(this, m_luaRoot);
m_chatProcessor = make_shared<ChatProcessor>();
m_chatProcessor->setCommandHandler(bind(&CommandProcessor::userCommand, m_commandProcessor.get(), _1, _2, _3));
Logger::info("UniverseServer: Acquiring universe lock file");
m_storageDirectoryLock = LockFile::acquireLock(File::relativeTo(m_storageDirectory, LockFile));
if (!m_storageDirectoryLock)
throw UniverseServerException("Could not acquire lock for the universe directory");
if (configuration->get("clearUniverseFiles").toBool()) {
Logger::info("UniverseServer: Clearing all universe files");
for (auto file : File::dirList(storageDir)) {
if (!file.second && file.first != LockFile)
File::remove(File::relativeTo(storageDir, file.first));
}
}
m_celestialDatabase = make_shared<CelestialMasterDatabase>(File::relativeTo(m_storageDirectory, "universe.chunks"));
Logger::info("UniverseServer: Loading settings");
loadSettings();
loadTempWorldIndex();
m_lastClockUpdateSent = 0.0;
m_stop = false;
m_tcpState = TcpState::No;
m_storageTriggerDeadline = 0;
m_clearBrokenWorldsDeadline = 0;
m_maxPlayers = configuration->get("maxPlayers").toUInt();
auto universeConfig = assets->json("/universe_server.config");
for (auto const& pair : universeConfig.get("speciesShips").iterateObject())
m_speciesShips[pair.first] = jsonToStringList(pair.second);
m_teamManager = make_shared<TeamManager>();
m_workerPool.start(universeConfig.getUInt("workerPoolThreads"));
m_connectionServer = make_shared<UniverseConnectionServer>(bind(&UniverseServer::packetsReceived, this, _1, _2, _3));
m_pause = make_shared<atomic<bool>>(false);
}
UniverseServer::~UniverseServer() {
stop();
stopLua();
join();
m_workerPool.stop();
RecursiveMutexLocker locker(m_mainLock);
WriteLocker clientsLocker(m_clientsLock);
m_connectionServer->removeAllConnections();
m_deadConnections.clear();
// Make sure that all world threads and net sockets (and associated threads)
// are shutdown before other member destruction.
m_clients.clear();
m_worlds.clear();
}
void UniverseServer::setListeningTcp(bool listenTcp) {
if (!listenTcp || m_tcpState != TcpState::Fuck)
m_tcpState = listenTcp ? TcpState::Yes : TcpState::No;
}
void UniverseServer::addClient(UniverseConnection remoteConnection) {
RecursiveMutexLocker locker(m_mainLock);
// Binding requires us to make the given lambda copy constructible, so the
// make_shared is requried here.
m_connectionAcceptThreads.append(Thread::invoke("UniverseServer::acceptConnection", [this, conn = make_shared<UniverseConnection>(std::move(remoteConnection))]() {
acceptConnection(std::move(*conn), {});
}));
}
UniverseConnection UniverseServer::addLocalClient() {
auto pair = LocalPacketSocket::openPair();
addClient(UniverseConnection(std::move(pair.first)));
return UniverseConnection(std::move(pair.second));
}
void UniverseServer::stop() {
m_stop = true;
}
void UniverseServer::setPause(bool pause) {
ReadLocker clientsLocker(m_clientsLock);
// Pausing is disabled for multiplayer
if (m_clients.size() > 1)
pause = false;
if (pause == *m_pause)
return;
*m_pause = pause;
if (pause)
m_universeClock->stop();
else
m_universeClock->start();
for (auto& p : m_clients)
m_connectionServer->sendPackets(p.first, {make_shared<PausePacket>(*m_pause, GlobalTimescale)});
}
void UniverseServer::setTimescale(float timescale) {
ReadLocker clientsLocker(m_clientsLock);
GlobalTimescale = timescale;
for (auto& p : m_clients)
m_connectionServer->sendPackets(p.first, {make_shared<PausePacket>(*m_pause, GlobalTimescale)});
}
void UniverseServer::setTickRate(float tickRate) {
ServerGlobalTimestep = 1.0f / tickRate;
}
List<WorldId> UniverseServer::activeWorlds() const {
RecursiveMutexLocker locker(m_mainLock);
return m_worlds.keys();
}
bool UniverseServer::isWorldActive(WorldId const& worldId) const {
RecursiveMutexLocker locker(m_mainLock);
return m_worlds.contains(worldId);
}
List<ConnectionId> UniverseServer::clientIds() const {
ReadLocker clientsLocker(m_clientsLock);
return m_clients.keys();
}
List<pair<ConnectionId, int64_t>> UniverseServer::clientIdsAndCreationTime() const {
List<pair<ConnectionId, int64_t>> result;
ReadLocker clientsLocker(m_clientsLock);
result.reserve(m_clients.size());
for (auto& pair : m_clients)
result.emplaceAppend(pair.first, pair.second->creationTime());
return result;
}
size_t UniverseServer::numberOfClients() const {
ReadLocker clientsLocker(m_clientsLock);
return m_clients.size();
}
uint32_t UniverseServer::maxClients() const {
return m_maxPlayers;
}
bool UniverseServer::isConnectedClient(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
return m_clients.contains(clientId);
}
String UniverseServer::clientDescriptor(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->descriptiveName();
else
return "disconnected_client";
}
String UniverseServer::clientNick(ConnectionId clientId) const {
return m_chatProcessor->connectionNick(clientId);
}
Maybe<ConnectionId> UniverseServer::findNick(String const& nick) const {
return m_chatProcessor->findNick(nick);
}
Maybe<Uuid> UniverseServer::uuidForClient(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->playerUuid();
return {};
}
Maybe<ConnectionId> UniverseServer::clientForUuid(Uuid const& uuid) const {
ReadLocker clientsLocker(m_clientsLock);
return getClientForUuid(uuid);
}
void UniverseServer::adminBroadcast(String const& text) {
m_chatProcessor->adminBroadcast(text);
}
void UniverseServer::adminWhisper(ConnectionId clientId, String const& text) {
m_chatProcessor->adminWhisper(clientId, text);
}
String UniverseServer::adminCommand(String text) {
String command = text.extract();
return m_commandProcessor->adminCommand(command, text);
}
bool UniverseServer::isAdmin(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->isAdmin();
return false;
}
bool UniverseServer::canBecomeAdmin(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->canBecomeAdmin();
return false;
}
void UniverseServer::setAdmin(ConnectionId clientId, bool admin) {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
clientContext->setAdmin(admin);
}
bool UniverseServer::isLocal(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return !clientContext->remoteAddress();
return false;
}
bool UniverseServer::isPvp(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->team().type == TeamType::PVP;
return false;
}
void UniverseServer::setPvp(ConnectionId clientId, bool pvp) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId)) {
if (pvp) {
TeamNumber pvpTeam = m_teamManager->getPvpTeam(clientContext->playerUuid());
if (pvpTeam == 0)
pvpTeam = soloPvpTeam(clientId);
clientContext->setTeam(EntityDamageTeam(TeamType::PVP, pvpTeam));
} else
clientContext->setTeam(EntityDamageTeam(TeamType::Friendly));
}
}
RpcThreadPromise<Json> UniverseServer::sendWorldMessage(WorldId const& worldId, String const& message, JsonArray const& args) {
auto pair = RpcThreadPromise<Json>::createPair();
RecursiveMutexLocker locker(m_mainLock);
m_pendingWorldMessages[worldId].push_back({ message, args, pair.second });
return pair.first;
}
void UniverseServer::clientWarpPlayer(ConnectionId clientId, WarpAction action, bool deploy) {
RecursiveMutexLocker locker(m_mainLock);
m_pendingPlayerWarps[clientId] = pair<WarpAction, bool>(std::move(action), std::move(deploy));
}
void UniverseServer::clientFlyShip(ConnectionId clientId, Vec3I const& system, SystemLocation const& location, Json const& settings) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (m_pendingFlights.contains(clientId) || m_queuedFlights.contains(clientId))
return;
auto clientContext = m_clients.get(clientId);
if (!clientContext)
return;
if (system == Vec3I()) {
m_pendingFlights.set(clientId, make_tuple(Vec3I(), SystemLocation(), settings)); // find starter world
return;
}
auto clientSystem = clientContext->systemWorld();
bool sameSystem = clientSystem && clientSystem->location() == system;
bool sameLocation = clientSystem && clientSystem->clientShipLocation(clientId) == location;
if (m_pendingArrivals.contains(clientId) && sameSystem && location && !sameLocation) {
// for continuing flight within a system, set the new destination immediately
clientSystem->setClientDestination(clientId, location);
return;
}
// don't switch systems while already flying
if (!m_pendingArrivals.contains(clientId) || sameSystem)
m_pendingFlights.set(clientId, make_tuple(system, location, settings));
}
WorldId UniverseServer::clientWorld(ConnectionId clientId) const {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->playerWorldId();
return WorldId();
}
CelestialCoordinate UniverseServer::clientShipCoordinate(ConnectionId clientId) const {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId))
return clientContext->shipCoordinate();
return CelestialCoordinate();
}
ClockPtr UniverseServer::universeClock() const {
return m_universeClock;
}
UniverseSettingsPtr UniverseServer::universeSettings() const {
return m_universeSettings;
}
CelestialDatabase & UniverseServer::celestialDatabase() {
return *m_celestialDatabase;
}
bool UniverseServer::executeForClient(ConnectionId clientId, function<void(WorldServer*, PlayerPtr)> action) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
bool success = false;
if (auto clientContext = m_clients.value(clientId)) {
if (auto currentWorld = clientContext->playerWorld()) {
currentWorld->executeAction([clientId, action, &success](WorldServerThread*, WorldServer* worldServer) {
if (auto player = worldServer->clientPlayer(clientId)) {
action(worldServer, player);
success = true;
}
});
}
}
return success;
}
void UniverseServer::disconnectClient(ConnectionId clientId, String const& reason) {
RecursiveMutexLocker locker(m_mainLock);
m_pendingDisconnections.add(clientId, reason);
}
void UniverseServer::banUser(ConnectionId clientId, String const& reason, pair<bool, bool> banType, Maybe<int> timeout) {
RecursiveMutexLocker locker(m_mainLock);
if (timeout)
doTempBan(clientId, reason, banType, *timeout);
else
doPermBan(clientId, reason, banType);
m_pendingDisconnections.add(clientId, reason);
}
bool UniverseServer::unbanUuid(String const& uuidString) {
RecursiveMutexLocker locker(m_mainLock);
bool entryFound = false;
auto config = Root::singleton().configuration();
auto bannedUuids = config->get("bannedUuids").toArray();
eraseWhere(bannedUuids, [&](Json const& entry) {
if (entry.getString("uuid") == uuidString) {
entryFound = true;
return true;
}
return false;
});
config->set("bannedUuids", bannedUuids);
eraseWhere(m_tempBans, [&](TimeoutBan const& b) {
if (b.uuid && b.uuid->hex() == uuidString) {
entryFound = true;
return true;
}
return false;
});
return entryFound;
}
bool UniverseServer::unbanIp(String const& addressString) {
RecursiveMutexLocker locker(m_mainLock);
auto addressLookup = HostAddress::lookup(addressString);
if (addressLookup.isLeft()) {
return false;
} else {
HostAddress address = std::move(addressLookup.right());
String cleanAddressString = toString(address);
bool entryFound = false;
auto config = Root::singleton().configuration();
auto bannedIPs = config->get("bannedIPs").toArray();
eraseWhere(bannedIPs, [&](Json const& entry) {
if (entry.getString("ip") == cleanAddressString) {
entryFound = true;
return true;
}
return false;
});
config->set("bannedIPs", bannedIPs);
eraseWhere(m_tempBans, [&](TimeoutBan const& b) {
if (b.ip && b.ip.value() == address) {
entryFound = true;
return true;
}
return false;
});
return entryFound;
}
}
bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, String const& newType, String const& weatherBiome) {
RecursiveMutexLocker locker(m_mainLock);
if (!coordinate.isNull() && m_celestialDatabase->coordinateValid(coordinate)) {
if (auto celestialParameters = m_celestialDatabase->parameters(coordinate)) {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(celestialParameters->visitableParameters())) {
auto newTerrestrialParameters = make_shared<TerrestrialWorldParameters>(*terrestrialParameters);
newTerrestrialParameters->typeName = newType;
auto biomeDatabase = Root::singleton().biomeDatabase();
auto newWeatherPool = biomeDatabase->biomeWeathers(weatherBiome, celestialParameters->seed(), terrestrialParameters->threatLevel);
newTerrestrialParameters->weatherPool = newWeatherPool;
newTerrestrialParameters->terraformed = true;
celestialParameters->setVisitableParameters(newTerrestrialParameters);
m_celestialDatabase->updateParameters(coordinate, *celestialParameters);
ReadLocker clientsLocker(m_clientsLock);
for (auto clientId : m_clients.keys())
m_connectionServer->sendPackets(clientId, {make_shared<PlanetTypeUpdatePacket>(coordinate)});
return true;
}
}
}
return false;
}
bool UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (m_clients.contains(clientId)) {
clientsLocker.unlock();
m_connectionServer->sendPackets(clientId, {packet});
return true;
}
return false;
}
void UniverseServer::run() {
Logger::info("UniverseServer: Starting UniverseServer with UUID: {}", m_universeSettings->uuid().hex());
int mainWakeupInterval = Root::singleton().assets()->json("/universe_server.config:mainWakeupInterval").toInt();
TcpServerPtr tcpServer;
while (!m_stop) {
if (m_tcpState == TcpState::Yes && !tcpServer) {
auto& root = Root::singleton();
auto configuration = root.configuration();
auto assets = root.assets();
HostAddressWithPort bindAddress(configuration->get("gameServerBind").toString(), configuration->get("gameServerPort").toUInt());
unsigned maxPendingConnections = assets->json("/universe_server.config:maxPendingConnections").toInt();
Logger::info("UniverseServer: listening for incoming TCP connections on {}", bindAddress);
try {
tcpServer = make_shared<TcpServer>(bindAddress);
tcpServer->setAcceptCallback([this, maxPendingConnections](TcpSocketPtr socket) {
RecursiveMutexLocker locker(m_mainLock);
if (m_connectionAcceptThreads.size() < maxPendingConnections) {
Logger::info("UniverseServer: Connection received from: {}", socket->remoteAddress());
m_connectionAcceptThreads.append(Thread::invoke("UniverseServer::acceptConnection", [this, socket]() {
acceptConnection(UniverseConnection(TcpPacketSocket::open(socket)), socket->remoteAddress().address());
}));
}
else {
Logger::warn("UniverseServer: maximum pending connections, dropping connection from: {}", socket->remoteAddress().address());
}
});
}
catch (StarException const& e) {
Logger::error("UniverseServer: Error setting up TCP, cannot accept connections: {}", e.what());
m_tcpState = TcpState::Fuck;
tcpServer.reset();
}
} else if (m_tcpState == TcpState::No && tcpServer) {
Logger::info("UniverseServer: Not listening for incoming TCP connections");
tcpServer.reset();
}
LogMap::set("universe_time", m_universeClock->time());
try {
updateLua();
processUniverseFlags();
removeTimedBan();
sendPendingChat();
updateTeams();
updateShips();
sendClockUpdates();
kickErroredPlayers();
reapConnections();
processPlanetTypeChanges();
warpPlayers();
flyShips();
arriveShips();
processChat();
sendClientContextUpdates();
respondToCelestialRequests();
clearBrokenWorlds();
handleWorldMessages();
shutdownInactiveWorlds();
doTriggeredStorage();
} catch (std::exception const& e) {
Logger::error("UniverseServer: exception caught: {}", outputException(e, true));
}
Thread::sleep(mainWakeupInterval);
}
Logger::info("UniverseServer: Stopping UniverseServer");
try {
m_workerPool.stop();
if (tcpServer) {
Logger::info("UniverseServer: Stopping TCP Server");
tcpServer.reset();
}
RecursiveMutexLocker locker(m_mainLock);
WriteLocker clientsLocker(m_clientsLock);
for (auto clientId : m_clients.keys())
doDisconnection(clientId, "ServerShutdown");
saveSettings();
saveTempWorldIndex();
m_worlds.clear();
} catch (std::exception const& e) {
Logger::error("UniverseServer: exception caught cleaning up: {}", outputException(e, true));
}
}
void UniverseServer::processUniverseFlags() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (auto actions = m_universeSettings->pullPendingFlagActions()) {
for (auto action : *actions) {
if (action.is<PlaceDungeonFlagAction>()) {
auto placeDungeonAction = action.get<PlaceDungeonFlagAction>();
if (instanceWorldStoredOrActive(placeDungeonAction.targetInstance)) {
auto worldId = InstanceWorldId(placeDungeonAction.targetInstance);
m_pendingFlagActions.append(pair<WorldId, UniverseFlagAction>{worldId, placeDungeonAction});
}
}
}
}
eraseWhere(m_pendingFlagActions, [this](pair<WorldId, UniverseFlagAction> const& p) {
if (p.first.is<InstanceWorldId>() && instanceWorldStoredOrActive(p.first.get<InstanceWorldId>())) {
// world is stored or active; perform flag actions once it loads
if (auto maybeTargetWorld = triggerWorldCreation(p.first)) {
if (auto targetWorld = maybeTargetWorld.value()) {
if (p.second.is<PlaceDungeonFlagAction>()) {
auto placeDungeonAction = p.second.get<PlaceDungeonFlagAction>();
targetWorld->executeAction([placeDungeonAction](WorldServerThread*, WorldServer* worldServer) {
worldServer->placeDungeon(placeDungeonAction.dungeonId, placeDungeonAction.targetPosition, 0);
});
}
return true;
}
}
return false;
} else {
// world hasn't yet been created; flag actions will be handled by normal creation
return true;
}
});
}
void UniverseServer::sendPendingChat() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto const& p : m_clients) {
for (auto const& message : m_chatProcessor->pullPendingMessages(p.first))
m_connectionServer->sendPackets(p.first, {make_shared<ChatReceivePacket>(message)});
}
}
void UniverseServer::updateTeams() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
StringMap<List<Uuid>> connectedPlayers;
auto teams = m_teamManager->getPvpTeams();
for (auto const& p : m_clients) {
connectedPlayers[p.second->playerName().toLower()].append(p.second->playerUuid());
if (p.second->team().type == TeamType::PVP)
p.second->setTeam(EntityDamageTeam(TeamType::PVP, teams.value(p.second->playerUuid(), soloPvpTeam(p.second->clientId()))));
else
p.second->setTeam(EntityDamageTeam(TeamType::Friendly));
auto channels = m_chatProcessor->clientChannels(p.first);
auto team = m_teamManager->getTeam(p.second->playerUuid());
for (auto const& channel : channels) {
if (channel != printWorldId(p.second->playerWorldId()) && (!team || channel != team.value().hex()))
m_chatProcessor->leaveChannel(p.first, channel);
}
if (team && !channels.contains(team.value().hex()))
m_chatProcessor->joinChannel(p.first, team.value().hex());
}
m_teamManager->setConnectedPlayers(connectedPlayers);
}
void UniverseServer::updateShips() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto const& p : m_clients) {
auto newShipUpgrades = p.second->shipUpgrades();
if (auto shipWorld = getWorld(ClientShipWorldId(p.second->playerUuid()))) {
shipWorld->executeAction([&](WorldServerThread*, WorldServer* shipWorld) {
String species;
Json jSpecies = shipWorld->getProperty("ship.species");
if (jSpecies.isType(Json::Type::String))
species = jSpecies.toString();
else
shipWorld->setProperty("ship.species", species = p.second->playerSpecies());
auto const& speciesShips = m_speciesShips.get(species);
Json jOldShipLevel = shipWorld->getProperty("ship.level");
unsigned newShipLevel = min<unsigned>(speciesShips.size() - 1, newShipUpgrades.shipLevel);
if (jOldShipLevel.isType(Json::Type::Int)) {
auto oldShipLevel = jOldShipLevel.toUInt();
if (oldShipLevel < newShipLevel) {
for (unsigned i = oldShipLevel + 1; i <= newShipLevel; ++i) {
auto shipStructure = WorldStructure(speciesShips[i]);
shipWorld->setCentralStructure(shipStructure);
newShipUpgrades.apply(shipStructure.configValue("shipUpgrades"));
}
p.second->setShipUpgrades(newShipUpgrades);
p.second->updateShipChunks(shipWorld->readChunks());
}
}
shipWorld->setProperty("ship.level", newShipUpgrades.shipLevel);
shipWorld->setProperty("ship.maxFuel", newShipUpgrades.maxFuel);
shipWorld->setProperty("ship.crewSize", newShipUpgrades.crewSize);
shipWorld->setProperty("ship.fuelEfficiency", newShipUpgrades.fuelEfficiency);
});
}
if (auto systemWorld = p.second->systemWorld()) {
float speed = newShipUpgrades.shipSpeed;
systemWorld->executeClientShipAction(p.first, [speed](SystemClientShip* ship) {
if (ship)
ship->setSpeed(speed);
});
}
}
}
void UniverseServer::sendClockUpdates() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
int64_t currentTime = Time::monotonicMilliseconds();
if (currentTime > m_lastClockUpdateSent + Root::singleton().assets()->json("/universe_server.config:clockUpdatePacketInterval").toInt()) {
for (auto clientId : m_clients.keys())
m_connectionServer->sendPackets(clientId, {make_shared<UniverseTimeUpdatePacket>(m_universeClock->time())});
m_lastClockUpdateSent = currentTime;
}
}
void UniverseServer::sendClientContextUpdate(ServerClientContextPtr clientContext) {
auto clientContextData = clientContext->writeUpdate();
if (!clientContextData.empty())
m_connectionServer->sendPackets(clientContext->clientId(), {make_shared<ClientContextUpdatePacket>(clientContextData)});
}
void UniverseServer::sendClientContextUpdates() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto const& p : m_clients)
sendClientContextUpdate(p.second);
}
void UniverseServer::kickErroredPlayers() {
RecursiveMutexLocker locker(m_mainLock);
for (auto const& worldId : m_worlds.keys()) {
if (auto world = getWorld(worldId)) {
for (auto clientId : world->erroredClients())
m_pendingDisconnections[clientId] = "Incoming client packet has caused exception";
}
}
}
void UniverseServer::reapConnections() {
RecursiveMutexLocker locker(m_mainLock);
int64_t startTime = Time::monotonicMilliseconds();
int64_t timeout = Root::singleton().assets()->json("/universe_server.config:connectionTimeout").toInt();
eraseWhere(m_connectionAcceptThreads, [](ThreadFunction<void>& function) {
if (!function.isRunning()) {
try {
function.finish();
} catch (std::exception const& e) {
Logger::error("UniverseServer: Exception caught accepting new connection: {}", outputException(e, true));
}
}
return function.isFinished();
});
WriteLocker clientsLocker(m_clientsLock);
for (auto p : take(m_pendingDisconnections))
doDisconnection(p.first, p.second);
for (auto clientId : m_clients.keys()) {
auto clientContext = m_clients.value(clientId);
if (!m_connectionServer->connectionIsOpen(clientId)) {
Logger::info("UniverseServer: Client {} connection lost", clientContext->descriptiveName());
doDisconnection(clientId, String("Disconnected due to connection lost"));
} else {
if (clientContext->remoteAddress() && startTime - m_connectionServer->lastActivityTime(clientId) > timeout) {
Logger::info("UniverseServer: Kicking client {} due to inactivity", clientContext->descriptiveName());
doDisconnection(clientId, String("Disconnected due to inactivity"));
}
}
}
// Once connections are waiting to close, send any pending data and wait up
// to the connection timeout for the client to do the closing to ensure the
// client has all the data.
size_t previousDeadConnections = m_deadConnections.size();
m_deadConnections.filter([startTime, timeout](auto& pair) {
if (pair.first.send())
pair.second = startTime;
return pair.first.isOpen() && startTime - pair.second < timeout;
});
if (previousDeadConnections > m_deadConnections.size())
Logger::info("UniverseServer: Reaped {} dead connections", previousDeadConnections);
}
void UniverseServer::processPlanetTypeChanges() {
RecursiveMutexLocker locker(m_mainLock);
for (auto const& worldId : m_worlds.keys()) {
if (auto celestialWorldId = worldId.ptr<CelestialWorldId>()) {
if (auto world = getWorld(worldId)) {
if (auto newPlanetType = world->pullNewPlanetType())
updatePlanetType(*celestialWorldId, newPlanetType->first, newPlanetType->second);
}
}
}
}
void UniverseServer::warpPlayers() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto const& clientId : m_pendingPlayerWarps.keys()) {
auto& warp = m_pendingPlayerWarps.get(clientId);
WarpAction warpAction = warp.first;
bool deploy = warp.second;
auto clientContext = m_clients.value(clientId);
if (!clientContext)
continue;
WarpToWorld warpToWorld = resolveWarpAction(warpAction, clientId, deploy);
if (auto maybeToWorld = triggerWorldCreation(warpToWorld.world)) {
Logger::info("UniverseServer: Warping player {} to {}", clientId, printWarpAction(warpToWorld));
if (auto toWorld = maybeToWorld.value()) {
if (toWorld->spawnTargetValid(warpToWorld.target)) {
if (auto currentWorld = clientContext->playerWorld()) {
if (auto playerRevivePosition = currentWorld->playerRevivePosition(clientId))
clientContext->setPlayerReturnWarp(WarpToWorld{currentWorld->worldId(), SpawnTargetPosition(*playerRevivePosition)});
clientContext->clearPlayerWorld();
m_connectionServer->sendPackets(clientId, currentWorld->removeClient(clientId));
m_chatProcessor->leaveChannel(clientId, printWorldId(currentWorld->worldId()));
}
clientContext->setOrbitWarpAction({});
// having stale world ids in the client context is bad,
// make sure it's at least null until the next client context update
sendClientContextUpdate(clientContext);
// Checking the spawn target validity then adding the client is not
// perfect, it can still become invalid in between, if we fail at
// adding the client we need to warp them back.
if (toWorld && toWorld->addClient(clientId, warpToWorld.target,
!clientContext->remoteAddress(),
clientContext->canBecomeAdmin(),
clientContext->netRules())) {
clientContext->setPlayerWorld(toWorld);
m_chatProcessor->joinChannel(clientId, printWorldId(warpToWorld.world));
if (warpToWorld.world.is<ClientShipWorldId>()) {
if (auto clientId = getClientForUuid(warpToWorld.world.get<ClientShipWorldId>())) {
if (auto systemWorld = m_clients.get(*clientId)->systemWorld())
clientContext->setOrbitWarpAction(systemWorld->clientWarpAction(*clientId));
}
}
} else if (auto returnWarp = clientContext->playerReturnWarp()) {
Logger::info("UniverseServer: Warping player {} failed, returning to '{}'", clientId, printWarpAction(returnWarp));
m_pendingPlayerWarps[clientId] = {returnWarp, false};
} else {
Logger::info("UniverseServer: Warping player {} failed, returning to ship", clientId);
m_pendingPlayerWarps[clientId] = {WarpAlias::OwnShip, false};
}
m_connectionServer->sendPackets(clientId, {make_shared<PlayerWarpResultPacket>(true, warpAction, false)});
m_pendingPlayerWarps.remove(clientId);
} else {
Logger::info("UniverseServer: Warping player {} failed, invalid spawn target '{}'", clientId, printSpawnTarget(warpToWorld.target));
m_connectionServer->sendPackets(clientId, {make_shared<PlayerWarpResultPacket>(false, warpAction, true)});
m_pendingPlayerWarps.remove(clientId);
}
} else {
Logger::info("UniverseServer: Warping player {} failed, invalid world '{}' or world failed to load", clientId, printWorldId(warpToWorld.world));
m_connectionServer->sendPackets(clientId, {make_shared<PlayerWarpResultPacket>(false, warpAction, false)});
m_pendingPlayerWarps.remove(clientId);
}
} else {
// If the world is not created yet, just set a new warp again to wait for
// it to create.
m_pendingPlayerWarps[clientId] = {warpAction, deploy};
}
}
}
void UniverseServer::flyShips() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
double queuedFlightWaitTime = Root::singleton().assets()->json("/universe_server.config:queuedFlightWaitTime").toDouble();
for (auto clientId : m_queuedFlights.keys()) {
if (!m_pendingFlights.contains(clientId) && !m_pendingArrivals.contains(clientId)) {
auto& flight = m_queuedFlights.get(clientId);
if (flight.second.isNothing())
flight.second = m_universeClock->time() + queuedFlightWaitTime;
else if (m_universeClock->time() > *flight.second)
m_pendingFlights.set(clientId, flight.first);
if (m_pendingFlights.contains(clientId))
m_queuedFlights.remove(clientId);
}
}
eraseWhere(m_pendingFlights, [this](pair<ConnectionId const, tuple<Vec3I, SystemLocation, Json>> const& p) {
ConnectionId clientId = p.first;
Vec3I system = get<0>(p.second);
SystemLocation location = get<1>(p.second);
Json settings = get<2>(p.second);
auto clientContext = m_clients.value(clientId);
if (!clientContext)
return true;
auto clientSystem = clientContext->systemWorld();
if (!clientSystem)
system = Vec3I();
if (system != Vec3I() && clientContext->shipCoordinate().location() == system && clientContext->shipLocation() == location)
return true;
// if the ship is flying to another system do nothing
// if the ship is flying within the target system, just update the ship destination
if (m_pendingArrivals.contains(clientId)) {
return true;
}
auto maybeClientShip = triggerWorldCreation(ClientShipWorldId(clientContext->playerUuid()));
if (!maybeClientShip)
return false; // ship is not loaded yet
auto clientShip = *maybeClientShip;
if (!clientShip)
return true; // ship is broken
CelestialCoordinate destination = location.maybe<CelestialCoordinate>().value(CelestialCoordinate(system));
bool interstellar = clientSystem ? clientContext->shipCoordinate().location() != system : true;
if (!interstellar) {
// don't fly to null locations in the same system
if (!location)
return true;
clientSystem->setClientDestination(clientId, location);
} else if (system != Vec3I()) {
// changing systems
clientSystem->removeClient(clientId);
clientContext->setSystemWorld({});
if (location)
m_queuedFlights.set(clientId, {make_tuple(system, location, settings), {}});
destination = CelestialCoordinate(system);
}
if (destination.isNull())
Logger::info("Flying ship for player {} to new starter world", clientId);
else
Logger::info("Flying ship for player {} to {}", clientId, destination);
bool startInWarp = system == Vec3I();
clientShip->executeAction([interstellar, startInWarp, settings](WorldServerThread*, WorldServer* worldServer) {
worldServer->startFlyingSky(interstellar, startInWarp, settings);
});
clientContext->setShipCoordinate(CelestialCoordinate(system));
clientContext->setOrbitWarpAction({});
for (auto clientId : clientShip->clients()) {
if (auto clientContext = m_clients.get(clientId))
clientContext->setOrbitWarpAction({});
}
m_pendingArrivals.set(clientId, destination);
return true;
});
}
void UniverseServer::arriveShips() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
eraseWhere(m_pendingArrivals, [this](pair<ConnectionId const, CelestialCoordinate>& p) {
auto& clientId = p.first;
auto& coordinate = p.second;
if (!coordinate)
coordinate = nextStarterWorld().value();
if (!coordinate)
return false;
auto clientContext = m_clients.value(clientId);
if (!clientContext)
return true;
auto clientSystem = clientContext->systemWorld();
if (!clientSystem) {
clientSystem = createSystemWorld(coordinate.location());
if (coordinate.isSystem())
clientSystem->addClient(clientId, clientContext->playerUuid(), clientContext->shipUpgrades().shipSpeed, {});
else
clientSystem->addClient(clientId, clientContext->playerUuid(), clientContext->shipUpgrades().shipSpeed, coordinate);
clientContext->setSystemWorld(clientSystem);
}
auto location = clientSystem->clientShipLocation(clientId);
if (!location)
return false;
if (!coordinate.isSystem() && !triggerWorldCreation(CelestialWorldId(coordinate)))
return false;
Logger::info("UniverseServer: Arriving ship for player {} at {}", clientId, coordinate);
// world is loaded, ship has arrived
clientContext->setShipCoordinate(coordinate);
clientContext->setShipLocation(location);
if (auto clientShip = createWorld(ClientShipWorldId(clientContext->playerUuid()))) {
auto skyParameters = clientSystem->clientSkyParameters(clientId);
clientShip->executeAction([skyParameters](WorldServerThread*, WorldServer* worldServer) {
worldServer->stopFlyingSkyAt(skyParameters);
});
for (auto shipClientId : clientShip->clients()) {
if (auto clientContext = m_clients.get(shipClientId))
clientContext->setOrbitWarpAction(clientSystem->clientWarpAction(clientId));
}
}
return true;
});
}
void UniverseServer::respondToCelestialRequests() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto& p : m_pendingCelestialRequests) {
List<CelestialResponse> responses;
eraseWhere(p.second, [&responses](WorkerPoolPromise<CelestialResponse> const& request) {
if (request.poll()) {
responses.append(request.get());
return true;
}
return false;
});
if (m_clients.contains(p.first))
m_connectionServer->sendPackets(p.first, {make_shared<CelestialResponsePacket>(std::move(responses))});
}
eraseWhere(m_pendingCelestialRequests, [](auto const& p) {
return p.second.empty();
});
}
void UniverseServer::processChat() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
for (auto const& p : take(m_pendingChat)) {
if (auto clientContext = m_clients.get(p.first)) {
for (auto const& chat : p.second) {
auto& message = get<0>(chat);
auto sendMode = get<1>(chat);
auto& data = get<2>(chat);
if (clientContext->remoteAddress())
Logger::info("Chat: <{}> {}", clientContext->playerName(), message);
auto team = m_teamManager->getTeam(clientContext->playerUuid());
if (sendMode == ChatSendMode::Broadcast)
m_chatProcessor->broadcast(p.first, message, std::move(data));
else if (sendMode == ChatSendMode::Party && team.isValid())
m_chatProcessor->message(p.first, MessageContext::Mode::Party, team.value().hex(), message, std::move(data));
else
m_chatProcessor->message(p.first, MessageContext::Mode::Local, printWorldId(clientContext->playerWorldId()), message, std::move(data));
}
}
}
}
void UniverseServer::clearBrokenWorlds() {
RecursiveMutexLocker locker(m_mainLock);
if (Time::monotonicMilliseconds() >= m_clearBrokenWorldsDeadline) {
// Clear out all broken worlds
eraseWhere(m_worlds, [](auto const& p) {
if (!p.second.isValid()) {
Logger::info("UniverseServer: Clearing broken world {}", p.first);
return true;
} else {
return false;
}
});
int clearBrokenWorldsInterval = Root::singleton().assets()->json("/universe_server.config:clearBrokenWorldsInterval").toInt();
m_clearBrokenWorldsDeadline = Time::monotonicMilliseconds() + clearBrokenWorldsInterval;
}
}
void UniverseServer::handleWorldMessages() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
auto it = m_pendingWorldMessages.begin();
while (it != m_pendingWorldMessages.end()) {
auto& worldId = it->first;
if (auto worldResult = triggerWorldCreation(worldId)) {
auto& world = *worldResult;
if (world)
world->passMessages(std::move(it->second));
else for (auto& message : it->second)
message.promise.fail("Error creating world");
it = m_pendingWorldMessages.erase(it);
}
else
++it;
}
}
void UniverseServer::shutdownInactiveWorlds() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
// Shutdown idle and errored worlds.
for (auto const& worldId : m_worlds.keys()) {
if (auto world = getWorld(worldId)) {
if (world->serverErrorOccurred()) {
world->stop();
Logger::error("UniverseServer: World {} has stopped due to an error", worldId);
worldDiedWithError(world->worldId());
} else if (world->noClients()) {
bool anyPendingWarps = false;
for (auto const& p : m_pendingPlayerWarps) {
if (resolveWarpAction(p.second.first, p.first, p.second.second).world == world->worldId()) {
anyPendingWarps = true;
break;
}
}
if (!anyPendingWarps && world->shouldExpire()) {
Logger::info("UniverseServer: Stopping idle world {}", worldId);
world->stop();
}
}
if (world->isJoined()) {
auto kickClients = world->clients();
if (!kickClients.empty()) {
Logger::info("UniverseServer: World {} shutdown, kicking {} players to their own ships", worldId, world->clients().size());
for (auto clientId : world->clients())
clientWarpPlayer(clientId, WarpAlias::OwnShip);
}
if (worldId.is<ClientShipWorldId>()) {
if (auto clientId = getClientForUuid(worldId.get<ClientShipWorldId>()))
m_clients.get(*clientId)->updateShipChunks(world->readChunks());
}
m_worlds.remove(worldId);
// Once a world is shutdown, mark its shutdown time in m_tempWorldIndex
if (auto instanceWorldId = worldId.maybe<InstanceWorldId>()) {
if (m_tempWorldIndex.contains(*instanceWorldId))
m_tempWorldIndex[*instanceWorldId].first = m_universeClock->milliseconds();
}
}
}
}
// Clear out all temporary worlds shut down more than tempWorldDeleteTime time ago.
// Keep around worlds that are currently running or are active in system worlds
Set<InstanceWorldId> systemLocationWorlds;
for (auto p : m_systemWorlds) {
for (auto instanceWorldId : p.second->activeInstanceWorlds()) {
if (m_tempWorldIndex.contains(instanceWorldId))
systemLocationWorlds.add(instanceWorldId);
}
}
eraseWhere(m_tempWorldIndex, [this, systemLocationWorlds](pair<InstanceWorldId, pair<uint64_t, uint64_t>> const& p) {
String storageFile = tempWorldFile(p.first);
if (!m_worlds.contains(WorldId(p.first)) && !systemLocationWorlds.contains(p.first) && m_universeClock->milliseconds() > int64_t(p.second.first + p.second.second)) {
Logger::info("UniverseServer: Expiring temporary world {}", printWorldId(p.first));
if (File::isFile(storageFile))
File::remove(storageFile);
return true;
}
return false;
});
// Clear out empty system worlds
eraseWhere(m_systemWorlds, [](pair<Vec3I, SystemWorldServerThreadPtr> w) {
return w.second->clients().empty();
});
}
void UniverseServer::doTriggeredStorage() {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (Time::monotonicMilliseconds() >= m_storageTriggerDeadline) {
Logger::debug("UniverseServer: periodic sync to disk");
saveSettings();
saveTempWorldIndex();
for (auto const& p : m_clients) {
if (auto shipWorld = getWorld(ClientShipWorldId(p.second->playerUuid())))
p.second->updateShipChunks(shipWorld->readChunks());
auto versioningDatabase = Root::singleton().versioningDatabase();
String clientContextFile = File::relativeTo(m_storageDirectory, strf("{}.clientcontext", p.second->playerUuid().hex()));
VersionedJson::writeFile(versioningDatabase->makeCurrentVersionedJson("ClientContext", p.second->storeServerData()), clientContextFile);
}
int storageTriggerInterval = Root::singleton().assets()->json("/universe_server.config:universeStorageInterval").toInt();
m_storageTriggerDeadline = Time::monotonicMilliseconds() + storageTriggerInterval;
m_celestialDatabase->cleanupAndCommit();
}
}
void UniverseServer::saveSettings() {
RecursiveMutexLocker locker(m_mainLock);
auto versioningDatabase = Root::singleton().versioningDatabase();
auto versionedSettings = versioningDatabase->makeCurrentVersionedJson("UniverseSettings",
m_universeSettings->toJson().set("time", m_universeClock->time()));
VersionedJson::writeFile(versionedSettings, File::relativeTo(m_storageDirectory, "universe.dat"));
}
void UniverseServer::loadSettings() {
RecursiveMutexLocker locker(m_mainLock);
auto loadDefaultSettings = [this]() {
m_universeClock = make_shared<Clock>();
m_universeSettings = make_shared<UniverseSettings>();
};
auto versioningDatabase = Root::singleton().versioningDatabase();
auto storageFile = File::relativeTo(m_storageDirectory, "universe.dat");
if (File::isFile(storageFile)) {
try {
auto settings = versioningDatabase->loadVersionedJson(VersionedJson::readFile(storageFile), "UniverseSettings");
m_universeSettings = make_shared<UniverseSettings>(settings);
m_universeClock = make_shared<Clock>();
m_universeClock->setTime(settings.getDouble("time"));
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load universe settings file, loading defaults {}", outputException(e, false));
File::rename(storageFile, strf("{}.{}.fail", storageFile, Time::millisecondsSinceEpoch()));
loadDefaultSettings();
}
} else {
loadDefaultSettings();
}
m_universeClock->start();
}
Maybe<CelestialCoordinate> UniverseServer::nextStarterWorld() {
RecursiveMutexLocker locker(m_mainLock);
auto assets = Root::singleton().assets();
String defaultWorldCoordinate = assets->json("/universe_server.config:defaultWorldCoordinate").toString();
if (!defaultWorldCoordinate.empty())
return CelestialCoordinate(defaultWorldCoordinate);
if (m_nextRandomizedStarterWorld && m_nextRandomizedStarterWorld->done()) {
CelestialCoordinate nextWorld = m_nextRandomizedStarterWorld->get();
m_nextRandomizedStarterWorld.reset();
return nextWorld;
}
if (!m_nextRandomizedStarterWorld) {
m_nextRandomizedStarterWorld = m_workerPool.addProducer<CelestialCoordinate>([assets, celestialDatabase = m_celestialDatabase]() {
Logger::info("Searching for new randomized starter world");
auto filterWorld = [celestialDatabase](CelestialCoordinate const& coordinate, Json const& filter) {
auto parameters = celestialDatabase->parameters(coordinate);
auto visitableParameters = parameters->visitableParameters();
if (!visitableParameters)
return false;
if (auto biome = filter.optString("terrestrialBiome")) {
auto terrestrialParameters = as<TerrestrialWorldParameters>(visitableParameters);
if (!terrestrialParameters || *biome != terrestrialParameters->primaryBiome)
return false;
}
if (auto size = filter.optString("terrestrialSize")) {
auto terrestrialParameters = as<TerrestrialWorldParameters>(visitableParameters);
if (!terrestrialParameters || *size != terrestrialParameters->sizeName)
return false;
}
if (auto dungeon = filter.optString("floatingDungeon")) {
auto dungeonParameters = as<FloatingDungeonWorldParameters>(visitableParameters);
if (!dungeonParameters || *dungeon != dungeonParameters->primaryDungeon)
return false;
}
return true;
};
auto findParameters = assets->json("/universe_server.config:findStarterWorldParameters");
auto randomWorld = celestialDatabase->findRandomWorld(findParameters.getUInt("tries"), findParameters.getUInt("range"), [&](CelestialCoordinate const& coordinate) {
if (!filterWorld(coordinate, findParameters.get("starterWorld")))
return false;
List<CelestialCoordinate> allChildren;
for (auto const& planet : celestialDatabase->children(coordinate.system())) {
allChildren.append(planet);
for (auto const& satellite : celestialDatabase->children(planet))
allChildren.append(satellite);
}
for (auto const& requiredSystemWorld : findParameters.getArray("requiredSystemWorlds", {})) {
bool worldFound = false;
for (auto const& world : allChildren) {
if (filterWorld(world, requiredSystemWorld)) {
worldFound = true;
break;
}
}
if (!worldFound)
return false;
}
return true;
});
if (randomWorld)
Logger::info("UniverseServer: Found randomized starter world at {}", *randomWorld);
else
Logger::error("UniverseServer: Could not find randomized starter world!");
return randomWorld.value();
});
}
return {};
}
void UniverseServer::loadTempWorldIndex() {
auto versioningDatabase = Root::singleton().versioningDatabase();
auto storageFile = File::relativeTo(m_storageDirectory, "tempworlds.index");
if (File::isFile(storageFile)) {
try {
m_tempWorldIndex.clear();
auto settings = versioningDatabase->loadVersionedJson(VersionedJson::readFile(storageFile), "TempWorldIndex");
for (auto p : settings.iterateObject()) {
WorldId worldId = parseWorldId(p.first);
pair<uint64_t, uint64_t> deleteTime = {p.second.get(0).toUInt(), p.second.get(1).toUInt()};
m_tempWorldIndex.insert(worldId.get<InstanceWorldId>(), deleteTime);
}
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load temp world index file", outputException(e, false));
File::rename(storageFile, strf("{}.{}.fail", storageFile, Time::millisecondsSinceEpoch()));
}
}
// delete temporary instance worlds not found in the index on load
auto tempWorldFiles = m_tempWorldIndex.keys().transformed([this](InstanceWorldId const& worldId) { return tempWorldFile(worldId); });
for (auto p : File::dirList(m_storageDirectory)) {
if (p.second == false && p.first.endsWith(".tempworld")) {
String storageFile = File::relativeTo(m_storageDirectory, p.first);
if (!tempWorldFiles.contains(storageFile)) {
Logger::info("UniverseServer: Removing unindexed temporary world {}", p.first);
File::remove(storageFile);
}
}
}
}
void UniverseServer::saveTempWorldIndex() {
JsonObject worldIndex = JsonObject();
for (auto p : m_tempWorldIndex) {
worldIndex.set(printWorldId(p.first), JsonArray{p.second.first, p.second.second});
}
auto versioningDatabase = Root::singleton().versioningDatabase();
auto versionedJson = versioningDatabase->makeCurrentVersionedJson("TempWorldIndex", worldIndex);
VersionedJson::writeFile(versionedJson, File::relativeTo(m_storageDirectory, "tempworlds.index"));
}
String UniverseServer::tempWorldFile(InstanceWorldId const& worldId) const {
String identifier = worldId.instance;
if (worldId.uuid)
identifier = strf("{}-{}", identifier, worldId.uuid->hex());
if (worldId.level)
identifier = strf("{}-{}", identifier, worldId.level.value());
return File::relativeTo(m_storageDirectory, strf("{}.tempworld", identifier));
}
Maybe<String> UniverseServer::isBannedUser(Maybe<HostAddress> hostAddress, Uuid playerUuid) const {
RecursiveMutexLocker locker(m_mainLock);
auto config = Root::singleton().configuration();
if (hostAddress) {
for (auto const& ban : m_tempBans) {
if (ban.ip) {
if (*ban.ip == *hostAddress) {
return ban.reason;
}
}
}
for (auto banEntry : config->get("bannedIPs").iterateArray()) {
if (HostAddress(banEntry.getString("ip")) == *hostAddress) {
return banEntry.getString("reason");
}
}
}
for (auto const& ban : m_tempBans) {
if (ban.uuid) {
if (*ban.uuid == playerUuid)
return ban.reason;
}
}
for (auto banEntry : config->get("bannedUuids").iterateArray()) {
if (Uuid(banEntry.getString("uuid")) == playerUuid)
return banEntry.getString("reason");
}
return {};
}
void UniverseServer::doTempBan(ConnectionId clientId, String const& reason, pair<bool, bool> banType, int timeout) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId)) {
if (!clientContext->remoteAddress())
return;
auto banExpiry = Time::monotonicMilliseconds() + timeout * 1000; // current time is in millis, conversion factor
Maybe<HostAddress> ip;
if (banType.first)
ip = clientContext->remoteAddress();
Maybe<Uuid> uuid;
if (banType.second)
uuid = clientContext->playerUuid();
m_tempBans.append({banExpiry, reason, ip, uuid});
}
}
void UniverseServer::doPermBan(ConnectionId clientId, String const& reason, pair<bool, bool> banType) {
RecursiveMutexLocker locker(m_mainLock);
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId)) {
if (!clientContext->remoteAddress())
return;
auto config = Root::singleton().configuration();
if (banType.first) {
auto bannedIPs = config->get("bannedIPs").toArray();
bannedIPs.append(JsonObject{
{"ip", toString(*clientContext->remoteAddress())},
{"reason", reason},
});
config->set("bannedIPs", bannedIPs);
}
if (banType.second) {
auto bannedUuids = config->get("bannedUuids").toArray();
bannedUuids.append(JsonObject{
{"uuid", clientContext->playerUuid().hex()},
{"reason", reason},
});
config->set("bannedUuids", bannedUuids);
}
}
}
void UniverseServer::removeTimedBan() {
RecursiveMutexLocker locker(m_mainLock);
auto currentTime = Time::monotonicMilliseconds();
eraseWhere(m_tempBans, [currentTime](TimeoutBan const& b) {
return b.banExpiry <= currentTime;
});
}
void UniverseServer::addCelestialRequests(ConnectionId clientId, List<CelestialRequest> requests) {
RecursiveMutexLocker locker(m_mainLock);
for (auto request : requests) {
m_pendingCelestialRequests[clientId].append(m_workerPool.addProducer<CelestialResponse>([this, request]() {
return m_celestialDatabase->respondToRequest(request);
}));
}
}
void UniverseServer::worldUpdated(WorldServerThread* server) {
for (auto clientId : server->clients()) {
auto packets = server->pullOutgoingPackets(clientId);
m_connectionServer->sendPackets(clientId, std::move(packets));
}
}
void UniverseServer::systemWorldUpdated(SystemWorldServerThread* systemWorldServer) {
for (auto clientId : systemWorldServer->clients()) {
auto packets = systemWorldServer->pullOutgoingPackets(clientId);
m_connectionServer->sendPackets(clientId, std::move(packets));
}
}
void UniverseServer::packetsReceived(UniverseConnectionServer*, ConnectionId clientId, List<PacketPtr> packets) {
ReadLocker clientsLocker(m_clientsLock);
if (auto clientContext = m_clients.value(clientId)) {
clientsLocker.unlock();
for (auto& packet : packets) {
if (auto warpAction = as<PlayerWarpPacket>(packet)) {
clientWarpPlayer(clientId, warpAction->action, warpAction->deploy);
} else if (auto flyShip = as<FlyShipPacket>(packet)) {
clientFlyShip(clientId, flyShip->system, flyShip->location, flyShip->settings);
} else if (auto chatSend = as<ChatSendPacket>(packet)) {
RecursiveMutexLocker locker(m_mainLock);
m_pendingChat[clientId].append(make_tuple(std::move(chatSend->text), chatSend->sendMode, std::move(chatSend->data)));
} else if (auto clientContextUpdatePacket = as<ClientContextUpdatePacket>(packet)) {
clientContext->readUpdate(std::move(clientContextUpdatePacket->updateData));
} else if (auto clientDisconnectPacket = as<ClientDisconnectRequestPacket>(packet)) {
disconnectClient(clientId, String());
} else if (auto celestialRequest = as<CelestialRequestPacket>(packet)) {
addCelestialRequests(clientId, std::move(celestialRequest->requests));
} else if (is<SystemObjectSpawnPacket>(packet)) {
if (auto currentSystem = clientContext->systemWorld())
currentSystem->pushIncomingPacket(clientId, std::move(packet));
} else {
if (auto currentWorld = clientContext->playerWorld())
currentWorld->pushIncomingPackets(clientId, {std::move(packet)});
}
}
}
}
void UniverseServer::acceptConnection(UniverseConnection connection, Maybe<HostAddress> remoteAddress) {
auto& root = Root::singleton();
auto assets = root.assets();
auto configuration = root.configuration();
auto versioningDatabase = root.versioningDatabase();
int clientWaitLimit = assets->json("/universe_server.config:clientWaitLimit").toInt();
String serverAssetsMismatchMessage = assets->json("/universe_server.config:serverAssetsMismatchMessage").toString();
String clientAssetsMismatchMessage = assets->json("/universe_server.config:clientAssetsMismatchMessage").toString();
auto connectionSettings = configuration->get("connectionSettings");
RecursiveMutexLocker mainLocker(m_mainLock, false);
connection.receiveAny(clientWaitLimit);
auto protocolRequest = as<ProtocolRequestPacket>(connection.pullSingle());
if (!protocolRequest) {
Logger::warn("UniverseServer: client connection aborted, expected ProtocolRequestPacket");
return;
}
bool legacyClient = protocolRequest->compressionMode() != PacketCompressionMode::Enabled;
if (legacyClient)
connection.packetSocket().setNetRules(LegacyVersion);
auto protocolResponse = make_shared<ProtocolResponsePacket>();
protocolResponse->setCompressionMode(PacketCompressionMode::Enabled); // Signal that we're OpenStarbound
if (protocolRequest->requestProtocolVersion != StarProtocolVersion) {
Logger::warn("UniverseServer: client connection aborted, unsupported protocol version {}, supported version {}",
protocolRequest->requestProtocolVersion, StarProtocolVersion);
protocolResponse->allowed = false;
connection.pushSingle(protocolResponse);
connection.sendAll(clientWaitLimit);
mainLocker.lock();
m_deadConnections.append({std::move(connection), Time::monotonicMilliseconds()});
return;
}
bool useCompressionStream = false;
protocolResponse->allowed = true;
if (!legacyClient) {
auto compressionName = connectionSettings.getString("compression", "None");
auto compressionMode = NetCompressionModeNames.maybeLeft(compressionName).value(NetCompressionMode::None);
useCompressionStream = compressionMode == NetCompressionMode::Zstd;
protocolResponse->info = JsonObject{
{"compression", NetCompressionModeNames.getRight(compressionMode)},
{"openProtocolVersion", OpenProtocolVersion}
};
}
connection.pushSingle(protocolResponse);
connection.sendAll(clientWaitLimit);
if (auto compressedSocket = as<CompressedPacketSocket>(&connection.packetSocket()))
compressedSocket->setCompressionStreamEnabled(useCompressionStream);
String remoteAddressString = remoteAddress ? toString(*remoteAddress) : "local";
Logger::info("UniverseServer: Awaiting connection info from {} ({} client)", remoteAddressString, legacyClient ? "vanilla" : "custom");
connection.receiveAny(clientWaitLimit);
auto clientConnect = as<ClientConnectPacket>(connection.pullSingle());
if (!clientConnect) {
Logger::warn("UniverseServer: client connection aborted");
connection.pushSingle(make_shared<ConnectFailurePacket>("connect timeout"));
mainLocker.lock();
m_deadConnections.append({std::move(connection), Time::monotonicMilliseconds()});
return;
}
bool administrator = false;
String accountString = !clientConnect->account.empty() ? strf("'{}'", clientConnect->account) : "<anonymous>";
auto connectionFail = [&](String message) {
Logger::warn("UniverseServer: Login attempt failed with account '{}' as player '{}' from address {}, error: {}",
accountString, clientConnect->playerName, remoteAddressString, message);
connection.pushSingle(make_shared<ConnectFailurePacket>(std::move(message)));
mainLocker.lock();
m_deadConnections.append({std::move(connection), Time::monotonicMilliseconds()});
};
if (!remoteAddress) {
administrator = true;
Logger::info("UniverseServer: Logged in player '{}' locally", clientConnect->playerName);
} else {
if (clientConnect->assetsDigest != m_assetsDigest) {
if (!configuration->get("allowAssetsMismatch").toBool()) {
connectionFail(serverAssetsMismatchMessage);
return;
} else if (!clientConnect->allowAssetsMismatch) {
connectionFail(clientAssetsMismatchMessage);
return;
}
}
if (!m_speciesShips.contains(clientConnect->playerSpecies)) {
connectionFail("Unknown player species");
return;
}
if (!clientConnect->account.empty()) {
auto passwordSalt = secureRandomBytes(assets->json("/universe_server.config:passwordSaltLength").toUInt());
Logger::info("UniverseServer: Sending Handshake Challenge");
connection.pushSingle(make_shared<HandshakeChallengePacket>(passwordSalt));
connection.sendAll(clientWaitLimit);
connection.receiveAny(clientWaitLimit);
shared_ptr<HandshakeResponsePacket> handshakeResponsePacket = as<HandshakeResponsePacket>(connection.pullSingle());
if (!handshakeResponsePacket) {
connectionFail("Expected HandshakeResponsePacket.");
return;
}
bool success = false;
if (Json account = configuration->get("serverUsers").get(clientConnect->account, {})) {
administrator = account.getBool("admin", false);
ByteArray passAccountSalt = (account.getString("password") + clientConnect->account).utf8Bytes();
passAccountSalt.append(passwordSalt);
ByteArray passHash = sha256(passAccountSalt);
if (passHash == handshakeResponsePacket->passHash)
success = true;
}
// Give the same message for missing account vs wrong password to
// prevent account detection, overkill given the overall level of
// security but hey, why not.
if (!success) {
connectionFail(strf("No such account '{}' or incorrect password", clientConnect->account));
return;
}
} else {
if (!configuration->get("allowAnonymousConnections").toBool()) {
connectionFail("Anonymous connections disallowed");
return;
}
administrator = configuration->get("anonymousConnectionsAreAdmin").toBool();
}
if (auto reason = isBannedUser(remoteAddress, clientConnect->playerUuid)) {
connectionFail("You are banned: " + *reason);
return;
}
}
String connectionLog = strf("UniverseServer: Logged in account '{}' as player '{}' from address {}",
accountString, clientConnect->playerName, remoteAddressString);
NetCompatibilityRules netRules(legacyClient ? LegacyVersion : 1);
if (Json& info = clientConnect->info) {
if (auto openProtocolVersion = info.optUInt("openProtocolVersion"))
netRules.setVersion(*openProtocolVersion);
if (Json brand = info.get("brand", "custom"))
connectionLog += strf(" ({} client)", brand.toString());
if (info.getBool("legacy", false))
netRules.setVersion(LegacyVersion);
}
connection.packetSocket().setNetRules(netRules);
Logger::log(LogLevel::Info, connectionLog.utf8Ptr());
mainLocker.lock();
WriteLocker clientsLocker(m_clientsLock);
if (auto clashId = getClientForUuid(clientConnect->playerUuid)) {
if (administrator) {
doDisconnection(*clashId, "Duplicate UUID joined and is Administrator so has priority.");
} else {
connectionFail("Duplicate player UUID");
return;
}
}
if (m_clients.size() + 1 > m_maxPlayers && !administrator) {
connectionFail("Max player connections");
return;
}
ConnectionId clientId = m_clients.nextId();
auto clientContext = make_shared<ServerClientContext>(clientId, remoteAddress, netRules, clientConnect->playerUuid,
clientConnect->playerName, clientConnect->playerSpecies, administrator, clientConnect->shipChunks);
m_clients.add(clientId, clientContext);
m_connectionServer->addConnection(clientId, std::move(connection));
clientsLocker.unlock();
clientContext->registerRpcHandlers(m_teamManager->rpcHandlers());
String clientContextFile = File::relativeTo(m_storageDirectory, strf("{}.clientcontext", clientConnect->playerUuid.hex()));
if (File::isFile(clientContextFile)) {
try {
auto contextStore = versioningDatabase->loadVersionedJson(VersionedJson::readFile(clientContextFile), "ClientContext");
clientContext->loadServerData(contextStore);
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load client context file for <User: {}>, ignoring! {}",
clientConnect->playerName, outputException(e, false));
File::rename(clientContextFile, strf("{}.{}.fail", clientContextFile, Time::millisecondsSinceEpoch()));
}
}
// Need to do this after loadServerData because it sets the admin flag
if (!administrator)
clientContext->setAdmin(false);
clientContext->setShipUpgrades(clientConnect->shipUpgrades);
m_chatProcessor->connectClient(clientId, clientConnect->playerName);
m_connectionServer->sendPackets(clientId, {
make_shared<ConnectSuccessPacket>(clientId, m_universeSettings->uuid(), m_celestialDatabase->baseInformation()),
make_shared<UniverseTimeUpdatePacket>(m_universeClock->time()),
make_shared<PausePacket>(*m_pause, GlobalTimescale)
});
setPvp(clientId, false);
Vec3I location = clientContext->shipCoordinate().location();
if (location != Vec3I()) {
auto clientSystem = createSystemWorld(location);
clientSystem->addClient(clientId, clientContext->playerUuid(), clientContext->shipUpgrades().shipSpeed, clientContext->shipLocation());
addCelestialRequests(clientId, {makeLeft(location.vec2()), makeRight(location)});
clientContext->setSystemWorld(clientSystem);
}
Json introInstance = assets->json("/universe_server.config:introInstance");
String speciesIntroInstance = introInstance.getString(clientConnect->playerSpecies, introInstance.getString("default", ""));
if (!speciesIntroInstance.empty() && !clientConnect->introComplete) {
Logger::info("UniverseServer: Spawning player in intro instance {}", speciesIntroInstance);
WarpAction introWarp = WarpToWorld{InstanceWorldId(speciesIntroInstance, clientContext->playerUuid()), {}};
clientWarpPlayer(clientId, introWarp);
} else if (auto reviveWarp = clientContext->playerReviveWarp()) {
// Do not revive players at non-persistent instance worlds or on ship worlds that
// are not their own ship.
bool useReviveWarp = true;
if (reviveWarp.world.is<InstanceWorldId>()) {
String instance = reviveWarp.world.get<InstanceWorldId>().instance;
auto worldConfig = Root::singleton().assets()->json("/instance_worlds.config").opt(instance);
if (!worldConfig || !worldConfig->getBool("persistent", false))
useReviveWarp = false;
}
if (reviveWarp.world.is<ClientShipWorldId>() && reviveWarp.world.get<ClientShipWorldId>() != clientConnect->playerUuid)
useReviveWarp = false;
if (useReviveWarp) {
Logger::info("UniverseServer: Reviving player at {}", reviveWarp.world);
clientWarpPlayer(clientId, reviveWarp);
} else {
Logger::info("UniverseServer: Player revive position is expired, spawning back at own ship");
clientWarpPlayer(clientId, WarpAlias::OwnShip);
}
} else {
Maybe<String> defaultReviveWarp = assets->json("/universe_server.config").optString("defaultReviveWarp");
if (defaultReviveWarp) {
Logger::info("UniverseServer: Spawning player at default warp");
clientWarpPlayer(clientId, parseWarpAction(*defaultReviveWarp));
} else {
Logger::info("UniverseServer: Spawning player at ship");
clientWarpPlayer(clientId, WarpAlias::OwnShip);
}
}
clientFlyShip(clientId, clientContext->shipCoordinate().location(), clientContext->shipLocation());
Logger::info("UniverseServer: Client {} connected", clientContext->descriptiveName());
ReadLocker m_clientsReadLocker(m_clientsLock);
auto players = static_cast<uint16_t>(m_clients.size());
auto clients = m_clients.keys();
m_clientsReadLocker.unlock();
for (auto clientId : clients) {
m_connectionServer->sendPackets(clientId, {
make_shared<ServerInfoPacket>(players, static_cast<uint16_t>(m_maxPlayers))
});
}
for (auto& p : m_scriptContexts)
p.second->invoke("acceptConnection", clientId);
}
WarpToWorld UniverseServer::resolveWarpAction(WarpAction warpAction, ConnectionId clientId, bool deploy) const {
auto clientContext = m_clients.value(clientId);
if (!clientContext)
return {};
WorldId toWorldId;
SpawnTarget spawnTarget;
if (auto toWorld = warpAction.ptr<WarpToWorld>()) {
if (!toWorld->world)
toWorldId = clientContext->playerWorldId();
else
toWorldId = toWorld->world;
spawnTarget = toWorld->target;
} else if (auto toPlayerUuid = warpAction.ptr<WarpToPlayer>()) {
if (auto toClientId = getClientForUuid(*toPlayerUuid)) {
if (auto toClientWorld = m_clients.get(*toClientId)->playerWorld()) {
if (auto toClientPosition = toClientWorld->playerRevivePosition(*toClientId)) {
toWorldId = toClientWorld->worldId();
if (deploy)
spawnTarget.reset();
else
spawnTarget = SpawnTargetPosition(*toClientPosition);
}
}
}
} else if (auto shortcut = warpAction.ptr<WarpAlias>()) {
if (*shortcut == WarpAlias::Return) {
if (auto returnWarp = clientContext->playerReturnWarp()) {
toWorldId = returnWarp.world;
spawnTarget = returnWarp.target;
}
} else if (*shortcut == WarpAlias::OrbitedWorld) {
if (auto warpAction = clientContext->orbitWarpAction()) {
if (auto warpToWorld = warpAction->first.maybe<WarpToWorld>()) {
toWorldId = warpToWorld->world;
spawnTarget = warpToWorld->target;
}
}
} else if (*shortcut == WarpAlias::OwnShip) {
toWorldId = ClientShipWorldId(clientContext->playerUuid());
}
}
return WarpToWorld(toWorldId, spawnTarget);
}
void UniverseServer::doDisconnection(ConnectionId clientId, String const& reason) {
if (auto clientContext = m_clients.value(clientId)) {
m_teamManager->playerDisconnected(clientContext->playerUuid());
// The client should revive at their ship if they are in an un-revivable
// state
WarpToWorld reviveWarp = WarpToWorld(ClientShipWorldId(clientContext->playerUuid()));
if (auto currentWorld = clientContext->playerWorld()) {
auto currentWorldId = currentWorld->worldId();
if (auto playerRevivePosition = currentWorld->playerRevivePosition(clientId))
reviveWarp = WarpToWorld(currentWorldId, SpawnTargetPosition(*playerRevivePosition));
m_connectionServer->sendPackets(clientId, currentWorld->removeClient(clientId));
m_chatProcessor->leaveChannel(clientId, printWorldId(currentWorld->worldId()));
}
clientContext->clearPlayerWorld();
clientContext->setPlayerReviveWarp(reviveWarp);
if (auto systemWorld = clientContext->systemWorld())
systemWorld->removeClient(clientId);
clientContext->clearSystemWorld();
if (m_chatProcessor->hasClient(clientId))
m_chatProcessor->disconnectClient(clientId);
if (m_connectionServer->connectionIsOpen(clientId)) {
// Send the client the last ship update.
if (auto shipWorld = getWorld(ClientShipWorldId(clientContext->playerUuid()))) {
clientContext->updateShipChunks(shipWorld->readChunks());
shipWorld->stop();
}
sendClientContextUpdate(clientContext);
// Then send the disconnect packet.
m_connectionServer->sendPackets(clientId, {make_shared<ServerDisconnectPacket>(reason)});
}
// Write the final client context.
auto versioningDatabase = Root::singleton().versioningDatabase();
String clientContextFile = File::relativeTo(m_storageDirectory, strf("{}.clientcontext", clientContext->playerUuid().hex()));
VersionedJson::writeFile(versioningDatabase->makeCurrentVersionedJson("ClientContext", clientContext->storeServerData()), clientContextFile);
m_clients.remove(clientId);
m_deadConnections.append({m_connectionServer->removeConnection(clientId), Time::monotonicMilliseconds()});
Logger::info("UniverseServer: Client {} disconnected for reason: {}", clientContext->descriptiveName(), reason);
auto players = static_cast<uint16_t>(m_clients.size());
for (auto clientId : m_clients.keys()) {
m_connectionServer->sendPackets(clientId, {
make_shared<ServerInfoPacket>(players, static_cast<uint16_t>(m_maxPlayers))
});
}
for (auto& p : m_scriptContexts)
p.second->invoke("doDisconnection", clientId);
}
}
Maybe<ConnectionId> UniverseServer::getClientForUuid(Uuid const& uuid) const {
for (auto const& p : m_clients) {
if (p.second->playerUuid() == uuid)
return p.second->clientId();
}
return {};
}
WorldServerThreadPtr UniverseServer::getWorld(WorldId const& worldId) {
if (m_worlds.contains(worldId)) {
auto& maybeWorldPromise = m_worlds.get(worldId);
try {
if (!maybeWorldPromise || !maybeWorldPromise->poll())
return {};
return maybeWorldPromise->get();
} catch (std::exception const& e) {
maybeWorldPromise.reset();
Logger::error("UniverseServer: error during world create: {}", outputException(e, true));
worldDiedWithError(worldId);
}
}
return {};
}
WorldServerThreadPtr UniverseServer::createWorld(WorldId const& worldId) {
if (!m_worlds.contains(worldId)) {
if (auto promise = makeWorldPromise(worldId))
m_worlds.add(worldId, promise.take());
else
return {};
}
auto& maybeWorldPromise = m_worlds.get(worldId);
if (!maybeWorldPromise)
return {};
try {
return maybeWorldPromise->get();
} catch (std::exception const& e) {
maybeWorldPromise.reset();
Logger::error("UniverseServer: error during world create: {}", outputException(e, true));
worldDiedWithError(worldId);
return {};
}
}
Maybe<WorldServerThreadPtr> UniverseServer::triggerWorldCreation(WorldId const& worldId) {
if (!m_worlds.contains(worldId)) {
if (auto promise = makeWorldPromise(worldId)) {
m_worlds.add(worldId, promise.take());
return {};
} else {
return WorldServerThreadPtr();
}
} else {
auto& maybeWorldPromise = m_worlds.get(worldId);
try {
// If the promise is reset, this means that the promise threw an
// exception, return nullptr to signify error.
if (!maybeWorldPromise)
return WorldServerThreadPtr();
if (!maybeWorldPromise->poll())
return {};
return maybeWorldPromise->get();
} catch (std::exception const& e) {
maybeWorldPromise.reset();
Logger::error("UniverseServer: error during world create: {}", outputException(e, true));
worldDiedWithError(worldId);
return WorldServerThreadPtr();
}
}
}
Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::makeWorldPromise(WorldId const& worldId) {
if (auto celestialWorld = worldId.ptr<CelestialWorldId>())
return celestialWorldPromise(*celestialWorld);
else if (auto shipWorld = worldId.ptr<ClientShipWorldId>())
return shipWorldPromise(*shipWorld);
else if (auto instanceWorld = worldId.ptr<InstanceWorldId>())
return instanceWorldPromise(*instanceWorld);
else
return {};
}
Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::shipWorldPromise(
ClientShipWorldId const& clientShipWorldId) {
auto clientId = clientForUuid(clientShipWorldId);
if (!clientId)
return {};
auto clientContext = m_clients.get(*clientId);
auto speciesShips = m_speciesShips;
auto celestialDatabase = m_celestialDatabase;
auto universeClock = m_universeClock;
return m_workerPool.addProducer<WorldServerThreadPtr>([this, clientShipWorldId, clientContext, speciesShips, celestialDatabase, universeClock]() {
WorldServerPtr shipWorld;
auto shipChunks = clientContext->shipChunks();
if (!shipChunks.empty()) {
try {
Logger::info("UniverseServer: Loading client ship world {}", clientShipWorldId);
shipWorld = make_shared<WorldServer>(shipChunks);
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load client ship {}, resetting ship to default state! {}",
clientShipWorldId, outputException(e, false));
}
}
if (!shipWorld) {
Logger::info("UniverseServer: Creating new client ship world {}", clientShipWorldId);
shipWorld = make_shared<WorldServer>(Vec2U(2048, 2048), File::ephemeralFile());
auto& species = clientContext->playerSpecies();
auto shipStructure = WorldStructure(speciesShips.get(species).first());
shipStructure = shipWorld->setCentralStructure(shipStructure);
ShipUpgrades currentUpgrades = clientContext->shipUpgrades();
currentUpgrades.apply(Root::singleton().assets()->json("/ships/shipupgrades.config"));
currentUpgrades.apply(shipStructure.configValue("shipUpgrades"));
clientContext->setShipUpgrades(currentUpgrades);
shipWorld->setSpawningEnabled(false);
shipWorld->setProperty("invinciblePlayers", true);
shipWorld->setProperty("ship.level", 0);
shipWorld->setProperty("ship.species", species);
shipWorld->setProperty("ship.fuel", 0);
shipWorld->setProperty("ship.maxFuel", currentUpgrades.maxFuel);
shipWorld->setProperty("ship.crewSize", currentUpgrades.crewSize);
shipWorld->setProperty("ship.fuelEfficiency", currentUpgrades.fuelEfficiency);
shipWorld->setProperty("ship.epoch", Time::timeSinceEpoch());
}
auto shipClock = make_shared<Clock>();
auto shipTime = shipWorld->getProperty("ship.epoch");
if (!shipTime.canConvert(Json::Type::Float)) {
auto now = Time::timeSinceEpoch();
shipWorld->setProperty("ship.epoch", now);
} else {
shipClock->setTime(Time::timeSinceEpoch() - shipTime.toDouble());
}
shipWorld->setUniverseSettings(m_universeSettings);
shipWorld->setReferenceClock(shipClock);
shipClock->start();
if (auto systemWorld = clientContext->systemWorld())
shipWorld->setOrbitalSky(systemWorld->clientSkyParameters(clientContext->clientId()));
else
shipWorld->setOrbitalSky(celestialSkyParameters(clientContext->shipCoordinate()));
shipWorld->initLua(this);
auto shipWorldThread = make_shared<WorldServerThread>(shipWorld, ClientShipWorldId(clientShipWorldId));
shipWorldThread->setPause(m_pause);
clientContext->updateShipChunks(shipWorldThread->readChunks());
shipWorldThread->start();
shipWorldThread->setUpdateAction(bind(&UniverseServer::worldUpdated, this, _1));
return shipWorldThread;
});
}
Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::celestialWorldPromise(CelestialWorldId const& celestialWorldId) {
if (!celestialWorldId)
return {};
auto storageDirectory = m_storageDirectory;
auto celestialDatabase = m_celestialDatabase;
auto universeClock = m_universeClock;
return m_workerPool.addProducer<WorldServerThreadPtr>([this, celestialWorldId, storageDirectory, celestialDatabase, universeClock]() {
WorldServerPtr worldServer;
String storageFile = File::relativeTo(storageDirectory, strf("{}.world", celestialWorldId.filename()));
if (File::isFile(storageFile)) {
try {
Logger::info("UniverseServer: Loading celestial world {}", celestialWorldId);
worldServer = make_shared<WorldServer>(File::open(storageFile, IOMode::ReadWrite));
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load celestial world {}, removing! Cause: {}",
celestialWorldId, outputException(e, false));
File::rename(storageFile, strf("{}.{}.fail", storageFile, Time::millisecondsSinceEpoch()));
}
}
if (!worldServer) {
Logger::info("UniverseServer: Creating celestial world {}", celestialWorldId);
auto worldTemplate = make_shared<WorldTemplate>(celestialWorldId, celestialDatabase);
worldServer = make_shared<WorldServer>(worldTemplate, File::open(storageFile, IOMode::ReadWrite | IOMode::Truncate));
}
worldServer->setUniverseSettings(m_universeSettings);
worldServer->setReferenceClock(universeClock);
worldServer->initLua(this);
auto worldThread = make_shared<WorldServerThread>(worldServer, celestialWorldId);
worldThread->setPause(m_pause);
worldThread->start();
worldThread->setUpdateAction(bind(&UniverseServer::worldUpdated, this, _1));
return worldThread;
});
}
Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::instanceWorldPromise(InstanceWorldId const& instanceWorldId) {
auto storageDirectory = m_storageDirectory;
auto universeClock = m_universeClock;
return m_workerPool.addProducer<WorldServerThreadPtr>([this, storageDirectory, instanceWorldId, universeClock]() {
Json worldConfig = Root::singleton().assets()->json("/instance_worlds.config").get(instanceWorldId.instance);
uint64_t worldSeed;
if (worldConfig.contains("seed"))
worldSeed = worldConfig.getUInt("seed");
else
worldSeed = Random::randu64();
String worldType = worldConfig.getString("type");
VisitableWorldParametersPtr worldParameters;
if (worldType.equalsIgnoreCase("Terrestrial"))
worldParameters = generateTerrestrialWorldParameters(worldConfig.getString("planetType"), worldConfig.getString("planetSize"), worldSeed);
else if (worldType.equalsIgnoreCase("Asteroids"))
worldParameters = generateAsteroidsWorldParameters(worldSeed);
else if (worldType.equalsIgnoreCase("FloatingDungeon"))
worldParameters = generateFloatingDungeonWorldParameters(worldConfig.getString("dungeonWorld"));
else
throw UniverseServerException(strf("Unknown world type: '{}'\n", worldType));
if (instanceWorldId.level)
worldParameters->threatLevel = *instanceWorldId.level;
if (worldConfig.contains("beamUpRule"))
worldParameters->beamUpRule = BeamUpRuleNames.getLeft(worldConfig.getString("beamUpRule"));
worldParameters->disableDeathDrops = worldConfig.getBool("disableDeathDrops", false);
SkyParameters skyParameters = SkyParameters(worldConfig.get("skyParameters", Json()));
auto worldTemplate = make_shared<WorldTemplate>(worldParameters, skyParameters, worldSeed);
Json worldProperties = worldConfig.get("worldProperties", JsonObject{});
bool spawningEnabled = worldConfig.getBool("spawningEnabled", true);
bool persistent = worldConfig.getBool("persistent", false);
bool useUniverseClock = worldConfig.getBool("useUniverseClock", false);
WorldServerPtr worldServer;
bool worldExisted = false;
if (persistent) {
String identifier = instanceWorldId.instance;
if (instanceWorldId.uuid)
identifier = strf("{}-{}", identifier, instanceWorldId.uuid->hex());
if (instanceWorldId.level)
identifier = strf("{}-{}", identifier, instanceWorldId.level.value());
String storageFile = File::relativeTo(storageDirectory, strf("unique-{}.world", identifier));
if (File::isFile(storageFile)) {
try {
Logger::info("UniverseServer: Loading persistent unique instance world {}", instanceWorldId.instance);
worldServer = make_shared<WorldServer>(File::open(storageFile, IOMode::ReadWrite));
worldExisted = true;
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load persistent unique instance world {}, removing! Cause: {}",
instanceWorldId.instance, outputException(e, false));
File::rename(storageFile, strf("{}.{}.fail", storageFile, Time::millisecondsSinceEpoch()));
}
}
if (!worldServer) {
Logger::info("UniverseServer: Creating persistent unique instance world {}", instanceWorldId.instance);
worldServer = make_shared<WorldServer>(worldTemplate, File::open(storageFile, IOMode::ReadWrite | IOMode::Truncate));
}
} else {
String storageFile = tempWorldFile(instanceWorldId);
uint64_t deleteTime = worldConfig.optInt("tempWorldDeleteTime").value(0);
if (File::isFile(storageFile)) {
if (m_tempWorldIndex.contains(instanceWorldId)) {
auto file = File::open(storageFile, IOMode::ReadWrite);
if (file->size() > 0) {
Logger::info("UniverseServer: Loading temporary instance world {} from storage", instanceWorldId);
try {
worldServer = make_shared<WorldServer>(file);
worldExisted = true;
} catch (std::exception const& e) {
Logger::error("UniverseServer: Could not load temporary instance world '{}', re-creating cause: {}",
instanceWorldId, outputException(e, false));
}
}
} else {
File::remove(storageFile);
}
}
if (!worldServer) {
Logger::info("UniverseServer: Creating temporary instance world '{}' with expiry time {}", instanceWorldId, deleteTime);
worldServer = make_shared<WorldServer>(worldTemplate, File::open(storageFile, IOMode::ReadWrite));
m_tempWorldIndex.set(instanceWorldId, pair<uint64_t, uint64_t>(m_universeClock->milliseconds(), deleteTime));
}
}
worldServer->setUniverseSettings(m_universeSettings);
for (auto const& p : worldProperties.iterateObject())
worldServer->setProperty(p.first, p.second);
worldServer->setProperty("ephemeral", !persistent);
worldServer->setSpawningEnabled(spawningEnabled);
if (useUniverseClock)
worldServer->setReferenceClock(universeClock);
if (!worldExisted) {
for (auto flagAction : m_universeSettings->currentFlagActionsForInstanceWorld(instanceWorldId.instance)) {
if (flagAction.is<PlaceDungeonFlagAction>()) {
auto placeDungeonAction = flagAction.get<PlaceDungeonFlagAction>();
worldServer->placeDungeon(placeDungeonAction.dungeonId, placeDungeonAction.targetPosition, 0);
}
}
}
worldServer->initLua(this);
auto worldThread = make_shared<WorldServerThread>(worldServer, instanceWorldId);
worldThread->setPause(m_pause);
worldThread->start();
worldThread->setUpdateAction(bind(&UniverseServer::worldUpdated, this, _1));
return worldThread;
});
}
SystemWorldServerThreadPtr UniverseServer::createSystemWorld(Vec3I const& location) {
if (!m_systemWorlds.contains(location)) {
SystemWorldServerPtr systemWorld;
String storageFile = File::relativeTo(m_storageDirectory, strf("{}_{}_{}.system", location[0], location[1], location[2]));
bool loadedFromStorage = false;
if (File::isFile(storageFile)) {
Logger::info("UniverseServer: Loading system world {} from disk storage", location);
try {
auto versioningDatabase = Root::singleton().versioningDatabase();
VersionedJson versionedStore = VersionedJson::readFile(storageFile);
Json store = versioningDatabase->loadVersionedJson(versionedStore, "System");
systemWorld = make_shared<SystemWorldServer>(store, m_universeClock, m_celestialDatabase);
loadedFromStorage = true;
} catch (std::exception const& e) {
Logger::error("UniverseServer: Failed to load system {} from disk storage, re-creating. Cause: {}", location, outputException(e, false));
File::rename(storageFile, strf("{}.{}.fail", storageFile, Time::millisecondsSinceEpoch()));
loadedFromStorage = false;
}
}
if (!loadedFromStorage) {
Logger::info("UniverseServer: Creating new system world at location {}", location);
systemWorld = make_shared<SystemWorldServer>(location, m_universeClock, m_celestialDatabase);
}
auto systemThread = make_shared<SystemWorldServerThread>(location, systemWorld, storageFile);
systemThread->setUpdateAction(bind(&UniverseServer::systemWorldUpdated, this, _1));
systemThread->start();
m_systemWorlds.set(location, systemThread);
}
return m_systemWorlds.get(location);
}
bool UniverseServer::instanceWorldStoredOrActive(InstanceWorldId const& worldId) const {
String storageFile = File::relativeTo(m_storageDirectory, strf("unique-{}.world", worldId.instance));
return m_worlds.value(worldId).isValid() || m_tempWorldIndex.contains(worldId) || File::isFile(storageFile);
}
void UniverseServer::worldDiedWithError(WorldId world) {
if (world.is<ClientShipWorldId>()) {
if (auto clientId = getClientForUuid(world.get<ClientShipWorldId>()))
m_pendingDisconnections.add(*clientId, "Client ship world has errored");
}
}
SkyParameters UniverseServer::celestialSkyParameters(CelestialCoordinate const& coordinate) const {
if (m_celestialDatabase->coordinateValid(coordinate))
return SkyParameters(coordinate, m_celestialDatabase);
return SkyParameters();
}
void UniverseServer::startLuaScripts() {
auto assets = Root::singleton().assets();
auto universeConfig = assets->json("/universe_server.config");
m_luaRoot = make_shared<LuaRoot>();
m_luaRoot->tuneAutoGarbageCollection(universeConfig.getFloat("luaGcPause"), universeConfig.getFloat("luaGcStepMultiplier"));
for (auto& p : universeConfig.getObject("scriptContexts")) {
auto scriptComponent = make_shared<ScriptComponent>();
scriptComponent->setLuaRoot(m_luaRoot);
scriptComponent->addCallbacks("universe", LuaBindings::makeUniverseServerCallbacks(this));
scriptComponent->setScripts(jsonToStringList(p.second.toArray()));
m_scriptContexts.set(p.first, scriptComponent);
scriptComponent->init();
}
}
void UniverseServer::updateLua() {
for (auto& p : m_scriptContexts)
p.second->update();
}
void UniverseServer::stopLua() {
for (auto& p : m_scriptContexts)
p.second->uninit();
m_scriptContexts.clear();
}
}
|