ModelManager.js 7.3 KB

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