|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using MIDIKeyboard.dataFolder; |
| 5 | +using static MIDIKeyboard.miscellaneous.Miscellaneous; |
| 6 | + |
| 7 | +namespace MIDIKeyboard.Run |
| 8 | +{ |
| 9 | + class LoadData |
| 10 | + { |
| 11 | + public static bool Load() => LoadFile() && SendOutputData() & LoadKeyData(); |
| 12 | + |
| 13 | + static string[] array; |
| 14 | + |
| 15 | + static bool LoadFile(string path = "data.txt") |
| 16 | + { |
| 17 | + Console.Write("Loading data from path \"{0}\"...", path); |
| 18 | + if (File.Exists(path) && (array = File.ReadAllLines(path)).Length > 2) { |
| 19 | + Console.WriteLine(" Done."); |
| 20 | + return true; |
| 21 | + } |
| 22 | + Console.WriteLine(" Error."); |
| 23 | + Error("Error -> No data."); |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + static bool SendOutputData() |
| 28 | + { |
| 29 | + if (array[0].Split(',').Length > 1) { |
| 30 | + Console.Write("Sending output data..."); |
| 31 | + string[] ColorArray = array[0].Split(','); |
| 32 | + |
| 33 | + InputPort midiOut = new InputPort(); |
| 34 | + midiOut.OpenOut(int.Parse(ColorArray[1].Trim())); |
| 35 | + |
| 36 | + //send output |
| 37 | + for (int i = 2; i + 1 < ColorArray.Length; i += 2) { |
| 38 | + midiOut.MidiOutMsg((byte)int.Parse(ColorArray[i].Trim()), (byte)int.Parse(ColorArray[i + 1].Trim())); |
| 39 | + } |
| 40 | + |
| 41 | + midiOut.CloseOut(); |
| 42 | + Console.WriteLine(" Done."); |
| 43 | + return true; |
| 44 | + } |
| 45 | + Console.WriteLine("No output data detected."); |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + static bool LoadKeyData() |
| 50 | + { |
| 51 | + bool noErrors = true; |
| 52 | + Console.WriteLine("loading from file..."); |
| 53 | + int chanel = int.Parse(array[0].Split(',')[0]); |
| 54 | + |
| 55 | + for (int lineID = 0; lineID < array.Length; lineID++) { |
| 56 | + if (array[lineID] != string.Concat(chanel)) { |
| 57 | + string[] loadedValues = array[lineID].Split(new char[] { ',' }); |
| 58 | + int[] tempValues = new int[loadedValues.Length]; |
| 59 | + StringBuilder log = new StringBuilder(); |
| 60 | + for (int i = 0; i < tempValues.Length; i++) { |
| 61 | + string value = loadedValues[i].Trim(); |
| 62 | + if (!int.TryParse(value, out tempValues[i])) { |
| 63 | + string[] convertValue; |
| 64 | + if ((convertValue = value.Split('\'')).Length > 1) { |
| 65 | + tempValues[i] = GetID(convertValue[1][0]); |
| 66 | + if (convertValue[0][convertValue[0].Length - 1] == '-') { |
| 67 | + if (i < 2 || i > 1 && tempValues[1] != 1) { |
| 68 | + noErrors = false; |
| 69 | + Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value); |
| 70 | + } else |
| 71 | + tempValues[i] = -tempValues[i]; |
| 72 | + } |
| 73 | + } else if ((convertValue = value.Split('"')).Length > 1) { |
| 74 | + tempValues[i] = GetID(convertValue[1][0]); |
| 75 | + if (convertValue[0][convertValue[0].Length - 1] == '-') { |
| 76 | + if (i < 2 || i > 1 && tempValues[1] != 1) { |
| 77 | + noErrors = false; |
| 78 | + Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value); |
| 79 | + } else |
| 80 | + tempValues[i] = -tempValues[i]; |
| 81 | + } |
| 82 | + } else if (value.Length == 1) { |
| 83 | + tempValues[i] = GetID(value[0]); |
| 84 | + } else { |
| 85 | + if (value.Length == 2 && value[0] == '-') { |
| 86 | + tempValues[i] = -GetID(value[1]); |
| 87 | + } else { |
| 88 | + noErrors = false; |
| 89 | + Error("Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineID, array[lineID], value.Length > 1 ? "s" : "", value); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + log.Append(value); |
| 94 | + log.Append(' '); |
| 95 | + } |
| 96 | + Program.values.Add(tempValues); |
| 97 | + Console.WriteLine(log); |
| 98 | + } |
| 99 | + } |
| 100 | + return noErrors; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments