Пример #1
0
        public void OnValueStart(ValueInfo info)
        {
            _scope = new ScopeState
            {
                ParentScope        = _scope,
                Container          = _scope == null ? _targetRootContainer : null,
                IsDynamicContainer = _scope == null && _targetRootContainer is ValueContainer.ValueContainer
            };
            _scope.ValueInfo = info;

            if (info.Type != null)
            {
                _scope.Type = _typeSerializerHelper.ResolveType(info.Type);
                if (_scope.Container == null)
                {
                    _scope.Composer = _composerSelector.SelectComposerOrPoco(_scope.Type);
                    if (_scope.Composer == null)
                    {
                        throw new UnserializableTypeException(_scope.Type);
                    }
                    _scope.Container = _scope.Composer.CreatePropertySet(_scope.Type);
                }
            }

            if (info.IsCollection)
            {
                _scope.ItemType = info.ItemType != null
                    ? _typeSerializerHelper.ResolveType(info.ItemType)
                    : typeof(object);

                _scope.Array         = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(_scope.ItemType));
                _scope.Value         = _scope.Array;
                _scope.ValueReceived = true;
            }
        }
Пример #2
0
        public void Stop()
        {
            try
            {
                foreach (KeyValuePair <string, Agent> item in agents)
                {
                    item.Value.Halt();
                }
                mainSock.Close();

                mainThread.Abort();

                _scopeState = ScopeState.Stopped;
            }
            catch (Exception e)
            {
                if (OnExceptionThrown != null)
                {
                    OnExceptionThrown(this, e);
                }
                else
                {
                    throw e;
                }
            }
        }
Пример #3
0
 public TrackableScope(string name, ScopeState state = ScopeState.Do)
 {
     _state = state;
     // if this isn't a 'sub' scope, then save it off to add to appropriate stack
     if (Accumulator.CurrentOrNew(out var acc, name))
     {
         Accumulator = acc;
     }
 }
Пример #4
0
        public Scope Start(bool singleThread = false)
        {
            try
            {
                _scopeState = ScopeState.Started;
                foreach (KeyValuePair <string, Agent> item in agents)
                {
                    item.Value.Start();
                }

                if (singleThread)
                {
                    OnStart?.Invoke();
                    Listen();
                    OnStop?.Invoke();
                    return(this);
                }
                else
                {
                    ThreadStart starter = Listen;
                    starter += () =>
                    {
                        _scopeState = ScopeState.Stopped;
                        if (OnStop != null)
                        {
                            new Thread(new ThreadStart(OnStop));
                        }
                    };
                    mainThread = new Thread(starter)
                    {
                        IsBackground = true
                    };
                    mainThread.Start();
                    _scopeState = ScopeState.Started;
                    if (OnStart != null)
                    {
                        new Thread(new ThreadStart(OnStart));
                    }
                    return(this);
                }
            }
            catch (Exception e)
            {
                if (OnExceptionThrown != null)
                {
                    OnExceptionThrown(this, e);
                    return(null);
                }
                else
                {
                    throw e;
                }
            }
        }
        public void Add(Accumulator scope, ScopeState state)
        {
            switch (state)
            {
            case ScopeState.Do:
                _redoables.Clear();
                _undoables.Push(scope);
                break;

            case ScopeState.Undo:
                _undoables.Push(scope);
                break;

            case ScopeState.Redo:
                _redoables.Push(scope);
                break;
            }
        }
