Esempio n. 1
0
        private static StaticObject LoadTextualX(string Text)
        {
            Text = Text.Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " ").Replace("\t", " ").Trim(new char[] { });
            StaticObject obj      = new StaticObject(Plugin.currentHost);
            MeshBuilder  builder  = new MeshBuilder(Plugin.currentHost);
            Material     material = new Material();
            Block        block    = new TextualBlock(Text);

            while (block.Position() < block.Length() - 5)
            {
                Block subBlock = block.ReadSubBlock();
                ParseSubBlock(subBlock, ref obj, ref builder, ref material);
            }
            builder.Apply(ref obj);
            obj.Mesh.CreateNormals();
            if (rootMatrix != Matrix4D.NoTransformation)
            {
                for (int i = 0; i < obj.Mesh.Vertices.Length; i++)
                {
                    obj.Mesh.Vertices[i].Coordinates.Transform(rootMatrix);
                }
            }
            return(obj);
        }
Esempio n. 2
0
        internal static ObjectManager.AnimatedObjectCollection ReadObject(string fileName)
        {
            MsTsShape shape = new MsTsShape();

            ObjectManager.AnimatedObjectCollection Result = new ObjectManager.AnimatedObjectCollection
            {
                Objects = new ObjectManager.AnimatedObject[4]
            };

            currentFolder = Path.GetDirectoryName(fileName);
            Stream fb = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            byte[] buffer = new byte[34];
            fb.Read(buffer, 0, 2);

            bool unicode = (buffer[0] == 0xFF && buffer[1] == 0xFE);

            string headerString;

            if (unicode)
            {
                fb.Read(buffer, 0, 32);
                headerString = Encoding.Unicode.GetString(buffer, 0, 16);
            }
            else
            {
                fb.Read(buffer, 2, 14);
                headerString = Encoding.ASCII.GetString(buffer, 0, 8);
            }

            // SIMISA@F  means compressed
            // SIMISA@@  means uncompressed
            if (headerString.StartsWith("SIMISA@F"))
            {
                fb = new ZlibStream(fb, CompressionMode.Decompress);
            }
            else if (headerString.StartsWith("\r\nSIMISA"))
            {
                // ie us1rd2l1000r10d.s, we are going to allow this but warn
                Console.Error.WriteLine("Improper header in " + fileName);
                fb.Read(buffer, 0, 4);
            }
            else if (!headerString.StartsWith("SIMISA@@"))
            {
                throw new Exception("Unrecognized shape file header " + headerString + " in " + fileName);
            }

            string subHeader;

            if (unicode)
            {
                fb.Read(buffer, 0, 32);
                subHeader = Encoding.Unicode.GetString(buffer, 0, 16);
            }
            else
            {
                fb.Read(buffer, 0, 16);
                subHeader = Encoding.ASCII.GetString(buffer, 0, 8);
            }
            if (subHeader[7] == 't')
            {
                using (BinaryReader reader = new BinaryReader(fb))
                {
                    byte[] newBytes = reader.ReadBytes((int)(fb.Length - fb.Position));
                    string s;
                    if (unicode)
                    {
                        s = Encoding.Unicode.GetString(newBytes);
                    }
                    else
                    {
                        s = Encoding.ASCII.GetString(newBytes);
                    }

                    s = s.Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " ").Replace("\t", " ").Trim();
                    if (!s.StartsWith("shape", StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new Exception();                         //Shape definition
                    }
                    TextualBlock block = new TextualBlock(s, KujuTokenID.shape);
                    ParseBlock(block, ref shape);
                }
            }
            else if (subHeader[7] != 'b')
            {
                throw new Exception("Unrecognized subHeader \"" + subHeader + "\" in " + fileName);
            }
            else
            {
                using (BinaryReader reader = new BinaryReader(fb))
                {
                    KujuTokenID currentToken = (KujuTokenID)reader.ReadUInt16();
                    if (currentToken != KujuTokenID.shape)
                    {
                        throw new Exception();                         //Shape definition
                    }
                    reader.ReadUInt16();
                    uint        remainingBytes = reader.ReadUInt32();
                    byte[]      newBytes       = reader.ReadBytes((int)remainingBytes);
                    BinaryBlock block          = new BinaryBlock(newBytes, KujuTokenID.shape);
                    ParseBlock(block, ref shape);
                }
            }
            Array.Resize(ref Result.Objects, shape.totalObjects);
            int idx = 0;

            double[] previousLODs = new double[shape.totalObjects];
            for (int i = 0; i < shape.LODs.Count; i++)
            {
                for (int j = 0; j < shape.LODs[i].subObjects.Count; j++)
                {
                    Result.Objects[idx]        = new ObjectManager.AnimatedObject();
                    Result.Objects[idx].States = new AnimatedObjectState[1];
                    AnimatedObjectState aos = new AnimatedObjectState(null, Vector3.Zero);
                    shape.LODs[i].subObjects[j].Apply(out aos.Object);
                    aos.Position = new Vector3(0, 0, 0);
                    Result.Objects[idx].States[0] = aos;
                    previousLODs[idx]             = shape.LODs[i].viewingDistance;
                    int k = idx;
                    while (k > 0)
                    {
                        if (previousLODs[k] < shape.LODs[i].viewingDistance)
                        {
                            break;
                        }

                        k--;
                    }

                    if (k != 0)
                    {
                        Result.Objects[idx].StateFunction = new FunctionScript(Program.CurrentHost, "if[cameraDistance <" + shape.LODs[i].viewingDistance + ",if[cameraDistance >" + previousLODs[k] + ",0,-1],-1]", true);
                    }
                    else
                    {
                        Result.Objects[idx].StateFunction = new FunctionScript(Program.CurrentHost, "if[cameraDistance <" + shape.LODs[i].viewingDistance + ",0,-1]", true);
                    }

                    idx++;
                }
            }
            return(Result);
        }