Exemplo n.º 1
0
        byte[] GetCookedTriangleMeshFromCache( Vec3[] vertices, int[] indices, short[] materialIndices )
        {
            try
            {
                string cacheDirectory = "user:Caches\\PhysXCookedTriangleMeshes";
                string realCacheDirectory = VirtualFileSystem.GetRealPathByVirtual( cacheDirectory );

                if( Directory.Exists( realCacheDirectory ) )
                {
                    byte[] sourceBuffer;
                    string baseName;
                    GetCookTriangleMeshSourceBuffer( vertices, indices, materialIndices, out sourceBuffer, out baseName );
                    string realFileName = Path.Combine( realCacheDirectory, baseName + ".cache" );

                    if( File.Exists( realFileName ) )
                    {
                        int major, minor, bugfix;
                        PhysXNativeWorld.GetSDKVersion( out major, out minor, out bugfix );
                        int additional = 0;
                        int version = ( ( ( ( ( major << 8 ) + minor ) << 8 ) + bugfix ) << 8 ) + additional;

                        //File format:
                        //1. PhysX version (4 bytes)
                        //2. source buffer length (4 bytes)
                        //3. source buffer
                        //4. cooked data length (4 bytes)
                        //5. cooked data

                        //no archive
                        byte[] fileData = File.ReadAllBytes( realFileName );

                        //archive
                        //byte[] fileData;
                        ////Mono runtime specific
                        //if( RuntimeFramework.Runtime == RuntimeFramework.RuntimeType.Mono )
                        //   ZipConstants.DefaultCodePage = 0;
                        //using( ZipFile zipFile = new ZipFile( realFileName ) )
                        //{
                        //   ZipEntry entry = zipFile.GetEntry( "data" );
                        //   Stream zipStream = zipFile.GetInputStream( entry );
                        //   fileData = new byte[ entry.Size ];
                        //   if( zipStream.Read( fileData, 0, (int)entry.Size ) != fileData.Length )
                        //      return null;
                        //}

                        ReceiveDataReader reader = new ReceiveDataReader();
                        reader.Init( fileData, 0, fileData.Length * 8 );

                        //check for old version
                        int fileVersion = reader.ReadInt32();
                        if( version != fileVersion )
                            return null;

                        int fileBufferSize = reader.ReadInt32();
                        if( sourceBuffer.Length != fileBufferSize )
                            return null;

                        byte[] fileSourceBuffer = new byte[ fileBufferSize ];
                        reader.ReadBuffer( fileSourceBuffer );
                        unsafe
                        {
                            fixed( byte* pSource = sourceBuffer, pFileBuffer = fileSourceBuffer )
                            {
                                if( NativeUtils.CompareMemory( (IntPtr)pSource, (IntPtr)pFileBuffer,
                                    sourceBuffer.Length ) != 0 )
                                {
                                    return null;
                                }
                            }
                        }

                        int cookedSize = reader.ReadInt32();
                        byte[] cookedData = new byte[ cookedSize ];
                        reader.ReadBuffer( cookedData );

                        return cookedData;
                    }
                }
            }
            catch { }

            return null;
        }