Beispiel #1
0
        public CPtr CreatePtr(CR2WChunk in_chunk, string type, CR2WChunk to_chunk, string varname = "")
        {
            var var = CreatePtr(type, to_chunk, varname);

            in_chunk.data.AddVariable(var);
            return(var);
        }
Beispiel #2
0
        public CPtr CreatePtr(CVector in_vector, string type, CR2WChunk to_chunk, string varname = "")
        {
            var var = CreatePtr(type, to_chunk, varname);

            in_vector.AddVariable(var);
            return(var);
        }
Beispiel #3
0
        public CPtr CreatePtr(CArray in_array, CR2WChunk to_chunk, string varname = "")
        {
            var var = CreatePtr("", to_chunk, varname);

            in_array.AddVariable(var);
            return(var);
        }
Beispiel #4
0
        public CHandle CreateHandle(
            CR2WChunk in_chunk,
            string type,
            string handle,
            string varname = "")
        {
            var handle1 = CreateHandle(type, handle, varname);

            in_chunk.data.AddVariable(handle1);
            return(handle1);
        }
Beispiel #5
0
        public CPtr CreatePtr(string type, CR2WChunk tochunk, string varname = "")
        {
            var cptr = new CPtr(this);

            cptr.Name = varname;
            cptr.Type = type;
            if (tochunk != null)
            {
                cptr.val = chunks.IndexOf(tochunk) + 1;
            }
            return(cptr);
        }
Beispiel #6
0
        public CR2WChunk CreateChunk(string type, CR2WChunk parent = null)
        {
            var cr2Wchunk = new CR2WChunk(this);

            cr2Wchunk.Type = type;
            cr2Wchunk.CreateDefaultData();
            if (parent != null)
            {
                cr2Wchunk.ParentChunkId = (uint)(chunks.IndexOf(parent) + 1);
            }
            chunks.Add(cr2Wchunk);
            return(cr2Wchunk);
        }
Beispiel #7
0
        public CR2WChunk CreateChunk(string type, CVariable data, CR2WChunk parent = null)
        {
            var chunk = new CR2WChunk(this);

            chunk.Type = type;
            chunk.data = data;
            if (parent != null)
            {
                chunk.ParentChunkId = (uint)chunks.IndexOf(parent) + 1;
            }

            chunks.Add(chunk);
            return(chunk);
        }
        public static CVariable GetVariableByName(this CR2WChunk arr, CR2WFile file, string name)
        {
            if (arr.data is CVector)
            {
                var vdata = (CVector)arr.data;

                for (var i = 0; i < vdata.variables.Count; i++)
                {
                    if (vdata.variables[i].Name == name)
                    {
                        return(vdata.variables[i]);
                    }
                }
            }
            return(null);
        }
        public static void CreateConnection(this CR2WChunk chunk, string in_name, string out_name, CR2WChunk out_target)
        {
            var cachedConnections = (CArray)chunk.GetVariableByName("cachedConnections");

            if (cachedConnections == null)
            {
                cachedConnections =
                    (CArray)chunk.cr2w.CreateVariable(chunk, "array:2,0,SCachedConnections", "cachedConnections");
            }

            {
                // connection 1

                var connection = (CVector)cachedConnections.array.Find(delegate(CVariable item)
                {
                    var vec = (CVector)item;
                    if (vec == null)
                    {
                        return(false);
                    }

                    var socketId = (CName)vec.GetVariableByName("socketId");
                    return(socketId != null && socketId.Value == in_name);
                });

                if (connection == null)
                {
                    connection = chunk.cr2w.CreateVector(cachedConnections);
                    ((CName)chunk.cr2w.CreateVariable(connection, "CName", "socketId")).Value = in_name;
                }


                var blocks = (CArray)connection.GetVariableByName("blocks");

                if (blocks == null)
                {
                    blocks = (CArray)chunk.cr2w.CreateVariable(connection, "array:2,0,SBlockDesc", "blocks");
                }

                var block = chunk.cr2w.CreateVector(blocks);
                chunk.cr2w.CreatePtr(block, "ptr:CQuestGraphBlock", out_target, "ock");
                ((CName)chunk.cr2w.CreateVariable(block, "CName", "putName")).Value = out_name;
            }
        }
Beispiel #10
0
        private void OnCopyStorySceneSection(CR2WChunk chunkcopy)
        {
            var storysection = (CStorySceneSection)chunkcopy.data;

            if (StorySceneRemoveAddFacts)
            {
                removeStorySceneAddFacts(storysection);
            }

            if (StorySceneRemoveUnnessaryTeleportation)
            {
                removeUnnessaryTeleportation(storysection);
            }

            if (StorySceneCopyDialogsets)
            {
                copyDialogset(storysection);
            }

            if (StorySceneCopyCameras)
            {
                copyStorySceneCameras(storysection);
            }
        }
