• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Jass] Save/Load System

Level 5
Joined
Sep 13, 2007
Messages
33
Version 1.2

For those of you that don't want to read the whole READ ME:

This system can save and load. When saving the player gets a message, when he enters it the game restore integers that you can use. SLS_Save_Exe for saving, SLS_Load_Example for loading. Easy to use for GUIers with SLS_Save_GUI and SLS_Load_GUI. Many more details in the READ ME, the important chapters are "Importing" and "How to use?"

Enjoy, feedback is welcomed.

Read Me:
Code:
 | ----------------------------------- |
 | ----------------------------------- |
 | SaveLoadSystem (SLS) - By Rheias    |
 | Version: 1.2 Release Date: 02.08.07 |
 | ----------------------------------- |
 | ----------------------------------- |

// 1.3v will include security function, this version is released without security functions so users could understand the system
// until I'll manage to get some decent security.


What is this?
¯¯¯¯¯¯¯¯¯¯¯¯¯

    This system, SaveLoadSystem (the name is not very original) or SLS is a system that allows you to convert integers into 
    strings. Once done, those strings can be converted back to integers. Yes, that is all it does. How does it help us? Well,
    you can store in this way huge integers in small strings, so players can copy them and then when they enter those strings
    later the game will restore those integers for you - as the mapper - to use.

    Check the history at the bottom of this document to view the important changes done to the system.

Others?
¯¯¯¯¯¯¯

    Wait just a minute! There are systems like this already! Bah, someone beat you to it!

    Yes, I know indeed that there are already systems like that (three that I know of), but another one can't hurt now, can't it?
    Anyway, this system has certain Pros that others systems might not have. Yes, there are Cons as well, that might make you 
    rather use other systems, that's fine, but don't decide too quickly that this one isn't for you.

Pros & Cons
¯¯¯¯¯¯¯¯¯¯¯

    As I said, there are Pros, and there are Cons, here are few of each, if you feel one of them doesn't reflect the truth,
    please, do share your opinion.

    Pros
    ¯¯¯¯                       
        - Short Code: The code is rather short, pretty straight forward, we got two saving functions, one of them only to keep
          things clean but isn't really needed, loading function, two flushing functions, that one of them is completely useless
          but it keeps things clean. Also there is changing function which is just a little bonus. And the best part? Only one
          is used, and again, it isn't needed but it is used to keep things clean. So the key sentence is: Short code, gets abit
          longer so it would be more comfortable.

        - Flexible: This system was meant to be flexible, it is quite easy to use it, and it can serve several goals. You can
          flush codes in the middle, add to them other values or remove them, add values in the middle of the code if you need
          it (for example coloring). Not only that, but the system leaves room to do with it lots of things if you have enough
          knowledge to do them and want to.

        - Comments: You hate systems where there are no comments; you have to figure out everything by yourself? Well, good, this
          system makes your life easier, every function as a comment as well as few comments here and there explaining bits of
          the code. Anyway to code is very simple to understand, so I doubt if you'll need those.

        - GUI Friendly - This system can easily be used by GUIers with two extra globals, SLS_Save_GUI and SLS_Load_GUI, of
          course Jasser's way is better, it is always is, but GUIers' way is still good none the less.

    Cons
    ¯¯¯¯
        - One Value: The system can only save and load one integer / string at a time. That might make things a bit harder, but
          it is not hard to pass this problem, simply it may make codes a little longer, or take some of the readability, but
          nothing serious at all. See the example functions to see I passed this little Con.

        - Flush: In order to use this system well we must flush it after completing a saving process. This could have been
          stopped with arrays, but then it would make the system longer, harder to read, and frankly we wouldn't have gotten
          much. Also without recycling it will put a limitation of 8192 times that players may save. Arrays < flush but still
          nothing > flush. You can also implement some changes and not use udg_code - it is not really needed anyway to be honest
          but it gives the system some defined structure, and keeps it orginized, so udg_code & flush > nothing.

        - Length: When setting SLS_Seperate to true, to make our loading process (MUCH) easier, then codes might get a bit long.
          If you are worried about it though, you can always change SLS_ABC (See later). Setting it to false makes the loading
          the loading process much longer, if it is even possible, but keeps the strings shown shorter.

    Those are more or less the Pros and Cons, I'm sure we could have found more from each, but those are the important ones, 
    anyway, I bet you want already to get to know the system rather then reading why it is good or bad, so with that, 
                                                       let's go on!

