Beispiel #1
0
        public static Srd FromFile(string srdPath)
        {
            Srd result = new Srd();

            FileInfo srdInfo = new FileInfo(srdPath);

            if (!srdInfo.Exists)
            {
                Console.WriteLine("ERROR: Input file does not exist.");
                return(null);
            }

            if (srdInfo.Extension.ToLower() != ".srd")
            {
                Console.WriteLine("ERROR: Input file does not have the \".srd\" extension.");
                return(null);
            }

            result.Filepath = srdPath;


            BinaryReader reader = new BinaryReader(new FileStream(srdPath, FileMode.Open));

            result.Blocks = new List <Block>();

            // Read blocks
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                Block block;

                string blockType = new ASCIIEncoding().GetString(reader.ReadBytes(4));
                switch (blockType)
                {
                case "$CFH":
                    block = new CfhBlock(ref reader);
                    break;

                case "$CT0":
                    block = new Ct0Block(ref reader);
                    break;

                case "$RSF":
                    block = new RsfBlock(ref reader);
                    result.ResourceFolder = (RsfBlock)block;
                    break;

                case "$RSI":
                    block = new RsiBlock(ref reader);
                    break;

                case "$TRE":
                    block = new TreBlock(ref reader);
                    break;

                case "$TXI":
                    block = new TxiBlock(ref reader);
                    break;

                case "$TXR":
                    block = new TxrBlock(ref reader);
                    break;

                case "$VTX":
                    block = new VtxBlock(ref reader);
                    break;

                default:
                    block = new UnknownBlock(ref reader, blockType);
                    break;
                }
                result.Blocks.Add(block);

                Utils.ReadPadding(ref reader);
            }

            reader.Close();
            return(result);
        }
        private static List <Material> GetMaterials(SrdFile srd)
        {
            var materialList = new List <Material>();

            var matBlocks = srd.Blocks.Where(b => b is MatBlock).ToList();
            var txiBlocks = srd.Blocks.Where(b => b is TxiBlock).ToList();

            foreach (MatBlock mat in matBlocks)
            {
                var matResources = mat.Children[0] as RsiBlock;

                Material material = new Material();
                material.Name = matResources.ResourceStringList[0];

                foreach (var pair in mat.MapTexturePairs)
                {
                    // Find the TXI block associated with the current map
                    TxiBlock matchingTxi = new TxiBlock();
                    foreach (TxiBlock txi in txiBlocks)
                    {
                        var txiResources = txi.Children[0] as RsiBlock;
                        if (txiResources.ResourceStringList[0] == pair.Value)
                        {
                            matchingTxi = txi;
                            break;
                        }
                    }

                    TextureSlot texSlot = new TextureSlot
                    {
                        FilePath = matchingTxi.TextureFilename,
                        Mapping  = TextureMapping.FromUV,
                        UVIndex  = 0,
                    };

                    // Determine map type
                    if (pair.Key.StartsWith("COLORMAP"))
                    {
                        if (matchingTxi.TextureFilename.StartsWith("lm"))
                        {
                            texSlot.TextureType = TextureType.Lightmap;
                        }
                        else
                        {
                            texSlot.TextureType = TextureType.Diffuse;
                        }
                    }
                    else if (pair.Key.StartsWith("NORMALMAP"))
                    {
                        texSlot.TextureType = TextureType.Normals;
                    }
                    else if (pair.Key.StartsWith("SPECULARMAP"))
                    {
                        texSlot.TextureType = TextureType.Specular;
                    }
                    else if (pair.Key.StartsWith("TRANSPARENCYMAP"))
                    {
                        texSlot.TextureType = TextureType.Opacity;
                    }
                    else if (pair.Key.StartsWith("REFLECTMAP"))
                    {
                        texSlot.TextureType = TextureType.Reflection;
                    }
                    else
                    {
                        Console.WriteLine($"WARNING: Texture map type {pair.Key} is not currently supported.");
                    }
                    texSlot.TextureIndex = material.GetMaterialTextureCount(texSlot.TextureType);

                    if (!material.AddMaterialTexture(texSlot))
                    {
                        Console.WriteLine($"WARNING: Adding map ({pair.Key}, {pair.Value}) did not update or create new data!");
                    }
                }

                materialList.Add(material);
            }

            return(materialList);
        }