示例#1
0
        public void CombineTest()
        {
            //Define our people
            Variant people = JSON.Load("[{\"@index\":0,\"name\":\"Jenny\",\"age\":12},{\"@index\":1,\"name\":\"Frank\",\"age\":32}]");

            //Redefine person[1] name to Mike from Frank
            Variant nameOverride = JSON.Load("[{\"@index\":1,\"name\":\"Mike\"}]");

            //Redefine person[1] age to 100 from 32
            Variant ageOverride = JSON.Load("[{\"@index\":1,\"age\":100}]");

            //Merge three variants into one ProxyArray
            ProxyArray peopleArray = Variant.CombineInto <ProxyArray>(people, nameOverride, ageOverride);


            Person[] persons = peopleArray.Make <Person[]>();

            Assert.AreEqual(2, persons.Length);
            Assert.AreEqual("Jenny", persons[0].name);
            Assert.AreEqual(12, persons[0].age);
            Assert.AreEqual("Mike", persons[1].name);
            Assert.AreEqual(100, persons[1].age);

            for (int i = 0; i < persons.Length; i++)
            {
                Console.WriteLine(persons[i].name + " is " + persons[i].age + " years old");
            }
        }
        /// <summary>
        /// Internal lookup. TProxy type is deduced by the caller.
        /// </summary>
        /// <typeparam name="TProbingPolicy">HashTable lookup policy.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <param name="codegenKey"></param>
        /// <param name="slotIndex"></param>
        /// <returns></returns>
        public static SharedConfig <TProxy> Get <TProbingPolicy, TProxy>(
            this SharedConfigMemoryRegion sharedConfigMemoryRegion,
            ICodegenKey codegenKey,
            ref uint slotIndex)
            where TProbingPolicy : IProbingPolicy
            where TProxy : ICodegenProxy, new()
        {
            TProbingPolicy probingPolicy = default;

            uint probingCount = 0;

            slotIndex = 0;

            var configsArray = new UIntArray()
            {
                Buffer = sharedConfigMemoryRegion.Buffer + (int)sharedConfigMemoryRegion.ConfigsArrayOffset
            };

            uint elementCount = configsArray.Count;
            ProxyArray <uint> sharedConfigsOffsets = configsArray.Elements;

            SharedConfig <TProxy> sharedConfig = default;

            while (true)
            {
                slotIndex = probingPolicy.CalculateIndex(codegenKey, ref probingCount, elementCount);

                uint sharedConfigOffsets = sharedConfigsOffsets[(int)slotIndex];

                if (sharedConfigOffsets == 0)
                {
                    // Slot entry is empty.
                    //
                    sharedConfig.Buffer = IntPtr.Zero;
                    break;
                }

                // Compare the object keys.
                // Create a proxy to the shared config.
                //
                sharedConfig.Buffer = sharedConfigMemoryRegion.Buffer + (int)sharedConfigOffsets;

                // Compare key with the proxy.
                //
                bool foundEntry = codegenKey.CodegenTypeIndex() == sharedConfig.Header.CodegenTypeIndex &&
                                  codegenKey.CompareKey(sharedConfig.Config);
                if (foundEntry)
                {
                    break;
                }

                ++probingCount;
            }

            return(sharedConfig);
        }
        /// <summary>
        /// Internal lookup. TProxy type is deduced by the caller.
        /// </summary>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="codegenKey"></param>
        /// <param name="slotIndex"></param>
        /// <returns></returns>
        internal static SharedConfig <TProxy> Get <TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ICodegenKey codegenKey,
            ref uint slotIndex)
            where TProxy : ICodegenProxy, new()
        {
            TProbingPolicy probingPolicy = default;

            uint probingCount = 0;

            slotIndex = 0;

            UIntArray configsArray = sharedConfigDictionary.ConfigsOffsetArray;

            uint elementCount = configsArray.Count;
            ProxyArray <uint> sharedConfigsOffsets = configsArray.Elements;

            SharedConfig <TProxy> sharedConfig = default;

            while (true)
            {
                slotIndex = probingPolicy.CalculateIndex(codegenKey, ref probingCount, elementCount);

                uint offsetToSharedConfig = sharedConfigsOffsets[(int)slotIndex];

                if (offsetToSharedConfig == 0)
                {
                    // Slot entry is empty.
                    //
                    sharedConfig.Buffer = IntPtr.Zero;
                    break;
                }

                // Compare the object keys.
                // Create a proxy to the shared config.
                //
                sharedConfig.Buffer = sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator + (int)offsetToSharedConfig;

                // Compare key with the proxy.
                //
                bool foundEntry = codegenKey.CodegenTypeIndex() == sharedConfig.Header.CodegenTypeIndex &&
                                  codegenKey.CompareKey(sharedConfig.Config);
                if (foundEntry)
                {
                    break;
                }

                ++probingCount;
            }

            return(sharedConfig);
        }
