Handle saved builds and old URLs

This commit is contained in:
Cmdr McDonald
2016-11-14 11:57:39 +00:00
parent 0c94c81746
commit 4486aa2e2b
5 changed files with 17 additions and 13 deletions

View File

@@ -90,7 +90,7 @@ export default class Coriolis extends React.Component {
r.params.code = ship.toString(); r.params.code = ship.toString();
this._setPage(OutfittingPage, r); this._setPage(OutfittingPage, r);
} catch (err) { } catch (err) {
this._onError("Failed to import ship", r.path, 0, 0, err); this._onError('Failed to import ship', r.path, 0, 0, err);
} }
} }

View File

@@ -21,6 +21,7 @@ import PowerManagement from '../components/PowerManagement';
import CostSection from '../components/CostSection'; import CostSection from '../components/CostSection';
import ModalExport from '../components/ModalExport'; import ModalExport from '../components/ModalExport';
import Slider from '../components/Slider'; import Slider from '../components/Slider';
import * as Utils from '../utils/UtilityFunctions';
const SPEED_SERIES = ['boost', '4 Pips', '2 Pips', '0 Pips']; const SPEED_SERIES = ['boost', '4 Pips', '2 Pips', '0 Pips'];
const SPEED_COLORS = ['#0088d2', '#ff8c0d', '#D26D00', '#c06400']; const SPEED_COLORS = ['#0088d2', '#ff8c0d', '#D26D00', '#c06400'];
@@ -58,7 +59,7 @@ export default class OutfittingPage extends Page {
_initState(context) { _initState(context) {
let params = context.route.params; let params = context.route.params;
let shipId = params.ship; let shipId = params.ship;
let code = params.code; let code = Utils.fromUrlSafe(params.code);
let buildName = params.bn; let buildName = params.bn;
let data = Ships[shipId]; // Retrieve the basic ship properties, slots and defaults let data = Ships[shipId]; // Retrieve the basic ship properties, slots and defaults
let savedCode = Persist.getBuild(shipId, buildName); let savedCode = Persist.getBuild(shipId, buildName);

View File

@@ -596,21 +596,23 @@ export default class Ship {
hardpoints = new Array(this.hardpoints.length), hardpoints = new Array(this.hardpoints.length),
internal = new Array(this.internal.length), internal = new Array(this.internal.length),
modifications = new Array(1 + this.standard.length + this.hardpoints.length + this.internal.length), modifications = new Array(1 + this.standard.length + this.hardpoints.length + this.internal.length),
parts = serializedString.split('.'), // Although we un-UrlSafe the serialized string when it comes in as a URL old code used to url-encode
// the build before it was written to local store so we do it again here to catch that situation
parts = Utils.fromUrlSafe(serializedString).split('.'),
priorities = null, priorities = null,
enabled = null, enabled = null,
code = parts[0]; code = parts[0];
if (parts[1]) { if (parts[1]) {
enabled = Utils.fromUrlSafe(LZString.decompressFromBase64(parts[1])).split(''); enabled = LZString.decompressFromBase64(parts[1]).split('');
} }
if (parts[2]) { if (parts[2]) {
priorities = Utils.fromUrlSafe(LZString.decompressFromBase64(parts[2])).split(''); priorities = LZString.decompressFromBase64(parts[2]).split('');
} }
if (parts[3]) { if (parts[3]) {
const modstr = Utils.fromUrlSafe(parts[3]); const modstr = parts[3];
if (modstr.match(':')) { if (modstr.match(':')) {
this.decodeModificationsString(modstr, modifications); this.decodeModificationsString(modstr, modifications);
} else { } else {
@@ -1199,7 +1201,7 @@ export default class Ship {
priorities.push(slot.priority); priorities.push(slot.priority);
} }
this.serialized.priorities = Utils.toUrlSafe(LZString.compressToBase64(priorities.join(''))); this.serialized.priorities = LZString.compressToBase64(priorities.join(''));
return this; return this;
} }
@@ -1220,7 +1222,7 @@ export default class Ship {
enabled.push(slot.enabled ? 1 : 0); enabled.push(slot.enabled ? 1 : 0);
} }
this.serialized.enabled = Utils.toUrlSafe(LZString.compressToBase64(enabled.join(''))); this.serialized.enabled = LZString.compressToBase64(enabled.join(''));
return this; return this;
} }
@@ -1266,7 +1268,7 @@ export default class Ship {
} }
allMods.push(slotMods.join(';')); allMods.push(slotMods.join(';'));
} }
this.serialized.modifications = Utils.toUrlSafe(LZString.compressToBase64(allMods.join(',').replace(/,+$/, ''))); this.serialized.modifications = LZString.compressToBase64(allMods.join(',').replace(/,+$/, ''));
return this; return this;
} }
@@ -1387,7 +1389,7 @@ export default class Ship {
buffer.writeInt8(-1, curpos++); buffer.writeInt8(-1, curpos++);
} }
this.serialized.modifications = Utils.toUrlSafe(zlib.gzipSync(buffer).toString('base64')); this.serialized.modifications = zlib.gzipSync(buffer).toString('base64');
} else { } else {
this.serialized.modifications = null; this.serialized.modifications = null;
} }

View File

@@ -1,3 +1,4 @@
import * as Utils from './/UtilityFunctions';
/** /**
* Generates a URL for the outiffing page * Generates a URL for the outiffing page
@@ -10,7 +11,7 @@ export function outfitURL(shipId, code, buildName) {
let parts = ['/outfit/', shipId]; let parts = ['/outfit/', shipId];
if (code) { if (code) {
parts.push('/', code); parts.push('/', Utils.toUrlSafe(code));
} }
if (buildName) { if (buildName) {

View File

@@ -65,7 +65,7 @@ export function shallowEqual(objA, objB) {
* @return {string} the converted string * @return {string} the converted string
*/ */
export function toUrlSafe(data) { export function toUrlSafe(data) {
return data.replace(/\//g, '-').replace(/\+/g, '_'); return data ? data.replace(/\//g, '-').replace(/\+/g, '_') : null;
} }
/** /**
@@ -74,5 +74,5 @@ export function toUrlSafe(data) {
* @return {string} the converted string * @return {string} the converted string
*/ */
export function fromUrlSafe(data) { export function fromUrlSafe(data) {
return data.replace(/-/g, '/').replace(/_/g, '+'); return data ? data.replace(/-/g, '/').replace(/_/g, '+') : null;
} }