Importing
¯¯¯¯¯¯¯¯¯

    If you want to use this system you must follow the following steps.

    A) Create a global variable in the variables tab (Ctrl + B) called code of type string with no array.

    GUIers only (read more under "How to use?"):

        1) Create a global variable in the same manner called save, type integer with array.
        2) Create a global variable in the same manner called load, type string with no array.

    B) Copy this map's header to your map's header. For those of you a map header is the thing on top of the triggers and trigger
       files, with The Frozen Throne icon and text that says *.w3x (SLS.w3x).

    C) Learn how to use it, and if you need, modify it or even create new functions to follow the system. As I said it is very
       flexible. Keep reading!

How to use?
¯¯¯¯¯¯¯¯¯¯¯

    Finally, we can get started on how to use this system! In this chapter I will discuss each one of the given functions that
    you can use. Also at the end there will be a short article saying how GUIers and Jassers can use this system.

    Now, one thing to understand about this system, what it does is quite simple, it takes integers and turns them into strings,
    those string as those integers represented by letters, in other ways we go from base10 (Decimal) to base62 and back all the
    time. Base62 (the strings) can represent the same number as Base10 (the integers) in a shorter format. 

    So, when saving you take an integer in Base10 and turn it to a string in Base62, when loading you take a string in Base62
    and turn it into an integer in Base10 (regular integer) which is readable by the game and you. Why do we do it? Because this 
    way we can make the codes shorter.

    To go into more detail, in this system when saving we take a global string and keep adding to it strings in Base62, when
    needed we show that string to the player we want, he/she, then, copies the code (and then the code is flushed to be ready to
    the next saving event). After joining the game of the same type, later on, he can enter the copied string and restore 
    information in integer (Base10) format.

   
    __________________________________________________
   | function SLS_Save takes integer c returns string |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This function will take a value (integer) and return it as a string. In other word, this is the actual function that turns
    Base10 into Base62. This however will not store it, yet, in udg_code, for that we use the next function.

    ______________________________________________________
   | function SLS_Save_Exe takes integer c returns string |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    
    Uses SLS_Save to take an integer and save it regularly. However, this function will also add it to udg_code to makes things
    neater, and if needed it will also add "-" at the end of the code (see SLS_Seperate)

    __________________________________________________
   | function SLS_Load takes string c returns integer |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This function, as expected does the opposite that SLS_Save does; it takes Base62 and turns it into a useable Base10 integer.
 
    __________________________________________________
   | function SLS_Flush takes nothing returns nothing | 
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 
    SLS_Flush flushes the global string "udg_code", in other words, it nulls it, sets it to nothing, so we can use it again in 
    a new saving event. Because when saving we keep adding to the original "udg_code" new values, when we are done and we want
    to prepare ourselves for the next time, we need to flush it, to keep it empty for the next use of it.
 
    _________________________________________________
   | function SLS_End takes player d returns nothing | 
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    
    This function is pretty much useless, yet it keeps things clean. What we are doing with it is flushing udg_code (remember
    "SLS_Flush"?) but before that we show it to a player. In other words SLS_End shows the saving code so far to a picked 
    player so he can copy it, afterwards it flushes udg_code just like SLS_Flush would do it.

    _________________________________________________________________
   | function SLS_Change takes string save, integer o returns string |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   
    This one is more unique then the others, what it does it takes a value and adds it in the MIDDLE of udg_code, that way you
    can do in-game changes of the values' order, color certain letters and so on. String save is the value you want to add, like
    to udg_code in the middle, integer o is the strings's order. In others when it will come. 0 and 1 are first, 2 will be second
    value, 3 will be third and so on. Setting o to a bigger integer then the number of values in the system will simply put it
    as the last value. Example

    call SLS_Change("jX6-",12)

    will add "jX6" as the 12th value in udg_code.


    Constants:

    _______________________________________________
   | function SLS_ABC takes nothing returns string |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This little function will return the system's characters in order (Starting from 0). You can change it to whatever you like,
    you can remove letters, change their order, add letters, as long as you don't have "-" and possibly " " as characters you are
    good. Also don't repeat the same character twice.

    _______________________________________________
   | function SLS_TS takes nothing returns integer |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    SLS_TS simply returns the base (62 at default). No need to edit this one, it will return the strings length of SLS_ABC.

    _____________________________________________
   | function SLS_Max takes nothing returns real | 
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    I don't suggest modifying this one. What it returns is the max number of power (in other words, max number of characters in
    the Base62 string) it is just there for safety, changing this function to a smaller integer may cause problems, and to a 
    bigger integer will do nothing but a mess.

    _____________________________________________________
   | function SLS_Seperate takes nothing returns Boolean |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This function returns true or false (true in default). If true, then after each value saved there will by it (to the right)
    a little "-", this is served to separate between values, so we can load with a great ease. If you are saving only one value
    or the values saved have known amount of integers (in Base62) then you can set it to false, otherwise, keep it true.

    __________________________________________________
   | function SLS_Duration takes nothing returns real |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    Quite simply, returns the amount of second the player will see his code when using SLS_End. Keeping this function low may
    prevent players from copying the code, but keeping it for too long may disturb the game play. Default is 180s (3m).

    _______________________________________________
   | function SLS_If takes nothing returns integer |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    If there will be something before the code, for example -load , then you need to return it's string length plus one.

    Now, all we got left is the examples, however I will talk about them under the chapter modifying - I will talk there about
    writing function that will use the system presented to actually do what it needs to do, Save and Load.

    GUIers & Jassers
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
       This system can be used by GUIers and Jassers easily. Jassers, all you need to do when saving is use SLS_Save_Exe, and
       when you are done SLS_End(player). Simple? Yeah, if you need coloring etc. just use SLS_Change. When loading, simply use
       SLS_Load_Example (or other function, if you got one that works better for you).

       GUIers, for you it's abit harder, but I prepared a special function for you, call SLS_Save_GUI, all you need to do is to
       keep all values you want to save in the global array variable, save when save[0] is the number of the values you want to
       save and then do:
       
       Custom script: call SLS_Save_GUI()
       Game - Display to (Player group((<player>)) the text: code

       A LEAK?!?!!?!?!1 
       Yes - a small one for readability's sake, you can remove it easily.


       For loading, simply get the string you want to load in the global variable load, and do:

       Custom script: call SLS_Load_GUI()

       Very simple for both users' types. NOTICE: If you are a Jasser, no need to get the globals udg_save and udg_load.



Modify
¯¯¯¯¯¯

    Now let's talk a bit about creating new functions to use SaveLoadSystem system. Let's start but taking a look at the two
    example functions given to you. Note before we start that the GUI trigger's used under the fine "example" do what

    _________________________________________________________
   | function SLS_Save_Example takes nothing returns nothing |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This function will loop through all numbers between 0 and 15 and it will turn them into Base62, after that it will display
    the code to a given player (Player(0) in this case) and it will also flush the code so he could save it later on. Now, this
    function is basically how you can properly save a code. You pick few values you want to save, save all of them and then you
    use SLS_End to show the loading code to some as well as flushing it for later use? Simple? Indeed. The code the player will
    get is:

    0-1-2-3-4-5-6-7-8-9-a-b-c-d-e-f-

    If it isn't that simple to you, let's take another look at SLS_Save

 --------------------------------------
|    local integer counter = 0         |
|                                      |
|    loop                              |
|        exitwhen counter > 15         |
|        call SLS_Save_Exe(counter)    |
|        set counter = counter + 1     |
|   endloop                            |  
|                                      |
|    call SLS_End(Player(0))           |
 --------------------------------------

    local integer counter = quite obvious, we are creating a local variable so it could be used for our loop

    loop stuff: Just some loop stuff that aren't really important for us (loop, endloop, exitwhen xx, etc.)

    call SLS_Save_Exe(counter): Now, here is the most important line. What is does is, as said in the previous chapter is 
    changing Base10 to Base62. The key point is that you can replace counter with any integer, I put there counter just for the 
    example but you could have entered unit's level, player's gold, charges left in an item, whatever you would like.

    call SLS_End(player): Another important line, shows a chose player udg_code so far (as you probably remember, I said already
    the everything saved is added to udg_code), in this example Player(0) is the chosen player, and after showing udg_code it
    will flush it for later use.

    Should be clear enough to you now, if need, re-read until you get it. Now, let's talk about the loading example.

    __________________________________________________________
   | function SLS_Load_Example takes string c returns nothing |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    This function will load a given string (string c) in Base62 to Base10. Because I use SLS with SLS_Seperate set to true, this
    example will assume that after every entered value there will be a "-". E.G.

    This is legal:
    ¯¯¯¯¯¯¯¯¯¯¯¯¯
    a-ZZ-J-NBT-68G-

    Those are illegal:
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    anjdfshfsfjk

    asfmf-assd // No "-" after the last one (yes the last one needs "-" too to keep the code short)

    The way this function works is looping through string c. Once it finds "-" it will load (SLS_Load(string)) the string that
    come before this "-" and after the last "-". So, if will give it this string: "9-a-b-" It will return 9, 10 and 11. The
    problem is that you have to do something with some integers within the function because we can't return few values. As long
    as you know the saving order, that's not a problem, but anyway you should know that.

    Let's go into more detail:

 -----------------------------------------------
|    local integer start = 0                    |
|    local integer end = 1                      |
|    local integer l = StringLength(c)          |
|    local string sub = ""                      |
|    local string temp = ""                     |
|    local integer counter = 0                  |
|                                               |
|    loop                                       |
|        exitwhen end > l                       |
|        set sub = SubString(c,end - 1, end)    |
|        if sub == "-" then                     |
|            set temp = SubString(c,start,end)  |
|            call SLS_Load(temp)                |
|            set start = end                    |
|            set counter = counter + 1          |
|        endif                                  |
|        set end = end + 1                      |
|    endloop                                    |
 -----------------------------------------------

    Locals: We need to set few locals for the loop, not very important.

    Exitwhen end > l: Simply we tell the loop to stop running once "end" is greater then "l", or in other words once "l" is
    bigger then c's string length.

    Set sub = SubString(c,end - 1, end): Each time we set sub to a different character further along the string, we keep
    increasing end's size, so it will pick different character each time.
    
    If sub == "-" then: We check if sub is "-" or not, if it isn't, we need to keep waiting until the next one shows up, if it is
    then...

    Set temp = SubString(c,start,end): We get the code after the last "-" to the variable end.

    Call SLS_Load(temp): We load the string temp, which as remembered is the code, now this is the key line in this code, this
    line will return the integer in Base10, you have to use it. You have three options:

    A) Do something with it inside the loop - not suggested because you keep getting different integers which you don't know what
       they are.
    B) Save it in a local array integer[counter] (you'll need to create counter variable) - This way is suggested, that way you
       can use the integer in the function, but outside the loop, easier to edit and use this way, especially because you can
       tell its saving order.
    C) Or you can save it in a global array and then use it whenever you want, but keep in mind that if this function is ran
       before you get to use the globals then it is all gone Kaput.

    Set start = end: You set start to end, we passed this "-", now when we call the next will copy only the needed piece of code.

    Set end = end + 1: And finally, we make end bigger by one so we could keep checking the string.

    Set counter = counter + 1: If you are using way B of manipulating the returned integer, then you need counter to keep the
    array of the local you are going to use correct.

    So, those are the example functions, they do exactly what I need them to do. However, you may want to use my system
    differently - that's fine, you can go on and create functions to use SLS_Save and SLS_Load for your own needs, however
    they need to do what my functions did, take integers and turn them to strings, or take strings and turn them to integers.

    _______________________________________________________________
   | function SLS_Change_Example takes string save returns nothing |
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

   This one is simple, very simple. Takes a string and adds it as the third value. Why third? Becuase three was the first 
   number I thought of. No need to go into further explanation about this one, this document is long enough,


    Main Use
    ¯¯¯¯¯¯¯¯
        One thing I would like to go over quickly is the main use of this system, so you could create you function accordingly.
        Usually, the best way to work with this system will be when player enters the word "-save " then show him a loading
        code with the wanted integers. When a player types "-load <code>" then to check then to load integers and do with them
        something. Note: if you are using this way of loading ("-load <code>") then in SLS_Load you should set counter[0] to 7,
        not to one. So change this line:

        set count[0] = 1

        To:

        set count[0] = 7

        Why 7? 6 + 1 = 7. Why 6? The number of characters there is in "-load " is 6.

    Changing the characters
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
        One last thing - you can change the characters of the system easily, you delete them (in SLS Vars Set), add a few, change
        existing ones, whatever you want to do, however you must make sure those happen:

            A) SLS_TS will be updated, so it will always be equal to the number of characters you have in the system including 0.
            B) You must work in order, which means you must start from array 0 and then each time raise the array only
               by one, not doing so will cause some major problems.
            C) If SLS_Seperate is true, never use "-" as a character.


