summaryrefslogtreecommitdiff
path: root/play.sh
blob: f1d86bac578ce93c022bf9011b63504dd4116223 (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
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
#!/bin/zsh -f

# initialization {{{
PLAY_DEBUG=${PLAY_DEBUG:-0}

[[ $PLAY_DEBUG == 2 ]] && setopt xtrace

PLAY_DIR="${PLAY_DIR:-${0:h}}"
PLAY_GAMES="${PLAY_GAMES:-$PLAY_DIR/installed}"
PLAY_TEMPLATES="${PLAY_TEMPLATES:-$PLAY_DIR/templates}"

typeset -A ENV EENV
BIN=$0
# }}}

# global functions {{{
out () {
    echo ">>> $@" >&2
}

log () {
    [[ $PLAY_DEBUG > 0 ]] && echo "*** $@" >&2
}

die () {
    out "*** ERROR: $@"
    exit 1
}

exp () {
    log "Setting envvar '$1' to '$2'"
    export $1=$2
}

exc () {
    cmd="eval"

    if [[ $1 == "-e" ]]; then
        cmd="exec"
        shift
    fi

    log "Executing (using '$cmd'):"
    log "> $@"

    $cmd "$@"
}

EXPORT () {
    local name=$1
    shift

    for f in $@; do
        eval "$f () { ${name}_${f}; }"
    done
}

inherit () {
    zparseopts -D e=nonfatal
    
    if [[ ! -e $PLAY_TEMPLATES/$1 ]]; then
        if [[ -n $nonfatal ]]; then
            log "Template '$1' not found"
            return
        else
            die "Template '$1' not found"
        fi
    fi
    
    source $PLAY_TEMPLATES/$1
}

load () {
    inherit -e default

    source "$PLAY_GAMES/$1"
}
# }}}

# default template {{{

# exporting variables
EENV[WINEPREFIX]='eval echo $PREFIX'
ENV[DISPLAY]=":1"

PREFIX="~/.wine"

# functions
play_execute () {
    exc -e startx $BIN -x $GAME -- $DISPLAY -ac -br -quiet ${=EXARGS}
}

play_prepare () {
    # set display size
    [[ -n $SIZE ]] && exc xrandr -s $SIZE
}

play_setenv () {
    for e v in ${(kv)ENV}; do
        v=${(P)${:-PLAY_ENV_$e}:-$v}
        exp $e $v
    done
    
    for e v in ${(kv)EENV}; do
        v=${(P)${:-PLAY_EENV_$e}:-$v}
        exp $e `eval $v`
    done
}

play_run () {
    # cd into dir
    local dir=$(exc winepath -u "$GPATH")
    exc cd $(dirname $dir)

    # start game
    exc wine start $GPATH "$ARGS"
    
    # wait for wine to shutdown
    exc wineserver -w
}

play_cleanup () {
}

EXPORT play execute prepare setenv run cleanup
# }}}

if [[ $1 == "-x" ]]; then
    load $2
    prepare
    run
    cleanup
else
    GAME=$1
    DGAME="$PLAY_GAMES/$GAME"

    list () {
        out "The installed games are:"
        for k in $PLAY_GAMES/*(.,@:t); do
            echo "\t> $k"
        done
    }
    
    if [[ -z $GAME || ! -e $DGAME ]]; then
        [[ ! -e $DGAME ]] && out "Game '$GAME' not found"
        list
        exit 1
    else
        out "Launching '$GAME'"
        load $GAME
        setenv
        execute
    fi
fi

# vim: foldmethod=marker