Update to water shot with addict code

This commit is contained in:
Willem Cazander 2022-02-23 17:11:05 +01:00
parent a05493c56c
commit 0c906b701f
8 changed files with 142 additions and 43 deletions

View file

@ -1,26 +0,0 @@
package love.distributedrebirth.warpme.hash;
import java.util.ArrayList;
import java.util.List;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterDrugs {
private List<WaterFileHash> fileHashes = new ArrayList<>();
public List<WaterFileHash> getFileHashes() {
return fileHashes;
}
public void setFileHashes(List<WaterFileHash> fileHashes) {
for (WaterFileHash hash:fileHashes) {
addFileHash(hash);
}
}
public void addFileHash(WaterFileHash fileHash) {
this.fileHashes.add(fileHash);
}
}

View file

@ -0,0 +1,26 @@
package love.distributedrebirth.warpme.hash;
import java.util.ArrayList;
import java.util.List;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterShot {
private List<WaterShotHash> shotHashes = new ArrayList<>();
public List<WaterShotHash> getShotHashes() {
return shotHashes;
}
public void setShotHashes(List<WaterShotHash> shotHashes) {
for (WaterShotHash hash:shotHashes) {
addShotHash(hash);
}
}
public void addShotHash(WaterShotHash shotHash) {
this.shotHashes.add(shotHash);
}
}

View file

@ -0,0 +1,65 @@
package love.distributedrebirth.warpme.hash;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.x4o.xml.io.X4OConnectionException;
import org.xml.sax.SAXException;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
import love.distributedrebirth.warpme.Warpᵐᵉ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterShotAddict {
public boolean validateWarpChainLink(File folder) throws FileNotFoundException, X4OConnectionException, SAXException, IOException, NoSuchAlgorithmException {
File fileWarpHash = new File(folder, Warpᵐᵉ.WARP_HASH);
if (!fileWarpHash.exists()) {
return false;
}
WaterShot waterShot = WaterShotDriver.newInstance().createReader().readFile(fileWarpHash);
for (WaterShotHash hash:waterShot.getShotHashes()) {
File checkFile = new File(folder, hash.getFile());
if (!checkFile.exists()) {
return false;
}
String fileHex = calcHashInHex(checkFile);
if (!fileHex.equals(hash.getHex())) {
return false;
}
}
return true;
}
public void updateWarpHashes(File folder) throws FileNotFoundException, X4OConnectionException, SAXException, IOException, NoSuchAlgorithmException {
File fileWarpHash = new File(folder, Warpᵐᵉ.WARP_HASH);
WaterShot waterShot = WaterShotDriver.newInstance().createReader().readFile(fileWarpHash);
for (WaterShotHash hash:waterShot.getShotHashes()) {
hash.setHex(calcHashInHex(new File(folder, hash.getFile())));
}
WaterShotDriver.newInstance().createWriter().writeFile(waterShot, fileWarpHash);
}
private String calcHashInHex(File file) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] bytes = Files.readAllBytes(file.toPath());
byte[] encodedhash = digest.digest(bytes);
return bytesToHex(encodedhash);
}
private String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}

View file