示例#4
0
    protected override void UpdateShot(ProxyArray <Vector3> verts, ProxyArray <Vector2> uvs, SShot shot)
    {
        Vector3 up         = shot.up * width;
        Vector3 rocketBack = shot.pos - shot.velocity / (shot.speed / length);

        //rocket
        verts[0] = rocketBack - up;
        verts[1] = shot.pos - up;
        verts[2] = rocketBack + up;
        verts[3] = shot.pos + up;


        uvs[0] = uvRocket.leftBot;
        uvs[1] = uvRocket.rightBot;
        uvs[2] = uvRocket.leftTop;
        uvs[3] = uvRocket.rightTop;

        /*
         * tris[0] = 0;
         * tris[1] = 2;
         * tris[2] = 1;
         *
         * tris[3] = 2;
         * tris[4] = 3;
         * tris[5] = 1;*/

        //trail

        verts[4] = shot.back - up;
        verts[5] = rocketBack - up;
        verts[6] = shot.back + up;
        verts[7] = rocketBack + up;


        uvs[4] = uvTrail.leftBot;
        uvs[5] = uvTrail.rightBot;
        uvs[6] = uvTrail.leftTop;
        uvs[7] = uvTrail.rightTop;

        /*
         * tris[6] = 4;
         * tris[7] = 6;
         * tris[8] = 5;
         *
         * tris[9] = 6;
         * tris[10] = 7;
         * tris[11] = 5;*/
    }
示例#5
0
        public static string[] MakeStringArray(ProxyArray proxy)
        {
            int count = 0;

            foreach (var item in proxy)
            {
                count++;
            }
            string[] result = new string[count];
            int      i      = 0;

            foreach (var item in proxy)
            {
                result[i] = item;
                i++;
            }
            return(result);
        }
示例#6
0
        public static int[] MakeIntArray(ProxyArray proxy)
        {
            int count = 0;

            foreach (var item in proxy)
            {
                count++;
            }
            int[] result = new int[count];
            int   i      = 0;

            foreach (var item in proxy)
            {
                result[i] = item;
                i++;
            }
            return(result);
        }
