Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/src/main/cpp/Core/Canvas/InitializationGate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <atomic>

namespace Canvas {

// Registry writes belong to the bootstrap thread, before publication to native readers.
class InitializationGate {
enum class State { New, Loading, Ready };
std::atomic<State> state{State::New};

public:
bool begin() {
State expected = State::New;
return state.compare_exchange_strong(expected, State::Loading);
}

bool isLoading() const {
return state.load(std::memory_order_acquire) == State::Loading;
}

bool publish() {
State expected = State::Loading;
return state.compare_exchange_strong(expected, State::Ready);
}

bool isReady() const {
return state.load(std::memory_order_acquire) == State::Ready;
}
};

}
4 changes: 2 additions & 2 deletions app/src/main/cpp/include/misc/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ bool isLibraryLoaded(const char *libraryName) {
FILE *fp = fopen(OBFUSCATE("/proc/self/maps"), OBFUSCATE("rt"));
if (fp != NULL) {
while (fgets(line, sizeof(line), fp)) {
std::string a = line;
if (strstr(line, libraryName)) {
libLoaded = true;
fclose(fp);
return true;
}
}
Expand Down Expand Up @@ -110,4 +110,4 @@ uintptr_t string2Offset(const char *c) {
namespace ToastLength {
inline const int LENGTH_LONG = 1;
inline const int LENGTH_SHORT = 0;
}
}
29 changes: 24 additions & 5 deletions app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <limits>
#include <vector>
#include "Canvas/Canvas.h"
#include "Canvas/InitializationGate.h"
#include "Cipher/CipherUtils.h"
#include "Cipher/Cipher.h"
#include "include/misc/visibility.h"
Expand All @@ -31,6 +32,7 @@ extern "C" {
}

static std::string g_skyBuildKey;
static Canvas::InitializationGate g_modInitialization;

void fsel_setup(JNIEnv*);

Expand Down Expand Up @@ -171,6 +173,7 @@ PRIVATE_API void DrawMods() {
}

PRIVATE_API void Canvas::CanvasMenu() {
if (!g_modInitialization.isReady()) return;
ImGui::GetStyle() = canvasBaseStyle;

if(Canvas::hideCanvasMenu) {
Expand Down Expand Up @@ -256,8 +259,12 @@ Java_git_artdeell_skymodloader_MainActivity_settle(
jboolean _ceserver_enabled,
jboolean _hideCanvasMenu
) {
if (!g_modInitialization.begin()) {
jclass error = env->FindClass("java/lang/IllegalStateException");
if (error != nullptr) env->ThrowNew(error, "Native game initialization already started");
return;
}
fsel_setup(env);
Canvas::userLibs.clear();
Canvas::MainActivity = clazz;
Canvas::CeserverEnabled = _ceserver_enabled;
Canvas::hideCanvasMenu = _hideCanvasMenu;
Expand Down Expand Up @@ -293,7 +300,7 @@ PRIVATE_API void *UserThread(Canvas::UserLib *pUserLib){
return nullptr;
}

PRIVATE_API void crash(JNIEnv *env, char* crashReason) {
PRIVATE_API void crash(JNIEnv *env, const char* crashReason) {
jclass exception = env->FindClass("java/lang/Exception");
if(exception != nullptr) {
env->ThrowNew(exception, crashReason);
Expand All @@ -313,7 +320,10 @@ Java_git_artdeell_skymodloader_LibrarySelectorListener_onModLibrary(
jstring _version,
jboolean _selfManagedUI
) {

if (!g_modInitialization.isLoading()) {
crash(env, "Mod registration outside initialization");
return;
}
const char *temp = env->GetStringUTFChars(_path, 0);
void *dl_entry = dlopen(temp, RTLD_LOCAL);
if(dl_entry == nullptr) {
Expand Down Expand Up @@ -376,9 +386,18 @@ Java_git_artdeell_skymodloader_MainActivity_setDeviceInfoNative(
Canvas::deviceInfo.deviceModel = env->GetStringUTFChars(_model, nullptr);
}

extern "C"
JNIEXPORT void JNICALL
Java_git_artdeell_skymodloader_MainActivity_completeModInitialization(JNIEnv *env, jclass clazz) {
if (!g_modInitialization.publish()) {
crash(env, "Invalid native initialization state");
}
}

extern "C"
JNIEXPORT void JNICALL
Java_git_artdeell_skymodloader_MainActivity_lateInitUserLibs(JNIEnv *env, jclass clazz) {
if (!g_modInitialization.isReady()) return;
std::thread lateInitThread([]() -> void {
for (auto& userlib : Canvas::userLibs) {
if (userlib.InitLate != nullptr) {
Expand All @@ -395,8 +414,8 @@ Java_git_artdeell_skymodloader_MainActivity_lateInitUserLibs(JNIEnv *env, jclass
extern "C"
JNIEXPORT void JNICALL
Java_git_artdeell_skymodloader_MainActivity_customServer(JNIEnv *env, jclass clazz, jstring url) {
uintptr_t ssl = (new CipherUtils())->CipherScan("\\x00\\x00\\x00\\x52\\xE2\\x03\\x1F\\xAA\\x00\\x00\\x00\\x94\\x00\\x00\\x00\\xF9\\x00\\x00\\x00\\x52", "???xxxxx???x???x???x");
LOGD("scanner %p", ssl);
uintptr_t ssl = CipherUtils::CipherScan("\\x00\\x00\\x00\\x52\\xE2\\x03\\x1F\\xAA\\x00\\x00\\x00\\x94\\x00\\x00\\x00\\xF9\\x00\\x00\\x00\\x52", "???xxxxx???x???x???x");
LOGD("scanner %p", reinterpret_cast<void*>(ssl));
}

extern "C"
Expand Down
26 changes: 21 additions & 5 deletions app/src/main/java/com/tgc/sky/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private void fixTextRenderingOnProblemDevices_HACK() {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (!hasNativeActivity()) return;
DialogJNI.setActivity(this);
hideNavigationFullScreen(getWindow().getDecorView());
getWindow().addFlags(2097280);
Expand Down Expand Up @@ -319,7 +320,7 @@ private void setupDisplayListener() {
@Override public void onDisplayAdded(int i) {}
@Override public void onDisplayRemoved(int i) {}
@Override public void onDisplayChanged(int i) {
if (displayManager.getDisplay(i) != null) GameActivity.this.onDisplayChangedNative();
if (hasNativeActivity() && displayManager.getDisplay(i) != null) GameActivity.this.onDisplayChangedNative();
}
}, null);
}
Expand All @@ -328,14 +329,20 @@ private void setupDisplayListener() {

@Override
public void onDestroy() {
FMOD.close();
FileSelector.unsetActivity();
this.m_systemIO.onDestroy();
if (hasNativeActivity()) {
FMOD.close();
FileSelector.unsetActivity();
}
if (this.m_systemIO != null) this.m_systemIO.onDestroy();
super.onDestroy();
}

@Override
public void onResume() {
if (!hasNativeActivity()) {
super.onResume();
return;
}
if (this.m_sensorManager != null && this.m_motionListening) {
this.m_motionListening = registerMotionListeners();
}
Expand All @@ -353,6 +360,10 @@ public void onResume() {

@Override
public void onPause() {
if (!hasNativeActivity()) {
super.onPause();
return;
}
if (this.portraitOnResume) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
this.m_systemIO.onPause();
if (this.m_sensorManager != null && this.m_motionListening) {
Expand Down Expand Up @@ -530,7 +541,10 @@ public void onWindowFocusChanged(boolean z) {
}

@Override
public void onBackPressed() { onBackPressedNative(); }
public void onBackPressed() {
if (hasNativeActivity()) onBackPressedNative();
else super.onBackPressed();
}

public void AddOnActivityIntentListener(OnActivityIntentListener l) {
if (this.mOnActivityIntentListeners == null) this.mOnActivityIntentListeners = new ArrayList<>();
Expand All @@ -544,6 +558,8 @@ public void RemoveOnActivityIntentListeners(OnActivityIntentListener l) {

@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (!hasNativeActivity()) return;
ArrayList<OnActivityIntentListener> a = this.mOnActivityIntentListeners;
if (a != null) {
Iterator<OnActivityIntentListener> it = a.iterator();
Expand Down
48 changes: 34 additions & 14 deletions app/src/main/java/com/tgc/sky/TGCNativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;

import git.artdeell.skymodloader.SMLApplication;
import git.artdeell.skymodloader.GameRuntime;

/* loaded from: classes2.dex */
public abstract class TGCNativeActivity extends Activity implements SurfaceHolder.Callback2, ViewTreeObserver.OnGlobalLayoutListener {
Expand All @@ -21,6 +22,7 @@ public abstract class TGCNativeActivity extends Activity implements SurfaceHolde
int m_lastContentX;
int m_lastContentY;
private boolean m_destroyed = false;
private boolean m_nativeCreated = false;
private long m_handle = 0;
final int[] m_location = new int[2];

Expand Down Expand Up @@ -57,12 +59,27 @@ public abstract class TGCNativeActivity extends Activity implements SurfaceHolde
@Override // android.app.Activity
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (!GameRuntime.isReady()) {
GameRuntime.returnToManager(this);
return;
}
findViewById(android.R.id.content).getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
this.m_handle = onCreateNative(getAbsolutePath(getFilesDir()), getAbsolutePath(getObbDir()), getAbsolutePath(getExternalFilesDir(null)), Build.VERSION.SDK_INT, SMLApplication.skyRes.getAssets(), bundle != null ? bundle.getByteArray(KEY_NATIVE_SAVED_STATE) : null);
m_nativeCreated = true;
}

protected final boolean hasNativeActivity() {
return m_nativeCreated && !m_destroyed;
}

@Override
public void onDestroy() {
if (!hasNativeActivity()) {
m_destroyed = true;
super.onDestroy();
return;
}
findViewById(android.R.id.content).getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
git.artdeell.skymodloader.AccountStorage.saveActive(this);
this.m_destroyed = true;
SurfaceHolder surfaceHolder = this.m_currentSurfaceHolder;
Expand All @@ -71,25 +88,28 @@ public void onDestroy() {
this.m_currentSurfaceHolder = null;
}
onDestroyNative(this.m_handle);
m_nativeCreated = false;
m_handle = 0;
super.onDestroy();
}

@Override
public void onPause() {
git.artdeell.skymodloader.AccountStorage.saveActive(this);
if (hasNativeActivity()) git.artdeell.skymodloader.AccountStorage.saveActive(this);
super.onPause();
onPauseNative(this.m_handle);
if (hasNativeActivity()) onPauseNative(this.m_handle);
}

@Override
public void onResume() {
super.onResume();
onResumeNative(this.m_handle);
if (hasNativeActivity()) onResumeNative(this.m_handle);
}

@Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
if (!hasNativeActivity()) return;
byte[] onSaveInstanceStateNative = onSaveInstanceStateNative(this.m_handle);
if (onSaveInstanceStateNative != null) {
bundle.putByteArray(KEY_NATIVE_SAVED_STATE, onSaveInstanceStateNative);
Expand All @@ -99,20 +119,20 @@ protected void onSaveInstanceState(Bundle bundle) {
@Override
protected void onStart() {
super.onStart();
onStartNative(this.m_handle);
if (hasNativeActivity()) onStartNative(this.m_handle);
}

@Override
protected void onStop() {
git.artdeell.skymodloader.AccountStorage.saveActive(this);
if (hasNativeActivity()) git.artdeell.skymodloader.AccountStorage.saveActive(this);
super.onStop();
onStopNative(this.m_handle);
if (hasNativeActivity()) onStopNative(this.m_handle);
}

@Override // android.app.Activity, android.content.ComponentCallbacks
public void onConfigurationChanged(Configuration configuration) {
super.onConfigurationChanged(configuration);
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
onConfigurationChangedNative(this.m_handle);
Expand All @@ -121,7 +141,7 @@ public void onConfigurationChanged(Configuration configuration) {
@Override // android.app.Activity, android.content.ComponentCallbacks
public void onLowMemory() {
super.onLowMemory();
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
onLowMemoryNative(this.m_handle);
Expand All @@ -130,15 +150,15 @@ public void onLowMemory() {
@Override // android.app.Activity, android.view.Window.Callback
public void onWindowFocusChanged(boolean z) {
super.onWindowFocusChanged(z);
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
onWindowFocusChangedNative(this.m_handle, z);
}

@Override // android.view.SurfaceHolder.Callback
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
this.m_currentSurfaceHolder = surfaceHolder;
Expand All @@ -147,7 +167,7 @@ public void surfaceCreated(SurfaceHolder surfaceHolder) {

@Override // android.view.SurfaceHolder.Callback
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
this.m_currentSurfaceHolder = surfaceHolder;
Expand All @@ -156,7 +176,7 @@ public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {

@Override // android.view.SurfaceHolder.Callback2
public void surfaceRedrawNeeded(SurfaceHolder surfaceHolder) {
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
this.m_currentSurfaceHolder = surfaceHolder;
Expand All @@ -166,7 +186,7 @@ public void surfaceRedrawNeeded(SurfaceHolder surfaceHolder) {
@Override // android.view.SurfaceHolder.Callback
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
this.m_currentSurfaceHolder = null;
if (this.m_destroyed) {
if (!hasNativeActivity()) {
return;
}
onSurfaceDestroyedNative(this.m_handle, surfaceHolder.getSurface());
Expand All @@ -186,7 +206,7 @@ public void onGlobalLayout() {
this.m_lastContentY = i2;
this.m_lastContentWidth = width;
this.m_lastContentHeight = height;
if (!this.m_destroyed) {
if (hasNativeActivity()) {
onContentRectChangedNative(this.m_handle, i, i2, width, height);
}
}
Expand Down
Loading
Loading