Пример #6
0
        public void OnValueEnd()
        {
            if (_scope.ParentScope != null &&
                _scope.ParentScope.Composer == null &&
                _scope.ParentScope.Container == null)
            {
                _scope.ParentScope.IsDynamicContainer = true;
            }

            if (!_scope.ValueReceived && _scope.Composer != null)
            {
                _scope.Value         = _scope.Composer.Compose(_scope.Container, _scope.Type);
                _scope.ValueReceived = true;
            }

            if (!_scope.ValueReceived && _scope.IsDynamicContainer)
            {
                _scope.Value         = _scope.Container;
                _scope.ValueReceived = true;
            }

            if (!_scope.ValueReceived && !_scope.ValueInfo.IsCollection && _scope.Container == null)
            {
                if (_scope.ValueInfo.ReferenceId.HasValue)
                {
                    if (!_objectByIdMap.TryGetValue(_scope.ValueInfo.ReferenceId.Value, out _scope.Value))
                    {
                        throw new InvalidOperationException($"Could not find object by ID '{_scope.ValueInfo.ReferenceId.Value}'.");
                    }
                    _scope.ValueReceived = true;
                }
                else if (_scope.ValueInfo.SpecialId != null)
                {
                    if (_objectByNameMap == null || !_objectByNameMap.TryGetValue(_scope.ValueInfo.SpecialId, out _scope.Value))
                    {
                        throw new InvalidOperationException($"Could not find object by ID '{_scope.ValueInfo.SpecialId}'.");
                    }
                    _scope.ValueReceived = true;
                }
            }

            var valueScope = _scope;

            _scope = _scope.ParentScope;

            if (valueScope.ValueReceived && _scope != null)
            {
                var value = valueScope.Value;

#warning Support various collection types. Currently supports arrays only.
                if (valueScope.ValueInfo.IsCollection)
                {
                    value = this.GetType()
                            .GetMethod(nameof(ToArray), BindingFlags.Static | BindingFlags.NonPublic)
                            .MakeGenericMethod(value.GetType().GetGenericArguments()[0])
                            .Invoke(null, new object[] { value });
                }

                Type targetType;
                if (_scope.ValueInfo.IsCollection)
                {
                    targetType = _scope.ItemType;
                }
                else if (_scope.IsDynamicContainer)
                {
                    targetType = value?.GetType();
                }
                else
                {
                    var startIndex = _scope.Index + 1;

                    _scope.Index = FindIndex(_scope.Container, valueScope.ValueInfo.Name, startIndex);

                    // Try to find by value type if the argument was renamed.
                    if (_scope.Index < 0 && value != null)
                    {
                        _scope.Index = FindIndex(_scope.Container, value.GetType());
                    }

                    if (_scope.Index >= 0)
                    {
                        targetType = _scope.Container.GetType(_scope.Index);
                    }
                    else
                    {
                        // TODO: better skip logic?
                        value        = null;
                        targetType   = null;
                        _scope.Index = startIndex - 1;
                    }
                }

                if (value != null && targetType != null)
                {
                    value = EnsureValueType(targetType, value);

                    if (valueScope.ValueInfo.ReferenceId.HasValue)
                    {
                        _objectByIdMap[valueScope.ValueInfo.ReferenceId.Value] = value;
                    }

                    if (_scope.ValueInfo.IsCollection)
                    {
                        _scope.Array.Add(value);
                    }
                    else if (_scope.IsDynamicContainer)
                    {
                        var dynamicContainer = _scope.Container as ValueContainer.ValueContainer;
                        if (dynamicContainer == null)
                        {
                            dynamicContainer = new ValueContainer.ValueContainer();
                            _scope.Container = dynamicContainer;
                        }
                        dynamicContainer.Add(valueScope.ValueInfo.Name, targetType, value);
                    }
                    else
                    {
                        _scope.Container.SetValue(_scope.Index, value);
                    }
                }
            }
        }