Last Words
¯¯¯¯¯¯¯¯¯¯
    So, we are nearing the end of this long READ ME. I hope you enjoyed and got something out of it, if you don't understand it
    just ask question in the thread the system was posted and I will be more then happy to help you. Feedback / bugs found and
    all of that should go to the thread this was posted in as well.

    One thing I want to make sure I managed to get to you is why I created this system. I wanted a system that can be very easily
    used to do different things, so I could each time create slightly different functions while using few base ones so complete
    different goals. Even a beginner Jasser should be able to read this system, and an intermediate Jassers could even throw
    the example functions a side and create new ones that fit their need.




With that, I think I will finish this READ ME, now you can fully use this system, and even edit it to your need, as said
questions / feedback / bug and anything like that should go to the thread which is system was originally created in. I have
one request, no flaming. You don't like this system? Move along, I'm the last person that would want to hear how much this
system is awful.

Version History
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    Version 1:
        August 02, 08
        - Original release of the system, including SLS, SLS Var Set, and the READ ME.

    Version 1.1:
        August 03, 08
        - The READ ME was fixed. 
        - The comments in SLS were edited. 
        - A small bug was fixed.

    Version 1.2:
        August 06, 08
        - Updated the READ ME (this document) to fit 1.2v. Inculding:
            - Few mistakes and typos fixed.
            - The document was shortened to make the read easier (Many chapter were cut and the chapter "Bases" was removed)
            - Terms of Agreement were added.
            - Updated the READ ME to include explanation about the new functions.
        - General re-work of few of the functions
        - Old messy SLS_Chars[index] is replaced now with SLS_ABC and few SubString. Globals are pain. 
        - SLS_Save was edited to not return udg_code but the saved value.
        - SLS_Save_Exe, SLS_ABC, SLS_Change and SLS_Change_Example were all edited.
        - Added GUI friendly functions.
        - Example GUI triggers were added under the folder "Examples".

