Allow server side lang file injection, hopefully

This commit is contained in:
Christian 2013-08-27 22:15:56 -04:00
parent 5925fa7c79
commit 4f9b778d14
2 changed files with 84 additions and 3 deletions

View File

@ -12,7 +12,18 @@
*/
package cpw.mods.fml.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.MapDifference;
@ -23,8 +34,10 @@ import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet131MapData;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.StringTranslate;
import net.minecraft.world.World;
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;
@ -193,11 +206,58 @@ public class FMLServerHandler implements IFMLSidedHandler
@Override
public void addModAsResource(ModContainer container)
{
// NOOP on server
File source = container.getSource();
try
{
if (source.isDirectory())
{
searchDirForENUSLanguage(source,"");
}
else
{
searchZipForENUSLanguage(source);
}
}
catch (IOException ioe)
{
}
}
private static final Pattern assetENUSLang = Pattern.compile("assets/(.*)/lang/en_US.lang");
private void searchZipForENUSLanguage(File source) throws IOException
{
ZipFile zf = new ZipFile(source);
for (ZipEntry ze : Collections.list(zf.entries()))
{
Matcher matcher = assetENUSLang.matcher(ze.getName());
if (matcher.matches())
{
FMLLog.fine("Injecting found translation data in zip file %s at %s into language system", source.getName(), ze.getName());
StringTranslate.inject(zf.getInputStream(ze));
}
}
IOUtils.closeQuietly(zf);
}
private void searchDirForENUSLanguage(File source, String path) throws IOException
{
for (File file : source.listFiles())
{
String currPath = path+file.getName();
if (file.isDirectory())
{
searchDirForENUSLanguage(file, currPath+'/');
}
Matcher matcher = assetENUSLang.matcher(currPath);
if (matcher.matches())
{
FMLLog.fine("Injecting found translation data at %s into language system", currPath);
StringTranslate.inject(new FileInputStream(file));
}
}
}
@Override
public void updateResourcePackList()
{
// NOOP on server
}
}

View File

@ -9,7 +9,28 @@
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.IOException;
@@ -45,7 +47,7 @@
@@ -23,9 +25,19 @@
public StringTranslate()
{
+ InputStream inputstream = StringTranslate.class.getResourceAsStream("/assets/minecraft/lang/en_US.lang");
+ localInject(inputstream);
+ }
+
+ public static void inject(InputStream inputstream)
+ {
+ field_74817_a.localInject(inputstream);
+ }
+
+ private void localInject(InputStream inputstream)
+ {
try
{
- InputStream inputstream = StringTranslate.class.getResourceAsStream("/assets/minecraft/lang/en_US.lang");
Iterator iterator = IOUtils.readLines(inputstream, Charsets.UTF_8).iterator();
while (iterator.hasNext())
@@ -45,7 +57,7 @@
}
}
}