public static void LoadAllChips(string[] chipPaths, Manager manager) { SavedChip[] savedChips = GetAllSavedChips(chipPaths); 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 < manager.builtinChips.Length; i++) { Chip builtinChip = manager.builtinChips[i]; loadedChips.Add(builtinChip.chipName, builtinChip); } for (int i = 0; i < savedChips.Length; i++) { SavedChip chipToTryLoad = savedChips[i]; ChipSaveData loadedChipData = LoadChip(chipToTryLoad, loadedChips, manager.wirePrefab); Chip loadedChip = manager.LoadChip(loadedChipData); if (loadedChip is CustomChip custom) { custom.ApplyWireModes(); } loadedChips.Add(loadedChip.chipName, loadedChip); } }
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 void LoadFromSaveData(ChipSaveData saveData) { chipName = saveData.chipName; chipColour = saveData.chipColour; chipNameColour = saveData.chipNameColour; creationIndex = saveData.creationIndex; // Load component chips for (int i = 0; i < saveData.componentChips.Length; i++) { Chip componentChip = saveData.componentChips[i]; if (componentChip as InputSignal) { inputsEditor.LoadSignal(componentChip as InputSignal); } else if (componentChip as OutputSignal) { outputsEditor.LoadSignal(componentChip as OutputSignal); } else { chipInteraction.LoadChip(componentChip); } } // Load wires if (saveData.wires != null) { for (int i = 0; i < saveData.wires.Length; i++) { pinAndWireInteraction.LoadWire(saveData.wires[i]); } } }
public static void LoadAllChips(string[] chipPaths, Manager manager) { SavedChip[] savedChips = new SavedChip[chipPaths.Length]; // Read saved chips from file for (int i = 0; i < chipPaths.Length; i++) { using (StreamReader reader = new StreamReader(chipPaths[i])) { string chipSaveString = reader.ReadToEnd(); savedChips[i] = JsonUtility.FromJson <SavedChip> (chipSaveString); } } 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 < manager.builtinChips.Length; i++) { Chip builtinChip = manager.builtinChips[i]; loadedChips.Add(builtinChip.chipName, builtinChip); } for (int i = 0; i < savedChips.Length; i++) { SavedChip chipToTryLoad = savedChips[i]; ChipSaveData loadedChipData = LoadChip(chipToTryLoad, loadedChips, manager.wirePrefab); Chip loadedChip = manager.LoadChip(loadedChipData); loadedChips.Add(loadedChip.chipName, loadedChip); } }
public void ViewChip(Chip chip) { LoadNewEditor(); UIManager.ChangeState(UIManagerState.Update); ChipSaveData chipSaveData = ChipLoader.GetChipSaveData(chip, builtinChips, spawnableChips, wirePrefab, activeChipEditor); activeChipEditor.LoadFromSaveData(chipSaveData); }
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 Chip LoadChip(ChipSaveData loadedChipData) { activeChipEditor.LoadFromSaveData(loadedChipData); currentChipCreationIndex = activeChipEditor.creationIndex; Chip loadedChip = PackageChip(); LoadNewEditor(); return(loadedChip); }
public SavedWireLayout(ChipSaveData chipSaveData) { Wire[] allWires = chipSaveData.wires; serializableWires = new SavedWire[allWires.Length]; for (int i = 0; i < allWires.Length; i++) { serializableWires[i] = new SavedWire(chipSaveData, allWires[i]); } }
public SavedWire(ChipSaveData chipSaveData, Wire wire) { Pin parentPin = wire.startPin; Pin childPin = wire.endPin; parentChipIndex = chipSaveData.ComponentChipIndex(parentPin.chip); parentChipOutputIndex = parentPin.index; childChipIndex = chipSaveData.ComponentChipIndex(childPin.chip); childChipInputIndex = childPin.index; anchorPoints = wire.anchorPoints.ToArray(); }
public SavedInputPin(ChipSaveData chipSaveData, Pin pin) { name = pin.pinName; isCylic = pin.cyclic; if (pin.parentPin) { parentChipIndex = chipSaveData.ComponentChipIndex(pin.parentPin.chip); parentChipOutputIndex = pin.parentPin.index; } else { parentChipIndex = -1; parentChipOutputIndex = -1; } }
public SavedChip(ChipSaveData chipSaveData) { name = chipSaveData.chipName; creationIndex = chipSaveData.creationIndex; colour = chipSaveData.chipColour; nameColour = chipSaveData.chipNameColour; // Create list of (unique) names of all chips used to make this chip componentNameList = chipSaveData.componentChips.Select(x => x.chipName).Distinct().ToArray(); // Create serializable chips savedComponentChips = new SavedComponentChip[chipSaveData.componentChips.Length]; for (int i = 0; i < chipSaveData.componentChips.Length; i++) { savedComponentChips[i] = new SavedComponentChip(chipSaveData, chipSaveData.componentChips[i]); } }
public SavedComponentChip(ChipSaveData chipSaveData, Chip chip) { chipName = chip.chipName; // Store position in doubles and limit precision to reduce space in save file const double precision = 10000; posX = ((int)(chip.transform.position.x * precision)) / precision; posY = ((int)(chip.transform.position.y * precision)) / precision; // Input pins inputPins = new SavedInputPin[chip.inputPins.Length]; for (int i = 0; i < inputPins.Length; i++) { inputPins[i] = new SavedInputPin(chipSaveData, chip.inputPins[i]); } // Output pins (only need to store names, connections are stored on input pins only) outputPinNames = new string[chip.outputPins.Length]; for (int i = 0; i < chip.outputPins.Length; i++) { outputPinNames[i] = chip.outputPins[i].pinName; } }
public static void EditSavedChip(SavedChip savedChip, ChipSaveData chipSaveData) { }
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); } } } }
// Instantiates all components that make up the given clip, and connects them up with wires // The components are parented under a single "holder" object, which is returned from the function static ChipSaveData LoadChip(SavedChip chipToLoad, Dictionary <string, Chip> previouslyLoadedChips, Wire wirePrefab) { ChipSaveData loadedChipData = new ChipSaveData(); int numComponents = chipToLoad.savedComponentChips.Length; loadedChipData.componentChips = new Chip[numComponents]; loadedChipData.chipName = chipToLoad.name; loadedChipData.chipColour = chipToLoad.colour; loadedChipData.chipNameColour = chipToLoad.nameColour; loadedChipData.creationIndex = chipToLoad.creationIndex; // Spawn component chips (the chips used to create this chip) // These will have been loaded already, and stored in the previouslyLoadedChips dictionary for (int i = 0; i < numComponents; i++) { SavedComponentChip componentToLoad = chipToLoad.savedComponentChips[i]; string componentName = componentToLoad.chipName; Vector2 pos = new Vector2((float)componentToLoad.posX, (float)componentToLoad.posY); if (!previouslyLoadedChips.ContainsKey(componentName)) { Debug.LogError("Failed to load sub component: " + componentName + " While loading " + chipToLoad.name); } Chip loadedComponentChip = GameObject.Instantiate(previouslyLoadedChips[componentName], pos, Quaternion.identity); loadedChipData.componentChips[i] = loadedComponentChip; // Load input pin names for (int inputIndex = 0; inputIndex < componentToLoad.inputPins.Length; inputIndex++) { loadedChipData.componentChips[i].inputPins[inputIndex].pinName = componentToLoad.inputPins[inputIndex].name; } // Load output pin names for (int ouputIndex = 0; ouputIndex < componentToLoad.outputPinNames.Length; ouputIndex++) { loadedChipData.componentChips[i].outputPins[ouputIndex].pinName = componentToLoad.outputPinNames[ouputIndex]; } } // Connect pins with wires for (int chipIndex = 0; chipIndex < chipToLoad.savedComponentChips.Length; chipIndex++) { Chip loadedComponentChip = loadedChipData.componentChips[chipIndex]; for (int inputPinIndex = 0; inputPinIndex < loadedComponentChip.inputPins.Length; inputPinIndex++) { SavedInputPin savedPin = chipToLoad.savedComponentChips[chipIndex].inputPins[inputPinIndex]; Pin pin = loadedComponentChip.inputPins[inputPinIndex]; // If this pin should receive input from somewhere, then wire it up to that pin if (savedPin.parentChipIndex != -1) { Pin connectedPin = loadedChipData.componentChips[savedPin.parentChipIndex].outputPins[savedPin.parentChipOutputIndex]; pin.cyclic = savedPin.isCylic; Pin.TryConnect(connectedPin, pin); //if (Pin.TryConnect (connectedPin, pin)) { //Wire loadedWire = GameObject.Instantiate (wirePrefab, parent : chipHolder); //loadedWire.Connect (connectedPin, loadedComponentChip.inputPins[inputPinIndex]); //} } } } return(loadedChipData); }
public SavedOutputPin(ChipSaveData chipSaveData, Pin pin) { name = pin.pinName; wireType = pin.wireType; }