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 static bool IsSignalSafeToDelete(string chipName, string signalName) { SavedChip[] savedChips = SaveSystem.GetAllSavedChips(); for (int i = 0; i < savedChips.Length; i++) { if (savedChips[i].componentNameList.Contains(chipName)) { SavedChip parentChip = savedChips[i]; int currentChipIndex = Array.FindIndex(parentChip.savedComponentChips, scc => scc.chipName == chipName); SavedComponentChip currentChip = parentChip.savedComponentChips[currentChipIndex]; int currentSignalIndex = Array.FindIndex(currentChip.outputPins, name => name.name == signalName); if (Array.Find(currentChip.inputPins, pin => pin.name == signalName && pin.parentChipIndex >= 0) != null) { return(false); } else if (currentSignalIndex >= 0 && parentChip.savedComponentChips.Any(scc => scc.inputPins.Any(pin => pin.parentChipIndex == currentChipIndex && pin.parentChipOutputIndex == currentSignalIndex))) { return(false); } } } return(true); }
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); }