1
0

ImportCM2.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. public class ImportCM2 : MonoBehaviour
  8. {
  9. public static byte[] m_aniTempFile = null;
  10. private static Dictionary<string, Material> m_dicCacheMaterial = new Dictionary<string, Material>();
  11. private static Dictionary<int, KeyValuePair<string, float>> m_hashPriorityMaterials;
  12. private static byte[] m_matTempFile;
  13. private static byte[] m_skinTempFile;
  14. private static string[] properties = new string[7]
  15. {
  16. "m_LocalRotation.x", "m_LocalRotation.y", "m_LocalRotation.z", "m_LocalRotation.w", "m_LocalPosition.x", "m_LocalPosition.y",
  17. "m_LocalPosition.z"
  18. };
  19. public static GameObject LoadSkinMesh_R(string filename, string[] filename2, string slotname, TBodySkin bodyskin, int layer)
  20. {
  21. try
  22. {
  23. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  24. {
  25. if (m_skinTempFile == null)
  26. m_skinTempFile = new byte[Math.Max(500000, afileBase.GetSize())];
  27. else if (m_skinTempFile.Length < afileBase.GetSize())
  28. m_skinTempFile = new byte[afileBase.GetSize()];
  29. afileBase.Read(ref m_skinTempFile, afileBase.GetSize());
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. NDebug.Assert("ファイルが開けませんでした。" + filename + "\n" + ex.Message, false);
  35. }
  36. var r = new BinaryReader(new MemoryStream(m_skinTempFile), Encoding.UTF8);
  37. TBodySkin.OriVert oriVert = bodyskin.m_OriVert;
  38. var gameObject1 = Instantiate(Resources.Load("seed")) as GameObject;
  39. gameObject1.layer = layer;
  40. var gameObject2 = (GameObject) null;
  41. var hashtable = new Hashtable();
  42. string str1 = r.ReadString();
  43. if (str1 != "CM3D2_MESH")
  44. NDebug.Assert("LoadSkinMesh_R 例外 : ヘッダーファイルが不正です。" + str1, false);
  45. int num1 = r.ReadInt32();
  46. string str2 = r.ReadString();
  47. gameObject1.name = "_SM_" + str2;
  48. string str3 = r.ReadString();
  49. int num2 = r.ReadInt32();
  50. var gameObjectList = new List<GameObject>();
  51. for (int index = 0; index < num2; ++index)
  52. {
  53. var gameObject3 = Instantiate(Resources.Load("seed")) as GameObject;
  54. gameObject3.layer = layer;
  55. gameObject3.name = r.ReadString();
  56. gameObjectList.Add(gameObject3);
  57. if (gameObject3.name == str3)
  58. gameObject2 = gameObject3;
  59. hashtable[gameObject3.name] = gameObject3;
  60. if (r.ReadByte() != 0)
  61. {
  62. var gameObject4 = Instantiate(Resources.Load("seed")) as GameObject;
  63. gameObject4.name = gameObject3.name + "_SCL_";
  64. gameObject4.transform.parent = gameObject3.transform;
  65. hashtable[gameObject3.name + "&_SCL_"] = gameObject4;
  66. }
  67. }
  68. for (int index1 = 0; index1 < num2; ++index1)
  69. {
  70. int index2 = r.ReadInt32();
  71. gameObjectList[index1].transform.parent = index2 < 0 ? gameObject1.transform : gameObjectList[index2].transform;
  72. }
  73. for (int index = 0; index < num2; ++index)
  74. {
  75. Transform transform = gameObjectList[index].transform;
  76. float x1 = r.ReadSingle();
  77. float y1 = r.ReadSingle();
  78. float z1 = r.ReadSingle();
  79. transform.localPosition = new Vector3(x1, y1, z1);
  80. float x2 = r.ReadSingle();
  81. float y2 = r.ReadSingle();
  82. float z2 = r.ReadSingle();
  83. float w = r.ReadSingle();
  84. transform.localRotation = new Quaternion(x2, y2, z2, w);
  85. if (2001 <= num1 && r.ReadBoolean())
  86. {
  87. float x3 = r.ReadSingle();
  88. float y3 = r.ReadSingle();
  89. float z3 = r.ReadSingle();
  90. transform.localScale = new Vector3(x3, y3, z3);
  91. }
  92. }
  93. int length1 = r.ReadInt32();
  94. int length2 = r.ReadInt32();
  95. int length3 = r.ReadInt32();
  96. oriVert.VCount = length1;
  97. oriVert.nSubMeshCount = length2;
  98. gameObject2.AddComponent(typeof(SkinnedMeshRenderer));
  99. gameObject1.AddComponent(typeof(Animation));
  100. var component = gameObject2.GetComponent<Renderer>() as SkinnedMeshRenderer;
  101. component.updateWhenOffscreen = true;
  102. var transformArray = new Transform[length3];
  103. for (int index = 0; index < length3; ++index)
  104. {
  105. string str4 = r.ReadString();
  106. if (hashtable.ContainsKey(str4))
  107. {
  108. GameObject gameObject3 = !hashtable.ContainsKey(str4 + "&_SCL_")
  109. ? (GameObject) hashtable[str4]
  110. : (GameObject) hashtable[str4 + "&_SCL_"];
  111. transformArray[index] = gameObject3.transform;
  112. }
  113. }
  114. component.bones = transformArray;
  115. var mesh1 = new Mesh();
  116. component.sharedMesh = mesh1;
  117. Mesh mesh2 = mesh1;
  118. var matrix4x4Array = new Matrix4x4[length3];
  119. for (int index1 = 0; index1 < length3; ++index1)
  120. for (int index2 = 0; index2 < 16; ++index2)
  121. matrix4x4Array[index1][index2] = r.ReadSingle();
  122. mesh2.bindposes = matrix4x4Array;
  123. var vector3Array1 = new Vector3[length1];
  124. var vector3Array2 = new Vector3[length1];
  125. var vector2Array = new Vector2[length1];
  126. var boneWeightArray = new BoneWeight[length1];
  127. for (int index = 0; index < length1; ++index)
  128. {
  129. float new_x1 = r.ReadSingle();
  130. float new_y1 = r.ReadSingle();
  131. float new_z1 = r.ReadSingle();
  132. vector3Array1[index].Set(new_x1, new_y1, new_z1);
  133. float new_x2 = r.ReadSingle();
  134. float new_y2 = r.ReadSingle();
  135. float new_z2 = r.ReadSingle();
  136. vector3Array2[index].Set(new_x2, new_y2, new_z2);
  137. float newX = r.ReadSingle();
  138. float newY = r.ReadSingle();
  139. vector2Array[index].Set(newX, newY);
  140. }
  141. mesh2.vertices = vector3Array1;
  142. mesh2.normals = vector3Array2;
  143. mesh2.uv = vector2Array;
  144. oriVert.vOriVert = vector3Array1;
  145. oriVert.vOriNorm = vector3Array2;
  146. int length4 = r.ReadInt32();
  147. if (length4 > 0)
  148. {
  149. var vector4Array = new Vector4[length4];
  150. for (int index = 0; index < length4; ++index)
  151. {
  152. float x = r.ReadSingle();
  153. float y = r.ReadSingle();
  154. float z = r.ReadSingle();
  155. float w = r.ReadSingle();
  156. vector4Array[index] = new Vector4(x, y, z, w);
  157. }
  158. mesh2.tangents = vector4Array;
  159. }
  160. for (int index = 0; index < length1; ++index)
  161. {
  162. boneWeightArray[index].boneIndex0 = r.ReadUInt16();
  163. boneWeightArray[index].boneIndex1 = r.ReadUInt16();
  164. boneWeightArray[index].boneIndex2 = r.ReadUInt16();
  165. boneWeightArray[index].boneIndex3 = r.ReadUInt16();
  166. boneWeightArray[index].weight0 = r.ReadSingle();
  167. boneWeightArray[index].weight1 = r.ReadSingle();
  168. boneWeightArray[index].weight2 = r.ReadSingle();
  169. boneWeightArray[index].weight3 = r.ReadSingle();
  170. }
  171. mesh2.boneWeights = boneWeightArray;
  172. mesh2.subMeshCount = length2;
  173. oriVert.bwWeight = boneWeightArray;
  174. oriVert.nSubMeshCount = length2;
  175. oriVert.nSubMeshOriTri = new int[length2][];
  176. for (int submesh = 0; submesh < length2; ++submesh)
  177. {
  178. int length5 = r.ReadInt32();
  179. var triangles = new int[length5];
  180. for (int index = 0; index < length5; ++index)
  181. triangles[index] = r.ReadUInt16();
  182. oriVert.nSubMeshOriTri[submesh] = triangles;
  183. mesh2.SetTriangles(triangles, submesh);
  184. }
  185. int length6 = r.ReadInt32();
  186. var materialArray = new Material[length6];
  187. for (int index = 0; index < length6; ++index)
  188. {
  189. Material material = ReadMaterial(r, bodyskin, null, filename2[1 + index]);
  190. materialArray[index] = material;
  191. }
  192. component.materials = materialArray;
  193. r.Close();
  194. return gameObject1;
  195. }
  196. public static Material ReadMaterial(BinaryReader r, TBodySkin bodyskin, Material existmat, string filename)
  197. {
  198. bodyskin = null;
  199. existmat = null;
  200. if (m_hashPriorityMaterials == null)
  201. {
  202. m_hashPriorityMaterials = new Dictionary<int, KeyValuePair<string, float>>();
  203. var list = GameUty.FileSystem.GetList("prioritymaterial", AFileSystemBase.ListType.AllFile);
  204. if (list != null && 0 < list.Length)
  205. for (int index = 0; index < list.Length; ++index)
  206. if (Path.GetExtension(list[index]) == ".pmat")
  207. {
  208. string fileName = list[index];
  209. using (AFileBase afileBase = GameUty.FileOpen(fileName, null))
  210. {
  211. NDebug.Assert(afileBase.IsValid(), fileName + "を開けませんでした");
  212. using (var binaryReader = new BinaryReader(new MemoryStream(afileBase.ReadAll()), Encoding.UTF8))
  213. {
  214. NDebug.Assert(binaryReader.ReadString() == "CM3D2_PMATERIAL", "ヘッダーエラー\n" + fileName);
  215. binaryReader.ReadInt32();
  216. int key1 = binaryReader.ReadInt32();
  217. string key2 = binaryReader.ReadString();
  218. float num = binaryReader.ReadSingle();
  219. NDebug.Assert(!m_hashPriorityMaterials.ContainsKey(key1), "すでにハッシュが登録されています");
  220. m_hashPriorityMaterials.Add(key1, new KeyValuePair<string, float>(key2, num));
  221. }
  222. }
  223. }
  224. }
  225. string str1 = r.ReadString();
  226. string str2 = r.ReadString();
  227. string path1 = "DefMaterial/" + r.ReadString();
  228. Material material;
  229. if (existmat == null)
  230. {
  231. var original = Resources.Load(path1, typeof(Material)) as Material;
  232. if (original == null)
  233. return original;
  234. material = Instantiate(original);
  235. }
  236. else
  237. {
  238. material = existmat;
  239. NDebug.Assert(material.shader.name == str2, "マテリアル入れ替えエラー。違うシェーダーに入れようとしました。 " + str2 + " -> " + material.shader.name);
  240. }
  241. material.name = str1;
  242. int hashCode1 = material.name.GetHashCode();
  243. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode1))
  244. {
  245. var priorityMaterial = m_hashPriorityMaterials[hashCode1];
  246. if (priorityMaterial.Key == material.name)
  247. {
  248. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  249. material.renderQueue = (int) priorityMaterial.Value;
  250. }
  251. }
  252. string str3;
  253. Vector2 vector2_1;
  254. Vector2 vector2_2;
  255. string str4;
  256. string str5;
  257. Color color;
  258. Vector4 vector4;
  259. while (true)
  260. {
  261. string str6 = r.ReadString();
  262. if (!(str6 == "end"))
  263. {
  264. string name = r.ReadString();
  265. if (str6 == "tex")
  266. {
  267. string str7 = r.ReadString();
  268. if (str7 == "null")
  269. {
  270. material.SetTexture(name, null);
  271. }
  272. else if (str7 == "tex2d")
  273. {
  274. try
  275. {
  276. string str8 = r.ReadString();
  277. str3 = r.ReadString();
  278. var data = ImportCM.LoadTexture(GameUty.FileSystem, str8 + ".tex", false).data;
  279. vector2_1.x = r.ReadSingle();
  280. vector2_1.y = r.ReadSingle();
  281. vector2_2.x = r.ReadSingle();
  282. vector2_2.y = r.ReadSingle();
  283. if (filename == null || !filename.Contains(".mate"))
  284. {
  285. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  286. texture2D.LoadImage(data);
  287. texture2D.name = str8;
  288. texture2D.wrapMode = TextureWrapMode.Clamp;
  289. material.SetTexture(name, texture2D);
  290. material.SetTextureOffset(name, vector2_1);
  291. material.SetTextureScale(name, vector2_2);
  292. }
  293. }
  294. catch
  295. {
  296. break;
  297. }
  298. }
  299. else if (str7 == "texRT")
  300. {
  301. str4 = r.ReadString();
  302. str5 = r.ReadString();
  303. }
  304. }
  305. else if (str6 == "col")
  306. {
  307. color.r = r.ReadSingle();
  308. color.g = r.ReadSingle();
  309. color.b = r.ReadSingle();
  310. color.a = r.ReadSingle();
  311. material.SetColor(name, color);
  312. }
  313. else if (str6 == "vec")
  314. {
  315. vector4.x = r.ReadSingle();
  316. vector4.y = r.ReadSingle();
  317. vector4.z = r.ReadSingle();
  318. vector4.w = r.ReadSingle();
  319. material.SetVector(name, vector4);
  320. }
  321. else if (str6 == "f")
  322. {
  323. float num = r.ReadSingle();
  324. material.SetFloat(name, num);
  325. }
  326. }
  327. else
  328. {
  329. break;
  330. }
  331. }
  332. if (filename != null && filename.Contains(".mate"))
  333. {
  334. try
  335. {
  336. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  337. {
  338. if (m_matTempFile == null)
  339. m_matTempFile = new byte[Math.Max(10000, afileBase.GetSize())];
  340. else if (m_matTempFile.Length < afileBase.GetSize())
  341. m_matTempFile = new byte[afileBase.GetSize()];
  342. afileBase.Read(ref m_matTempFile, afileBase.GetSize());
  343. }
  344. }
  345. catch { }
  346. var binaryReader = new BinaryReader(new MemoryStream(m_matTempFile), Encoding.UTF8);
  347. string str6 = binaryReader.ReadString();
  348. NDebug.Assert(str6 == "CM3D2_MATERIAL", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + str6);
  349. binaryReader.ReadInt32();
  350. binaryReader.ReadString();
  351. r = binaryReader;
  352. string str7 = r.ReadString();
  353. string str8 = r.ReadString();
  354. string path2 = "DefMaterial/" + r.ReadString();
  355. if (existmat == null)
  356. {
  357. var original = Resources.Load(path2, typeof(Material)) as Material;
  358. if (original == null)
  359. return original;
  360. material = Instantiate(original);
  361. }
  362. else
  363. {
  364. material = existmat;
  365. NDebug.Assert(material.shader.name == str8, "マテリアル入れ替えエラー。違うシェーダーに入れようとしました。 " + str8 + " -> " + material.shader.name);
  366. }
  367. material.name = str7;
  368. int hashCode2 = material.name.GetHashCode();
  369. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode2))
  370. {
  371. var priorityMaterial = m_hashPriorityMaterials[hashCode2];
  372. if (priorityMaterial.Key == material.name)
  373. {
  374. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  375. material.renderQueue = (int) priorityMaterial.Value;
  376. }
  377. }
  378. while (true)
  379. {
  380. string str9 = r.ReadString();
  381. if (!(str9 == "end"))
  382. {
  383. string name = r.ReadString();
  384. if (str9 == "tex")
  385. {
  386. string str10 = r.ReadString();
  387. if (str10 == "null")
  388. {
  389. material.SetTexture(name, null);
  390. }
  391. else if (str10 == "tex2d")
  392. {
  393. try
  394. {
  395. string str11 = r.ReadString();
  396. str3 = r.ReadString();
  397. var data = ImportCM.LoadTexture(GameUty.FileSystem, str11 + ".tex", false).data;
  398. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  399. texture2D.LoadImage(data);
  400. texture2D.name = str11;
  401. texture2D.wrapMode = TextureWrapMode.Clamp;
  402. material.SetTexture(name, texture2D);
  403. vector2_1.x = r.ReadSingle();
  404. vector2_1.y = r.ReadSingle();
  405. material.SetTextureOffset(name, vector2_1);
  406. vector2_2.x = r.ReadSingle();
  407. vector2_2.y = r.ReadSingle();
  408. material.SetTextureScale(name, vector2_2);
  409. }
  410. catch
  411. {
  412. break;
  413. }
  414. }
  415. else if (str10 == "texRT")
  416. {
  417. str4 = r.ReadString();
  418. str5 = r.ReadString();
  419. }
  420. }
  421. else if (str9 == "col")
  422. {
  423. color.r = r.ReadSingle();
  424. color.g = r.ReadSingle();
  425. color.b = r.ReadSingle();
  426. color.a = r.ReadSingle();
  427. material.SetColor(name, color);
  428. }
  429. else if (str9 == "vec")
  430. {
  431. vector4.x = r.ReadSingle();
  432. vector4.y = r.ReadSingle();
  433. vector4.z = r.ReadSingle();
  434. vector4.w = r.ReadSingle();
  435. material.SetVector(name, vector4);
  436. }
  437. else if (str9 == "f")
  438. {
  439. float num = r.ReadSingle();
  440. material.SetFloat(name, num);
  441. }
  442. }
  443. else
  444. {
  445. break;
  446. }
  447. }
  448. }
  449. return material;
  450. }
  451. }