Skip to content

Commit

Permalink
fix property sync bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wyb10a10 committed Aug 16, 2023
1 parent 14d3e41 commit 02bf836
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions assets/Scene/example_sync_scene.scene
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
"__expectedType__": "cc.Prefab"
},
{
"__uuid__": "cf379138-1f7f-4cee-84d2-4d7a71b98f94",
"__uuid__": "fc4b58c9-e6a4-4392-9c90-1f10d66e95b4",
"__expectedType__": "cc.Prefab"
},
{
Expand All @@ -441,7 +441,7 @@
},
{
"__type__": "cc.Node",
"_name": "client",
"_name": "server",
"_objFlags": 0,
"_parent": {
"__id__": 1
Expand Down
4 changes: 4 additions & 0 deletions assets/Script/sync/ReplicateMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ export default class ReplicateMark {
// 如果没有指定SyncProperty,则添加所有属性到标记中
// 使用Keys遍历,避免遍历到其原型属性导致无限递归
for (let key of Object.keys(cls)) {
// 跳过__开头的属性
if (key.indexOf("__") == 0) {
continue;
}
this.addMark(key, cls[key]);
}
}
Expand Down
6 changes: 5 additions & 1 deletion assets/Script/sync/ReplicatorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ArrayLinkReplicator, ArrayReplicator, SimpleArrayReplicator } from "./A
import { CCVec3Replicator } from "./CocosReplicator";
import { ReplicateScanner } from "./DiffScaner";
import { ReplicateTrigger } from "./DiffTrigger";
import ReplicateMark, { ReplicateType } from "./ReplicateMark";
import ReplicateMark, { ReplicateType, getReplicateMark } from "./ReplicateMark";
import { IReplicator, isSimpleType } from "./SyncUtil";
import { SimpleSetReplicator } from "./SetReplicator";
import { HashReplicator, SimpleHashReplicator } from "./HashReplicator";
Expand Down Expand Up @@ -61,6 +61,10 @@ export function createReplicator(target: any, mark?: ReplicateMark): IReplicator
}
return null;
} else if (target instanceof Object) {
if (!mark) {
// 尝试从类的定义中获取ReplicateMark
mark = getReplicateMark(target.constructor, false);
}
if (mark) {
let objMark = mark.getObjMark();
if (objMark && objMark.Type == ReplicateType.REPLICATE_TRIGGER) {
Expand Down
6 changes: 6 additions & 0 deletions assets/Script/sync/components/ClientReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export class ClientReplicator extends Component {
}
}
instanceNode.setPosition(new Vec3(data.position.x, data.position.y, data.position.z));
if (data.data) {
const nodeSync = (instanceNode as Node).getComponent(NodeSync);
if (nodeSync) {
nodeSync.applyDiff(data.data);
}
}
console.log(`sync instance ${data.instanceId}`);
}
}
Expand Down
6 changes: 4 additions & 2 deletions assets/Script/sync/components/ServerReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ServerReplicator extends Component {
public prefabs: Prefab[] = [];

private instanceCounter: number = 0;
private serverVersion: number = 0;
private serverVersion: number = -1;

public createRandomPrefab() {
const randomIndex = Math.floor(Math.random() * this.prefabs.length);
Expand Down Expand Up @@ -44,9 +44,11 @@ export class ServerReplicator extends Component {
syncData.push({
instanceId: nodeSync.instanceId,
prefabPath: nodeSync.prefabPath,
position: nodeSync.node.position
position: nodeSync.node.position,
data: nodeSync.genDiff(this.serverVersion, ++this.serverVersion),
});
}
console.log(`generate sync data ${JSON.stringify(syncData)}`);
return syncData;
}

Expand Down

0 comments on commit 02bf836

Please sign in to comment.