Ejemplo n.º 1
0
 public void Dispose(bool disposeChildren)
 {
     if (disposeChildren && Texture != null)
     {
         if (!Texture.IsDisposed)
         {
             Texture.Dispose();
         }
         Texture = null;
     }
     Dispose();
 }
Ejemplo n.º 2
0
        public override void Entry(IModHelper help)
        {
            AssemblyPath = help.DirectoryPath;

            var ConfigPath = Path.Combine(help.DirectoryPath, ConfigName);

            using (var tempStream = new MemoryStream()) {
                SerializeConfig.Save(tempStream);

                if (!Config.IgnoreConfig)
                {
                    SerializeConfig.Load(ConfigPath);
                }

                if (IsVersionOutdated(Config.ConfigVersion))
                {
                    Debug.WarningLn("config.toml is out of date, rewriting it.");
                    SerializeConfig.Load(tempStream);
                    Config.ConfigVersion = Config.CurrentVersion;
                }
            }

            if (Config.ShowIntroMessage && !Config.SkipIntro)
            {
                help.Events.GameLoop.GameLaunched += (_, _1) => {
                    Game1.drawLetterMessage("Welcome to SpriteMaster!\nSpriteMaster must resample sprites as it sees them and thus some lag will likely be apparent at the start of the game, upon entering new areas, and when new sprites are seen.\n\nPlease be patient and do not take this as an indication that your computer is incapable of running SpriteMaster.\n\nEnjoy!".Replace("\n", "^"));
                };
                Config.ShowIntroMessage = false;
            }

            SerializeConfig.Save(ConfigPath);

            ConfigureHarmony();
            help.Events.Input.ButtonPressed += OnButtonPressed;

            help.ConsoleCommands.Add("spritemaster_stats", "Dump SpriteMaster Statistics", (_, _1) => { ManagedTexture2D.DumpStats(); });
            help.ConsoleCommands.Add("spritemaster_memory", "Dump SpriteMaster Memory", (_, _1) => { Debug.DumpMemory(); });

            //help.ConsoleCommands.Add("night", "make it dark", (_, _1) => { help.ConsoleCommands.Trigger("world_settime", new string[] { "2100" }); });

            help.Events.GameLoop.DayStarted += OnDayStarted;
            // GC after major events
            help.Events.GameLoop.SaveLoaded += (_, _1) => Garbage.Collect(compact: true, blocking: true, background: false);

            if (MemoryPressureThread != null)
            {
                MemoryPressureThread.Start();
            }
            if (GarbageCollectThread != null)
            {
                GarbageCollectThread.Start();
            }
        }
Ejemplo n.º 3
0
        internal ScaledTexture(string assetName, SpriteInfo textureWrapper, Bounds sourceRectangle, ulong hash, bool isSprite, bool async, int expectedScale)
        {
            IsSprite = isSprite;
            Hash     = hash;
            var source = textureWrapper.Reference;

            this.OriginalSourceRectangle = new Bounds(sourceRectangle);
            this.Reference       = source.MakeWeak();
            this.sourceRectangle = sourceRectangle;
            this.refScale        = expectedScale;
            SpriteMap.Add(source, this, sourceRectangle, expectedScale);

            this.Name    = source.Name.IsBlank() ? assetName : source.SafeName();
            originalSize = IsSprite ? sourceRectangle.Extent : new Vector2I(source);

            if (async && Config.AsyncScaling.Enabled)
            {
                ThreadPool.QueueUserWorkItem((object wrapper) => {
                    Thread.CurrentThread.Priority = ThreadPriority.Lowest;
                    Thread.CurrentThread.Name     = "Texture Resampling Thread";
                    Upscaler.Upscale(
                        texture: this,
                        scale: ref refScale,
                        input: (SpriteInfo)wrapper,
                        desprite: IsSprite,
                        hash: Hash,
                        wrapped: ref Wrapped,
                        async: true
                        );
                    // If the upscale fails, the asynchronous action on the render thread automatically cleans up the ScaledTexture.
                }, textureWrapper);
            }
            else
            {
                // TODO store the HD Texture in _this_ object instead. Will confuse things like subtexture updates, though.
                this.Texture = Upscaler.Upscale(
                    texture: this,
                    scale: ref refScale,
                    input: textureWrapper,
                    desprite: IsSprite,
                    hash: Hash,
                    wrapped: ref Wrapped,
                    async: false
                    );

                if (this.Texture != null)
                {
                    Finish();
                }
                else
                {
                    Dispose();
                    return;
                }
            }

            // TODO : I would love to dispose of this texture _now_, but we rely on it disposing to know if we need to dispose of ours.
            // There is a better way to do this using weak references, I just need to analyze it further. Memory leaks have been a pain so far.
            source.Disposing += (object sender, EventArgs args) => { OnParentDispose((Texture2D)sender); };

            lock (MostRecentList) {
                CurrentRecentNode = MostRecentList.AddFirst(this.MakeWeak());
            }
        }
Ejemplo n.º 4
0
        public override void Entry(IModHelper help)
        {
            AssemblyPath = help.DirectoryPath;

            var ConfigPath = Path.Combine(help.DirectoryPath, ConfigName);

            using (var tempStream = new MemoryStream()) {
                SerializeConfig.Save(tempStream);

                SerializeConfig.Load(ConfigPath);

                if (IsVersionOutdated(Config.ConfigVersion))
                {
                    Debug.WarningLn("config.toml is out of date, rewriting it.");
                    SerializeConfig.Load(tempStream);
                    Config.ConfigVersion = Config.CurrentVersion;
                }
            }
            SerializeConfig.Save(ConfigPath);

            ConfigureHarmony();
            help.Events.Input.ButtonPressed += OnButtonPressed;

            help.ConsoleCommands.Add("spritemaster_stats", "Dump SpriteMaster Statistics", (_, _1) => { ManagedTexture2D.DumpStats(); });
            help.ConsoleCommands.Add("spritemaster_memory", "Dump SpriteMaster Memory", (_, _1) => { Debug.DumpMemory(); });

            //help.ConsoleCommands.Add("night", "make it dark", (_, _1) => { help.ConsoleCommands.Trigger("world_settime", new string[] { "2100" }); });

            help.Events.GameLoop.DayStarted += OnDayStarted;
            // GC after major events
            help.Events.GameLoop.SaveLoaded += (_, _1) => Garbage.Collect(compact: true, blocking: true, background: false);

            MemoryPressureThread.Start();
            GarbageCollectThread.Start();
        }