public Dictionary <PCFResourceType, DataBlockBase> Serialize(object[] serializeOptions)
        {
            //Holds and indexes all serialized assets/resources.
            SerializedAssets serializedAssets = new SerializedAssets(this.destinationDirectory);

            //Allow serialized nodes to define actions to do after the entire tree has been serialized.
            List <Action <NodeBase> > postSerializeActions = new List <Action <NodeBase> >();

            //Populate dataBlocks with stuff.
            SerializeRecursivly(this.selectedGameObject, serializedAssets, serializeOptions, this.rootNode, postSerializeActions);

            //Run all post serialize actions.
            foreach (Action <NodeBase> action in postSerializeActions)
            {
                action(this.rootNode);
            }

            //Tracking object, used to debug what happens during serialization.
            ISerializeLogging logger = new NodeCreationDebugger();

            //Recursivly create serialized representations of node tree. (Aka NodeResource objects)
            NodeResource serializedRootNode = this.rootNode.Serialize(serializedAssets, null, logger);

            //Lets see...
            logger.PrintResult();

            Dictionary <PCFResourceType, DataBlockBase> dataBlocks = serializedAssets.GetDataBlocks();

            //Create nodeblock and set it to the datablocks dictionary.
            DataBlockBase nodeBlock = new NodeBlock(serializedRootNode);

            dataBlocks.Add(PCFResourceType.NODE, nodeBlock);

            return(dataBlocks);
        }
Beispiel #2
0
        public static UnityRootNode CreateNodeTree(Dictionary <PCFResourceType, DataBlockBase> dataBlocks, IFileHandle filePath, NodeFactory nodeFactory)
        {
            NodeBlock nodeBlock = dataBlocks[PCFResourceType.NODE] as NodeBlock;

            //Create and hook up node graph, but do no actual deserialization.
            UnityNodeBase node = nodeBlock.RecreateNodeGraph <UnityNodeBase>(nodeFactory);

            UnityRootNode rootNode = node as UnityRootNode;

            //Give rootnode a reference to the file we operate on, some nodes stream their data from this file.
            rootNode.SetFile(filePath);

            return(rootNode);
        }
Beispiel #3
0
        void LoadDataFromDisk(PCFResourceType loadFlags, bool streamResources, bool assemblyMode)
        {
            Stream file = this.path.GetFileStream(FileMode.Open);

            //Load header first.
            this.fileHeader = LoadHeader(file);

            int bytesRead  = HeaderByteLength;
            int fileLength = (int)file.Length;

            while (bytesRead < fileLength)
            {
                int             chunkLength  = GetNextSegmentLength(file);
                PCFBlockTypes   blockType    = GetNextBlockType(file);
                DataBlockBase   block        = null;
                PCFResourceType resourceType = PCFResourceType.NONE;

                if (blockType == PCFBlockTypes.INDEXBLOCK)
                {
                    block = new IndexBlock();

                    this.blockData.Add(PCFResourceType.INDEX, block);
                }
                else if (blockType == PCFBlockTypes.NODEBLOCK)
                {
                    block = new NodeBlock(loadFlags);

                    this.blockData.Add(PCFResourceType.NODE, block);
                }
                else if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                {
                    //Read 4 bytes to determine resourcetype.
                    file.Read(INT_BUFFER, 0, ChunkByteLength);
                    int rawResourceTypeValue = BitConverter.ToInt32(INT_BUFFER, 0);
                    resourceType = (PCFResourceType)Enum.ToObject(typeof(PCFResourceType), rawResourceTypeValue);

                    //Allows us to mask what blocks to read in from file.
                    if ((resourceType & loadFlags) != 0)
                    {
                        block = new ResourceBlock(resourceType, streamResources);
                        this.blockData.Add(resourceType, block);
                    }
                }
                else
                {
                    Debug.LogError("Unknown block type");
                }

                //Increment file position, make sure to count the chunk size bytes.
                bytesRead += chunkLength + ChunkByteLength;

                if (block != null)
                {
                    if (memStream)
                    {
                        int bytsOffset = ChunkByteLength;
                        if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                        {
                            bytsOffset += ChunkByteLength;
                        }

                        long   fileStreamPos = file.Position;
                        byte[] streamBytes   = new byte[chunkLength - bytsOffset];
                        file.Read(streamBytes, 0, chunkLength - bytsOffset);

                        Stream stream = new MemoryStream(streamBytes);

                        block.SetBytes(stream, chunkLength, fileStreamPos, assemblyMode);
                    }
                    else
                    {
                        //Will internally read from file and increment its position.
                        block.SetBytes(file, chunkLength, 0, assemblyMode);
                    }
                }
                else
                {
                    //Manually seek to next chunk incase SetBytes was never called.
                    file.Seek(bytesRead, SeekOrigin.Begin);
                }
            }

            if (file != null)
            {
                //Stop reading pcf file.
                file.Close();
            }
        }