Skip to content

Commit

Permalink
Release again (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Mar 21, 2024
2 parents c2ff35b + dba4043 commit e94d4d7
Show file tree
Hide file tree
Showing 30 changed files with 496 additions and 186 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/next-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Vercel Deploy Next
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
push:
branches:
- next
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Global Dependencies
run: npm install --global vercel pnpm
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
- run: pnpm build-storybook
- name: Copy playground files
run: node prismarine-viewer/esbuild.mjs && cp prismarine-viewer/public/index.html .vercel/output/static/playground.html && cp prismarine-viewer/public/playground.js .vercel/output/static/playground.js
- name: Download Generated Sounds map
run: node scripts/downloadSoundsMap.mjs
- name: Deploy Project Artifacts to Vercel
uses: mathiasvr/[email protected]
with:
run: vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}
id: deploy
- name: Set deployment alias
run: vercel alias set ${{ steps.deploy.outputs.stdout }} ${{ secrets.TEST_PREVIEW_DOMAIN }} --token=${{ secrets.VERCEL_TOKEN }}
# - uses: mshick/add-pr-comment@v2
# with:
# message: |
# Deployed to Vercel Preview: ${{ steps.deploy.outputs.stdout }}
2 changes: 1 addition & 1 deletion assets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"icons": [
{
"src": "favicon.png",
"sizes": "512x512"
"sizes": "720x720"
}
],
"background_color": "#349474",
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
"esbuild": "^0.19.3",
"esbuild-plugin-polyfill-node": "^0.3.0",
"express": "^4.18.2",
"flying-squid": "npm:@zardoy/flying-squid@^0.0.10",
"flying-squid": "npm:@zardoy/flying-squid@^0.0.15",
"fs-extra": "^11.1.1",
"google-drive-browserfs": "github:zardoy/browserfs#google-drive",
"iconify-icon": "^1.0.8",
"jszip": "^3.10.1",
"lit": "^2.8.0",
Expand All @@ -61,6 +62,7 @@
"node-gzip": "^1.1.2",
"peerjs": "^1.5.0",
"pretty-bytes": "^6.1.1",
"prismarine-provider-anvil": "github:zardoy/prismarine-provider-anvil#everything",
"qrcode.react": "^3.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -74,8 +76,7 @@
"title-case": "3.x",
"ua-parser-js": "^1.0.37",
"valtio": "^1.11.1",
"workbox-build": "^7.0.0",
"google-drive-browserfs": "github:zardoy/browserfs#google-drive"
"workbox-build": "^7.0.0"
},
"devDependencies": {
"@storybook/addon-essentials": "^7.4.6",
Expand Down
24 changes: 17 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 62 additions & 6 deletions prismarine-viewer/viewer/lib/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function getEntityMesh (entity, scene, options, overrides) {
}

export class Entities extends EventEmitter {
constructor (scene) {
constructor(scene) {
super()
this.scene = scene
this.entities = {}
Expand All @@ -88,6 +88,8 @@ export class Entities extends EventEmitter {
this.onSkinUpdate = () => { }
this.clock = new THREE.Clock()
this.visible = true
this.itemsTexture = null
this.getItemUv = undefined
}

clear () {
Expand All @@ -110,7 +112,7 @@ export class Entities extends EventEmitter {
}
}

setVisible(visible, /** @type {THREE.Object3D?} */entity = null) {
setVisible (visible, /** @type {THREE.Object3D?} */entity = null) {
this.visible = visible
for (const mesh of entity ? [entity] : Object.values(this.entities)) {
mesh.visible = visible
Expand Down Expand Up @@ -205,9 +207,9 @@ export class Entities extends EventEmitter {
if (!playerObject.backEquipment) {
playerObject.backEquipment = 'cape'
}
}, () => {})
}, () => { })
}
}, () => {})
}, () => { })