Terms of Agreement
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    Follow the following and we are good. Don't follow them and we have a bit of a problem. I made this system and offered it to
    you, the least you could do it follow those:

        - You may use this system freely in you map, no credits needed, but they will be appreciated.
        - You may not submit this system in other website without premission from me (Rheias).
        - You may no edit this system and claim it as your own, start from nothing if you want to do something similiar to this.
        - Don't come complaining to me if you managed to ruin something, re-read the READ ME instead.
.





                                                           ~~~~~

                                        Yes, finally, that's it, have fun, go wild.
                                                           Rheias




Code:

JASS:
// | ----------------------------------- |
// | ----------------------------------- |
// | SaveLoadSystem (SLS) - By Rheias    |
// | Version: 1.2 Release Date: 02.08.07 |
// | ----------------------------------- |
// | ----------------------------------- |

// Reading the READ ME is strongly suggested.

// =============================================


// _______________________________ 
//| Constants (Safety)            |
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯  



//The system's ABC.
function SLS_ABC takes nothing returns string
    return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
endfunction


// Number of characters the system uses, no need to change.
function SLS_TS takes nothing returns integer
    return StringLength(SLS_ABC())
endfunction


// I don't suggest modifying this one, it can only harm doing so.
function SLS_Max takes nothing returns real
    return 7.
