1515import net .minecraft .client .network .NetHandlerPlayClient ;
1616import net .minecraft .client .renderer .EntityRenderer ;
1717import net .minecraft .client .renderer .GlStateManager ;
18- import net .minecraft .client .renderer .OpenGlHelper ;
1918import net .minecraft .client .renderer .texture .TextureManager ;
2019import net .minecraft .client .resources .IReloadableResourceManager ;
2120import net .minecraft .client .resources .LanguageManager ;
3231import org .lwjgl .LWJGLException ;
3332import org .lwjgl .input .Mouse ;
3433import org .lwjgl .opengl .Display ;
35- import org .lwjgl .opengl .GL11 ;
36- import org .lwjgl .opengl .GL12 ;
37- import org .lwjgl .opengl .GL14 ;
3834import org .polyfrost .crashpatch .CrashPatch ;
3935import org .polyfrost .crashpatch .CrashPatchConfig ;
4036import org .polyfrost .crashpatch .crashes .StateManager ;
4137import org .polyfrost .crashpatch .gui .CrashUI ;
4238import org .polyfrost .crashpatch .hooks .MinecraftHook ;
39+ import org .polyfrost .crashpatch .utils .GlUtil ;
4340import org .polyfrost .crashpatch .utils .GuiDisconnectedHook ;
44- import org .spongepowered .asm .mixin .Final ;
45- import org .spongepowered .asm .mixin .Mixin ;
46- import org .spongepowered .asm .mixin .Shadow ;
47- import org .spongepowered .asm .mixin .Unique ;
41+ import org .spongepowered .asm .mixin .*;
4842import org .spongepowered .asm .mixin .injection .At ;
4943import org .spongepowered .asm .mixin .injection .Inject ;
5044import org .spongepowered .asm .mixin .injection .Redirect ;
@@ -111,9 +105,6 @@ public abstract class MixinMinecraft implements MinecraftHook {
111105 @ Shadow
112106 protected abstract void runGameLoop ();
113107
114- @ Shadow
115- public abstract void freeMemory ();
116-
117108 @ Shadow
118109 public abstract void shutdownMinecraftApplet ();
119110
@@ -248,10 +239,14 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
248239 crashReport .getCategory ().addCrashSectionCallable ("Integrated Server Crashes Since Restart" , () -> String .valueOf (crashpatch$serverCrashCount ));
249240 }
250241
242+ public void crashpatch$resetGameState () {
243+ crashpatch$resetGameState (false );
244+ }
245+
251246 /**
252247 * @author Runemoro
253248 */
254- public void crashpatch$resetGameState () {
249+ public void crashpatch$resetGameState (boolean freeingMemory ) {
255250 try {
256251 // Free up memory such that this works properly in case of an OutOfMemoryError
257252 int originalMemoryReserveSize = -1 ;
@@ -265,9 +260,18 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
265260
266261 StateManager .INSTANCE .resetStates ();
267262
268- if (crashpatch$clientCrashCount >= CrashPatchConfig .INSTANCE .getLeaveLimit () || crashpatch$serverCrashCount >= CrashPatchConfig .INSTANCE .getLeaveLimit ()) {
263+ boolean shouldCrash = crashpatch$clientCrashCount >= CrashPatchConfig .INSTANCE .getLeaveLimit () || crashpatch$serverCrashCount >= CrashPatchConfig .INSTANCE .getLeaveLimit ();
264+
265+ if (shouldCrash && !freeingMemory ) {
269266 this .logger .error ("Crash limit reached, exiting world" );
270267 CrashUI .Companion .setLeaveWorldCrash (true );
268+ }
269+
270+ if (shouldCrash || freeingMemory
271+ //#if MC > 1.12
272+ //$$ || true
273+ //#endif
274+ ) {
271275 if (getNetHandler () != null ) {
272276 getNetHandler ().getNetworkManager ().closeChannel (new ChatComponentText ("[CrashPatch] Client crashed" ));
273277 }
@@ -280,7 +284,7 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
280284 this .scheduledTasks .clear (); // TODO: Figure out why this isn't necessary for vanilla disconnect
281285 }
282286
283- crashpatch$ resetState ();
287+ GlUtil . INSTANCE . resetState ();
284288
285289 if (originalMemoryReserveSize != -1 ) {
286290 try {
@@ -293,11 +297,25 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
293297 this .logger .error ("Failed to reset state after a crash" , t );
294298 try {
295299 StateManager .INSTANCE .resetStates ();
296- crashpatch$ resetState ();
300+ GlUtil . INSTANCE . resetState ();
297301 } catch (Throwable ignored ) {}
298302 }
299303 }
300304
305+ /**
306+ * @reason Disconnect from the current world and free memory, using a memory reserve
307+ * to make sure that an OutOfMemory doesn't happen while doing this.
308+ * <p>
309+ * Bugs Fixed:
310+ * - https://bugs.mojang.com/browse/MC-128953
311+ * - Memory reserve not recreated after out-of memory
312+ * @author Runemoro
313+ */
314+ @ Overwrite
315+ public void freeMemory () {
316+ crashpatch$resetGameState (true );
317+ }
318+
301319 /**
302320 * @author Runemoro
303321 */
@@ -417,59 +435,6 @@ public void redirect(FMLCommonHandler instance, int code) {
417435 }
418436 //#endif
419437
420- @ Unique
421- private void crashpatch$resetState () {
422- GlStateManager .bindTexture (0 );
423- GlStateManager .disableTexture2D ();
424-
425- // Reset depth
426- GlStateManager .disableDepth ();
427- GlStateManager .depthFunc (513 );
428- GlStateManager .depthMask (true );
429-
430- // Reset blend mode
431- GlStateManager .disableBlend ();
432- GlStateManager .blendFunc (1 , 0 );
433- GlStateManager .tryBlendFuncSeparate (1 , 0 , 1 , 0 );
434- GL14 .glBlendEquation (GL14 .GL_FUNC_ADD );
435-
436- // Reset polygon offset
437- GlStateManager .doPolygonOffset (0.0F , 0.0F );
438- GlStateManager .disablePolygonOffset ();
439-
440- // Reset color logic
441- GlStateManager .disableColorLogic ();
442- GlStateManager .colorLogicOp (5379 );
443-
444- // Disable lightmap
445- GlStateManager .setActiveTexture (OpenGlHelper .lightmapTexUnit );
446- GlStateManager .disableTexture2D ();
447-
448- GlStateManager .setActiveTexture (OpenGlHelper .defaultTexUnit );
449-
450- // Reset texture parameters
451- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL11 .GL_TEXTURE_MAG_FILTER , GL11 .GL_LINEAR );
452- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL11 .GL_TEXTURE_MIN_FILTER , GL11 .GL_NEAREST_MIPMAP_LINEAR );
453- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL11 .GL_TEXTURE_WRAP_S , GL11 .GL_REPEAT );
454- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL11 .GL_TEXTURE_WRAP_T , GL11 .GL_REPEAT );
455- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL12 .GL_TEXTURE_MAX_LEVEL , 1000 );
456- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL12 .GL_TEXTURE_MAX_LOD , 1000 );
457- GL11 .glTexParameteri (GL11 .GL_TEXTURE_2D , GL12 .GL_TEXTURE_MIN_LOD , -1000 );
458- GL11 .glTexParameterf (GL11 .GL_TEXTURE_2D , GL14 .GL_TEXTURE_LOD_BIAS , 0.0F );
459-
460- GlStateManager .colorMask (true , true , true , true );
461- GlStateManager .clearDepth (1.0D );
462- GL11 .glLineWidth (1.0F );
463- GL11 .glNormal3f (0.0F , 0.0F , 1.0F );
464- GL11 .glPolygonMode (GL11 .GL_FRONT , GL11 .GL_FILL );
465- GL11 .glPolygonMode (GL11 .GL_BACK , GL11 .GL_FILL );
466- GlStateManager .enableTexture2D ();
467- GlStateManager .clearDepth (1.0D );
468- GlStateManager .enableDepth ();
469- GlStateManager .depthFunc (515 );
470- GlStateManager .enableCull ();
471- GL11 .glDisable (GL11 .GL_SCISSOR_TEST );
472- }
473438
474439}
475440
0 commit comments