/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * 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 3 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, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.data; import java.io.EOFException; import java.io.IOException; import java.text.NumberFormat; import java.util.Calendar; import net.rubikscomplex.ufosge.util.UFOInputStream; import net.rubikscomplex.ufosge.util.UFOOutputStream; /** * * @author Chris Davoren */ public class SaveInfo { public static final byte TYPE_GEOSCAPE = 0; public static final byte TYPE_BATTLESCAPE = 1; public static final int NAME_LEN = 26; public int slot; public boolean isBattlescapeAutosave; public String name; public Calendar time; public short type; public static String getSaveTypeName (byte type) { switch (type) { case TYPE_GEOSCAPE: return "Geoscape"; case TYPE_BATTLESCAPE: return "Battlescape"; default: return "Unknown"; } } public SaveInfo() { } public boolean readFromFile(UFOInputStream in) throws IOException { try { isBattlescapeAutosave = in.readShort() == 0; name = in.readUFOString(NAME_LEN); short year = in.readUFOShort(); short month = in.readUFOShort(); short day = in.readUFOShort(); short hour = in.readUFOShort(); short minute = in.readUFOShort(); time = Calendar.getInstance(); time.set(year, month, day, hour, minute); type = in.readShort(); return true; } catch (EOFException e) { return false; } } public void writeToFile(UFOOutputStream out) throws IOException { out.writeUFOShort((short)(isBattlescapeAutosave ? 0 : 1)); out.writeUFOString(name, NAME_LEN); out.writeUFOShort((short)time.get(Calendar.YEAR)); out.writeUFOShort((short)time.get(Calendar.MONTH)); out.writeUFOShort((short)time.get(Calendar.DATE)); out.writeUFOShort((short)time.get(Calendar.HOUR_OF_DAY)); out.writeUFOShort((short)time.get(Calendar.MINUTE)); out.writeUFOShort(type); } public String formatTime() { NumberFormat nf = NumberFormat.getInstance(); StringBuffer sb = new StringBuffer(); nf.setMinimumIntegerDigits(2); sb.append(nf.format(time.get(Calendar.DATE))); sb.append("/"); sb.append(nf.format(time.get(Calendar.MONTH) + 1)); sb.append("/"); sb.append(time.get(Calendar.YEAR)); sb.append(" "); sb.append(nf.format(time.get(Calendar.HOUR_OF_DAY))); sb.append(":"); sb.append(nf.format(time.get(Calendar.MINUTE))); return sb.toString(); } }