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
"use strict";
cc._RF.push(module, '2ce03npZLpOF7g2NUPT++6l', 'battle_res_pool');
// Scripts/mod/battle/battle_res_pool.js
 
"use strict";
 
var GC_TIME = 300; // 动画资源的缓存池
 
var BattleResPool = function BattleResPool() {
  this._loaddingRes = {}; // 加载中的资源
 
  this._finishRes = {}; // 加载完成的资源
 
  this._rescounter = {};
  this._delres = {};
  this.mainloop_timer = gcore.Timer.set(function () {
    this.update(10);
  }.bind(this), 10000, -1);
  this._test_load = {};
  this._test_del = {};
};
 
var proto = BattleResPool.prototype;
 
proto.preLoadRes = function (path, callback) {
  this.getRes(path, callback, true);
};
 
proto.getRes = function (path, callback, is_pre) {
  if (!path || !callback) return; // if (!is_pre) {
  //     if (!this._test_load[path])
  //         this._test_load[path] = 0;
  //     this._test_load[path]++        
  // }
 
  if (this._delres[path]) {
    delete this._delres[path];
  }
 
  if (this._finishRes[path]) {
    callback(this._finishRes[path]);
 
    if (!is_pre) {
      if (!this._rescounter[path]) this._rescounter[path] = 0;
      this._rescounter[path]++;
    }
 
    return;
  } else {
    var load_info = {};
    load_info.callback = callback;
    load_info.is_pre = is_pre;
 
    if (this._loaddingRes[path] && this._loaddingRes[path].length > 0) {
      this._loaddingRes[path].push(load_info);
 
      return;
    } else {
      this._loaddingRes[path] = [];
 
      this._loaddingRes[path].push(load_info);
    }
  }
 
  LoaderManager.getInstance().loadRes(path, function (path, res_object) {
    this._finishRes[path] = res_object;
 
    for (var callback_i in this._loaddingRes[path]) {
      var load_info = this._loaddingRes[path][callback_i];
      if (load_info.callback) load_info.callback(res_object);
 
      if (!load_info.is_pre) {
        // 如果不是预加载的则进行引用计数
        if (!this._rescounter[path]) this._rescounter[path] = 0;
        this._rescounter[path]++;
        if (this._delres[path]) delete this._delres[path];
      } else {
        // 如果是预加载的暂时放到删除队列
        if (!this._rescounter[path] || this._rescounter[path] <= 0) this.delRes(path);
      }
    }
 
    this._loaddingRes[path] = [];
  }.bind(this, path));
};
 
proto.delRes = function (path) {
  if (this._rescounter[path] && this._rescounter[path] > 0) {
    this._rescounter[path]--;
 
    if (this._rescounter[path] <= 0) {
      if (!this._delres[path]) this._delres[path] = GC_TIME;
      delete this._rescounter[path];
    } // if (!this._test_del[path])
    //     this._test_del[path] = 0;
    // this._test_del[path]++
 
  }
};
 
proto.update = function (dt) {
  for (var res_i in this._delres) {
    this._delres[res_i] -= dt;
 
    if (this._delres[res_i] < 0) {
      LoaderManager.getInstance().releaseRes(res_i);
      delete this._delres[res_i];
    }
  }
};
 
BattleResPool.getInstance = function () {
  if (!BattleResPool.instance) {
    BattleResPool.instance = new BattleResPool();
  }
 
  return BattleResPool.instance;
};
 
module.exports = BattleResPool;
 
cc._RF.pop();