Increment KUID Version
m |
|||
Line 23: | Line 23: | ||
;increment kuid version | ;increment kuid version | ||
− | AssetX has variables named 0, 1, 2 up to 9 that can store integers, floats or strings. | + | AssetX has variables named 0, 1, 2 up to 9 that can store integers, floats or strings.<br> |
− | These are nominated by the carat " ^ " symbol as shown below in the tokens command. | + | These are nominated by the carat " ^ " symbol as shown below in the tokens command.<br> |
To start our macro we firstly save the kuid value as a string into variable 9 | To start our macro we firstly save the kuid value as a string into variable 9 | ||
-save|9|config.txt\kuid | -save|9|config.txt\kuid | ||
− | The - | + | The -tokens instruction takes the string stored in variable 9 and divides it into sub strings at the locations nominated by the letters/symbols in the second parameter of the instruction.<br> |
+ | In this case we are dividing the kuid string at the "<", ":" and ">" locations. | ||
-tokens|^9|<:> | -tokens|^9|<:> | ||
− | divides string into<br> | + | divides the kuid string into<br> |
variable 0.. number of tokens in the string, in this case 5 or 6<br> | variable 0.. number of tokens in the string, in this case 5 or 6<br> | ||
variable 1.. empty string because "<" is at the beginning of the string.<br> | variable 1.. empty string because "<" is at the beginning of the string.<br> | ||
Line 36: | Line 37: | ||
variable 3.. UserID number<br> | variable 3.. UserID number<br> | ||
variable 4.. AssetID number<br> | variable 4.. AssetID number<br> | ||
− | variable 5.. Update number if | + | variable 5.. Update number if kuid2 OR empty string if kuid<br> |
variable 6.. empty string | variable 6.. empty string | ||
− | We now need to do a number comparison to determine how to handle the kuid or kuid2 cases. | + | We now need to do a number comparison to determine how to handle the kuid or kuid2 cases.<br> |
− | AssetX does not support | + | AssetX does not support comparisons of variables so we must place the tokens count into a tag in the config.txt to be able to the do a count comparison with an -if command.<br> |
+ | So we create a tag named "temp" and give it the value of tokens count. | ||
-update|config.txt\temp|0 ....create a temporary tag in the config | -update|config.txt\temp|0 ....create a temporary tag in the config | ||
-restore|0|config.txt\temp ...save the tokens count to the temporary tag so we can use it in an "-if" instruction. | -restore|0|config.txt\temp ...save the tokens count to the temporary tag so we can use it in an "-if" instruction. | ||
Line 56: | Line 58: | ||
-endif | -endif | ||
− | + | In the case of <kuid2:1111:2222:3> the tokens count is 6.<br> | |
+ | So if the token count is 6 then we aleady have a kuid2 with an Update value. | ||
-if|config.txt\temp=6 ....if tokens count is 6 then | -if|config.txt\temp=6 ....if tokens count is 6 then | ||
− | The version number is stored in variable 5 so we add 1 to it and save to variable 8. | + | The update (version) number is stored in variable 5 so we add 1 to it and save to variable 8. |
− | -compute|8|^5+1 ....increment | + | -compute|8|^5+1 ....increment update number and save in variable 8 |
− | We have the data available now to make kuid2 any version. | + | We have the data available now to make kuid2 any version.<br> |
+ | The -combine instruction joins the text and variable values together to make the new kuid string. | ||
-combine|9|<kuid2:^3:^4:^8> ..builds string "<kuid2:userID:assetID:update>" into variable 9 | -combine|9|<kuid2:^3:^4:^8> ..builds string "<kuid2:userID:assetID:update>" into variable 9 | ||
Revision as of 07:47, 4 February 2017
Here is an AssetX script macro to increment the version number of a kuid.
;increment kuid version -save|9|config.txt\kuid -tokens|^9|<:> -update|config.txt\temp|0 -restore|0|config.txt\temp -if|config.txt\temp=5 -combine|9|<kuid2:^3:^4:1> -restore|9|config.txt\kuid -endif -if|config.txt\temp=6 -compute|8|^5+1 -combine|9|<kuid2:^3:^4:^8> -restore|9|config.txt\kuid -endif -delete|config.txt\temp -saveconfig
How it Works
The first line of the macro is a comment telling us the purpose of the macro. Comment lines start with a semicolon " ; ". The rest of the line is ignored by the script interpreter.
;increment kuid version
AssetX has variables named 0, 1, 2 up to 9 that can store integers, floats or strings.
These are nominated by the carat " ^ " symbol as shown below in the tokens command.
To start our macro we firstly save the kuid value as a string into variable 9
-save|9|config.txt\kuid
The -tokens instruction takes the string stored in variable 9 and divides it into sub strings at the locations nominated by the letters/symbols in the second parameter of the instruction.
In this case we are dividing the kuid string at the "<", ":" and ">" locations.
-tokens|^9|<:>
divides the kuid string into
variable 0.. number of tokens in the string, in this case 5 or 6
variable 1.. empty string because "<" is at the beginning of the string.
variable 2.. "kuid" or "kuid2"
variable 3.. UserID number
variable 4.. AssetID number
variable 5.. Update number if kuid2 OR empty string if kuid
variable 6.. empty string
We now need to do a number comparison to determine how to handle the kuid or kuid2 cases.
AssetX does not support comparisons of variables so we must place the tokens count into a tag in the config.txt to be able to the do a count comparison with an -if command.
So we create a tag named "temp" and give it the value of tokens count.
-update|config.txt\temp|0 ....create a temporary tag in the config -restore|0|config.txt\temp ...save the tokens count to the temporary tag so we can use it in an "-if" instruction.
In the case of <kuid:1111:2222> the tokens count is 5.
-if|config.txt\temp=5 ....if tokens count is 5 then do the following up to the next -endif (or -else) statement.
We have the data available now to make kuid2 version 1. The -combine instruction joins the text and variable values together to make the new kuid string.
-combine|9|<kuid2:^3:^4:1> ..build string "<kuid2:userID:assetID:1>" into variable 9
Now we put the new kuid value in to the kuid tag of the config.txt.
-restore|9|config.txt\kuid ..put variable 9 string into kuid tag.
That's the end of processing for kuid2 version 1 values.
-endif
In the case of <kuid2:1111:2222:3> the tokens count is 6.
So if the token count is 6 then we aleady have a kuid2 with an Update value.
-if|config.txt\temp=6 ....if tokens count is 6 then
The update (version) number is stored in variable 5 so we add 1 to it and save to variable 8.
-compute|8|^5+1 ....increment update number and save in variable 8
We have the data available now to make kuid2 any version.
The -combine instruction joins the text and variable values together to make the new kuid string.
-combine|9|<kuid2:^3:^4:^8> ..builds string "<kuid2:userID:assetID:update>" into variable 9
Now we put the new kuid value in to the kuid tag of the config.txt.
-restore|9|config.txt\kuid ..put variable 9 string into kuid tag.
That's the end of processing for kuid2 for any version number
-endif
The tag "temp" must be removed. Trainz does not accept such a tag.
-delete|config.txt\temp ....delete temporary tag
Save the config.txt with its new kuid.
-saveconfig ......... finished
The macro can be made more bullet proof by inserting a few lines below after the line -update|config.txt\temp|0.
This prevents negative userID kuids from being processed.
;increment kuid version -save|9|config.txt\kuid -tokens|^9|<:> -update|config.txt\temp|0 -restore|3|config.txt\temp -if|config.txt\temp<0 -messagebox|Negative User numbers cannot have kuid2 values -exit -endif -restore|0|config.txt\temp -if|config.txt\temp=5 -combine|9|<kuid2:^3:^4:1> -restore|9|config.txt\kuid -endif -if|config.txt\temp=6 -compute|8|^5+1 -combine|9|<kuid2:^3:^4:^8> -restore|9|config.txt\kuid -endif -delete|config.txt\temp -saveconfig