Exemplo n.º 1
0
 private void Start()
 {
     for (int i = 0; i < 4; i++)
     {
         KeySetter keySetter = Instantiate(this.keySetButton, this.transform, false).GetComponent <KeySetter>();
         keySetter.keyIndex = i;
     }
 }
Exemplo n.º 2
0
        public void StringKeySetterTest()
        {
            var keySetter = new KeySetter <Part>();

            string testValue = "test";
            var    part      = new Part();
            var    prop      = typeof(Part).GetProperty(nameof(part.Description));
            var    key       = prop.GetCustomAttribute <DisplayAttribute>().Name;

            keySetter.SetProperty(key, testValue, part);

            Assert.Equal(testValue, part.Description);
        }
Exemplo n.º 3
0
        public void DateKeySetterTest()
        {
            var keySetter    = new KeySetter <Part>();
            var testDate     = "17.06.96/13:48:10";
            var expectedDate = new DateTime(1996, 6, 17, 13, 48, 10);

            var part = new Part();
            var prop = typeof(Part).GetProperty(nameof(part.TestBegin));
            var key  = prop.GetCustomAttribute <DisplayAttribute>().Name;

            keySetter.SetProperty(key, testDate, part);

            Assert.Equal(expectedDate, part.TestBegin);
        }
Exemplo n.º 4
0
        public Database(string path, KeyValidator <TKey> keyValidator, long offset = 0, long pageSize = 0, bool sortable = true, LoggerInterface logger = null)
        {
            if (typeof(T).GetFields(BindingFlags.NonPublic).Any(f => !f.FieldType.IsValueType))
            {
                throw new Exception("Structure fields can be only value type.");
            }

            valueSize = Marshal.SizeOf <T>();
            fileSize  = new FileInfo(path).Length;

            if ((fileSize - offset) % valueSize != 0)
            {
                throw new Exception("Database is corrupted.");
            }

            validateKey = keyValidator;

            this.sortable = sortable;
            this.pageSize = pageSize - pageSize % valueSize;
            this.offset   = offset;
            this.path     = path;
            this.logger   = logger ?? new LoggerInterface();
            this.progress = new ProgressLogger(this.logger);

            accessor = new MemoryAccessor(path, 0, this.pageSize);

            idIsIndex = typeof(T).GetCustomAttribute <IdIsIndexAttribute>() != null;
            if (!idIsIndex)
            {
                var idField = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).First(f => f.GetCustomAttribute <IdAttribute>() != null);
                if (idField.GetCustomAttribute <FixedBufferAttribute>() != null)
                {
                    if (typeof(TKey) == typeof(string))
                    {
                        getKey = i => (TKey)(object)GetKeyString(accessor, i, idField.GetCustomAttribute <FixedBufferAttribute>().Length);
                        setKey = (TKey key, ref T val) => SetKeyString((string)(object)key, ref val);
                    }
                    else if (typeof(TKey) == typeof(Array))
                    {
                        getKey = i => (TKey)(object)GetKeyArray(accessor, i, idField.GetCustomAttribute <FixedBufferAttribute>().Length, idField.GetCustomAttribute <FixedBufferAttribute>().ElementType);
                        setKey = (TKey key, ref T val) => SetKeyArray((Array)(object)key, ref val);
                    }
                    else
                    {
                        throw new Exception("Fixed array key field should be String or Array.");
                    }
                }
                else
                {
                    getKey = i => GetKey(accessor, i);
                    setKey = (TKey key, ref T val) => SetKey(key, ref val);
                }

                keyOffset = Marshal.OffsetOf <T>(idField.Name).ToInt32();
            }
            else
            {
                getKey    = i => typeof(TKey) == typeof(long) ? (TKey)(object)i : throw new ArgumentException("Index of IdIsIndex table should be long.");
                setKey    = (TKey key, ref T val) => throw new InvalidOperationException("Unable to set key for IdIsIndex database.");
                keyOffset = 0;
            }
        }