jkxr/java/com/drbeef/jkquest/GLES3JNIActivity.java
2022-10-15 00:26:21 +01:00

341 lines
8.5 KiB
Java

package com.drbeef.jkquest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import static android.system.Os.setenv;
@SuppressLint("SdCardPath") public class GLES3JNIActivity extends Activity implements SurfaceHolder.Callback
{
private static String game = "";
// Load the gles3jni library right away to make sure JNI_OnLoad() gets called as the very first thing.
static
{
game = "jo";
System.loadLibrary( "openjk_" + game );
}
private static final String TAG = "JKQuest";
private static final String APPLICATION = "JKQuest";
private int permissionCount = 0;
private static final int READ_EXTERNAL_STORAGE_PERMISSION_ID = 1;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_ID = 2;
String commandLineParams;
private SurfaceView mView;
private SurfaceHolder mSurfaceHolder;
private long mNativeHandle;
// Main components
protected static GLES3JNIActivity mSingleton;
public static void initialize() {
// The static nature of the singleton and Android quirkyness force us to initialize everything here
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
mSingleton = null;
}
public void shutdown() {
System.exit(0);
}
@Override protected void onCreate( Bundle icicle )
{
Log.v( TAG, "----------------------------------------------------------------" );
Log.v( TAG, "GLES3JNIActivity::onCreate()" );
super.onCreate( icicle );
GLES3JNIActivity.initialize();
// So we can call stuff from static callbacks
mSingleton = this;
mView = new SurfaceView( this );
setContentView( mView );
mView.getHolder().addCallback( this );
// Force the screen to stay on, rather than letting it dim and shut off
// while the user is watching a movie.
getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
// Force screen brightness to stay at maximum
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes( params );
checkPermissionsAndInitialize();
}
/** Initializes the Activity only if the permission has been granted. */
private void checkPermissionsAndInitialize() {
// Boilerplate for checking runtime permissions in Android.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(
GLES3JNIActivity.this,
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_PERMISSION_ID);
}
else
{
permissionCount++;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(
GLES3JNIActivity.this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
READ_EXTERNAL_STORAGE_PERMISSION_ID);
}
else
{
permissionCount++;
}
if (permissionCount == 2) {
// Permissions have already been granted.
create();
}
}
/** Handles the user accepting the permission. */
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
if (requestCode == READ_EXTERNAL_STORAGE_PERMISSION_ID) {
if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
permissionCount++;
}
else
{
System.exit(0);
}
}
if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_ID) {
if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
permissionCount++;
}
else
{
System.exit(0);
}
}
checkPermissionsAndInitialize();
}
public void create() {
//Make the directories
new File("/sdcard/JKQuest/Demo/base").mkdirs();
//clear the contents of the base folder
for (File child : new File("/sdcard/JKQuest/Demo/base").listFiles()) {
if (child.isFile() &&
child.getName().contains("pk3") &&
!child.getName().contains("z_vr_assets") // don't delete our npc mod as this might be tweaked
)
child.delete();
}
//Weapon offsets
copy_asset("/sdcard/JKQuest/Demo/base", "weapons_vr_jo.cfg", true);
//Demo
copy_asset("/sdcard/JKQuest/Demo/base", "assets0.pk3", false);
//Bunch of cool mods just for this
copy_asset("/sdcard/JKQuest/Demo/base", "GGDynamicWeapons.pk3", false);
copy_asset("/sdcard/JKQuest/Demo/base", "Haps_Stormtrooper.pk3", false);
copy_asset("/sdcard/JKQuest/Demo/base", "Kyle's_lightsaber_hilt_hd.pk3", false);
copy_asset("/sdcard/JKQuest/Demo/base", "UltimateSounds_JK2.pk3", false);
copy_asset("/sdcard/JKQuest/Demo/base", "z_bryar_ashura.pk3", false);
//Our assets
copy_asset("/sdcard/JKQuest/Demo/base", "z_vr_assets.pk3", true);
commandLineParams = "jo";
try {
setenv("JK_LIBDIR", getApplicationInfo().nativeLibraryDir, true);
}
catch (Exception e)
{
}
mNativeHandle = GLES3JNILib.onCreate( this, commandLineParams );
}
public void copy_asset(String path, String name, boolean force) {
File f = new File(path + "/" + name);
if (!f.exists() || force) {
//Ensure we have an appropriate folder
String fullname = path + "/" + name;
String directory = fullname.substring(0, fullname.lastIndexOf("/"));
new File(directory).mkdirs();
_copy_asset(name, path + "/" + name);
}
}
public void _copy_asset(String name_in, String name_out) {
AssetManager assets = this.getAssets();
try {
InputStream in = assets.open(name_in);
OutputStream out = new FileOutputStream(name_out);
copy_stream(in, out);
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy_stream(InputStream in, OutputStream out)
throws IOException {
byte[] buf = new byte[1024];
while (true) {
int count = in.read(buf);
if (count <= 0)
break;
out.write(buf, 0, count);
}
}
@Override protected void onStart()
{
Log.v( TAG, "GLES3JNIActivity::onStart()" );
super.onStart();
if ( mNativeHandle != 0 )
{
GLES3JNILib.onStart(mNativeHandle, this);
}
}
@Override protected void onResume()
{
Log.v( TAG, "GLES3JNIActivity::onResume()" );
super.onResume();
if ( mNativeHandle != 0 )
{
GLES3JNILib.onResume(mNativeHandle);
}
}
@Override protected void onPause()
{
Log.v( TAG, "GLES3JNIActivity::onPause()" );
if ( mNativeHandle != 0 )
{
GLES3JNILib.onPause(mNativeHandle);
}
super.onPause();
}
@Override protected void onStop()
{
Log.v( TAG, "GLES3JNIActivity::onStop()" );
if ( mNativeHandle != 0 )
{
GLES3JNILib.onStop(mNativeHandle);
}
super.onStop();
}
@Override protected void onDestroy()
{
Log.v( TAG, "GLES3JNIActivity::onDestroy()" );
if ( mSurfaceHolder != null )
{
GLES3JNILib.onSurfaceDestroyed( mNativeHandle );
}
if ( mNativeHandle != 0 )
{
GLES3JNILib.onDestroy(mNativeHandle);
}
super.onDestroy();
// Reset everything in case the user re opens the app
GLES3JNIActivity.initialize();
mNativeHandle = 0;
}
@Override public void surfaceCreated( SurfaceHolder holder )
{
Log.v( TAG, "GLES3JNIActivity::surfaceCreated()" );
if ( mNativeHandle != 0 )
{
GLES3JNILib.onSurfaceCreated( mNativeHandle, holder.getSurface() );
mSurfaceHolder = holder;
}
}
@Override public void surfaceChanged( SurfaceHolder holder, int format, int width, int height )
{
Log.v( TAG, "GLES3JNIActivity::surfaceChanged()" );
if ( mNativeHandle != 0 )
{
GLES3JNILib.onSurfaceChanged( mNativeHandle, holder.getSurface() );
mSurfaceHolder = holder;
}
}
@Override public void surfaceDestroyed( SurfaceHolder holder )
{
Log.v( TAG, "GLES3JNIActivity::surfaceDestroyed()" );
if ( mNativeHandle != 0 )
{
GLES3JNILib.onSurfaceDestroyed( mNativeHandle );
mSurfaceHolder = null;
}
}
}