| 1 |
package net.oni2.aeinstaller.backend.oni; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.Vector; |
| 6 |
|
| 7 |
import net.oni2.aeinstaller.backend.CaseInsensitiveFile; |
| 8 |
import net.oni2.aeinstaller.backend.DotNet; |
| 9 |
import net.oni2.aeinstaller.backend.Paths; |
| 10 |
import net.oni2.aeinstaller.backend.Settings; |
| 11 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
| 12 |
import net.oni2.aeinstaller.backend.appexecution.AppExecution; |
| 13 |
import net.oni2.aeinstaller.backend.appexecution.AppExecutionResult; |
| 14 |
|
| 15 |
/** |
| 16 |
* @author Christian Illy |
| 17 |
*/ |
| 18 |
public class OniSplit { |
| 19 |
|
| 20 |
/** |
| 21 |
* @return Is Onisplit installed? |
| 22 |
*/ |
| 23 |
public static boolean isOniSplitInstalled() { |
| 24 |
return getProgramFile().exists(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Export given dat-file to target folder |
| 29 |
* |
| 30 |
* @param targetFolder |
| 31 |
* Target folder |
| 32 |
* @param input |
| 33 |
* Dat file |
| 34 |
* @return OniSplit output |
| 35 |
*/ |
| 36 |
public static AppExecutionResult export(File targetFolder, File input) { |
| 37 |
return export(targetFolder, input, null); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Export named resources from given dat-file to target folder |
| 42 |
* |
| 43 |
* @param targetFolder |
| 44 |
* Target folder |
| 45 |
* @param input |
| 46 |
* Dat file |
| 47 |
* @param patterns |
| 48 |
* Filename patterns for files to export |
| 49 |
* @return OniSplit output |
| 50 |
*/ |
| 51 |
public static AppExecutionResult export(File targetFolder, File input, |
| 52 |
Vector<String> patterns) { |
| 53 |
if (!targetFolder.exists()) |
| 54 |
targetFolder.mkdir(); |
| 55 |
|
| 56 |
Vector<String> cmdLine = getProgramInvocation(); |
| 57 |
if (patterns == null) |
| 58 |
cmdLine.add("-export"); |
| 59 |
else { |
| 60 |
for (String p : patterns) |
| 61 |
cmdLine.add("-export:" + p); |
| 62 |
} |
| 63 |
cmdLine.add(targetFolder.getPath()); |
| 64 |
cmdLine.add(input.getPath()); |
| 65 |
AppExecutionResult res = null; |
| 66 |
try { |
| 67 |
res = AppExecution.executeAndWait(cmdLine); |
| 68 |
} catch (IOException e) { |
| 69 |
e.printStackTrace(); |
| 70 |
} |
| 71 |
return res; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Import given folder to a .dat-file |
| 76 |
* |
| 77 |
* @param sourceFolders |
| 78 |
* Folders containing .oni-files |
| 79 |
* @param targetFile |
| 80 |
* Target .dat-file |
| 81 |
* @return OniSplit output |
| 82 |
*/ |
| 83 |
public static AppExecutionResult importLevel(Vector<File> sourceFolders, |
| 84 |
File targetFile) { |
| 85 |
Vector<String> cmdLine = getProgramInvocation(); |
| 86 |
cmdLine.add(getImportParam()); |
| 87 |
for (File f : sourceFolders) |
| 88 |
cmdLine.add(f.getPath()); |
| 89 |
cmdLine.add(targetFile.getPath()); |
| 90 |
AppExecutionResult res = null; |
| 91 |
try { |
| 92 |
res = AppExecution.executeAndWait(cmdLine); |
| 93 |
} catch (IOException e) { |
| 94 |
e.printStackTrace(); |
| 95 |
} |
| 96 |
return res; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Pack a level to a .dat-file. More powerful variant which allows |
| 101 |
* specifying single .oni/.dat files |
| 102 |
* |
| 103 |
* @param sourceFoldersFiles |
| 104 |
* Folders (for recursive .oni import) or files (.dat and single |
| 105 |
* .oni) to import |
| 106 |
* @param targetFile |
| 107 |
* Target .dat-file |
| 108 |
* @return OniSplit output |
| 109 |
*/ |
| 110 |
public static AppExecutionResult packLevel(Vector<File> sourceFoldersFiles, |
| 111 |
File targetFile) { |
| 112 |
Vector<String> cmdLine = getProgramInvocation(); |
| 113 |
cmdLine.add(getPackParam()); |
| 114 |
cmdLine.add(getPackTypeParam()); |
| 115 |
cmdLine.add("-out"); |
| 116 |
cmdLine.add(targetFile.getPath()); |
| 117 |
for (File f : sourceFoldersFiles) |
| 118 |
cmdLine.add(f.getPath()); |
| 119 |
AppExecutionResult res = null; |
| 120 |
try { |
| 121 |
res = AppExecution.executeAndWait(cmdLine); |
| 122 |
} catch (IOException e) { |
| 123 |
e.printStackTrace(); |
| 124 |
} |
| 125 |
return res; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Move files from one location to another using OniSplit so relations are |
| 130 |
* handled |
| 131 |
* |
| 132 |
* @param targetFolder |
| 133 |
* Target folder for files |
| 134 |
* @param input |
| 135 |
* Files to move, can contain wildcards |
| 136 |
* @param moveParameter |
| 137 |
* e.g. overwrite, delete |
| 138 |
* @return OniSplit output |
| 139 |
*/ |
| 140 |
public static AppExecutionResult move(File targetFolder, String input, |
| 141 |
String moveParameter) { |
| 142 |
if (!targetFolder.exists()) |
| 143 |
targetFolder.mkdir(); |
| 144 |
|
| 145 |
Vector<String> cmdLine = getProgramInvocation(); |
| 146 |
cmdLine.add("-move" |
| 147 |
+ (moveParameter != null ? ":" + moveParameter : "")); |
| 148 |
cmdLine.add(targetFolder.getPath()); |
| 149 |
cmdLine.add(input); |
| 150 |
AppExecutionResult res = null; |
| 151 |
try { |
| 152 |
res = AppExecution.executeAndWait(cmdLine); |
| 153 |
} catch (IOException e) { |
| 154 |
e.printStackTrace(); |
| 155 |
} |
| 156 |
return res; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Convert given .oni-files to XML and put them in target folder |
| 161 |
* |
| 162 |
* @param targetFolder |
| 163 |
* Target folder |
| 164 |
* @param inputFiles |
| 165 |
* .oni files |
| 166 |
* @return OniSplit output |
| 167 |
*/ |
| 168 |
public static AppExecutionResult convertOniToXML(File targetFolder, |
| 169 |
Vector<File> inputFiles) { |
| 170 |
if (!targetFolder.exists()) |
| 171 |
targetFolder.mkdirs(); |
| 172 |
|
| 173 |
Vector<String> cmdLine = getProgramInvocation(); |
| 174 |
cmdLine.add("-extract:xml"); |
| 175 |
cmdLine.add(targetFolder.getPath()); |
| 176 |
for (File f : inputFiles) { |
| 177 |
cmdLine.add(f.getPath()); |
| 178 |
} |
| 179 |
AppExecutionResult res = null; |
| 180 |
try { |
| 181 |
res = AppExecution.executeAndWait(cmdLine); |
| 182 |
} catch (IOException e) { |
| 183 |
e.printStackTrace(); |
| 184 |
} |
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Convert given XML-files to .oni and put them in target folder |
| 190 |
* |
| 191 |
* @param targetFolder |
| 192 |
* Target folder |
| 193 |
* @param inputFiles |
| 194 |
* XML-files |
| 195 |
* @return OniSplit output |
| 196 |
*/ |
| 197 |
public static AppExecutionResult convertXMLtoOni(File targetFolder, |
| 198 |
Vector<File> inputFiles) { |
| 199 |
if (!targetFolder.exists()) |
| 200 |
targetFolder.mkdirs(); |
| 201 |
|
| 202 |
Vector<String> cmdLine = getProgramInvocation(); |
| 203 |
cmdLine.add("-create"); |
| 204 |
cmdLine.add(targetFolder.getPath()); |
| 205 |
for (File f : inputFiles) { |
| 206 |
cmdLine.add(f.getPath()); |
| 207 |
} |
| 208 |
AppExecutionResult res = null; |
| 209 |
try { |
| 210 |
res = AppExecution.executeAndWait(cmdLine); |
| 211 |
} catch (IOException e) { |
| 212 |
e.printStackTrace(); |
| 213 |
} |
| 214 |
return res; |
| 215 |
} |
| 216 |
|
| 217 |
private static String getImportParam() { |
| 218 |
if (Settings.getPlatform() == Platform.MACOS) |
| 219 |
return "-import:sep"; |
| 220 |
else |
| 221 |
return "-import:nosep"; |
| 222 |
} |
| 223 |
|
| 224 |
private static String getPackParam() { |
| 225 |
return "pack"; |
| 226 |
} |
| 227 |
|
| 228 |
private static String getPackTypeParam() { |
| 229 |
if (Settings.getPlatform() == Platform.MACOS) |
| 230 |
return "-type:macintel"; |
| 231 |
else |
| 232 |
return "-type:pc"; |
| 233 |
} |
| 234 |
|
| 235 |
private static Vector<String> getProgramInvocation() { |
| 236 |
Vector<String> res = new Vector<String>(); |
| 237 |
if (DotNet.getRuntimeExe().length() > 0) |
| 238 |
res.add(DotNet.getRuntimeExe()); |
| 239 |
res.add(getProgramFile().getPath()); |
| 240 |
return res; |
| 241 |
} |
| 242 |
|
| 243 |
private static File getProgramFile() { |
| 244 |
File toolsPath = CaseInsensitiveFile.getCaseInsensitiveFile( |
| 245 |
Paths.getEditionBasePath(), "Tools"); |
| 246 |
return CaseInsensitiveFile.getCaseInsensitiveFile(toolsPath, |
| 247 |
"Onisplit.exe"); |
| 248 |
} |
| 249 |
} |