endfunction


// True means "-" will be put after each value saved, false mean it won't happen. Keep this true makes the loading process much
// easier, keeping this flase makes the code shorter.
function SLS_Seperate takes nothing returns boolean
    return true
endfunction


// The number of second a player will see udg_code when displayed to him.
function SLS_Duration takes nothing returns real
    return 180.
endfunction

// If you have something before the code, return it's length plus 1. -load <code> is seven then.
function SLS_If takes nothing returns integer
    return 7
endfunction

// Number of fake values security will use. Too less will make the code easy to hack, too many will make the code too long.
function SLS_Sec takes nothing returns integer
    return 3
endfunction

// _______________________________
//| Decimal <-> 62 (Basic Funcs)  |
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯



// Simple save, does the actual Base10 -> Base62 process.
function SLS_Save takes integer c returns string
    local string cod =""
    local integer a = c
    local integer array count   
    local integer n = 0
    local integer temp = 0
    local integer reduce = 0
    local string abc = SLS_ABC()

    if a < 10 then // Keeps the functions from being slow when not needed. Also 0,1 made problems.
        return SubString(abc,a,a + 1)
    endif    
    
    set count[0] = 0
    loop
        exitwhen count[0] > SLS_Max()
        if Pow(SLS_TS(),count[0]) > a then
            set n = count[0] // Number of digits the number will have in Base62 is stored in n.
            exitwhen true
        endif
        set count[0] = count[0] + 1
    endloop    

    set count[1] = n - 1

    loop
        exitwhen count[1] < 0
        set reduce = R2I(Pow(SLS_TS(),count[1]))
        set temp = a / reduce
        set a = a - reduce * temp
        set cod = cod + SubString(abc,temp, temp + 1) // The actual line that saves.
        set count[1] = count[1] - 1
    endloop

    // Stores the saving in udg_code

    return cod