Beispiel #11
0
 public bool RemoveChunk(CR2WChunk chunk)
 {
     return(chunks.Remove(chunk));
 }
Beispiel #12
0
        public void Read(BinaryReader file)
        {
            var mw = file.ReadBytes(4);

            if (!IDString.SequenceEqual(mw))
            {
                throw new InvalidFileTypeException("Invalid file type");
            }

            FileVersion = file.ReadUInt32();
            flags       = file.ReadUInt32();
            Datetime.Read(file, 0);
            buildversion = file.ReadUInt32();

            cr2wsize   = file.ReadUInt32();
            buffersize = file.ReadUInt32();

            crc32     = file.ReadUInt32();
            numchunks = file.ReadUInt32();

            headers = new List <CR2WHeaderData>();

            for (var i = 0; i < 10; i++)
            {
                var head = new CR2WHeaderData(file);
                headers.Add(head);

                if (head.size > 0 && i > 6)
                {
                    // Unhandled blocks
                    Debugger.Break();
                }
            }

            var string_buffer_start = headers[0].offset;

            // Read Strings
            {
                strings = new List <CR2WHeaderString>();

                var string_start = headers[1].offset;
                var string_count = headers[1].size;

                file.BaseStream.Seek(string_start, SeekOrigin.Begin);

                for (var i = 0; i < string_count; i++)
                {
                    var str = new CR2WHeaderString(file);
                    str.ReadString(file, string_buffer_start);
                    strings.Add(str);
                }
            }

            // Read Handles
            {
                handles = new List <CR2WHeaderHandle>();

                var handles_start = headers[2].offset;
                var handles_count = headers[2].size;

                file.BaseStream.Seek(handles_start, SeekOrigin.Begin);

                for (var i = 0; i < handles_count; i++)
                {
                    var handle = new CR2WHeaderHandle(file);
                    handle.ReadString(file, string_buffer_start);
                    handles.Add(handle);
                }
            }

            // TODO: Figure out where these things belong, possibly block 4 or 7?
            //file.BaseStream.Seek(handles[handles.Count - 1].offset + string_buffer_start, SeekOrigin.Begin);
            //file.ReadCR2WString();
            //unknownstrings = new List<string>();
            //while (file.BaseStream.Position < headers[1].offset)
            //{
            //   unknownstrings.Add(file.ReadCR2WString());
            //}

            // Read Unknown Block 4
            {
                block4 = new List <CR2WHeaderBlock4>();

                var block4_start = headers[3].offset;
                var block4_count = headers[3].size;

                file.BaseStream.Seek(block4_start, SeekOrigin.Begin);

                for (var i = 0; i < block4_count; i++)
                {
                    var block = new CR2WHeaderBlock4(file);
                    block4.Add(block);
                }
            }

            // Read Chunks
            {
                chunks = new List <CR2WChunk>();

                var chunk_start = headers[4].offset;
                var chunk_count = headers[4].size;

                file.BaseStream.Seek(chunk_start, SeekOrigin.Begin);

                for (var i = 0; i < chunk_count; i++)
                {
                    var chunktypeId = file.ReadUInt16();

                    var chunk = new CR2WChunk(this);
                    chunk.typeId = chunktypeId;
                    chunk.Read(file);
                    chunks.Add(chunk);
                }
            }

            // Read Unknown Block 6
            {
                block6 = new List <CR2WHeaderBlock6>();

                var block6_start = headers[5].offset;
                var block6_count = headers[5].size;

                file.BaseStream.Seek(block6_start, SeekOrigin.Begin);

                for (var i = 0; i < block6_count; i++)
                {
                    var block = new CR2WHeaderBlock6(file);
                    block6.Add(block);
                }
            }


            // Read Unknown Block 7
            {
                block7 = new List <CR2WHeaderBlock7>();

                var block7_start = headers[6].offset;
                var block7_count = headers[6].size;

                file.BaseStream.Seek(block7_start, SeekOrigin.Begin);

                for (var i = 0; i < block7_count; i++)
                {
                    var block = new CR2WHeaderBlock7(file);
                    block7.Add(block);
                }
            }

            // Read Chunk buffers
            {
                for (var i = 0; i < chunks.Count; i++)
                {
                    chunks[i].ReadData(file);
                }
            }

            // Read Block7 buffers
            {
                for (var i = 0; i < block7.Count; i++)
                {
                    block7[i].ReadString(file, string_buffer_start);
                    block7[i].ReadData(file);
                }
            }

            file.BaseStream.Seek(cr2wsize, SeekOrigin.Begin);

            var actualbuffersize = (int)(buffersize - cr2wsize);

            if (actualbuffersize > 0)
            {
                bufferdata = new byte[actualbuffersize];
                file.BaseStream.Read(bufferdata, 0, actualbuffersize);
            }
        }
