Clean progress bar code and fix TextureMap patch (#5304)

This commit is contained in:
mezz authored and GitHub committed 2018-12-28 18:05:05 -08:00
1 parent b4cf6f7f36
commit dc12cda505
15 files changed
+632 -290

No files matched your search

@@ -89,8 +89,6 @@ import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.Properties;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.client.ClientModLoader;
import net.minecraftforge.fml.common.ProgressManager;
import net.minecraftforge.fml.common.ProgressManager.ProgressBar;
import net.minecraftforge.logging.ModelLoaderErrorMessage;
import net.minecraftforge.registries.IRegistryDelegate;
@@ -34,13 +34,23 @@ import java.util.function.Supplier;
public enum SidedProvider
{
// All of these need to be careful not to directly dereference the client and server elements in their signatures
DATAFIXER(c->c.get().getDataFixer(), s->s.get().getDataFixer()),
SIDEDINIT((Function<Supplier<Minecraft>, Function<ModContainer, Event>>)c-> mc->new FMLClientInitEvent(c, mc),
(Function<Supplier<DedicatedServer>, Function<ModContainer, Event>>)s-> mc->new FMLServerInitEvent(s, mc)),
STRIPCHARS((Function<Supplier<Minecraft>, Function<String, String>>)c-> ClientHooks::stripSpecialChars,
(Function<Supplier<DedicatedServer>, Function<String, String>>)s-> str->str),
DATAFIXER(
c->c.get().getDataFixer(),
s->s.get().getDataFixer(),
()-> { throw new UnsupportedOperationException(); }),
SIDEDINIT(
(Function<Supplier<Minecraft>, Function<ModContainer, Event>>)c-> mc->new FMLClientInitEvent(c, mc),
s-> mc->new FMLServerInitEvent(s, mc),
()-> { throw new UnsupportedOperationException(); }),
STRIPCHARS(
(Function<Supplier<Minecraft>, Function<String, String>>)c-> ClientHooks::stripSpecialChars,
s-> str->str,
()-> str->str),
@SuppressWarnings("Convert2MethodRef") // need to not be methodrefs to avoid classloading all of StartupQuery's data (supplier is coming from StartupQuery)
STARTUPQUERY(c->StartupQuery.QueryWrapper.clientQuery(c), s->StartupQuery.QueryWrapper.dedicatedServerQuery(s));
STARTUPQUERY(
c->StartupQuery.QueryWrapper.clientQuery(c),
s->StartupQuery.QueryWrapper.dedicatedServerQuery(s),
()-> { throw new UnsupportedOperationException(); });
private static Supplier<Minecraft> client;
private static Supplier<DedicatedServer> server;
@@ -56,12 +66,13 @@ public enum SidedProvider
private final Function<Supplier<Minecraft>, ?> clientSide;
private final Function<Supplier<DedicatedServer>, ?> serverSide;
private final Supplier<?> testSide;
SidedProvider(Function<Supplier<Minecraft>,?> clientSide, Function<Supplier<DedicatedServer>,?> serverSide)
<T> SidedProvider(Function<Supplier<Minecraft>, T> clientSide, Function<Supplier<DedicatedServer>, T> serverSide, Supplier<T> testSide)
{
this.clientSide = clientSide;
this.serverSide = serverSide;
this.testSide = testSide;
}
@SuppressWarnings("unchecked")
@@ -73,7 +84,7 @@ public enum SidedProvider
return (T)this.serverSide.apply(server);
}
else {
throw new IllegalArgumentException("THREE SIDES? WUT?");
return (T)this.testSide.get();
}
}
}
@@ -125,8 +125,10 @@ public class SplashProgress
private static final int TIMING_FRAME_THRESHOLD = TIMING_FRAME_COUNT * 5 * 1000000; // 5 ms per frame, scaled to nanos
private static final Semaphore mutex = new Semaphore(1);
*/
public static Void processMessages() {
public static void processMessages() {
/*
// workaround for windows requiring messages being processed on the main thread
if (LWJGLUtil.getPlatform() != LWJGLUtil.PLATFORM_WINDOWS) return null;
// If we can't grab the mutex, the update call is blocked, probably in native code, just skip it and carry on
@@ -134,9 +136,9 @@ public class SplashProgress
if (!SplashProgress.mutex.tryAcquire()) return null;
Display.processMessages();
SplashProgress.mutex.release();
return null;
*/
}
/*
private static String getString(String name, String def)
{
String value = config.getProperty(name, def);
@@ -448,9 +450,10 @@ public class SplashProgress
drawBox(barWidth - 2, barHeight - 2);
// slidy part
setColor(barColor);
drawBox((barWidth - 2) * (b.getStep() + 1) / (b.getSteps() + 1), barHeight - 2); // Step can sometimes be 0.
int step = b.getStep();
drawBox((barWidth - 2) * (step + 1) / (b.getTotalSteps() + 1), barHeight - 2); // Step can sometimes be 0.
// progress text
String progress = "" + b.getStep() + "/" + b.getSteps();
String progress = "" + step + "/" + b.getTotalSteps();
glTranslatef(((float)barWidth - 2) / 2 - fontRenderer.getStringWidth(progress), 2, 0);
setColor(fontColor);
glScalef(2, 2, 1);
@@ -1,162 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.minecraftforge.fml.SidedProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.MessageFormatMessage;
import static net.minecraftforge.fml.Logging.SPLASH;
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public class ProgressManager
{
private static final Logger LOGGER = LogManager.getLogger();
private static final List<ProgressBar> bars = new CopyOnWriteArrayList<ProgressBar>();
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public static ProgressBar push(String title, int steps)
{
return push(title, steps, false);
}
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public static ProgressBar push(String title, int steps, boolean timeEachStep)
{
ProgressBar bar = new ProgressBar(title, steps);
bars.add(bar);
if (timeEachStep)
{
bar.timeEachStep();
}
// DistExecutor.runWhenOn(Dist.CLIENT, ()->SplashProgress::processMessages);
return bar;
}
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public static void pop(ProgressBar bar)
{
if(bar.getSteps() != bar.getStep()) throw new IllegalStateException("can't pop unfinished ProgressBar " + bar.getTitle());
bars.remove(bar);
if (bar.getSteps() != 0)
{
long newTime = System.nanoTime();
if (bar.timeEachStep)
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("Bar Step: {0} - {1} took {2,number,0.000}ms", bar.getTitle(), bar.getMessage(), (newTime - bar.lastTime) / 1.0e6));
if (bar.getSteps() == 1)
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("Bar Finished: {0} - {1} took {2,number,0.000}ms", bar.getTitle(), bar.getMessage(), (newTime - bar.lastTime) / 1.0e6));
else
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("Bar Finished: {0} took {1,number,0.000}ms", bar.getTitle(), (newTime - bar.lastTime) / 1.0e6));
}
// DistExecutor.runWhenOn(Dist.CLIENT, ()->SplashProgress::processMessages);
}
/*
* Internal use only.
*/
public static Iterator<ProgressBar> barIterator()
{
return bars.iterator();
}
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public static class ProgressBar
{
private final String title;
private final int steps;
private volatile int step = 0;
private volatile String message = "";
private boolean timeEachStep = false;
private long startTime = System.nanoTime();
private long lastTime = startTime;
private ProgressBar(String title, int steps)
{
this.title = title;
this.steps = steps;
}
public void step(Class<?> classToName, String... extra)
{
step(ClassNameUtils.shortName(classToName)+ String.join(" ", extra));
}
public void step(String message)
{
if(step >= steps) throw new IllegalStateException("too much steps for ProgressBar " + title);
if (timeEachStep && step != 0)
{
long newTime = System.nanoTime();
LOGGER.debug(SPLASH,new MessageFormatMessage("Bar Step: {0} - {1} took {2,number,0.000}ms", getTitle(), getMessage(), (newTime - lastTime) / 1.0e6));
lastTime = newTime;
}
step += 1;
this.message = SidedProvider.STRIPCHARS.<Function<String, String>>get().apply(message);
}
public String getTitle()
{
return title;
}
public int getSteps()
{
return steps;
}
public int getStep()
{
return step;
}
public String getMessage()
{
return message;
}
public void timeEachStep()
{
this.timeEachStep = true;
}
}
}
@@ -0,0 +1,31 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.progress;
public interface IProgressBarTracker
{
default void onBarCreated(ProgressBar bar) {}
default void onStepStarted(ProgressBar bar, int step, String message) {}
default void onStepFinished(ProgressBar bar, int step, String message) {}
default void onBarFinished(ProgressBar bar, int step, String message) {}
}
@@ -0,0 +1,98 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.progress;
import net.minecraftforge.fml.common.ClassNameUtils;
import org.apache.commons.lang3.tuple.Pair;
import java.util.function.Function;
/**
* Not a fully fleshed out API, may change in future MC versions.
* However feel free to use and suggest additions.
*/
public final class ProgressBar implements AutoCloseable
{
private final String title;
private final int totalSteps;
private final Function<String, String> stripChars;
private final IProgressBarTracker tracker;
private volatile int step = 0;
private volatile String message = "";
public ProgressBar(String title, int totalSteps, Function<String, String> stripChars, IProgressBarTracker tracker)
{
this.title = title;
this.totalSteps = totalSteps;
this.stripChars = stripChars;
this.tracker = tracker;
this.tracker.onBarCreated(this);
}
public void step(Class<?> classToName, String... extra)
{
step(ClassNameUtils.shortName(classToName) + String.join(" ", extra));
}
public synchronized void step(String newMessage)
{
if (step > 0)
{
tracker.onStepFinished(this, step, message);
}
step++;
message = stripChars.apply(newMessage);
tracker.onStepStarted(this, step, message);;
}
/**
* Get the current step and message.
*/
public synchronized Pair<Integer, String> getStepAndMessage()
{
return Pair.of(step, message);
}
/**
* Get the total number of steps.
*/
public int getTotalSteps()
{
return totalSteps;
}
/**
* Get the current title.
*/
public String getTitle()
{
return title;
}
@Override
public synchronized void close()
{
if (step > 0)
{
tracker.onStepFinished(this, step, message);
}
tracker.onBarFinished(this, step, message);
}
}
@@ -0,0 +1,129 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.progress;
import com.google.common.base.Stopwatch;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.client.SplashProgress;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.MessageFormatMessage;
import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import static net.minecraftforge.fml.Logging.SPLASH;
class StartupProgressBarTracker implements IProgressBarTracker
{
private static final Logger LOGGER = LogManager.getLogger();
private static final List<ProgressBar> bars = new CopyOnWriteArrayList<>();
private final boolean timeEachStep;
private final int steps;
private final Stopwatch stopwatch;
@Nullable
private Stopwatch stepStopwatch;
private String logPrefix = "";
StartupProgressBarTracker(boolean timeEachStep, int steps)
{
this.timeEachStep = timeEachStep;
this.steps = steps;
this.stopwatch = Stopwatch.createUnstarted();
}
@Override
public void onBarCreated(ProgressBar bar)
{
int depth = bars.size();
logPrefix = StringUtils.repeat(" ", depth);
bars.add(bar);
DistExecutor.runWhenOn(Dist.CLIENT, ()-> SplashProgress::processMessages);
}
@Override
public void onStepStarted(ProgressBar bar, int step, String message)
{
if (step == 1)
{
if (steps > 1)
{
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0}Bar Starting: {1}", logPrefix, bar.getTitle()));
if (timeEachStep)
{
stepStopwatch = Stopwatch.createStarted();
}
}
else
{
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0}Bar Starting: {1} - {2}", logPrefix, bar.getTitle(), message));
}
stopwatch.start();
}
if (stepStopwatch != null)
{
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0} Bar Step: {1} - {2} - starting", logPrefix, bar.getTitle(), message));
stepStopwatch.reset();
stepStopwatch.start();
}
}
@Override
public void onStepFinished(ProgressBar bar, int step, String message)
{
if (stepStopwatch != null)
{
stepStopwatch.stop();
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0} Bar Step: {1} - {2} - took {3}", logPrefix, bar.getTitle(), message, stepStopwatch));
}
}
@Override
public void onBarFinished(ProgressBar bar, int step, String message)
{
if (steps > 0)
{
stopwatch.stop();
bars.remove(bar);
if (steps > 1)
{
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0}Bar Finished: {1} - took {2}", logPrefix, bar.getTitle(), stopwatch));
}
else
{
LOGGER.debug(SPLASH, () -> new MessageFormatMessage("{0}Bar Finished: {1} - {2} - took {3}", logPrefix, bar.getTitle(), message, stopwatch));
}
DistExecutor.runWhenOn(Dist.CLIENT, () -> SplashProgress::processMessages);
}
}
/**
* Internal use only.
*/
public static Iterator<ProgressBar> barIterator()
{
return bars.iterator();
}
}
@@ -0,0 +1,63 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.progress;
import net.minecraftforge.fml.SidedProvider;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Function;
public class StartupProgressManager
{
private static final Function<String, String> stripChars = SidedProvider.STRIPCHARS.get();
public static void start(String title, int steps, Consumer<ProgressBar> task)
{
start(title, steps, false, task);
}
public static void start(String title, int steps, boolean timeEachStep, Consumer<ProgressBar> task)
{
try (ProgressBar bar = start(title, steps, timeEachStep))
{
task.accept(bar);
}
}
public static ProgressBar start(String title, int steps)
{
return start(title, steps, false);
}
public static ProgressBar start(String title, int steps, boolean timeEachStep)
{
StartupProgressBarTracker tracker = new StartupProgressBarTracker(timeEachStep, steps);
return new ProgressBar(title, steps, stripChars, tracker);
}
/**
* Internal use only.
*/
public static Iterator<ProgressBar> barIterator()
{
return StartupProgressBarTracker.barIterator();
}
}