Пример #1
0
 /// <summary>
 /// Resets all primitive fields in the container to valid values, recursively.
 /// (For the moment, we'll assume that's all that's required.)
 /// </summary>
 public void Reset(FixedContainer context, ModuleData data)
 {
     foreach (var field in Fields.OfType <IPrimitiveField>())
     {
         field.Reset(context, data);
     }
 }
Пример #2
0
        public override bool TrySetText(FixedContainer context, ModuleData data, string text)
        {
            var address = GetAddress(context);

            if (text.Length > Length || text.Any(c => c < 32 || c > 126))
            {
                return(false);
            }
            text = text.PadRight(Length);
            switch (bytesPerChar)
            {
            case 1:
                byte[] ascii = Encoding.ASCII.GetBytes(text);
                data.SetData(address, ascii);
                break;

            case 2:
                byte[] rawBytes = new byte[Length * 2];
                for (int i = 0; i < Length; i++)
                {
                    rawBytes[i * 2]     = (byte)(text[i] >> 4);
                    rawBytes[i * 2 + 1] = (byte)(text[i] & 0xf);
                }
                data.SetData(address, rawBytes);
                break;

            default:
                throw new InvalidOperationException($"Can't set a string with bytesPerChar of {bytesPerChar}");
            }
            return(true);
        }
Пример #3
0
        public bool Validate(FixedContainer context, ModuleData data, out string?error)
        {
            var segment = data.GetSegment(GetAddress(context));

            // TODO: Check the length against size?
            if (segment == null)
            {
                error = "No segment containing field present in the module data.";
                return(false);
            }
            return(ValidateData(context, data, out error));
        }
Пример #4
0
        public Container GetOverlaidContainer(FixedContainer context, ModuleData data)
        {
            var switchContainerAddress = context.Address + SwitchContainerOffset;
            var switchContainer        = Schema.LoadableContainersByAddress[switchContainerAddress];
            var switchContext          = new FixedContainer(switchContainer, switchContainerAddress);
            var field = switchContainer.GetField(switchField);

            int index = field switch
            {
                // User samples get an extra overlay at the end.
                InstrumentField instrumentField =>
                instrumentField.GetInstrument(switchContext, data).Group?.Index ?? Schema.InstrumentGroups.Count,
                NumericFieldBase nfb => nfb.GetRawValue(switchContext, data),
                _ => throw new InvalidOperationException($"Invalid switch field type '{field.GetType()}'")
            };

            return(OverlaidContainers[index]);
        }
    }
Пример #5
0
        public override string GetText(FixedContainer context, ModuleData data)
        {
            var address = GetAddress(context);

            byte[] rawBytes = data.GetData(address, Size);
            switch (bytesPerChar)
            {
            case 1:
                return(Encoding.ASCII.GetString(rawBytes));

            case 2:
                byte[] asciiBytes = new byte[Length];
                for (int i = 0; i < Length; i++)
                {
                    asciiBytes[i] = (byte)((rawBytes[i * 2] << 4) | rawBytes[i * 2 + 1]);
                }
                return(Encoding.ASCII.GetString(asciiBytes));

            default:
                throw new InvalidOperationException($"Can't get a string with bytesPerChar of {bytesPerChar}");
            }
        }
Пример #6
0
 /// <summary>
 /// Validates the data against the field. This is called by <see cref="Validate"/>
 /// after performing the common check that the field has a segment in the module data.
 /// Implementations may therefore assume that the data exists.
 /// </summary>
 protected abstract bool ValidateData(FixedContainer context, ModuleData data, [NotNullWhen(false)] out string?error);
Пример #7
0
 public abstract bool TrySetText(FixedContainer context, ModuleData data, string text);
Пример #8
0
 public abstract void Reset(FixedContainer context, ModuleData data);
Пример #9
0
 public abstract string GetText(FixedContainer context, ModuleData data);
Пример #10
0
 protected override bool ValidateData(FixedContainer context, ModuleData data, out string?error)
 {
     // We could potentially validate that it contains non-control-character ASCII...
     error = null;
     return(true);
 }
Пример #11
0
 public override void Reset(FixedContainer context, ModuleData data) => TrySetText(context, data, new string(' ', Length));
Пример #12
0
 public void SetValue(FixedContainer context, ModuleData data, bool value) => SetRawValue(context, data, value ? 1 : 0);
Пример #13
0
 public bool GetValue(FixedContainer context, ModuleData data) => GetRawValue(context, data) == 1;