• Overloading AddCSLuaFile()
    11 replies, posted
Hello all, I recently finished a gLua minifier. I want to use this minifier to minify files sent to the client to increase loading times. The obvious way to do it seems to be to overload _G.AddCSLuaFile(), but this comes with some issues 1. AddCSLuaFile can be called with relative paths, or with no arguments at all. How can I get the file that was AddCSLuaFile'd to minifiy it? 2. How can I then AddCSLuaFile() my minified file? The obvious way to do this seems to be to save it (somewhere in data/ ) and AddCSLuaFile the new file, but again there's not way to specify this directory, since the overloaded AddCSLuaFile can be called in any file, so relative paths won't work, and neither do absolute paths. 2-2. I might be able to dodge around 2, if I can get the file, minify it, then send it using the net library to dostring() on the client side, what are the tricks and pitfalls I should watch out for? Do I need to compress the minified code? Can code be modified in-transit? Is there any way to do this without having to delve into binary modules?
I don't understand what you're asking exactly; minifying just changes existing code, right?
[QUOTE=code_gs;50557417]I don't understand what you're asking exactly; minifying just changes existing code, right?[/QUOTE] Yes, it changes it to a syntactically similar (but hopefully more compact) version For example: [CODE] --Test calls! include("string1") include("string2") require("string3") dofile("string4") [/CODE] Becomes [CODE] local b,g,c,e,d,a,f="string1","string4","string2","string3",require,include,dofile;a(b)a(c)d(e)f(g) [/CODE] Not a great example since there's not much repetition, but you get the idea. What I'm asking is how can I modify this code: [CODE] local addcsfunc = AddCSLuaFile local newcslua = function(file) file = file or "" print("AddCSLua called on " .. file) addcsfunc(file) end _G.AddCSLuaFile = newcslua [/CODE] To find the files that AddCSLuaFile gets called on if it is called with an argument in if it's called without an argument
You can run [url]https://wiki.garrysmod.com/page/debug/Trace[/url] and get where it was called from.
[QUOTE=code_gs;50557961]You can run [url]https://wiki.garrysmod.com/page/debug/Trace[/url] and get where it was called from.[/QUOTE] that's disgusting use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/debug/getinfo]debug.getinfo[/url] instead
[QUOTE=xaviergmail;50558003]that's disgusting use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/debug/getinfo]debug.getinfo[/url] instead[/QUOTE] But I don't have a reference to a function, all I have is the string that was addcsluafile'd (and the call stack), don't I need one? I kinda-sorta got a 2-2 in the op working with debug.traceback. Also, how in the world do I AddCSLuaFile the now-minified code? Trying to send over net will break include(), but I can't save it over/near the original file because file.Write will only write to data/ Any advice about how to minimize the network footprint or any measures I need to take to make sure the code got there ok is welcome.
Doesn't gmod already compress cs lua files? Compression does pretty much the same thing minify does except better.
You're overcomplicating something that is normally very simple. All you have to do is, when you're deploying your code, minify it beforehand and put the already-minified files in place. There's no reason to minify them on-the-fly. Also no need to override anything. Keep your full files somewhere else, outside of your server's active files or maybe on a development server. This is how many websites handle javascript by the way. The good ones at least. [editline]21st June 2016[/editline] [QUOTE=thegrb93;50562500]Doesn't gmod already compress cs lua files? Compression does pretty much the same thing minify does except better.[/QUOTE] Care to provide more details? If that's true, it's news to me.
[QUOTE=thegrb93;50562500] Compression does pretty much the same thing minify does except better.[/QUOTE] no I'm fairly certain gmod uses a variant of [URL="https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch"]LZW compression[/URL] (the same one gzip uses, I think) Take a module I recently released, [URL="https://facepunch.com/showthread.php?t=1521810"]fuzzel.lua[/URL] It goes from ~13kb, to ~3kb when compressed with gzip, but goes down to ~2.5kb when run though a [URL="https://mothereff.in/lua-minifier"]minifier[/URL](without even compressing!), and down to ~1kb when ran through both a minifier and compressor. A 300% increase from what it can be is a big deal, especially as my projects grow larger, so I wrote a minifier, and adapted it for glua. [editline]21st June 2016[/editline] [QUOTE=NeatNit;50562547]You're overcomplicating something that is normally very simple. All you have to do is, when you're deploying your code, minify it beforehand and put the already-minified files in place. There's no reason to minify them on-the-fly. Also no need to override anything. Keep your full files somewhere else, outside of your server's active files or maybe on a development server. This is how many websites handle javascript by the way. The good ones at least.[/QUOTE] It's a good idea, but I've come so far, I'd hate to have to settle for less now...
The wise thing to do would be to cut your losses, rather than going deeper and deeper down the rabbit hole. Also you wouldn't be settling for less, you'd be getting more - 0 performance loss, and no risk of breaking something with a bad overridden function if it didn't account for some edge case that the default does. By the way, regarding your minifier - does it really put global functions into local variables? Because that's not good. If the global variable is ever changed, the change won't be reflected in the local copies. And there's no shortage of addons that modify global functions.
[QUOTE=NeatNit;50562740]The wise thing to do would be to cut your losses, rather than going deeper and deeper down the rabbit hole. Also you wouldn't be settling for less, you'd be getting more - 0 performance loss, and no risk of breaking something with a bad overridden function if it didn't account for some edge case that the default does.[/QUOTE] I guess you're right, still a little disappointing I couldn't completely automate it, but I guess throwing it in the git hook somewhere will be just as good. (Although now I have to list all of the shared or client-side files somewhere...) [QUOTE=NeatNit;50562740]By the way, regarding your minifier - does it really put global functions into local variables? Because that's not good. If the global variable is ever changed, the change won't be reflected in the local copies. And there's no shortage of addons that modify global functions.[/QUOTE] Sort of, but only for the globals defined on the gmod lua wiki (or in luajit, like dostring).But i guess that's still faliable, since I tried to change one... Is modifying global functions really that common?
Well, I know that some popular admin mods modify the hook library (and 'hook' is a global), but I'm not as sure about global functions directly. For what it's worth though, you were about to override the AddCSLuaFile global function ;)
Sorry, you need to Log In to post a reply to this thread.