sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] custom file_loaded() and file_loaded?() v2.0.0

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 4 Posters 6.3k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      @unknownuser said:

      (http://forums.sketchucation.com/memberlist.php?mode) in another topic":kda7q0ry]
      @dan rathbun said:

      
      > > unless file_loaded?(File.basename(__FILE__))
      > > ...
      > > file_loaded(File.basename(__FILE__))
      > > 
      

      I recommend putting the whole __FILE__ instead of a File.basename(__FILE__). That way you would eliminate same filenames that are in different folders.

      Actually.. the $loaded_files array, is nothing more than an array of strings. It does NOT matter whether the strings are pathnames to existing files, or non-existing files, or just some strings representing unique "already loaded flags".

      @unknownuser said:

      (http://forums.sketchucation.com/memberlist.php?mode) in the same topic":kda7q0ry]Just using the filename is not a unique ID - full path is.

      It doesn't even have to be a filename you feed file_loaded - just any unique string.

      What I mean is that the file_loaded() and file_loaded?() methods, do not do any testing to determine if the argument is a valid pathname.

      SO

      • to save space (memory)
      • to be sure that the "loaded keys" are unique
      • to side-step the fact that __FILE__ does not work inside scrambled rubies

      I have begun to set my "already loaded" arguments to a qualified namespace string, like:

      unless file_loaded?("*MyTopModule*::*ThisPluginModule*:*filename_without_extension*")
      I do not put the file extension as they may distro' as .rb or .rbs and it's not important anyway.

      So, let's say my namespace is Boozer, and the plugin is Gadget, and the file sets up menu commands:

      unless file_loaded?("Boozer::Gadget:menusetup")

      Noone else is using my namespace, so the string should be unique. Even for me it should be unique, because I include the plugin name, and files only ever load once (in normal usage. ie, not debugging.)

      So to use your example, the following strings would be unique:
      "Anton::Lib:core"
      "TT::Lib:core"
      and
      "TT::Lib2:core"


      Furthermore... nothing really says that we MUST use the global $loaded_files array. (It's an invention of the SU DevTeam.)

      If you are coding within your own unique toplevel "Author"/"Company" namespace... you can define your own "already loaded" string tag array, as a module variable, named whatever you like.
      Then define getter and setter methods (or copy & modify the two from "sketchup.rb",) into your toplevel module.

      Here's a template (now tested, and updated to v2.0.0) to do this, as a mixin module:
      LoadUtil.rb

      πŸ’­

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Ok I guess I need to explain how to use a mixin module, using the global include() method.

        In the example below (and in the template,) you change the word "Author", to YOUR toplevel module name.

        This LoadUtil module is written FOR PRIVATE USE. append_features will not let it be included into any one else's namespace.
        It makes Author::LoadUtil a private module !!

        YOU use it by including it in your sub-modules.

        # file; "Author/NiftyPlugin/Setup.rb"
        module Author;;NiftyPlugin
        
          require("Author/LoadUtil")
          include(Author;;LoadUtil)
        
          unless file_loaded?("NiftyPlugin;Setup")
            # do code to setup menus
            # do code to setup toolbars
            file_loaded("NiftyPlugin;Setup")
          end
        
        end
        

        In the above code sample, the file_loaded and file_loaded? that get called, are the ones you mixed in (which LOCALLY overrode the global ones, that are defined in "Tools/sketchup.rb")

        You have your OWN @@loaded_files array, up in module Author, that you keep all to yourself, and there will never be any clashes caused by OTHER people's scripts. (Now you can make a mistake and cause a clash, but you can fix it, without having to get someone else to change their code.)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          πŸ‘ πŸ‘

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            The only purpose of file_loaded appears to be as "menu guards" that prevent duplicate menu items from being created if the file is loaded more than once. This is helpful when developing a plugin, but what is the point in a released plugin? Better to not add elements to either a global $loaded_files or a namespaced equivalent.

            Hi

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              Sometimes it's easier to just "go with the flow.."

              Keeping two editions (a debug, and a release,) for every script and plugin just confuses things.

              Also there has been historical "bugs" and "quirks" in the way the Ruby's require worked. Remember that until SU 8.x, everyone was still running on the PC with 1.8.0, which had a require that works a bit differently than it does in later versions.

              So.. a historical part of this issues, was also dealing with require's wanting to load the same file more than once.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • A Offline
                Anton_S
                last edited by

                Here is jst a little mistake in the script:
                error.png
                script.png
                In append_features, mod.name returns "". Where first interacted mod itself is #<Class:Anton>.

                Since, I quite don't know what should there be, I can't fix it. So Dan fix it please πŸ˜„

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  Anton, I set you a personalized copy of v2.0.0 by Private Message.

                  Everyone else... you can get the generic template v2.0.0 update, in the first post.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    Now.. that said.. yes there are alternatives.

                    Sometimes I use defined?

                    If the module var is going to be @@menu, then I use:

                    unless defined?(@@menu)
                      @@menu=UI.menu('Plugins')
                      # define items, etc
                    end
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @anton_s said:

                      In append_features, mod.name returns "". Where first interacted mod itself is #<Class:Anton>.

                      I did say it was not tested.. so it's at concept level.

                      It cannot get past the mod.name test, because I tried to include it into module Anton's proxy class, which is anonymous, so it has no name. (That is why mod.name == "") I thought it would call the name method of the Anton module, but it did not.

                      I will have to modify the tests a bit.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        Anton_S
                        last edited by

                        @dan rathbun said:

                        Anton, I set you a personalized copy of v2.0.0 by Private Message.

                        Lol, I feel speacial now.

                        πŸ‘ πŸ‘ πŸ‘

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          OK.. so, for those you who have read the thread.. looked at the template, and scratch your head and wonder why all the complexity to control a private array ??

                          Answer.. it's just an example, of a private library mixin module.

                          In practice, you can rename the mixin module to whatever you like... and insert whatever proprietary functionality you wish to include in only YOUR plugins (ie, methods in the mixin sections, other class variables and constants, etc.) whilst not allowing some other "hack" to use those features in their plugins.

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement