ModelManager.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const log$f = new Logger("model-manager")
  2. class ModelManager{
  3. constructor(e, t) {
  4. E(this, "avatarModelList", []);
  5. E(this, "skinList", []);
  6. E(this, "applicationConfig");
  7. E(this, "config");
  8. E(this, "appId");
  9. E(this, "releaseId");
  10. this.appId = e,
  11. this.releaseId = t
  12. }
  13. static getInstance(e, t) {
  14. return me.instance || (me.instance = new me(e,t)),
  15. me.instance
  16. }
  17. static findModels(e, t, r) {
  18. return e.filter(o=>o.typeName === t && o.className === r)
  19. }
  20. static findModel(e, t, r) {
  21. const n = e.filter(o=>o.typeName === t && o.className === r)[0];
  22. return n || null
  23. }
  24. async findSkinConfig(e) {
  25. let t = null;
  26. if (t = (this.skinList = await this.getSkinsList()).find(n=>n.id === e),
  27. t)
  28. return t;
  29. {
  30. const n = `skin is invalid: skinId: ${e}`;
  31. return Promise.reject(new ParamError(n))
  32. }
  33. }
  34. async findRoute(e, t) {
  35. const n = (await this.findSkinConfig(e)).routeList.find(o=>o.pathName === t);
  36. if (!n) {
  37. const o = `find path failed: skinId: ${e}, pathName: ${t}`;
  38. return Promise.reject(new ParamError(o))
  39. }
  40. return log$f.debug("find route success", n),
  41. n
  42. }
  43. async findAssetList(e) {
  44. const r = (await this.findSkinConfig(e)).assetList;
  45. if (!r) {
  46. const n = `find path failed: skinId: ${e}`;
  47. return Promise.reject(new ParamError(n))
  48. }
  49. return log$f.debug("find route success", r),
  50. r
  51. }
  52. async findAsset(e, t, r="id") {
  53. const n = await this.findSkinConfig(e);
  54. if (Array.isArray(t))
  55. return t.map(a=>n.models.find(s=>s[r] === a)).filter(Boolean);
  56. const o = n.models.find(a=>a[r] === t);
  57. if (!o) {
  58. const a = `find asset failed: skinId: ${e}, keyValue: ${t}`;
  59. return Promise.reject(new ParamError(a))
  60. }
  61. return log$f.debug("find asset success", o),
  62. o
  63. }
  64. async findPoint(e, t) {
  65. const n = (await this.findSkinConfig(e)).pointList.find(o=>o.id === t);
  66. if (!n) {
  67. const o = `find point failed: skinId: ${e}, id: ${t}`;
  68. return Promise.reject(new ParamError(o))
  69. }
  70. return log$f.debug("find point success", n),
  71. n
  72. }
  73. async requestConfig() {
  74. if (this.config)
  75. return this.config;
  76. let e = `https://static.xverse.cn/console/config/${this.appId}/config.json`;
  77. this.releaseId && (e = `https://static.xverse.cn/console/config/${this.appId}/${this.releaseId}/config.json`);
  78. const t = Xverse$1.USE_TME_CDN ? "https://static.xverse.cn/tmeland/config/tme_config.json" : e;
  79. try {
  80. const r = await http$1.get({
  81. url: `${t}?t=${Date.now()}`,
  82. key: "config",
  83. timeout: 6e3,
  84. retry: 2
  85. })
  86. , {config: n, preload: o} = r.data.data || {};
  87. if (!n)
  88. throw new Error("config data parse error" + r.data);
  89. return this.config = {
  90. config: n,
  91. preload: o
  92. },
  93. log$f.debug("get config success", this.config),
  94. this.config
  95. } catch (r) {
  96. return Promise.reject(r)
  97. }
  98. }
  99. async getApplicationConfig() {
  100. if (this.applicationConfig)
  101. return this.applicationConfig;
  102. try {
  103. const e = await this.requestConfig();
  104. return this.applicationConfig = e.config,
  105. this.applicationConfig
  106. } catch (e) {
  107. return Promise.reject(e)
  108. }
  109. }
  110. async getAvatarModelList() {
  111. if (this.avatarModelList.length)
  112. return this.avatarModelList;
  113. try {
  114. const {avatars: e} = await this.getApplicationConfig();
  115. return this.avatarModelList = e.map(t=>({
  116. name: t.name,
  117. id: t.id,
  118. modelUrl: t.url,
  119. gender: t.gender,
  120. components: t.components
  121. })),
  122. this.avatarModelList
  123. } catch (e) {
  124. return log$f.error(e),
  125. []
  126. }
  127. }
  128. async getSkinsList() {
  129. if (this.skinList.length)
  130. return this.skinList;
  131. try {
  132. const {skins: e} = await this.getApplicationConfig();
  133. return this.skinList = e.map(t=>{
  134. var r;
  135. return {
  136. name: t.name,
  137. dataVersion: t.id + t.versionId,
  138. id: t.id,
  139. fov: parseInt(t.fov || 90),
  140. models: t.assetList.map(n=>{
  141. const {assetId: o, url: a, thumbnailUrl: s, typeName: l, className: u} = n;
  142. return {
  143. id: o,
  144. modelUrl: a,
  145. name: n.name,
  146. thumbnailUrl: s,
  147. typeName: l,
  148. className: u === "\u4F4E\u6A21" ? "\u7C97\u6A21" : u
  149. }
  150. }
  151. ),
  152. routeList: (r = t.routeList) == null ? void 0 : r.map(n=>{
  153. const {areaName: o, attitude: a, id: s, pathName: l, step: u, birthPointList: c} = n;
  154. return {
  155. areaName: o,
  156. attitude: a,
  157. id: s,
  158. pathName: l,
  159. step: u,
  160. birthPointList: c.map(h=>({
  161. camera: h.camera && {
  162. position: objectParseFloat(h.camera.position),
  163. angle: objectParseFloat(h.camera.rotation)
  164. },
  165. player: h.player && {
  166. position: objectParseFloat(h.player.position),
  167. angle: objectParseFloat(h.player.rotation)
  168. }
  169. }))
  170. }
  171. }
  172. ),
  173. pointList: t.pointList.map(n=>le(oe({}, n), {
  174. position: objectParseFloat(n.position),
  175. rotation: objectParseFloat(n.rotation)
  176. })),
  177. versionId: t.versionId,
  178. isEnable: t.isEnable,
  179. assetList: t.assetList,
  180. visibleRules: t.visibleRules,
  181. animationList: t.animationList
  182. }
  183. }
  184. ),
  185. this.skinList
  186. } catch (e) {
  187. return log$f.error(e),
  188. []
  189. }
  190. }
  191. async getBreathPointTextrueList() {
  192. return [{
  193. url: TEXTURE_URL
  194. }]
  195. }
  196. }
  197. ;