Skip to content

Commit 0e53bcb

Browse files
Initial code commit
1 parent 93ad116 commit 0e53bcb

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ExplodingAULib</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.release=disabled
12+
org.eclipse.jdt.core.compiler.source=1.8

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This repository contains a library able of identifying known programs to register them and allow them to be updated.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* ExplodingAU - The automatic update system for ExplodingBottle projects.
3+
* Copyright (C) 2023 ExplodingBottle
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package io.github.explodingbottle.explodingau;
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.math.BigInteger;
24+
import java.security.MessageDigest;
25+
import java.util.ArrayList;
26+
import java.util.Properties;
27+
28+
/**
29+
* This class contains utilities to register your application in the update
30+
* candidates list.
31+
*
32+
* @author ExplodingBottle
33+
*
34+
*/
35+
public class ExplodingAULib {
36+
37+
/**
38+
* Preventing to construct this class.
39+
*/
40+
private ExplodingAULib() {
41+
throw new IllegalAccessError("Cannot construct this class.");
42+
}
43+
44+
private static final String EXPLODING_AU_VERSION = "1.0.0.0";
45+
46+
/**
47+
* Computes the hash of a file.
48+
*
49+
* @param toHash The file to compute the hash.
50+
* @return The hash or {@code null} if it failed.
51+
*/
52+
public static String hashFile(File toHash) {
53+
try {
54+
MessageDigest md = MessageDigest.getInstance("SHA-256");
55+
FileInputStream toRead = new FileInputStream(toHash);
56+
byte[] buffer = new byte[4096];
57+
int read = toRead.read(buffer, 0, buffer.length);
58+
while (read != -1) {
59+
md.update(buffer, 0, read);
60+
read = toRead.read(buffer, 0, buffer.length);
61+
}
62+
toRead.close();
63+
return new BigInteger(md.digest()).toString(32);
64+
65+
} catch (Exception e) {
66+
67+
}
68+
return null;
69+
}
70+
71+
/**
72+
* Returns the AU folder root.
73+
*
74+
* @return The folder or null if error.
75+
*/
76+
public static File seekAUFolder() {
77+
File fold = new File(System.getProperty("user.home"), ".explodingau");
78+
if (!fold.exists()) {
79+
if (fold.mkdir()) {
80+
return fold;
81+
}
82+
} else {
83+
return fold;
84+
}
85+
return null;
86+
}
87+
88+
/**
89+
* Returns the loaded properties stored in the AU folder.
90+
*
91+
* @param auFolder A reference to the AU folder.
92+
* @return Returns the properties or null if failed.
93+
*/
94+
public static Properties loadPropsFromAUFolder(File auFolder) {
95+
Properties toRet = null;
96+
try {
97+
File auprops = new File(auFolder, "reglist.txt");
98+
if (auprops.exists()) {
99+
Properties okProps = new Properties();
100+
FileInputStream fis = new FileInputStream(auprops);
101+
okProps.loadFromXML(fis);
102+
fis.close();
103+
toRet = okProps;
104+
}
105+
} catch (Exception e) {
106+
107+
}
108+
return toRet;
109+
}
110+
111+
/**
112+
* Saves the properties.
113+
*
114+
* @param auFolder Where is the AU folder.
115+
* @param props What are the properties to save.
116+
* @return If we managed to save or not.
117+
*/
118+
public static boolean storePropsToAUFolder(File auFolder, Properties props) {
119+
try {
120+
File auprops = new File(auFolder, "reglist.txt");
121+
FileOutputStream fos = new FileOutputStream(auprops);
122+
props.storeToXML(fos, "ExplodingAU Version " + EXPLODING_AU_VERSION + " - Managed file");
123+
fos.close();
124+
return true;
125+
} catch (Exception e) {
126+
127+
}
128+
return false;
129+
}
130+
131+
public static void standardProgramRoutine(String programName) {
132+
File auFolder = seekAUFolder();
133+
if (auFolder != null) {
134+
Properties lk = loadPropsFromAUFolder(auFolder);
135+
if (lk == null) {
136+
lk = new Properties();
137+
}
138+
fileCfgRoutine(programName, lk);
139+
storePropsToAUFolder(auFolder, lk);
140+
}
141+
}
142+
143+
/**
144+
* Starts the routine to update the list.
145+
*
146+
* @param programName The program identifier.
147+
* @param props The loaded properties
148+
* @return If we managed to do the update.
149+
*/
150+
public static void fileCfgRoutine(String programName, Properties props) {
151+
152+
final Properties loaded = props;
153+
ArrayList<String> toRemove = new ArrayList<String>();
154+
loaded.forEach((k, v) -> {
155+
String key = (String) k;
156+
String val = (String) v;
157+
File loc = new File(key);
158+
if (!loc.exists() || loc.isDirectory()) {
159+
toRemove.add(key);
160+
} else {
161+
String[] split = val.split(";");
162+
if (split.length != 2) {
163+
toRemove.add(key);
164+
} else {
165+
String hash = split[1];
166+
String hashToCompare = hashFile(loc);
167+
if (hashToCompare != null) {
168+
if (!hash.equalsIgnoreCase(hashToCompare)) {
169+
toRemove.add(key);
170+
}
171+
}
172+
}
173+
}
174+
});
175+
if (programName != null) {
176+
try {
177+
File location = new File(
178+
ExplodingAULib.class.getProtectionDomain().getCodeSource().getLocation().toURI());
179+
if (location != null && !location.isDirectory()) {
180+
String fHash = hashFile(location);
181+
loaded.put(location.getAbsolutePath(), programName + ";" + fHash);
182+
}
183+
} catch (Exception e) {
184+
}
185+
}
186+
toRemove.forEach(trm -> {
187+
loaded.remove(trm);
188+
});
189+
190+
}
191+
192+
}

0 commit comments

Comments
 (0)