-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TickThreading.java
199 lines (187 loc) · 10.2 KB
/
TickThreading.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
package me.nallar.tickthreading.minecraft;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkMod;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.commands.TPSCommand;
import me.nallar.tickthreading.minecraft.commands.TicksCommand;
import me.nallar.tickthreading.minecraft.entitylist.EntityList;
import me.nallar.tickthreading.minecraft.entitylist.LoadedEntityList;
import me.nallar.tickthreading.minecraft.entitylist.LoadedTileEntityList;
import me.nallar.tickthreading.patcher.PatchManager;
import me.nallar.tickthreading.util.FieldUtil;
import me.nallar.tickthreading.util.LocationUtil;
import me.nallar.tickthreading.util.PatchUtil;
import net.minecraft.command.ServerCommandManager;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.Property;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.WorldEvent;
@SuppressWarnings ("WeakerAccess")
@Mod (modid = "TickThreading", name = "TickThreading", version = "1.0")
@NetworkMod (clientSideRequired = false, serverSideRequired = false)
public class TickThreading {
private static final int loadedEntityFieldIndex = 0;
private static final int loadedTileEntityFieldIndex = 2;
public final boolean enabled;
private int tickThreads = 0;
private boolean enableEntityTickThreading = true;
private boolean enableTileEntityTickThreading = true;
private int regionSize = 16;
private boolean variableTickRate = true;
private boolean requirePatched = true;
public boolean exitOnDeadlock = false;
final Map<World, TickManager> managers = new WeakHashMap<World, TickManager>();
private DeadLockDetector deadLockDetector = null;
public static TickThreading instance;
public boolean enableChunkTickThreading = true;
public boolean enableWorldTickThreading = true;
public boolean requireOpForTicksCommand = true;
public int saveInterval = 900;
public TickThreading() {
Log.LOGGER.getLevel(); // Force log class to load
if (requirePatched && PatchManager.shouldPatch(LocationUtil.getJarLocations())) {
enabled = false;
try {
PatchUtil.writePatchRunners();
} catch (IOException e) {
Log.severe("Failed to write patch runners", e);
}
} else {
enabled = true;
}
instance = this;
}
@Mod.Init
public void init(FMLInitializationEvent event) {
if (enabled) {
MinecraftForge.EVENT_BUS.register(this);
}
}
@Mod.PreInit
public void preInit(FMLPreInitializationEvent event) {
Log.severe("TickThreading is installed on this server!"
+ "\nIf anything breaks, check if it is still broken without TickThreading"
+ "\nWe don't want to annoy mod devs with issue reports caused by TickThreading."
+ "\nIf it's only broken with TickThreading, report it at"
+ "http://github.com/nallar/TickThreading"
+ "\n\nThe FML invalid fingerprint event has been disabled as some mods break if it is fired"
+ "\nEG, forestry as an attempt to stop tekkit from including it.");
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
Property tickThreadsProperty = config.get(Configuration.CATEGORY_GENERAL, "tickThreads", tickThreads);
tickThreadsProperty.comment = "number of threads to use to tick. 0 = automatic";
Property enableEntityTickThreadingProperty = config.get(Configuration.CATEGORY_GENERAL, "enableEntityTickThreading", enableEntityTickThreading);
enableEntityTickThreadingProperty.comment = "Whether entity ticks should be threaded";
Property enableTileEntityTickThreadingProperty = config.get(Configuration.CATEGORY_GENERAL, "enableTileEntityTickThreading", enableTileEntityTickThreading);
enableTileEntityTickThreadingProperty.comment = "Whether tile entity ticks should be threaded";
Property enableChunkTickThreadingProperty = config.get(Configuration.CATEGORY_GENERAL, "enableChunkTickThreading", enableChunkTickThreading);
enableChunkTickThreadingProperty.comment = "Whether chunk ticks should be threaded";
Property enableWorldTickThreadingProperty = config.get(Configuration.CATEGORY_GENERAL, "enableWorldTickThreading", enableWorldTickThreading);
enableWorldTickThreadingProperty.comment = "Whether world ticks should be threaded";
Property regionSizeProperty = config.get(Configuration.CATEGORY_GENERAL, "regionSize", regionSize);
regionSizeProperty.comment = "width/length of tick regions, specified in blocks.";
Property variableTickRateProperty = config.get(Configuration.CATEGORY_GENERAL, "variableRegionTickRate", variableTickRate);
variableTickRateProperty.comment = "Allows tick rate to vary per region so that each region uses at most 50ms on average per tick.";
Property ticksCommandName = config.get(Configuration.CATEGORY_GENERAL, "ticksCommandName", TicksCommand.name);
ticksCommandName.comment = "Name of the command to be used for performance stats. Defaults to ticks.";
Property tpsCommandName = config.get(Configuration.CATEGORY_GENERAL, "tpsCommandName", TPSCommand.name);
tpsCommandName.comment = "Name of the command to be used for TPS reports.";
Property requirePatchedProperty = config.get(Configuration.CATEGORY_GENERAL, "requirePatched", requirePatched);
requirePatchedProperty.comment = "If the server must be patched to run with TickThreading";
Property exitOnDeadlockProperty = config.get(Configuration.CATEGORY_GENERAL, "exitOnDeadlock", exitOnDeadlock);
exitOnDeadlockProperty.comment = "If the server should shut down when a deadlock is detected";
Property requireOpForTicksCommandProperty = config.get(Configuration.CATEGORY_GENERAL, "exitOnDeadlock", requireOpForTicksCommand);
requireOpForTicksCommandProperty.comment = "If a player must be opped to use /ticks";
Property saveIntervalProperty = config.get(Configuration.CATEGORY_GENERAL, "saveInterval", saveInterval);
saveIntervalProperty.comment = "Time between auto-saves, in ticks.";
config.save();
TicksCommand.name = ticksCommandName.value;
TPSCommand.name = tpsCommandName.value;
tickThreads = tickThreadsProperty.getInt(tickThreads);
regionSize = regionSizeProperty.getInt(regionSize);
saveInterval = saveIntervalProperty.getInt(saveInterval);
enableEntityTickThreading = enableEntityTickThreadingProperty.getBoolean(enableEntityTickThreading);
enableTileEntityTickThreading = enableTileEntityTickThreadingProperty.getBoolean(enableTileEntityTickThreading);
variableTickRate = variableTickRateProperty.getBoolean(variableTickRate);
requirePatched = requirePatchedProperty.getBoolean(requirePatched);
exitOnDeadlock = exitOnDeadlockProperty.getBoolean(exitOnDeadlock);
enableChunkTickThreading = enableChunkTickThreadingProperty.getBoolean(enableChunkTickThreading);
enableWorldTickThreading = enableWorldTickThreadingProperty.getBoolean(enableWorldTickThreading);
requireOpForTicksCommand = requireOpForTicksCommandProperty.getBoolean(requireOpForTicksCommand);
}
@Mod.ServerStarting
public void serverStarting(FMLServerStartingEvent event) {
if (enabled) {
ServerCommandManager serverCommandManager = (ServerCommandManager) event.getServer().getCommandManager();
serverCommandManager.registerCommand(new TicksCommand());
serverCommandManager.registerCommand(new TPSCommand());
} else {
Log.severe("TickThreading is disabled, because your server has not been patched!" +
"\nTo patch your server, simply run the PATCHME.bat/sh file in your server directory" +
"\nAlternatively, you can try to run without patching, just edit the config. Probably won't end well.");
}
}
@ForgeSubscribe
public void onWorldLoad(WorldEvent.Load event) {
TickManager manager = new TickManager(event.world, regionSize, tickThreads);
manager.setVariableTickRate(variableTickRate);
try {
if (enableTileEntityTickThreading) {
Field loadedTileEntityField = FieldUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
new LoadedTileEntityList<TileEntity>(event.world, loadedTileEntityField, manager);
}
if (enableEntityTickThreading) {
Field loadedEntityField = FieldUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
new LoadedEntityList<TileEntity>(event.world, loadedEntityField, manager);
}
Log.info("Threading initialised for world " + Log.name(event.world));
managers.put(event.world, manager);
} catch (Exception e) {
Log.severe("Failed to initialise threading for world " + Log.name(event.world), e);
}
if (deadLockDetector == null) {
deadLockDetector = new DeadLockDetector(managers);
}
}
@ForgeSubscribe
public void onWorldUnload(WorldEvent.Unload event) {
try {
managers.get(event.world).unload();
managers.remove(event.world);
if (enableTileEntityTickThreading) {
Field loadedTileEntityField = FieldUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
Object loadedTileEntityList = loadedTileEntityField.get(event.world);
if (!(loadedTileEntityList instanceof EntityList)) {
Log.severe("Looks like another mod broke TickThreaded tile entities in world: " + Log.name(event.world));
}
}
if (enableEntityTickThreading) {
Field loadedEntityField = FieldUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
Object loadedEntityList = loadedEntityField.get(event.world);
if (!(loadedEntityList instanceof EntityList)) {
Log.severe("Looks like another mod broke TickThreaded entities in world: " + Log.name(event.world));
}
}
} catch (Exception e) {
Log.severe("Probable memory leak, failed to unload threading for world " + Log.name(event.world), e);
}
}
public TickManager getManager(World world) {
return managers.get(world);
}
public List<TickManager> getManagers() {
return new ArrayList<TickManager>(managers.values());
}
}