예제 #1
0
        public static CircuitChip Deserialize(string name)
        {
            var path = NameToPath(name);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            var reader = new StreamReader(path);

            var version = reader.ReadLine();

            ParseField(version, "v", out _, out int versionNumber);

            if (versionNumber != 1)
            {
                throw new InvalidDataException("Invalid version number");
            }

            var circuitName  = reader.ReadLine();
            var circuitChip  = new CircuitChip(circuitName);
            var pinStateDict = new Dictionary <int, PinState>();

            var pinsIdentifier = reader.ReadLine();

            ParseField(pinsIdentifier, "Pins", out _, out int pinCount);

            for (var i = 0; i < pinCount; i++)
            {
                var hasPinName = ParseField(reader.ReadLine(), out string pinName, out int pinStateId);

                Pin pinToAdd;
                if (pinStateDict.TryGetValue(pinStateId, out PinState pinState))
                {
                    pinToAdd = new Pin(pinState); // id exists, connect pin to existing pin state
                }
                else
                {
                    pinToAdd = new Pin(); // id does not exist, create new pin state and add to dict
                    pinStateDict.Add(pinStateId, pinToAdd.PinState);
                }

                if (hasPinName)
                {
                    pinToAdd.Name = pinName;
                }

                circuitChip.AddPin(pinToAdd);
            }

            var chipsIdentifier = reader.ReadLine();

            ParseField(chipsIdentifier, "Chips", out _, out int chipCount);

            for (var i = 0; i < chipCount; i++)
            {
                var  chipName = reader.ReadLine();
                Chip chip;
                if (Chip.IsPrimative(chipName, out PrimativeChipType primativeChipType))
                {
                    chip = new PrimativeChip(primativeChipType);
                }
                else
                {
                    chip = Deserialize(chipName);
                }

                for (var pinIndex = 0; pinIndex < chip.Pins.Count; pinIndex++)
                {
                    var currentPin = chip.Pins[pinIndex];

                    var hasPinName = ParseField(reader.ReadLine(), out string pinName, out int pinStateId);
                    if (hasPinName)
                    {
                        currentPin.Name = pinName;
                    }

                    if (pinStateDict.TryGetValue(pinStateId, out PinState pinState))
                    {
                        PinState.Connect(pinState, currentPin.PinState); // connect on the pinstate level
                    }
                    else
                    {
                        pinStateDict.Add(pinStateId, currentPin.PinState);
                    }
                }

                circuitChip.AddChip(chip);
            }

            return(circuitChip);
        }
예제 #2
0
 public static bool IsPrimative(Chip chip)
 {
     return(chip is PrimativeChip);
 }
예제 #3
0
 // recursively serializes all chips uses in the chip arg
 public static void DeepSerialize(Chip chip)
 {
     DeepSerialize(chip, new HashSet <string>());
 }
예제 #4
0
 private static int GetChipId(Chip chip)
 {
     return(allChips.IndexOf(chip));
 }