Fix some save/movie handling bugs in Lua
[jpcrr.git] / exceptiondefs / ExceptionDefProcessor.java
bloba72954883bbc018cb48a9b48207eb639da65bec3
1 package exceptiondefs;
3 import java.util.*;
4 import java.io.*;
5 import java.nio.*;
6 import java.nio.charset.*;
8 class ExceptionDefProcessor
10 private static Map<Class<?>, String> classes;
12 static class UTFStream
14 FileOutputStream stream;
16 UTFStream(String name) throws IOException
18 stream = new FileOutputStream(name);
21 void println(String str)
23 try {
24 ByteBuffer buf;
25 buf = Charset.forName("UTF-8").newEncoder().encode(CharBuffer.wrap(str));
26 byte[] buf2 = new byte[buf.remaining() + 1];
27 buf.get(buf2, 0, buf.remaining());
28 buf2[buf2.length - 1] = 10;
29 stream.write(buf2);
30 } catch(Exception e) {
31 e.printStackTrace();
35 void close()
37 try {
38 stream.close();
39 } catch(Exception e) {
40 e.printStackTrace();
45 private static char identity(char x)
47 return x;
50 private static String deriveLogFormat(String x)
52 //This shouldn't be interpretted as keyword!
53 String dollar = "$";
54 return x.replaceFirst("\\" + dollar + "Format:([^" + dollar + "]*)\\" + dollar + ".*", "--pretty=format:$1");
57 private static String getRevision() throws IOException
59 String x = "$Format:%h by %cn on %ci$";
60 if(identity(x.charAt(0)) != '$') {
61 System.err.println("Detected revision: " + x + ".");
62 return x;
64 ProcessBuilder gitproc = new ProcessBuilder();
65 gitproc.command("git", "log", deriveLogFormat(x), "-1");
66 Process git = gitproc.start();
67 InputStream output = git.getInputStream();
68 while(true) {
69 try {
70 if(git.waitFor() != 0)
71 throw new IOException("Git subprocess failed");
72 break;
73 } catch(InterruptedException e) {
76 BufferedReader r = new BufferedReader(new InputStreamReader(output));
77 x = r.readLine();
78 r.close();
80 System.err.println("Detected revision: " + x + ".");
81 return x;
84 private static String getRelease() throws IOException
86 BufferedReader kbd2 = new BufferedReader(new InputStreamReader(
87 new FileInputStream("VERSIONINFO"), "UTF-8"));
88 String cmd = kbd2.readLine();
89 kbd2.close();
90 System.err.println("Detected release: " + cmd + ".");
91 return cmd;
94 private static String escapeString(String s)
96 StringBuffer r = new StringBuffer();;
97 for(int i = 0; i < s.length(); i++) {
98 char x = s.charAt(i);
99 if(x == '\"')
100 r.append("\"");
101 else if(x == '\\')
102 r.append("\\");
103 else
104 r.append(x);
106 return r.toString();
109 private static void doClass(String line)
111 int split = line.indexOf(32);
112 String clazz = line.substring(0, split);
113 String desc = line.substring(split + 1);
114 Class<?> classObject;
116 try {
117 classObject = Class.forName(clazz);
118 } catch(Exception e) {
119 System.err.println("Warning: Can't find class \"" + clazz + "\", dropping.");
120 return;
122 classes.put(classObject, desc);
125 public static void main(String[] args)
127 classes = new HashMap<Class<?>, String>();
129 if(args == null || args.length < 1) {
130 System.err.println("Syntax: java ExceptionDefProcessor <inputfile>");
131 System.exit(1);
134 String autoexec = args[0];
135 try {
136 BufferedReader kbd2 = new BufferedReader(new InputStreamReader(
137 new FileInputStream(autoexec), "UTF-8"));
138 while(true) {
139 String cmd = kbd2.readLine();
140 if(cmd == null)
141 break;
142 if(!cmd.equals(""))
143 doClass(cmd);
145 } catch (Exception e) {
146 System.err.println("Failed to load exception defintions: " + e.getMessage());
149 Class<?> failingClass = null;
150 do {
151 if(failingClass != null)
152 classes.put(failingClass, failingClass.getName());
153 failingClass = null;
154 for(Map.Entry<Class<?>, String> x : classes.entrySet()) {
155 Class<?> superclass = x.getKey().getSuperclass();
156 if(x.getKey().getName().equals("java.lang.Error") ||
157 x.getKey().getName().equals("java.lang.RuntimeException"))
158 continue;
159 if(!classes.containsKey(superclass)) {
160 System.err.println("Warning: Missing superclass \"" + superclass.getName() + "\" for \"" +
161 x.getKey().getName() + "\".");
162 failingClass = superclass;
163 break;
166 } while(failingClass != null);
168 UTFStream stream = null;
169 try {
170 stream = new UTFStream("org/jpc/Exceptions.java");
171 } catch(Exception e) {
172 System.err.println("Can't open org/jpc/Exceptions.java: " + e.getMessage());
173 System.exit(1);
176 stream.println("package org.jpc;");
177 stream.println("import java.util.*;");
178 stream.println("class Exceptions {");
179 stream.println("public static Map<String,String> classes;");
180 stream.println("static {");
181 stream.println("classes = new HashMap<String,String>();");
182 Class<?> out = null;
183 String desc = null;
184 do {
185 if(out != null) {
186 classes.remove(out);
187 stream.println("classes.put(\"" + out.getName() + "\", \"" + desc + "\");");
189 out = null;
190 for(Map.Entry<Class<?>, String> x : classes.entrySet()) {
191 Class<?> superclass = x.getKey().getSuperclass();
192 if(!classes.containsKey(superclass)) {
193 out = x.getKey();
194 desc = x.getValue();
195 break;
198 } while(out != null);
199 stream.println("}}");
200 stream.close();
202 try {
203 stream = new UTFStream("org/jpc/Revision.java");
204 } catch(Exception e) {
205 System.err.println("Can't open org/jpc/Revision.java: " + e.getMessage());
206 System.exit(1);
208 stream.println("package org.jpc;");
209 stream.println("public class Revision {");
210 stream.println("public static String getRevision() {");
211 try {
212 stream.println("return \"" + escapeString(getRevision()) + "\";");
213 stream.println("}\npublic static String getRelease() {");
214 stream.println("return \"" + escapeString(getRelease()) + "\";");
215 } catch(Exception e) {
216 System.err.println("Can't get revision: " + e.getMessage());
217 System.exit(1);
219 stream.println("}}");
220 stream.close();