mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 19:39:22 +00:00
Added ability for language files to opt-in to loading using normal Java properties format.
This allows for escape characters, as well as keeping %d and %f replacements in tact. To opt-in, the lang file must have this comment line: # PARSE_ESCAPES As this is opt-in, any errors resulting from misused format strings is on the modder/end user.
This commit is contained in:
1 parent
1b80328d3f
commit
e903f0ff69
3 files changed
+67
-2
No files matched your search
@@ -12,10 +12,18 @@
|
||||
|
||||
package net.minecraftforge.fml.common;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -50,6 +58,8 @@ import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.server.FMLServerHandler;
|
||||
|
||||
import org.apache.commons.io.Charsets;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -685,4 +695,45 @@ public class FMLCommonHandler
|
||||
FMLLog.log(Level.FATAL, e, "Exception caught executing FutureTask: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a lang file, first searching for a marker to enable the 'extended' format {escape charaters}
|
||||
* If the marker is not found it simply returns and let the vanilla code load things.
|
||||
* The Marker is 'PARSE_ESCAPES' by itself on a line starting with '#' as such:
|
||||
* #PARSE_ESCAPES
|
||||
*
|
||||
* @param table The Map to load each key/value pair into.
|
||||
* @param inputstream Input stream containing the lang file.
|
||||
* @return A new InputStream that vanilla uses to load normal Lang files, Null if this is a 'enhanced' file and loading is done.
|
||||
*/
|
||||
public InputStream loadLanguage(Map<String, String> table, InputStream inputstream) throws IOException
|
||||
{
|
||||
byte[] data = IOUtils.toByteArray(new InputStreamReader(inputstream, Charsets.UTF_8));
|
||||
|
||||
boolean isEnhanced = false;
|
||||
for (String line : IOUtils.readLines(new ByteArrayInputStream(data), Charsets.UTF_8))
|
||||
{
|
||||
if (line.charAt(0) == '#')
|
||||
{
|
||||
line = line.substring(1).trim();
|
||||
if (line.equals("PARSE_ESCAPES"))
|
||||
{
|
||||
isEnhanced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isEnhanced)
|
||||
return new ByteArrayInputStream(data);
|
||||
|
||||
Properties props = new Properties();
|
||||
props.load(new InputStreamReader(new ByteArrayInputStream(data), Charsets.UTF_8));
|
||||
for (Entry e : props.entrySet())
|
||||
{
|
||||
table.put((String)e.getKey(), (String)e.getValue());
|
||||
}
|
||||
props.clear();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user