@ -6,7 +6,7 @@ import org.x4o.xml.X4ODriverManager;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterDrugsDriver extends X4ODriver<WaterDrugs> {
public class WaterShotDriver extends X4ODriver<WaterShot> {
static final public String LANGUAGE_NAME = "warp-hash";
static final public String[] LANGUAGE_VERSIONS = new String[]{X4ODriver.DEFAULT_LANGUAGE_VERSION};
@ -21,7 +21,7 @@ public class WaterDrugsDriver extends X4ODriver<WaterDrugs> {
return LANGUAGE_VERSIONS;
}
static public WaterDrugsDriver newInstance() {
return (WaterDrugsDriver)X4ODriverManager.getX4ODriver(LANGUAGE_NAME);
static public WaterShotDriver newInstance() {
return (WaterShotDriver)X4ODriverManager.getX4ODriver(LANGUAGE_NAME);
}
}

View file

@ -3,10 +3,18 @@ package love.distributedrebirth.warpme.hash;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterFileHash {
public class WaterShotHash {
private String file;
private String hash;
private String hex;
public WaterShotHash() {
}
public WaterShotHash(String file, String hex) {
setFile(file);
setHex(hex);
}
public String getFile() {
return file;
@ -16,11 +24,11 @@ public class WaterFileHash {
this.file = file;
}
public String getHash() {
return hash;
public String getHex() {
return hex;
}
public void setHash(String hash) {
this.hash = hash;
public void setHex(String hex) {
this.hex = hex;
}
}

View file

@ -7,10 +7,10 @@
providerHost="warp-hash.x4o.distributedrebirth.love" providerName="WarpHashᵐᵉ"
id="warp-hash-module">
<eld:classBindingHandler id="WaterDrugs-WaterFileHash"
parentClass="love.distributedrebirth.warpme.hash.WaterDrugs"
childClass="love.distributedrebirth.warpme.hash.WaterFileHash"
addMethod="addFileHash" getMethod="getFileHashes" />
<eld:classBindingHandler id="WaterShot-WaterShotHash"
parentClass="love.distributedrebirth.warpme.hash.WaterShot"
childClass="love.distributedrebirth.warpme.hash.WaterShotHash"
addMethod="addShotHash" getMethod="getShotHashes" />
<eld:namespace
uri="http://wrap-core.x4o.distributedrebirth.love/xml/ns/warp-hash-root"
@ -18,8 +18,8 @@
schemaResource="warp-hash-root-1.0.xsd" schemaPrefix="root"
description="Root namespace to have nice namespaceing."
name="WarpHash Root Namespace" languageRoot="true" id="root">
<eld:element tag="drugs"
objectClass="love.distributedrebirth.warpme.hash.WaterDrugs"/>
<eld:element tag="shot"
objectClass="love.distributedrebirth.warpme.hash.WaterShot"/>
</eld:namespace>
<eld:namespace
@ -29,6 +29,6 @@
description="Language namespace to store warp hash"
name="WarpHash Language Namespace" id="soft">
<eld:element tag="hash"
objectClass="love.distributedrebirth.warpme.hash.WaterFileHash" />
objectClass="love.distributedrebirth.warpme.hash.WaterShotHash" />
</eld:namespace>
</root:module>

View file

@ -5,6 +5,6 @@
xsi:schemaLocation="http://language.x4o.org/xml/ns/drivers http://language.x4o.org/xml/ns/drivers-1.0.xsd"
>
<driver language="warp-core" className="love.distributedrebirth.warpme.core.WaterBucketDriver"/>
<driver language="warp-hash" className="love.distributedrebirth.warpme.hash.WaterDrugsDriver"/>
<driver language="warp-hash" className="love.distributedrebirth.warpme.hash.WaterShotDriver"/>
<driver language="warp-sea" className="love.distributedrebirth.warpme.sea.WaterOceanDriver"/>
</drivers>

View file

@ -0,0 +1,26 @@
package love.distributedrebirth.warpme.hash;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.x4o.xml.io.X4OConnectionException;
import org.xml.sax.SAXException;
import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ;
@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天")
public class WaterShotDriverTest {
@Test
public void testWaterShot() throws X4OConnectionException, SAXException, IOException {
WaterShot drugs = new WaterShot();
drugs.addShotHash(new WaterShotHash("test.txt", "234SDFSDF234234SDFSDF234"));
drugs.addShotHash(new WaterShotHash("test2.txt", "ASDFYYF234234SDFSDFASDFS"));
Assertions.assertNotNull(drugs);
//WaterShotDriver.newInstance().createWriter().writeFile(drugs, new File("build/output.xml"));
}
}