2016-04-30 22:12:26 +08:00
|
|
|
package customskinloader.utils;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.net.URLClassLoader;
|
2016-05-07 19:41:32 +08:00
|
|
|
import java.net.URLDecoder;
|
2016-04-30 22:12:26 +08:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MinecraftUtil for mcp version.
|
|
|
|
|
* It is the only class in package 'customskinloader' which has differences in mcp/non-mcp version.
|
|
|
|
|
*
|
|
|
|
|
* @author Alexander Xia
|
|
|
|
|
* @since 13.6
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class MinecraftUtil {
|
|
|
|
|
public static File getMinecraftDataDir0(){
|
|
|
|
|
return net.minecraft.client.Minecraft.getMinecraft().mcDataDir;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 01:01:18 +08:00
|
|
|
public static File minecraftDataFolder=null;
|
|
|
|
|
private static final String MINECRAFT_DATA_FOLDER=".minecraft";
|
2016-04-30 22:12:26 +08:00
|
|
|
public static File getMinecraftDataDir(){
|
2016-05-07 13:11:08 +08:00
|
|
|
if(minecraftDataFolder!=null)
|
|
|
|
|
return minecraftDataFolder;
|
|
|
|
|
testProbe();
|
2016-05-02 01:01:18 +08:00
|
|
|
if(minecraftDataFolder!=null)
|
|
|
|
|
return minecraftDataFolder;
|
2016-04-30 22:12:26 +08:00
|
|
|
File temp=new File("");
|
|
|
|
|
if(temp.getAbsolutePath().endsWith(MINECRAFT_DATA_FOLDER))
|
|
|
|
|
return temp;
|
|
|
|
|
File temp0=new File(MINECRAFT_DATA_FOLDER);
|
|
|
|
|
if(temp0.exists())
|
|
|
|
|
return temp0;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 13:11:08 +08:00
|
|
|
private static String minecraftVersion=null;
|
2016-04-30 22:12:26 +08:00
|
|
|
public static String getMinecraftVersion(){
|
2016-05-07 13:11:08 +08:00
|
|
|
if(minecraftVersion!=null)
|
|
|
|
|
return minecraftVersion;
|
|
|
|
|
testProbe();
|
|
|
|
|
return minecraftVersion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/(.*?)/(.*?).jar$");
|
2016-05-07 19:41:32 +08:00
|
|
|
private static void testProbe(){
|
2016-05-07 13:11:08 +08:00
|
|
|
URLClassLoader ucl = (URLClassLoader)new MinecraftUtil().getClass().getClassLoader();
|
|
|
|
|
URL urls[] = ucl.getURLs();
|
|
|
|
|
for(URL url:urls){
|
2016-05-07 19:41:32 +08:00
|
|
|
Matcher m = null;
|
|
|
|
|
try{
|
|
|
|
|
m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if(m==null)
|
|
|
|
|
continue;
|
2016-05-07 13:11:08 +08:00
|
|
|
if(!m.matches())
|
|
|
|
|
continue;
|
2016-05-07 19:41:32 +08:00
|
|
|
if(minecraftDataFolder==null)
|
|
|
|
|
minecraftDataFolder=new File(m.group(1));
|
|
|
|
|
//System.out.println(url.getPath()+" "+minecraftDataFolder.getAbsolutePath());
|
2016-05-07 13:11:08 +08:00
|
|
|
if(minecraftVersion==null)
|
|
|
|
|
minecraftVersion=m.group(2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-04-30 22:12:26 +08:00
|
|
|
}
|
2016-07-31 22:00:46 +08:00
|
|
|
public static boolean isCoreFile(URL url){
|
|
|
|
|
Matcher m = null;
|
|
|
|
|
try{
|
|
|
|
|
m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(m==null)
|
|
|
|
|
return false;
|
|
|
|
|
return m.matches();
|
|
|
|
|
}
|
2016-04-30 22:12:26 +08:00
|
|
|
}
|