general mindustry schematic tools
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
#!/usr/bin/env -S node --no-warnings

import fs from "fs";
import { Schematic } from "mindustry-schematic-parser";
import { ArgumentParser } from "argparse";
import clipboard from "clipboardy";

const parser = new ArgumentParser();

parser.add_argument("--base64", "-b", {
  nargs: 1,
  type: "string",
});
parser.add_argument("-c", "--clipboard", {
  action: "store_true",
});
parser.add_argument("--file", {
  nargs: 1,
  type: "string",
  action: "store",
});
parser.add_argument("--image", "-i", {
  nargs: 1,
  type: "string",
  help: "where to output the image",
});
parser.add_argument("--rename", "-r", {
  nargs: "+",
  type: "string",
  help: "to rename the schematic, and copy the new schematic",
});
parser.add_argument("--format", "-f", {
  action: "store_true",
});

let base64;
let file;

const args = parser.parse_args();

base64 = args.base64 ? args.base64[0] : "";
file = args.file ? args.file[0] : "";

if (args.clipboard) {
  const clip = clipboard.readSync();
  if (!clip) throw new Error("clipboard empty");
  base64 = clip;
}

if (!file && !base64) {
  throw new Error("file/base64 not specified");
}

let schematic;
if (base64) {
  schematic = Schematic.decode(base64);
} else if (file) {
  const buffer = fs.readFileSync(file);
  schematic = Schematic.decode(buffer);
}

if (args.image) {
  const opacity = 0.25;
  // save a preview of the schematic
  schematic
    .render({
      background: false, // disable background
      bridges: {
        opacity: opacity,
      },
      phaseBridges: {
        opacity: opacity,
      },
    })
    .then((nodeCanvas) => nodeCanvas.toBuffer())
    .then((buffer) => fs.writeFileSync(args.image[0], buffer));
}

if (args.rename) {
  schematic.name = args.rename.join(" ");
  if (args.format) clipboard.write(`\`\`\`${schematic.encode()}\`\`\``);
  else clipboard.write(schematic.encode());
} else if (args.format) clipboard.write(`\`\`\`${schematic.encode()}\`\`\``);

console.log(schematic.name);