forked from wyb10a10/cocos_creator_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Node, Component } from 'cc'; | ||
import { IReplicator } from './SyncUtil'; | ||
import { createReplicator } from './ReplicatorFactory'; | ||
import { getReplicateMark } from './ReplicateMark'; | ||
|
||
class NodeReplicator implements IReplicator { | ||
private target: Node; | ||
private replicators: Map<string, IReplicator> = new Map(); | ||
private version: number = 0; | ||
|
||
constructor(target: Node) { | ||
this.target = target; | ||
this.scanNode(target); | ||
} | ||
|
||
private scanNode(node: Node): void { | ||
for (const component of node.components) { | ||
if (this.isReplicated(component)) { | ||
const key = this.getComponentKey(node, component); | ||
const replicator = createReplicator(component); | ||
if (replicator) { | ||
this.replicators.set(key, replicator); | ||
} else { | ||
console.error(`NodeReplicator scanNode error, key: ${key}`); | ||
} | ||
} | ||
} | ||
|
||
for (const child of node.children) { | ||
this.scanNode(child); | ||
} | ||
} | ||
|
||
private isReplicated(component: Component): boolean { | ||
if (getReplicateMark(component.constructor, false)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private getComponentKey(node: Node, component: Component): string { | ||
return `${this.getNodePath(node)}:${component.constructor.name}`; | ||
} | ||
|
||
private getNodePath(node: Node): string { | ||
let path = node.name; | ||
let current = node.parent; | ||
|
||
while (current) { | ||
path = `${current.name}/${path}`; | ||
current = current.parent; | ||
} | ||
|
||
return path; | ||
} | ||
|
||
genDiff(fromVersion: number, toVersion: number): any { | ||
const diff: any = {}; | ||
|
||
for (const [key, replicator] of this.replicators) { | ||
const componentDiff = replicator.genDiff(fromVersion, toVersion); | ||
if (componentDiff) { | ||
diff[key] = componentDiff; | ||
} | ||
} | ||
|
||
return Object.keys(diff).length > 0 ? diff : false; | ||
} | ||
|
||
applyDiff(diff: any): void { | ||
for (const key in diff) { | ||
const replicator = this.replicators.get(key); | ||
if (replicator) { | ||
replicator.applyDiff(diff[key]); | ||
} else { | ||
console.error(`NodeReplicator applyDiff error, key: ${key}`); | ||
} | ||
} | ||
} | ||
|
||
getVersion(): number { | ||
return this.version; | ||
} | ||
|
||
getTarget(): Node { | ||
return this.target; | ||
} | ||
|
||
setTarget(target: Node): void { | ||
this.target = target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"ver": "4.0.23", | ||
"importer": "typescript", | ||
"imported": true, | ||
"uuid": "0d64f4ba-955d-4b1d-84c0-e4a3890cf27a", | ||
"files": [], | ||
"subMetas": {}, | ||
"userData": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"ver": "1.1.0", | ||
"importer": "directory", | ||
"imported": true, | ||
"uuid": "9fcf55da-4c89-422b-883b-0131dc4e1360", | ||
"files": [], | ||
"subMetas": {}, | ||
"userData": { | ||
"compressionType": {}, | ||
"isRemoteBundle": {} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.