1 | package net.oni2.aeinstaller.backend.oni.management;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.TreeSet;
|
---|
9 |
|
---|
10 | import net.oni2.aeinstaller.backend.CaseInsensitiveFile;
|
---|
11 | import net.oni2.aeinstaller.backend.Paths;
|
---|
12 | import net.oni2.aeinstaller.backend.packages.Package;
|
---|
13 | import net.oni2.platformtools.PlatformInformation;
|
---|
14 | import net.oni2.platformtools.PlatformInformation.Platform;
|
---|
15 |
|
---|
16 | import org.apache.commons.io.FileUtils;
|
---|
17 |
|
---|
18 | import com.thoughtworks.xstream.XStream;
|
---|
19 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @author Christian Illy
|
---|
23 | */
|
---|
24 | public class ToolsManager {
|
---|
25 | /**
|
---|
26 | * @return Currently installed tools
|
---|
27 | */
|
---|
28 | @SuppressWarnings("unchecked")
|
---|
29 | public static TreeSet<Integer> getInstalledTools() {
|
---|
30 | File installCfg = new File(Paths.getInstallerPath(),
|
---|
31 | "installed_tools.xml");
|
---|
32 | TreeSet<Integer> res = new TreeSet<Integer>();
|
---|
33 | try {
|
---|
34 | if (installCfg.exists()) {
|
---|
35 | FileInputStream fis = new FileInputStream(installCfg);
|
---|
36 | XStream xs = new XStream(new StaxDriver());
|
---|
37 | Object obj = xs.fromXML(fis);
|
---|
38 | if (obj instanceof TreeSet<?>)
|
---|
39 | res = (TreeSet<Integer>) obj;
|
---|
40 | fis.close();
|
---|
41 | }
|
---|
42 | } catch (FileNotFoundException e) {
|
---|
43 | e.printStackTrace();
|
---|
44 | } catch (IOException e) {
|
---|
45 | e.printStackTrace();
|
---|
46 | }
|
---|
47 | return res;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static void writeInstalledTools(TreeSet<Integer> tools) {
|
---|
51 | File installCfg = new File(Paths.getInstallerPath(),
|
---|
52 | "installed_tools.xml");
|
---|
53 | try {
|
---|
54 | FileOutputStream fos = new FileOutputStream(installCfg);
|
---|
55 | XStream xs = new XStream(new StaxDriver());
|
---|
56 | xs.toXML(tools, fos);
|
---|
57 | fos.close();
|
---|
58 | } catch (FileNotFoundException e) {
|
---|
59 | e.printStackTrace();
|
---|
60 | } catch (IOException e) {
|
---|
61 | e.printStackTrace();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @param tools
|
---|
67 | * Tools to (un)install
|
---|
68 | * @param uninstall
|
---|
69 | * Uninstall tools or install?
|
---|
70 | */
|
---|
71 | public static void installTools(TreeSet<Package> tools, boolean uninstall) {
|
---|
72 | TreeSet<Integer> installed = getInstalledTools();
|
---|
73 | for (Package m : tools) {
|
---|
74 | if (!uninstall || installed.contains(m.getPackageNumber())) {
|
---|
75 | File plain = CaseInsensitiveFile.getCaseInsensitiveFile(
|
---|
76 | m.getLocalPath(), "plain");
|
---|
77 | if (plain.exists()) {
|
---|
78 | if (m.hasSeparatePlatformDirs()) {
|
---|
79 | File plainCommon = CaseInsensitiveFile
|
---|
80 | .getCaseInsensitiveFile(plain, "common");
|
---|
81 | File plainMac = CaseInsensitiveFile
|
---|
82 | .getCaseInsensitiveFile(plain, "mac_only");
|
---|
83 | File plainWin = CaseInsensitiveFile
|
---|
84 | .getCaseInsensitiveFile(plain, "win_only");
|
---|
85 | if (plainCommon.exists())
|
---|
86 | copyRemoveToolsFiles(plainCommon,
|
---|
87 | Paths.getEditionBasePath(), uninstall);
|
---|
88 | if (PlatformInformation.getPlatform() == Platform.MACOS
|
---|
89 | && plainMac.exists())
|
---|
90 | copyRemoveToolsFiles(plainMac,
|
---|
91 | Paths.getEditionBasePath(), uninstall);
|
---|
92 | else if (plainWin.exists())
|
---|
93 | copyRemoveToolsFiles(plainWin,
|
---|
94 | Paths.getEditionBasePath(), uninstall);
|
---|
95 | } else {
|
---|
96 | copyRemoveToolsFiles(plain, Paths.getEditionBasePath(),
|
---|
97 | uninstall);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (uninstall)
|
---|
102 | installed.remove(m.getPackageNumber());
|
---|
103 | else
|
---|
104 | installed.add(m.getPackageNumber());
|
---|
105 | }
|
---|
106 | writeInstalledTools(installed);
|
---|
107 | }
|
---|
108 |
|
---|
109 | private static void copyRemoveToolsFiles(File srcFolder, File targetFolder,
|
---|
110 | boolean remove) {
|
---|
111 | for (File f : srcFolder.listFiles()) {
|
---|
112 | try {
|
---|
113 | if (f.isDirectory())
|
---|
114 | copyRemoveToolsFiles(f,
|
---|
115 | CaseInsensitiveFile.getCaseInsensitiveFile(
|
---|
116 | targetFolder, f.getName()), remove);
|
---|
117 | else {
|
---|
118 | File targetFile = CaseInsensitiveFile
|
---|
119 | .getCaseInsensitiveFile(targetFolder, f.getName());
|
---|
120 | if (remove) {
|
---|
121 | if (targetFile.exists())
|
---|
122 | targetFile.delete();
|
---|
123 | } else {
|
---|
124 | if (!targetFile.getName().equals(f.getName()))
|
---|
125 | targetFile.delete();
|
---|
126 | FileUtils.copyFileToDirectory(f, targetFolder);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } catch (IOException e) {
|
---|
130 | e.printStackTrace();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | if (remove)
|
---|
134 | if (targetFolder.list().length == 0)
|
---|
135 | targetFolder.delete();
|
---|
136 | }
|
---|
137 |
|
---|
138 | }
|
---|