playerObject.cape.visible = false
Expand Down Expand Up @@ -258,7 +260,60 @@ export class Entities extends EventEmitter {
if (!this.entities[entity.id] && !entity.delete) {
const group = new THREE.Group()
let mesh
if (entity.name === 'player') {
if (entity.name === 'item') {
/** @type {any} */
//@ts-ignore
const item = entity.metadata?.find(m => typeof m === 'object' && m !== null && m.itemCount)
if (item) {
const textureUv = this.getItemUv?.(item.itemId ?? item.blockId)
if (textureUv) {
const { u, v, size, su, sv, texture } = textureUv
const itemsTexture = texture.clone()
itemsTexture.flipY = true
itemsTexture.offset.set(u, 1 - v - (sv ?? size))
itemsTexture.repeat.set(su ?? size, sv ?? size)
itemsTexture.needsUpdate = true
itemsTexture.magFilter = THREE.NearestFilter
itemsTexture.minFilter = THREE.NearestFilter
const itemsTextureFlipped = itemsTexture.clone()
itemsTextureFlipped.repeat.x *= -1
itemsTextureFlipped.needsUpdate = true
itemsTextureFlipped.offset.set(u + (su ?? size), 1 - v - (sv ?? size))
const material = new THREE.MeshStandardMaterial({
map: itemsTexture,
transparent: true,
alphaTest: 0.1,
})
const materialFlipped = new THREE.MeshStandardMaterial({
map: itemsTextureFlipped,
transparent: true,
alphaTest: 0.1,
})
mesh = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 0.0), [
// top left and right bottom are black box materials others are transparent
new THREE.MeshBasicMaterial({ color: 0x000000 }), new THREE.MeshBasicMaterial({ color: 0x000000 }),
new THREE.MeshBasicMaterial({ color: 0x000000 }), new THREE.MeshBasicMaterial({ color: 0x000000 }),
material, materialFlipped
])
mesh.scale.set(0.5, 0.5, 0.5)
mesh.position.set(0, 0.2, 0)
// set faces
// mesh.position.set(targetPos.x + 0.5 + 2, targetPos.y + 0.5, targetPos.z + 0.5)
// viewer.scene.add(mesh)
const clock = new THREE.Clock()
mesh.onBeforeRender = () => {
const delta = clock.getDelta()
mesh.rotation.y += delta
}
//@ts-ignore
group.additionalCleanup = () => {
// important: avoid texture memory leak and gpu slowdown
itemsTexture.dispose()
itemsTextureFlipped.dispose()
}
}
}
} else if (entity.name === 'player') {
// CREATE NEW PLAYER ENTITY
const wrapper = new THREE.Group()
/** @type {PlayerObject & { animation?: PlayerAnimation }} */
Expand Down Expand Up @@ -331,7 +386,7 @@ export class Entities extends EventEmitter {
}
}
// not player
const displayText = entity.metadata?.[3] && this.displaySimpleText(entity.metadata[2]);
const displayText = entity.metadata?.[3] && this.displaySimpleText(entity.metadata[2])
if (entity.name !== 'player' && displayText) {
addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh'))
}
Expand Down Expand Up @@ -379,6 +434,7 @@ export class Entities extends EventEmitter {
}

if (entity.delete && e) {
if (e.additionalCleanup) e.additionalCleanup()
this.emit('remove', entity)
this.scene.remove(e)
dispose3(e)
Expand Down
2 changes: 1 addition & 1 deletion prismarine-viewer/viewer/lib/worldDataEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class WorldDataEmitter extends EventEmitter {
this.loadedChunks[`${pos.x},${pos.z}`] = true
}
} else {
console.debug('skipped loading chunk', dx, dz, '>', this.viewDistance)
// console.debug('skipped loading chunk', dx, dz, '>', this.viewDistance)
}
}

Expand Down
4 changes: 1 addition & 3 deletions prismarine-viewer/viewer/prepare/atlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ export type JsonAtlas = {
[file: string]: {
u: number,
v: number,
su: number,
sv: number
}
}
}