Пример #7
0
        public override void Update()
        {
            if (AimSprite == null)
            {
                AimSprite   = new GraphSprite(PulseAimImg, null);
                AimSprite.X = X;
                AimSprite.Y = Y;
                Graph.Add(AimSprite);
                aimLight         = new GraphLight();
                aimLight.Diffuse = new OpenTK.Vector3(0, 0.2f, 0.2f);
                aimLight.Range   = 150;

                Graph.Add(aimLight);
            }
            else
            {
                if (usePad)
                {
                    float xi = XIn.rightX();
                    float yi = XIn.rightY();

                    float dis = (float)Math.Sqrt((xi * xi) + (yi * yi));

                    if (dis > 0.5)
                    {
                        float ang = (float)Math.Atan2(-yi, xi);

                        aimAngle = OpenTK.MathHelper.RadiansToDegrees(ang);


                        //aimAngle = ang;


                        AimSprite.Rot = aimAngle;

                        float xo = (float)Math.Cos(ang);
                        float yo = (float)Math.Sin(ang);

                        xo = xo * 64;
                        yo = yo * 64;


                        AimSprite.X = X + xo;
                        AimSprite.Y = Y + yo;
                        aimLight.X  = AimSprite.X;
                        aimLight.Y  = AimSprite.Y;
                    }
                }
                else
                {
                    float rx = RealX;
                    float ry = RealY;

                    float mx = Vivid.Input.Input.MX;
                    float my = Vivid.Input.Input.MY;

                    float ang = (float)Math.Atan2(my - ry, mx - rx);

                    aimAngle = ang;

                    AimSprite.Rot = OpenTK.MathHelper.RadiansToDegrees(aimAngle);

                    //Console.WriteLine("Rot:" + AimSprite.Rot);

                    float xo = (float)Math.Cos(aimAngle);
                    float yo = (float)Math.Sin(aimAngle);

                    xo = xo * 64;
                    yo = yo * 64;


                    AimSprite.X = X + xo;
                    AimSprite.Y = Y + yo;
                    aimLight.X  = AimSprite.X;
                    aimLight.Y  = AimSprite.Y;
                }
            }


            if (!changedSlot)
            {
                if (XIn.DLeft() || Vivid.Input.Input.KeyIn(OpenTK.Input.Key.Q))
                {
                    CurSlot--;
                    if (CurSlot < 0)
                    {
                        if (Slots[2] == null)
                        {
                            CurSlot = 1;
                        }
                        else
                        {
                            CurSlot = 2;
                        }
                    }
                    changedSlot = true;
                    RebuildWeaponHud();
                    Songs.PlaySource(ChangeSrc, false);
                }
                if (XIn.DRight() || Vivid.Input.Input.KeyIn(OpenTK.Input.Key.E))
                {
                    CurSlot++;
                    if (CurSlot == 3)
                    {
                        CurSlot = 0;
                    }
                    if (Slots[CurSlot] == null)
                    {
                        CurSlot = 0;
                    }
                    changedSlot = true;
                    RebuildWeaponHud();
                    Songs.PlaySource(ChangeSrc, false);
                }
            }
            else
            {
                if (XIn.DLeft() == false && XIn.DRight() == false && Input.KeyIn(OpenTK.Input.Key.Q) == false && Input.KeyIn(OpenTK.Input.Key.E) == false)
                {
                    changedSlot = false;
                }
            }

            switch (State)
            {
            case ScopeState.Idle:
                if (CurAnim != Anims["Idle1"])
                {
                    SetAnim("Idle1");
                }
                break;

            case ScopeState.Walking:
                if (CurAnim != Anims["Walk1"])
                {
                    SetAnim("Walk1");
                }
                break;

            case ScopeState.Running:
                if (CurAnim != Anims["Run1"])
                {
                    SetAnim("Run1");
                }
                break;
            }

            base.Update();
            ImgFrame = GetAnimFrame();

            float xm = XIn.LeftX();

            if (Vivid.Input.Input.MB[0] && !ShotAlready && Environment.TickCount > (lastShot + 800))
            {
                var shotSpr = Slots[CurSlot].GetNew();
                shotSpr.X = AimSprite.X;
                shotSpr.Y = AimSprite.Y;
                Graph.Add(shotSpr);
                Graph.Add(shotSpr.Light1);

                shotSpr.XM = (float)Math.Cos(aimAngle);
                shotSpr.YM = (float)Math.Sin(aimAngle);

                ShotAlready = true;
                lastShot    = Environment.TickCount;
            }
            if (Vivid.Input.Input.MB[0] == false)
            {
                ShotAlready = false;
            }

            if (Vivid.Input.Input.KeyIn(OpenTK.Input.Key.A))
            {
                xm = -1;
            }

            if (Vivid.Input.Input.KeyIn(OpenTK.Input.Key.D))
            {
                xm = 1;
            }


            xm = xm * 0.4f;

            if (XIn.bX() || Input.KeyIn(OpenTK.Input.Key.Space))
            {
                if (onGround && Environment.TickCount > (lastJump + 500))
                {
                    Move2D(0, -10);
                    onGround = false;
                    //State = ScopeState.Jumping;
                    Y = Y - 10;

                    lastJump = Environment.TickCount;
                }
            }

            if (XIn.leftB() || Input.KeyIn(OpenTK.Input.Key.ShiftLeft))
            {
                xm = xm * 2;

                if (Math.Abs(xm) > 0)
                {
                    State = ScopeState.Running;
                }
            }
            else
            {
                if (Math.Abs(xm) > 0)
                {
                    State = ScopeState.Walking;
                }
                else
                {
                    State = ScopeState.Idle;
                }
            }

            if (Math.Abs(xm) < 0.2)
            {
                State = ScopeState.Idle;
            }
            else
            {
                //State = ScopeState.Walking;
            }

            Move2D(xm * 0.35f, 0);

            if (xm > 0)
            {
                FlipDrawX = false;
            }
            else if (xm < 0)
            {
                FlipDrawX = true;
            }

            //X = X + xm;
            var cm = Graph.CreateCollisionMap(0.05f);


            Graph.StoreCam();
            Graph.X   = 0;
            Graph.Y   = 0;
            Graph.Z   = 1;
            Graph.Rot = 0;
            float cx = this.RealX;
            float cy = this.RealY;

            Graph.RestoreCam();


            if (Environment.TickCount > (lastJump + 500))
            {
                var hit = cm.RayCast(cx, cy, cx, cy + 32);

                if (hit == null)
                {
                    //X = hit.HitX;
                    Move2D(0, 0.3f);
                    onGround = false;
                }
                else
                {
                    //Y =
                    //Y = hit.HitY - 40;
                    Yi       = 0;
                    onGround = true;

                    /*
                     * Environment.Exit(1);
                     * float dis = hit.HitY - 32;
                     *
                     * dis = Math.Abs((dis - Y));
                     *
                     *
                     * if (dis < 32)
                     * {
                     *
                     *  Yi = 0;
                     *  Y = cy; // hit.HitY - 32 * Graph.Z;
                     *  onGround = true;
                     * }
                     */
                }
            }

            //Rot += 0.1f;

            //vironment.Exit(1);
            //base.Update();
        }
