// -- NamedJavaFunction methods @Override public int invoke(LuaState luaState) { // Find class final Class<?> clazz; if (luaState.isJavaObject(2, Class.class)) { clazz = luaState.checkJavaObject(2, Class.class); } else { String className = luaState.checkString(2); clazz = loadType(luaState, className); } // Get the object final Object object = luaState.checkJavaObject(1, clazz); // Push result luaState.pushJavaObject( new TypedJavaObject() { @Override public Object getObject() { return object; } @Override public Class<?> getType() { return clazz; } @Override public boolean isStrong() { return false; } }); return 1; }
/** prepares Lua state and some bookkeeping */ protected void prepareState() throws IOException { ui.debugMsg("Creating state...\n"); state = new LuaState(System.out); /*write("Registering base libs...\n"); BaseLib.register(state); MathLib.register(state); StringLib.register(state); CoroutineLib.register(state); OsLib.register(state);*/ ui.debugMsg("Building javafunc map...\n"); savegame.buildJavafuncMap(state.getEnvironment()); ui.debugMsg("Loading stdlib..."); InputStream stdlib = getClass().getResourceAsStream("/cz/matejcik/openwig/stdlib.lbc"); LuaClosure closure = LuaPrototype.loadByteCode(stdlib, state.getEnvironment()); ui.debugMsg("calling...\n"); state.call(closure, null, null, null); stdlib.close(); stdlib = null; ui.debugMsg("Registering WIG libs...\n"); WherigoLib.register(state); ui.debugMsg("Building event queue...\n"); eventRunner = new BackgroundRunner(true); eventRunner.setQueueListener( new Runnable() { public void run() { ui.refresh(); } }); }
public boolean next(LuaState vm, LValue key, boolean indexedonly) { while (super.next(vm, key, indexedonly)) { if (!vm.isnil(-1)) return true; vm.pop(1); key = vm.poplvalue(); } return false; }
// -- NamedJavaFunction methods @Override public int invoke(LuaState luaState) { Iterable<?> iterable = luaState.checkJavaObject(1, Iterable.class); luaState.pushJavaObject(new ElementIterator(iterable.iterator())); luaState.pushJavaObject(iterable); luaState.pushNil(); return 3; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { if (iterator.hasNext()) { luaState.pushJavaObject(iterator.next()); } else { luaState.pushNil(); } return 1; }
// -- NamedJavaFunction methods @Override public int invoke(LuaState luaState) { luaState.checkArg( 1, luaState.isJavaObjectRaw(1), String.format("expected Java object, got %s", luaState.typeName(1))); JavaFunction metamethod = luaState.getMetamethod(luaState.toJavaObjectRaw(1), Metamethod.JAVAPROPERTIES); return metamethod.invoke(luaState); }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { LuaMap luaMap = (LuaMap) luaState.toJavaObjectRaw(1); Object key = luaState.toJavaObject(2, Object.class); if (key == null) { throw new LuaRuntimeException( String.format("attempt to read map with %s accessor", luaState.typeName(2))); } luaState.pushJavaObject(luaMap.getMap().get(key)); return 1; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { LuaList luaList = (LuaList) luaState.toJavaObjectRaw(1); if (!luaState.isNumber(2)) { throw new LuaRuntimeException( String.format("attempt to read list with %s accessor", luaState.typeName(2))); } int index = luaState.toInteger(2); luaState.pushJavaObject(luaList.getList().get(index - 1)); return 1; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { LuaMap luaMap = (LuaMap) luaState.toJavaObjectRaw(1); Object key = luaState.toJavaObject(2, Object.class); if (key == null) { throw new LuaRuntimeException( String.format("attempt to write map with %s accessor", luaState.typeName(2))); } Object value = luaState.toJavaObject(3, Object.class); if (value != null) { luaMap.getMap().put(key, value); } else { luaMap.getMap().remove(key); } return 0; }
public static LPrototype undump(LuaState L, InputStream stream, String name) throws IOException { // check first byte to see if its a precompiled chunk int c = stream.read(); if (c != LUA_SIGNATURE[0]) { if (compiler != null) return compiler.compile(c, stream, name); throw new IllegalArgumentException("no compiler"); } // check rest of signature for (int i = 1; i < 4; i++) { if (stream.read() != LUA_SIGNATURE[i]) throw new IllegalArgumentException("bad signature"); } // load file as a compiled chunk String sname = getSourceName(name); LoadState s = new LoadState(L, stream, sname); s.loadHeader(); // check format switch (s.luacNumberFormat) { case DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES: case DumpState.NUMBER_FORMAT_INTS_ONLY: case DumpState.NUMBER_FORMAT_NUM_PATCH_INT32: break; default: L.error("unsupported int size"); } return s.loadFunction(LString.valueOf(sname)); }
/** Function called when a proxy object function is invoked. */ public Object invoke(Object proxy, Method method, Object[] args) throws LuaException { synchronized (obj.L) { String methodName = method.getName(); LuaObject func = obj.getField(methodName); if (func.isNil()) { return null; } Class<?> retType = method.getReturnType(); Object ret; // Checks if returned type is void. if it is returns null. if (retType.equals(Void.class) || retType.equals(void.class)) { func.call(args, 0); ret = null; } else { ret = func.call(args, 1)[0]; if (ret != null && ret instanceof Double) { ret = LuaState.convertLuaNumber((Double) ret, retType); } } return ret; } }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { // Get the object Object object = luaState.checkJavaObject(1, Object.class); // Find class Class<?> clazz; if (luaState.isJavaObject(2, Class.class)) { clazz = luaState.checkJavaObject(2, Class.class); } else { String className = luaState.checkString(2); clazz = loadType(luaState, className); } // Type check luaState.pushBoolean(clazz.isInstance(object)); return 1; }
/** Loads a type. The named type is a primitive type or a class. */ private static Class<?> loadType(LuaState luaState, String typeName) { Class<?> clazz; if ((clazz = PRIMITIVE_TYPES.get(typeName)) != null) { return clazz; } try { clazz = luaState.getClassLoader().loadClass(typeName); return clazz; } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }
public void run() { // perform the actual sync try { ui.blockForSaving(); savegame.store(state.getEnvironment()); } catch (IOException e) { log("STOR: save failed: " + e.toString(), LOG_WARN); ui.showError("Sync failed.\n" + e.getMessage()); } finally { ui.unblock(); } }
/** invokes creation of clean new game environment */ private void newGame() throws IOException { // starting game normally ui.debugMsg("Loading gwc..."); if (gwcfile == null) throw new IOException("invalid cartridge file"); ui.debugMsg("pre-setting properties..."); player.rawset("CompletionCode", gwcfile.code); player.rawset("Name", gwcfile.member); ui.debugMsg("loading code..."); byte[] lbc = gwcfile.getBytecode(); ui.debugMsg("parsing..."); LuaClosure closure = LuaPrototype.loadByteCode(new ByteArrayInputStream(lbc), state.getEnvironment()); ui.debugMsg("calling...\n"); state.call(closure, null, null, null); lbc = null; closure = null; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { LuaList luaList = (LuaList) luaState.toJavaObjectRaw(1); if (!luaState.isNumber(2)) { throw new LuaRuntimeException( String.format("attempt to write list with %s accessor", luaState.typeName(2))); } int index = luaState.toInteger(2); Object value = luaState.toJavaObject(3, Object.class); if (value != null) { int size = luaList.getList().size(); if (index - 1 != size) { luaList.getList().set(index - 1, value); } else { luaList.getList().add(value); } } else { luaList.getList().remove(index - 1); } return 0; }
// -- JavaFunction methods @SuppressWarnings("unchecked") @Override public int invoke(LuaState luaState) { if (luaState.isJavaObject(1, Map.class)) { Map<Object, Object> map = luaState.toJavaObject(1, Map.class); luaState.pushJavaObject(new LuaMap(map)); } else if (luaState.isJavaObject(1, List.class)) { List<Object> list = luaState.toJavaObject(1, List.class); luaState.pushJavaObject(new LuaList(list)); } else { luaState.checkArg( 1, false, String.format("expected map or list, got %s", luaState.typeName(1))); } return 1; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { // Check table luaState.checkType(1, LuaType.TABLE); // Get interfaces int interfaceCount = luaState.getTop() - 1; luaState.checkArg(2, interfaceCount > 0, "no interface specified"); Class<?>[] interfaces = new Class<?>[interfaceCount]; for (int i = 0; i < interfaceCount; i++) { if (luaState.isJavaObject(i + 2, Class.class)) { interfaces[i] = luaState.checkJavaObject(i + 2, Class.class); } else { String interfaceName = luaState.checkString(i + 2); interfaces[i] = loadType(luaState, interfaceName); } } // Create proxy luaState.pushJavaObjectRaw(luaState.getProxy(1, interfaces)); return 1; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { // Find class Class<?> clazz; if (luaState.isJavaObject(1, Class.class)) { clazz = luaState.checkJavaObject(1, Class.class); } else { String className = luaState.checkString(1); clazz = loadType(luaState, className); } // Instantiate Object object; int dimensionCount = luaState.getTop() - 1; switch (dimensionCount) { case 0: try { object = clazz.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } break; case 1: object = Array.newInstance(clazz, luaState.checkInteger(2)); break; default: int[] dimensions = new int[dimensionCount]; for (int i = 0; i < dimensionCount; i++) { dimensions[i] = luaState.checkInteger(i + 2); } object = Array.newInstance(clazz, dimensions); } // Return luaState.pushJavaObject(object); return 1; }
/** invokes game restore */ private void restoreGame() throws IOException { ui.debugMsg("Restoring saved state..."); cartridge = new Cartridge(); savegame.restore(state.getEnvironment()); }
@Override public void onCreate(Bundle savedInstanceState) { // System.loadLibrary("luajava-1.1"); // 設定橫向螢幕 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); this.getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.getWindow() .setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); L = LuaStateFactory.newLuaState(); L.pushJavaObject(this); L.setGlobal("activity"); res = getResources(); conf = res.getConfiguration(); conf.locale = Locale.TAIWAN; res.updateConfiguration(conf, null); super.onCreate(savedInstanceState); setContentView(R.layout.main); appobject = this.getApplicationContext(); surfaceView = (SurfaceView) findViewById(R.id.surfaceView1); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //////////////////////////////////////////////////////////////////////////////////////////// /*bitmap2 = BitmapFactory.decodeResource( getResources() , R.raw.line ); surfaceView2 = (SurfaceView)findViewById(R.id.surface_level ); surfaceHolder2 = surfaceView2.getHolder(); SurfaceHolder.Callback level_callback = new SurfaceHolder.Callback() { public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //需要 on_level_change(new float[3]); } public void surfaceCreated(SurfaceHolder holder) { } public void surfaceDestroyed(SurfaceHolder holder) { } }; surfaceHolder2.addCallback( level_callback ); surfaceHolder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); */ ////////////////////////////////////////////////////////////////////////////////////////// /* sensormanager = (SensorManager) getSystemService(SENSOR_SERVICE); sensors = sensormanager.getSensorList(Sensor.TYPE_ORIENTATION); SensorEventListener sl =new SensorEventListener () { public void onAccuracyChanged(Sensor sensor, int accuracy) { //不需要 } public void onSensorChanged(SensorEvent event) { on_level_change(event.values); } }; sensormanager.registerListener ( sl , sensors.get(0) , SensorManager.SENSOR_DELAY_GAME ); */ ///////////////////////////////////////////////////////////////////// Button run_script = (Button) this.findViewById(R.id.run_script); run_script.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (is_video == true) { print_screen(res.getString(R.string.video_rec_ing)); return; } if (is_audio == true) { print_screen(res.getString(R.string.audio_rec_ing)); return; } if (lua_file == "") { print_screen(res.getString(R.string.file_load_no)); return; } File f = new File("/sdcard/ez_Lua_Script_Camera/lua_scripts/" + lua_file); if (f.exists() == false) { print_screen(res.getString(R.string.file_load_fail)); return; } ((TextView) findViewById(R.id.lua_file_screen)).setText(lua_file); all_pics = 0; ((TextView) findViewById(R.id.shutter_times_screen)).setText("拍攝張數 : 00000"); (new lua_run()).start(); } }); Button about = (Button) this.findViewById(R.id.about); about.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (is_video == true) { print_screen(res.getString(R.string.video_rec_ing)); return; } if (is_audio == true) { print_screen(res.getString(R.string.audio_rec_ing)); return; } if (is_script_running == true) { print_screen(res.getString(R.string.script_running)); return; } Intent i = new Intent( Intent.ACTION_VIEW, Uri.parse("http://dl.dropbox.com/u/61164954/homepage/ez_LSC/index.htm")); startActivity(i); } }); Button audio_c = (Button) this.findViewById(R.id.audio); audio_c.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (is_script_running == true) { print_screen(res.getString(R.string.script_running)); return; } if (is_video == true) { print_screen(res.getString(R.string.video_rec_ing)); return; } if (is_audio == false) { print_screen(res.getString(R.string.audio_rec_start)); CharSequence cs = res.getString(R.string.stop); Button keepa = (Button) findViewById(R.id.audio); keepa.setText(cs); is_audio = true; (new audio()).start(); } else { arecorder.stop(); is_audio = false; print_screen(res.getString(R.string.audio_rec_end)); CharSequence xcs = res.getString(R.string.audio_rec); Button keepax = (Button) findViewById(R.id.audio); keepax.setText(xcs); camera.startPreview(); } } }); Button movie = (Button) this.findViewById(R.id.movie); movie.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (is_script_running == true) { print_screen(res.getString(R.string.script_running)); return; } if (is_audio == true) { print_screen(res.getString(R.string.audio_rec_ing)); return; } if (is_video == false) { print_screen(res.getString(R.string.video_rec_start)); CharSequence cs = res.getString(R.string.stop); Button keepa = (Button) findViewById(R.id.movie); keepa.setText(cs); parameters.setFocusMode("continuous-video"); camera.setParameters(parameters); is_video = true; (new video()).start(); } else { recorder.stop(); is_video = false; print_screen(res.getString(R.string.video_rec_end)); CharSequence xcs = res.getString(R.string.video_rec); Button keepax = (Button) findViewById(R.id.movie); keepax.setText(xcs); camera.lock(); camera.release(); camera = Camera.open(); parameters = camera.getParameters(); parameters.setPreviewSize(640, 480); parameters.setPreviewFrameRate(30); parameters.setJpegQuality(100); parameters.setFocusMode("continuous-video"); parameters.setPictureSize(2048, 1536); camera.setParameters(parameters); try { camera.setPreviewDisplay(surfaceHolder); } catch (IOException e) { } camera.startPreview(); } } }); Button lua = (Button) this.findViewById(R.id.lua); lua.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (is_script_running == true) { print_screen(res.getString(R.string.script_running)); return; } if (is_audio == true) { print_screen(res.getString(R.string.audio_rec_ing)); return; } if (is_video == true) { print_screen(res.getString(R.string.video_rec_ing)); return; } show_choose_Dialog(); } }); test_caller(); load_config(); try { player.setDataSource("/sdcard/ez_Lua_Script_Camera/sound/shuttersound.ogg"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { shutter_trigger.setDataSource("/sdcard/ez_Lua_Script_Camera/sound/trigger.wav"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Opens this module in a Lua state. The method is invoked by {@link LuaState#openLibs()} or by * {@link LuaState#openLib(li.cil.repack.com.naef.jnlua.LuaState.Library)} if {@link * LuaState.Library#JAVA} is passed. The module is pushed onto the stack. * * @param luaState the Lua state to open in */ public void open(LuaState luaState) { luaState.register("java", functions, true); }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { // Check arguments String className = luaState.checkString(1); boolean doImport = luaState.toBoolean(2); // Load Class<?> clazz = loadType(luaState, className); luaState.pushJavaObject(clazz); // Import if (doImport) { luaState.rawGet(LuaState.REGISTRYINDEX, LuaState.RIDX_GLOBALS); String name = clazz.getName(); int index = name.indexOf('.'); while (index >= 0) { String part = name.substring(0, index); luaState.getField(-1, part); if (!luaState.isTable(-1)) { luaState.pop(1); luaState.newTable(); luaState.pushValue(-1); luaState.setField(-3, part); } luaState.remove(-2); name = name.substring(index + 1); index = name.indexOf('.'); } luaState.pushValue(-2); luaState.setField(-2, name); luaState.pop(1); } luaState.pushBoolean(doImport); // Return return 2; }
// -- JavaFunction methods @Override public int invoke(LuaState luaState) { LuaList luaList = (LuaList) luaState.toJavaObjectRaw(1); luaState.pushInteger(luaList.getList().size()); return 1; }