-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TickThreading.java
196 lines (177 loc) · 8.47 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
package me.nallar.tickthreading.minecraft;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.google.common.io.Files;
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.EnumerableWrapper;
import me.nallar.tickthreading.util.FieldUtil;
import me.nallar.tickthreading.util.LocationUtil;
import net.minecraft.command.ServerCommandManager;
import net.minecraft.server.MinecraftServer;
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 final int loadedTileEntityFieldIndex = 2;
private final int loadedEntityFieldIndex = 0;
public final boolean enabled;
private int regionSize = 16;
private boolean variableTickRate = true;
private boolean requirePatched = true;
private static File configurationDirectory;
final Map<World, TickManager> managers = new HashMap<World, TickManager>();
private static TickThreading instance;
public TickThreading() {
if (requirePatched) {
MinecraftServer.class.getProtectionDomain().getCodeSource().getLocation();
if (PatchManager.shouldPatch(LocationUtil.locationOf(MinecraftServer.class))) {
enabled = false;
try {
writePatchRunners();
} catch (IOException e) {
Log.severe("Failed to write patchrunners", e);
}
} else {
enabled = true;
}
} else {
enabled = true;
}
}
@Mod.Init
public void init(FMLInitializationEvent event) {
if (enabled) {
MinecraftForge.EVENT_BUS.register(this);
}
instance = this;
}
@Mod.PreInit
public void preInit(FMLPreInitializationEvent event) {
configurationDirectory = event.getSuggestedConfigurationFile().getParentFile();
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
Property regionSizeProperty = config.get(Configuration.CATEGORY_GENERAL, "regionSize", String.valueOf(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);
tpsCommandName.comment = "If the server must be patched to run with TickThreading";
config.save();
regionSize = regionSizeProperty.getInt(regionSize);
variableTickRate = variableTickRateProperty.getBoolean(variableTickRate);
TicksCommand.name = ticksCommandName.value;
TPSCommand.name = tpsCommandName.value;
requirePatched = requirePatchedProperty.getBoolean(requirePatched);
}
@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);
manager.setVariableTickRate(variableTickRate);
try {
Field loadedTileEntityField = FieldUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
Field loadedEntityField = FieldUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
new LoadedTileEntityList<TileEntity>(event.world, loadedTileEntityField, manager);
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);
}
}
@ForgeSubscribe
public void onWorldUnload(WorldEvent.Unload event) {
managers.remove(event.world);
try {
Field loadedTileEntityField = FieldUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
Object loadedTileEntityList = loadedTileEntityField.get(event.world);
if (loadedTileEntityList instanceof EntityList) {
((EntityList) loadedTileEntityList).unload();
} else {
Log.severe("Looks like another mod broke TickThreading in world: " + Log.name(event.world));
}
Field loadedEntityField = FieldUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
Object loadedEntityList = loadedEntityField.get(event.world);
if (loadedEntityList instanceof EntityList) {
((EntityList) loadedEntityList).unload();
} else {
Log.severe("Looks like another mod broke TickThreading 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());
}
public static TickThreading instance() {
return instance;
}
public static File getServerDirectory() {
File jarPath = LocationUtil.directoryOf(MinecraftServer.class);
return MinecraftServer.getServer().isDedicatedServer() ? jarPath : jarPath.getParentFile();
}
public static File getDataDirectory() {
return configurationDirectory;
}
private void writePatchRunners() throws IOException {
String java = System.getProperties().getProperty("java.home") + File.separator + "bin" + File.separator + "java";
String CP = LocationUtil.locationOf(TickThreading.class).getAbsolutePath();
String MS = LocationUtil.locationOf(MinecraftServer.class).getAbsolutePath();
ZipFile zipFile = new ZipFile(new File(CP));
CP += File.pathSeparator + new File(LocationUtil.directoryOf(MinecraftServer.class), "lib/guava-12.0.1.jar").getAbsolutePath();
for (ZipEntry zipEntry : new EnumerableWrapper<ZipEntry>((Enumeration<ZipEntry>) zipFile.entries())) {
if (zipEntry.getName().startsWith("patchrun/") && !zipEntry.getName().endsWith("/")) {
String data = new Scanner(zipFile.getInputStream(zipEntry), "UTF-8").useDelimiter("\\A").next();
data = data.replace("%JAVA%", java).replace("%CP%", CP).replace("%MS%", MS).replace("\r\n", "\n");
Files.write(data.getBytes("UTF-8"), new File(getServerDirectory(), zipEntry.getName().replace("patchrun/", "")));
}
}
zipFile.close();
}
}