Пример #1
0
 public BuiltinFieldInfo(ReflectedField value, ProjectState projectState)
     : base(new LazyDotNetDict(ClrModule.GetPythonType(value.FieldType), projectState, true))
 {
     _value = value;
     _doc   = null;
     _type  = ClrModule.GetPythonType(value.FieldType);
 }
Пример #2
0
        internal static PythonTypeSlot GetReflectedField(FieldInfo info)
        {
            PythonTypeSlot res;

            NameType nt = NameType.Field;

            if (!PythonBinder.IsExtendedPythonType(info.DeclaringType) &&
                !PythonBinder.IsPythonSupportingType(info.DeclaringType) &&
                !PythonHiddenAttribute.IsHidden(info))
            {
                nt |= NameType.PythonField;
            }

            lock (_fieldCache) {
                if (!_fieldCache.TryGetValue(info, out res))
                {
                    if (nt == NameType.PythonField && info.IsLiteral)
                    {
                        if (info.FieldType == typeof(int))
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                ScriptingRuntimeHelpers.Int32ToObject((int)info.GetRawConstantValue()),
                                true
                                );
                        }
                        else if (info.FieldType == typeof(bool))
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                ScriptingRuntimeHelpers.BooleanToObject((bool)info.GetRawConstantValue()),
                                true
                                );
                        }
                        else
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                info.GetValue(null),
                                true
                                );
                        }
                    }
                    else
                    {
                        res = new ReflectedField(info, nt);
                    }

                    _fieldCache[info] = res;
                }
            }

            return(res);
        }
Пример #3
0
        /// <summary>
        /// Determines if a type member can be imported.  This is used to treat static types like modules.
        /// </summary>
        private static bool IsStaticTypeMemberInAll(CodeContext /*!*/ context, PythonType /*!*/ pt, string name, out object res)
        {
            PythonTypeSlot pts;

            res = null;
            if (pt.TryResolveSlot(context, name, out pts))
            {
                if (name == "__doc__" || name == "__class__")
                {
                    // these exist but we don't want to clobber __doc__ on import * or bring in __class__
                    return(false);
                }
                else if (pts is ReflectedGetterSetter)
                {
                    // property or indexer, these fetch the value at runtime, the user needs to explicitly
                    // import them using from type import property
                    return(false);
                }

                ReflectedField rf = pts as ReflectedField;
                if (rf != null && !rf._info.IsInitOnly && !rf._info.IsLiteral)
                {
                    // only bring in read-only fields, if the value can change the user needs to explicitly
                    // import by name
                    return(false);
                }

                BuiltinMethodDescriptor method = pts as BuiltinMethodDescriptor;
                if (method != null && (!method.DeclaringType.IsSealed() || !method.DeclaringType.IsAbstract()))
                {
                    // inherited object member on a static class (GetHashCode, Equals, etc...)
                    return(false);
                }

                BuiltinFunction bf = pts as BuiltinFunction;
                if (bf != null && (!bf.DeclaringType.IsSealed() || !bf.DeclaringType.IsAbstract()))
                {
                    // __new__/ReferenceEquals inherited from object
                    return(false);
                }

                if (pts.TryGetValue(context, null, pt, out res))
                {
                    return(true);
                }
            }

            res = null;
            return(false);
        }
Пример #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ReflectedField <bool> isValid = null;

            if (!_isValidCache.ContainsKey(property.propertyPath))
            {
                property.FindMethodRelative("OnValidate").Invoke();
                isValid = property.FindField <bool>("m_IsValid");
                _isValidCache[property.propertyPath] = isValid;
            }
            else
            {
                isValid = _isValidCache[property.propertyPath];
            }

            EditorGUI.BeginChangeCheck();
            {
                const float        BUTTON_WIDTH = 25;
                SerializedProperty relativePath = property.FindPropertyRelative("m_RelativePath");
                SerializedProperty enabled      = property.FindPropertyRelative("m_Enabled");
                position.width -= BUTTON_WIDTH;
                if (!isValid.value)
                {
                    GUI.Box(position, m_MissingAssemblyLabel);
                }

                EditorGUI.SelectableLabel(position, relativePath.stringValue, EditorStyles.textArea);
                position.x       += position.width;
                position.width    = BUTTON_WIDTH;
                enabled.boolValue = EditorGUI.Toggle(position, enabled.boolValue);
            }
            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }
        }
