The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Legacy:El Muerte TDS/WUtils

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

Latest version: 103

download the latest version

UT2003 has a nice set of methods and structures defined in XGame.xUtils, but there are more common used methods and don't need to be rewritten every time somebody needs them. That's why I've started wUtils to create a package containing another set of usefull methods.

Feel free to contribute more usefull methods to this package.

You are completely free to use wUtils and distribute it with your mod. You may also use only part of the code of wUtils. You are not allowed to distribute a changed version of wUtils under the same name, this is to prevent confusion.

Note: if you use wUtils in your mod, please check to this page to check if there's a newer version available before you release the mod.

Note 2: feel free to contribute methods/classes/... you think should belong into this package.

Usage

To use the methods from wUtils place wUtils in your EditPackages list.

Then call the methods via:

class'wUtilsClass'.static.method()

for example:

mystring = class'wString'.static.Trim(mystring);

Classes of wUtils

wString

function string StrShift(out string line, string delim, optional string quotechar)
this will shift the first element of the line (delimited by delim). fixed: delim can be longer that 1 char (v101) added: optional quote char so you can use: "one item" "second item" (v103)
function string StrReplace(coerce string target, array<string> replace, array<string> with, optional bool bOnlyFirst)
string replace that accepts arrays, usefull if you want to implement a bad word filter, optional bOnlyFirst added in v101
function string Lower(coerce string Text)
like Caps but lowers strings
function bool IsUpper(coerce string S)
check if a string is all uppercase
function bool IsLower(coerce string S)
check if a string is all lowercase
function string LTrim(coerce string S)
trims leading spaces/tabs
function string RTrim(coerce string S)
trims trailing spaces/tabs
function string Trim(coerce string S)
trims leading and trailing spaces/tabs
function bool MaskedCompare(coerce string target, string mask, optional bool casesensitive)
check target using mask, mask can contain the following wildcards: * for X chars, or ? for 1 char, check my source if it can be optimized.
function int InStrFrom(coerce string StrText, coerce string StrPart, optional int OffsetStart)
like InStr except you can specify where to start the search.
function bool ReplaceOption(out string Options, string Key, string NewValue, optional bool bAddIfMissing, optional out string OldValue, optional string delim) 
Allows you to quickly change a value in the Options string used in Login and PostLogin. (Source)
function string Capitalize(coerce string S) (v101)
will change "this is a string" to "This Is A String"
function int Split2(coerce string src, string delim, out array<string> parts, optional bool ignoreBlanks, optional string quotechar) (v101)
fixed version of split. Fixed: first char can be a delim, last char can be a space. Added: delim as a string. Added: ignoreblanks: "||" results in an empty array, and quotechar. (v103)
function string StrSubst(coerce string target, optional string r0, optional string r1, ... , optional string r9) (v102)
StrSubst will replace the occurances of %s in target with the value of r#, where # is the n'th occurance of %s
function string ReplaceInString(coerce string src, int from, int length, coerce string with) (v102) 
replaces part of a string: ReplaceInString("A stupid string.", 2, 6, "good") == "A good string."

nomad: I was thinking of doing something similar to wUtils, good job I came across this page. I've added InStrPlus() and ReplaceOption() with links to the source code.

El Muerte TDS: took Mychaeel's InStrFrom which is better, also changed a bit of code of ReplaceOption

Wormbo: How about a function that replaces a part of a string based on a given start and length of the section to replace?
e.g.: ReplaceInString("A stupid string.", 2, 6, "good") == "A good string."

El Muerte TDS: that's indeed a usefull function, I'll add it to the next release. Update: ReplaceInString requires more lines to call then to realy implement :)

wMath

function bool IsInt(coerce string Param, optional bool bPositiveOnly) 
checks if param is an integer
function bool IsFloat(coerce string Param, optional bool bPositiveOnly) 
checks if param is a float (or int)
function CRC32Init(out int CrcTable[256]) (v101) 
init function to call (once) before you use CRC32
function int CRC32(coerce string Text, int CrcTable[256]) (v101) 
calculate the CRC32 hash of a string, more info: CRC32
function int PowerMod(int C, int D, int N) (v102) 
this will calculate x^e mod y
function int RSAPublicKeygen(int p, int q) (v102) 
calculate encode key using primes P and Q, RSA
function int RSAPrivateKeygen(int E, int p, int q) (v102) 
calculate decode key, RSA
function RSAEncode(coerce string data, int E, int N, out array<int> data2) (v102) 
encode data using the keys E and N, RSA
function string RSADecode(array<int> data, int D, int N) (v102) 
encode data using the keys D and N, RSA

