HookProfiler.java
3.63 KB
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
package com.mumfrey.liteloader.core;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.logging.Logger;
import net.minecraft.client.Minecraft;
import net.minecraft.src.GameSettings;
import net.minecraft.src.Profiler;
/**
* Main LiteLoader tick hook
*
* @author Adam Mummery-Smith
*/
public class HookProfiler extends Profiler
{
/**
* Logger instance
*/
private Logger logger;
/**
* LiteLoader instance which will receive callbacks
*/
private LiteLoader loader;
/**
* Section list, used as a kind of stack to determine where we are in the profiler stack
*/
private LinkedList<String> sectionStack = new LinkedList<String>();
/**
* Initialisation done
*/
private boolean initDone = false;
/**
* Tick clock, sent as a flag to the core onTick so that mods know it's a new tick
*/
private boolean tick;
/**
* Optifine compatibility, pointer to the "Profiler" setting so we can enable it if it's disabled
*/
private Field ofProfiler;
/**
* Minecraft reference, only set if optifine compatibility is enabled
*/
private Minecraft mc;
/**
* .ctor
*
* @param core LiteLoader object which will get callbacks
* @param logger Logger instance
*/
public HookProfiler(LiteLoader core, Logger logger)
{
this.loader = core;
this.logger = logger;
// Detect optifine (duh!)
DetectOptifine();
}
/**
* Try to detect optifine using reflection
*
* @param logger
*/
private void DetectOptifine()
{
try
{
ofProfiler = GameSettings.class.getDeclaredField("ofProfiler");
}
catch (SecurityException ex) {}
catch (NoSuchFieldException ex)
{
logger.info("Optifine not detected");
}
finally
{
if (ofProfiler != null)
{
logger.info(String.format("Optifine version %s detected, enabling compatibility check", GetOptifineVersion()));
mc = Minecraft.getMinecraft();
}
}
}
/**
* Try to get the optifine version using reflection
*
* @return
*/
private String GetOptifineVersion()
{
try
{
Class config = Class.forName("Config");
if (config != null)
{
@SuppressWarnings("unchecked")
Method getVersion = config.getDeclaredMethod("getVersion");
if (getVersion != null)
{
return (String)getVersion.invoke(null);
}
}
}
catch (Exception ex) {}
return "Unknown";
}
/* (non-Javadoc)
* @see net.minecraft.src.Profiler#startSection(java.lang.String)
*/
@Override
public void startSection(String sectionName)
{
if (!initDone)
{
initDone = true;
loader.onInit();
}
if ("gameRenderer".equalsIgnoreCase(sectionName) && "root".equalsIgnoreCase(sectionStack.getLast()))
{
loader.onRender();
}
if ("animateTick".equals(sectionName)) tick = true;
sectionStack.add(sectionName);
super.startSection(sectionName);
if (ofProfiler != null)
{
try
{
ofProfiler.set(mc.gameSettings, true);
}
catch (IllegalArgumentException ex)
{
ofProfiler = null;
}
catch (IllegalAccessException ex)
{
ofProfiler = null;
}
}
}
/* (non-Javadoc)
* @see net.minecraft.src.Profiler#endSection()
*/
@Override
public void endSection()
{
super.endSection();
String endingSection = sectionStack.removeLast();
if ("gameRenderer".equalsIgnoreCase(endingSection) && "root".equalsIgnoreCase(sectionStack.getLast()))
{
super.startSection("litetick");
loader.onTick(this, tick);
tick = false;
super.endSection();
}
}
}