Пример #5
0
        internal IMember MakeObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            lock (this) {
                IMember res;
                if (!_members.TryGetValue(obj, out res))
                {
                    PythonModule mod = obj as PythonModule;
                    if (mod != null)
                    {
                        // FIXME: name
                        object name;
                        if (!mod.Get__dict__().TryGetValue("__name__", out name) || !(name is string))
                        {
                            name = "";
                        }
                        _members[obj] = res = new IronPythonModule(this, mod, (string)name);
                    }

                    PythonType type = obj as PythonType;
                    if (type != null)
                    {
                        _members[obj] = res = GetTypeFromType(type.__clrtype__());
                    }

                    BuiltinFunction func = obj as BuiltinFunction;
                    if (func != null)
                    {
                        _members[obj] = res = new IronPythonBuiltinFunction(this, func);
                    }

                    BuiltinMethodDescriptor methodDesc = obj as BuiltinMethodDescriptor;
                    if (methodDesc != null)
                    {
                        _members[obj] = res = new IronPythonBuiltinMethodDescriptor(this, methodDesc);
                    }

                    ReflectedField field = obj as ReflectedField;
                    if (field != null)
                    {
                        return(new IronPythonField(this, field));
                    }

                    ReflectedProperty prop = obj as ReflectedProperty;
                    if (prop != null)
                    {
                        _members[obj] = res = new IronPythonProperty(this, prop);
                    }

                    ReflectedExtensionProperty extProp = obj as ReflectedExtensionProperty;
                    if (extProp != null)
                    {
                        _members[obj] = res = new IronPythonExtensionProperty(this, extProp);
                    }

                    NamespaceTracker ns = obj as NamespaceTracker;
                    if (ns != null)
                    {
                        _members[obj] = res = new IronPythonNamespace(this, ns);
                    }

                    Method method = obj as Method;
                    if (method != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, method, PythonMemberType.Method);
                    }

                    var classMethod = obj as ClassMethodDescriptor;
                    if (classMethod != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, classMethod, PythonMemberType.Method);
                    }

                    var typeSlot = obj as PythonTypeTypeSlot;
                    if (typeSlot != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, typeSlot, PythonMemberType.Property);
                    }

                    ReflectedEvent eventObj = obj as ReflectedEvent;
                    if (eventObj != null)
                    {
                        return(new IronPythonEvent(this, eventObj));
                    }

                    if (res == null)
                    {
                        var genericTypeSlot = obj as PythonTypeSlot;
                        if (genericTypeSlot != null)
                        {
                            _members[obj] = res = new IronPythonGenericMember(this, genericTypeSlot, PythonMemberType.Property);
                        }
                    }

                    TypeGroup tg = obj as TypeGroup;
                    if (tg != null)
                    {
                        _members[obj] = res = new PythonObject <TypeGroup>(this, tg);
                    }

                    var attrType = (obj != null) ? obj.GetType() : typeof(DynamicNull);
                    if (attrType == typeof(bool) || attrType == typeof(int) || attrType == typeof(Complex) ||
                        attrType == typeof(string) || attrType == typeof(long) || attrType == typeof(double) ||
                        attrType.IsEnum || obj == null)
                    {
                        _members[obj] = res = new IronPythonConstant(this, obj);
                    }

                    if (res == null)
                    {
                        Debug.Assert(!(obj is bool));
                        _members[obj] = res = new PythonObject <object>(this, obj);
                    }
                }

                return(res);
            }
        }
