Property cache menu

Shout-Out

User Tag List

Results 1 to 3 of 3
  1. #1
    Kryso's Avatar Active Member
    Reputation
    40
    Join Date
    Jul 2009
    Posts
    97
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Property cache

    I've started working with in-process because out of process is such pain in the ass and noticed that I need to read some things many times and somewhere (names for example) it looks like waste of resources. So I've made simple caching system and here it is:

    This interface is required for my invalidator. It's useful when you want to invalidate other classes besides CachedProperty.
    Code:
    internal interface IInvalidatable {
        void Invalidate();
    
        bool IsValid { get; }
    }
    Here is class that takes care of invalidating items each frame. It uses WeakReference because we don't want to prevent GC collecting those objects. It's calling "Invalidate" method on each frame - you will need to setup own endscene hook or something.
    Code:
    internal class Invalidator {
        private Invalidator() {
            items = new LinkedList<WeakReference>();
    
            Direct3D.RegisterEndSceneCallback( new EndSceneCallback( direct3D_EndScene ), CallbackPriority.Critical );
        }
    
        private LinkedList<WeakReference> items;
    
        private void direct3D_EndScene( IntPtr instance ) {
            LinkedList<WeakReference> toRemove = new LinkedList<WeakReference>();
            foreach ( WeakReference item in items ) {
                if ( item.IsAlive )
                    ( ( IInvalidatable )item.Target ).Invalidate();
                else
                    toRemove.AddLast( item );
            }
            foreach ( WeakReference item in toRemove )
                items.Remove( item );
        }
    
        public void Register( IInvalidatable item ) {
            items.AddLast( new WeakReference( item ) );
        }
    
        #region Static members
    
        public static readonly Invalidator Instance = new Invalidator();
    
        #endregion
    }
    And finally the main class. It will register itself automatically in Invalidator. Here you must supply method that takes care of updating and optionally how many frames should value live. 1 means value can be refreshed once every frame, 10 means value can be refreshed once in 10 frames. Value is refreshed only on demand.
    Code:
    public class CachedProperty<T> : IInvalidatable {
        public CachedProperty( Func<T> update, int framesToLive = 1 ) {
            this.update = update;
            this.framesToLive = framesToLive;
            age = framesToLive + 1;
    
            Invalidator.Instance.Register( this );
        }
    
        private Func<T> update;
        private int framesToLive;
    
        private T value;
        private int age;
    
        private void Update() {
            value = update();
            age = 1;
        }
    
        #region IInvalidatable
    
        void IInvalidatable.Invalidate() {
            age++;
        }
    
        public bool IsValid {
            get {
                return age < framesToLive;
            }
        }
    
        #endregion
    
        #region Operators
    
        public static implicit operator T( CachedProperty<T> self ) {
            if ( !self.IsValid )
                self.Update();
            return self.value;
        }
    
        #endregion
    }
    Example:

    Code:
    public class WarcraftObject {
        internal WarcraftObject( IntPtr address ) {
            this.address = address;
    
            name = new CachedProperty<string>( new Func<string>( () => {
                return Process.Read<string>( Delegates.Get<CGObject_C__GetNameDelegate>( address, 52 )( address ) );
            } ), 10 );
        }
    
        protected IntPtr address;
        private CachedProperty<string> name;
    
        #region Properties
    
        public string Name {
            get { return name; }
        }
    
        #endregion
    }
    Any criticism is welcome
    Last edited by Kryso; 12-20-2009 at 11:39 AM.

    Property cache
  2. #2
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Neat concept; however that only really helps with smaller updatable items. (Eg; items, game objects, etc.)

    Wrap it around with an attribute [say, a CachedAttribute] that can automatically handle the caching [just execute once; return the cached val until n frames] so you can selectively cache specific things. [GUID, Name, etc, are things you'd want cached say; 60 frames, stuff like Position you don't want cached.].

    Again, good idea. I just think it's a bit limited in design. Plus; it'll greatly increase the amount of variables in a single class. (I don't know about you; but I'd much rather just expose a bunch of read only properties that tie directly into certain functions/descriptors)

  3. #3
    Kryso's Avatar Active Member
    Reputation
    40
    Join Date
    Jul 2009
    Posts
    97
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using attributes is really good idea, I've totally forgot about them. Thanks

    edit: ok it's not as easy as i thought..

    I was thinking about something like this:
    Code:
    [CachedPropertyAttribute( 10 )]
    public string Name {
            get { return process.Read<string>( delegates.Get<CGObject_C__GetNameDelegate>( address, 52 )( address ) ); }
    }
    And then on runtime replace get method with my own that would take care of everything and call old method only when required. Well it looks like it's not easy to replace method From what I've found I could use remoting somehow but that seems to be overkill. Ironically, now I can't think of any other way to do this.. well, maybe tomorrow
    Last edited by Kryso; 12-20-2009 at 05:30 PM.

Similar Threads

  1. Deleting Database cache
    By saagdawg in forum World of Warcraft Emulator Servers
    Replies: 5
    Last Post: 12-05-2007, 06:31 PM
  2. [Request] An Item Cache with everything seen.
    By Emuchild in forum WoW ME Questions and Requests
    Replies: 3
    Last Post: 11-26-2007, 02:51 AM
  3. Patch 2.1 Tier 6 item ids for WDB (cache) editing
    By perilousbanana in forum World of Warcraft Model Editing
    Replies: 14
    Last Post: 06-22-2007, 04:33 AM
  4. Hex code search for cache model edits
    By Dave-evad in forum World of Warcraft Model Editing
    Replies: 9
    Last Post: 06-15-2007, 03:55 PM
  5. Warglaive's of azzinoth ids for WDB (cache) editing
    By chaimdasilva in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 06-11-2007, 02:31 PM
All times are GMT -5. The time now is 12:10 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search