Modified the function convert_quest_from_model to do a deep copy of the given object.

Added a couple other cloning functions.
This commit is contained in:
jtuu 2019-11-01 18:13:32 +02:00
parent af5c27a565
commit 89a119e90d
2 changed files with 48 additions and 10 deletions

View File

@ -64,6 +64,17 @@ function instructions_equal(a: Instruction, b: Instruction): boolean {
return a.opcode.code === b.opcode.code && arrays_equal(a.args, b.args, args_equal);
}
export function clone_instruction(instr: Instruction): Instruction {
return {
opcode: instr.opcode,
args: instr.args.map(arg => ({ ...arg })),
arg_size: instr.arg_size,
size: instr.size,
param_to_args: instr.param_to_args.map(args => args.map(arg => ({ ...arg }))),
asm: instr.asm,
};
}
/**
* Instruction argument.
*/
@ -163,3 +174,29 @@ function segments_equal(a: Segment, b: Segment): boolean {
export function segment_arrays_equal(a: readonly Segment[], b: readonly Segment[]): boolean {
return arrays_equal(a, b, segments_equal);
}
export function clone_segment(seg: Segment): Segment {
const clone: Partial<Segment> = {
type: seg.type,
labels: seg.labels.slice(),
asm: {
labels: seg.asm.labels.map(label => ({ ...label })),
},
};
switch (clone.type) {
case SegmentType.Instructions:
clone.instructions = (seg as InstructionSegment).instructions.map(instr =>
clone_instruction(instr),
);
break;
case SegmentType.Data:
clone.data = (seg as DataSegment).data.slice(0);
break;
case SegmentType.String:
clone.value = (seg as StringSegment).value;
break;
}
return clone as Segment;
}

View File

@ -18,6 +18,7 @@ import {
import { QuestEventDagModel, QuestEventDagModelMeta } from "../model/QuestEventDagModel";
import { QuestEvent } from "../../core/data_formats/parsing/quest/entities";
import Logger from "js-logger";
import { clone_segment } from "../scripting/instructions";
const logger = Logger.get("quest_editor/stores/model_conversion");
@ -251,20 +252,20 @@ export function convert_quest_from_model(quest: QuestModel): Quest {
type: obj.type,
area_id: obj.area_id,
section_id: obj.section_id.val,
position: obj.position.val,
rotation: obj.rotation.val,
position: obj.position.val.clone(),
rotation: obj.rotation.val.clone(),
unknown: obj.unknown,
id: obj.id,
group_id: obj.group_id,
properties: obj.properties,
properties: new Map(obj.properties),
})),
npcs: quest.npcs.val.map(npc => ({
type: npc.type,
area_id: npc.area_id,
section_id: npc.section_id.val,
position: npc.position.val,
rotation: npc.rotation.val,
scale: npc.scale,
position: npc.position.val.clone(),
rotation: npc.rotation.val.clone(),
scale: npc.scale.clone(),
unknown: npc.unknown,
pso_type_id: npc.pso_type_id,
npc_id: npc.npc_id,
@ -272,10 +273,10 @@ export function convert_quest_from_model(quest: QuestModel): Quest {
pso_roaming: npc.pso_roaming,
})),
events: convert_quest_events_from_model(quest.event_dags.val),
dat_unknowns: quest.dat_unknowns,
object_code: quest.object_code,
shop_items: quest.shop_items,
map_designations: quest.map_designations.val,
dat_unknowns: quest.dat_unknowns.map(unk => ({...unk})),
object_code: quest.object_code.map(seg => clone_segment(seg)),
shop_items: quest.shop_items.slice(),
map_designations: new Map(quest.map_designations.val),
};
}