Skip to content

Commit

Permalink
💥 add shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Szzrain committed Apr 1, 2024
1 parent 61787e3 commit 927a1fd
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 10 deletions.
15 changes: 15 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ android {
versionName "v0.6.1-dev"
buildConfigField "String", "DOCUMENTS_AUTHORITY", "\"com.sealdice.dice.authorities\""
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
externalNativeBuild {
ndkBuild {
cFlags "-std=c11", "-Wall", "-Wextra", "-Werror", "-Os", "-fno-stack-protector", "-Wl,--gc-sections"
}
}

ndk {
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
dataBinding {
enabled = true
Expand All @@ -26,6 +35,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -57,6 +71,7 @@ dependencies {
implementation 'androidx.preference:preference:1.2.0'
implementation 'androidx.lifecycle:lifecycle-service:2.6.1'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
implementation 'org.apache.commons:commons-compress:1.20'
api 'com.tencent.tbs:tbssdk:44286'
implementation 'androidx.core:core-ktx:1.10.0'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools" package="com.sealdice.dice">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/sealdice/dice/DebugActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class DebugActivity : AppCompatActivity() {
val command = findViewById<EditText>(R.id.DEBUG_edittext_command).text.toString()
if (!isInit) {
processBuilder.directory(File(this.filesDir.absolutePath))
// processBuilder.environment()["LD_LIBRARY_PATH"] = this.filesDir.absolutePath + "/sealdice/lagrange/openssl-1.1"
// processBuilder.environment()["CLR_OPENSSL_VERSION_OVERRIDE"] = "1.1"
processBuilder.environment()["LD_LIBRARY_PATH"] = this.filesDir.absolutePath + "/sealdice/lagrange/openssl-1.1"
processBuilder.environment()["CLR_OPENSSL_VERSION_OVERRIDE"] = "1.1"

GlobalScope.launch(context = Dispatchers.IO) {
isRunning = true
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/sealdice/dice/FirstFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.finishAffinity
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import com.google.android.material.snackbar.Snackbar
import com.sealdice.dice.common.DeviceInfo
import com.sealdice.dice.common.ExtractAssets
import com.sealdice.dice.common.FileWrite
import com.sealdice.dice.databinding.FragmentFirstBinding
import com.sealdice.dice.utils.DecompressUtil
import com.sealdice.dice.utils.Utils
import com.sealdice.dice.utils.ViewModelMain
import kotlinx.coroutines.*
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import kotlin.system.exitProcess


Expand Down Expand Up @@ -159,6 +156,8 @@ class FirstFragment : Fragment() {
} else {
if (sharedPreferences?.getBoolean("extract_on_start", true) == true) {
ExtractAssets(context).extractResources("sealdice")
ExtractAssets(context).extractResources("runner")
DecompressUtil.decompressTar(FileWrite.getPrivateFileDir(requireContext())+"runner/app-runner-arm64.tar", FileWrite.getPrivateFileDir(requireContext())+"runner/")
}
binding.buttonTut.visibility = View.GONE
binding.buttonInput.visibility = View.GONE
Expand Down Expand Up @@ -441,7 +440,7 @@ class FirstFragment : Fragment() {
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
return if (file.exists() && file.isFile) {
if (file.delete()) {
Log.e(
Log.d(
"--Method--",
"Copy_Delete.deleteSingleFile: 删除单个文件" + filePath + "成功!"
)
Expand Down Expand Up @@ -487,7 +486,7 @@ class FirstFragment : Fragment() {
}
// 删除当前目录
return if (dirFile.delete()) {
Log.e("--Method--", "Copy_Delete.deleteDirectory: 删除目录" + filePath + "成功!")
Log.d("--Method--", "Copy_Delete.deleteDirectory: 删除目录" + filePath + "成功!")
true
} else {
false
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/sealdice/dice/JNI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.sealdice.dice;

final class JNI {

static {
System.loadLibrary("sealjni");
}

/**
* Create a subprocess. Differs from {@link ProcessBuilder} in that a pseudoterminal is used to communicate with the
* subprocess.
* <p/>
* Callers are responsible for calling {@link #close(int)} on the returned file descriptor.
*
* @param cmd The command to execute
* @param cwd The current working directory for the executed command
* @param args An array of arguments to the command
* @param envVars An array of strings of the form "VAR=value" to be added to the environment of the process
* @param processId A one-element array to which the process ID of the started process will be written.
* @return the file descriptor resulting from opening /dev/ptmx master device. The sub process will have opened the
* slave device counterpart (/dev/pts/$N) and have it as stdint, stdout and stderr.
*/
public static native int createSubprocess(String cmd, String cwd, String[] args, String[] envVars, int[] processId, int rows, int columns);

/** Set the window size for a given pty, which allows connected programs to learn how large their screen is. */
public static native void setPtyWindowSize(int fd, int rows, int cols);

/**
* Causes the calling thread to wait for the process associated with the receiver to finish executing.
*
* @return if >= 0, the exit status of the process. If < 0, the signal causing the process to stop negated.
*/
public static native int waitFor(int processId);

/** Close a file descriptor through the close(2) system call. */
public static native void close(int fileDescriptor);

}
2 changes: 2 additions & 0 deletions app/src/main/java/com/sealdice/dice/ProcessService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class ProcessService : LifecycleService(){
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
processBuilder.environment()["LD_LIBRARY_PATH"] = this.filesDir.absolutePath + sharedPreferences.getString("ld_library_path", "/sealdice/lagrange/openssl-1.1")
processBuilder.environment()["CLR_OPENSSL_VERSION_OVERRIDE"] = "1.1"
processBuilder.environment()["RUNNER_PATH"] = this.filesDir.absolutePath + "/runner"
// var env = arrayOf("LD_LIBRARY_PATH=${this.filesDir.absolutePath + sharedPreferences.getString("ld_library_path", "/sealdice/lagrange/openssl-1.1")}", "CLR_OPENSSL_VERSION_OVERRIDE=1.1")
process = processBuilder.start()
val args = sharedPreferences.getString("launch_args", "")
val cmd = "cd sealdice&&./sealdice-core $args"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sealdice.dice.common;

import android.content.Context;

import android.util.Log;


import java.util.HashMap;
Expand Down Expand Up @@ -44,6 +44,7 @@ private String extractScript(String fileName) {

public String extractResource(String fileName) {
if (fileName == null || fileName.isEmpty()) {
Log.e("ExtractAssetsResource", "fileName is null or empty");
return null;
}

Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/sealdice/dice/utils/DecompressUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.sealdice.dice.utils;

import java.io.*;
import java.nio.file.*;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

public class DecompressUtil {


public static void decompressTar(String tarFilePath, String outputDir) throws IOException {
try (FileInputStream fis = new FileInputStream(tarFilePath);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(fis)) {
org.apache.commons.compress.archivers.tar.TarArchiveEntry entry;

while ((entry = (org.apache.commons.compress.archivers.tar.TarArchiveEntry) tarArchiveInputStream.getNextEntry()) != null) {
final String individualFile = outputDir + File.separator + entry.getName();
final File file = new File(individualFile);

if (entry.isDirectory()) {
if (!file.exists()) {
file.mkdirs();
}
} else {
int count;
byte[] data = new byte[1024];
FileOutputStream fos = new FileOutputStream(individualFile, false);
try (BufferedOutputStream dest = new BufferedOutputStream(fos, 1024)) {
while ((count = tarArchiveInputStream.read(data, 0, 1024)) != -1) {
dest.write(data, 0, count);
}
}
}
}
}
}

public static void decompressTarGz(String tarGzFilePath, String outputDir) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(Files.newInputStream(Paths.get(tarGzFilePath)));
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gis)) {
org.apache.commons.compress.archivers.tar.TarArchiveEntry entry;

while ((entry = (org.apache.commons.compress.archivers.tar.TarArchiveEntry) tarArchiveInputStream.getNextEntry()) != null) {
final Path outputPath = Paths.get(outputDir, entry.getName());

if (entry.isDirectory()) {
Files.createDirectories(outputPath);
} else if (entry.isSymbolicLink()) {
// 对符号链接的处理
Path target = Paths.get(entry.getLinkName());
Files.createSymbolicLink(outputPath, target);
} else {
// 处理常规文件
Files.createDirectories(outputPath.getParent());
Files.copy(tarArchiveInputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libsealjni
LOCAL_SRC_FILES:= jni.c
include $(BUILD_SHARED_LIBRARY)
Loading

0 comments on commit 927a1fd

Please sign in to comment.