Пример #8
0
        public void OnValueEnd()
        {
            if (_scope.ParentScope != null &&
                _scope.ParentScope.Composer == null &&
                _scope.ParentScope.Container == null)
            {
                _scope.ParentScope.IsDynamicContainer = true;
            }

            if (!_scope.ValueReceived && _scope.Composer != null)
            {
                _scope.Value         = _scope.Composer.Compose(_scope.Container, _scope.Type);
                _scope.ValueReceived = true;
            }

            if (!_scope.ValueReceived && _scope.IsDynamicContainer)
            {
                _scope.Value         = _scope.Container;
                _scope.ValueReceived = true;
            }

            if (!_scope.ValueReceived)
            {
                if (_scope.ValueInfo.ReferenceId.HasValue)
                {
                    if (!_objectByIdMap.TryGetValue(_scope.ValueInfo.ReferenceId.Value, out _scope.Value))
                    {
                        throw new InvalidOperationException($"Could not find object by ID '{_scope.ValueInfo.ReferenceId.Value}'.");
                    }
                    _scope.ValueReceived = true;
                }
                else if (_scope.ValueInfo.SpecialId != null)
                {
                    if (_objectByNameMap == null || !_objectByNameMap.TryGetValue(_scope.ValueInfo.SpecialId, out _scope.Value))
                    {
                        throw new InvalidOperationException($"Could not find object by ID '{_scope.ValueInfo.SpecialId}'.");
                    }
                    _scope.ValueReceived = true;
                }
            }

            var valueScope = _scope;

            _scope = _scope.ParentScope;

            if (valueScope.ValueReceived)
            {
                var value = valueScope.Value;

#warning Support various collection types. Currently supports arrays only.
                if (valueScope.ValueInfo.IsCollection)
                {
                    value = this.GetType()
                            .GetMethod(nameof(ToArray), BindingFlags.Static | BindingFlags.NonPublic)
                            .MakeGenericMethod(value.GetType().GetGenericArguments()[0])
                            .Invoke(null, new object[] { value });
                }

                Type targetType;
                if (_scope.ValueInfo.IsCollection)
                {
                    targetType = _scope.ItemType;
                }
                else if (_scope.IsDynamicContainer)
                {
                    targetType = value?.GetType();
                }
                else
                {
                    _scope.Index = FindIndex(_scope.Container, valueScope.ValueInfo.Name, _scope.Index + 1);
                    if (_scope.Index >= 0)
                    {
                        targetType = _scope.Container.GetType(_scope.Index);
                    }
                    else
                    {
                        // TODO: better skip logic?
                        value      = null;
                        targetType = null;
                    }
                }

                if (value != null && targetType != null)
                {
                    // TODO: converter
                    if (!targetType.IsAssignableFrom(value.GetType()))
                    {
                        if (targetType.IsArray || ((value is IList) && targetType == typeof(object)))
                        {
#warning This byte[] conversion is a quick-fix. Do it properly by adding type during serialization?
                            if (targetType == typeof(byte[]) && value is string)
                            {
                                value = Convert.FromBase64String((string)value);
                            }
                            else
                            {
                                value = this.GetType()
                                        .GetMethod(nameof(ToArray), BindingFlags.Static | BindingFlags.NonPublic)
                                        .MakeGenericMethod(targetType.GetElementType() ?? value.GetType().GetGenericArguments()[0])
                                        .Invoke(null, new object[] { value });
                            }
                        }
                        else if (targetType.IsEnum())
                        {
                            value = Enum.ToObject(targetType, value);
                        }
                        else if (targetType == typeof(Guid) && value is string strGuid)
                        {
                            value = Guid.Parse(strGuid);
                        }
                        else if (targetType is Type && value is TypeSerializationInfo typeInfo)
                        {
                            value = ResolveType(typeInfo);
                        }
                        else
                        {
                            value = Convert.ChangeType(value, targetType);
                        }
                    }

                    if (valueScope.ValueInfo.ReferenceId.HasValue)
                    {
                        _objectByIdMap[valueScope.ValueInfo.ReferenceId.Value] = value;
                    }

                    if (_scope.ValueInfo.IsCollection)
                    {
                        _scope.Array.Add(value);
                    }
                    else if (_scope.IsDynamicContainer)
                    {
                        var dynamicContainer = _scope.Container as ValueContainer.ValueContainer;
                        if (dynamicContainer == null)
                        {
                            dynamicContainer = new ValueContainer.ValueContainer();
                            _scope.Container = dynamicContainer;
                        }
                        dynamicContainer.Add(valueScope.ValueInfo.Name, targetType, value);
                    }
                    else
                    {
                        _scope.Container.SetValue(_scope.Index, value);
                    }
                }
            }
        }
Пример #9
0
 public ServiceProviderEngineScope(ServiceProviderEngine engine)
 {
     Engine = engine;
     _state = new ScopeState();
 }
Пример #10
0
 public ServiceProviderEngineScope(ServiceProviderEngine engine, bool isRoot = false)
 {
     Engine = engine;
     _state = new ScopeState(isRoot);
 }