lwzm
2/8/2017 - 4:40 AM

my resources loader

my resources loader

base = "http://z.tyio.net:8001/zip/"


@loadResource = (name, callback) ->
    i = name.indexOf "#"
    if i > 0
        path = name.slice 0, i
        key = name.slice i + 1
    else
        path = name
        key = null

    res = resources[path]

    run = (res) ->
        if key
            [x, y, w, h] = res.meta[key]
            res.zip.file key .async \uint8array
                .then (data) ->
                    ok =
                        x: x
                        y: y
                        w: w
                        h: h
                        data: data
                    callback ok
        else
            'blah...'

    if not res
        res = resources[path] = []
        JSZipUtils.getBinaryContent "#base#path", (err, data) ->
            if err
                resources[path] = null
                throw err
            JSZip.loadAsync data
            .then (zip) ->
                zip.file \_ .async \string
                    .then (meta) ->
                        res =
                            meta: JSON.parse(meta)
                            zip: zip
                        for run in resources[path]
                            run(res)
                        resources[path] = res

    if res.push  # downloading, push into queue
        res.push run
    else  # downloaded
        run res