MegascansBridgeLink.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #if UNITY_EDITOR
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using UnityEditor;
  10. using UnityEngine;
  11. namespace Quixel {
  12. public class QXLServer {
  13. private TcpListener tcpListener;
  14. private Thread tcpListenerThread;
  15. private TcpClient connectedTcpClient;
  16. private bool isRunning;
  17. public List<string> jsonData = new List<string> ();
  18. // Use this for initialization
  19. public void StartServer () {
  20. if(!isRunning)
  21. {
  22. // Start TcpServer background thread
  23. tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));
  24. tcpListenerThread.IsBackground = true;
  25. tcpListenerThread.Start();
  26. isRunning = true;
  27. }
  28. }
  29. public void EndServer () {
  30. isRunning = false;
  31. tcpListener.Stop ();
  32. tcpListenerThread.Abort();
  33. Debug.Log ("Quixel Bridge Plugin - Status: Disabled.");
  34. }
  35. private void ListenForIncommingRequests () {
  36. try {
  37. tcpListener = new TcpListener (IPAddress.Parse ("127.0.0.1"), 13081);
  38. tcpListener.Start ();
  39. Debug.Log ("Quixel Bridge Plugin - Status: Enabled.");
  40. Byte[] bytes = new Byte[4096];
  41. while (true) {
  42. using (connectedTcpClient = tcpListener.AcceptTcpClient ()) {
  43. using (NetworkStream stream = connectedTcpClient.GetStream ()) {
  44. int length;
  45. string clientMessage = "";
  46. while ((length = stream.Read (bytes, 0, bytes.Length)) != 0) {
  47. try {
  48. byte[] incommingData = new byte[length];
  49. Array.Copy(bytes, 0, incommingData, 0, length);
  50. UTF8Encoding encodingUnicode = new UTF8Encoding();
  51. clientMessage += encodingUnicode.GetString(incommingData);
  52. } catch (Exception ex)
  53. {
  54. Debug.Log("Bridge Plugin Exception::Error::Encoding json data.");
  55. Debug.Log("Exception: " + ex.ToString());
  56. }
  57. }
  58. jsonData.Add (clientMessage);
  59. }
  60. }
  61. }
  62. } catch (SocketException socketException) {
  63. Debug.Log ("Bridge Plugin - Status: Stopped.");
  64. Debug.Log ("SocketException " + socketException.ToString ());
  65. }
  66. }
  67. }
  68. [InitializeOnLoad]
  69. [ExecuteInEditMode]
  70. public class MegascansBridgeLink {
  71. static private bool isRunning = false;
  72. static private QXLServer listener;
  73. static private MegascansImporter mi;
  74. static MegascansBridgeLink () {
  75. listener = new QXLServer ();
  76. EditorApplication.update += ImportTheThing;
  77. mi = ScriptableObject.CreateInstance<MegascansImporter> ();
  78. }
  79. public static void ToggleServer(bool runServer = true)
  80. {
  81. if(runServer) //Start server
  82. {
  83. if (isRunning)
  84. {
  85. try
  86. {
  87. if (isRunning)
  88. {
  89. listener.EndServer();
  90. isRunning = false;
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Debug.Log(ex.ToString());
  96. }
  97. }
  98. isRunning = true;
  99. listener.StartServer();
  100. }
  101. else //Stop server
  102. {
  103. try
  104. {
  105. if (isRunning)
  106. {
  107. listener.EndServer();
  108. isRunning = false;
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. Debug.Log(ex.ToString());
  114. }
  115. }
  116. }
  117. static void ImportTheThing () {
  118. if (listener != null && isRunning) {
  119. if (listener.jsonData.Count > 0) {
  120. try {
  121. string jArray = listener.jsonData[0];
  122. Newtonsoft.Json.Linq.JArray testArray = Newtonsoft.Json.Linq.JArray.Parse(jArray);
  123. List<Newtonsoft.Json.Linq.JObject> objectList = new List<Newtonsoft.Json.Linq.JObject>();
  124. for (int i = 0; i < testArray.Count; ++i)
  125. {
  126. JObject assetObj = testArray[i].ToObject<Newtonsoft.Json.Linq.JObject>();
  127. objectList.Add(assetObj);
  128. }
  129. string lastFolderPath = null;
  130. for (int i = 0; i < objectList.Count; ++i)
  131. {
  132. Debug.Log(objectList[i]);
  133. lastFolderPath = mi.ImportMegascansAssets(objectList[i]);
  134. }
  135. //Highlight the last imported asset at the end of the import operation.
  136. if (lastFolderPath != null)
  137. {
  138. UnityEngine.Object folder = AssetDatabase.LoadAssetAtPath(lastFolderPath, typeof(UnityEngine.Object));
  139. Selection.activeObject = folder;
  140. EditorGUIUtility.PingObject(folder);
  141. }
  142. listener.jsonData.RemoveAt(0);
  143. }
  144. catch (Exception ex)
  145. {
  146. Debug.Log("Bridge Plugin Exception::Error::Parsing json data.");
  147. Debug.Log("Bridge Plugin::Data::Received JSON Data: " + listener.jsonData);
  148. Debug.Log("Exception: " + ex.ToString());
  149. listener.jsonData.RemoveAt(0);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. #endif