File size: 1,769 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class extensionsPatch {
    /**
     * constructor
     * @param {Runtime} runtime runtime
     */
    constructor (runtime) {
        this.runtime = runtime;
        this.extensions = {};
        this.loaded = [];
    }

    /**
     * replaces a core extension with a external extension for the loader
     * @param {String} id extension id
     * @param {String} url new extension url
     * @param {Object} extensions sb3 loader extension object
     */
    basicPatch (id, url, extensions) {
        extensions.extensionURLs.set(id, url);
    }

    /**
     * runs the patch for an extension
     * @param {String} id the extension to patch
     * @param {Object} extensions the extensions object
     * @param {Blocks} blocks all of the blocks
     */
    runExtensionPatch (id, extensions, object) {
        const patch = this.extensions[id];
        if (typeof patch === 'object') {
            if (!this.loaded.includes(id)) {
                // patch to fix added urls not loading
                this.runtime.extensionManager.loadExtensionURL(patch.url);
                this.loaded.push(id);
            }
            this.basicPatch(patch.id, patch.url, extensions);
            return;
        }
        patch(extensions, object, this.runtime);
    }

    /**
     * registers extension patches to the patcher
     * @param {Object} list a list of patches to register
     */
    registerExtensions (list) {
        this.extensions = Object.assign(this.extensions, list);
    }

    /**
     * gets if a patch exists for an extension
     * @param {String} id the extension id to check
     * @returns {Boolean} if the given extension exists
     */
    patchExists (id) {
        return !!this.extensions[id];
    }
}

module.exports = extensionsPatch;