repaired hash & added tests

This commit is contained in:
Szum123321 2022-12-27 13:31:23 +01:00
parent 97c607f9b2
commit 7d31e6710c
8 changed files with 73 additions and 78 deletions

View File

@ -17,17 +17,6 @@ repositories {
mavenCentral() mavenCentral()
} }
loom {
runs {
testServer {
server()
ideConfigGenerated project.rootProject == project
name = "Testmod Server"
source sourceSets.test
}
}
}
dependencies { dependencies {
//to change the versions see the gradle.properties file //to change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}" minecraft "com.mojang:minecraft:${project.minecraft_version}"

View File

@ -27,7 +27,6 @@ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.commands.create.CleanupCommand; import net.szum123321.textile_backup.commands.create.CleanupCommand;
import net.szum123321.textile_backup.commands.create.StartBackupCommand; import net.szum123321.textile_backup.commands.create.StartBackupCommand;
@ -43,11 +42,11 @@ import net.szum123321.textile_backup.core.ActionInitiator;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
import net.szum123321.textile_backup.core.create.BackupScheduler; import net.szum123321.textile_backup.core.create.BackupScheduler;
import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory; import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
import net.szum123321.textile_backup.test.BalticHashTest;
public class TextileBackup implements ModInitializer { public class TextileBackup implements ModInitializer {
public static final String MOD_NAME = "Textile Backup"; public static final String MOD_NAME = "Textile Backup";
public static final String MOD_ID = "textile_backup"; public static final String MOD_ID = "textile_backup";
public static final Version VERSION;
private final static TextileLogger log = new TextileLogger(MOD_NAME); private final static TextileLogger log = new TextileLogger(MOD_NAME);
private final static ConfigHelper config = ConfigHelper.INSTANCE; private final static ConfigHelper config = ConfigHelper.INSTANCE;
@ -62,6 +61,11 @@ public class TextileBackup implements ModInitializer {
log.info("Starting Textile Backup {} by Szum123321", Globals.INSTANCE.getCombinedVersionString()); log.info("Starting Textile Backup {} by Szum123321", Globals.INSTANCE.getCombinedVersionString());
if(FabricLoader.getInstance().isDevelopmentEnvironment()) {
//Run the tests
BalticHashTest.run();
}
ConfigHelper.updateInstance(AutoConfig.register(ConfigPOJO.class, JanksonConfigSerializer::new)); ConfigHelper.updateInstance(AutoConfig.register(ConfigPOJO.class, JanksonConfigSerializer::new));
ServerTickEvents.END_SERVER_TICK.register(BackupScheduler::tick); ServerTickEvents.END_SERVER_TICK.register(BackupScheduler::tick);
@ -114,9 +118,4 @@ public class TextileBackup implements ModInitializer {
.then(KillRestoreCommand.register()) .then(KillRestoreCommand.register())
)); ));
} }
static {
VERSION = FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata().getVersion();
FabricLoader.getInstance().getModContainer("minecraft").orElseThrow().getMetadata().getVersion();
}
} }

View File

@ -51,10 +51,10 @@ public class BalticHash implements Hash {
} }
public void update(byte[] data, int off, int len) { public void update(byte[] data, int off, int len) {
int pos = off; int pos = 0;
while(pos < len) { while(pos < len) {
int n = Math.min(len - pos, buffer_limit - buffer.position()); int n = Math.min(len - pos, buffer_limit - buffer.position());
System.arraycopy(data, pos, _byte_buffer, buffer.position(), n); System.arraycopy(data, off + pos, _byte_buffer, buffer.position(), n);
pos += n; pos += n;
buffer.position(buffer.position() + n); buffer.position(buffer.position() + n);
if(buffer.position() >= buffer_limit) round(); if(buffer.position() >= buffer_limit) round();

View File

@ -51,9 +51,10 @@ public class BalticHashSIMD extends BalticHash {/*
return xorshift64star(result); return xorshift64star(result);
} }
//This is wrong. will have to correct (
@Override @Override
public void update(byte[] data, int off, int len) { public void update(byte[] data, int off, int len) {
int pos = off; int pos = off; //should be = 0
while (pos < len) { while (pos < len) {
int n = Math.min(len - pos, buffer_limit - buffer.position()); int n = Math.min(len - pos, buffer_limit - buffer.position());
if (n == 32) { if (n == 32) {

View File

@ -0,0 +1,63 @@
/*
* A simple backup mod for Fabric
* Copyright (C) 2022 Szum123321
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.szum123321.textile_backup.test;
import net.minecraft.util.math.random.Random;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.TextileLogger;
import net.szum123321.textile_backup.core.digest.BalticHash;
public class BalticHashTest {
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
final static int TEST_LEN = 21377; //simple prime
public static void run() throws RuntimeException {
log.info("Running hash test");
Random r = Random.create(2137);
long x = 0;
byte[] data = new byte[TEST_LEN];
for(int i = 0; i < TEST_LEN; i++) data[i] = (byte)r.nextInt();
//Test block mode
for(int i = 0; i < 5*2; i++) x ^= randomHash(data, r);
if(x != 0) throw new RuntimeException("Hash mismatch!");
log.info("Test passed");
}
static long randomHash(byte[] data, Random r) {
int n = data.length;
BalticHash h = new BalticHash();
int m = r.nextBetween(1, n);
int nn = n, p = 0;
for(int i = 0; i < m; i++) {
int k = r.nextBetween(1, nn - (m - i - 1));
h.update(data, p, k);
p += k;
nn -= k;
}
return h.getValue();
}
}

View File

@ -1,10 +0,0 @@
package net.szum123321.test.textile_backup;
import net.fabricmc.api.ModInitializer;
public class TextileBackupTest implements ModInitializer {
@Override
public void onInitialize() {
}
}

View File

@ -1,35 +0,0 @@
{
"schemaVersion": 1,
"id": "textile_backup",
"version": "${version}",
"name": "Textile Backup Test",
"authors": [
"Szum123321"
],
"contact": {
"homepage": "https://www.curseforge.com/minecraft/mc-mods/textile-backup",
"issues": "https://github.com/Szum123321/textile_backup/issues",
"sources": "https://github.com/Szum123321/textile_backup"
},
"license": "GPLv3",
"environment": "*",
"entrypoints": {
"main": [
"net.szum123321.test.textile_backup.TextileBackupTest"
]
},
"mixins": [
],
"depends": {
"fabricloader": ">=0.14.6",
"fabric": "*",
"minecraft": ">=1.19.1",
"cloth-config2": "*",
"java": ">=16",
"textile_backup": "*"
}
}

View File

@ -1,12 +0,0 @@
{
"required": true,
"package": "net.szum123321.test.textile_backup.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}