aboutsummaryrefslogtreecommitdiff
path: root/mcsh.sh
blob: d3e57cb262ebe5591d96ec94189a9eb436d55e1c (plain)
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

VERSIONS_DIR="versions"
LIBRARIES_DIR="libraries"
NATIVES_DIR="natives"
ASSETS_DIR="assets"
GAME_DIR="run"

function update_metadata (
    printf 'updating version manifest\n' >&2
    mkdir -p "$VERSIONS_DIR"
    curl --silent "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json" | jq '{"latest": .latest, "versions": [.versions[] | {"key": .id, "value": .url}] | from_entries}' > "$VERSIONS_DIR/manifest.json"

    version="${1:-"$(jq -r '.latest.release' "$VERSIONS_DIR/manifest.json")"}"

    printf 'updating %s meta %s\n' "$version" >&2
    mkdir -p "$VERSIONS_DIR/$version"
    curl --silent -o "$VERSIONS_DIR/$version/meta.json" "$(jq -r --arg version "$version" '.versions[$version]' $VERSIONS_DIR/manifest.json)"
    meta="$(jq -c --slurpfile info info.json --slurpfile auth auth.json -f meta.jq "$VERSIONS_DIR/$version/meta.json")"

    printf 'updating %s asset index...\n' "$version" >&2
    mkdir -p "$ASSETS_DIR/indexes"
    assets_url="$(<<<"$meta" jq -r '.asset_index')"
    curl --silent -o "$ASSETS_DIR/indexes/$(basename "$assets_url")" "$assets_url"

    printf '%s\n' "$meta"
)

function launch (
    meta="$(update_metadata "${1:-}")"

    export LIBRARIES_DIR
    function download_library {
        if ! [[ -e "$LIBRARIES_DIR/${2:-}" ]]; then
            printf '%s\n' "${1:-}" >&2
            mkdir -p "$(dirname "$LIBRARIES_DIR/${2:-}")"
            curl --silent -o "$LIBRARIES_DIR/${2:-}" "${3:-}"
        fi
    }
    export -f download_library

    export ASSETS_DIR
    function download_asset {
        if ! [[ -e "$ASSETS_DIR/objects/${2:-}" ]]; then
            printf '%s\n' "${1:-}" >&2
            mkdir -p "$(dirname "$ASSETS_DIR/objects/${2:-}")"
            curl --silent -o "$ASSETS_DIR/objects/${2:-}" "https://resources.download.minecraft.net/${2:-}"
        fi
    }
    export -f download_asset

    #TODO: check hash
    version="$(<<<"$meta" jq -r '.version')"
    if ! [[ -e "$VERSIONS_DIR/$version/client.jar" ]]; then
        printf 'downloading %s client jar...\n' "$version" >&2
        <<<"$meta" jq -r '.client_url' | xargs curl --silent -o "$VERSIONS_DIR/$version/client.jar"
    fi

    printf 'downloading libraries...\n' >&2
    <<<"$meta" jq -r '.libraries[] | "\(.name) \(.path) \(.url)"' | xargs -I {} "$SHELL" -c "download_library {}"

    printf 'downloading assets...\n' >&2
    jq -r '.objects | to_entries[] | "\(.key) \(.value.hash[:2])/\(.value.hash)"' "$ASSETS_DIR/indexes/$(basename "$(<<<"$meta" jq -r '.asset_index')")" | xargs -P 10 -I {} "$SHELL" -c "download_asset {}"

    # if ! [[ -e "$VERSIONS_DIR/$version/log4j.xml" ]]; then
    #     printf 'downloading %s log4j config...\n' "$version" >&2
    #     <<<"$meta" jq -r '.logging_config' | xargs curl --silent -o "$VERSIONS_DIR/$version/log4j.xml"
    # fi

    function replace_placeholders {
        cat - | sed \
            -e "s/\${versions_directory}/$VERSIONS_DIR/g" \
            -e "s/\${libraries_directory}/$LIBRARIES_DIR/g" \
            -e "s/\${natives_directory}/$NATIVES_DIR/g" \
            -e "s/\${assets_root}/$ASSETS_DIR/g" \
            -e "s/\${game_directory}/$GAME_DIR/g" \
            \
            -e "s/\${launcher_name}/mcsh/g" \
            -e "s/\${launcher_version}/v0.1.0/g"
    }

    printf 'starting game :3\n' >&2
    <<<"$meta" jq -r '.args.jvm + [.main_class] + .args.game | join(" ")' | replace_placeholders | xargs java
)

launch "${1:-}"