From c882ff2e192aaad0f84a1099db1877e0870e4f35 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Sat, 16 Mar 2013 18:21:27 -0500 Subject: [PATCH] Added fixme in version check. Need to add test for flight configurations. --- .../file/openrocket/OpenRocketSaver.java | 223 +++++++++--------- 1 file changed, 112 insertions(+), 111 deletions(-) diff --git a/core/src/net/sf/openrocket/file/openrocket/OpenRocketSaver.java b/core/src/net/sf/openrocket/file/openrocket/OpenRocketSaver.java index 761546ec6..650894e83 100644 --- a/core/src/net/sf/openrocket/file/openrocket/OpenRocketSaver.java +++ b/core/src/net/sf/openrocket/file/openrocket/OpenRocketSaver.java @@ -38,59 +38,59 @@ import net.sf.openrocket.util.TextUtil; public class OpenRocketSaver extends RocketSaver { private static final LogHelper log = Application.getLogger(); - - + + /** * Divisor used in converting an integer version to the point-represented version. * The integer version divided by this value is the major version and the remainder is * the minor version. For example 101 corresponds to file version "1.1". */ public static final int FILE_VERSION_DIVISOR = 100; - - + + private static final String OPENROCKET_CHARSET = "UTF-8"; - + private static final String METHOD_PACKAGE = "net.sf.openrocket.file.openrocket.savers"; private static final String METHOD_SUFFIX = "Saver"; - - + + // Estimated storage used by different portions // These have been hand-estimated from saved files private static final int BYTES_PER_COMPONENT_COMPRESSED = 80; private static final int BYTES_PER_SIMULATION_COMPRESSED = 100; private static final int BYTES_PER_DATAPOINT_COMPRESSED = 100; - - + + private int indent; private Writer dest; - + @Override public void save(OutputStream output, OpenRocketDocument document, StorageOptions options) throws IOException { - + log.info("Saving .ork file"); - + dest = new BufferedWriter(new OutputStreamWriter(output, OPENROCKET_CHARSET)); - + // Select file version number final int fileVersion = calculateNecessaryFileVersion(document, options); final String fileVersionString = (fileVersion / FILE_VERSION_DIVISOR) + "." + (fileVersion % FILE_VERSION_DIVISOR); log.debug("Storing file version " + fileVersionString); - - + + this.indent = 0; - - + + writeln(""); writeln(""); indent++; - + // Recursively save the rocket structure saveComponent(document.getRocket()); - + writeln(""); - + // Save custom expressions; saveCustomDatatypes(document); @@ -156,7 +156,7 @@ public class OpenRocketSaver extends RocketSaver { long size = 0; // FIXME - estimate decals - + // Size per component int componentCount = 0; Rocket rocket = doc.getRocket(); @@ -165,14 +165,14 @@ public class OpenRocketSaver extends RocketSaver { iterator.next(); componentCount++; } - + size += componentCount * BYTES_PER_COMPONENT_COMPRESSED; - - + + // Size per simulation size += doc.getSimulationCount() * BYTES_PER_SIMULATION_COMPRESSED; - - + + // Size per flight data point int pointCount = 0; double timeSkip = options.getSimulationTimeSkip(); @@ -186,13 +186,13 @@ public class OpenRocketSaver extends RocketSaver { } } } - + size += pointCount * BYTES_PER_DATAPOINT_COMPRESSED; - + return size; } - - + + /** * Determine which file version is required in order to store all the features of the * current design. By default the oldest version that supports all the necessary features @@ -205,7 +205,7 @@ public class OpenRocketSaver extends RocketSaver { private int calculateNecessaryFileVersion(OpenRocketDocument document, StorageOptions opts) { /* * File version 1.6 is required for: - * - saving files using appearances and textures + * - saving files using appearances and textures, flight configurations. * * File version 1.5 is requires for: * - saving designs using ComponentPrests @@ -222,21 +222,22 @@ public class OpenRocketSaver extends RocketSaver { * * Otherwise use version 1.0. */ - - // Search the rocket for any Appearnces (version 1.6) + + // Search the rocket for any Appearnces or non-motor flight configurations (version 1.6) for (RocketComponent c : document.getRocket()) { if (c.getAppearance() != null) { return FILE_VERSION_DIVISOR + 6; } + // FIXME - test for flight configurations } - + // Search the rocket for any ComponentPresets (version 1.5) for (RocketComponent c : document.getRocket()) { if (c.getPresetComponent() != null) { return FILE_VERSION_DIVISOR + 5; } } - + // Search for recovery device deployment type LOWER_STAGE_SEPARATION (version 1.5) for (RocketComponent c : document.getRocket()) { if (c instanceof RecoveryDevice) { @@ -250,17 +251,17 @@ public class OpenRocketSaver extends RocketSaver { if (!document.getCustomExpressions().isEmpty()) { return FILE_VERSION_DIVISOR + 5; } - + // Check if design has simulations defined (version 1.4) if (document.getSimulationCount() > 0) { return FILE_VERSION_DIVISOR + 4; } - + // Check for motor definitions (version 1.4) for (RocketComponent c : document.getRocket()) { if (!(c instanceof MotorMount)) continue; - + MotorMount mount = (MotorMount) c; for (String id : document.getRocket().getFlightConfigurationIDs()) { if (mount.getMotor(id) != null) { @@ -268,7 +269,7 @@ public class OpenRocketSaver extends RocketSaver { } } } - + // Check for fin tabs (version 1.1) for (RocketComponent c : document.getRocket()) { // Check for fin tabs @@ -279,7 +280,7 @@ public class OpenRocketSaver extends RocketSaver { return FILE_VERSION_DIVISOR + 1; } } - + // Check for components attached to tube coupler if (c instanceof TubeCoupler) { if (c.getChildCount() > 0) { @@ -287,45 +288,45 @@ public class OpenRocketSaver extends RocketSaver { } } } - + // Default (version 1.0) return FILE_VERSION_DIVISOR + 0; } - - - + + + @SuppressWarnings("unchecked") private void saveComponent(RocketComponent component) throws IOException { - + log.debug("Saving component " + component.getComponentName()); - + Reflection.Method m = Reflection.findMethod(METHOD_PACKAGE, component, METHOD_SUFFIX, "getElements", RocketComponent.class); if (m == null) { throw new BugException("Unable to find saving class for component " + component.getComponentName()); } - + // Get the strings to save List list = (List) m.invokeStatic(component); int length = list.size(); - + if (length == 0) // Nothing to do return; - + if (length < 2) { throw new RuntimeException("BUG, component data length less than two lines."); } - + // Open element writeln(list.get(0)); indent++; - + // Write parameters for (int i = 1; i < length - 1; i++) { writeln(list.get(i)); } - + // Recursively write subcomponents if (component.getChildCount() > 0) { writeln(""); @@ -341,25 +342,25 @@ public class OpenRocketSaver extends RocketSaver { indent--; writeln(""); } - + // Close element indent--; writeln(list.get(length - 1)); } - - + + private void saveSimulation(Simulation simulation, double timeSkip) throws IOException { SimulationOptions cond = simulation.getOptions(); - + writeln(""); indent++; - + writeln("" + escapeXML(simulation.getName()) + ""); // TODO: MEDIUM: Other simulators/calculators - + writeln("RK4Simulator"); writeln("BarrowmanCalculator"); - + writeln(""); indent++; @@ -373,7 +374,7 @@ public class OpenRocketSaver extends RocketSaver { writeElement("launchlatitude", cond.getLaunchLatitude()); writeElement("launchlongitude", cond.getLaunchLongitude()); writeElement("geodeticmethod", cond.getGeodeticComputation().name().toLowerCase(Locale.ENGLISH)); - + if (cond.isISAAtmosphere()) { writeln(""); } else { @@ -384,19 +385,19 @@ public class OpenRocketSaver extends RocketSaver { indent--; writeln(""); } - + writeElement("timestep", cond.getTimeStep()); - + indent--; writeln(""); - - + + for (String s : simulation.getSimulationListeners()) { writeElement("listener", escapeXML(s)); } - + // Write basic simulation data - + FlightData data = simulation.getSimulatedData(); if (data != null) { String str = ""); } - + indent--; writeln(""); - + } - - - + + + private void saveFlightDataBranch(FlightDataBranch branch, double timeSkip) throws IOException { double previousTime = -100000; - + if (branch == null) return; - + // Retrieve the types from the branch FlightDataType[] types = branch.getTypes(); - + if (types.length == 0) return; - + // Retrieve the data from the branch List> data = new ArrayList>(types.length); for (int i = 0; i < types.length; i++) { data.add(branch.get(types[i])); } List timeData = branch.get(FlightDataType.TYPE_TIME); - + // Build the tag StringBuilder sb = new StringBuilder(); sb.append(""); writeln(sb.toString()); indent++; - + // Write events for (FlightEvent event : branch.getEvents()) { writeln(""); } - + // Write the data int length = branch.getLength(); if (length > 0) { writeDataPointString(data, 0, sb); previousTime = timeData.get(0); } - + for (int i = 1; i < length - 1; i++) { if (timeData != null) { if (Math.abs(timeData.get(i) - previousTime - timeSkip) < Math.abs(timeData.get(i + 1) - previousTime - timeSkip)) { @@ -517,61 +518,61 @@ public class OpenRocketSaver extends RocketSaver { writeDataPointString(data, i, sb); } } - + if (length > 1) { writeDataPointString(data, length - 1, sb); } - + indent--; writeln(""); } - - - + + + /* TODO: LOW: This is largely duplicated from above! */ private int countFlightDataBranchPoints(FlightDataBranch branch, double timeSkip) { int count = 0; - + double previousTime = -100000; - + if (branch == null) return 0; - + // Retrieve the types from the branch FlightDataType[] types = branch.getTypes(); - + if (types.length == 0) return 0; - + List timeData = branch.get(FlightDataType.TYPE_TIME); if (timeData == null) { // If time data not available, store all points return branch.getLength(); } - + // Write the data int length = branch.getLength(); if (length > 0) { count++; previousTime = timeData.get(0); } - + for (int i = 1; i < length - 1; i++) { if (Math.abs(timeData.get(i) - previousTime - timeSkip) < Math.abs(timeData.get(i + 1) - previousTime - timeSkip)) { count++; previousTime = timeData.get(i); } } - + if (length > 1) { count++; } - + return count; } - - - + + + private void writeDataPointString(List> data, int index, StringBuilder sb) throws IOException { sb.setLength(0); @@ -584,17 +585,17 @@ public class OpenRocketSaver extends RocketSaver { sb.append(""); writeln(sb.toString()); } - - - + + + private void writeElement(String element, Object content) throws IOException { if (content == null) content = ""; writeln("<" + element + ">" + content + ""); } - - - + + + private void writeln(String str) throws IOException { if (str.length() == 0) { dest.write("\n"); @@ -606,10 +607,10 @@ public class OpenRocketSaver extends RocketSaver { s = s + str + "\n"; dest.write(s); } - - - - + + + + /** * Return the XML equivalent of an enum name. * @@ -619,5 +620,5 @@ public class OpenRocketSaver extends RocketSaver { public static String enumToXMLName(Enum e) { return e.name().toLowerCase(Locale.ENGLISH).replace("_", ""); } - + }