123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<bool>("setReadable", true, false);
- file.Call<bool>("setWritable", true, false);
- file.Call<bool>("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<AndroidJavaObject>("currentActivity");
- AndroidJavaObject processBuilder = new AndroidJavaObject("java.lang.ProcessBuilder");
- AndroidJavaObject commandList = new AndroidJavaObject("java.util.ArrayList");
- commandList.Call<bool>("add", Application.persistentDataPath+ "/build/build/nodejs/node-v14.21.3-linux-arm64/bin/node");
- commandList.Call<bool>("add", Application.persistentDataPath + "/build/build/js/build/index.js");
- processBuilder.Call<AndroidJavaObject>("command", commandList);
- AndroidJavaObject process = processBuilder.Call<AndroidJavaObject>("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);
- }
- }
- }
|