-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
1153 lines (970 loc) · 47.6 KB
/
Copy pathmigrate.py
File metadata and controls
1153 lines (970 loc) · 47.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os
import sys
import re
import json
import shutil
import zipfile
import time
from datetime import datetime
from pathlib import Path
from collections import defaultdict
# ==============================================================================
# Terminal Text Hacks & Color Configuration (ANSI Color Codes + Stylizers)
# ==============================================================================
try:
import colorama
colorama.init(autoreset=True)
except ImportError:
pass
class Logger:
# ANSI Formatting Styles
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
REVERSE = "\033[7m"
# Standard Colors
HEADER = "\033[95m"
INFO = "\033[94m"
SUCCESS = "\033[92m"
WARNING = "\033[93m"
ERROR = "\033[91m"
MUTED = "\033[90m"
CYAN = "\033[96m"
@classmethod
def _timestamp(cls) -> str:
return f"{cls.MUTED}[{datetime.now().strftime('%H:%M:%S')}]{cls.RESET}"
@classmethod
def banner(cls, text: str):
width = 65
print(f"\n{cls.CYAN}╔{'═' * width}╗{cls.RESET}")
print(f"{cls.CYAN}║{cls.BOLD}{text.center(width)}{cls.RESET}{cls.CYAN}║{cls.RESET}")
print(f"{cls.CYAN}╚{'═' * width}╝{cls.RESET}\n")
@classmethod
def step(cls, step_num, name: str):
timestamp = cls._timestamp()
divider = f"{cls.MUTED}{'─' * 55}{cls.RESET}"
label = f"{step_num:02d}" if isinstance(step_num, int) else str(step_num)
print(f"\n{divider}")
print(f"{timestamp} {cls.BOLD}{cls.HEADER}🚀 STEP {label}:{cls.RESET} {cls.BOLD}{name}{cls.RESET}")
print(f"{divider}")
@classmethod
def info(cls, msg: str):
print(f" {cls._timestamp()} {cls.INFO}ℹ️ [INFO]{cls.RESET} {msg}")
@classmethod
def success(cls, msg: str):
print(f" {cls._timestamp()} {cls.SUCCESS}✔ [OK]{cls.RESET} {msg}")
@classmethod
def warn(cls, msg: str):
print(f" {cls._timestamp()} {cls.WARNING}⚠️ [WARN]{cls.RESET} {msg}")
@classmethod
def error(cls, msg: str):
print(f" {cls._timestamp()} {cls.ERROR}✖ [ERROR]{cls.RESET} {msg}")
@classmethod
def debug(cls, msg: str):
print(f" {cls._timestamp()} {cls.MUTED}🔍 [DEBUG]{cls.RESET} {msg}")
@classmethod
def progress_bar(cls, iteration: int, total: int, prefix='Progress', suffix='Complete', length=30):
"""Terminal Text Hack: Smooth Dynamic ASCII Progress Bar"""
percent = f"{100 * (iteration / float(total)):.1f}"
filled_length = int(length * iteration // total)
bar = '█' * filled_length + '░' * (length - filled_length)
sys.stdout.write(f"\r {cls._timestamp()} {cls.CYAN}⏳ {prefix}{cls.RESET} |{cls.SUCCESS}{bar}{cls.RESET}| {percent}% {cls.DIM}{suffix}{cls.RESET}")
sys.stdout.flush()
if iteration == total:
sys.stdout.write("\n")
@classmethod
def spinner(cls, message: str, duration: float = 0.6):
"""Terminal Text Hack: Inline Braille Animation Spinner"""
frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
end_time = time.time() + duration
idx = 0
while time.time() < end_time:
sys.stdout.write(f"\r {cls._timestamp()} {cls.CYAN}{frames[idx % len(frames)]}{cls.RESET} {message}...")
sys.stdout.flush()
time.sleep(0.08)
idx += 1
sys.stdout.write(f"\r{' ' * (len(message) + 25)}\r")
# ==============================================================================
# Directory Setup & Constants
# ==============================================================================
WORKSPACE = Path(os.getcwd()).resolve()
TMP_BACKUP = Path("/tmp/backup/dataLib-dp-pristine")
TMP_WORK = Path("/tmp/work/dataLib-dp")
TMP_ANALYSIS = Path("/tmp/analysis")
DIST_DIR = TMP_WORK / "dist"
SRC_ROOT = TMP_WORK / "datapacks/dataLib/data/datalib/function"
DST_BASE = TMP_WORK / "packs/modules"
# Single source of truth for which source subtrees are scanned for
# per-module discovery. Previously this list only lived inside
# step_8_build_module_map(); every other step relied on module_map.json
# already reflecting it, which is fragile if scan_roots ever changes.
# Now every step references this same constant directly.
SCAN_ROOTS = ["systems", "input", "api", "player", "world", "systems",
"core/cooldown", "core/lib", "core/state", "core/queue"]
# ==============================================================================
# Migration Steps
# ==============================================================================
def step_1_backup():
Logger.step(1, "Backup Pristine Checkout")
Logger.spinner("Creating full backup of original repo")
if TMP_BACKUP.exists():
shutil.rmtree(TMP_BACKUP)
TMP_BACKUP.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(WORKSPACE, TMP_BACKUP)
Logger.success(f"Backup created: {Logger.UNDERLINE}{TMP_BACKUP}{Logger.RESET}")
def step_2_prepare_work_dir():
Logger.step(2, "Prepare Isolated Work Directory")
Logger.spinner("Initializing isolated workspace")
if TMP_WORK.exists():
shutil.rmtree(TMP_WORK)
TMP_WORK.parent.mkdir(parents=True, exist_ok=True)
TMP_ANALYSIS.mkdir(parents=True, exist_ok=True)
shutil.copytree(WORKSPACE, TMP_WORK)
Logger.success(f"Working copy ready: {Logger.UNDERLINE}{TMP_WORK}{Logger.RESET}")
def step_3_verify_layout():
Logger.step(3, "Verify Expected Source Layout")
expected = TMP_WORK / "datapacks/dataLib/data/datalib/function"
if not expected.is_dir():
Logger.error(f"Expected source path not found: {expected}")
Logger.info("This script expects layout: datapacks/dataLib/data/datalib/...")
sys.exit(1)
Logger.success(f"Layout OK: {expected.relative_to(TMP_WORK)} exists")
def step_4_move_fabric_files():
Logger.step(4, "Move Fabric/Mod/Script Files into 'other/'")
other_dir = TMP_WORK / "other"
other_dir.mkdir(exist_ok=True)
targets = [
"build.gradle", "settings.gradle", "gradle.properties", "gradle",
"gradlew", "gradlew.bat", "src", "build_jmc.py", "jmc_config.json",
"main.jmc", "spyglass.json", "commit.sh", "show_dependencies.sh", "NOTICE.sh"
]
moved_count = 0
for item in targets:
src = TMP_WORK / item
if src.exists():
shutil.move(str(src), str(other_dir / item))
Logger.debug(f"Moved: {item} -> other/")
moved_count += 1
else:
Logger.warn(f"Skipped (not found): {item}")
Logger.info(f"Relocated {moved_count} project management / source files.")
def step_5_clean_deps():
Logger.step(5, "Remove Duplicate Dependencies File")
dep_file = TMP_WORK / ".dependencies.json"
if dep_file.exists():
dep_file.unlink()
Logger.success(".dependencies.json removed.")
else:
Logger.debug("No .dependencies.json found, skipping clean.")
def step_6_build_function_graph():
Logger.step(6, "Build Function Dependency Graph")
root = TMP_WORK / "datapacks/dataLib/data/datalib/function"
files = list(root.glob("**/*.mcfunction"))
if not files:
Logger.error(f"No .mcfunction files found under {root}")
sys.exit(1)
def to_id(path: Path):
rel = path.relative_to(root).with_suffix("")
return "datalib:" + str(rel).replace(os.sep, "/")
all_ids = set(to_id(f) for f in files)
ref_pattern = re.compile(r'datalib:([a-z0-9_/.\-]+)')
other_ns_pattern = re.compile(r'\b(dl_load|stringlib|player_action|load):([a-z0-9_/.\-]+)')
edges = defaultdict(set)
external_refs = defaultdict(set)
for i, f in enumerate(files, 1):
fid = to_id(f)
content = f.read_text(encoding="utf-8", errors="replace")
for m in ref_pattern.finditer(content):
edges[fid].add("datalib:" + m.group(1))
for m in other_ns_pattern.finditer(content):
external_refs[fid].add(f"{m.group(1)}:{m.group(2)}")
if i % 10 == 0 or i == len(files):
Logger.progress_bar(i, len(files), prefix="Scanning functions")
(TMP_ANALYSIS / "edges.json").write_text(json.dumps({k: sorted(v) for k, v in edges.items()}, indent=2))
(TMP_ANALYSIS / "external_refs.json").write_text(json.dumps({k: sorted(v) for k, v in external_refs.items() if v}, indent=2))
(TMP_ANALYSIS / "all_ids.json").write_text(json.dumps(sorted(all_ids), indent=2))
Logger.info(f"Total .mcfunction files scanned: {len(files)}")
Logger.info(f"Files referencing external namespaces: {len([k for k, v in external_refs.items() if v])}")
def step_7_prepare_core_skeleton():
Logger.step(7, "Prepare Core Module Skeleton")
base = TMP_WORK / "packs/modules/datalib-core/data"
subdirs = [
"datalib_core/function/internal", "datalib_core/function/load",
"datalib_core/function/tick", "datalib_core/function/gate",
"minecraft/tags/function/load", "minecraft/tags/function/tick"
]
for path in subdirs:
(base / path).mkdir(parents=True, exist_ok=True)
Logger.success("Core skeleton structure generated.")
def step_8_build_module_map():
Logger.step(8, "Build Module -> Source Path Mapping")
src = TMP_WORK / "datapacks/dataLib/data/datalib/function"
scan_roots = ["systems", "api","core/cooldown","core/lib","core/state","core/queue"]
discovered = {}
underscore_dirs = [] # (scan_root, entry_name) pairs -- resolved in step_8b
for r in scan_roots:
r_full = src / r
if not r_full.is_dir():
continue
for entry in sorted(os.listdir(r_full)):
if not (r_full / entry).is_dir():
continue
if entry.startswith("_"):
underscore_dirs.append((r, entry))
continue
discovered.setdefault(entry, []).append(f"{r}/{entry}")
# `player`, `world`, `events`, `input` are each their own standalone
# module as a whole -- unlike scan_roots (which turns each subdirectory
# into a separate module), everything directly under these four roots
# (loose .mcfunction files included) belongs to one module named after
# the root itself.
whole_root_modules = ["player", "world", "events", "input"]
for root_name in whole_root_modules:
root_full = src / root_name
if root_full.is_dir():
discovered.setdefault(root_name, []).append(root_name)
covered = set(path for paths in discovered.values() for path in paths)
remaining_top = set()
for entry in os.listdir(src):
full = src / entry
if not full.is_dir():
continue
if entry == "systems":
for sub in os.listdir(full):
key = f"systems/{sub}"
if key not in covered: remaining_top.add(key)
elif entry == "core":
for sub in os.listdir(full):
if sub == "internal":
for sub2 in os.listdir(full / "internal"):
if sub2 == "systems":
for sub3 in os.listdir(full / "internal/systems"):
key = f"core/internal/systems/{sub3}"
if key not in covered: remaining_top.add(key)
else:
remaining_top.add(f"core/internal/{sub2}")
else:
remaining_top.add(f"core/{sub}")
elif entry == "api":
for sub in os.listdir(full):
key = f"api/{sub}"
if key not in covered: remaining_top.add(key)
elif entry in whole_root_modules:
pass # already captured as a whole-root module above
else:
remaining_top.add(entry)
(TMP_ANALYSIS / "module_map.json").write_text(json.dumps({
"modules": discovered,
"core_remaining": sorted(remaining_top),
"underscore_dirs": underscore_dirs,
}, indent=2, ensure_ascii=False))
Logger.info(f"Auto-detected modules ({len(discovered)}): {Logger.CYAN}{sorted(discovered.keys())}{Logger.RESET}")
Logger.success("module_map.json successfully written.")
def step_8b_resolve_underscore_dirs():
# Underscore-prefixed dirs (e.g. input/_private) are internal-helper
# trees, not standalone modules -- but they must never be silently
# dropped. Each one is folded into exactly one real module, with an
# explicit, logged decision for every case:
# 1 sibling module in the same scan root -> fold into it
# 0 or >1 siblings (ambiguous) -> fold into "core"
# Either way the source path is guaranteed to end up in `discovered`.
Logger.step("8b", "Resolve Underscore-Prefixed Internal Dirs")
src = TMP_WORK / "datapacks/dataLib/data/datalib/function"
map_path = TMP_ANALYSIS / "module_map.json"
mm = json.loads(map_path.read_text())
discovered = mm["modules"]
underscore_dirs = mm.get("underscore_dirs", [])
if not underscore_dirs:
Logger.info("No underscore-prefixed directories found.")
return
for r, entry in underscore_dirs:
r_full = src / r
siblings = [
s for s in os.listdir(r_full)
if s != entry and not s.startswith("_") and (r_full / s).is_dir()
]
source_path = f"{r}/{entry}"
if len(siblings) == 1:
target = siblings[0]
reason = f"sole sibling module '{target}' under {r}/"
else:
target = "core"
reason = f"ambiguous ({len(siblings)} siblings under {r}/) -> defaulting to core"
discovered.setdefault(target, []).append(source_path)
Logger.info(f"{source_path} -> {target} ({reason})")
mm["modules"] = discovered
map_path.write_text(json.dumps(mm, indent=2, ensure_ascii=False))
Logger.success(f"Resolved {len(underscore_dirs)} underscore-prefixed dir(s); none dropped.")
def step_8c_relocate_root_dirs_to_api():
# `player`, `world`, `events`, and `input` are standalone modules in
# their own right and must NOT be folded into api/ -- they're scanned
# directly as their own scan roots in step_8 instead.
#
# Any *other* root-level dir that isn't a recognized scan root (i.e.
# not systems/api/core/config/debug and not one of the four standalone
# roots above) gets folded into api/ before module discovery, so it
# still becomes a real module instead of silently sitting outside
# every scan root. This runs before step_8's discovery scan, so
# step_9's existing classify()/token_pattern remap naturally picks up
# the new on-disk location -- no separate call-rewriting pass needed.
Logger.step("8c", "Relocate Unrecognized Root-Level Dirs Into api/")
src = TMP_WORK / "datapacks/dataLib/data/datalib/function"
api_dir = src / "api"
standalone_roots = {"player", "world", "events", "input"}
reserved = {"systems", "api", "core", "config", "debug"} | standalone_roots
root_dirs = sorted(
entry for entry in os.listdir(src)
if (src / entry).is_dir() and entry not in reserved
)
if not root_dirs:
Logger.info("No unrecognized root-level directories found; player/world/events/input kept as their own standalone modules.")
return
api_dir.mkdir(parents=True, exist_ok=True)
total_moved = []
for root_name in root_dirs:
root_dir = src / root_name
moved = []
for entry in sorted(os.listdir(root_dir)):
src_entry = root_dir / entry
dst_entry = api_dir / entry
if dst_entry.exists():
Logger.warn(f"api/{entry} already exists -- merging contents from {root_name}/{entry} instead of overwriting.")
if src_entry.is_dir():
for sub_dirpath, _, sub_files in os.walk(src_entry):
rel = Path(sub_dirpath).relative_to(src_entry)
target_dir = dst_entry / rel
target_dir.mkdir(parents=True, exist_ok=True)
for f in sub_files:
shutil.move(str(Path(sub_dirpath) / f), str(target_dir / f))
shutil.rmtree(src_entry)
else:
shutil.move(str(src_entry), str(dst_entry))
else:
shutil.move(str(src_entry), str(dst_entry))
moved.append(entry)
root_dir.rmdir()
total_moved.extend(moved)
Logger.info(f"Relocated {len(moved)} entr{'y' if len(moved)==1 else 'ies'} from {root_name}/ to api/: {moved}")
Logger.success(f"{len(root_dirs)} unrecognized root-level dir(s) absorbed into api/; player/world/events/input kept standalone.")
def step_9_migrate_functions():
Logger.step(9, "Migrate Function Files and Remap Namespaces")
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
modules = mmap["modules"]
prefix_to_module = {p: mod for mod, paths in modules.items() for p in paths}
def classify(rel_path):
best = None
for prefix in prefix_to_module:
if rel_path == prefix or rel_path.startswith(prefix + "/"):
if best is None or len(prefix) > len(best):
best = prefix
return prefix_to_module.get(best, "core") if best else "core"
def new_namespace_for(rel_noext):
mod = classify(rel_noext)
return f"datalib_{mod}" if mod != "core" else "datalib_core"
id_table = {}
all_files = []
for dirpath, _, filenames in os.walk(SRC_ROOT):
for fn in filenames:
if not fn.endswith(".mcfunction"):
continue
old_abs = Path(dirpath) / fn
rel = old_abs.relative_to(SRC_ROOT)
rel_noext = str(rel.with_suffix("")).replace(os.sep, "/")
ns = new_namespace_for(rel_noext)
id_table[rel_noext] = f"{ns}:{rel_noext}"
all_files.append((old_abs, rel_noext, ns))
shared_storages = {"engine", "input", "output"}
token_pattern = re.compile(r'datalib:([a-zA-Z0-9_/.\-]+)')
def remap_token(match):
path = match.group(1)
trail = ""
core_path = path
while core_path and core_path[-1] in ".,":
trail = core_path[-1] + trail
core_path = core_path[:-1]
if core_path in shared_storages:
return f"datalib_core:{core_path}{trail}"
if core_path in id_table:
return id_table[core_path] + trail
ns = new_namespace_for(core_path)
return f"{ns}:{core_path}{trail}"
total_files = len(all_files)
for i, (old_abs, rel_noext, ns) in enumerate(all_files, 1):
mod_key = ns.replace("datalib_", "")
if mod_key == "core":
dst_dir = DST_BASE / "datalib-core/data/datalib_core/function"
else:
dst_dir = DST_BASE / f"datalib-{mod_key}-module/data/{ns}/function"
dst_path = dst_dir / f"{rel_noext}.mcfunction"
dst_path.parent.mkdir(parents=True, exist_ok=True)
content = old_abs.read_text(encoding="utf-8", errors="replace")
new_content = token_pattern.sub(remap_token, content)
dst_path.write_text(new_content, encoding="utf-8")
if i % 15 == 0 or i == total_files:
Logger.progress_bar(i, total_files, prefix="Migrating functions")
(TMP_ANALYSIS / "id_table.json").write_text(json.dumps(id_table, indent=2))
Logger.success(f"Files moved & namespaces remapped: {total_files}")
def step_10_migrate_dlload():
Logger.step(10, "Migrate dl_load into datalib_core:load_gate")
dlload_stage = Path("/tmp/dlload_stage")
if dlload_stage.exists(): shutil.rmtree(dlload_stage)
src_dlload = TMP_WORK / "datapacks/dataLib/data/dl_load/function"
if src_dlload.exists():
shutil.copytree(src_dlload, dlload_stage / "function")
dst = DST_BASE / "datalib-core/data/datalib_core/function/load_gate"
dst.mkdir(parents=True, exist_ok=True)
token_pattern = re.compile(r'\bdl_load:([a-zA-Z0-9_/.\-]+)')
def remap(m):
path = m.group(1).rstrip(".,")
return f"datalib_core:load_gate/{path}"
count = 0
stage_func = dlload_stage / "function"
if stage_func.exists():
for dirpath, _, files in os.walk(stage_func):
for fn in files:
if not fn.endswith(".mcfunction"): continue
old_abs = Path(dirpath) / fn
rel = old_abs.relative_to(stage_func)
dst_path = dst / rel
dst_path.parent.mkdir(parents=True, exist_ok=True)
content = old_abs.read_text(encoding="utf-8", errors="replace")
new_content = token_pattern.sub(remap, content)
new_content = re.sub(r'\bdatalib:(engine|input|output)\b', r'datalib_core:\1', new_content)
dst_path.write_text(new_content, encoding="utf-8")
count += 1
Logger.success(f"Migrated dl_load files: {count}")
def step_11_create_tags_and_ticks():
Logger.step(11, "Create Load/Tick Function Tags")
core_dir = DST_BASE / "datalib-core"
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
modules = sorted(mmap["modules"].keys())
module_load_tags = [f"dl_{m}.load:main" for m in modules]
def write_json(rel, obj):
p = core_dir / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(obj, indent=2) + "\n", encoding="utf-8")
def write_text(rel, body):
p = core_dir / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(body, encoding="utf-8")
def merge_json_values(rel, extra_values):
p = core_dir / rel
existing = []
if p.is_file():
try:
existing = json.loads(p.read_text()).get("values", [])
except Exception:
existing = []
seen = set(v if isinstance(v, str) else json.dumps(v, sort_keys=True) for v in existing)
for v in extra_values:
k = v if isinstance(v, str) else json.dumps(v, sort_keys=True)
if k not in seen:
existing.append(v)
seen.add(k)
write_json(rel, {"values": existing})
write_text(
"data/load/function/_private/load.mcfunction",
"# Reset scoreboards for current load.\nscoreboard objectives add load.status dummy\nscoreboard players reset * load.status\n"
)
write_text("data/load/function/_private/init.mcfunction", "# load:_private/init\n")
write_json("data/load/tags/function/_private/load.json", {
"values": ["#load:_private/init", {"id": "#load:pre_load", "required": False}, {"id": "#load:load", "required": False}, {"id": "#load:post_load", "required": False}]
})
write_json("data/load/tags/function/_private/init.json", {"values": ["load:_private/init"]})
write_json("data/load/tags/function/load.json", {"values": ["stringlib:zprivate/load", "datalib_core:init", "#player_action:load","datalib_core:load_gate/main"]})
write_json("data/load/tags/function/pre_load.json", {"values": []})
write_json("data/load/tags/function/post_load.json", {"values": module_load_tags})
write_json("data/minecraft/tags/function/load.json", {"values": ["datalib_core:init"]})
write_json("data/minecraft/tags/function/tick.json", {"values": ["datalib_core:tick"]})
write_text(
"data/datalib_core/function/init.mcfunction",
"#> Runs on datapack loading\nexecute if data storage datalib_core:engine {global:{loaded:1b}} run return 0\nfunction #load:_private/load\n"
)
write_text(
"data/datalib_core/function/tick.mcfunction",
"# datalib_core:tick\nfunction datalib_core:core/tick\nfunction #datalib_core:loop\nfunction #datalib_core:events/on_tick\n"
)
default_loop = [
"datalib_core:input/writable_book",
"datalib_core:input/command_block_minecart",
"datalib_core:core/internal/api/cmd/freeze/tick",
"datalib_core:input/_private/cbm_align_tp",
]
merge_json_values("data/datalib_core/tags/function/loop.json", default_loop)
discovered_ticks = []
for m in modules:
ns = f"datalib_{m}"
pack = f"datalib-{m}-module"
fn_root = DST_BASE / pack / "data" / ns / "function"
if not fn_root.is_dir(): continue
for dirpath, _, files in os.walk(fn_root):
for fn in files:
if not fn.endswith(".mcfunction"): continue
stem = fn[:-len(".mcfunction")]
if stem == "tick" or stem.startswith("tick_") or stem.endswith("_tick"):
rel = Path(dirpath, fn).relative_to(fn_root).with_suffix("")
rel_noext = str(rel).replace(os.sep, "/")
discovered_ticks.append(f"{ns}:{rel_noext}")
discovered_ticks = sorted(set(discovered_ticks))
merge_json_values("data/datalib_core/tags/function/events/on_tick.json", discovered_ticks)
on_load = "data/datalib_core/tags/function/events/on_load.json"
if not (core_dir / on_load).is_file():
write_json(on_load, {"values": []})
write_text("data/datalib.main/function/empty.mcfunction", "#> datalib.main:empty\n")
Logger.success("Load & Tick tags correctly configured.")
def step_12_migrate_extras():
Logger.step(12, "Migrate Advancements, Loot Tables, Predicates, Item Modifiers & Dialogs")
src_root = TMP_WORK / "datapacks/dataLib/data/datalib"
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
modules = mmap["modules"]
id_table = json.loads((TMP_ANALYSIS / "id_table.json").read_text())
prefix_to_module = {p: mod for mod, paths in modules.items() for p in paths}
def classify(rel_path):
best = None
for prefix in prefix_to_module:
if rel_path == prefix or rel_path.startswith(prefix + "/"):
if best is None or len(prefix) > len(best):
best = prefix
return prefix_to_module.get(best, "core") if best else "core"
categories = ["advancement", "loot_table", "predicate", "item_modifier", "dialog"]
shared_storages = {"engine", "input", "output"}
token_pattern = re.compile(r'datalib:([a-zA-Z0-9_/.\-]+)')
def remap_token_in_text(text):
def remap(m):
path = m.group(1)
trail = ""
core_path = path
while core_path and core_path[-1] in ".,":
trail = core_path[-1] + trail
core_path = core_path[:-1]
if core_path in shared_storages:
return f"datalib_core:{core_path}{trail}"
if core_path in id_table:
return id_table[core_path] + trail
ns = classify(core_path)
ns_name = f"datalib_{ns}" if ns != "core" else "datalib_core"
return f"{ns_name}:{core_path}{trail}"
return token_pattern.sub(remap, text)
moved = 0
for cat in categories:
cat_src = src_root / cat
if not cat_src.is_dir(): continue
for dirpath, _, files in os.walk(cat_src):
for fn in files:
old_abs = Path(dirpath) / fn
rel = old_abs.relative_to(cat_src)
rel_noext = os.path.join(cat, rel)
rel_noext_no_json = str(rel_noext)[:-len(".json")] if str(rel_noext).endswith(".json") else str(rel_noext)
content = old_abs.read_text(encoding="utf-8", errors="replace")
m = token_pattern.search(content)
target_mod = "core"
if m:
candidate = m.group(1).rstrip(".,")
if candidate not in shared_storages:
target_mod = classify(candidate)
if not m:
target_mod = classify(rel_noext_no_json)
ns = f"datalib_{target_mod}" if target_mod != "core" else "datalib_core"
dst_dir = DST_BASE / ("datalib-core/data/datalib_core" if target_mod == "core" else f"datalib-{target_mod}-module/data/{ns}") / cat
dst_path = dst_dir / rel
dst_path.parent.mkdir(parents=True, exist_ok=True)
dst_path.write_text(remap_token_in_text(content), encoding="utf-8")
moved += 1
Logger.success(f"Extra data assets migrated: {moved}")
def step_13_migrate_tags():
Logger.step(13, "Migrate Function Tags")
src = TMP_WORK / "datapacks/dataLib/data/datalib/tags"
dst = DST_BASE / "datalib-core/data/datalib_core/tags"
id_table = json.loads((TMP_ANALYSIS / "id_table.json").read_text())
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
modules = mmap["modules"]
prefix_to_module = {p: mod for mod, paths in modules.items() for p in paths}
def classify(rel_path):
best = None
for prefix in prefix_to_module:
if rel_path == prefix or rel_path.startswith(prefix + "/"):
if best is None or len(prefix) > len(best):
best = prefix
return prefix_to_module.get(best, "core") if best else "core"
shared_storages = {"engine", "input", "output"}
token_pattern = re.compile(r'datalib:([a-zA-Z0-9_/.\-]+)')
def remap(text):
def sub(m):
path = m.group(1)
trail = ""
core_path = path
while core_path and core_path[-1] in ".,":
trail = core_path[-1] + trail
core_path = core_path[:-1]
if core_path in shared_storages:
return f"datalib_core:{core_path}{trail}"
if core_path in id_table:
return id_table[core_path] + trail
ns = classify(core_path)
ns_name = f"datalib_{ns}" if ns != "core" else "datalib_core"
return f"{ns_name}:{core_path}{trail}"
return token_pattern.sub(sub, text)
moved = 0
if src.is_dir():
for dirpath, _, files in os.walk(src):
for fn in files:
old_abs = Path(dirpath) / fn
rel = old_abs.relative_to(src)
dst_path = dst / rel
dst_path.parent.mkdir(parents=True, exist_ok=True)
content = old_abs.read_text(encoding="utf-8", errors="replace")
dst_path.write_text(remap(content), encoding="utf-8")
moved += 1
Logger.success(f"Tag files migrated: {moved}")
def step_14_cleanup_old_module_dirs():
Logger.step(14, "Remove Old Module Source Directories")
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
modules = mmap["modules"]
removed, skipped = [], []
for mod, paths in modules.items():
for rel_path in paths:
full = SRC_ROOT / rel_path
if full.is_dir():
shutil.rmtree(full)
removed.append(rel_path)
else:
skipped.append(rel_path)
for parent in ["systems", "core/internal/systems", "api"]:
full = SRC_ROOT / parent
if full.is_dir() and not os.listdir(full):
full.rmdir()
Logger.info(f"Removed {len(removed)} old module source directories.")
for r in sorted(removed):
Logger.debug(f" - {r}")
def step_15_post_fixes():
Logger.step(15, "Post-Migration Cleanups & Metadata Generation")
# 1. Clean duplicate load.mcfunction
dup = DST_BASE / "datalib-core/data/datalib_core/function/load.mcfunction"
if dup.is_file():
dup.unlink()
Logger.debug("Duplicate load.mcfunction removed.")
# 2. Fix sound namespace
sound_target = DST_BASE / "datalib-core/data/datalib_core/function"
if sound_target.is_dir():
for p in sound_target.glob("**/*.mcfunction"):
txt = p.read_text(encoding="utf-8", errors="replace")
if "playsound datalib_core:" in txt:
p.write_text(txt.replace("playsound datalib_core:", "playsound datalib:"), encoding="utf-8")
# 3. Create load skeleton
mmap = json.loads((TMP_ANALYSIS / "module_map.json").read_text())
module_list = sorted(mmap["modules"].keys())
for m in module_list:
(DST_BASE / f"datalib-{m}-module/data/datalib_{m}/function/load").mkdir(parents=True, exist_ok=True)
(DST_BASE / f"datalib-{m}-module/data/dl_{m}.load/function").mkdir(parents=True, exist_ok=True)
# 4. Discover each module's actual scoreboard objectives & storage
# roots by scanning its migrated .mcfunction files, then GENERATE
# real init.mcfunction / cleanup.mcfunction files from that data.
# Previously init/cleanup were either hardcoded to a wrong module
# set or left as bare comments -- neither approach reflected what
# the module actually touches, and both could leave init/cleanup
# empty or referencing files that don't exist.
objective_def_pattern = re.compile(r'\bscoreboard\s+objectives\s+add\s+(\S+)\s+\S+')
# `scoreboard players <set|add|remove|get|enable|operation> <targets> <objective> ...`
# -- the objective is the token right after the target selector.
objective_use_pattern = re.compile(
r'\bscoreboard\s+players\s+(?:set|add|remove|get|enable|operation|reset)\s+\S+\s+(\S+)'
)
def _clean_objective(raw_obj):
# Reject macro placeholders (e.g. $(objective), $(pb_obj)) -- these
# are resolved at runtime per-call and aren't a fixed objective
# name this module can pre-declare.
if not raw_obj or raw_obj.startswith("$("):
return None
return raw_obj
# Captures the storage id as a single colon-joined token (ns:root) and,
# if present, the following NBT key as a separate token -- this matches
# real syntax: `data modify storage <ns:root> <nbt_key> set value ...`.
# The storage id itself is never split on the colon; `<ns:root>` and
# `<key>` are two distinct command arguments.
storage_pattern = re.compile(r'\bstorage\s+(datalib_[a-z0-9_]*:[a-zA-Z0-9_]+)(?:\s+([a-zA-Z0-9_.\[\]]+))?')
def _clean_key(raw_key):
# Reject anything that isn't a genuine NBT path segment (e.g. an
# inline `{}`/`{...}` literal that the regex's generic tail
# accidentally swept up).
if not raw_key or raw_key in ("{}",) or raw_key.startswith("{"):
return None
return raw_key
def scan_module_usage(m, ns):
mod_root = DST_BASE / f"datalib-{m}-module/data/{ns}/function"
objectives = set()
storages = set() # set of (storage_id, nbt_key_or_None) pairs
if mod_root.is_dir():
for p in mod_root.glob("**/*.mcfunction"):
txt = p.read_text(encoding="utf-8", errors="replace")
for line in txt.splitlines():
stripped = line.strip()
if stripped.startswith("#"):
continue
for om in objective_def_pattern.finditer(line):
obj = _clean_objective(om.group(1))
if obj:
objectives.add(obj)
for om in objective_use_pattern.finditer(line):
obj = _clean_objective(om.group(1))
if obj:
objectives.add(obj)
for sm in storage_pattern.finditer(line):
storages.add((sm.group(1), _clean_key(sm.group(2))))
return sorted(objectives), sorted(storages, key=lambda t: (t[0], t[1] or ""))
for m in module_list:
ns = f"datalib_{m}"
dir_path = DST_BASE / f"datalib-{m}-module/data/dl_{m}.load/function"
objectives, storage_pairs = scan_module_usage(m, ns)
# -- init.mcfunction: (re)create every objective/storage this
# module actually references, so nothing is left undeclared.
init_dir = DST_BASE / f"datalib-{m}-module/data/{ns}/function/load"
init_dir.mkdir(parents=True, exist_ok=True)
init_path = init_dir / "init.mcfunction"
init_lines = [f"# {ns}:load/init -- ensures this module's objectives/storages exist"]
for obj in objectives:
init_lines.append(f"scoreboard objectives add {obj} dummy")
for st_id, key in storage_pairs:
if key:
init_lines.append(f"execute unless data storage {st_id} {key} run data modify storage {st_id} {key} set value {{}}")
else:
init_lines.append(f"execute unless data storage {st_id} run data modify storage {st_id} set value {{}}")
if not objectives and not storage_pairs:
init_lines.append("# No module-local scoreboards/storages detected.")
init_path.write_text("\n".join(init_lines) + "\n", encoding="utf-8")
init_line = f"function {ns}:load/init"
# -- cleanup.mcfunction: reset every objective and clear every
# storage key this module owns, run on datapack unload/reload.
cleanup_dir = DST_BASE / f"datalib-{m}-module/data/{ns}/function/load"
cleanup_path = cleanup_dir / "cleanup.mcfunction"
cleanup_lines = [f"# {ns}:load/cleanup -- resets this module's scoreboards/storages"]
for obj in objectives:
cleanup_lines.append(f"scoreboard players reset * {obj}")
for st_id, key in storage_pairs:
# `data remove storage <ns:root>` alone (no key) removes the
# entire storage root -- valid. `data remove storage <ns:root>
# <key>` removes just that key -- also valid, and required
# whenever a specific NBT key was observed in usage.
if key:
cleanup_lines.append(f"execute if data storage {st_id} {key} run data remove storage {st_id} {key}")
else:
cleanup_lines.append(f"execute if data storage {st_id} run data remove storage {st_id}")
if not objectives and not storage_pairs:
cleanup_lines.append("# No module-local scoreboards/storages detected.")
cleanup_path.write_text("\n".join(cleanup_lines) + "\n", encoding="utf-8")
cleanup_note = f"# Cleanup: #load:module_cleanup -> {ns}:load/cleanup"
content = f"""# dl_{m}.load:main -- Stage entry point for the {m} module
scoreboard objectives add dl.{m}.version dummy
scoreboard players set $dl_{m}_version dl.{m}.version 601
{init_line}
{cleanup_note}
"""
(dir_path / "main.mcfunction").write_text(content, encoding="utf-8")
# 5. Create pack.mcmeta files
def get_mcmeta(title, desc_extra=""):
return {
"pack": {
"description": [
{"text": title, "color": "#00ccff", "bold": True, "italic": False},
{"text": " | by runtoolkit", "color": "#00ff33", "bold": False, "italic": False},
{"text": desc_extra, "color": "#ffaa00" if "v6" in desc_extra else "#ff5555", "bold": False, "italic": True}
],
"min_format": 107,
"max_format": 107
},
"features": {"enabled": ["minecraft:vanilla"]}
}
(DST_BASE / "datalib-core/pack.mcmeta").write_text(json.dumps(get_mcmeta("D.L. Core", " | v6.0.1"), indent=2, ensure_ascii=False))
for m in module_list:
p_dir = DST_BASE / f"datalib-{m}-module"
(p_dir / "pack.mcmeta").write_text(json.dumps(get_mcmeta(f"D.L. -- {m}", " | requires datalib-core"), indent=2, ensure_ascii=False))
Logger.success(f"Generated pack.mcmeta and load mains for {len(module_list)} modules.")
def step_16_support_packs():
Logger.step(16, "Migrate Support Packs (stringlib & player_action)")
sup_base = TMP_WORK / "packs/support"
str_dir = sup_base / "stringlib-dp/data"
pl_dir = sup_base / "player-action-dp/data"
str_src = TMP_WORK / "datapacks/dataLib/data/stringlib"
pl_src = TMP_WORK / "datapacks/dataLib/data/player_action"
if str_src.is_dir():
shutil.copytree(str_src, str_dir / "stringlib", dirs_exist_ok=True)
if pl_src.is_dir():
shutil.copytree(pl_src, pl_dir / "player_action", dirs_exist_ok=True)
def write_sup_mcmeta(path, name, desc):
(path / "pack.mcmeta").write_text(json.dumps({
"pack": {
"description": [
{"text": name, "color": "#00ccff", "bold": True, "italic": False},
{"text": " | by runtoolkit", "color": "#00ff33", "bold": False, "italic": False},
{"text": f" | {desc}", "color": "#aaaaaa", "bold": False, "italic": True}
],
"min_format": 107, "max_format": 107
},
"features": {"enabled": ["minecraft:vanilla"]}
}, indent=2))
write_sup_mcmeta(sup_base / "stringlib-dp", "StringLib", "string utility library")
write_sup_mcmeta(sup_base / "player-action-dp", "Player Action", "player event capture library")
Logger.success("Support packs successfully extracted.")
def step_17_remap_leftovers():
Logger.step(17, "Remap Leftover References Across Support/Modules")
id_table = {}
if (TMP_ANALYSIS / "id_table.json").is_file():
id_table = json.loads((TMP_ANALYSIS / "id_table.json").read_text())
shared = {"engine", "input", "output"}
token = re.compile(r"\bdatalib:([a-zA-Z0-9_/.\-]+)")
def classify_fallback(p):
parts = p.split("/")
if p.startswith("core/internal/systems/") and len(parts) > 3:
return f"datalib_{parts[3]}" if parts[3] != "core" else "datalib_core"
if p.startswith("systems/") and len(parts) > 1:
return f"datalib_{parts[1]}"
if p.startswith("api/") and len(parts) > 1:
return f"datalib_{parts[1]}"
return "datalib_core"
def remap_token(m):
path = m.group(1)
trail = ""
core_path = path
while core_path and core_path[-1] in ".,":
trail = core_path[-1] + trail
core_path = core_path[:-1]
if core_path in shared:
return f"datalib_core:{core_path}{trail}"
if core_path in id_table:
return id_table[core_path] + trail
ns = classify_fallback(core_path)
return f"{ns}:{core_path}{trail}"
changed = 0