using System; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; namespace Parabox.STL { // Token: 0x02000005 RID: 5 public static class pb_Stl_Importer { // Token: 0x06000009 RID: 9 RVA: 0x000028CC File Offset: 0x00000ACC public static Mesh[] Import(string path) { if (pb_Stl_Importer.IsBinary(path)) { try { return pb_Stl_Importer.ImportBinary(path); } catch (Exception ex) { Debug.LogWarning(string.Format("Failed importing mesh at path {0}.\n{1}", path, ex.ToString())); return null; } } return pb_Stl_Importer.ImportAscii(path); } // Token: 0x0600000A RID: 10 RVA: 0x00002920 File Offset: 0x00000B20 private static Mesh[] ImportBinary(string path) { pb_Stl_Importer.Facet[] array; using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (BinaryReader binaryReader = new BinaryReader(fileStream, new ASCIIEncoding())) { binaryReader.ReadBytes(80); uint num = binaryReader.ReadUInt32(); array = new pb_Stl_Importer.Facet[num]; for (uint num2 = 0U; num2 < num; num2 += 1U) { array[(int)num2] = new pb_Stl_Importer.Facet(); array[(int)num2].normal.x = binaryReader.ReadSingle(); array[(int)num2].normal.y = binaryReader.ReadSingle(); array[(int)num2].normal.z = binaryReader.ReadSingle(); array[(int)num2].a.x = binaryReader.ReadSingle(); array[(int)num2].a.y = binaryReader.ReadSingle(); array[(int)num2].a.z = binaryReader.ReadSingle(); array[(int)num2].b.x = binaryReader.ReadSingle(); array[(int)num2].b.y = binaryReader.ReadSingle(); array[(int)num2].b.z = binaryReader.ReadSingle(); array[(int)num2].c.x = binaryReader.ReadSingle(); array[(int)num2].c.y = binaryReader.ReadSingle(); array[(int)num2].c.z = binaryReader.ReadSingle(); binaryReader.ReadUInt16(); } } } return pb_Stl_Importer.CreateMeshWithFacets(array); } // Token: 0x0600000B RID: 11 RVA: 0x00002AC0 File Offset: 0x00000CC0 private static int ReadState(string line) { if (line.StartsWith("solid")) { return 1; } if (line.StartsWith("facet")) { return 2; } if (line.StartsWith("outer")) { return 3; } if (line.StartsWith("vertex")) { return 4; } if (line.StartsWith("endloop")) { return 5; } if (line.StartsWith("endfacet")) { return 6; } if (line.StartsWith("endsolid")) { return 7; } return 0; } // Token: 0x0600000C RID: 12 RVA: 0x00002B38 File Offset: 0x00000D38 private static Mesh[] ImportAscii(string path) { List list = new List(); using (StreamReader streamReader = new StreamReader(path)) { int num = 0; pb_Stl_Importer.Facet facet = null; bool flag = false; while (streamReader.Peek() > 0 && !flag) { string text = streamReader.ReadLine().Trim(); switch (pb_Stl_Importer.ReadState(text)) { case 2: facet = new pb_Stl_Importer.Facet(); facet.normal = pb_Stl_Importer.StringToVec3(text.Replace("facet normal ", "")); break; case 3: num = 0; break; case 4: if (num == 0) { facet.a = pb_Stl_Importer.StringToVec3(text.Replace("vertex ", "")); } else if (num == 1) { facet.b = pb_Stl_Importer.StringToVec3(text.Replace("vertex ", "")); } else if (num == 2) { facet.c = pb_Stl_Importer.StringToVec3(text.Replace("vertex ", "")); } num++; break; case 6: list.Add(facet); break; case 7: flag = true; break; } } } return pb_Stl_Importer.CreateMeshWithFacets(list); } // Token: 0x0600000D RID: 13 RVA: 0x00002C88 File Offset: 0x00000E88 private static Vector3 StringToVec3(string str) { string[] array = str.Trim().Split(null); Vector3 vector = default(Vector3); float.TryParse(array[0], out vector.x); float.TryParse(array[1], out vector.y); float.TryParse(array[2], out vector.z); return vector; } // Token: 0x0600000E RID: 14 RVA: 0x00002CDC File Offset: 0x00000EDC private static bool IsBinary(string path) { FileInfo fileInfo = new FileInfo(path); if (fileInfo.Length < 130L) { return false; } bool flag = false; using (FileStream fileStream = fileInfo.OpenRead()) { using (BufferedStream bufferedStream = new BufferedStream(fileStream)) { for (long num = 0L; num < 80L; num += 1L) { if (bufferedStream.ReadByte() == 0) { flag = true; break; } } } } if (!flag) { using (FileStream fileStream2 = fileInfo.OpenRead()) { using (BufferedStream bufferedStream2 = new BufferedStream(fileStream2)) { byte[] array = new byte[6]; for (int i = 0; i < 6; i++) { array[i] = (byte)bufferedStream2.ReadByte(); } flag = Encoding.UTF8.GetString(array) != "solid "; } } } return flag; } // Token: 0x0600000F RID: 15 RVA: 0x00002DE8 File Offset: 0x00000FE8 private static Mesh[] CreateMeshWithFacets(IList facets) { int count = facets.Count; int num = 0; int num2 = 65535; Mesh[] array = new Mesh[count / 21845 + 1]; for (int i = 0; i < array.Length; i++) { int num3 = Math.Min(num2, (count - num) * 3); Vector3[] array2 = new Vector3[num3]; Vector3[] array3 = new Vector3[num3]; int[] array4 = new int[num3]; for (int j = 0; j < num3; j += 3) { array2[j] = facets[num].a; array2[j + 1] = facets[num].b; array2[j + 2] = facets[num].c; array3[j] = facets[num].normal; array3[j + 1] = facets[num].normal; array3[j + 2] = facets[num].normal; array4[j] = j; array4[j + 1] = j + 1; array4[j + 2] = j + 2; num++; } array[i] = new Mesh(); array[i].vertices = array2; array[i].normals = array3; array[i].triangles = array4; } return array; } // Token: 0x04000004 RID: 4 private const int MAX_FACETS_PER_MESH = 21845; // Token: 0x04000005 RID: 5 private const int SOLID = 1; // Token: 0x04000006 RID: 6 private const int FACET = 2; // Token: 0x04000007 RID: 7 private const int OUTER = 3; // Token: 0x04000008 RID: 8 private const int VERTEX = 4; // Token: 0x04000009 RID: 9 private const int ENDLOOP = 5; // Token: 0x0400000A RID: 10 private const int ENDFACET = 6; // Token: 0x0400000B RID: 11 private const int ENDSOLID = 7; // Token: 0x0400000C RID: 12 private const int EMPTY = 0; // Token: 0x02000008 RID: 8 private class Facet { // Token: 0x06000017 RID: 23 RVA: 0x00002F7C File Offset: 0x0000117C public override string ToString() { return string.Format("{0:F2}: {1:F2}, {2:F2}, {3:F2}", new object[] { this.normal, this.a, this.b, this.c }); } // Token: 0x04000012 RID: 18 public Vector3 normal; // Token: 0x04000013 RID: 19 public Vector3 a; // Token: 0x04000014 RID: 20 public Vector3 b; // Token: 0x04000015 RID: 21 public Vector3 c; } } }