ModelManager.js 7.4 KB

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