forked from pcal43/fastback
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve pcal43#261 autoback (backup after autosaves) is not currently…
… supported on Forge Mixins are used in much the same way they are in the fabric version.
- Loading branch information
Showing
5 changed files
with
158 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
neoforge/src/main/java/net/pcal/fastback/mod/neoforge/MixinGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* FastBack - Fast, incremental Minecraft backups powered by Git. | ||
* Copyright (C) 2022 pcal.net | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package net.pcal.fastback.mod.neoforge; | ||
|
||
/** | ||
* Singleton 'gateway' that mixin code goes through to call back into the mod. | ||
* | ||
* @author pcal | ||
* @since 0.13.1 | ||
*/ | ||
public interface MixinGateway { | ||
|
||
static MixinGateway get() { | ||
return Singleton.INSTANCE; | ||
} | ||
|
||
boolean isWorldSaveEnabled(); | ||
|
||
void autoSaveCompleted(); | ||
|
||
class Singleton { | ||
private static MixinGateway INSTANCE = null; | ||
|
||
public static void register(MixinGateway gateway) { | ||
Singleton.INSTANCE = gateway; | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
neoforge/src/main/java/net/pcal/fastback/mod/neoforge/mixins/MinecraftServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* FastBack - Fast, incremental Minecraft backups powered by Git. | ||
* Copyright (C) 2022 pcal.net | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package net.pcal.fastback.mod.neoforge.mixins; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.pcal.fastback.mod.neoforge.MixinGateway; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import static net.pcal.fastback.logging.SystemLogger.syslog; | ||
|
||
/** | ||
* Allows us to disable vanilla saving during 'git add' to avoid coherency problems in the backup snapshots. Also | ||
* sends notifications when autosaving completes so we can follow them with automated backups. | ||
* | ||
* @author pcal | ||
* @since 0.0.1 | ||
*/ | ||
@Mixin(MinecraftServer.class) | ||
public class MinecraftServerMixin { | ||
|
||
/** | ||
* Intercept the call to saveAll that triggers on autosave, pass it through and then send out notification that | ||
* the autosave is done. | ||
*/ | ||
@Redirect(method = "autoSave()V", | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;saveEverything(ZZZ)Z")) | ||
public boolean fastback_saveAll(MinecraftServer instance, boolean suppressLogs, boolean flush, boolean force) { | ||
boolean result = instance.saveEverything(suppressLogs, flush, force); | ||
MixinGateway.get().autoSaveCompleted(); | ||
return result; | ||
} | ||
|
||
/** | ||
* Intercept save so we can hard-disable saving during critical parts of the backup. | ||
*/ | ||
@Inject(at = @At("HEAD"), method = "saveAllChunks(ZZZ)Z", cancellable = true) | ||
public void fastback_save(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> ci) { | ||
synchronized (this) { | ||
if (MixinGateway.get().isWorldSaveEnabled()) { | ||
syslog().debug("world saves are enabled, doing requested save"); | ||
} else { | ||
syslog().warn("Skipping requested save because a backup is in progress."); | ||
ci.setReturnValue(false); | ||
ci.cancel(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Intercept saveAll so we can hard-disable saving during critical parts of the backup. | ||
*/ | ||
@Inject(at = @At("HEAD"), method = "saveEverything(ZZZ)Z", cancellable = true) | ||
public void fastback_saveAll(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> ci) { | ||
synchronized (this) { | ||
if (MixinGateway.get().isWorldSaveEnabled()) { | ||
syslog().debug("world saves are enabled, doing requested saveAll"); | ||
//TODO should call save here to ensure all synced? | ||
} else { | ||
syslog().warn("Skipping requested saveAll because a backup is in progress."); | ||
ci.setReturnValue(false); | ||
ci.cancel(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "net.pcal.fastback.mod.neoforge.mixins", | ||
"compatibilityLevel": "JAVA_16", | ||
"mixins": [ | ||
"MinecraftServerMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |