Ejemplo n.º 1
0
        public CPtr CreatePtr(string type, CR2WChunk tochunk, string varname = "")
        {
            var ptr = new CPtr(this);

            ptr.Name = varname;
            ptr.Type = type;

            if (tochunk != null)
            {
                ptr.val = chunks.IndexOf(tochunk) + 1;
            }
            return(ptr);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public CR2WChunk CreateChunk(string type, CR2WChunk parent = null)
        {
            var chunk = new CR2WChunk(this);

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

            chunks.Add(chunk);
            return(chunk);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
 public bool RemoveChunk(CR2WChunk chunk)
 {
     return(chunks.Remove(chunk));
 }
Ejemplo n.º 9
0
        public void Read(BinaryReader file)
        {
            var filetype = file.ReadBytes(4);

            if (!IDString.SequenceEqual(filetype))
            {
                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 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);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Convert a CBitmapTexture's image to a DDS image
        /// </summary>
        /// <returns>A proper dds file</returns>
        public static Bitmap Xbm2Dds(CR2WChunk imagechunk)
        {
            try
            {
                var image       = ((CBitmapTexture)(imagechunk.data)).Image;
                var compression = imagechunk.GetVariableByName("compression").ToString();
                var width       = int.Parse(imagechunk.GetVariableByName("width").ToString());
                var height      = int.Parse(imagechunk.GetVariableByName("height").ToString());
                var tempfile    = new MemoryStream();

                /* DDS magic header contains:
                 *      Required .dds flags for compressed texture without pitch
                 * More on: https://msdn.microsoft.com/en-us/library/windows/desktop/bb943982(v=vs.85).aspx */
                byte[] ddsheader =
                {
                    0x44, 0x44, 0x53, 0x20, 0x7C, 0x00, 0x00, 0x00, 0x07, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
                    0x05, 0x00, 0x00, 0x00, 0x44, 0x58, 0x54, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x40, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };
                byte[] dxt;
                switch (compression)
                {
                case "TCM_DXTNoAlpha":
                    dxt = new byte[] { 0x44, 0x58, 0x54, 0x31 };
                    break;

                case "TCM_DXTAlpha":
                    dxt = new byte[] { 0x44, 0x58, 0x54, 0x35 };
                    break;

                case "TCM_NormalsHigh":
                    dxt = new byte[] { 0x44, 0x58, 0x54, 0x35 };
                    break;

                case "TCM_Normals":
                    dxt = new byte[] { 0x44, 0x58, 0x54, 0x31 };
                    break;

                default:
                    throw new Exception("Invalid compression type! [" + compression + "]");
                }
                using (var bw = new BinaryWriter(tempfile))
                {
                    bw.Write(ddsheader);
                    bw.Seek(0xC, SeekOrigin.Begin);
                    bw.Write(height);
                    bw.Seek(0x10, SeekOrigin.Begin);
                    bw.Write(width);
                    bw.Seek(0x54, SeekOrigin.Begin);
                    bw.Write(dxt);
                    bw.Seek(128, SeekOrigin.Begin);
                    bw.Write(image.Bytes);
                }
                tempfile.Flush();
#if DEBUG
                File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\asd.dds", tempfile.GetBuffer());
#endif
                return(new DdsImage(tempfile.GetBuffer()).BitmapImage);
            }
            catch (Exception)
            {
                // fails silently...
                return(null);
            }
        }