Пример #1
0
    static Dictionary <int, string> FindChildrenChips(string chipName)
    {
        Dictionary <int, string> childrenChips = new Dictionary <int, string>();

        Manager manager = GameObject.FindObjectOfType <Manager>();

        SavedChip[] allChips    = SaveSystem.GetAllSavedChips();
        SavedChip   currentChip = Array.Find(allChips, c => c.name == chipName);

        if (currentChip == null)
        {
            return(childrenChips);
        }

        childrenChips.Add(currentChip.creationIndex, chipName);

        foreach (SavedComponentChip scc in currentChip.savedComponentChips)
        {
            if (Array.FindIndex(manager.builtinChips, c => c.chipName == scc.chipName) != -1)
            {
                continue;
            }

            foreach (KeyValuePair <int, string> chip in FindChildrenChips(scc.chipName))
            {
                if (childrenChips.ContainsKey(chip.Key))
                {
                    continue;
                }
                childrenChips.Add(chip.Key, chip.Value);
            }
        }

        return(childrenChips);
    }
Пример #2
0
    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);
        }
    }
Пример #3
0
    public static SavedChip[] GetAllSavedChips(string[] chipPaths)
    {
        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();

                // If the save does not contain wireType and contains outputPinNames then its a previous version save (v 0.25 expected)
                if (!chipSaveString.Contains("wireType") ||  chipSaveString.Contains("outputPinNames") ||   !chipSaveString.Contains("outputPins"))
                {
                    // An update is made to the save string and returned
                    string updatedSave = SaveCompatibility.FixSaveCompatibility(chipSaveString);
                    savedChips[i] = JsonUtility.FromJson <SavedChip>(updatedSave);
                }
                else
                {
                    savedChips[i] = JsonUtility.FromJson <SavedChip>(chipSaveString);
                }
            }
        }
        return(savedChips);
    }
Пример #4
0
    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);
        }
    }
Пример #5
0
    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);
        }
    }
Пример #6
0
    public static SavedChip[] GetAllSavedChips(string[] chipPaths)
    {
        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);
            }
        }
        return(savedChips);
    }
Пример #7
0
    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);
    }
Пример #8
0
 public static void EditSavedChip(SavedChip savedChip, ChipSaveData chipSaveData)
 {
 }
Пример #9
0
    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);
                }
            }
        }
    }
Пример #10
0
    // 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);
    }