Increment KUID Version

From TrainzOnline
Revision as of 16:52, 1 February 2017 by Pev (Talk | contribs)

Jump to: navigation, search

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 command takes the string stored in variable 9 and divides it into sub strings at the locations nominated by the letters/symbols in the next parameter in the command. In this case we are dividing the kuid string at the "<", ":" and ">" locations.

-tokens|^9|<:>

divides 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 applicable else empty string
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 comaprisons 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

If the token count is 6 then we aleady have a kuid2 value

-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.

-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
Personal tools