Skip to content

Commit 8b13137

Browse files
committed
Add runOnAndroid.py
1 parent bc21c2d commit 8b13137

File tree

3 files changed

+127
-7
lines changed

3 files changed

+127
-7
lines changed

Editor/Script~/runOnAndroid.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
import os
3+
import shlex
4+
import subprocess
5+
import sys
6+
7+
8+
def call(cmd: str, printOutput: bool = True) -> tuple[str, bool]:
9+
# print(f"{printOutput = }, {cmd = }")
10+
if sys.platform == "win32":
11+
args = cmd
12+
else:
13+
# linux must split arguments
14+
args = shlex.split(cmd)
15+
try:
16+
if printOutput:
17+
isOk = subprocess.call(args) == 0
18+
return "", isOk
19+
20+
data = subprocess.check_output(args)
21+
# python3 output is bytes
22+
output = data.decode("utf-8")
23+
return output, True
24+
except subprocess.CalledProcessError as callerr:
25+
print(f"cmd = {cmd}, callerr.output = {callerr.output}", file=sys.stderr)
26+
return (callerr.output, False)
27+
except IOError as ioerr:
28+
print(f"cmd = {cmd}, ioerr = {ioerr}", file=sys.stderr)
29+
return "", False
30+
31+
32+
def get_project_dir():
33+
BASE_DIR = os.path.dirname(__file__)
34+
BASE_DIR = os.path.dirname(BASE_DIR)
35+
while BASE_DIR:
36+
BASE_DIR = os.path.dirname(BASE_DIR)
37+
if not BASE_DIR:
38+
return None
39+
assets_dir = os.path.join(BASE_DIR, "Assets")
40+
projectsettings_dir = os.path.join(BASE_DIR, "ProjectSettings")
41+
if os.path.exists(assets_dir) and os.path.exists(projectsettings_dir):
42+
return BASE_DIR
43+
return None
44+
45+
def get_script_file(project_dir):
46+
setting_file = os.path.join(project_dir, "UserSettings/LuaInteractive.json")
47+
if os.path.exists(setting_file):
48+
with open(setting_file, mode='r', encoding='utf-8') as f:
49+
setting = json.loads(f.read())
50+
script_file = setting.get('scriptPath', '')
51+
if script_file:
52+
script_file = os.path.join(project_dir, script_file)
53+
if os.path.exists(script_file):
54+
return script_file
55+
56+
return None
57+
58+
def get_package_name(project_dir):
59+
import yaml
60+
setting_file = os.path.join(project_dir, "ProjectSettings/ProjectSettings.asset")
61+
with open(setting_file, mode='r', encoding='utf-8') as f:
62+
f.readline()
63+
f.readline()
64+
f.readline()
65+
setting = yaml.load(f, Loader=yaml.CLoader)
66+
name = setting.get("PlayerSettings").get("applicationIdentifier").get("Android")
67+
return name.strip()
68+
return None
69+
70+
def get_real_package_name(project_dir):
71+
if len(sys.argv) == 2:
72+
return sys.argv[1]
73+
74+
return get_package_name(project_dir)
75+
76+
77+
project_dir = get_project_dir()
78+
assert project_dir is not None, 'Cannot found unity project folder'
79+
script_file = get_script_file(project_dir)
80+
assert script_file is not None, 'Cannot found lua script file'
81+
package_name = get_real_package_name(project_dir)
82+
assert package_name is not None, 'Cannot found package name'
83+
84+
print(script_file)
85+
86+
cmd = f'adb push "{script_file}" /sdcard/Android/data/{package_name}/files/_luarunner.lua'
87+
call(cmd)
88+
cmd = "adb shell input keyevent KEYCODE_F8"
89+
call(cmd)
90+
print("run success")

