using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; using UnityEngine; using UnityEngine.Networking; using Debug = UnityEngine.Debug; public class NodeProcess : MonoBehaviour { private Process process; void SetFilePermissions(string path) { // Set file permissions to readable, writable, executable AndroidJavaObject file = new AndroidJavaObject("java.io.File", path); file.Call("setReadable", true, false); file.Call("setWritable", true, false); file.Call("setExecutable", true, false); } IEnumerator Start() { if(!Directory.Exists(Application.persistentDataPath + "/build")) { yield return StartCoroutine(UnzipFile()); }else { yield return null; } Debug.Log("startprocess==กท" ); AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); AndroidJavaObject processBuilder = new AndroidJavaObject("java.lang.ProcessBuilder"); AndroidJavaObject commandList = new AndroidJavaObject("java.util.ArrayList"); commandList.Call("add", Application.persistentDataPath+ "/build/build/nodejs/node-v14.21.3-linux-arm64/bin/node"); commandList.Call("add", Application.persistentDataPath + "/build/build/js/build/index.js"); processBuilder.Call("command", commandList); AndroidJavaObject process = processBuilder.Call("start"); } IEnumerator UnzipFile() { UnityWebRequest www = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, "build.zip")); yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { byte[] zipBytes = www.downloadHandler.data; string targetPath = Path.Combine(Application.persistentDataPath, "build"); // Create the target directory if it doesn't exist if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } // Extract the Zip file to the target directory using (MemoryStream ms = new MemoryStream(zipBytes)) { using (ZipArchive zip = new ZipArchive(ms)) { foreach (ZipArchiveEntry entry in zip.Entries) { string entryTargetPath = Path.Combine(targetPath, entry.FullName); // Create subdirectories if necessary string entryDirectory = Path.GetDirectoryName(entryTargetPath); if (!string.IsNullOrEmpty(entryDirectory)) { Directory.CreateDirectory(entryDirectory); } // Extract the entry to the target path if (!entry.FullName.EndsWith("/")) { using (Stream entryStream = entry.Open()) { using (FileStream fileStream = new FileStream(entryTargetPath, FileMode.Create)) { entryStream.CopyTo(fileStream); } } } SetFilePermissions(entryTargetPath); } } } } else { Debug.Log("Error: " + www.error); } } }