@hology/core 0.0.52 → 0.0.53
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
|
|
1
|
-
import e from"typedi";import{loadScene as t}from"../scene/bootstrap.js";import{ActorFactory as n}from"./actors/factory.js";import{World as s}from"./services/world.js";import{ViewController as o}from"./services/render.js";import{RenderingView as r}from"../rendering.js";import{PhysicsSystem as i}from"./services/physics/physics-system.js";import{MeshComponent as a}from"./actors/builtin/components/mesh-component.js";import{activeContainerInstance as c}from"./actors/internal/container-map.js";import{InputService as m}from"./input/index.js";import{RuntimeAssetsService as l}from"../scene/runtime-asset-service.js";import{AssetResourceLoader as d}from"../scene/asset-resource-loader.js";import{AssetLoader as p}from"./services/asset-loader.js";import{polyfillClient as h}from"./polyfill.js";import{Subject as u}from"rxjs";import{PointerEvents as f}from"./services/pointer-events.js";import{RuntimeBundledBackendService as g}from"../scene/runtime-bundled-backend-service.js";export function initiateGame(m,u){if(h(),0!=u.element.childNodes.length)return console.error("Can not initialize the game with a non-empty html element"),null;e.has(o);const w=e.of("default"),j=new HologyRuntime(w),v=new n(w,{inEditor:!1});var S;e.set(n,v),S=u.element,Object.assign(S.style,{position:"absolute",top:"0",left:"0",width:"100%",height:"100%"});const y=new r(u.element,{enableXR:!0===u.xr?.enabled,maxPixelRatio:u.rendering?.maxPixelRatio});y.renderer.shadowMap.autoUpdate=!0,null!=u?.rendering?.resolutionScale&&(y.resolutionScale=u.rendering.resolutionScale),e.set(r,y);const I=new o(y);e.set(o,I);const x=e.get(s);e.set(s,x);const b=new g,D=new l(b),R=new d;R.setDataDir(u.dataDir);const G=Object.entries(u.shaders).map((([e,t])=>({name:e,type:t}))),H=new p(R,D,G);return e.set(p,H),(async()=>{const n=e.get(i);if(await n.start(),j.isShutdown)return;if(await b.preloadData(),j.isShutdown)return;const{scene:s,actors:o}=await t(y,u.sceneName,u.dataDir,u.shaders,u.actors,v,b,D,R);if(x.scene=s,j.isShutdown)return void y.stop();e.import([a]);for(const e of o)x.addActor(e);n.addFromScene(s),y.loop((e=>{})),j.status=5,c.value=w,w.remove(m),w.set({id:m,type:m});const r=w.get(m);c.value=null,j.gameInstance=r,w.get(f).start(),r instanceof GameInstance&&r.onStart(),j._resolver(!0)})(),j}export class GameInstance{onStart(){}onShutdown(){}}export function createHologyScene(){}export class HologyRuntime{constructor(e){this.containerInstance=e,this.status=0,this.isShutdown=!1,this.shutdownStarted=new u,this.ready=new Promise((e=>{this._resolver=e}))}getWorld(){return this.containerInstance.get(s)}getService(e){return this.containerInstance.get(e)}shutdown(){this.isShutdown=!0,this.shutdownStarted.next(),this.gameInstance instanceof GameInstance&&this.gameInstance.onShutdown(),this.containerInstance.get(m).stop();const e=this.containerInstance.get(r);e?.stop();for(const e of this.getWorld().actors)this.getWorld().removeActor(e);this.containerInstance.get(i).stop(),this.containerInstance.get(f).stop(),this.containerInstance.reset()}}
|
1
|
+
import e from"typedi";import{loadScene as t}from"../scene/bootstrap.js";import{ActorFactory as n}from"./actors/factory.js";import{World as s}from"./services/world.js";import{ViewController as o}from"./services/render.js";import{RenderingView as r}from"../rendering.js";import{PhysicsSystem as i}from"./services/physics/physics-system.js";import{MeshComponent as a}from"./actors/builtin/components/mesh-component.js";import{activeContainerInstance as c}from"./actors/internal/container-map.js";import{InputService as m}from"./input/index.js";import{RuntimeAssetsService as l}from"../scene/runtime-asset-service.js";import{AssetResourceLoader as d}from"../scene/asset-resource-loader.js";import{AssetLoader as p}from"./services/asset-loader.js";import{polyfillClient as h}from"./polyfill.js";import{Subject as u}from"rxjs";import{PointerEvents as f}from"./services/pointer-events.js";import{RuntimeBundledBackendService as g}from"../scene/runtime-bundled-backend-service.js";export function initiateGame(m,u){if(h(),0!=u.element.childNodes.length)return console.error("Can not initialize the game with a non-empty html element"),null;e.has(o);const w=e.of("default"),j=new HologyRuntime(w),v=new n(w,{inEditor:!1});var S;e.set(n,v),S=u.element,Object.assign(S.style,{position:"absolute",top:"0",left:"0",width:"100%",height:"100%",overflow:"hidden"});const y=new r(u.element,{enableXR:!0===u.xr?.enabled,maxPixelRatio:u.rendering?.maxPixelRatio});y.renderer.shadowMap.autoUpdate=!0,null!=u?.rendering?.resolutionScale&&(y.resolutionScale=u.rendering.resolutionScale),e.set(r,y);const I=new o(y);e.set(o,I);const x=e.get(s);e.set(s,x);const b=new g,D=new l(b),R=new d;R.setDataDir(u.dataDir);const G=Object.entries(u.shaders).map((([e,t])=>({name:e,type:t}))),H=new p(R,D,G);return e.set(p,H),(async()=>{const n=e.get(i);if(await n.start(),j.isShutdown)return;if(await b.preloadData(),j.isShutdown)return;const{scene:s,actors:o}=await t(y,u.sceneName,u.dataDir,u.shaders,u.actors,v,b,D,R);if(x.scene=s,j.isShutdown)return void y.stop();e.import([a]);for(const e of o)x.addActor(e);n.addFromScene(s),y.loop((e=>{})),j.status=5,c.value=w,w.remove(m),w.set({id:m,type:m});const r=w.get(m);c.value=null,j.gameInstance=r,w.get(f).start(),r instanceof GameInstance&&r.onStart(),j._resolver(!0)})(),j}export class GameInstance{onStart(){}onShutdown(){}}export function createHologyScene(){}export class HologyRuntime{constructor(e){this.containerInstance=e,this.status=0,this.isShutdown=!1,this.shutdownStarted=new u,this.ready=new Promise((e=>{this._resolver=e}))}getWorld(){return this.containerInstance.get(s)}getService(e){return this.containerInstance.get(e)}shutdown(){this.isShutdown=!0,this.shutdownStarted.next(),this.gameInstance instanceof GameInstance&&this.gameInstance.onShutdown(),this.containerInstance.get(m).stop();const e=this.containerInstance.get(r);e?.stop();for(const e of this.getWorld().actors)this.getWorld().removeActor(e);this.containerInstance.get(i).stop(),this.containerInstance.get(f).stop(),this.containerInstance.reset()}}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { Observable } from 'rxjs';
|
1
2
|
interface Entity {
|
2
3
|
id: string;
|
3
4
|
name: string;
|
@@ -21,7 +22,8 @@ export declare class ObjectStorage<T extends Entity> {
|
|
21
22
|
constructor(name: string, filePathFn?: (o: ObjectIndexEntry) => string);
|
22
23
|
setBasePath(path: string): void;
|
23
24
|
private determineIfMetaFileShouldBeCreated;
|
24
|
-
watch():
|
25
|
+
watch(): Observable<T>;
|
26
|
+
private readFileIfExists;
|
25
27
|
getAll(): Promise<T[]>;
|
26
28
|
get(id: string): Promise<T>;
|
27
29
|
save(obj: T): Promise<T>;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import{randomUUID as t}from"../../utils/uuid.js";import{pathJoin as e}from"../../utils/files.js";import{
|
1
|
+
import{randomUUID as t}from"../../utils/uuid.js";import{pathJoin as e}from"../../utils/files.js";import{filter as i,firstValueFrom as a,from as s,map as n,mergeMap as r,Observable as h,Subject as o,switchMap as c,tap as l}from"rxjs";import{sleepDelay as d}from"../../utils/async.js";const u={},p={},w={};null==u.read&&window.require&&(Object.assign(u,window.require("fs")),Object.assign(p,u.promises),Object.assign(w,window.require("path")));const f=null!=u.existsSync;function m(){if(f){const t="--path=",e=window.process.argv.find((e=>e.startsWith(t)));return e?e.substring(t.length):""}return""}const y=/^[A-Z]:/;function g(...t){return 0===t.length?"":y.test(t[0])?w.join(...t):e(...t)}export class ObjectStorage{constructor(t,e){this.name=t,this.filePathFn=e,this.basePathUpdates=new o,this.basePath=this.basePathUpdates.pipe(n((t=>g(m(),t))),l((t=>{this.path=g(t,this.name),x(this.path),this.determineIfMetaFileShouldBeCreated()}))),this.loaded=a(this.basePath),this.shouldCreateIndex=!0}setBasePath(t){this.basePathUpdates.next(t)}async determineIfMetaFileShouldBeCreated(){try{(await p.readFile(g(m(),"vite.config.ts"))).toString().includes("hologyBuild")&&(this.shouldCreateIndex=!1)}catch(t){console.warn("Failed to read vite config to determine if meta files should be created")}}watch(){return P(),s(this.loaded).pipe(c((()=>function(t){return new h((e=>{const i=new AbortController,{signal:a}=i,s=p.watch(t,{recursive:!0,signal:a});return(async()=>{for await(const t of s)console.log(t),e.next(t)})(),()=>{i.abort()}}))}(this.path))),r((t=>s(this.readFileIfExists(t.filename)))),i((t=>null!=t)))}async readFileIfExists(t){const e=g(this.path,t);try{const i=await p.readFile(e);return{...JSON.parse(i.toString()),path:w.basename(t)}}catch{return console.warn("Could not find file at "+e),null}}async getAll(){if(f){await this.loaded,await x(this.path);const t=(await p.readdir(this.path,{recursive:!0,withFileTypes:!0})).filter((t=>t.isFile()&&t.name.endsWith(".json")&&!/^[\._]/.test(t.name)));return await Promise.all(t.map((t=>p.readFile(g(t.path,t.name)).then((e=>({...JSON.parse(e.toString()),path:t}))))))}const t=await this.loadIndex();return Promise.all(Object.keys(t).map((t=>this.get(t))))}async get(t){const e=await this.loadIndex(),i=e[t]??Object.values(e).find((e=>e.name===t));if(null==i)return;const a=this.privateObjectPath(i);if(!f)return(await fetch(a)).json();return await F(a)?JSON.parse((await p.readFile(a)).toString()):null}async save(t){return P(),await this.loaded,await p.writeFile(this.privateObjectPath(t),this.serialize(t)),await this.updateIndex(),t}async rename(t,e){const i={...t,name:e},a=this.privateObjectPath(t),s=this.privateObjectPath(i);try{await p.rename(a,s)}catch(t){console.error(t),console.warn("Rename failed, retrying",{currentPath:a,newPath:s}),await d(400),await p.rename(a,s)}return await this.save(i),await this.updateIndex(),i}async delete(t){await p.unlink(this.privateObjectPath(t)),this.updateIndex()}async create(e){return P(),await this.loaded,e.id=t(),await p.writeFile(this.privateObjectPath(e),this.serialize(e)),await this.updateIndex(),e}serialize(t){const e={...t};return delete e.path,JSON.stringify(e,null,2)}async updateIndex(){P();const t=await this.getAll(),e={};for(const i of t)e[i.id]={id:i.id,name:i.name??i.id,path:i.path};if(this.cachedIndex=e,f){if(!this.shouldCreateIndex)return;await p.writeFile(this.indexFilePath,JSON.stringify(e,null,2))}}get indexFilePath(){return g(this.path,"_meta.json")}async loadIndex(){return null==this.cachedIndex&&(f?await this.updateIndex():this.cachedIndex=await(await fetch(this.indexFilePath)).json()),this.cachedIndex}async ensureResourceDir(){await x(g(this.path+"-resources"))}async saveFile(t,e){return P(),await x(g(this.path+"-resources")),p.copyFile(e.path,g(this.path+"-resources",t.fileKey))}async saveExtraFile(t,e){return P(),await x(g(this.path+"-resources")),p.copyFile(t,g(this.path+"-resources",e))}getAssetPath(t){return window&&"function"==typeof window.require?window.require("path").join(this.path+"-resources",t.fileKey):g(this.path+"-resources",t.fileKey)}async replaceFile(t,e){if(await F(e))return p.copyFile(e,g(this.path+"-resources",t.fileKey));console.error("Failed to replace file using path "+e)}async deleteFile(t){if(null==t)return;P();const e=g(this.path+"-resources",t);return await F(e)?p.unlink(e):void 0}privateObjectPath(t){return this.filePathFn?g(this.path,this.filePathFn(t)):g(this.path,tokenizeName(t.name??t.id)+".json")}}export function tokenizeName(t){return t.trim().replace(/\s/g,"_").replace(/[^a-z0-9_\-\.]/gi,"")}async function x(t){f&&(await F(t)||await p.mkdir(t,{recursive:!0}))}function F(t){return!!f&&new Promise((function(e,i){u.exists(t,(function(t){e(t)}))}))}function P(){if(!f)throw new Error("Must have direct access to filesystem")}
|
2
2
|
/*
|
3
3
|
* Copyright (©) 2023. All rights reserved.
|
4
4
|
* See the LICENSE.md file for details.
|