Modular interface for scripting

Started by EK.IceFlake, Nov 09, 2016, 03:43 PM

Previous topic - Next topic

EK.IceFlake

This is a modular interface which means that you don't need to figure out where that part of code is, or have to make changes to global event functions, etc.
main.nut: http://pastebin.com/juKzUBxe

How to use it:
If you notice, at the top of main.nut is a line
modulelist <- [];You have to add all your modules here. For example:
modulelist <- ["default", "registration"];Now you will need to have 2 files in the 'modules' folder (you need to create a folder called 'modules' in your script root directory); default.nut and registration.nut.
Here is how a module should look like:
default.nut
class default
{
    function Init()
    {
        print("Default module loaded");
    }
    function onplayerjoin(player)
   
        ::MessagePlayer("Welcome To The Server!!!", player);
    }
}

Here is a template:
class [module name]
{
    function Init()
    {
        //Any initialization steps or simply leave this blank
    }
    //Module functions or events
}

//Functions that need to be outside the class

All modules can share events, so you can have 2 modules with function onplayerjoin and they would each get called (in the order the modules were loaded) and you wouldn't need to change a global onPlayerJoin function.

A list of all events and their parameters can be found in main.nut l35-70.

This is a very simple script however I have noticed that it makes your life easier a lot.

This script is distributed under the copyleft CLNS. Here is the copyleft:
[spoiler=CLNS]CLNS
You may use and redistribute this license, but changing it requires that the new license is compatible with this license, see section 5.

This software comes with NO WARRANTIES, read section 2.

PREAMBLE:
      This is a copyleft license--
      While most software licenses are designed to take away your freedom, this license, on the contrary, will preserve your freedom, even when redistributed.
      To preserve the freedom, any user or contributor to this software must retain the freedoms of this license.
      The freedoms provided by this license are:
            Every user is permitted to use or modify this software in any way
            Every user is permitted to override any technical limitations, including overriding activation features
            Every user is permitted to decompile or disassemble this software
            Every contributor is permitted to commercialize this software
            Every contributor is permitted to apply technical limitations to this software
            Every contributor is permitted to change this license agreement, providing the new agreement is compatible (see section 5) with this agreement
      Please read this agreement carefully

TERMS AND CONDITIONS:
1>    Definitions:
       1>   "Technical limitation" means preventing an action without legal intervention.
       2>   "User" means any person using the front-end.
       3>   "Contributor" means any person using the source code or developing directly on the binaries.
       4>   "You" means any user or contributor.
       5>   "Deriative" means any work based on this software.
       6>   "Including" implies 'but not limited to'.
       7>    All definitions are case-insensitive.
2>    No liablity:
       1>   This software is provided without any warranties, implied or otherwise.
       2>   The authors of this software are not liable for any losses or lost profits that occur by the use of this product, directly or otherwise.
3>    Application:
       1>   This license applies to any user or contributor of this software.
       2>   This license grants you explicit permission to use, modify and/or relicense (to a CLNS compatible (see section 5) license) this software.
       2>   This license may be revoked by removing all copies and traces of the software.
4>    Scope:
       1>   This license applies to the source code, its binaries and any deriatives.
       2>   Additional license terms may apply.
       3>   Additional license terms may not conflict with this license.
5>    Compatability
       1>   A compatible license must retain the legal effect of all conditions, except as stated.
       2>   A compatible license can change section 2 class 1 and the disclaimer on the fourth line thereof.
6>    Freedoms
       1>   You are permitted to modify this software, by source if available, binary or otherwise.
       2>   You are permitted to override any technical limitations in this software, including activation/commercialization features.
       3>   You are permitted to commercialize this software.
       4>   You are permitted to apply technical limitations to this software and they will not carry any legal protection.
       5>   You are permitted to change this license agreement to an agreement that is compatible with section 5.
7>    Legal measures
       1>   You are not permitted to use any legal measures, including patents or trademarks, to override this license.[/spoiler]

KAKAN

ontopic: Nice work. I liked it, but, why don't you use constructor instead of calling a 'Init' function?
offtopic: That spoiler though. I didn't even bother to read it, and I guess no one will.
oh no

.

Quote from: KAKAN on Nov 09, 2016, 05:29 PMontopic: Nice work. I liked it, but, why don't you use constructor instead of calling a 'Init' function?

Normally people use such approach to make sure that when a certain component initializes, all other components are fully loaded. Although a bit risky because not all components are initialized and one could attempt to use a component that wasn't initialized. Therefore defeating the purpose of such approach. I'm guessing you then rely on partial initialization in the constructor and then full initialization in the method? Who knows.
.

.

#3
Quote from: EK.CrystalBlue on Nov 09, 2016, 03:43 PMIf you notice, at the top of main.nut is a line
modulelist <- [];You have to add all your modules here. For example:
modulelist <- ["default", "registration"];

Just use modulelist.push("whatever") and think of it as better isolation.



If you wanted such system then it would've been much easier with meta-methods and a polymorphic approach. This is a very naive approach. You'll waste a bunch of time searching for methods (which might not even exist) in class instances on each event. Which is something already bad since the main plugin does it to the root table. You basically just amplified the effect several times depending on how many modules/components you have. Besides that, you perform the search twice to perform the actual call.

I'll see if I can post a snippet later tonight as an example.
.

EK.IceFlake

Quote from: . on Nov 09, 2016, 08:45 PM
Quote from: KAKAN on Nov 09, 2016, 05:29 PMontopic: Nice work. I liked it, but, why don't you use constructor instead of calling a 'Init' function?

Normally people use such approach to make sure that when a certain component initializes, all other components are fully loaded. Although a bit risky because not all components are initialized and one could attempt to use a component that wasn't initialized. Therefore defeating the purpose of such approach. I'm guessing you then rely on partial initialization in the constructor and then full initialization in the method? Who knows.
Exactly, on the constructor only the functions in the modules loaded previously will be available.
Init is called after all the modules have been loaded.

EK.IceFlake

#5
Quote from: KAKAN on Nov 09, 2016, 05:29 PMofftopic: That spoiler though. I didn't even bother to read it, and I guess no one will.
It's still necessary for protecting your freedom :D
Anyways all you need to know about it is you can do whatever you want but can't prevent anyone else from doing so. So for example anyone has full rights to decompile it (if you make it a .cnut), and you can't give it to someone with the agreement that they won't do this to it (well you can but then they can still do that).

EK.IceFlake

Quote from: happymint on Nov 09, 2016, 08:51 PM=
I'll see if I can post a snippet later tonight as an example.
@happymint
Still waiting :D

.

Quote from: EK.IceFlake on Dec 31, 2016, 08:04 AM
Quote from: happymint on Nov 09, 2016, 08:51 PM=
I'll see if I can post a snippet later tonight as an example.
@happymint
Still waiting :D

Squirrel language does not work as promised or as expected. Therefore, the thing that I had in mind cannot be achieved at this point. I guess we'll have to wait and see if the Squirrel author does something about this. Already tried to settle this but so far no official answer.

http://forum.squirrel-lang.org/default.aspx?g=posts&t=5061
.