Beispiel #13
0
        public void Read(BinaryReader file)
        {
            var numArray = file.ReadBytes(4);

            if (!IDString.SequenceEqual(numArray))
            {
                throw new InvalidFileTypeException("Invalid file type");
            }
            FileVersion = file.ReadUInt32();
            unk2        = file.ReadUInt32();
            unk3        = file.ReadUInt32();
            unk4        = file.ReadUInt32();
            unk5        = file.ReadUInt32();
            cr2wsize    = file.ReadUInt32();
            buffersize  = file.ReadUInt32();
            unk6        = file.ReadUInt32();
            unk7        = file.ReadUInt32();
            headers     = new List <CR2WHeaderData>();
            for (var index = 0; index < 10; ++index)
            {
                var cr2WheaderData = new CR2WHeaderData(file);
                headers.Add(cr2WheaderData);
                if (cr2WheaderData.size > 0U && index > 6)
                {
                    Debugger.Break();
                }
            }

            var offset1 = headers[0].offset;

            strings = new List <CR2WHeaderString>();
            var offset2 = headers[1].offset;
            var size1   = headers[1].size;

            file.BaseStream.Seek(offset2, SeekOrigin.Begin);
            for (var index = 0; (long)index < (long)size1; ++index)
            {
                var cr2WheaderString = new CR2WHeaderString(file);
                cr2WheaderString.ReadString(file, offset1);
                strings.Add(cr2WheaderString);
            }

            handles = new List <CR2WHeaderHandle>();
            var offset3 = headers[2].offset;
            var size2   = headers[2].size;

            file.BaseStream.Seek(offset3, SeekOrigin.Begin);
            for (var index = 0; (long)index < (long)size2; ++index)
            {
                var cr2WheaderHandle = new CR2WHeaderHandle(file);
                cr2WheaderHandle.ReadString(file, offset1);
                handles.Add(cr2WheaderHandle);
            }

            block4 = new List <CR2WHeaderBlock4>();
            var offset4 = headers[3].offset;
            var size3   = headers[3].size;

            file.BaseStream.Seek(offset4, SeekOrigin.Begin);
            for (var index = 0; index < size3; ++index)
            {
                block4.Add(new CR2WHeaderBlock4(file));
            }
            chunks = new List <CR2WChunk>();
            var offset5 = headers[4].offset;
            var size4   = headers[4].size;

            file.BaseStream.Seek(offset5, SeekOrigin.Begin);
            for (var index = 0; index < size4; ++index)
            {
                var num       = file.ReadUInt16();
                var cr2Wchunk = new CR2WChunk(this);
                cr2Wchunk.typeId = num;
                cr2Wchunk.Read(file);
                chunks.Add(cr2Wchunk);
            }

            block6 = new List <CR2WHeaderBlock6>();
            var offset6 = headers[5].offset;
            var size5   = headers[5].size;

            file.BaseStream.Seek(offset6, SeekOrigin.Begin);
            for (var index = 0; index < size5; ++index)
            {
                block6.Add(new CR2WHeaderBlock6(file));
            }
            block7 = new List <CR2WHeaderBlock7>();
            var offset7 = headers[6].offset;
            var size6   = headers[6].size;

            file.BaseStream.Seek(offset7, SeekOrigin.Begin);
            for (var index = 0; index < size6; ++index)
            {
                block7.Add(new CR2WHeaderBlock7(file));
            }
            for (var index = 0; index < chunks.Count; ++index)
            {
                chunks[index].ReadData(file);
            }
            for (var index = 0; index < block7.Count; ++index)
            {
                block7[index].ReadString(file, offset1);
                block7[index].ReadData(file);
            }

            file.BaseStream.Seek(cr2wsize, SeekOrigin.Begin);
            var count = (int)buffersize - (int)cr2wsize;

            if (count <= 0)
            {
                return;
            }
            bufferdata = new byte[count];
            file.BaseStream.Read(bufferdata, 0, count);
        }
