Add sample applet build file.

This commit is contained in:
James Roseborough
2014-01-14 07:15:26 +00:00
parent 1b6be3c99f
commit 1bc4acdb89
3 changed files with 157 additions and 3 deletions

116
build-applet.xml Normal file
View File

@@ -0,0 +1,116 @@
<?xml version="1.0"?>
<project name="sample" default="main" basedir=".">
<property file="version.properties"/>
<!-- find libs -->
<import file="build-libs.xml"/>
<!-- main java class -->
<property name="java.dir" value="examples/jse"/>
<property name="java.name" value="SampleApplet"/>
<!-- main script -->
<property name="script.dir" value="examples/lua"/>
<property name="script.name" value="swingapplet"/>
<property name="image.name" value="logo.gif"/>
<!-- location of luaj jar -->
<property name="libs.dir" value="lib"/>
<property name="luaj.jar" value="${libs.dir}/luaj-jse-${version}.jar"/>
<!-- location of build results -->
<property name="build.dir" value="build/applet"/>
<target name="clean">
<delete failonerror="false" dir="${build.dir}"/>
</target>
<target name="dirs">
<mkdir dir="build/applet/classes"/>
</target>
<target name="classes" depends="dirs">
<copy todir="${build.dir}">
<fileset dir="${script.dir}" includes="${script.name}.lua,${image.name}"/>
</copy>
<javac destdir="${build.dir}/classes" source="1.4" target="1.4"
classpath="${luaj.jar}" srcdir="${java.dir}" includes="${java.name}.java"/>
</target>
<target name="manifest" depends="dirs">
<manifest file="${build.dir}/MANIFEST.MF">
<attribute name="Permissions" value="sandbox"/>
<attribute name="Main-class" value="${java.name}"/>
</manifest>
</target>
<target name="jar" depends="classes,manifest">
<jar destfile="${build.dir}/${script.name}.jar"
manifest="${build.dir}/MANIFEST.MF">
<fileset dir="${build.dir}" includes="*.lua"/>
<fileset dir="${build.dir}/classes"/>
<zipfileset
src="${luaj.jar}"
excludes="**/script/*,**/luajc/**,**/parser/**,**/ast/**,lua*"/>
</jar>
</target>
<target name="obf" depends="jar,proguard-lib">
<taskdef resource="proguard/ant/task.properties" classpath="lib/proguard.jar" />
<copy file="${build.dir}/${script.name}.jar"
tofile="${build.dir}/${script.name}-unobfuscated.jar"/>
<proguard>
-injars ${build.dir}/${script.name}-unobfuscated.jar
-outjars ${build.dir}/${script.name}.jar
-libraryjars ${java.home}/lib/rt.jar
-overloadaggressively
-repackageclasses ''
-allowaccessmodification
-printmapping ${build.dir}/mapping.txt
-keep public class * extends java.applet.Applet
-target 1.4
</proguard>
</target>
<target name="sign" depends="jar">
<signjar jar="${build.dir}/${script.name}.jar"
alias="${sign.alias}"
storepass="${sign.storepass}"
keypass="${sign.keypass}"
keystore="${sign.keystore}" />
</target>
<target name="html" depends="dirs">
<echoxml file="build/applet/LuajSampleApplet.html">
<html>
<head><title>Luaj Sample Applet</title></head>
<body>
<h1>Luaj Sample Applet</h1>
Requires browser that supports applets.
${script.name}
<applet archive='${script.name}.jar'
code='${java.name}.class'
width='640'
height='480' >
<param name='luaj.script' value='${script.name}.lua'/>
<param name="java_version" value="1.4+"/>
</applet>
</body>
</html>
</echoxml>
</target>
<target name="run" depends="jar,html">
<exec executable="open" spawn="true">
<arg value="-a Firefox"/>
<arg path="build/applet/LuajSampleApplet.html"/>
</exec>
</target>
<target name="all" depends="clean,sign,html,run"/>
<target name="main" depends="sign,html"/>
</project>

View File

@@ -1,8 +1,11 @@
import java.applet.Applet;
import java.awt.Graphics;
import java.io.InputStream;
import java.net.URL;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ResourceFinder;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -35,7 +38,7 @@ import org.luaj.vm2.lib.jse.JsePlatform;
* @see LuaValue
* @see Applet
*/
public class SampleApplet extends Applet {
public class SampleApplet extends Applet implements ResourceFinder {
private static final long serialVersionUID = 1L;
Globals globals;
@@ -57,7 +60,12 @@ public class SampleApplet extends Applet {
System.out.println("Loading " + script);
// Construct globals to use.
globals = JsePlatform.debugGlobals();
globals = JsePlatform.standardGlobals();
// Use custom resource finder.
globals.FINDER = this;
// Look up and save the handy pcall method.
pcall = globals.get("pcall");
// Load and execute the script, supplying this Applet as the only
@@ -94,4 +102,29 @@ public class SampleApplet extends Applet {
coerced == g ? graphics : (graphics = CoerceJavaToLua
.coerce(coerced = g)));
}
// ResourceFinder implementation.
public InputStream findResource(String filename) {
InputStream stream = findAsResource(filename);
if (stream != null) return stream;
stream = findAsDocument(filename);
if (stream != null) return stream;
System.err.println("Failed to find resource " + filename);
return null;
}
private InputStream findAsResource(String filename) {
System.out.println("Looking for jar resource /"+filename);
return getClass().getResourceAsStream("/"+filename);
}
private InputStream findAsDocument(String filename) {
try {
final URL base = getDocumentBase();
System.out.println("Looking in document base "+base);
return new URL(base, filename).openStream();
} catch (Exception e) {
return null;
}
}
}

View File

@@ -81,8 +81,13 @@ public class PackageLib extends TwoArgFunction {
/** The default value to use for package.path. This can be set with the system property
* <code>"luaj.package.path"</code>, and is <code>"?.lua"</code> by default. */
public static String DEFAULT_LUA_PATH = System.getProperty("luaj.package.path");
public static String DEFAULT_LUA_PATH;
static {
try {
DEFAULT_LUA_PATH = System.getProperty("luaj.package.path");
} catch (Exception e) {
System.out.println(e.toString());
}
if (DEFAULT_LUA_PATH == null)
DEFAULT_LUA_PATH = "?.lua";
}