NodeProcess.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. using Debug = UnityEngine.Debug;
  10. public class NodeProcess : MonoBehaviour
  11. {
  12. private Process process;
  13. void SetFilePermissions(string path)
  14. {
  15. // Set file permissions to readable, writable, executable
  16. AndroidJavaObject file = new AndroidJavaObject("java.io.File", path);
  17. file.Call<bool>("setReadable", true, false);
  18. file.Call<bool>("setWritable", true, false);
  19. file.Call<bool>("setExecutable", true, false);
  20. }
  21. IEnumerator Start()
  22. {
  23. if(!Directory.Exists(Application.persistentDataPath + "/build"))
  24. {
  25. yield return StartCoroutine(UnzipFile());
  26. }else
  27. {
  28. yield return null;
  29. }
  30. Debug.Log("startprocess==¡·" );
  31. AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  32. AndroidJavaObject processBuilder = new AndroidJavaObject("java.lang.ProcessBuilder");
  33. AndroidJavaObject commandList = new AndroidJavaObject("java.util.ArrayList");
  34. commandList.Call<bool>("add", Application.persistentDataPath+ "/build/build/nodejs/node-v14.21.3-linux-arm64/bin/node");
  35. commandList.Call<bool>("add", Application.persistentDataPath + "/build/build/js/build/index.js");
  36. processBuilder.Call<AndroidJavaObject>("command", commandList);
  37. AndroidJavaObject process = processBuilder.Call<AndroidJavaObject>("start");
  38. }
  39. IEnumerator UnzipFile()
  40. {
  41. UnityWebRequest www = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, "build.zip"));
  42. yield return www.SendWebRequest();
  43. if (www.result == UnityWebRequest.Result.Success)
  44. {
  45. byte[] zipBytes = www.downloadHandler.data;
  46. string targetPath = Path.Combine(Application.persistentDataPath, "build");
  47. // Create the target directory if it doesn't exist
  48. if (!Directory.Exists(targetPath))
  49. {
  50. Directory.CreateDirectory(targetPath);
  51. }
  52. // Extract the Zip file to the target directory
  53. using (MemoryStream ms = new MemoryStream(zipBytes))
  54. {
  55. using (ZipArchive zip = new ZipArchive(ms))
  56. {
  57. foreach (ZipArchiveEntry entry in zip.Entries)
  58. {
  59. string entryTargetPath = Path.Combine(targetPath, entry.FullName);
  60. // Create subdirectories if necessary
  61. string entryDirectory = Path.GetDirectoryName(entryTargetPath);
  62. if (!string.IsNullOrEmpty(entryDirectory))
  63. {
  64. Directory.CreateDirectory(entryDirectory);
  65. }
  66. // Extract the entry to the target path
  67. if (!entry.FullName.EndsWith("/"))
  68. {
  69. using (Stream entryStream = entry.Open())
  70. {
  71. using (FileStream fileStream = new FileStream(entryTargetPath, FileMode.Create))
  72. {
  73. entryStream.CopyTo(fileStream);
  74. }
  75. }
  76. }
  77. SetFilePermissions(entryTargetPath);
  78. }
  79. }
  80. }
  81. }
  82. else
  83. {
  84. Debug.Log("Error: " + www.error);
  85. }
  86. }
  87. }