PageRenderTime 918ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/moCreatures/server/debug/sources - Copie/net/minecraft/src/ModLoader.java

https://code.google.com/p/minecraft-smp-mocreatures/
Java | 344 lines | 318 code | 23 blank | 3 comment | 43 complexity | 00da1e0e30a49ae7a10706d6c23caf3f MD5 | raw file
  1. package net.minecraft.src;
  2. // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
  3. // Jad home page: http://www.kpdus.com/jad.html
  4. // Decompiler options: packimports(3) braces deadcode
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Method;
  12. import java.util.HashMap;
  13. import java.util.LinkedList;
  14. import java.util.Map;
  15. import java.util.logging.FileHandler;
  16. import java.util.logging.Logger;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipInputStream;
  19. import net.minecraft.server.MinecraftServer;
  20. public final class ModLoader
  21. {
  22. public static final boolean DEBUG = false;
  23. private static Field modifiers = null;
  24. private static int highestEntityId = 100;
  25. private static int itemSpriteIndex = 0;
  26. private static final Logger logger = Logger.getLogger("ModLoader");
  27. private static FileHandler logHandler = null;
  28. private static Method method_RegisterEntityID = null;
  29. private static Method method_RegisterTileEntity = null;
  30. private static final LinkedList<BaseMod> modList = new LinkedList<BaseMod>();
  31. private static final Map<Integer, Map<String, Integer>> overrides = new HashMap<Integer, Map<String, Integer>>();
  32. private static int terrainSpriteIndex = 0;
  33. private static final boolean[] usedItemSprites = new boolean[256];
  34. private static final boolean[] usedTerrainSprites = new boolean[256];
  35. public static final String VERSION = "v1.0.0 (MC 1.3_01)";
  36. private ModLoader()
  37. {
  38. }
  39. public static void addAllRecipes(CraftingManager recipes)
  40. {
  41. logger.finer("Initializing recipes");
  42. for (BaseMod mod : modList)
  43. mod.addRecipes(recipes);
  44. }
  45. private static void addMod(ClassLoader loader, String fileName)
  46. {
  47. try
  48. {
  49. String name = fileName.split("\\.")[0];
  50. if(name.contains("$"))
  51. return;
  52. Package pack = ModLoader.class.getPackage();
  53. if(pack != null)
  54. name = new StringBuilder(pack.getName()).append(".").append(name).toString();
  55. Class<?> instClass = loader.loadClass(name);
  56. if(instClass.isAssignableFrom(BaseMod.class))
  57. return;
  58. BaseMod basemod = (BaseMod)instClass.newInstance();
  59. if(basemod != null)
  60. {
  61. modList.add(basemod);
  62. logger.fine((new StringBuilder("Mod Loaded: \"")).append(basemod.toString()).append("\" from ").append(fileName).toString());
  63. System.out.println("Mod Loaded: " + basemod.toString());
  64. }
  65. }
  66. catch(Throwable e)
  67. {
  68. logger.fine((new StringBuilder("Failed to load mod from \"")).append(fileName).append("\"").toString());
  69. System.out.println((new StringBuilder("Failed to load mod from \"")).append(fileName).append("\"").toString());
  70. logger.throwing("ModLoader", "addMod", e);
  71. throwException(e);
  72. }
  73. }
  74. public static int addOverride(String fileToOverride, String fileToAdd)
  75. {
  76. try
  77. {
  78. int i = getUniqueSpriteIndex(fileToOverride);
  79. addOverride(fileToOverride, fileToAdd, i);
  80. return i;
  81. }
  82. catch(Throwable e)
  83. {
  84. logger.throwing("ModLoader", "addOverride", e);
  85. throwException(e);
  86. throw new RuntimeException(e);
  87. }
  88. }
  89. public static void addOverride(String path, String overlayPath, int index)
  90. {
  91. int dst = -1;
  92. if(path.equals("/terrain.png"))
  93. dst = 0;
  94. else if(path.equals("/gui/items.png"))
  95. dst = 1;
  96. else
  97. return;
  98. logger.finer((new StringBuilder("addOverride(")).append(path).append(",").append(overlayPath).append(",").append(index).append(")").toString());
  99. Map<String, Integer> overlays = overrides.get(Integer.valueOf(dst));
  100. if(overlays == null)
  101. {
  102. overlays = new HashMap<String, Integer>();
  103. overrides.put(Integer.valueOf(dst), overlays);
  104. }
  105. overlays.put(overlayPath, Integer.valueOf(index));
  106. }
  107. public static Logger getLogger()
  108. {
  109. return logger;
  110. }
  111. public static int getUniqueEntityId()
  112. {
  113. return highestEntityId++;
  114. }
  115. private static int getUniqueItemSpriteIndex()
  116. {
  117. for(; itemSpriteIndex < usedItemSprites.length; itemSpriteIndex++)
  118. {
  119. if(!usedItemSprites[itemSpriteIndex])
  120. {
  121. usedItemSprites[itemSpriteIndex] = true;
  122. return itemSpriteIndex++;
  123. }
  124. }
  125. Exception e = new Exception("No more empty item sprite indices left!");
  126. logger.throwing("ModLoader", "getUniqueItemSpriteIndex", e);
  127. throwException(e);
  128. return 0;
  129. }
  130. public static int getUniqueSpriteIndex(String path)
  131. {
  132. if(path.equals("/gui/items.png"))
  133. return getUniqueItemSpriteIndex();
  134. if(path.equals("/terrain.png"))
  135. return getUniqueTerrainSpriteIndex();
  136. Exception e = new Exception((new StringBuilder("No registry for this texture: ")).append(path).toString());
  137. logger.throwing("ModLoader", "getUniqueItemSpriteIndex", e);
  138. throwException(e);
  139. return 0;
  140. }
  141. private static int getUniqueTerrainSpriteIndex()
  142. {
  143. for(; terrainSpriteIndex < usedTerrainSprites.length; terrainSpriteIndex++)
  144. {
  145. if(!usedTerrainSprites[terrainSpriteIndex])
  146. {
  147. usedTerrainSprites[terrainSpriteIndex] = true;
  148. return terrainSpriteIndex++;
  149. }
  150. }
  151. Exception e = new Exception("No more empty terrain sprite indices left!");
  152. logger.throwing("ModLoader", "getUniqueItemSpriteIndex", e);
  153. throwException(e);
  154. return 0;
  155. }
  156. public static void initialize()
  157. {
  158. String usedItemSpritesString = "1111111111111111111111111111111111111101111111011111111111110001111111111111111111111011111000111111100110000011111110000000001111111001100000110000000100000011000000010000001100000000000000110000000000000000000000000000000000000000000000001100000000000000";
  159. String usedTerrainSpritesString = "1111111111111111111111111111110111111100011111111111110001111110111111111111000011110011111111111111001111000000111111111111100011111111000010001111011110000000111011000000000011100000000000001110000000000111111000000000001101000000000001111111111111000011";
  160. for(int i = 0; i < 256; i++)
  161. {
  162. usedItemSprites[i] = usedItemSpritesString.charAt(i) == '1';
  163. usedTerrainSprites[i] = usedTerrainSpritesString.charAt(i) == '1';
  164. }
  165. try
  166. {
  167. modifiers = Field.class.getDeclaredField("modifiers");
  168. modifiers.setAccessible(true);
  169. try
  170. {
  171. method_RegisterTileEntity = TileEntity.class.getDeclaredMethod("a", new Class[] { Class.class, String.class });
  172. }
  173. catch(NoSuchMethodException e)
  174. {
  175. method_RegisterTileEntity = TileEntity.class.getDeclaredMethod("addMapping", new Class[] { Class.class, String.class });
  176. }
  177. method_RegisterTileEntity.setAccessible(true);
  178. try
  179. {
  180. method_RegisterEntityID = (EntityList.class).getDeclaredMethod("a", new Class[] { Class.class, String.class, Integer.TYPE });
  181. }
  182. catch(NoSuchMethodException e)
  183. {
  184. method_RegisterEntityID = (EntityList.class).getDeclaredMethod("addMapping", new Class[] { Class.class, String.class, Integer.TYPE });
  185. }
  186. method_RegisterEntityID.setAccessible(true);
  187. }
  188. catch(SecurityException e)
  189. {
  190. logger.throwing("ModLoader", "init", e);
  191. throwException(e);
  192. throw new RuntimeException(e);
  193. }
  194. catch(NoSuchFieldException e)
  195. {
  196. logger.throwing("ModLoader", "init", e);
  197. throwException(e);
  198. throw new RuntimeException(e);
  199. }
  200. catch(NoSuchMethodException e)
  201. {
  202. logger.throwing("ModLoader", "init", e);
  203. throwException(e);
  204. throw new RuntimeException(e);
  205. }
  206. catch(IllegalArgumentException e)
  207. {
  208. logger.throwing("ModLoader", "init", e);
  209. throwException(e);
  210. throw new RuntimeException(e);
  211. }
  212. try
  213. {
  214. MinecraftServer.logger.fine("ModLoader Initializing...");
  215. System.out.println("ModLoader Initializing...");
  216. File file1 = new File((ModLoader.class).getProtectionDomain().getCodeSource().getLocation().toURI());
  217. readFromClassPath(file1);
  218. System.out.println("ModLoader Initialized.");
  219. addAllRecipes(CraftingManager.getInstance());
  220. }
  221. catch(Throwable e)
  222. {
  223. logger.throwing("ModLoader", "init", e);
  224. throwException("ModLoader has failed to initialize.", e);
  225. if(logHandler != null)
  226. logHandler.close();
  227. throw new RuntimeException(e);
  228. }
  229. }
  230. private static void readFromClassPath(File file)
  231. throws FileNotFoundException, IOException
  232. {
  233. logger.finer((new StringBuilder("Adding mods from ")).append(file.getCanonicalPath()).toString());
  234. ClassLoader classloader = (ModLoader.class).getClassLoader();
  235. if(file.isFile() && (file.getName().endsWith(".jar") || file.getName().endsWith(".zip")))
  236. {
  237. logger.finer("Zip found.");
  238. FileInputStream fileinputstream = new FileInputStream(file);
  239. ZipInputStream zipinputstream = new ZipInputStream(fileinputstream);
  240. do
  241. {
  242. ZipEntry zipentry = zipinputstream.getNextEntry();
  243. if(zipentry == null)
  244. break;
  245. String s1 = zipentry.getName();
  246. if(!zipentry.isDirectory() && s1.startsWith("mod_") && s1.endsWith(".class"))
  247. addMod(classloader, s1);
  248. } while(true);
  249. fileinputstream.close();
  250. } else if(file.isDirectory())
  251. {
  252. Package package1 = (ModLoader.class).getPackage();
  253. if(package1 != null)
  254. {
  255. String s = package1.getName().replace('.', File.separatorChar);
  256. file = new File(file, s);
  257. }
  258. logger.finer("Directory found.");
  259. File afile[] = file.listFiles();
  260. if(afile != null)
  261. {
  262. for(int i = 0; i < afile.length; i++)
  263. {
  264. String s2 = afile[i].getName();
  265. if(afile[i].isFile() && s2.startsWith("mod_") && s2.endsWith(".class"))
  266. addMod(classloader, s2);
  267. }
  268. }
  269. }
  270. }
  271. public static void zzz(Class<? extends Entity> entityClass, String entityName, int id)
  272. {
  273. try
  274. {
  275. method_RegisterEntityID.invoke(null, new Object[] { entityClass, entityName, Integer.valueOf(id) });
  276. }
  277. catch(IllegalArgumentException illegalargumentexception)
  278. {
  279. logger.throwing("ModLoader", "RegisterEntityID", illegalargumentexception);
  280. throwException(illegalargumentexception);
  281. }
  282. catch(IllegalAccessException illegalaccessexception)
  283. {
  284. logger.throwing("ModLoader", "RegisterEntityID", illegalaccessexception);
  285. throwException(illegalaccessexception);
  286. }
  287. catch(InvocationTargetException invocationtargetexception)
  288. {
  289. logger.throwing("ModLoader", "RegisterEntityID", invocationtargetexception);
  290. throwException(invocationtargetexception);
  291. }
  292. }
  293. public static <T, E> void zzy(Class<? super T> instanceClass, T instance, String field, E value)
  294. throws IllegalArgumentException, SecurityException, NoSuchFieldException
  295. {
  296. try
  297. {
  298. Field f = instanceClass.getDeclaredField(field);
  299. int i = modifiers.getInt(f);
  300. if((i & 0x10) != 0)
  301. {
  302. modifiers.setInt(f, i & 0xffffffef);
  303. }
  304. f.setAccessible(true);
  305. f.set(instance, value);
  306. }
  307. catch(IllegalAccessException illegalaccessexception)
  308. {
  309. logger.throwing("ModLoader", "setPrivateValue", illegalaccessexception);
  310. throwException("An impossible error has occured!", illegalaccessexception);
  311. }
  312. }
  313. public static void throwException(String s, Throwable throwable)
  314. {
  315. MinecraftServer.logger.warning(s);
  316. }
  317. private static void throwException(Throwable throwable)
  318. {
  319. throwException("Exception occured in ModLoader", throwable);
  320. }
  321. }