ConanServerManager/lib/structs/mod.dart
zontreck e2d64a2a9d Add a comment field to mods
ADDTL; Edited the deserialization routine for mods to make it less prone to errors in the event a mod does not contain every field in the NBT data, ex. new comment field would have crashed the deserializer, causing it to skip past the mod or fully reset server settings.
2025-01-04 17:25:51 -07:00

76 lines
2 KiB
Dart

import 'package:libac_dart/nbt/NbtUtils.dart';
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
import 'package:libac_dart/nbt/impl/LongTag.dart';
import 'package:libac_dart/nbt/impl/StringTag.dart';
import 'package:libac_dart/utils/uuid/UUID.dart';
class Mod {
String mod_name = "";
int mod_id = 0;
String mod_pak = "";
String mod_hash = "";
bool enabled = true;
String comment = "";
bool newMod = false;
UUID _id = UUID.ZERO;
String mod_instance_id() {
if (_id.toString() == UUID.ZERO.toString()) {
_id = UUID.generate(4);
}
return _id.toString();
}
Mod(
{this.mod_name = "undef",
this.mod_id = 0,
this.newMod = false,
this.mod_pak = "Not Initialized",
this.mod_hash = "",
this.enabled = true,
this.comment = ""});
CompoundTag serialize() {
CompoundTag tag = CompoundTag();
tag.put("name", StringTag.valueOf(mod_name));
tag.put("id", LongTag.valueOf(mod_id));
tag.put("pak", StringTag.valueOf(mod_pak));
tag.put("hash", StringTag.valueOf(mod_hash));
tag.put("comment", StringTag.valueOf(comment));
NbtUtils.writeBoolean(tag, "enabled", enabled);
return tag;
}
static Mod deserialize(CompoundTag tag) {
CompoundTag ct = tag;
String modName = "undef";
if (ct.containsKey("name")) modName = ct.get("name")!.asString();
int modID = 0;
if (ct.containsKey("id")) modID = ct.get("id")!.asLong();
String pakFile = "Not yet initialized";
if (ct.containsKey("pak")) pakFile = ct.get("pak")!.asString();
String hash = "1234ABCD";
if (ct.containsKey("hash")) hash = ct.get("hash")!.asString();
bool enabled = true;
if (ct.containsKey("enabled"))
enabled = NbtUtils.readBoolean(tag, "enabled");
String comment = "";
if (ct.containsKey("comment")) comment = ct.get("comment")!.asString();
return Mod(
mod_name: modName,
mod_id: modID,
mod_pak: pakFile,
mod_hash: hash,
enabled: enabled,
comment: comment);
}
}