From: Rene S. <sa...@us...> - 2004-08-20 21:30:08
|
Update of /cvsroot/jake2/jake2/src/jake2/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1597/src/jake2/util Modified Files: Lib.java Added Files: QuakeFile.java Log Message: savegames now seem to work. Index: Lib.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/util/Lib.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Lib.java 23 Jul 2004 10:09:55 -0000 1.5 --- Lib.java 20 Aug 2004 21:29:57 -0000 1.6 *************** *** 35,40 **** import net.java.games.jogl.util.BufferUtils; ! public class Lib { ! /* --- 35,40 ---- import net.java.games.jogl.util.BufferUtils; ! public class Lib ! { /* *************** *** 46,90 **** ============= */ ! public static String vtos(float[] v) { return (int) v[0] + " " + (int) v[1] + " " + (int) v[2]; } ! public static String vtofs(float[] v) { return v[0] + " " + v[1] + " " + v[2]; } ! ! public static String vtofsbeaty(float[] v) { return Com.sprintf("%8.2f %8.2f %8.2f", new Vargs().add(v[0]).add(v[1]).add(v[2])); } ! public static short rand() { //return (short) (Math.random() * 0x8000); ! return (short)Globals.rnd.nextInt(Short.MAX_VALUE+1); } ! public static float crandom() { return (Globals.rnd.nextFloat() - 0.5f) * 2.0f; //return (float) (Math.random() - 0.5) * 2.0f; } ! public static float random() { return Globals.rnd.nextFloat(); } ! public static float crand() { return (Globals.rnd.nextFloat() - 0.5f) * 2.0f; } ! public static int strcmp(String in1, String in2) { return in1.compareTo(in2); } public static boolean strstr(String i1, String i2) { return (i1.indexOf(i2) != -1); } ! public static float atof(String in) { ! float res = 0; ! try { ! res = Float.parseFloat(in); } ! catch (Exception e) { } --- 46,110 ---- ============= */ ! public static String vtos(float[] v) ! { return (int) v[0] + " " + (int) v[1] + " " + (int) v[2]; } ! public static String vtofs(float[] v) ! { return v[0] + " " + v[1] + " " + v[2]; } ! ! public static String vtofsbeaty(float[] v) ! { return Com.sprintf("%8.2f %8.2f %8.2f", new Vargs().add(v[0]).add(v[1]).add(v[2])); } ! public static short rand() ! { //return (short) (Math.random() * 0x8000); ! return (short) Globals.rnd.nextInt(Short.MAX_VALUE + 1); } ! public static float crandom() ! { return (Globals.rnd.nextFloat() - 0.5f) * 2.0f; //return (float) (Math.random() - 0.5) * 2.0f; } ! public static float random() ! { return Globals.rnd.nextFloat(); } ! public static float crand() ! { return (Globals.rnd.nextFloat() - 0.5f) * 2.0f; } ! public static int strcmp(String in1, String in2) ! { return in1.compareTo(in2); } + //<<<<<<< Lib.java + // public static int stricmp(String in1, String in2) + // { + // return in1.compareToIgnoreCase(in2); + // } + // + // public static boolean strstr(String i1, String i2) + // { + //======= public static boolean strstr(String i1, String i2) { return (i1.indexOf(i2) != -1); } ! public static float atof(String in) ! { ! float res= 0; ! try ! { ! res= Float.parseFloat(in); } ! catch (Exception e) ! { } *************** *** 92,123 **** } ! public static int Q_stricmp(String in1, String in2) { return in1.compareToIgnoreCase(in2); } - // ================================================================================= ! public static int atoi(String in) { ! try { return Integer.parseInt(in); } ! catch (Exception e) { return 0; } } ! public static float[] atov(String v) { ! float[] res = { 0, 0, 0 }; ! int i1 = v.indexOf(" "); ! int i2 = v.indexOf(" ", i1 + 1); ! res[0] = atof(v.substring(0, i1)); ! res[1] = atof(v.substring(i1 + 1, i2)); ! res[2] = atof(v.substring(i2 + 1, v.length())); return res; } public static int strlen(char in[]) { for (int i = 0; i < in.length; i++) --- 112,151 ---- } ! // TRASH! ! public static int Q_stricmp(String in1, String in2) ! { return in1.compareToIgnoreCase(in2); } ! /** Converts a string to an int. */ ! public static int atoi(String in) ! { ! try ! { return Integer.parseInt(in); } ! catch (Exception e) ! { return 0; } } ! /** Converts a string to a vector. */ ! public static float[] atov(String v) ! { ! float[] res= { 0, 0, 0 }; ! int i1= v.indexOf(" "); ! int i2= v.indexOf(" ", i1 + 1); ! res[0]= atof(v.substring(0, i1)); ! res[1]= atof(v.substring(i1 + 1, i2)); ! res[2]= atof(v.substring(i2 + 1, v.length())); return res; } + public static int strlen(char in[]) { for (int i = 0; i < in.length; i++) *************** *** 127,132 **** } ! public static int strlen(byte in[]) { ! for (int i = 0; i < in.length; i++) if (in[i] == 0) return i; --- 155,161 ---- } ! public static int strlen(byte in[]) ! { ! for (int i= 0; i < in.length; i++) if (in[i] == 0) return i; *************** *** 134,148 **** } static byte[] buffer = new byte[Defines.MAX_INFO_STRING]; public static String readString(ByteBuffer bb, int len) { bb.get(buffer, 0, len); return new String(buffer, 0, len); } ! public static String hexdumpfile(ByteBuffer bb, int len) throws IOException { ! ByteBuffer bb1 = bb.slice(); ! byte buf[] = new byte[len]; bb1.get(buf); --- 163,204 ---- } + //<<<<<<< Lib.java + // public static void strcat(String in, String i) + // { + // in += i; + // } + // + // public static void strcpy(byte dest[], byte src[]) + // { + // for (int i= 0; i < dest.length && i < src.length; i++) + // if (src[i] == 0) + // { + // dest[i]= 0; + // return; + // } + // else + // dest[i]= src[i]; + // + // } + // + // static byte[] buffer= new byte[Defines.MAX_INFO_STRING]; + // + // public static String readString(ByteBuffer bb, int len) + // { + //======= static byte[] buffer = new byte[Defines.MAX_INFO_STRING]; public static String readString(ByteBuffer bb, int len) { + bb.get(buffer, 0, len); return new String(buffer, 0, len); } ! /** Returns a string containing a hexdump of a bytebuffer content.*/ ! public static String hexdumpfile(ByteBuffer bb, int len) throws IOException ! { ! ByteBuffer bb1= bb.slice(); ! byte buf[]= new byte[len]; bb1.get(buf); *************** *** 151,168 **** } ! // dump data as hexstring ! public static String hexDump(byte data1[], int len, boolean showAddress) { ! StringBuffer result = new StringBuffer(); ! StringBuffer charfield = new StringBuffer(); ! int i = 0; ! while (i < len) { ! if ((i & 0xf) == 0) { ! if (showAddress) { ! String address = Integer.toHexString(i); ! address = ("0000".substring(0, 4 - address.length()) + address).toUpperCase(); result.append(address + ": "); } } ! int v = data1[i]; result.append(hex2(v)); --- 207,228 ---- } ! /** Returns a string containing a hexdump of a byte array.*/ ! public static String hexDump(byte data1[], int len, boolean showAddress) ! { ! StringBuffer result= new StringBuffer(); ! StringBuffer charfield= new StringBuffer(); ! int i= 0; ! while (i < len) ! { ! if ((i & 0xf) == 0) ! { ! if (showAddress) ! { ! String address= Integer.toHexString(i); ! address= ("0000".substring(0, 4 - address.length()) + address).toUpperCase(); result.append(address + ": "); } } ! int v= data1[i]; result.append(hex2(v)); *************** *** 173,177 **** // nach dem letzten, newline einfuegen ! if ((i & 0xf) == 0) { result.append(charfield); result.append("\n"); --- 233,238 ---- // nach dem letzten, newline einfuegen ! if ((i & 0xf) == 0) ! { result.append(charfield); result.append("\n"); *************** *** 179,183 **** } // in der Mitte ein Luecke einfuegen ? ! else if ((i & 0xf) == 8) { result.append(" "); } --- 240,245 ---- } // in der Mitte ein Luecke einfuegen ? ! else if ((i & 0xf) == 8) ! { result.append(" "); } *************** *** 186,196 **** } ! //formats an hex byte ! public static String hex2(int i) { ! String val = Integer.toHexString(i & 0xff); return ("00".substring(0, 2 - val.length()) + val).toUpperCase(); } ! public static char readableChar(int i) { if ((i < 0x20) || (i > 0x7f)) return '.'; --- 248,261 ---- } ! /** Formats a byte into 2 digit hexcode.*/ ! public static String hex2(int i) ! { ! String val= Integer.toHexString(i & 0xff); return ("00".substring(0, 2 - val.length()) + val).toUpperCase(); } ! /** Converts a char into a char suitable for a hexdump. */ ! public static char readableChar(int i) ! { if ((i < 0x20) || (i > 0x7f)) return '.'; *************** *** 199,215 **** } ! public static void printv(String in, float arr[]) { ! for (int n = 0; n < arr.length; n++) { Com.Println(in + "[" + n + "]: " + arr[n]); } } ! static final byte nullfiller[] = new byte[8192]; ! public static void fwriteString(String s, int len, RandomAccessFile f) throws IOException { ! if (s == null) return; ! int diff = len - s.length(); ! if (diff > 0) { f.write(s.getBytes()); --- 264,286 ---- } ! /** Prints a vector to the quake console. */ ! public static void printv(String in, float arr[]) ! { ! for (int n= 0; n < arr.length; n++) ! { Com.Println(in + "[" + n + "]: " + arr[n]); } } ! static final byte nullfiller[]= new byte[8192]; ! /** Trash !*/ ! public static void fwriteString1(String s, int len, RandomAccessFile f) throws IOException ! { ! if (s == null) return; ! int diff= len - s.length(); ! if (diff > 0) ! { f.write(s.getBytes()); *************** *** 220,228 **** } public static RandomAccessFile fopen(String name, String mode) { try { return new RandomAccessFile(name, mode); } ! catch (Exception e) { Com.DPrintf("Could not open file:" + name); return null; --- 291,317 ---- } + //<<<<<<< Lib.java + // /** Trash !*/ + // public static String cut(String in, char c) + // { + // int pos= in.indexOf(c); + // + // if (pos != -1) + // return in.substring(0, pos); + // return in; + // } + // + // /** Trash !*/ + // public static RandomAccessFile fopen(String name, String mode) + // { + // try + // { + //======= public static RandomAccessFile fopen(String name, String mode) { try { return new RandomAccessFile(name, mode); } ! catch (Exception e) ! { Com.DPrintf("Could not open file:" + name); return null; *************** *** 230,250 **** } ! public static void fclose(RandomAccessFile f) { ! try { f.close(); } ! catch (Exception e) { } } ! public static String freadString(RandomAccessFile f, int len) { ! byte buffer[] = new byte[len]; ! FS.Read(buffer, len, f); ! ! return new String(buffer).trim(); ! } ! ! public static String rightFrom(String in, char c) { ! int pos = in.lastIndexOf(c); if (pos == -1) return ""; --- 319,337 ---- } ! /** Trash !*/ ! public static void fclose(RandomAccessFile f) ! { ! try ! { f.close(); } ! catch (Exception e) ! { } } ! public static String rightFrom(String in, char c) ! { ! int pos= in.lastIndexOf(c); if (pos == -1) return ""; *************** *** 254,259 **** } ! public static String leftFrom(String in, char c) { ! int pos = in.lastIndexOf(c); if (pos == -1) return ""; --- 341,347 ---- } ! public static String leftFrom(String in, char c) ! { ! int pos= in.lastIndexOf(c); if (pos == -1) return ""; *************** *** 263,279 **** } ! public static String[] linesplit(String in) { ! ! StringTokenizer tk = new StringTokenizer(in, "\r\n"); ! int count = tk.countTokens(); if (count == 0) return new String[] { }; ! String result[] = new String[count]; ! for (int i = 0; tk.hasMoreTokens(); i++) { ! result[i] = tk.nextToken(); } --- 351,369 ---- } ! /** Splits a line at newline characters and returns a string array. */ ! public static String[] linesplit(String in) ! { ! StringTokenizer tk= new StringTokenizer(in, "\r\n"); ! int count= tk.countTokens(); if (count == 0) return new String[] { }; ! String result[]= new String[count]; ! for (int i= 0; tk.hasMoreTokens(); i++) ! { ! result[i]= tk.nextToken(); } *************** *** 281,311 **** } ! public static int rename(String oldn, String newn) { ! try { ! File f1 = new File(oldn); ! File f2 = new File(newn); f1.renameTo(f2); return 0; } ! catch (Exception e) { return 1; } } ! public static byte[] getIntBytes(int c) { ! byte b[] = new byte[4]; ! b[0] = (byte) ((c & 0xff)); ! b[1] = (byte) ((c >>> 8) & 0xff); ! b[2] = (byte) ((c >>> 16) & 0xff); ! b[3] = (byte) ((c >>> 24) & 0xff); return b; } ! public static int getInt(byte b[]) { return (b[0] & 0xff) | ((b[1] & 0xff) << 8) | ((b[2] & 0xff) << 16) | ((b[3] & 0xff) << 24); } ! ! public static float[] clone(float in[]) { ! float out[] = new float[in.length]; if (in.length != 0) --- 371,408 ---- } ! /** Just renames a file. */ ! public static int rename(String oldn, String newn) ! { ! try ! { ! File f1= new File(oldn); ! File f2= new File(newn); f1.renameTo(f2); return 0; } ! catch (Exception e) ! { return 1; } } ! public static byte[] getIntBytes(int c) ! { ! byte b[]= new byte[4]; ! b[0]= (byte) ((c & 0xff)); ! b[1]= (byte) ((c >>> 8) & 0xff); ! b[2]= (byte) ((c >>> 16) & 0xff); ! b[3]= (byte) ((c >>> 24) & 0xff); return b; } ! public static int getInt(byte b[]) ! { return (b[0] & 0xff) | ((b[1] & 0xff) << 8) | ((b[2] & 0xff) << 16) | ((b[3] & 0xff) << 24); } ! ! public static float[] clone(float in[]) ! { ! float out[]= new float[in.length]; if (in.length != 0) *************** *** 315,355 **** } ! /* ! * java.nio.* Buffer util functions ! */ ! ! public static final int SIZEOF_FLOAT = BufferUtils.SIZEOF_FLOAT; ! public static final int SIZEOF_INT = BufferUtils.SIZEOF_INT; ! public static FloatBuffer newFloatBuffer(int numElements) { ! ByteBuffer bb = newByteBuffer(numElements * SIZEOF_FLOAT); ! return bb.asFloatBuffer(); } ! public static FloatBuffer newFloatBuffer(int numElements, ByteOrder order) { ! ByteBuffer bb = newByteBuffer(numElements * SIZEOF_FLOAT, order); ! return bb.asFloatBuffer(); } ! public static IntBuffer newIntBuffer(int numElements) { ! ByteBuffer bb = newByteBuffer(numElements * SIZEOF_INT); ! return bb.asIntBuffer(); } ! public static IntBuffer newIntBuffer(int numElements, ByteOrder order) { ! ByteBuffer bb = newByteBuffer(numElements * SIZEOF_INT, order); ! return bb.asIntBuffer(); } ! public static ByteBuffer newByteBuffer(int numElements) { ! ByteBuffer bb = ByteBuffer.allocateDirect(numElements); ! bb.order(ByteOrder.nativeOrder()); ! return bb; } ! public static ByteBuffer newByteBuffer(int numElements, ByteOrder order) { ! ByteBuffer bb = ByteBuffer.allocateDirect(numElements); ! bb.order(order); ! return bb; } --- 412,460 ---- } ! private static final int SIZEOF_FLOAT= BufferUtils.SIZEOF_FLOAT; ! private static final int SIZEOF_INT= BufferUtils.SIZEOF_INT; ! /** java.nio.* Buffer util function. */ ! public static FloatBuffer newFloatBuffer(int numElements) ! { ! ByteBuffer bb= newByteBuffer(numElements * SIZEOF_FLOAT); ! return bb.asFloatBuffer(); } ! /** java.nio.* Buffer util function. */ ! public static FloatBuffer newFloatBuffer(int numElements, ByteOrder order) ! { ! ByteBuffer bb= newByteBuffer(numElements * SIZEOF_FLOAT, order); ! return bb.asFloatBuffer(); } ! /** java.nio.* Buffer util function. */ ! public static IntBuffer newIntBuffer(int numElements) ! { ! ByteBuffer bb= newByteBuffer(numElements * SIZEOF_INT); ! return bb.asIntBuffer(); } ! /** java.nio.* Buffer util function. */ ! public static IntBuffer newIntBuffer(int numElements, ByteOrder order) ! { ! ByteBuffer bb= newByteBuffer(numElements * SIZEOF_INT, order); ! return bb.asIntBuffer(); } ! /** java.nio.* Buffer util function. */ ! public static ByteBuffer newByteBuffer(int numElements) ! { ! ByteBuffer bb= ByteBuffer.allocateDirect(numElements); ! bb.order(ByteOrder.nativeOrder()); ! return bb; } ! /** java.nio.* Buffer util function. */ ! public static ByteBuffer newByteBuffer(int numElements, ByteOrder order) ! { ! ByteBuffer bb= ByteBuffer.allocateDirect(numElements); ! bb.order(order); ! return bb; } --- NEW FILE: QuakeFile.java --- /* Copyright (C) 1997-2001 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // Created on 24.07.2004 by RST. // $Id: QuakeFile.java,v 1.1 2004/08/20 21:29:57 salomo Exp $ package jake2.util; import jake2.game.Game; import jake2.game.SuperAdapter; import jake2.game.edict_t; import jake2.game.gitem_t; import java.io.*; /** RandomAccessFile, bur handles readString/WriteString specially and * offers other helper functions */ public class QuakeFile extends RandomAccessFile { /** Standard Constructor.*/ public QuakeFile(String filename, String mode) throws FileNotFoundException { super(filename, mode); } /** Writes a Vector to a RandomAccessFile. */ public void writeVector(float v[]) throws IOException { for (int n= 0; n < 3; n++) writeFloat(v[n]); } /** Writes a Vector to a RandomAccessFile. */ public float[] readVector() throws IOException { float res[]= { 0, 0, 0 }; for (int n= 0; n < 3; n++) res[n]= readFloat(); return res; } /** Reads a length specified string from a file. */ public String readString() throws IOException { int len= readInt(); if (len == -1) return null; if (len == 0) return ""; byte bb[]= new byte[len]; super.read(bb, 0, len); return new String(bb, 0, len); } /** Writes a length specified string to a file. */ public void writeString(String s) throws IOException { if (s == null) { writeInt(-1); return; } writeInt(s.length()); if (s.length() != 0) writeBytes(s); } /** Writes the edict reference. */ public void writeEdictRef(edict_t ent) throws IOException { if (ent == null) writeInt(-1); else { writeInt(ent.s.number); } } /** * Reads an edict index from a file and returns the edict. */ public edict_t readEdictRef() throws IOException { int i= readInt(); // handle -1 if (i < 0) return null; if (i > Game.g_edicts.length) { System.err.println("jake2: illegal edict num:" + i); return null; } // valid edict. return Game.g_edicts[i]; } /** Writes the Adapter-ID to the file. */ public void writeAdapter(SuperAdapter a) throws IOException { writeInt(3988); if (a == null) writeString(null); else { String str= a.getID(); if (a == null) { System.err.println("writeAdapter: invalid Adapter id for" + a); } System.out.println("writing adapter:" + str); writeString(str); } } /** Reads the adapter id and returns the adapter. */ public SuperAdapter readAdapter() throws IOException { if (readInt() != 3988) System.err.println("wrong read position: readadapter."); String id= readString(); if (id == null) { // null adapter. :-) return null; } return SuperAdapter.getFromID(id); } /** Writes an item reference. */ public void writeItem(gitem_t item) throws IOException { if (item == null) writeInt(-1); else writeInt(item.index); } /** Reads the item index and returns the game item. */ public gitem_t readItem() throws IOException { int ndx= readInt(); if (ndx == -1) return null; else return Game.itemlist[ndx]; } } |