Пример #6
0
        public CustomBobberBar(Farmer user, int whichFish, float fishSize, bool treasure, int bobber) : base(whichFish, fishSize, treasure, bobber)
        {
            this.User        = user;
            this._origStreak = ModFishing.Instance.Api.GetStreak(user);
            this._origFish   = whichFish;

            /* Private field hooks */
            this._treasure            = new ReflectedField <BobberBar, bool>(this, "treasure");
            this._treasure            = new ReflectedField <BobberBar, bool>(this, "treasure");
            this._treasureCaught      = new ReflectedField <BobberBar, bool>(this, "treasureCaught");
            this._treasurePosition    = new ReflectedField <BobberBar, float>(this, "treasurePosition");
            this._treasureAppearTimer = new ReflectedField <BobberBar, float>(this, "treasureAppearTimer");
            this._treasureScale       = new ReflectedField <BobberBar, float>(this, "treasureScale");

            this._distanceFromCatching = new ReflectedField <BobberBar, float>(this, "distanceFromCatching");
            this._treasureCatchLevel   = new ReflectedField <BobberBar, float>(this, "treasureCatchLevel");

            this._bobberBarPos    = new ReflectedField <BobberBar, float>(this, "bobberBarPos");
            this._bobberInBar     = new ReflectedField <BobberBar, bool>(this, "bobberInBar");
            this._difficulty      = new ReflectedField <BobberBar, float>(this, "difficulty");
            this._fishQuality     = new ReflectedField <BobberBar, int>(this, "fishQuality");
            this._perfect         = new ReflectedField <BobberBar, bool>(this, "perfect");
            this._scale           = new ReflectedField <BobberBar, float>(this, "scale");
            this._flipBubble      = new ReflectedField <BobberBar, bool>(this, "flipBubble");
            this._bobberBarHeight = new ReflectedField <BobberBar, int>(this, "bobberBarHeight");
            this._reelRotation    = new ReflectedField <BobberBar, float>(this, "reelRotation");
            this._bobberPosition  = new ReflectedField <BobberBar, float>(this, "bobberPosition");
            this._bossFish        = new ReflectedField <BobberBar, bool>(this, "bossFish");
            this._motionType      = new ReflectedField <BobberBar, int>(this, "motionType");
            this._fishSize        = new ReflectedField <BobberBar, int>(this, "fishSize");
            this._whichFish       = new ReflectedField <BobberBar, int>(this, "whichFish");

            this._barShake        = new ReflectedField <BobberBar, Vector2>(this, "barShake");
            this._fishShake       = new ReflectedField <BobberBar, Vector2>(this, "fishShake");
            this._treasureShake   = new ReflectedField <BobberBar, Vector2>(this, "treasureShake");
            this._everythingShake = new ReflectedField <BobberBar, Vector2>(this, "everythingShake");

            this._sparkleText = new ReflectedField <BobberBar, SparklingText>(this, "sparkleText");

            this._lastDistanceFromCatching = this._distanceFromCatching.Value;
            this._lastTreasureCatchLevel   = this._treasureCatchLevel.Value;

            /* Actual code */
            ConfigMain  config = ModFishing.Instance.MainConfig;
            IFishTraits traits = ModFishing.Instance.Api.GetFishTraits(whichFish);

            // Check if fish is unaware
            this.Unaware = Game1.random.NextDouble() < ModFishing.Instance.Api.GetUnawareChance(user, whichFish);

            // Applies difficulty modifier, including if fish is unaware
            float difficulty = traits?.Difficulty ?? this._difficulty.Value;

            difficulty *= config.DifficultySettings.BaseDifficultyMult;
            difficulty *= 1F + this._origStreak * config.DifficultySettings.DifficultyStreakEffect;
            if (this.Unaware)
            {
                difficulty *= config.UnawareSettings.UnawareMult;
                Game1.showGlobalMessage(ModFishing.Translate("text.unaware", ModFishing.Translate("text.percent", 1F - config.UnawareSettings.UnawareMult)));
            }
            this._difficulty.Value = difficulty;

            // Adjusts additional traits about the fish
            if (traits != null)
            {
                this._motionType.Value = (int)traits.MotionType;
                this._fishSize.Value   = traits.MinSize + (int)((traits.MaxSize - traits.MinSize) * fishSize) + 1;
            }

            // Adjusts quality to be increased by streak
            int fishQuality = this._fishQuality.Value;

            this._origQuality = fishQuality;
            int qualityBonus = (int)Math.Floor((double)this._origStreak / config.StreakSettings.StreakForIncreasedQuality);

            fishQuality = Math.Min(fishQuality + qualityBonus, 3);
            if (fishQuality == 3)
            {
                fishQuality++;                   // Iridium-quality fish. Only possible through your perfect streak
            }
            this._fishQuality.Value = fishQuality;

            // Increase the user's perfect streak (this will be dropped to 0 if they don't get a perfect catch)
            if (this._origStreak >= config.StreakSettings.StreakForIncreasedQuality)
            {
                this._sparkleText.Value = new SparklingText(Game1.dialogueFont, ModFishing.Translate("text.streak", this._origStreak), Color.Yellow, Color.White);
            }
        }