difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
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
window.DownloadManager = cc.Class({
    ctor: function() {
        this._spinesCache = [];
    },
 
    checkAnimaCache: function() {
        return
        cc.sys.localStorage.removeItem("sszg_anima_cache_1");
        var cache_info = cc.sys.localStorage.getItem("sszg_anima_cache_1");
        if (cache_info) { 
            cache_info = JSON.parse(cache_info);
            if (cache_info.status != "finish") {
                var finish_num = parseInt(cache_info.status);
                if (typeof finish_num == "number" && finish_num > 0 && finish_num < CacheAnimas.length) {
                    for (var anima_i = finish_num + 1; anima_i < CacheAnimas.length - 1; anima_i ++) {
                        this.downloadSpine(CacheAnimas[anima_i], anima_i);
                    }
                }
            }
        } else {
            for (var anima_i in CacheAnimas) {
                this.downloadSpine(CacheAnimas[anima_i], anima_i);
            }            
        }
    },
 
    downloadSpine: function(spine_id, index) {
        var spine_info = {spine_id: spine_id, index: index};
        this._spinesCache.push(spine_info);
 
        if (this._spinesCache.length > 0 && !this._downTimer) {
            this._downTimer = gcore.Timer.set(this.preDownLoad.bind(this), 500, -1);
        }
    },
 
    preDownLoad: function() {
        var load_status = cc.loader.getDownLoadStatus();
        if (!load_status) {        // 主队列没有下载任务
            var spine_info = this._spinesCache.shift();
 
            if (spine_info.index) {
                var status = spine_info.index;
                if (spine_info.index >= CacheAnimas.length - 1) {
                    status = "finish"
                } 
                cc.sys.localStorage.setItem("sszg_anima_cache_1", JSON.stringify({status: status}));
            }
            this.doDownloadSpine(spine_info.spine_id);
 
            if (this._spinesCache.length <= 0 && this._downTimer) {
                gcore.Timer.del(this._downTimer);
                this._downTimer = null;
            }
        }
    },
 
    doDownloadSpine: function(spine_id) {
        if (!spine_id)
            return
 
        var spine_path = "spine/" + spine_id + "/";
        // spien文件
        var spine_uuids = cc.loader.getDirUuids(spine_path, sp.SkeletonData);
 
        cc.log(spine_uuids);
 
        // for (var spine_i in spine_uuids) {
        //     var spine_url = cc.loader.getUrlWithUuid(spine_uuids[spine_i]);
 
        //     this.downloadText(spine_url);
        // }
 
        // // 图片和纹理
        // var img_uuids = cc.loader.getDirUuids(spine_path, cc.Texture2D);
        // for (var img_i in img_uuids) {
        //     var texture_url = cc.loader.getUrlWithUuid(img_uuids[img_i]);
        //     var img_url = texture_url.replace(".json", ".png");
        //     this.downloadText(texture_url);
        //     this.downloadImage(img_url); 
        // }
    },
 
    downloadText: function (url, callback) {
        var xhr = cc.loader.getXMLHttpRequest(),
        // var xhr = new ActiveXObject("MSXML2.XMLHTTP");
        errInfo = 'Load text file failed: ' + url;
        xhr.open('GET', url, true);
        if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8');
        xhr.onload = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200 || xhr.status === 0) {
                    callback && callback(null, xhr.responseText);
                }
                else {
                    callback && callback({status:xhr.status, errorMessage:errInfo + '(wrong status)'});
                }
            }
            else {
                callback && callback({status:xhr.status, errorMessage:errInfo + '(wrong readyState)'});
            }
        };
        xhr.onerror = function(){
            callback && callback({status:xhr.status, errorMessage:errInfo + '(error)'});
        };
        xhr.ontimeout = function(){
            callback && callback({status:xhr.status, errorMessage:errInfo + '(time out)'});
        };
        xhr.send(null);
    },
 
    downloadImage: function(url, callback, isCrossOrigin) {     
        if (!url) return;
        if (isCrossOrigin === undefined)
            isCrossOrigin = true;
        var img = new Image();
        if (isCrossOrigin && window.location.protocol !== 'file:') {
            img.crossOrigin = 'anonymous';
        } else {
            img.crossOrigin = null;
        }
        if (img.complete && img.naturalWidth > 0 && img.src === url) {
            return img;
        } else {
            var loadCallback = function loadCallback() {
                img.removeEventListener('load', loadCallback);
                img.removeEventListener('error', errorCallback);
            };
            var errorCallback = function errorCallback() {
                img.removeEventListener('load', loadCallback);
                img.removeEventListener('error', errorCallback);
            };
            img.addEventListener('load', loadCallback);
            img.addEventListener('error', errorCallback);
            img.src = url;
        }
    },
 
 
})
 
DownloadManager.getInstance = function () {
    if (!DownloadManager.instance) {
        DownloadManager.instance = new DownloadManager();
    }
    return DownloadManager.instance;
}
 
module.exports = DownloadManager;