/com.github.wiiclipse.core/src/com/github/wiiclipse/core/WiiClipsePathResolver.java

https://github.com/wiiclipse/wiiclipse · Java · 207 lines · 172 code · 29 blank · 6 comment · 62 complexity · 72a56bd4d6d49cedbcc26b5d285fa4a0 MD5 · raw file

  1. package com.github.wiiclipse.core;
  2. import java.io.File;
  3. import org.eclipse.cdt.core.CCorePlugin;
  4. import org.eclipse.cdt.core.model.CoreModel;
  5. import org.eclipse.core.runtime.IPath;
  6. import org.eclipse.core.runtime.Path;
  7. import org.eclipse.core.runtime.Platform;
  8. public class WiiClipsePathResolver {
  9. private static boolean checked = false;
  10. private static IPath __devkitProPath = null;
  11. private static IPath __devkitPPCPath = null;
  12. private static IPath __devkitPPCBinPath = null;
  13. private static IPath __libOGCPath = null;
  14. private static IPath __libOGCInlcudePath = null;
  15. private static IPath __libOGCLibPath = null;
  16. private static final String WIN_DEVKITPRO_DEFAULT_PATH = "c:/devkitPro/";
  17. private static final String DEVKITPRO_DEFAULT_PATH = "/opt/devkitPro/";
  18. private static final String DEVKITPPC_DEFAULT_REL_PATH = "devkitPPC/";
  19. private static final boolean isWindows = Platform.OS_WIN32.equals(Platform
  20. .getOS());
  21. public static void resolvePaths() {
  22. if (checked)
  23. return;
  24. // locate devkitppc
  25. // check env
  26. IPath devkitPPCBasePath = getPathFromEnv("DEVKITPPC");
  27. IPath devkitProBasePath = getPathFromEnv("DEVKITPRO");
  28. if (devkitPPCBasePath != null) {
  29. if (getBinPath(devkitPPCBasePath) == null) {
  30. devkitPPCBasePath = null;
  31. }
  32. }
  33. if (devkitPPCBasePath == null) {
  34. // check for devkitpro variable
  35. if (devkitProBasePath == null) {
  36. // use default location
  37. if (isWindows) {
  38. devkitProBasePath = new Path(WIN_DEVKITPRO_DEFAULT_PATH);
  39. } else {
  40. devkitProBasePath = new Path(DEVKITPRO_DEFAULT_PATH);
  41. }
  42. }
  43. devkitPPCBasePath = devkitProBasePath
  44. .append(DEVKITPPC_DEFAULT_REL_PATH);
  45. }
  46. if (getBinPath(devkitPPCBasePath) != null) {
  47. __devkitPPCPath = devkitPPCBasePath;
  48. }
  49. if (devkitProBasePath == null) {
  50. File devkitPPCBase = devkitPPCBasePath.toFile();
  51. File parent = devkitPPCBase.getParentFile();
  52. if (parent.exists() && parent.isDirectory()) {
  53. devkitProBasePath = new Path(parent.getAbsolutePath());
  54. }
  55. }
  56. // libogc
  57. if (devkitProBasePath != null) {
  58. // assume libogc is installed into devkitpro folder
  59. IPath libOGCBasePath = devkitProBasePath.append("libogc");
  60. IPath libOGCLibPath = getLibPath(libOGCBasePath);
  61. IPath libOGCIncPath = getIncludePath(libOGCBasePath);
  62. if (getIncludePath(libOGCBasePath) != null
  63. && getLibPath(libOGCBasePath) != null) {
  64. __libOGCPath = libOGCBasePath;
  65. __libOGCLibPath = libOGCLibPath;
  66. __libOGCInlcudePath = libOGCIncPath;
  67. }
  68. }
  69. checked = true;
  70. }
  71. private static String fixPath(String path) {
  72. if (path != null && !path.isEmpty() && !path.endsWith("/")) {
  73. path = path + "/";
  74. }
  75. return path;
  76. }
  77. private static String fixWindowsPath(String path) {
  78. if (path != null && path.length() >= 3 && path.startsWith("/")) {
  79. char volume = path.charAt(1);
  80. path = volume + ":" + path.substring(2);
  81. path.replaceAll("\\", "/");
  82. }
  83. return path;
  84. }
  85. public static IPath getDevkitPPCBinPath() {
  86. if (!checked)
  87. resolvePaths();
  88. return __devkitPPCBinPath;
  89. }
  90. public static IPath getDevkitPPCPath() {
  91. if (!checked)
  92. resolvePaths();
  93. return __devkitPPCPath;
  94. }
  95. public static IPath getDevkitProPath() {
  96. if (!checked)
  97. resolvePaths();
  98. return __devkitProPath;
  99. }
  100. public static IPath getLibOGCInlcudePath() {
  101. if (!checked)
  102. resolvePaths();
  103. return __libOGCInlcudePath;
  104. }
  105. public static IPath getLibOGCLibPath() {
  106. if (!checked)
  107. resolvePaths();
  108. return __libOGCLibPath;
  109. }
  110. public static IPath getLibOGCPath() {
  111. if (!checked)
  112. resolvePaths();
  113. return __libOGCPath;
  114. }
  115. public static IPath getLibPath(IPath basePath) {
  116. IPath libPath = null;
  117. File f = basePath.toFile();
  118. if (f.exists() && f.isDirectory()) {
  119. File p = f.getParentFile();
  120. String name = f.getName();
  121. if (name.equalsIgnoreCase("wii")) {
  122. if (p != null && p.exists() && p.getName().equals("lib")) {
  123. libPath = basePath;
  124. }
  125. } else if (name.equalsIgnoreCase("lib")) {
  126. IPath childPath = basePath.append("wii");
  127. File cFile = childPath.toFile();
  128. if (cFile.exists() && cFile.isDirectory()) {
  129. libPath = childPath;
  130. } else {
  131. libPath = basePath;
  132. }
  133. } else {
  134. libPath = getLibPath(basePath.append("lib"));
  135. }
  136. }
  137. return libPath;
  138. }
  139. public static IPath getBinPath(IPath basePath) {
  140. IPath binPath = null;
  141. File f = basePath.toFile();
  142. if (f.exists() && f.isDirectory()) {
  143. String name = f.getName();
  144. if (name.equalsIgnoreCase("bin")) {
  145. binPath = basePath;
  146. } else {
  147. binPath = getBinPath(basePath.append("bin"));
  148. }
  149. }
  150. return binPath;
  151. }
  152. public static IPath getIncludePath(IPath basePath) {
  153. IPath incPath = null;
  154. File f = basePath.toFile();
  155. if (f.exists() && f.isDirectory()) {
  156. String name = f.getName();
  157. if (name.equalsIgnoreCase("include")) {
  158. incPath = basePath;
  159. } else {
  160. incPath = getIncludePath(basePath.append("include"));
  161. }
  162. }
  163. return incPath;
  164. }
  165. static IPath getPathFromEnv(String envVar) {
  166. IPath path = null;
  167. String value = System.getenv(envVar);
  168. if (value != null) {
  169. if (isWindows) {
  170. value = fixWindowsPath(value);
  171. } else {
  172. value = fixPath(value);
  173. }
  174. path = new Path(value);
  175. }
  176. return path;
  177. }
  178. }