diff --git a/tools/test_upgrade_datapack_26.py b/tools/test_upgrade_datapack_26.py new file mode 100644 index 00000000..32cf1331 --- /dev/null +++ b/tools/test_upgrade_datapack_26.py @@ -0,0 +1,47 @@ +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +import zipfile + +spec = importlib.util.spec_from_file_location('upgrade', Path(__file__).with_name('upgrade-datapack-26.py')) +upgrade = importlib.util.module_from_spec(spec) +spec.loader.exec_module(upgrade) + + +class PackUpgradeTests(unittest.TestCase): + def test_keeps_planet_features_and_empty_spawn_categories(self): + source = {'features': [['minecraft:ore_coal_upper']], 'temperature': -0.5, + 'effects': {'sky_color': 8364543, 'water_color': 4159204}, + 'spawners': {'creature': [], 'monster': [ + {'type': 'minecraft:stray', 'minCount': 1, 'maxCount': 4, 'weight': 80}]}, 'spawn_costs': {}} + result = upgrade.convert_biome(source) + self.assertEqual(source['effects']['water_color'], 4159204) + self.assertEqual(result['features'], source['features']) + spawns = result['attributes']['minecraft:gameplay/natural_mob_spawns']['argument']['spawns_by_category'] + self.assertEqual(spawns['creature'], []) + self.assertEqual(spawns['monster'][0]['count']['max_inclusive'], 4) + self.assertEqual(result['effects']['water_color'], '#3f76e4') + self.assertNotIn('spawners', result) + self.assertEqual(upgrade.convert_biome(result), result) + + def test_unrelated_bytes_and_original_archive_are_preserved(self): + with tempfile.TemporaryDirectory() as tmp: + source, output = Path(tmp) / 'old.zip', Path(tmp) / 'new.zip' + payload = b'original structure data\x00\xff' + with zipfile.ZipFile(source, 'w') as z: + z.writestr('pack.mcmeta', json.dumps({'pack': {'pack_format': 81, 'description': 'Dune'}})) + z.writestr('data/dunerealms/structure/a.nbt', payload) + before = source.read_bytes() + upgrade.convert_pack(source, output) + self.assertEqual(source.read_bytes(), before) + with zipfile.ZipFile(output) as z: + self.assertEqual(z.read('data/dunerealms/structure/a.nbt'), payload) + self.assertEqual(json.loads(z.read('pack.mcmeta'))['pack']['min_format'], [121, 0]) + with self.assertRaises(ValueError): + upgrade.convert_pack(source, output) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/upgrade-datapack-26.py b/tools/upgrade-datapack-26.py new file mode 100644 index 00000000..a89a0e49 --- /dev/null +++ b/tools/upgrade-datapack-26.py @@ -0,0 +1,102 @@ +"""Convert the deployed Dune biome packs to Minecraft 26.3 without touching chunks. + +Uses the 26.3 native biome attribute format. Writes a separate deterministic ZIP; +the caller must stage/load it before replacing a production datapack. +""" +import argparse +import copy +import json +from pathlib import Path +import zipfile + + +def color(value): + return f'#{value:06x}' if isinstance(value, int) else value + + +def convert_biome(original): + biome = copy.deepcopy(original) + attributes = biome.setdefault('attributes', {}) + effects = biome.setdefault('effects', {}) + for field in ('fog_color', 'sky_color', 'water_fog_color'): + if field in effects: + attributes['minecraft:visual/' + field] = color(effects.pop(field)) + for field in ('water_color', 'grass_color', 'foliage_color', 'dry_foliage_color'): + if field in effects: + effects[field] = color(effects[field]) + ambient = {} + for old, new in [('mood_sound', 'mood'), ('additions_sound', 'additions'), ('ambient_sound', 'loop')]: + if old in effects: + ambient[new] = effects.pop(old) + if ambient: + attributes['minecraft:audio/ambient_sounds'] = ambient + if 'music_volume' in effects: + attributes['minecraft:audio/music_volume'] = effects.pop('music_volume') + if 'music' in effects: + music = effects.pop('music') + if not music: + attributes['minecraft:audio/music_volume'] = 0.0 + else: + if isinstance(music, list): + if len(music) != 1: + raise ValueError('Multiple weighted tracks require an explicit native music policy') + music = music[0].get('data', music[0]) + music.pop('replace_current_music', None) + attributes['minecraft:audio/background_music'] = {'default': music} + if 'particle' in effects: + particle = effects.pop('particle') + attributes['minecraft:visual/ambient_particles'] = {'argument': [ + {'particle': particle['options'], 'probability': particle['probability']} + ], 'modifier': 'append'} + if 'creature_spawn_probability' in biome: + attributes['minecraft:gameplay/creature_world_gen_spawn_probability'] = biome.pop('creature_spawn_probability') + if 'spawners' in biome: + categories = {} + for category, entries in biome.pop('spawners').items(): + categories[category] = [] + for entry in entries: + low, high = entry['minCount'], entry['maxCount'] + if not 1 <= low <= high: + raise ValueError('Invalid legacy spawn count') + categories[category].append({ + 'type': entry['type'], 'weight': entry['weight'], + 'count': low if low == high else { + 'type': 'minecraft:uniform', 'min_inclusive': low, 'max_inclusive': high + } + }) + attributes['minecraft:gameplay/natural_mob_spawns'] = { + 'argument': {'spawn_costs': biome.pop('spawn_costs', {}), 'spawns_by_category': categories}, + 'modifier': 'overlay' + } + return biome + + +def convert_pack(source, destination): + source, destination = Path(source), Path(destination) + if source.resolve() == destination.resolve() or destination.exists(): + raise ValueError('Use a new destination; source packs must remain intact') + changed = [] + with zipfile.ZipFile(source) as src, zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED) as dst: + for name in src.namelist(): + data = src.read(name) + if name == 'pack.mcmeta': + meta = json.loads(data) + for key in ('pack_format', 'supported_formats'): + meta['pack'].pop(key, None) + meta['pack'].update(min_format=[121, 0], max_format=[121, 0]) + data = (json.dumps(meta, indent=2) + '\n').encode() + elif '/worldgen/biome/' in name and '/tags/' not in name and name.endswith('.json'): + data = (json.dumps(convert_biome(json.loads(data)), indent=2) + '\n').encode() + changed.append(name) + info = zipfile.ZipInfo(name, date_time=(2026, 1, 1, 0, 0, 0)) + info.compress_type = zipfile.ZIP_DEFLATED + dst.writestr(info, data) + return changed + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('source') + parser.add_argument('destination') + args = parser.parse_args() + print(json.dumps({'convertedBiomes': convert_pack(args.source, args.destination)}))