Skip to content

"refId not found" encoding fixes #206

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "3.0.42",
"version": "3.0.45",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "bin/schema-codegen",
Expand Down Expand Up @@ -66,6 +66,7 @@
"@types/rimraf": "^2.0.3",
"@types/sinon": "^7.0.3",
"benchmark": "^2.1.4",
"core-js": "^3.44.0",
"flatbuffers": "^1.10.2",
"fossil-delta": "^1.0.2",
"glob": "^7.1.5",
Expand Down
30 changes: 23 additions & 7 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DEFAULT_VIEW_TAG, type DefinitionType } from "./annotations";

import { NonFunctionPropNames, ToJSON } from './types/HelperTypes';

import { ChangeSet, ChangeTree, Ref } from './encoder/ChangeTree';
import { ChangeSet, ChangeSetName, ChangeTree, Ref } from './encoder/ChangeTree';
import { $changes, $decoder, $deleteByIndex, $descriptors, $encoder, $filter, $getByIndex, $track } from './types/symbols';
import { StateView } from './encoder/StateView';

Expand Down Expand Up @@ -185,7 +185,7 @@ export class Schema {
* @param showContents display JSON contents of the instance
* @returns
*/
static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0, decoder?: Decoder) {
static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0, decoder?: Decoder, keyPrefix: string = "") {
const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
const changeTree: ChangeTree = ref[$changes];

Expand All @@ -197,15 +197,29 @@ export class Schema {
? ` [×${root.refCount[refId]}]`
: '';

let output = `${getIndent(level)}${ref.constructor.name} (refId: ${refId})${refCount}${contents}\n`;
let output = `${getIndent(level)}${keyPrefix}${ref.constructor.name} (refId: ${refId})${refCount}${contents}\n`;

changeTree.forEachChild((childChangeTree) =>
output += this.debugRefIds(childChangeTree.ref, showContents, level + 1, decoder));
changeTree.forEachChild((childChangeTree, key) => {
const keyPrefix = (ref['forEach'] !== undefined && key !== undefined) ? `["${key}"]: ` : "";
output += this.debugRefIds(childChangeTree.ref, showContents, level + 1, decoder, keyPrefix);
});

return output;
}

static debugRefIdsDecoder(decoder: Decoder) {
static debugRefIdEncodingOrder(ref: Ref, changeSet: ChangeSetName = 'allChanges') {
let encodeOrder: number[] = [];
let current = ref[$changes].root[changeSet].next;
while (current) {
if (current.changeTree) {
encodeOrder.push(current.changeTree.refId);
}
current = current.next;
}
return encodeOrder;
}

static debugRefIdsFromDecoder(decoder: Decoder) {
return this.debugRefIds(decoder.state, false, 0, decoder);
}

Expand Down Expand Up @@ -263,15 +277,17 @@ export class Schema {
static debugChangesDeep(ref: Ref, changeSetName: "changes" | "allChanges" | "allFilteredChanges" | "filteredChanges" = "changes") {
let output = "";

const rootChangeTree = ref[$changes];
const rootChangeTree: ChangeTree = ref[$changes];
const root = rootChangeTree.root;
const changeTrees: Map<ChangeTree, ChangeTree[]> = new Map();

const instanceRefIds = [];
let totalOperations = 0;

// TODO: FIXME: this method is not working as expected
for (const [refId, changes] of Object.entries(root[changeSetName])) {
const changeTree = root.changeTrees[refId];
if (!changeTree) { continue; }

let includeChangeTree = false;
let parentChangeTrees: ChangeTree[] = [];
Expand Down
23 changes: 9 additions & 14 deletions src/decoder/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,26 @@ export class Decoder<T extends Schema = any> {
if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {
it.offset++;

ref[$onDecodeEnd]?.()

const nextRefId = decode.number(bytes, it);
const nextRef = $root.refs.get(nextRefId);

//
// Trying to access a reference that haven't been decoded yet.
//
if (!nextRef) {
// throw new Error(`"refId" not found: ${nextRefId}`);
console.error(`"refId" not found: ${nextRefId}`, { previousRef: ref, previousRefId: this.currentRefId });
console.warn("Please report this to the developers. All refIds =>");
console.warn(Schema.debugRefIdsDecoder(this));
console.warn(Schema.debugRefIdsFromDecoder(this));
this.skipCurrentStructure(bytes, it, totalBytes);
}
ref[$onDecodeEnd]?.()

this.currentRefId = nextRefId;

ref = nextRef;
decoder = ref.constructor[$decoder];
} else {
ref = nextRef;
decoder = ref.constructor[$decoder];
this.currentRefId = nextRefId;
}

continue;
}
Expand Down Expand Up @@ -131,12 +133,6 @@ export class Decoder<T extends Schema = any> {
}

createInstanceOfType (type: typeof Schema): Schema {
// let instance: Schema = new (type as any)();

// // assign root on $changes
// instance[$changes].root = this.root[$changes].root;

// return instance;
return new (type as any)();
}

Expand All @@ -161,4 +157,3 @@ export class Decoder<T extends Schema = any> {
}

}

Loading