Editor/Settings.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,29 @@ public class SettingData
1717
}
1818
public static class Settings
1919
{
20-
const string SettingPath = "ProjectSettings/LuaInteractive.json";
20+
const string OldPath = "ProjectSettings/LuaInteractive.json";
21+
const string SettingPath = "UserSettings/LuaInteractive.json";
2122
private static SettingData settingData;
2223

2324
[InitializeOnLoadMethod]
2425
private static void Init()
2526
{
2627
settingData = new SettingData();
28+
MoveSettingFile();
2729
LoadData();
2830
}
2931

32+
private static void MoveSettingFile()
33+
{
34+
if (File.Exists(OldPath) && !File.Exists(SettingPath))
35+
{
36+
var dir = Path.GetDirectoryName(SettingPath);
37+
if (!Directory.Exists(dir))
38+
Directory.CreateDirectory(dir);
39+
File.Move(OldPath, SettingPath);
40+
}
41+
}
42+
3043
public static ClearLogMode AutoClearLog
3144
{
3245
get { return settingData.clearLogMode; }

Runtime/LuaRunner.cs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
#if UNITY_EDITOR || LUA_RUNNER_RUNTIME
12
#if UNITY_EDITOR
23
using litefeel.LuaInteractive.Editor;
3-
4+
#endif
45
using System;
56
using System.Collections;
67
using System.IO;
78
using System.Reflection;
8-
using UnityEditor;
99
using UnityEngine;
1010

1111
namespace litefeel.LuaInteractive
@@ -34,6 +34,7 @@ private static void Init()
3434
else if (IsXLua(out GetLuaState, out DoString))
3535
luaType = LuaType.XLua;
3636

37+
//Debug.LogError("LuaRunner.Init");
3738
var debuger = new GameObject("_LuaRunner");
3839
debuger.hideFlags = HideFlags.DontSave;
3940
DontDestroyOnLoad(debuger);
@@ -50,6 +51,7 @@ void Start()
5051

5152
private IEnumerator WaitLuaState()
5253
{
54+
//Debug.LogError("LuaRunner.WaitLuaState");
5355
if (luaType == LuaType.ToLua)
5456
{
5557
var args = new object[] { IntPtr.Zero };
@@ -62,7 +64,7 @@ private IEnumerator WaitLuaState()
6264
}
6365
else if (luaType == LuaType.XLua)
6466
{
65-
var args = new object[] {};
67+
var args = new object[] { };
6668
while (GetLuaState.Invoke(null, args) == null)
6769
yield return null;
6870

@@ -74,36 +76,51 @@ private IEnumerator WaitLuaState()
7476

7577
private void Update()
7678
{
79+
#if UNITY_EDITOR
7780
if (Input.GetKeyDown(KeyCode.R) && Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.LeftControl))
81+
#else
82+
if (Input.GetKeyDown(KeyCode.F8))
83+
#endif
7884
{
85+
#if UNITY_EDITOR
7986
if (Settings.AutoClearLog == ClearLogMode.Previous)
8087
ClearLog();
8188
var path = Settings.ScriptPath;
89+
#else
90+
var path = Path.Combine(Application.persistentDataPath, "_luarunner.lua");
91+
#endif
92+
93+
//Debug.LogError("LuaRunner.Update ");
8294
if (luaState != null && !string.IsNullOrEmpty(path) && File.Exists(path))
8395
{
96+
8497
var content = File.ReadAllText(path);
85-
switch(luaType)
98+
switch (luaType)
8699
{
87100
case LuaType.ToLua:
88101
DoString.Invoke(luaState, new object[] { content, null });
89102
break;
90103
case LuaType.XLua:
91104
DoString.Invoke(luaState, new object[] { content, null, null });
105+
Debug.LogError("LuaRunner.Update xlua");
92106
break;
93107
}
94108
}
95-
109+
#if UNITY_EDITOR
96110
if (Settings.AutoClearLog == ClearLogMode.All)
97111
ClearLog();
112+
#endif
98113
}
99114
}
100115

101116
private void ClearLog()
102117
{
103-
Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
118+
#if UNITY_EDITOR
119+
Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.SceneView));
104120
Type type = assembly.GetType("UnityEditor.LogEntries");
105121
MethodInfo method = type.GetMethod("Clear");
106122
method.Invoke(new object(), null);
123+
#endif
107124
}
108125

109126
private static bool IsToLua(out MethodInfo GetLuaState, out MethodInfo DoString)

0 commit comments

Comments
 (0)