mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 16:54:22 +00:00
Make UniqueIdentifier final and add a hashCode. Closes #348
Clean up language registry. It can be used to access all translations of mods on client or server. Auto loads from assets files.
This commit is contained in:
1 parent
f9bc1f0847
commit
b4a6418d1c
6 files changed
+165
-81
No files matched your search
@@ -1,6 +1,6 @@
|
||||
--- ../src-base/minecraft/net/minecraft/util/StringTranslate.java
|
||||
+++ ../src-work/minecraft/net/minecraft/util/StringTranslate.java
|
||||
@@ -3,6 +3,8 @@
|
||||
@@ -3,10 +3,13 @@
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Maps;
|
||||
@@ -9,33 +9,53 @@
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import java.io.IOException;
|
||||
@@ -25,9 +27,19 @@
|
||||
import java.io.InputStream;
|
||||
+import java.util.HashMap;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
@@ -25,9 +28,22 @@
|
||||
|
||||
public StringTranslate()
|
||||
{
|
||||
+ InputStream inputstream = StringTranslate.class.getResourceAsStream("/assets/minecraft/lang/en_US.lang");
|
||||
+ localInject(inputstream);
|
||||
+ inject(inputstream);
|
||||
+ }
|
||||
+
|
||||
+ public static void inject(InputStream inputstream)
|
||||
+ {
|
||||
+ field_74817_a.localInject(inputstream);
|
||||
+ HashMap<String, String> map = parseLangFile(inputstream);
|
||||
+ field_74817_a.field_74816_c.putAll(map);
|
||||
+ field_74817_a.field_150511_e = System.currentTimeMillis();
|
||||
+ }
|
||||
+
|
||||
+ private void localInject(InputStream inputstream)
|
||||
+ public static HashMap<String,String> parseLangFile(InputStream inputstream)
|
||||
+ {
|
||||
+ HashMap<String,String> table = Maps.newHashMap();
|
||||
try
|
||||
{
|
||||
- InputStream inputstream = StringTranslate.class.getResourceAsStream("/assets/minecraft/lang/en_US.lang");
|
||||
Iterator iterator = IOUtils.readLines(inputstream, Charsets.UTF_8).iterator();
|
||||
|
||||
while (iterator.hasNext())
|
||||
@@ -49,7 +61,7 @@
|
||||
@@ -42,17 +58,17 @@
|
||||
{
|
||||
String s1 = astring[0];
|
||||
String s2 = field_111053_a.matcher(astring[1]).replaceAll("%$1s");
|
||||
- this.field_74816_c.put(s1, s2);
|
||||
+ table.put(s1, s2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.field_150511_e = System.currentTimeMillis();
|
||||
- this.field_150511_e = System.currentTimeMillis();
|
||||
}
|
||||
- catch (IOException ioexception)
|
||||
+ catch (Exception ioexception)
|
||||
{
|
||||
;
|
||||
}
|
||||
+ return table;
|
||||
}
|
||||
|
||||
static StringTranslate func_74808_a()
|
||||
@@ -17,8 +17,8 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
@@ -75,6 +75,7 @@ import cpw.mods.fml.common.WrongMinecraftVersionException;
|
||||
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.LanguageRegistry;
|
||||
import cpw.mods.fml.common.toposort.ModSortingException;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
@@ -480,6 +481,7 @@ public class FMLClientHandler implements IFMLSidedHandler
|
||||
@Override
|
||||
public void addModAsResource(ModContainer container)
|
||||
{
|
||||
LanguageRegistry.instance().loadLanguagesFor(container, Side.CLIENT);
|
||||
Class<?> resourcePackType = container.getCustomResourcePackClass();
|
||||
if (resourcePackType != null)
|
||||
{
|
||||
|
||||
@@ -79,7 +79,6 @@ public class FMLCommonHandler
|
||||
private List<ICrashCallable> crashCallables = Lists.newArrayList(Loader.instance().getCallableCrashInformation());
|
||||
private Set<SaveHandler> handlerSet = Sets.newSetFromMap(new MapMaker().weakKeys().<SaveHandler,Boolean>makeMap());
|
||||
private EventBus eventBus = new EventBus();
|
||||
|
||||
/**
|
||||
* The FML event bus. Subscribe here for FML related events
|
||||
*
|
||||
|
||||
@@ -338,7 +338,7 @@ public class GameRegistry
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class UniqueIdentifier
|
||||
public static final class UniqueIdentifier
|
||||
{
|
||||
public final String modId;
|
||||
public final String name;
|
||||
@@ -363,7 +363,13 @@ public class GameRegistry
|
||||
final UniqueIdentifier other = (UniqueIdentifier) obj;
|
||||
return Objects.equal(modId, other.modId) && Objects.equal(name, other.name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hashCode(modId, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
|
||||
@@ -12,18 +12,26 @@
|
||||
|
||||
package cpw.mods.fml.common.registry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.StringTranslate;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
@@ -31,6 +39,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.relauncher.Side;
|
||||
|
||||
public class LanguageRegistry
|
||||
{
|
||||
@@ -38,6 +47,8 @@ public class LanguageRegistry
|
||||
|
||||
private Map<String,Properties> modLanguageData=new HashMap<String,Properties>();
|
||||
|
||||
private static final Pattern assetENUSLang = Pattern.compile("assets/(.*)/lang/(.*).lang");
|
||||
|
||||
public static LanguageRegistry instance()
|
||||
{
|
||||
return INSTANCE;
|
||||
@@ -62,10 +73,19 @@ public class LanguageRegistry
|
||||
return localizedString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void addStringLocalization(String key, String value)
|
||||
{
|
||||
addStringLocalization(key, "en_US", value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void addStringLocalization(String key, String lang, String value)
|
||||
{
|
||||
Properties langPack=modLanguageData.get(lang);
|
||||
@@ -76,10 +96,18 @@ public class LanguageRegistry
|
||||
langPack.put(key,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void addStringLocalization(Properties langPackAdditions) {
|
||||
addStringLocalization(langPackAdditions, "en_US");
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void addStringLocalization(Properties langPackAdditions, String lang) {
|
||||
Properties langPack = modLanguageData.get(lang);
|
||||
if (langPack == null) {
|
||||
@@ -91,14 +119,10 @@ public class LanguageRegistry
|
||||
}
|
||||
}
|
||||
|
||||
public static void reloadLanguageTable()
|
||||
{
|
||||
// // reload language table by forcing lang to null and reloading the properties file
|
||||
// String lang = StringTranslate.func_74808_a().func_74811_c();
|
||||
// StringTranslate.func_74808_a().func_74810_a(lang, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void addNameForObject(Object objectToName, String lang, String name)
|
||||
{
|
||||
String objectName;
|
||||
@@ -115,12 +139,20 @@ public class LanguageRegistry
|
||||
addStringLocalization(objectName, lang, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public static void addName(Object objectToName, String name)
|
||||
{
|
||||
instance().addNameForObject(objectToName, "en_US", name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Deprecated
|
||||
public void loadLanguageTable(@SuppressWarnings("rawtypes") Map field_135032_a, String lang)
|
||||
{
|
||||
Properties usPack=modLanguageData.get("en_US");
|
||||
@@ -134,6 +166,10 @@ public class LanguageRegistry
|
||||
field_135032_a.putAll(langPack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void loadLocalization(String localizationFile, String lang, boolean isXML)
|
||||
{
|
||||
URL urlResource = this.getClass().getResource(localizationFile);
|
||||
@@ -155,6 +191,10 @@ public class LanguageRegistry
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated for removal in 1.8. Use the assets lang system
|
||||
*/
|
||||
@Deprecated
|
||||
public void loadLocalization(URL localizationFile, String lang, boolean isXML)
|
||||
{
|
||||
InputStream langStream = null;
|
||||
@@ -186,4 +226,81 @@ public class LanguageRegistry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void injectLanguage(String language, HashMap<String, String> parsedLangFile)
|
||||
{
|
||||
|
||||
Properties p = modLanguageData.get(language);
|
||||
if (p == null)
|
||||
{
|
||||
p = new Properties();
|
||||
modLanguageData.put(language, p);
|
||||
}
|
||||
p.putAll(parsedLangFile);
|
||||
}
|
||||
|
||||
public void loadLanguagesFor(ModContainer container, Side side)
|
||||
{
|
||||
File source = container.getSource();
|
||||
try
|
||||
{
|
||||
if (source.isDirectory())
|
||||
{
|
||||
searchDirForLanguages(source, "", side);
|
||||
}
|
||||
else
|
||||
{
|
||||
searchZipForLanguages(source, side);
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void searchZipForLanguages(File source, Side side) throws IOException
|
||||
{
|
||||
ZipFile zf = new ZipFile(source);
|
||||
for (ZipEntry ze : Collections.list(zf.entries()))
|
||||
{
|
||||
Matcher matcher = assetENUSLang.matcher(ze.getName());
|
||||
if (matcher.matches())
|
||||
{
|
||||
String lang = matcher.group(2);
|
||||
FMLLog.fine("Injecting found translation data for lang %s in zip file %s at %s into language system", lang, source.getName(), ze.getName());
|
||||
LanguageRegistry.instance().injectLanguage(lang, StringTranslate.parseLangFile(zf.getInputStream(ze)));
|
||||
// Ensure en_US is available to StringTranslate on the server
|
||||
if ("en_US".equals(lang) && side == Side.SERVER)
|
||||
{
|
||||
StringTranslate.inject(zf.getInputStream(ze));
|
||||
}
|
||||
}
|
||||
}
|
||||
zf.close();
|
||||
}
|
||||
|
||||
private void searchDirForLanguages(File source, String path, Side side) throws IOException
|
||||
{
|
||||
for (File file : source.listFiles())
|
||||
{
|
||||
String currPath = path+file.getName();
|
||||
if (file.isDirectory())
|
||||
{
|
||||
searchDirForLanguages(file, currPath+'/', side);
|
||||
}
|
||||
Matcher matcher = assetENUSLang.matcher(currPath);
|
||||
if (matcher.matches())
|
||||
{
|
||||
String lang = matcher.group(2);
|
||||
FMLLog.fine("Injecting found translation assets for lang %s at %s into language system", lang, currPath);
|
||||
LanguageRegistry.instance().injectLanguage(lang, StringTranslate.parseLangFile(new FileInputStream(file)));
|
||||
// Ensure en_US is available to StringTranslate on the server
|
||||
if ("en_US".equals(lang) && side == Side.SERVER)
|
||||
{
|
||||
StringTranslate.inject(new FileInputStream(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,27 +12,14 @@
|
||||
*/
|
||||
package cpw.mods.fml.server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import net.minecraft.network.INetHandler;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import net.minecraft.network.NetworkManager;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.StringTranslate;
|
||||
|
||||
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;
|
||||
@@ -94,7 +81,6 @@ public class FMLServerHandler implements IFMLSidedHandler
|
||||
public void finishServerLoading()
|
||||
{
|
||||
Loader.instance().initializeMods();
|
||||
LanguageRegistry.reloadLanguageTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -150,55 +136,9 @@ public class FMLServerHandler implements IFMLSidedHandler
|
||||
@Override
|
||||
public void addModAsResource(ModContainer container)
|
||||
{
|
||||
File source = container.getSource();
|
||||
try
|
||||
{
|
||||
if (source.isDirectory())
|
||||
{
|
||||
searchDirForENUSLanguage(source,"");
|
||||
}
|
||||
else
|
||||
{
|
||||
searchZipForENUSLanguage(source);
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
LanguageRegistry.instance().loadLanguagesFor(container, Side.SERVER);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
zf.close();
|
||||
}
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user