endfunction


//The actual saving function that you should use, stores saved content in udg_code and adds "-" if needed.
function SLS_Save_Exe takes integer c returns string
    if SLS_Seperate() then
        set udg_code = udg_code + SLS_Save(c) + "-"
        return udg_code
    endif

    set udg_code = udg_code + SLS_Save(c)
    return udg_code
endfunction


// Takes a string and turns it into a useable integer for use to manipulate.
function SLS_Load takes string c returns integer
    local integer l = StringLength(c)
    local integer array count 
    local integer cod = 0
    local integer char = 0
    local string sub = ""
    local string temp = ""

    if SLS_Seperate() then
        set l = l - 1 // We don't need to read "-".
    endif

    set count[0] = SLS_If() // VERY IMPORTANT: Keep this (SLS_If) UPDATED!
    
    loop
        exitwhen count[0] > l
        set sub = SubString(c,count[0] - 1,count[0]) // We keep checking different characters in order
        set count[1] = 0
        loop
            exitwhen count[1] > SLS_TS()
            set temp = SubString(SLS_ABC(),count[1],count[1] + 1)
            if temp == sub then
                set char = count[1] // We store the character
                exitwhen true                
            endif
            set count[1] = count[1] + 1
        endloop
        set cod = cod + char * R2I(Pow(SLS_TS(), l - count[0])) // The actual line that is doing the loading process.
        set count[0] = count[0] + 1
    endloop

    return cod 