示例#7
0
    /// <summary>
    /// 把栈顶的表转为一个ProxyObject或ProxyArray
    /// 转为ProxyArray时,其下标可能和Lua表不一致
    /// </summary>
    public static Variant ToJsonObj(this ILuaState self)
    {
        Variant ret = null;
        var     top = self.GetTop();

        self.PushNil();
        if (self.Next(top))
        {
            var key = self.ToAnyObject(-2) as string;
            if (key != null)
            {
                var obj = new ProxyObject();
                ret = obj;
                var value = ToJsonValue(self);
                obj[key] = value;
                self.Pop(1);
                while (self.Next(top))
                {
                    key      = self.ToString(-2);
                    value    = ToJsonValue(self);
                    obj[key] = value;
                    self.Pop(1);
                }
            }
            else
            {
                var array = new ProxyArray();
                ret = array;
                array.Add(ToJsonValue(self));
                self.Pop(1);
                while (self.Next(top))
                {
                    array.Add(ToJsonValue(self));
                    self.Pop(1);
                }
            }
        }
        else
        {
            return(new ProxyArray());
        }
        return(ret);
    }
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="componentConfig"></param>
        internal static void Add <TType, TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ComponentConfig <TType, TProxy> componentConfig)
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = Get <TProxy>(sharedConfigDictionary, componentConfig.Config, ref slotIndex);

            if (sharedConfig.Buffer != IntPtr.Zero)
            {
                throw new ArgumentException("Config already present", nameof(componentConfig));
            }

            TType config = componentConfig.Config;

            // Calculate size to allocate.
            //
            sharedConfig = sharedConfigDictionary.Allocator.Allocate <SharedConfig <TProxy> >();

            // Update hash map
            //
            ProxyArray <uint> sharedConfigOffsets = sharedConfigDictionary.ConfigsOffsetArray.Elements;

            // #TODO verify.
            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator);

            // Copy header, copy config.
            //
            SharedConfigHeader sharedHeader = sharedConfig.Header;
            TProxy             configProxy  = sharedConfig.Config;

            // Initialize header.
            //
            sharedHeader.ConfigId.Store(1);
            sharedHeader.CodegenTypeIndex = config.CodegenTypeIndex();

            // Copy the config to proxy.
            //
            CodegenTypeExtensions.Serialize(componentConfig.Config, sharedConfig.Config.Buffer);
        }
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TProbingPolicy">HashTable lookup policy.</typeparam>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <param name="componentConfig"></param>
        public static void Add <TProbingPolicy, TType, TProxy>(
            this SharedConfigMemoryRegion sharedConfigMemoryRegion,
            ComponentConfig <TType, TProxy> componentConfig)
            where TProbingPolicy : IProbingPolicy
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = sharedConfigMemoryRegion.Get <TProbingPolicy, TProxy>(componentConfig.Config, ref slotIndex);

            if (sharedConfig.Buffer != IntPtr.Zero)
            {
                throw new ArgumentException("Config already present", nameof(componentConfig));
            }

            TType config = componentConfig.Config;

            // Calculate size to allocate.
            //
            sharedConfig = sharedConfigMemoryRegion.Allocate <SharedConfig <TProxy> >();

            // Update hash map
            //
            ProxyArray <uint> sharedConfigOffsets = sharedConfigMemoryRegion.ConfigsOffsetArray.Elements;

            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigMemoryRegion.Buffer);

            // Copy header, copy config.
            //
            SharedConfigHeader sharedHeader = sharedConfig.Header;
            TProxy             configProxy  = sharedConfig.Config;

            // Initialize header.
            //
            sharedHeader.ConfigId.Store(1);
            sharedHeader.CodegenTypeIndex = config.CodegenTypeIndex();

            // Copy the config to proxy.
            //
            CodegenTypeExtensions.Serialize(componentConfig.Config, sharedConfig.Config.Buffer);
        }
示例#10
0
    protected override void UpdateShot(ProxyArray <Vector3> verts, ProxyArray <Vector2> uvs, SShot shot)
    {
        Vector3 up = shot.up * .75f;

        verts[0] = shot.back - up;
        verts[1] = shot.pos - up;
        verts[2] = shot.back + up;
        verts[3] = shot.pos + up;


        uvs[0] = uvRect.leftBot;
        uvs[1] = uvRect.rightBot;
        uvs[2] = uvRect.leftTop;
        uvs[3] = uvRect.rightTop;

        /*
         * tris[0] = 0;
         * tris[1] = 2;
         * tris[2] = 1;
         *
         * tris[3] = 2;
         * tris[4] = 3;
         * tris[5] = 1;*/
    }
示例#11
0
 protected abstract void UpdateShot(ProxyArray <Vector3> verts, ProxyArray <Vector2> uvs, SShot shot);
示例#12
0
        ProxyArray DecodeArray()
        {
            ProxyArray proxy = new ProxyArray();

            // Ditch opening bracket.
            json.Read();

            // [
            var parsing = true;
            while (parsing)
            {
                Token nextToken = NextToken;

                switch (nextToken)
                {
                    case Token.None:
                        return null;

                    case Token.Comma:
                        continue;

                    case Token.CloseBracket:
                        parsing = false;
                        break;

                    default:
                        proxy.Add( DecodeByToken( nextToken ) );
                        break;
                }
            }

            return proxy;
        }