wArray

function string Join(array< string > ar, optional string delim, optional bool bIgnoreEmpty) 
join the array elements to a string using delimiter 'delim', added the optional bIgnoreEmpty (v101)
function string ShiftS(out array< string > ar) 
shift the first element of an string array
function object ShiftO(out array< object > ar) 
shift the first element of an object array
function int ShiftI(out array< int > ar) 
shift the first element of an int array
function array<string> AddS(array<string> A, array<string> B)
join the two arrays
function array<int> AddI(array<int> A, array<int> B)
join the two arrays
function array<object> AddO(array<object> A, array<object> B)
join the two arrays
function array<string> RemoveS(array<string> A, array<string> B)
remove elements in B from A
function array<int> RemoveI(array<int> A, array<int> B)
remove elements in B from A
function array<object> RemoveO(array<object> A, array<object> B)
remove elements in B from A
function SortS(out array<string> ar, optional bool bCaseInsenstive) 
sort a string array
function SortI(out array<int> ar) 
sort an int array
function int MaxI(array<int>) (v101)
get the highest value of an array
function int MinI(array<int>) (v101)
get the lowest value of an array
function string MaxS(array<string>) (v101)
get the highest value of an array
function string MinS(array<string>) (v101)
get the lowest value of an array
function int BinarySearch(array<string> Myarray, string SearchString, optional int Low, optional int High, optional bool bIgnoreCase) (v103)
searches a sorted array for a matching element, returns the index if found or -1
function string GetCommonBegin(array<string> slist) (v103)
returns the common beginning of elements in the array

Thoughts

Operators require you to define a precedence: operator(24) for example. This is only to define what operator comes first, for example: 1+2*3+4 will get a bullshit result if the operator precedence isn't set correctly. Deciding the operator precedence for numeric types is easy, but what to do for objects and strings, how do I decide what is more important. If you can't decide which of the two is more important then their precendence is equal.

Bah, operators suck.

nomad: Can you define new operators in UScript?? A string version of += would be nice.

El Muerte [TDS]: Yes you can define new operators, but you can use them from a general class like wUtils, that's why operators suck. Maybe this could have been possible if "import package" did something usefull.

Wormbo: Two small requests/suggestions: You could name the packages xUtils<version>.u to prevent version conflicts. And you could zip them without the wUtils directory in the ZIP file.

El Muerte TDS: Got a good point there. And WinZip sucks, where can I display the feature to include the dir name when compressing a directory ?

Wormbo: Currently the zip file content is \wUtils\wUtils.u, \wUtils\Readme.txt, \wUtils\source.zip and \wUtils\Licence.txt. Archiving them without that wUtils directory in them strikes me as more efficient. With Winzip (or any other archiver) you only select all the files and put them in the archive. In Winzip's Add dialog there should be an option called "Preserve Pathnames" or "Save Pathnames" or something like that.

For zipping a complex directory structure you have to prepare the directories before running Winzip.

wUtils      this one will be added to the zip file
 +- Help
 +- wUtils  the source files, maybe just put the source.zip here
 |   +- Classes
 + System

With Winzip 7.0 (the version I'm using) you have to select the wUtils directory and choose "add to zip" from the context menu. Other programs (e.g. Winrar) allow (or even require) you to select the subdirectories and add them to the archive, but Winzip 7 only puts the files in those subdirectories directly into the zip file.

El Muerte TDS: WinZip 7 works fine, but in WinZip 8 something changed that it will add the directory name when you compress it, I think I'm going to switch back to 7


Wormbo: How about a wGraphics or wDraw class? (or think up a better name if you want) It could offer draw fucntions like the ones from UT's UWindowWindow or UT2k3's HudBase, only better of course. :)

El Muerte TDS: sure, as long as it can be static

Wormbo: The static shouldn't be the problem here.

Mychaeel: If you make the functions both final and static and then try to call such a function on an uninitialized variable of that class... do you get an "Accessed None" error message or does the engine realize that the variable's type (not its value) is all it needs to identify the called function...?

Wormbo: I guess that's worth a try, but I'm almost sure this will create an Accessed None, even though the final keyword would make it pretty clear which function would be executed.

Downloads

Release 100 (includes source)

Release 101 (includes source)

Release 102 (includes source)

Release 103 (includes source)

CVS access

If you want bleeding edge you can pull the latest version from my CVS

CVS Web Repository 
http://el-muerte.student.utwente.nl/cgi-bin/cvsweb/UT2003/wUtils
CVS Root for anonymous access 
:pserver:anonymous@el-muerte.student.utwente.nl:/usr/local/cvsroot/UT2003