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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

FreezeEngine
Copy link

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/prismarine-block@1.17.1 Transitive: filesystem, shell +11 517 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
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
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
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
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
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
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

@rom1504 rom1504 added this to Needs triage in PR Triage Apr 19, 2024
@rom1504 rom1504 moved this from Needs triage to Waiting for user input in PR Triage Apr 19, 2024
@FreezeEngine
Copy link
Author

Should anything else be added/modified?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
PR Triage
  
Waiting for user input
Development

Successfully merging this pull request may close these issues.

None yet

3 participants