private void AddRotor(AutomatedRotor autoRotor) { if (autoRotor != null) { if (!rotorDictionary.ContainsKey(autoRotor.Rotor.CustomName.ToLower())) { rotorDictionary.Add(autoRotor.Rotor.CustomName.ToLower(), autoRotor); } else { rotorDictionary[autoRotor.Rotor.CustomName.ToLower()] = autoRotor; } } }
public void ConfigureRotors(bool shouldBeAlive) { // Only update rotor instructions when config file has been changed if (Me.CustomData.Length == configSize) { return; } // Ensure the rotor list contains all the current rotors rotors.Clear(); GridTerminalSystem.GetBlocksOfType(rotors); configSize = Me.CustomData.Length; // stop all active rotors before update isAlive = shouldBeAlive; runtimeMessages.Clear(); foreach (AutomatedRotor r in rotorDictionary.Values) { r.Kill(); } AutomatedRotor autoRotor = null; string[] lines = Me.CustomData.Split(new char[] { '\n', '\r' }); string[] operations = Enum.GetNames(typeof(Operations)); string[] modes = Enum.GetNames(typeof(Modes)); string[] properties = Enum.GetNames(typeof(Properties)); for (int i = 0; i < lines.Length; i++) { string line = lines[i].Trim(' ', '\t'); string[] settings = line.Split(' ').Where(x => !string.IsNullOrEmpty(x) && !x.StartsWith("\t")).ToArray(); string firstCommand = (settings.Length > 0) ? settings[0].ToLower() : ""; if (operations.Contains(firstCommand)) { // save and clear for next rotor if (autoRotor != null) { AddRotor(autoRotor); autoRotor = null; } Operations operation = (Operations)Enum.Parse(typeof(Operations), firstCommand); if (settings.Length < 3 && operation != Operations.simple || settings.Length < 2) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Not enough arguments\n"); } else { Modes mode = Modes.none; string rotorName = string.Empty; if (operation == Operations.simple) { rotorName = line.Substring(settings[0].Length).TrimStart(' ', '\t').ToLower(); } else if (modes.Contains(settings[1].ToLower())) { mode = (Modes)Enum.Parse(typeof(Modes), settings[1].ToLower()); rotorName = line.Substring(settings[0].Length).TrimStart(' ', '\t'); rotorName = rotorName.Substring(settings[1].Length).TrimStart(' ', '\t').ToLower(); } else { runtimeMessages.Append($"[ERROR] Line {i + 1}: The mode given does not mach existing modes\n"); } IMyMotorStator rotor = rotors.Find(r => r.CustomName.ToLower().Contains(rotorName)); if (rotor == null) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Could not find rotor with name \"{rotorName}\"\n"); } else { autoRotor = new AutomatedRotor(rotor, operation, mode, new List <InstructionSet>()); } } } else if (properties.Contains(firstCommand)) { Properties property = (Properties)Enum.Parse(typeof(Properties), firstCommand); if (autoRotor == null || autoRotor.Operation == Operations.simple) { runtimeMessages.Append($"[WARNING] Line {i + 1}: Skipping {settings[0]}\n"); } else if (settings.Length < 2) { runtimeMessages.Append($"[WARNING] Line {i + 1}: {settings[0]} does not have a value\n"); } else if (property == Properties.reset) { if (settings.Length < 3) { runtimeMessages.Append($"[WARNING] Line {i + 1}: Reset property does not have a speed value\n"); } else { float angle; if (float.TryParse(settings[1], out angle)) { float speed; if (float.TryParse(settings[2], out speed)) { autoRotor.ResetInstructions = new InstructionSet(speed, angle, 0); } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Failed to parse the Reset property speed value.\n"); } } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Failed to parse the Reset property angle value.\n"); } } } else if (property == Properties.rotorlock) { bool shouldRotorLock; if (bool.TryParse(settings[1], out shouldRotorLock)) { autoRotor.LockRotor = shouldRotorLock; } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Failed to parse RotorLock value.\n"); } } else if (property == Properties.precision) { float value; if (float.TryParse(settings[1], out value)) { if (value > 0) { autoRotor.Precision = value; } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Precision value must be greater than 0\n"); } } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Failed to parse Precision value.\n"); } } else { runtimeMessages.Append($"[WARNING] {settings[0]} is not being properly handled please contact the script owner\n"); } } else if (line.StartsWith("[")) { if (autoRotor == null || autoRotor.Operation == Operations.simple) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Cannot add instruction set\n"); } else { string[] instructionSet = line.Trim(new char[] { '[', ']' }).Split(','); float angle; float speed; long time = 0; if (instructionSet.Length < 3 && !(autoRotor.Operation == Operations.increment && autoRotor.Mode == Modes.once)) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Instruction set must contain waitTime\n"); } else if (instructionSet.Length < 2 && autoRotor.Operation == Operations.increment && autoRotor.Mode == Modes.once) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Instruction set is malformed\n"); } else if (autoRotor.Operation == Operations.increment && autoRotor.Instructions.Count == 1) { runtimeMessages.Append($"[WARNING] Line {i + 1}: Ignoring extra instruction set\n"); } else if (!float.TryParse(instructionSet[0], out angle)) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Failed to parse angle\n"); } else if (!float.TryParse(instructionSet[1], out speed)) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Failed to parse speed\n"); } else if (instructionSet.Length > 2 && !long.TryParse(instructionSet[2], out time)) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Failed to parse time\n"); } else if (speed == 0) { runtimeMessages.Append($"[ERROR] Line {i + 1}: Speed must not be 0\n"); } else if (time < 0) { runtimeMessages.Append($"[ERROR] Line {i + 1}: WiatTime must be greater than 0\n"); } else { autoRotor.Instructions.Add(new InstructionSet(speed, angle, time)); } } } else if (line.StartsWith("#") || string.IsNullOrWhiteSpace(line)) { // this lets people add comments } else { runtimeMessages.Append($"[WARNING] Line {i + 1}: Failed to parse {settings[0]}, check spelling\n"); } } // Adds the last rotor to the dictionary AddRotor(autoRotor); runtimeMessages.Append($"Done. {rotorDictionary.Count} rotors parsed\n"); }