- Jul 10, 2019
- 2
- 11
Hey all,
this is probably my first and last post.
I wrote some months ago a permanent save system for Renpy Android, which is used in MnF and MurMur. (I would link them but my post karma says no)
Today I wanted to share my bits here. Its not much I have changed.
Disclaimer
First of this is based on RenPy 7.3.5 so positions of code can differ with time.
Second I don't/never own/ed an Android Phone so I deved this on Bluestacks. Also I dislike Android for some reasons I wont go further in.
Perms and Usage:
Feel free to use it for your game, I would appreciate to be atleast named as Naidin when used.
Also I don't want to see this Stuff in the RenPy Repository! It has too many bad Coding habbits in it.
Feel free to change parts.
Known Stuff:
Somehow the persistent save from renpy on android is boggus, what I found out with tracing was that if you kill the running app it won't save. So this could effect galleries based on did the person read bla for example. Forcing renpy to save brought some lage spikes with it so can't recommend using this as countermeassure.
The two files are also attached.
The first file I changed was the renpy.py which is located in the base folder of the sdk this is from 53 to 93 it overwrites also
There is not much to add here i changed how the pathing works and created also a folder creation method.
Now to the second file I changed this is PythonSDLActivity.java this is the file which is loading RenPy in Android.
Location can be two(depeding if you already compiled sth or not)
\renpy-7.3.5-sdk\rapt\prototype\renpyandroid\src\main\java\org\renpy\android
\renpy-7.3.5-sdk\rapt\project\renpyandroid\src\main\java\org\renpy\android
I changed multiple parts in here so cant give row changes but the main change is between 280 and 344. This is where I wrote the hook for asking for Fileperms on android. Since this done async i had to add a bad thing called while(waiting) I would appreciate if someelse has a better Idea, Thread.sleep didnt work ...
this is probably my first and last post.
I wrote some months ago a permanent save system for Renpy Android, which is used in MnF and MurMur. (I would link them but my post karma says no)
Today I wanted to share my bits here. Its not much I have changed.
Disclaimer
First of this is based on RenPy 7.3.5 so positions of code can differ with time.
Second I don't/never own/ed an Android Phone so I deved this on Bluestacks. Also I dislike Android for some reasons I wont go further in.
Perms and Usage:
Feel free to use it for your game, I would appreciate to be atleast named as Naidin when used.
Also I don't want to see this Stuff in the RenPy Repository! It has too many bad Coding habbits in it.
Feel free to change parts.
Known Stuff:
Somehow the persistent save from renpy on android is boggus, what I found out with tracing was that if you kill the running app it won't save. So this could effect galleries based on did the person read bla for example. Forcing renpy to save brought some lage spikes with it so can't recommend using this as countermeassure.
The two files are also attached.
The first file I changed was the renpy.py which is located in the base folder of the sdk this is from 53 to 93 it overwrites also
Code:
# Makes sure the permissions are right on the save directory.
def test_writable(d):
try:
fn = os.path.join(d, "test.txt")
open(fn, "w").close()
open(fn, "r").close()
os.unlink(fn)
return True
except:
return False
def try_create_folder(d):
try:
os.mkdir(d)
return True
except:
return False
# Android.
if renpy.android:
paths = [
os.path.join("//sdcard//Android//data//RenPy", save_directory),
os.path.join(os.environ["ANDROID_PUBLIC"], "saves"),
os.path.join(os.environ["ANDROID_OLD_PUBLIC"], "game/saves"),
os.path.join(os.environ["ANDROID_PRIVATE"], "saves"),
]
if not os.path.isdir("//sdcard//Android//data//RenPy"):
try_create_folder("//sdcard//Android//data//RenPy")
for rv in paths:
if not os.path.isdir(rv):
try_create_folder(rv)
if os.path.isdir(rv) and test_writable(rv):
break
print("Saving to", rv)
# We return the last path as the default.
return rv
Now to the second file I changed this is PythonSDLActivity.java this is the file which is loading RenPy in Android.
Location can be two(depeding if you already compiled sth or not)
\renpy-7.3.5-sdk\rapt\prototype\renpyandroid\src\main\java\org\renpy\android
\renpy-7.3.5-sdk\rapt\project\renpyandroid\src\main\java\org\renpy\android
Java:
package org.renpy.android;
import org.libsdl.app.SDLActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnSystemUiVisibilityChangeListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
/* Naidn Special Import */
import android.support.v4.app.ActivityCompat;
import android.Manifest;
/* END */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.renpy.iap.Store;
public class PythonSDLActivity extends SDLActivity {
private static final int PERMISSION_REQUEST_WRITE = 0;
private boolean waiting = true;
/**
* This exists so python code can access this activity.
*/
public static PythonSDLActivity mActivity = null;
/**
* The layout that contains the SDL view. VideoPlayer uses this to add
* its own view on on top of the SDL view.
*/
public FrameLayout mFrameLayout;
/**
* A layout that contains mLayout. This is a 3x3 grid, with the layout
* in the center. The idea is that if someone wants to show an ad, they
* can stick it in one of the other cells..
*/
public LinearLayout mVbox;
ResourceManager resourceManager;
protected String[] getLibraries() {
return new String[] {
"png16",
"SDL2",
"SDL2_image",
"SDL2_ttf",
"SDL2_gfx",
"SDL2_mixer",
"python2.7",
"pymodules",
"main",
};
}
// GUI code. /////////////////////////////////////////////////////////////
public void addView(View view, int index) {
mVbox.addView(view, index, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, (float) 0.0));
}
public void removeView(View view) {
mVbox.removeView(view);
}
@Override
public void setContentView(View view) {
mFrameLayout = new FrameLayout(this);
mFrameLayout.addView(view);
mVbox = new LinearLayout(this);
mVbox.setOrientation(LinearLayout.VERTICAL);
mVbox.addView(mFrameLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, (float) 1.0));
super.setContentView(mVbox);
}
private void setupMainWindowDisplayMode() {
View decorView = setSystemUiVisibilityMode();
decorView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
setSystemUiVisibilityMode(); // Needed to avoid exiting immersive_sticky when keyboard is displayed
}
});
}
private View setSystemUiVisibilityMode() {
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int options;
options =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(options);
}
return decorView;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
setupMainWindowDisplayMode();
}
}
@Override
public void onResume() {
super.onResume();
setupMainWindowDisplayMode();
}
// Code to unpack python and get things running ///////////////////////////
public void recursiveDelete(File f) {
if (f.isDirectory()) {
for (File r : f.listFiles()) {
recursiveDelete(r);
}
}
f.delete();
}
/**
* This determines if unpacking one the zip files included in
* the .apk is necessary. If it is, the zip file is unpacked.
*/
public void unpackData(final String resource, File target) {
/**
* Delete main.pyo unconditionally. This fixes a problem where we have
* a main.py newer than main.pyo, but start.c won't run it.
*/
new File(target, "main.pyo").delete();
boolean shouldUnpack = false;
// The version of data in memory and on disk.
String data_version = resourceManager.getString(resource + "_version");
String disk_version = null;
String filesDir = target.getAbsolutePath();
String disk_version_fn = filesDir + "/" + resource + ".version";
// If no version, no unpacking is necessary.
if (data_version != null) {
try {
byte buf[] = new byte[64];
InputStream is = new FileInputStream(disk_version_fn);
int len = is.read(buf);
disk_version = new String(buf, 0, len);
is.close();
} catch (Exception e) {
disk_version = "";
}
if (! data_version.equals(disk_version)) {
shouldUnpack = true;
}
}
// If the disk data is out of date, extract it and write the
// version file.
if (shouldUnpack) {
Log.v("python", "Extracting " + resource + " assets.");
// Delete old libraries & renpy files.
recursiveDelete(new File(target, "lib"));
recursiveDelete(new File(target, "renpy"));
target.mkdirs();
AssetExtract ae = new AssetExtract(this);
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
toastError("Could not extract " + resource + " data.");
}
try {
// Write .nomedia.
new File(target, ".nomedia").createNewFile();
// Write version file.
FileOutputStream os = new FileOutputStream(disk_version_fn);
os.write(data_version.getBytes());
os.close();
} catch (Exception e) {
Log.w("python", e);
}
}
}
/**
* Show an error using a toast. (Only makes sense from non-UI
* threads.)
*/
public void toastError(final String msg) {
final Activity thisActivity = this;
runOnUiThread(new Runnable () {
public void run() {
Toast.makeText(thisActivity, msg, Toast.LENGTH_LONG).show();
}
});
// Wait to show the error.
synchronized (this) {
try {
this.wait(1000);
} catch (InterruptedException e) {
}
}
}
public native void nativeSetEnv(String variable, String value);
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_WRITE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
/*moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);*/
}
waiting = false;
return;
}
}
}
public void preparePython() {
Log.v("python", "Starting preparePython.");
mActivity = this;
resourceManager = new ResourceManager(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(PythonSDLActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE);
while(waiting){
}
}
File oldExternalStorage = new File(Environment.getExternalStorageDirectory(), getPackageName());
File externalStorage = getExternalFilesDir(null);
File path;
if (externalStorage == null) {
externalStorage = oldExternalStorage;
}
if (resourceManager.getString("public_version") != null) {
path = externalStorage;
} else {
path = getFilesDir();
}
unpackData("private", getFilesDir());
// unpackData("public", externalStorage);
nativeSetEnv("ANDROID_ARGUMENT", path.getAbsolutePath());
nativeSetEnv("ANDROID_PRIVATE", getFilesDir().getAbsolutePath());
nativeSetEnv("ANDROID_PUBLIC", externalStorage.getAbsolutePath());
nativeSetEnv("ANDROID_OLD_PUBLIC", oldExternalStorage.getAbsolutePath());
// Figure out the APK path.
String apkFilePath;
ApplicationInfo appInfo;
PackageManager packMgmr = getApplication().getPackageManager();
try {
appInfo = packMgmr.getApplicationInfo(getPackageName(), 0);
apkFilePath = appInfo.sourceDir;
} catch (NameNotFoundException e) {
apkFilePath = "";
}
nativeSetEnv("ANDROID_APK", apkFilePath);
String expansionFile = getIntent().getStringExtra("expansionFile");
if (expansionFile != null) {
nativeSetEnv("ANDROID_EXPANSION", expansionFile);
}
nativeSetEnv("PYTHONOPTIMIZE", "2");
nativeSetEnv("PYTHONHOME", getFilesDir().getAbsolutePath());
nativeSetEnv("PYTHONPATH", path.getAbsolutePath() + ":" + getFilesDir().getAbsolutePath() + "/lib");
Log.v("python", "Finished preparePython.");
};
// Code to support devicePurchase. /////////////////////////////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Store.create(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Store.getStore().destroy();
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (Store.getStore().onActivityResult(requestCode, resultCode, intent)) {
return;
}
super.onActivityResult(requestCode, resultCode, intent);
}
// Code to support public APIs. ////////////////////////////////////////////
public void openUrl(String url) {
Log.i("python", "Opening URL: " + url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
public void vibrate(double s) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate((int) (1000 * s));
}
}
public int getDPI() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.densityDpi;
}
public PowerManager.WakeLock wakeLock = null;
public void setWakeLock(boolean active) {
if (wakeLock == null) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Screen On");
wakeLock.setReferenceCounted(false);
}
if (active) {
wakeLock.acquire();
} else {
wakeLock.release();
}
}
}
Last edited: