initial attempt at a better way to ask the user in case of startup issues

This commit is contained in:
Player committed 2014-04-01 14:30:47 +02:00
1 parent 038fa17ad3
commit 49c623f59c
16 files changed
+482 -191

No files matched your search

@@ -25,6 +25,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.Gui;
@@ -32,6 +33,7 @@ import net.minecraft.client.gui.GuiIngameMenu;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiSelectWorld;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.gui.ServerListEntryNormal;
import net.minecraft.client.multiplayer.GuiConnecting;
import net.minecraft.client.multiplayer.ServerData;
@@ -55,7 +57,10 @@ import net.minecraft.network.ServerStatusResponse;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.WorldSettings;
import org.apache.logging.log4j.Level;
import org.lwjgl.input.Mouse;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.BiMap;
@@ -67,6 +72,7 @@ import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.DummyModContainer;
import cpw.mods.fml.common.DuplicateModsFoundException;
@@ -81,6 +87,7 @@ import cpw.mods.fml.common.MissingModsException;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.common.ObfuscationReflectionHelper;
import cpw.mods.fml.common.StartupQuery;
import cpw.mods.fml.common.WorldAccessContainer;
import cpw.mods.fml.common.WrongMinecraftVersionException;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
@@ -88,7 +95,6 @@ import cpw.mods.fml.common.event.FMLMissingMappingsEvent.Action;
import cpw.mods.fml.common.eventhandler.EventBus;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.common.registry.GameData;
import cpw.mods.fml.common.registry.GameRegistryException;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.toposort.ModSortingException;
import cpw.mods.fml.relauncher.Side;
@@ -424,6 +430,39 @@ public class FMLClientHandler implements IFMLSidedHandler
client.displayGuiScreen(gui);
}
@Override
public void queryUser(StartupQuery query) throws InterruptedException
{
if (query.getResult() == null)
{
client.displayGuiScreen(new GuiNotification(query));
}
else
{
client.displayGuiScreen(new GuiConfirmation(query));
}
}
public boolean handleLoadingScreen(ScaledResolution scaledResolution)
{
if (client.currentScreen instanceof GuiMainMenu)
{
return false;
}
else
{
int width = scaledResolution.getScaledWidth();
int height = scaledResolution.getScaledHeight();
int mouseX = Mouse.getX() * width / client.displayWidth;
int mouseZ = height - Mouse.getY() * height / client.displayHeight - 1;
client.currentScreen.drawScreen(mouseX, mouseZ, 0);
client.currentScreen.handleInput();
return true;
}
}
public WorldClient getWorldClient()
{
return client.theWorld;
@@ -596,42 +635,10 @@ public class FMLClientHandler implements IFMLSidedHandler
}
else
{
launchIntegratedServerCallback(dirName, saveName);
client.launchIntegratedServer(dirName, saveName, (WorldSettings)null);
}
}
private CountDownLatch gameReleaseLatch;
private Thread clientWaiter;
private GameRegistryException gre;
public void launchIntegratedServerCallback(String dirName, String saveName)
{
try
{
try
{
Thread.interrupted();
gameReleaseLatch = new CountDownLatch(1);
clientWaiter = Thread.currentThread();
client.launchIntegratedServer(dirName, saveName, (WorldSettings)null);
System.out.printf("POKEE %b\n", Thread.currentThread().isInterrupted());
gameReleaseLatch.await();
}
catch (InterruptedException ie)
{
Thread.interrupted();
throw gre;
}
}
catch (GameRegistryException gre)
{
client.loadWorld(null);
showGuiScreen(new GuiModItemsMissing(gre.getItems()));
}
Thread.interrupted();
}
public void showInGameModOptions(GuiIngameMenu guiIngameMenu)
{
showGuiScreen(new GuiIngameModOptions(guiIngameMenu));
@@ -826,57 +833,6 @@ public class FMLClientHandler implements IFMLSidedHandler
return defaultMissingAction;
}
@Override
public void serverLoadedSuccessfully()
{
if (gameReleaseLatch!=null)
{
gameReleaseLatch.countDown();
}
}
@Override
public void failedServerLoading(RuntimeException ex, WorldAccessContainer wac)
{
if (wac instanceof FMLContainer && ex instanceof GameRegistryException)
{
try
{
gre = (GameRegistryException) ex;
Executors.newSingleThreadExecutor().submit(new Callable<Void>()
{
// This needs to happen in a separate thread so that the server can "crash"
// The three pokes are for the client - it's in a sleep loop, and needs to get
// out of it
@Override
public Void call() throws Exception
{
System.err.println("POKE");
clientWaiter.interrupt();
Thread.sleep(50);
System.err.println("POKE");
clientWaiter.interrupt();
Thread.sleep(50);
System.err.println("POKE");
clientWaiter.interrupt();
return null;
}
});
}
catch (Throwable t)
{
FMLLog.log(Level.ERROR, t, "stuff");
}
}
}
public boolean handlingCrash(CrashReport report)
{
return report.getCrashCause() instanceof GameRegistryException;
}
@Override
public boolean shouldAllowPlayerLogins()
{
@@ -0,0 +1,33 @@
package cpw.mods.fml.client;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiOptionButton;
import net.minecraft.client.resources.I18n;
import cpw.mods.fml.common.StartupQuery;
public class GuiConfirmation extends GuiNotification
{
public GuiConfirmation(StartupQuery query)
{
super(query);
}
@SuppressWarnings("unchecked")
@Override
public void initGui()
{
this.buttonList.add(new GuiOptionButton(0, this.width / 2 - 155, this.height - 38, I18n.format("gui.yes")));
this.buttonList.add(new GuiOptionButton(1, this.width / 2 - 155 + 160, this.height - 38, I18n.format("gui.no")));
}
@Override
protected void actionPerformed(GuiButton button)
{
if (button.enabled && (button.id == 0 || button.id == 1))
{
FMLClientHandler.instance().showGuiScreen(null);
query.setResult(button.id == 0);
query.finish();
}
}
}
@@ -0,0 +1,62 @@
package cpw.mods.fml.client;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import cpw.mods.fml.common.StartupQuery;
public class GuiNotification extends GuiScreen
{
public GuiNotification(StartupQuery query)
{
this.query = query;
}
@SuppressWarnings("unchecked")
@Override
public void initGui()
{
this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height - 38, I18n.format("gui.done")));
}
@Override
protected void actionPerformed(GuiButton button)
{
if (button.enabled && button.id == 0)
{
FMLClientHandler.instance().showGuiScreen(null);
query.finish();
}
}
@Override
public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_)
{
this.drawDefaultBackground();
String[] lines = query.getText().split("\n");
int spaceAvailable = this.height - 38 - 20;
int spaceRequired = Math.min(spaceAvailable, 10 + 10 * lines.length);
int offset = 10 + (spaceAvailable - spaceRequired) / 2; // vertically centered
for (String line : lines)
{
if (offset >= spaceAvailable)
{
this.drawCenteredString(this.fontRendererObj, "...", this.width / 2, offset, 0xFFFFFF);
break;
}
else
{
if (!line.isEmpty()) this.drawCenteredString(this.fontRendererObj, line, this.width / 2, offset, 0xFFFFFF);
offset += 10;
}
}
super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_);
}
protected final StartupQuery query;
}
@@ -2,12 +2,16 @@ package cpw.mods.fml.client;
import java.io.File;
import java.io.IOException;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiLabel;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiSelectWorld;
import net.minecraft.client.gui.GuiYesNo;
import net.minecraft.world.WorldSettings;
import org.apache.logging.log4j.Level;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.ObfuscationReflectionHelper;
import cpw.mods.fml.common.ZipperUtil;
@@ -65,9 +69,7 @@ public class GuiOldSaveLoadConfirm extends GuiYesNo {
return;
}
FMLClientHandler.instance().showGuiScreen(null);
FMLClientHandler.instance().launchIntegratedServerCallback(dirName, saveName);
mc.launchIntegratedServer(dirName, saveName, (WorldSettings)null);
}
}
}
@@ -15,6 +15,9 @@ package cpw.mods.fml.common;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.entity.item.EntityItem;
@@ -29,8 +32,10 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraft.world.storage.SaveHandler;
import net.minecraft.world.storage.WorldInfo;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
@@ -39,6 +44,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
import cpw.mods.fml.common.eventhandler.EventBus;
import cpw.mods.fml.common.gameevent.InputEvent;
@@ -266,7 +272,6 @@ public class FMLCommonHandler
public boolean handleServerStarting(MinecraftServer server)
{
sidedDelegate.serverLoadedSuccessfully();
return Loader.instance().serverStarting(server);
}
@@ -290,6 +295,11 @@ public class FMLCommonHandler
sidedDelegate.showGuiScreen(clientGuiElement);
}
public void queryUser(StartupQuery query) throws InterruptedException
{
sidedDelegate.queryUser(query);
}
public void onServerStart(MinecraftServer dedicatedServer)
{
FMLServerHandler.instance();
@@ -381,15 +391,7 @@ public class FMLCommonHandler
WorldAccessContainer wac = ((InjectedModContainer)mc).getWrappedWorldAccessContainer();
if (wac != null)
{
try
{
wac.readData(handler, worldInfo, additionalProperties, tagCompound.getCompoundTag(mc.getModId()));
}
catch (RuntimeException ex)
{
sidedDelegate.failedServerLoading(ex, wac);
throw ex;
}
wac.readData(handler, worldInfo, additionalProperties, tagCompound.getCompoundTag(mc.getModId()));
}
}
}
@@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
@@ -40,7 +41,6 @@ import cpw.mods.fml.common.network.NetworkCheckHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import cpw.mods.fml.common.registry.GameData;
import cpw.mods.fml.common.registry.GameRegistryException;
import cpw.mods.fml.relauncher.Side;
/**
@@ -160,6 +160,9 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai
}
}
}
List<String> failedElements = null;
if (tag.hasKey("ModItemData"))
{
FMLLog.info("Attempting to convert old world data to new system. This may be trouble!");
@@ -200,11 +203,8 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai
dataList.put(itemLabel, itemId);
}
}
List<String> failedElements = GameData.injectWorldIDMap(dataList, true, true);
if (!failedElements.isEmpty())
{
throw new GameRegistryException("Failed to load the world - there are fatal block and item id issues", failedElements);
}
failedElements = GameData.injectWorldIDMap(dataList, true, true);
}
else if (tag.hasKey("ItemData"))
{
@@ -243,11 +243,20 @@ public class FMLContainer extends DummyModContainer implements WorldAccessContai
itemAliases.put(dataTag.getString("K"), dataTag.getString("V"));
}
List<String> failedElements = GameData.injectWorldIDMap(dataList, blockedIds, blockAliases, itemAliases, true, true);
if (!failedElements.isEmpty())
{
throw new GameRegistryException("Failed to load the world - there are fatal block and item id issues", failedElements);
}
failedElements = GameData.injectWorldIDMap(dataList, blockedIds, blockAliases, itemAliases, true, true);
}
if (failedElements != null && !failedElements.isEmpty())
{
String text = "Forge Mod Loader could not load this save\n\n" +
"There are "+failedElements.size()+" unassigned blocks and items in this save\n"+
"You will not be able to load until they are present again\n\n"+
"Missing Blocks/Items:\n";
for (String s : failedElements) text += s + "\n";
StartupQuery.notify(text);
StartupQuery.abort();
}
}
@@ -14,6 +14,8 @@ package cpw.mods.fml.common;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetworkManager;
@@ -32,6 +34,8 @@ public interface IFMLSidedHandler
void showGuiScreen(Object clientGuiElement);
void queryUser(StartupQuery query) throws InterruptedException;
void beginServerLoading(MinecraftServer server);
void finishServerLoading();
@@ -58,9 +62,5 @@ public interface IFMLSidedHandler
FMLMissingMappingsEvent.Action getDefaultMissingAction();
void serverLoadedSuccessfully();
void failedServerLoading(RuntimeException ex, WorldAccessContainer wac);
boolean shouldAllowPlayerLogins();
}
@@ -0,0 +1,142 @@
package cpw.mods.fml.common;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
public class StartupQuery {
// internal class/functionality, do not use
public static boolean confirm(String text)
{
StartupQuery query = new StartupQuery(text, new AtomicBoolean());
query.execute();
return query.getResult();
}
public static void notify(String text)
{
StartupQuery query = new StartupQuery(text, null);
query.execute();
}
public static void abort()
{
FMLCommonHandler.instance().getMinecraftServerInstance().initiateShutdown();
aborted = true; // to abort loading and go back to the main menu
throw new AbortedException(); // to halt the server
}
public static void reset()
{
pending = null;
aborted = false;
}
public static boolean check()
{
if (pending != null)
{
try
{
FMLCommonHandler.instance().queryUser(pending);
}
catch (InterruptedException e)
{
FMLLog.warning("query interrupted");
abort();
}
pending = null;
}
return !aborted;
}
private static volatile StartupQuery pending;
private static volatile boolean aborted = false;
private StartupQuery(String text, AtomicBoolean result)
{
this.text = text;
this.result = result;
}
public Boolean getResult()
{
return result == null ? null : result.get();
}
public void setResult(boolean result)
{
this.result.set(result);
}
public String getText()
{
return text;
}
public void finish()
{
signal.countDown();
}
private void execute()
{
String prop = System.getProperty("fml.queryResult");
if (result != null && prop != null)
{
FMLLog.info("Using fml.queryResult %s to answer the following query:\n%s", prop, text);
if (prop.equalsIgnoreCase("confirm"))
{
setResult(true);
return;
}
else if (prop.equalsIgnoreCase("cancel"))
{
setResult(false);
return;
}
FMLLog.warning("Invalid value for fml.queryResult: %s, expected confirm or cancel", prop);
}
pending = this; // let the other thread start the query
// the client will eventually check pending and execute the query
// command handling in mc is synchronous, execute the server-side query directly
if (FMLCommonHandler.instance().getSide().isServer()) check();
try
{
signal.await();
}
catch (InterruptedException e)
{
FMLLog.warning("query interrupted");
abort();
}
}
private String text;
private AtomicBoolean result;
private CountDownLatch signal = new CountDownLatch(1);
/**
* Exception not being caught by the crash report generation logic.
*/
public static class AbortedException extends RuntimeException
{
private static final long serialVersionUID = -5933665223696833921L;
private AbortedException()
{
super();
}
}
}
@@ -45,6 +45,7 @@ import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.StartupQuery;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent.MissingMapping;
import cpw.mods.fml.common.registry.GameRegistry.Type;
@@ -1,19 +0,0 @@
package cpw.mods.fml.common.registry;
import java.util.List;
public class GameRegistryException extends RuntimeException {
private static final long serialVersionUID = 1L;
private List<String> items;
public GameRegistryException(String message, List<String> items)
{
super(message);
this.items = items;
}
public List<String> getItems()
{
return this.items;
}
}
@@ -12,18 +12,27 @@
*/
package cpw.mods.fml.server;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import net.minecraft.command.ServerCommand;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.NetworkManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.dedicated.DedicatedServer;
import com.google.common.collect.ImmutableList;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.IFMLSidedHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.StartupQuery;
import cpw.mods.fml.common.WorldAccessContainer;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
import cpw.mods.fml.common.eventhandler.EventBus;
@@ -134,6 +143,65 @@ public class FMLServerHandler implements IFMLSidedHandler
{
}
@Override
public void queryUser(StartupQuery query) throws InterruptedException
{
if (query.getResult() == null)
{
FMLLog.warning("%s", query.getText());
query.finish();
}
else
{
String text = query.getText() +
"\n\nRun the command /fml confirm or or /fml cancel to proceed." +
"\nAlternatively start the server with -Dfml.queryResult=confirm or -Dfml.queryResult=cancel to preselect the answer.";
FMLLog.warning("%s", text);
boolean done = false;
while (!done && server.isServerRunning())
{
if (Thread.interrupted()) throw new InterruptedException();
DedicatedServer dedServer = (DedicatedServer) server;
// rudimentary command processing, check for fml confirm/cancel and stop commands
synchronized (dedServer.pendingCommandList)
{
for (Iterator<ServerCommand> it = dedServer.pendingCommandList.iterator(); it.hasNext(); )
{
String cmd = it.next().command.trim().toLowerCase();
if (cmd.equals("/fml confirm"))
{
FMLLog.info("confirmed");
query.setResult(true);
done = true;
it.remove();
}
else if (cmd.equals("/fml cancel"))
{
FMLLog.info("cancelled");
query.setResult(false);
done = true;
it.remove();
}
else if (cmd.equals("/stop"))
{
StartupQuery.abort();
}
}
}
Thread.sleep(10L);
}
query.finish();
}
}
@Override
public boolean shouldServerShouldBeKilledQuietly()
{
@@ -186,16 +254,6 @@ public class FMLServerHandler implements IFMLSidedHandler
public FMLMissingMappingsEvent.Action getDefaultMissingAction()
{
return FMLMissingMappingsEvent.Action.valueOf(System.getProperty("fml.missingBlockAction", "FAIL"));
}
@Override
public void serverLoadedSuccessfully()
{
}
@Override
public void failedServerLoading(RuntimeException ex, WorldAccessContainer wac)
{
}
@Override
public boolean shouldAllowPlayerLogins()