public static void Save(ChipEditor chipEditor) { ChipSaveData chipSaveData = new ChipSaveData(chipEditor); // Generate new chip save string var compositeChip = new SavedChip(chipSaveData); string saveString = JsonUtility.ToJson(compositeChip, usePrettyPrint); // Generate save string for wire layout var wiringSystem = new SavedWireLayout(chipSaveData); string wiringSaveString = JsonUtility.ToJson(wiringSystem, usePrettyPrint); // Write to file string savePath = SaveSystem.GetPathToSaveFile(chipEditor.chipName); using (StreamWriter writer = new StreamWriter(savePath)) { writer.Write(saveString); } string wireLayoutSavePath = SaveSystem.GetPathToWireSaveFile(chipEditor.chipName); using (StreamWriter writer = new StreamWriter(wireLayoutSavePath)) { writer.Write(wiringSaveString); } }
public static ChipSaveData GetChipSaveData(Chip chip, Chip[] builtinChips, List <Chip> spawnableChips, Wire wirePrefab, ChipEditor chipEditor) { // @NOTE: chipEditor can be removed here if: // * Chip & wire instatiation is inside their respective implementation holders is inside the chipEditor // * the wire connections are done inside ChipEditor.LoadFromSaveData instead of ChipLoader.LoadChipWithWires SavedChip chipToTryLoad; SavedChip[] savedChips = SaveSystem.GetAllSavedChips(); using (StreamReader reader = new StreamReader(SaveSystem.GetPathToSaveFile(chip.name))) { string chipSaveString = reader.ReadToEnd(); chipToTryLoad = JsonUtility.FromJson <SavedChip>(chipSaveString); } if (chipToTryLoad == null) { return(null); } SortChipsByOrderOfCreation(ref savedChips); // Maintain dictionary of loaded chips (initially just the built-in chips) Dictionary <string, Chip> loadedChips = new Dictionary <string, Chip> (); for (int i = 0; i < builtinChips.Length; i++) { Chip builtinChip = builtinChips[i]; loadedChips.Add(builtinChip.chipName, builtinChip); } foreach (Chip loadedChip in spawnableChips) { if (loadedChips.ContainsKey(loadedChip.chipName)) { continue; } loadedChips.Add(loadedChip.chipName, loadedChip); } ChipSaveData loadedChipData = LoadChipWithWires(chipToTryLoad, loadedChips, wirePrefab, chipEditor); SavedWireLayout wireLayout = LoadWiringFile(SaveSystem.GetPathToWireSaveFile(loadedChipData.chipName)); // Set wires anchor points for (int i = 0; i < wireLayout.serializableWires.Length; i++) { string startPinName = loadedChipData.componentChips[wireLayout.serializableWires[i].parentChipIndex].outputPins[wireLayout.serializableWires[i].parentChipOutputIndex].pinName; string endPinName = loadedChipData.componentChips[wireLayout.serializableWires[i].childChipIndex].inputPins[wireLayout.serializableWires[i].childChipInputIndex].pinName; int wireIndex = Array.FindIndex(loadedChipData.wires, w => w.startPin.pinName == startPinName && w.endPin.pinName == endPinName); if (wireIndex >= 0) { loadedChipData.wires[wireIndex].SetAnchorPoints(wireLayout.serializableWires[i].anchorPoints); } } return(loadedChipData); }
public static void Update(ChipEditor chipEditor, Chip chip) { ChipSaveData chipSaveData = new ChipSaveData(chipEditor); // Generate new chip save string var compositeChip = new SavedChip(chipSaveData); string saveString = JsonUtility.ToJson(compositeChip, usePrettyPrint); // Generate save string for wire layout var wiringSystem = new SavedWireLayout(chipSaveData); string wiringSaveString = JsonUtility.ToJson(wiringSystem, usePrettyPrint); // Write to file string savePath = SaveSystem.GetPathToSaveFile(chipEditor.chipName); using (StreamWriter writer = new StreamWriter(savePath)) { writer.Write(saveString); } string wireLayoutSavePath = SaveSystem.GetPathToWireSaveFile(chipEditor.chipName); using (StreamWriter writer = new StreamWriter(wireLayoutSavePath)) { writer.Write(wiringSaveString); } // Update parent chips using this chip string currentChipName = chipEditor.chipName; SavedChip[] savedChips = SaveSystem.GetAllSavedChips(); for (int i = 0; i < savedChips.Length; i++) { if (savedChips[i].componentNameList.Contains(currentChipName)) { int currentChipIndex = Array.FindIndex(savedChips[i].savedComponentChips, c => c.chipName == currentChipName); SavedComponentChip updatedComponentChip = new SavedComponentChip(chipSaveData, chip); SavedComponentChip oldComponentChip = savedChips[i].savedComponentChips[currentChipIndex]; // Update component chip I/O for (int j = 0; j < updatedComponentChip.inputPins.Length; j++) { for (int k = 0; k < oldComponentChip.inputPins.Length; k++) { if (updatedComponentChip.inputPins[j].name == oldComponentChip.inputPins[k].name) { updatedComponentChip.inputPins[j].parentChipIndex = oldComponentChip.inputPins[k].parentChipIndex; updatedComponentChip.inputPins[j].parentChipOutputIndex = oldComponentChip.inputPins[k].parentChipOutputIndex; updatedComponentChip.inputPins[j].isCylic = oldComponentChip.inputPins[k].isCylic; } } } // Write to file string parentSaveString = JsonUtility.ToJson(savedChips[i], usePrettyPrint); string parentSavePath = SaveSystem.GetPathToSaveFile(savedChips[i].name); using (StreamWriter writer = new StreamWriter(parentSavePath)) { writer.Write(parentSaveString); } } } }