Exemple #1
0
    internal void TaskLoad(string name)
    {
        try
        {
            string path    = Path.GetDirectoryName(name);
            string zipName = null;
            string fltName = Path.GetFileName(name);
            if (path.EndsWith(".zip"))
            {
                zipName = Path.GetFileName(path);
                path    = Path.GetDirectoryName(path);
            }

            if (fltName == null)
            {
                Debug.LogErrorFormat("[MeshManager] MeshEntry.TaskLoad(): no FLT specified in {0}", name);
                Loaded = true;
                return;
            }

            if (zipName == null)
            {
                var bytes  = System.IO.File.ReadAllBytes(name);
                var parser = new Cognitics.BinaryParser(bytes);
                var reader = new FltReader(parser);
                Flt = new FltDatabase(null, RecordType.Invalid, reader, fltName);
                Flt.Parse();
            }
            else
            {
                byte[] bytes = null;
                ZipReader.DoDecompression(path, zipName, fltName, ref bytes);
                if (bytes == null)
                {
                    // TODO: this log is too spammy for a regular build right now. remove conditional check once that issue is addressed
                    if (Database.isDebugBuild)
                    {
                        Debug.LogErrorFormat("Missing model: {0} {1} {2}", path, zipName, fltName);
                    }
                    Loaded = true;
                    return;
                }
                var parser = new Cognitics.BinaryParser(bytes);
                var reader = new FltReader(parser);
                Flt = new FltDatabase(null, RecordType.Invalid, reader, fltName);
                Flt.Parse();
            }

            foreach (var texturePalette in Flt.TexturePalettes)
            {
                string texPath = Path.Combine(path, texturePalette.filename);
                if (File.Exists(texPath))
                {
                    Textures.Add(texPath);
                }
                else
                {
                    string textureFilename = Path.Combine(path, texturePalette.filename);
                    string zippath         = Path.GetDirectoryName(textureFilename);
                    string zipfn           = Path.GetFileNameWithoutExtension(textureFilename);
                    int    us_index        = 0;
                    for (int i = 0; i < 7; ++i)
                    {
                        us_index = zipfn.IndexOf('_', us_index) + 1;
                    }
                    zipfn = zipfn.Substring(0, us_index - 1);
                    string zipfile = Path.Combine(zippath, zipfn + ".zip");
                    texPath = Path.Combine(zipfile, Path.GetFileName(texturePalette.filename));
                    // TODO: we could query the zip here to verify the file exists within its contents
                    //if (File.Exists(texPath))
                    {
                        Textures.Add(texPath);
                    }
                    //else
                    //{
                    //    Debug.LogErrorFormat("[MeshManager] MeshEntry.TaskLoad(): texture {0} does not exist", texPath);
                    //    return;
                    //}
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
        if ((Flt != null) && (Flt.geometryRecords.Count == 0))
        {
            Debug.LogError("[MeshManager] MeshEntry.TaskLoad(): no primary geometry for " + Flt.Path);
            Flt = null;
        }

        if (Flt != null)
        {
            vertices = new Vector3[Flt.VertexPalette.Dict.Count];
            normals  = new Vector3[Flt.VertexPalette.Dict.Count];
            uvs      = new Vector2[Flt.VertexPalette.Dict.Count];

            int i = 0;
            foreach (var elem in Flt.VertexPalette.Dict)
            {
                var vert = elem.Value;
                if (vert is VertexWithColorNormal)
                {
                    var v = vert as VertexWithColorNormal;
                    vertices[i].x = (float)v.x;
                    vertices[i].y = (float)v.z;
                    vertices[i].z = (float)v.y;
                    normals[i].x  = v.i;
                    normals[i].y  = v.k;
                    normals[i].z  = v.j;
                    uvs[i]        = Vector2.zero;
                }
                else if (vert is VertexWithColorNormalUV)
                {
                    var v = vert as VertexWithColorNormalUV;
                    vertices[i].x = (float)v.x;
                    vertices[i].y = (float)v.z;
                    vertices[i].z = (float)v.y;
                    normals[i].x  = v.i;
                    normals[i].y  = v.k;
                    normals[i].z  = v.j;
                    uvs[i].x      = v.u;
                    uvs[i].y      = v.v;
                }
                else if (vert is VertexWithColorUV)
                {
                    var v = vert as VertexWithColorUV;
                    vertices[i].x = (float)v.x;
                    vertices[i].y = (float)v.z;
                    vertices[i].z = (float)v.y;
                    normals[i]    = Vector3.up;
                    uvs[i].x      = v.u;
                    uvs[i].y      = v.v;
                }
                else if (vert is VertexWithColor) // base class
                {
                    var v = vert as VertexWithColor;
                    vertices[i].x = (float)v.x;
                    vertices[i].y = (float)v.z;
                    vertices[i].z = (float)v.y;
                    normals[i]    = Vector3.up;
                    uvs[i]        = Vector2.zero;
                }

                ++i;
            }
        }

        Loaded = true;
    }