Beispiel #14
0
        private CR2WChunk CopyChunk(CR2WChunk chunk)
        {
            if (ExcludeChunks != null &&
                ExcludeChunks.Contains(chunk.Type))
            {
                return(null);
            }

            var chunkcopy = chunk.Copy(this);

            if (chunkcopy != null)
            {
                if (chunkcopy.data is CStorySceneSection)
                {
                    OnCopyStorySceneSection(chunkcopy);
                }

                var CStoryScene = DestinationFile.GetChunkByType("CStoryScene");
                if (CStoryScene != null)
                {
                    var controlParts = CStoryScene.GetVariableByName("controlParts") as CArray;
                    // Add this chunk to the controlParts
                    if (controlParts != null)
                    {
                        switch (chunkcopy.Type)
                        {
                        case "CStorySceneInput":

                        case "CStorySceneScript":
                        case "CStorySceneFlowCondition":

                        case "CStorySceneSection":
                        case "CStorySceneCutsceneSection":
                        case "CStorySceneVideoSection":
                        case "CStorySceneRandomizer":
                        case "CStorySceneOutput":
                        case "CStorySceneCutscenePlayer":

                            DestinationFile.CreatePtr(controlParts, chunkcopy);
                            break;

                        default:
                            break;
                        }
                    }

                    var sections = CStoryScene.GetVariableByName("sections") as CArray;
                    // Add this chunk to the sections
                    if (sections != null)
                    {
                        switch (chunkcopy.Type)
                        {
                        case "CStorySceneSection":
                        case "CStorySceneCutsceneSection":
                        case "CStorySceneVideoSection":

                            DestinationFile.CreatePtr(sections, chunkcopy);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }

            return(chunkcopy);
        }
Beispiel #15
0
        public void Read(BinaryReader file)
        {
            m_stream = file.BaseStream;

            #region Read Headers
            var id = ReadStruct <uint>();
            if (id != MAGIC)
            {
                throw new FormatException($"Not a CR2W file, Magic read as 0x{id:X8}");
            }

            m_fileheader = ReadStruct <CR2WFileHeader>();
            if (m_fileheader.version > 163 || m_fileheader.version < 159)
            {
                throw new FormatException($"Unknown Version {m_fileheader.version}. Supported versions: 159 - 163.");
            }

            var dt = new CDateTime(m_fileheader.timeStamp);

            m_hasInternalBuffer = m_fileheader.bufferSize > m_fileheader.fileSize;
            m_tableheaders      = ReadStructs <CR2WTableHeader>(10);

            m_strings  = ReadStringsBuffer();
            m_names    = ReadTable <CR2WName>(1);
            m_imports  = ReadTable <CR2WImportHeader>(2);
            m_table4   = ReadTable <CR2WTable4Item>(3);
            m_exports  = ReadTable <CR2WExportHeader>(4);
            m_buffers  = ReadTable <CR2WBufferHeader>(5);
            m_embedded = ReadTable <CR2WEmbeddedHeader>(6);
            #endregion

            #region Read Data
            // read Name data
            names = (from n in m_names
                     let str = new CR2WString()
            {
                hash = n.hash,
                offset = n.value,
                str = m_dictionary[n.value]
            }
                     select str).ToList();
            // Read Handle data (imports)
            imports = (from i in m_imports
                       let handle = new CR2WHandle()
            {
                offset = i.depotPath,
                filetype = i.className,
                flags = i.flags,
                str = m_dictionary[i.depotPath]
            }
                       select handle).ToList();
            // Read Chunk data (exports)
            chunks = new List <CR2WChunk>();
            foreach (var c in m_exports)
            {
                var chunk = new CR2WChunk(this)
                {
                    typeId        = c.className,
                    Flags         = c.objectFlags,
                    ParentChunkId = c.parentID,
                    size          = c.dataSize,
                    offset        = c.dataOffset,
                    template      = c.template,
                    crc           = c.crc32
                };
                chunks.Add(chunk);
                chunk.ReadData(file);
            }

            // Read Buffer Data (block 6)
            buffers = new List <CR2WBuffer>();
            foreach (var b in m_buffers)
            {
                var buffer = new CR2WBuffer()
                {
                    flags   = b.flags,
                    index   = b.index,
                    offset  = b.offset,
                    size    = b.diskSize,
                    memsize = b.memSize,
                    crc     = b.crc32
                };
                buffers.Add(buffer);
            }
            // Read Embedded Data (Block 7)
            embedded = new List <CR2WEmbedded>();
            foreach (var e in m_embedded)
            {
                var emb = new CR2WEmbedded()
                {
                    handle_name_count  = e.importIndex,
                    handle_name_offset = e.path,
                    pathHash           = e.pathHash,
                    offset             = e.dataOffset,
                    size = e.dataSize
                };
                var offset = e.path;
                for (int i = 0; i < e.importIndex; i++)
                {
                    if (offset > m_dictionary.Last().Key)
                    {
                        continue;
                    }
                    emb.handles.Add(m_dictionary[offset]);
                    offset += (uint)m_dictionary[offset].Length + 1;
                }
                embedded.Add(emb);
                emb.ReadData(file);
            }
            #endregion

            #region Read Buffer

            file.BaseStream.Seek(m_fileheader.fileSize, SeekOrigin.Begin);

            var actualbuffersize = (int)(m_fileheader.bufferSize - m_fileheader.fileSize);
            if (actualbuffersize > 0)
            {
                bufferdata = new byte[actualbuffersize];
                file.BaseStream.Read(bufferdata, 0, actualbuffersize);
            }
            #endregion

            m_stream = null;
        }