Example #1
0
    public void LoadModelForTileId(int id)
    {
        if (TileList[id].CurrentLoadState == TileListEntry.LoadState.ModelLoaded)
        {
            return;
        }

        P3DModel model = P3DParser.LoadFromFile(TileList[id].ModelPath);

        TileList[id].Model = model;

        TileList[id].Materials = new List <Material>(model.P3DNumTextures);
        for (int i = 0; i < model.P3DNumTextures; i++)
        {
            MaterialListEntry m = Materials.FirstOrDefault(x => x.Name == model.P3DRenderInfo[i].TextureFile);
            if (m == default(MaterialListEntry))
            {
                m          = new MaterialListEntry();
                m.Material = model.CreateMaterial(i);
                m.Name     = model.P3DRenderInfo[i].TextureFile;
                Materials.Add(m);
            }

            TileList[id].Materials.Add(m.Material);
        }

        TileList[id].CurrentLoadState = TileListEntry.LoadState.ModelLoaded;
    }
    /// <summary>
    /// Loads all Cfl files using directory. Has to be run before ReadCatFiles.
    /// </summary>
    /// <param name="Dir"></param>
    private static List <string> ReadCflFiles(string Dir, string mod_id = null, string mod_to_filter_by = null)
    {
        List <string> names = new List <string>();

        string[] cfls = Directory.GetFiles(Dir, "*.cfl");
        foreach (var File in cfls)
        {
            string name = Path.GetFileNameWithoutExtension(File);
            if (mod_to_filter_by != null && DefaultTiles.ContainsKey(name))
            {
                List <string> mods = DefaultTiles[name];
                if (mods.Count() != 0 && mods[mods.Count - 1] != mod_to_filter_by)
                {
                    continue;
                }
            }
            string[] cfl = System.IO.File.ReadAllLines(File);

            for (int j = 0; j < cfl.Length; j++)
            {
                cfl[j] = IO.RemoveComment(cfl[j]);
            }

            if (cfl[2].LastIndexOf('.') < 0)
            {
                Debug.Log("modelName");
            }
            string modelName = cfl[2].Remove(cfl[2].LastIndexOf('.')).ToLower();

            // field is just block of grass. It's listed in cfl file but isn't showed. Mica used this tile as CP (weird right?) so I had to take this into consideration
            if (modelName == "field" && mod_id == null || modelName == "border1" || modelName == "border2")
            {
                continue;
            }
            string[]   size_str = Regex.Split(cfl[3], " ");
            Vector2Int size     = new Vector2Int(int.Parse(size_str[0]), int.Parse(size_str[1]));

            bool   IsCheckpoint = cfl[9] == "1" ? true : false;
            int    offset       = cfl[13].Contains("STOP") ? 1 : 0;    // checkpoints have one additional parameter for checkpoint dimensions, which moves forward all other parameters by one line
            string Restrictions = Regex.Replace(cfl[12 + offset], " ", "");
            // remove STOP characters
            Restrictions = Restrictions.Remove(Restrictions.Length - 4);
            string Vegetation_str = cfl[16 + offset];

            List <Vegetation> VegData = new List <Vegetation>();
            if (Vegetation_str != "NO_VEGETATION" && Vegetation_str != "SAME_VEGETATION_AS")
            {
                int bushCount = int.Parse(Vegetation_str);
                for (int j = 0; j < bushCount; j++)
                {
                    string VegName = cfl[19 + offset + 7 * j];
                    VegName = VegName.Substring(0, VegName.IndexOf('.'));
                    // don't load grass and small vegetation. We are interested only in big trees
                    // TODO
                    //if (!Consts.AllowedBushes.Contains(VegName))
                    //  continue;
                    //string[] xz = Regex.Split(cfl[21 + offset + 7 * j], " ");
                    //float x = float.Parse(xz[0], System.Globalization.CultureInfo.InvariantCulture);
                    //float z = float.Parse(xz[1], System.Globalization.CultureInfo.InvariantCulture);
                    //string y = cfl[23 + offset + 7 * j];
                    //VegData.Add(new Vegetation(VegName, x, z, y));
                }
            }
            // load icon of tile
            Texture2D texture = Texture2D.blackTexture;
            if (Directory.Exists(NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\"))
            {
                string[] files = Directory.GetFiles(NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\", name + ".*");
                files = files.Where(f => f.Contains("dds") || f.Contains("tga")).ToArray();
                if (files.Length == 0)
                {
                    string datapath = IO.GetCrashdayPath() + "\\data\\content\\textures\\pictures\\tiles\\" + name + ".tga";
                    texture = TgaDecoder.LoadTGA(datapath);
                }
                else if (files[0].Contains(".tga"))                 // tga format
                {
                    texture = TgaDecoder.LoadTGA(files[0]);
                }
                else
                {
                    // file is in dds format
                    string ddsFilePath = NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\" + name + ".dds";
                    byte[] bytes       = System.IO.File.ReadAllBytes(ddsFilePath);
                    texture = DDSDecoder.LoadTextureDXT(bytes, TextureFormat.DXT1);
                }
            }
            else
            {
                texture = Resources.Load <Texture2D>("flag");
            }
            string Model_path = NavigateDirUp(Dir, 2) + "\\models\\" + modelName + ".p3d";

            if (!System.IO.File.Exists(Model_path))
            {             // look for model in original files (used in mica's tiles)
                Model_path = IO.GetCrashdayPath() + "\\data\\content\\models\\" + modelName + ".p3d";
                if (!System.IO.File.Exists(Model_path))
                {
                    Debug.LogWarning("No p3d for tile " + name);
                    continue;
                }
            }

            P3DModel model = P3DParser.LoadFromFile(Model_path);

            List <Material> ModelMaterials = new List <Material>(model.P3DNumTextures);

            for (int j = 0; j < model.P3DNumTextures; j++)
            {
                MaterialListEntry m = Materials.FirstOrDefault(x => x.Name == model.P3DRenderInfo[j].TextureFile);
                if (m == default(MaterialListEntry))
                {
                    m = new MaterialListEntry
                    {
                        Material = model.CreateMaterial(j, mod_id),
                        Name     = model.P3DRenderInfo[j].TextureFile
                    };
                    Materials.Add(m);
                }
                ModelMaterials.Add(m.Material);
            }

            if (TileListInfo.ContainsKey(name))
            {
                TileListInfo[name].Set(size, Restrictions, IsCheckpoint, model, ModelMaterials, texture, VegData.ToArray(), mod_id);
            }
            else
            {
                TileListInfo.Add(name, new TileListEntry(size, Restrictions, IsCheckpoint, model, ModelMaterials, texture, VegData.ToArray(), mod_id));
            }

            names.Add(name);
        }
        return(names);
    }