Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hashed block network ID's #33

Merged
merged 14 commits into from
Jan 4, 2025

Conversation

FreezeEngine
Copy link
Contributor

Aims to add support for hashed block network ids.
WIP so more commits to follow

Comment on lines 64 to 81
function fnv1a32(s) {

//https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
const FNV1_OFFSET_32 = 0x811c9dc5
const FNV1_PRIME_32 = 0x01000193

var h = FNV1_OFFSET_32
for (let i = 0; i < s.length; i++) {

h ^= s.charCodeAt(i) & 0xff;
//h *= FNV1_PRIME_32; //<-- this does not work in nodejs
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}

return h & h
//return h >>> 0
//return h
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to prismarine-block

blockStatesByRuntimeId[hash] = Number(stateId)
}

data.blockStatesByRuntimeId = blockStatesByRuntimeId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be data.blocksByRuntimeId and similarly exposed for mcpc equal to data.blocksByStateId

Copy link

socket-security bot commented Mar 23, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] Transitive: filesystem, shell +12 550 kB rom1504

View full report↗︎

Comment on lines 86 to 88
for (const key of Object.keys(block.states).sort()) {
states[key] = block.states[key]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to pblock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that be a field or a function? Should it be only avalieble if blockHashes supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i understand, keys should be pre-sorted in pblock

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question. All prismarine-registry should be doing is

  function loadHashedRuntimeIds () {
    const Block = require('prismarine-block')(this)
    data.blocksByRuntimeId = {}
    for (let i = 0; i < data.blockStates.length; i++) {
      const { name, states } = data.blockStates[i]
      const hash = Block.getHash(name, states)
      data.blocksByRuntimeId[hash] = data.blocksByStateId[i]
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlocksByState have stateId excluded, should indexers keep that field/new indexer that keeps indexing field? Or technically since its sequencial index of hash in runtimeIds would give stateId?

Copy link
Member

@extremeheat extremeheat Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JavaScript, objects are passed by reference, not by copy. This means when you mutate one reference (like adding a .stateId to it) then all other references will also be updated. This effectively means iterating over blocksByStateId or any other blocks array and mutating a block object in one of them will update it everywhere else.

We don't want updates in prismarine-registry to blocks to affect the static data exposed by node-minecraft-data, so an explicit copy would need to be made before any edits. That's what I was mentioning in the prismarine-chunk comment

@FreezeEngine
Copy link
Contributor Author

Still reqires either stateId setter or a map from hash to stateId

Comment on lines 42 to 57
loadHashedRuntimeIds () {
const Block = require('prismarine-block')(this)
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
const { name, states } = data.blockStates[i]
const hash = Block.getHash(name, states)
data.blocksByRuntimeId[hash] = { stateId: i, ...data.blocksByStateId[i] }
}
},

loadRuntimeIds () {
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
data.blocksByRuntimeId[i] = { stateId: i, ...data.blocksByStateId[i] }
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things that return {}'ed inside this function are apart of the exposed prismarine-registry API. You should put non-API functions outside the returned object

@@ -1,26 +1,13 @@
const buildIndexFromArray = require('../indexer')

module.exports = (data) => {
return {
Copy link
Member

@extremeheat extremeheat Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean put the non-API helper functions before this return statement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you make a change to the PR that needs review, feel free to comment so we can get notified of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thank you, will note

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you make a change to the PR that needs review, feel free to comment so we can get notified of it

Might've done it

@FreezeEngine
Copy link
Contributor Author

Should anything else be added/modified?

@Slauh
Copy link

Slauh commented Jan 3, 2025

bump

@FreezeEngine
Copy link
Contributor Author

@extremeheat seems fine to me now

@rom1504 rom1504 merged commit f3bc5c6 into PrismarineJS:master Jan 4, 2025
3 checks passed
@rom1504
Copy link
Member

rom1504 commented Jan 4, 2025

/makerelease

@rom1504bot rom1504bot mentioned this pull request Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

5 participants