-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchMinecraftServer.java
647 lines (580 loc) · 19.9 KB
/
PatchMinecraftServer.java
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
package me.nallar.patched.server;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.RelaunchClassLoader;
import cpw.mods.fml.relauncher.Side;
import javassist.is.faulty.Timings;
import me.nallar.exception.ConcurrencyError;
import me.nallar.insecurity.InsecurityManager;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.DeadLockDetector;
import me.nallar.tickthreading.minecraft.ThreadManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.EnvironmentInfo;
import me.nallar.tickthreading.util.FakeServerThread;
import me.nallar.tickthreading.util.PatchUtil;
import me.nallar.unsafe.UnsafeUtil;
import net.minecraft.crash.CrashReport;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet4UpdateTime;
import net.minecraft.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ReportedException;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
public abstract class PatchMinecraftServer extends MinecraftServer {
private ThreadManager threadManager;
private static float tickTime = 0;
private static float networkTickTime = 0;
private AtomicInteger currentWorld;
private Integer[] dimensionIdsToTick;
private Runnable tickRunnable;
@SuppressWarnings ("FieldCanBeLocal") // Used by Spigot.
private static int currentTick;
private static int TARGET_TPS;
private static int TARGET_TICK_TIME;
private static int NETWORK_TPS;
private static int NETWORK_TICK_TIME;
private static double currentTPS = 0;
private static double networkTPS = 0;
private Map<Integer, Integer> exceptionCount;
private boolean tickNetworkInMainThread;
private Map<String, long[]> worldTickLengths;
@Declare
public List<WorldServer> worlds_;
public static java.util.ArrayList playersToCheckWorld = new ArrayList();
@Declare
public final java.util.concurrent.atomic.AtomicInteger currentlySaving_ = null;
@Declare
public static java.util.Set<net.minecraftforge.common.Configuration> toSaveConfigurationSet_;
public void construct() {
currentlySaving = new AtomicInteger();
tickNetworkInMainThread = true;
exceptionCount = new HashMap<Integer, Integer>();
worldTickLengths = new ConcurrentHashMap<String, long[]>();
}
public static void staticConstruct() {
setTargetTPS(20);
setNetworkTPS(40);
}
public PatchMinecraftServer(File par1File) {
super(par1File);
}
@Declare
public static void addPlayerToCheckWorld(net.minecraft.entity.player.EntityPlayerMP entityPlayerMP) {
ArrayList<EntityPlayerMP> playersToCheckWorld_ = playersToCheckWorld;
synchronized (playersToCheckWorld_) {
playersToCheckWorld_.add(entityPlayerMP);
}
}
@Override
public void initiateShutdown() {
if (!serverRunning) {
return;
}
SecurityManager securityManager = System.getSecurityManager();
if (securityManager != null) {
securityManager.checkExit(1);
}
this.serverRunning = false;
}
@Override
@Declare
public long[] getTickTimes(WorldServer w) {
return worldTickLengths.get(w.getName());
}
@Declare
public static void setTargetTPS(int targetTPS) {
assert targetTPS > 0 : "Target TPS must be greater than 0";
TARGET_TPS = targetTPS;
TARGET_TICK_TIME = 1000000000 / TARGET_TPS;
}
@Declare
public static void setNetworkTPS(int targetTPS) {
assert targetTPS > 0 : "Target TPS must be greater than 0";
NETWORK_TPS = targetTPS;
NETWORK_TICK_TIME = 1000000000 / NETWORK_TPS;
}
@Override
@Declare
public int getId(WorldServer world) {
if (worlds == null) {
for (int i = 0; i < worldServers.length; i++) {
if (worldServers[i] == world) {
return i;
}
}
} else {
for (int i = 0; i < worlds.size(); i++) {
if (worlds.get(i) == world) {
return i;
}
}
}
return Integer.MIN_VALUE;
}
@Override
public void run() {
//noinspection ThrowableInstanceNeverThrown
new ConcurrencyError("Just loading some exception classes.");
toSaveConfigurationSet = new HashSet<Configuration>();
FMLLog.info("Loaded " + RelaunchClassLoader.patchedClasses + " patched classes, cl: " + this.getClass().getClassLoader());
boolean isCrash = false;
try {
try {
InsecurityManager.init();
PatchUtil.writePatchRunners();
} catch (Throwable t) {
FMLLog.log(Level.SEVERE, t, "Failed to set up Security Manager. This is probably not a huge problem - but it could indicate classloading issues.");
}
if (this.startServer()) {
FMLLog.fine("calling handleServerStarted()");
FMLCommonHandler.instance().handleServerStarted();
FMLCommonHandler.instance().onWorldLoadTick(worlds == null ? worldServers : worlds.toArray(new WorldServer[worlds.size()]));
Set<Configuration> toSaveConfigurationSet = MinecraftServer.toSaveConfigurationSet;
MinecraftServer.toSaveConfigurationSet = null;
for (Configuration configuration : toSaveConfigurationSet) {
configuration.save();
}
// This block is derived from Spigot code,
// LGPL
this.serverIsRunning = true;
if (TickThreading.instance.concurrentNetworkTicks) {
tickNetworkInMainThread = false;
new FakeServerThread(new NetworkTickRunnable(this), "Network Tick", false).start();
}
long lastTick = 0L;
double currentMaxTPS = 0;
while (this.serverRunning) {
long curTime = System.nanoTime();
long wait = TARGET_TICK_TIME - (curTime - lastTick);
if (wait > 0 && currentMaxTPS > TARGET_TPS) {
Thread.sleep(wait / 1000000);
continue;
}
lastTick = curTime;
currentTick = ++tickCounter;
try {
this.tick();
} catch (Exception e) {
Log.severe("Exception in main tick loop", e);
}
currentMaxTPS = TARGET_TICK_TIME * TARGET_TPS / tickTime;
currentTPS = currentMaxTPS > TARGET_TPS ? TARGET_TPS : currentMaxTPS;
}
try {
FMLCommonHandler.instance().handleServerStopping();
} catch (Throwable t) {
Log.severe("Exception occurred while stopping the server", t);
}
} else {
FMLLog.severe("startServer() failed.");
this.finalTick(null);
}
} catch (Throwable throwable) {
isCrash = true;
try {
if (serverRunning && serverIsRunning) {
DeadLockDetector.sendChatSafely("The server has crashed due to an unexpected exception during the main tick loop: " + throwable.getClass().getSimpleName());
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
} catch (Throwable ignored) {
}
FMLLog.log(Level.SEVERE, throwable, "Encountered an unexpected exception" + throwable.getClass().getSimpleName());
CrashReport crashReport;
if (throwable instanceof ReportedException) {
crashReport = this.addServerInfoToCrashReport(((ReportedException) throwable).getCrashReport());
} else {
crashReport = this.addServerInfoToCrashReport(new CrashReport("Exception in server tick loop", throwable));
}
File var3 = new File(new File(this.getDataDirectory(), "crash-reports"), "crash-" + (new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss")).format(new Date()) + "-server.txt");
if (crashReport.saveToFile(var3)) {
logger.severe("This crash report has been saved to: " + var3.getAbsolutePath());
} else {
logger.severe("We were unable to save this crash report to disk.");
}
if (!TickThreading.instance.exitOnDeadlock) {
try {
Thread.sleep(100L);
} catch (InterruptedException ignored) {
}
this.saveEverything();
this.finalTick(crashReport);
}
} finally {
Log.info("Stopping the server. serverRunning: " + serverRunning + ", serverIsRunning: " + serverIsRunning + ", is crash: " + isCrash);
try {
this.stopServer();
this.serverStopped = true;
} catch (Throwable throwable) {
FMLLog.log(Level.SEVERE, throwable, "Exception while attempting to stop the server");
} finally {
try {
DeadLockDetector.checkForLeakedThreadManagers();
} finally {
this.systemExitNow();
}
}
}
}
@Override
public String getServerModName() {
return "tickthreading,mcpc,spigot,craftbukkit,forge,fml";
}
@Override
public void tick() {
long startTime = System.nanoTime();
EntityItem.resetLast();
TickThreading.recentSpawnedItems = 0;
DeadLockDetector.tick(startTime);
Timings.tick();
EntityTickProfiler.ENTITY_TICK_PROFILER.tick();
if (this.startProfiling) {
this.startProfiling = false;
theProfiler.profilingEnabled = true;
theProfiler.clearProfiling();
}
theProfiler.startSection("root");
theProfiler.startSection("forgePreServerTick");
FMLCommonHandler.instance().rescheduleTicks(Side.SERVER);
AxisAlignedBB.getAABBPool().cleanPool();
FMLCommonHandler.instance().onPreServerTick();
theProfiler.endSection();
this.updateTimeLightAndEntities();
int ticks = this.tickCounter;
if (ticks % TickThreading.instance.saveInterval == 0) {
theProfiler.startSection("save");
this.serverConfigManager.saveAllPlayerData();
theProfiler.endSection();
}
theProfiler.startSection("tallying");
this.sentPacketCountArray[ticks % 100] = Packet.sentID - this.lastSentPacketID;
this.lastSentPacketID = Packet.sentID;
this.sentPacketSizeArray[ticks % 100] = Packet.sentSize - this.lastSentPacketSize;
this.lastSentPacketSize = Packet.sentSize;
this.receivedPacketCountArray[ticks % 100] = Packet.receivedID - this.lastReceivedID;
this.lastReceivedID = Packet.receivedID;
this.receivedPacketSizeArray[ticks % 100] = Packet.receivedSize - this.lastReceivedSize;
this.lastReceivedSize = Packet.receivedSize;
theProfiler.endStartSection("snooper");
if (!this.usageSnooper.isSnooperRunning() && ticks > 100) {
this.usageSnooper.startSnooper();
}
if (ticks % 6000 == 0) {
this.usageSnooper.addMemoryStatsToSnooper();
}
if (ticks % 1200 == 0) {
EnvironmentInfo.checkOpenFileHandles();
}
theProfiler.endStartSection("forgePostServerTick");
FMLCommonHandler.instance().onPostServerTick();
ArrayList<EntityPlayerMP> playersToCheckWorld_ = playersToCheckWorld;
if (!playersToCheckWorld_.isEmpty()) {
synchronized (playersToCheckWorld_) {
for (EntityPlayerMP entityPlayerMP : playersToCheckWorld_) {
World world = entityPlayerMP.worldObj;
List<Entity> entityList = world.loadedEntityList;
synchronized (entityList) {
// No contains check, handled by TickManager, this list is an instance of LoadedEntityList.
entityList.add(entityPlayerMP);
}
if (!world.playerEntities.contains(entityPlayerMP)) {
world.playerEntities.add(entityPlayerMP);
}
}
playersToCheckWorld_.clear();
}
}
theProfiler.endSection();
theProfiler.endSection();
tickTime = tickTime * 0.98f + ((this.tickTimeArray[ticks % 100] = System.nanoTime() - startTime) * 0.02f);
}
@Override
public void updateTimeLightAndEntities() {
final Profiler profiler = theProfiler;
profiler.startSection("levels");
int var1;
spigotTLETick();
Integer[] dimensionIdsToTick = this.dimensionIdsToTick = DimensionManager.getIDs();
if (threadManager == null) {
threadManager = new ThreadManager(8, "World Tick");
currentWorld = new AtomicInteger(0);
tickRunnable = new TickRunnable(this);
}
currentWorld.set(0);
boolean concurrentTicking = tickCounter >= 100 && !profiler.profilingEnabled;
if (concurrentTicking) {
int count = threadManager.size();
if (count < dimensionIdsToTick.length) {
count = dimensionIdsToTick.length;
}
for (int i = 0; i < count; i++) {
threadManager.run(tickRunnable);
}
} else {
// Gregtech leaks the first world which is ticked. If we tick overworld first, no leak. Only applies to first tick on server start.
Integer[] dimensionIdsToTickOrdered = new Integer[dimensionIdsToTick.length];
int i = 0;
for (int d : dimensionIdsToTick) {
if (d == 0) {
dimensionIdsToTickOrdered[i++] = 0;
break;
}
}
for (int d : dimensionIdsToTick) {
if (d != 0) {
dimensionIdsToTickOrdered[i++] = d;
}
}
this.dimensionIdsToTick = dimensionIdsToTickOrdered;
if (tickCounter == 1) {
Log.checkWorlds();
}
doWorldTick();
}
TickThreading.instance.waitForEntityTicks();
profiler.endStartSection("players");
this.serverConfigManager.sendPlayerInfoToAllPlayers();
profiler.endStartSection("tickables");
for (var1 = 0; var1 < this.tickables.size(); ++var1) {
((IUpdatePlayerListBox) this.tickables.get(var1)).update();
}
if (concurrentTicking) {
threadManager.waitForCompletion();
}
if (tickNetworkInMainThread) {
profiler.endStartSection("connection");
this.getNetworkThread().networkTick();
}
profiler.endStartSection("dim_unloading");
DimensionManager.unloadWorlds(worldTickTimes);
profiler.endSection();
}
private void spigotTLETick() {
// Replaced in patcher
}
@Declare
public static float getTickTime() {
return tickTime;
}
@Declare
public static double getTPS() {
return currentTPS;
}
@Declare
public static double getTargetTickTime() {
return TARGET_TICK_TIME;
}
@Declare
public static double getTargetTPS() {
return TARGET_TPS;
}
@Declare
public static float getNetworkTickTime() {
return networkTickTime;
}
@Declare
public static double getNetworkTPS() {
return networkTPS;
}
@Declare
public static double getNetworkTargetTickTime() {
return NETWORK_TICK_TIME;
}
@Declare
public static double getNetworkTargetTPS() {
return NETWORK_TPS;
}
@Override
@Declare
public void doWorldTick() {
final Profiler profiler = this.theProfiler;
int i;
while ((i = currentWorld.getAndIncrement()) < dimensionIdsToTick.length) {
int id = dimensionIdsToTick[i];
long var2 = System.nanoTime();
WorldServer world = DimensionManager.getWorld(id);
String name = world.getName();
if (!world.loadEventFired) {
Log.severe("Not sent WorldEvent.Load for loaded world " + name + ", sending it now.");
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(world));
}
if (world.getDimension() != id) {
Log.severe("World " + world.getName() + " exists in DimensionManager with an apparently incorrect dimension ID of " + id);
continue;
}
try {
profiler.startSection(world.getWorldInfo().getWorldName());
profiler.startSection("pools");
world.getWorldVec3Pool().clear();
profiler.endSection();
if (world.tickCount % 30 == 0) {
profiler.startSection("timeSync");
long totalTime = world.getTotalWorldTime();
for (EntityPlayerMP entityPlayerMP : (Iterable<EntityPlayerMP>) world.playerEntities) {
entityPlayerMP.playerNetServerHandler.sendPacketToPlayer(new Packet4UpdateTime(totalTime, entityPlayerMP.getPlayerTime()));
}
profiler.endSection();
}
profiler.startSection("forgeTick");
FMLCommonHandler.instance().onPreWorldTick(world);
profiler.endStartSection("worldTick");
world.tick();
profiler.endStartSection("entityTick");
world.updateEntities();
profiler.endStartSection("postForgeTick");
FMLCommonHandler.instance().onPostWorldTick(world);
profiler.endSection();
profiler.startSection("tracker");
world.getEntityTracker().updateTrackedEntities();
profiler.endSection();
if (world.tickCount % 14 == 0) {
exceptionCount.put(id, 0);
}
if (world.tickCount % TickThreading.instance.saveInterval == 0) {
theProfiler.startSection("save");
try {
currentlySaving.getAndIncrement();
long st = 0;
boolean enabled = Timings.enabled;
if (enabled) {
st = System.nanoTime();
}
if (world.saveTickCount++ % 20 == 0) {
world.saveAllChunks(false, null);
} else {
if (world.theChunkProviderServer.canSave()) {
world.theChunkProviderServer.saveChunks(false, null);
}
}
if (enabled) {
Timings.record("World/save", System.nanoTime() - st);
}
} catch (MinecraftException e) {
throw UnsafeUtil.throwIgnoreChecked(e);
} finally {
currentlySaving.getAndDecrement();
}
theProfiler.endSection();
}
profiler.endSection();
long[] tickTimes = worldTickLengths.get(name);
if (tickTimes == null) {
tickTimes = new long[100];
worldTickLengths.put(name, tickTimes);
worldTickTimes.put(id, tickTimes);
}
tickTimes[this.tickCounter % 100] = System.nanoTime() - var2;
} catch (Throwable t) {
Log.severe("Exception ticking world " + Log.name(world), t);
Integer c = exceptionCount.get(id);
if (c == null) {
c = 0;
}
c++;
if (c >= 10) {
DeadLockDetector.sendChatSafely("The world " + Log.name(world) + " has become unstable, and the server will now stop.");
Log.severe(Log.name(world) + " has become unstable, stopping.");
this.initiateShutdown();
}
exceptionCount.put(id, c);
}
}
AxisAlignedBB.getAABBPool().cleanPool();
}
@Override
@Declare
public void doNetworkTicks() {
long lastTime = 1;
long lastTick = 0L;
while (serverRunning) {
long curTime = System.nanoTime();
long time = curTime - lastTick;
long wait = NETWORK_TICK_TIME - time;
if (wait > 0) {
try {
Thread.sleep(wait / 1000000);
} catch (InterruptedException ignored) {
}
continue;
}
networkTickTime = (networkTickTime * 127 + lastTime) / 128;
networkTPS = (networkTPS * 0.975) + (1E9 / time * 0.025);
lastTick = curTime;
lastTime = System.nanoTime();
this.getNetworkThread().networkTick();
lastTime = System.nanoTime() - lastTime;
}
}
@Override
@Declare
public void saveEverything() {
if (this.isServerRunning() && currentlySaving.get() == 0) {
currentlySaving.getAndIncrement();
try {
this.serverConfigManager.saveAllPlayerData();
this.saveAllWorlds(false);
if (worlds == null) {
for (WorldServer world : this.worldServers) {
world.flush();
}
} else {
for (WorldServer world : worlds) {
world.flush();
}
}
} finally {
currentlySaving.getAndDecrement();
}
} else {
Log.severe("Server is already saving or crashed while saving - not attempting to save.");
}
}
public static class TickRunnable implements Runnable {
final MinecraftServer minecraftServer;
public TickRunnable(MinecraftServer minecraftServer) {
this.minecraftServer = minecraftServer;
}
@Override
public void run() {
minecraftServer.doWorldTick();
}
}
public static class NetworkTickRunnable implements Runnable {
private final MinecraftServer minecraftServer;
public NetworkTickRunnable(MinecraftServer minecraftServer) {
this.minecraftServer = minecraftServer;
}
@Override
public void run() {
minecraftServer.doNetworkTicks();
}
}
}