refacto + recovery code from uncommited on my old pc

This commit is contained in:
marc barbier
2021-07-07 21:45:33 +02:00
parent 2a6b802bb0
commit 7c88e6cf2f
12 changed files with 496 additions and 410 deletions
+81 -104
View File
@@ -11,150 +11,127 @@ import java.util.TreeMap;
import java.util.zip.ZipFile;
public class IntegrityChecker {
public String[] fileList;
public SortedMap<Integer, String> installedMods;
public File modsMap;
public String[] fileList = (new File("mods")).list();
@SuppressWarnings("unchecked")
public IntegrityChecker()
{
fileList = new File("mods").list();
modsMap = new File("mods/modsMap.sav");
if(modsMap.exists())
{
public SortedMap<Integer, String> installedMods;
public File modsMap = new File("mods/modsMap.sav");
public IntegrityChecker() {
if (this.modsMap.exists()) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modsMap));
installedMods = (SortedMap<Integer, String>) ois.readObject();
if(installedMods == null)installedMods = new TreeMap<>();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(this.modsMap));
this.installedMods = (SortedMap<Integer, String>)ois.readObject();
if (this.installedMods == null)
this.installedMods = new TreeMap<>();
ois.close();
} catch (Exception e) {
installedMods = new TreeMap<>();
modsMap.delete();
}
}else {
this.installedMods = new TreeMap<>();
this.modsMap.delete();
}
} else {
try {
modsMap.createNewFile();
this.modsMap.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modsMap));
oos.writeObject(installedMods);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.modsMap));
oos.writeObject(this.installedMods);
oos.close();
} catch (IOException e) {
Log.e("SAV", "erreur pas de sauvegarde");
System.exit(1);
}
}
}
public boolean checkAndDelete(File modToDownload, int modid) {
String alredyInstalledName = installedMods.get(modid).toLowerCase();
File alredyInstalledMods= null;
//on cherche le fichier local affin de ne pas voir d'erreur avec la casse
String alredyInstalledName = ((String)this.installedMods.get(Integer.valueOf(modid))).toLowerCase();
File alredyInstalledMods = null;
File dir = new File("mods/");
for(String fileName : dir.list())
{
if(fileName.equalsIgnoreCase(alredyInstalledName))
{
alredyInstalledMods = new File("mods/"+fileName);
}
}
if(alredyInstalledMods == null) {
Log.e("IntegrityChecker", "alredy installed mod not found ("+alredyInstalledName+")");
installedMods.remove(modid);
byte b;
int i;
String[] arrayOfString;
for (i = (arrayOfString = dir.list()).length, b = 0; b < i; ) {
String fileName = arrayOfString[b];
if (fileName.equalsIgnoreCase(alredyInstalledName))
alredyInstalledMods = new File("mods/" + fileName);
b++;
}
if (alredyInstalledMods == null) {
Log.e("IntegrityChecker", "alredy installed mod not found (" + alredyInstalledName + ")");
this.installedMods.remove(Integer.valueOf(modid));
return false;
}
}
if (alredyInstalledMods.exists()) {
try {
//si sa balance une exception sa veut dire que le jar n'est pas lisible
new ZipFile(alredyInstalledMods).close();
(new ZipFile(alredyInstalledMods)).close();
} catch (IOException e) {
Log.i("integrityChecker","Corruption detected redownloading");
Log.i("integrityChecker", "Corruption detected redownloading");
alredyInstalledMods.delete();
installedMods.remove(modid);
this.installedMods.remove(Integer.valueOf(modid));
updateFile();
return false;
}
if(!modToDownload.getAbsolutePath().equalsIgnoreCase(alredyInstalledMods.getAbsolutePath()))
{
}
if (!modToDownload.getAbsolutePath().equalsIgnoreCase(alredyInstalledMods.getAbsolutePath())) {
alredyInstalledMods.delete();
Log.i("integrityChecker","Outdated mod detected redownloading ("+alredyInstalledName+" -> "+modToDownload.getName()+")");
installedMods.remove(modid);
Log.i("integrityChecker", "Outdated mod detected redownloading (" + alredyInstalledName + " -> " + modToDownload.getName() + ")");
this.installedMods.remove(Integer.valueOf(modid));
updateFile();
return false;
}
}
return true;
}else {
if(modToDownload.exists())
{
installedMods.put(modid, modToDownload.getName());
updateFile();
Log.i("interessting", modToDownload.getName());
}
}
}
if (modToDownload.exists()) {
this.installedMods.put(Integer.valueOf(modid), modToDownload.getName());
updateFile();
Log.i("interessting", modToDownload.getName());
}
return false;
}
public boolean checkAndDelete(File modToDownload, String slug) {
if (modToDownload.exists()) {
try {
//si sa balance une exception sa veut dire que le jar n'est pas lisible
new ZipFile(modToDownload).close();
} catch (IOException e) {
Log.i("integrityChecker","Corruption detected redownloading");
modToDownload.delete();
return false;
}
for (String s : fileList) {
if (s.contains(slug)) {
if (!modToDownload.getName().contains(s)) {
new File(s).delete();
Log.i("integrityChecker","Corruption detected redownloading");
return false;
}
return true;
}
}
if (modToDownload.exists())
try {
(new ZipFile(modToDownload)).close();
return true;
} catch (IOException e) {
Log.i("integrityChecker", "Corruption detected redownloading");
modToDownload.delete();
return false;
}
}
byte b;
int i;
String[] arrayOfString;
for (i = (arrayOfString = this.fileList).length, b = 0; b < i; ) {
String s = arrayOfString[b];
if (s.toLowerCase().contains(slug.trim())) {
if (modToDownload.getName().trim().equalsIgnoreCase(s.trim()))
return true;
(new File(s)).delete();
break;
}
b++;
}
return false;
}
/**
* un chemin d'acces ne marche pas
* @param fileName
* @param modID
*/
public void addModsToTheList(String fileName,int modID)
{
installedMods.put(modID, fileName);
updateFile();
public void addModsToTheList(String fileName, int modID) {
this.installedMods.put(Integer.valueOf(modID), fileName);
Main.changes.set(Main.changes.get() + 1);
}
public boolean isModIdKnown(int modId)
{
return installedMods.get(modId) != null;
public boolean isModIdKnown(int modId) {
return (this.installedMods.get(Integer.valueOf(modId)) != null);
}
private void updateFile()
{
public void updateFile() {
try {
modsMap.delete();
//modsMap.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modsMap));
oos.writeObject(installedMods);
this.modsMap.delete();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.modsMap));
oos.writeObject(this.installedMods);
oos.close();
} catch (IOException e) {}
} catch (IOException iOException) {}
}
}