endfunction 



// _______________________________
//| Others                        |
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯



// Adds the integer in the middle of the udg_code, integer represents value's order.
function SLS_Change takes string save, integer o returns string
    local integer counter = 0
    local integer codL = StringLength(udg_code)
    local integer eeC = 1
    local string sub = ""
    local string temp = ""

    if o < 2 then // 0 and 1 go first
        set udg_code = save + udg_code
        return udg_code
    endif
 
    loop
    exitwhen counter > codL
    set sub = SubString(udg_code,counter,counter + 1)
    if sub == "-" then
        set eeC = eeC + 1
        if eeC == o then // If we got the needed value we add after it our value
            set sub = SubString(udg_code,0, counter + 1)
            set temp = SubString(udg_code,counter + 1,codL)
            set udg_code = sub + save + temp
            return udg_code
        endif
    endif
    set counter = counter + 1
    endloop

    set udg_code = udg_code + save // if o is bigger then number of values, we add our value in the end.

    return udg_code
endfunction


// Flushes udg_code, makes it useable again.
function SLS_Flush takes nothing returns nothing
    set udg_code = ""
endfunction


// Shows udg_code to a player and after that flushes it.
function SLS_End takes player d returns nothing
    call DisplayTimedTextToPlayer(d,0.,0.,SLS_Duration(),udg_code)
    call SLS_Flush()
endfunction

// _______________________________
//| Examples (Save & Load)        |
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯



// Saves integers and then shows them to a picked player.
function SLS_Save_Example takes nothing returns nothing
    local integer counter = 0
    
    loop
        exitwhen counter > 15
        call SLS_Save_Exe(counter) // Actual saving process, counter is just a random integer, you don't need it nor the loop.
        set counter = counter + 1
    endloop

    call SLS_End(Player(0)) // Displays all saved values in Base62 to a player and flushes udg_code.
endfunction


// Loads a string, works for several values as well. Must keep SLS_Seperate() true for this one.
function SLS_Load_Example takes string c returns nothing
    local integer start = 0
    local integer end = 1
    local integer l = StringLength(c) 
    local string sub = ""
    local string temp = ""
    local integer counter = 0
    
    loop
        exitwhen end > l
        set sub = SubString(c,end - 1, end) // Keeping checking what is next character.
        if sub == "-" then
            set temp = SubString(c,start,end) // takes the code from the last "-" to the present "-".
            call SLS_Load(temp) // Actual loading, returns the wished integer, do something with it!            
            set start = end
            set counter = counter + 1 // Helpful you want to use integer array to save values.
        endif
        set end = end + 1
    endloop
endfunction

function SLS_Change_Example takes string save returns nothing
    set udg_code = "0-1-2-3-4-5-6-" // Set udg_code to random stuff for test's sake
    call SLS_Change(save,3) // Puts the taken string after the second value. Three is just a random integer

    call SLS_End(Player(0))
endfunction

// _______________________________
//| GUIers (GUI use)              |
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯



function SLS_Save_GUI takes nothing returns string
    local integer counter = 1

    loop
        exitwhen counter > udg_save[0]
        call SLS_Save_Exe(udg_save[counter])
        set counter = counter + 1
    endloop

    return udg_code
endfunction

function SLS_Load_GUI takes nothing returns nothing

    call SLS_Load_Example(udg_load)

endfunction

// End system - That's it! At least for now . . . .
 

Attachments

  • SLS 1.2.w3x
    28.9 KB · Views: 202
Level 1
Joined
Feb 19, 2017
Messages
1
Really a hard work, such a long readme.txt, good combat & split string,
but why can't find anything about Save & Load, and data only in joined string , not structure and key & value
just like a string paresr
 
Top