export const makeTextureAtlas = (input: string[], getInputData: (name) => {contents: string, tileWidthMult?: number}, tilesCount = input.length, suSvOptimize: 'remove' | null = null): {
export const makeTextureAtlas = (input: string[], getInputData: (name) => { contents: string, tileWidthMult?: number }, tilesCount = input.length, suSvOptimize: 'remove' | null = null): {
image: Buffer,
canvas: Canvas,
json: JsonAtlas
Expand Down
50 changes: 45 additions & 5 deletions src/browserfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,30 @@ browserfs.configure({
})

export const forceCachedDataPaths = {}
export const forceRedirectPaths = {}

window.fs = fs
//@ts-expect-error
fs.promises = new Proxy(Object.fromEntries(['readFile', 'writeFile', 'stat', 'mkdir', 'rmdir', 'unlink', 'rename', /* 'copyFile', */'readdir'].map(key => [key, promisify(fs[key])])), {
get (target, p: string, receiver) {
if (!target[p]) throw new Error(`Not implemented fs.promises.${p}`)
return (...args) => {
// browser fs bug: if path doesn't start with / dirname will return . which would cause infinite loop, so we need to normalize paths
if (typeof args[0] === 'string' && !args[0].startsWith('/')) args[0] = '/' + args[0]
const toRemap = Object.entries(forceRedirectPaths).find(([from]) => args[0].startsWith(from))
if (toRemap) {
args[0] = args[0].replace(toRemap[0], toRemap[1])
}
// Write methods
// todo issue one-time warning (in chat I guess)
if (fsState.isReadonly) {
const readonly = fsState.isReadonly && !(args[0].startsWith('/data') && !fsState.inMemorySave) // allow copying worlds from external providers such as zip
if (readonly) {
if (oneOf(p, 'readFile', 'writeFile') && forceCachedDataPaths[args[0]]) {
if (p === 'readFile') {
return Promise.resolve(forceCachedDataPaths[args[0]])
} else if (p === 'writeFile') {
forceCachedDataPaths[args[0]] = args[1]
console.debug('Skipped writing to readonly fs', args[0])
return Promise.resolve()
}
}
Expand Down Expand Up @@ -322,7 +330,19 @@ export const possiblyCleanHandle = (callback = () => { }) => {
}
}

export const copyFilesAsyncWithProgress = async (pathSrc: string, pathDest: string) => {
export const copyFilesAsyncWithProgress = async (pathSrc: string, pathDest: string, throwRootNotExist = true) => {
const stat = await existsViaStats(pathSrc)
if (!stat) {
if (throwRootNotExist) throw new Error(`Cannot copy. Source directory ${pathSrc} does not exist`)
console.debug('source directory does not exist', pathSrc)
return
}
if (!stat.isDirectory()) {
await fs.promises.writeFile(pathDest, await fs.promises.readFile(pathSrc))
console.debug('copied single file', pathSrc, pathDest)
return
}

try {
setLoadingScreenStatus('Copying files')
let filesCount = 0
Expand All @@ -339,21 +359,35 @@ export const copyFilesAsyncWithProgress = async (pathSrc: string, pathDest: stri
}
}))
}
console.debug('Counting files', pathSrc)
await countFiles(pathSrc)
console.debug('counted', filesCount)
let copied = 0
await copyFilesAsync(pathSrc, pathDest, (name) => {
copied++
setLoadingScreenStatus(`Copying files (${copied}/${filesCount}) ${name}...`)
setLoadingScreenStatus(`Copying files (${copied}/${filesCount}): ${name}`)
})
} finally {
setLoadingScreenStatus(undefined)
}
}

export const existsViaStats = async (path: string) => {
try {
return await fs.promises.stat(path)
} catch (e) {
return false
}
}

export const copyFilesAsync = async (pathSrc: string, pathDest: string, fileCopied?: (name) => void) => {
// query: can't use fs.copy! use fs.promises.writeFile and readFile
const files = await fs.promises.readdir(pathSrc)

if (!await existsViaStats(pathDest)) {
await fs.promises.mkdir(pathDest, { recursive: true })
}

// Use Promise.all to parallelize file/directory copying
await Promise.all(files.map(async (file) => {
const curPathSrc = join(pathSrc, file)
Expand All @@ -365,8 +399,14 @@ export const copyFilesAsync = async (pathSrc: string, pathDest: string, fileCopi
await copyFilesAsync(curPathSrc, curPathDest, fileCopied)
} else {
// Copy file
await fs.promises.writeFile(curPathDest, await fs.promises.readFile(curPathSrc))
fileCopied?.(file)
try {
await fs.promises.writeFile(curPathDest, await fs.promises.readFile(curPathSrc))
console.debug('copied file', curPathSrc, curPathDest)
} catch (err) {
console.error('Error copying file', curPathSrc, curPathDest, err)
throw err
}
fileCopied?.(curPathDest)
}
}))
}
Expand Down
Loading

0 comments on commit e94d4d7

Please sign in to comment.