CS Code convertion

From PSwiki
Jump to navigation Jump to search

Code convertion

Types

csArray<> -> TArray

csString -> psString or FString

psStringArray -> TArray<FString>

If you need to have unique key-value pairs:

csHash<Value,Key> -> TMap<Key,Value> (notice the swap of Key and Value)

If you need have multiple values for the same key:

csHash<Value,Key> -> TMultiMap<Key,Value> (notice the swap of Key and Value)

csList -> TDoubleLinkedList

csPDelArray<PhonicEntry> -> TArray<PhonicEntry*>;

csRandomGen -> FMath::FRandRange(low, high);

csRef<iDocumentNode> containerNode -> FXmlNode *containerNode

csRefArray<csString> -> TArray<FString*>

csSquaredDist::PointPoint() -> FVector::Dist(v1,v2)

csTicks -> time_t

CS::Threading::AtomicOperations::Increment(&nextid); -> FThreadSafeCounter.Increment()

csTuple2 -> TPair

csVector3 -> FVector

?? csSet -> TArray ??

uint -> uint32

uint32_t -> #include <stdint.h>

iResultRow -> psResultRow

Types conversion

FString to char* -> const char *myData = TCHAR_TO_ANSI(*NameString);

FString to TCHAR -> *myFString

char* to FString -> ANSI_TO_TCHAR(NameString);

int to FString -> FString::FromInt(myInt);

int32 MyShinyNewInt = FCString::Atoi(*TheString);

float MyShinyNewFloat = FCString::Atof(*TheString);

Functions on csString

csString csv.AppendFmt(",%d,%d", fs->faction->id, fs->score); -> csv.Append(FString::Printf(TEXT(",%d,%d"), fs->faction->id, fs->score));

csString.AppendFmtV(fmt, args) -> vsprintf(line, fmt, args); script.Append(line);

csString.Clear() -> Empty()

csString.CompareNoCase() -> FString.Compare("ADD", ESearchCase::IgnoreCase)

csString.DeleteAt(start,count) -> FString.RemoveAt(start,count)

csString str.Detach() -> TCHAR_TO_ANSI(*str) detaches the data from the csString

csString.Downcase() -> FString.ToLower()

csString.Find(search, pos, ignore_case) -> FString.Find(search, ESearchCase::IgnoreCase)

csString end = resp.Find("]", start); -> int end = resp.Find("]",ESearchCase::IgnoreCase , ESearchDir::FromStart, start);

csString.FindFirst ('a') -> int pos; FString.FindChar(' ',pos);

csString.FindReplace (search, replace) -> FString.Replace(search, replace);

csString.FindSubString() -> FString.Find()

string.Format("hey %s", string) -> string.Printf(TEXT("hey %s"), string)

csString.GetAt(0) -> [0]

csString.GetData() -> TCHAR_TO_ANSI(*mystring)

csString.GetDataSafe() -> TCHAR_TO_ANSI(*mystring)

csString.Length() -> Len()

csString.LTrim() -> FString.Trim()

csString.Replace(from, to) -> FString.Replace(TEXT("fromtext"), *to);

csString.ReplaceSubString(search, replace) -> FString.Replace(search, replace)

csString.ReplaceAll(search, replace) -> FString.Replace(search, replace)

csString.Slice(5) -> FString.RightChop(5);

csString.Slice(0,5) -> FString.Left(5);

csString.Split(csStringArray& arr, char delim) -> FString.ParseIntoArray(arr,TEXT("."));

csString resp.SubString(saySegment,start,numchars); -> FString saySegment = resp.Mid(start, numchars);

csString response.StartsWith("<Examine>", false) -> response.StartsWith("<Examine>", ESearchCase::IgnoreCase)

csString.RTrim() -> FString.TrimTrailing()

csString.Truncate(5) -> FString.Left(5)

Functions other types

csPDelArray.GetSize() -> TArray.Num()

csPDelArray.Push() -> TArray.Add()

csArray.Delete(item) -> TArray.Remove(item)

csArray.DeleteAll() -> TArray.Empty()

csArray.DeleteIndex(i) -> TArray.RemoveAt(i)

csArray.DeleteIndexFast(i) -> TArray.RemoveAt(i)

csArray.FormatPush("%d",i); -> TArray.Add(FString::Printf(TEXT("%d",i));

csArray myarray.Get(0) -> TArray myarray[0]

csArray.Insert(index, item) -> TArray.Insert(item, index); (note switch of order)

csArray.InsertSorted(item) -> TArray.Add(item); TArray.Sort();

csArray.Put(index, item) -> TArray.RemoveAt(index); TArray.Insert(item, index); (note switch of order)

csArrayItemNotFound -> INDEX_NONE

csList.Delete(node) -> TDoubleLinkedList.RemoveNode(node)

csList.DeleteAll() -> TDoubleLinkedList.Empty()

csList.Front() -> TDoubleLinkedList.GetHead()

csList.IsEmpty() -> TDoubleLinkedList.Num()!=0

csList.PushBack() -> TDoubleLinkedList.AddTail() adds an element at the end of the list

csList.PushFront() -> TDoubleLinkedList.AddHead() adds an element at the beginning of the list

csList attackList.PopFront() -> attackList.RemoveNode(attackList.GetHead()); deletes an element at the beginning

caHash.DeleteAll(name) -> If used as TMap (meaning no duplicate keys -> TMap.Remove(name)

csHash.GetElementPointer(name) -> TMap.Find(name);

csHash.Put() -> TMap.Add()

csHash.PutUnique() -> TMap.Add() (we should test if this is ok)

csHash.Get(key,fallback) -> TMap.FindRef(key)

csHash<FactionStanding*, int>::GlobalIterator iter(factionstandings.GetIterator()); -> TMap<int, FactionStanding*>::TIterator iter = factionstandings.CreateIterator();

csHash if(iter.HasNext()) -> if(iter)

csHash iter.Next() -> iter.Value(); iter++;

csHash.GetSize() -> TMap.Num();

csGetTicks() -> time(0) Need to #include <ctime>

csTuple.first -> TPair.Key

csTuple.second -> TPair.Value

csVector.Norm() -> FVector.Size() calculates the magnitude/norm of the vector.

csVector v1 * v2 -> FVector::DotProduct(v1,v2)

csVector.Unit() -> FVector.GetSafeNormal()

output.Insert(0, time_buffer) -> output.InsertAt(0, time_buffer)

output.GetAt(output.Length()-1) -> output [ output.Len()-1 ]

Cross platform types are defined in Platform.h , like uint32. Those can be included with #include "EngineMinimal.h"

CS_ASSERT() -> check()

CS_ASSERT_MSG() -> checkf(expr, TEXT("%s caused the error"), str);

strcasecmp and strcmp, I added those two defines, so the code can stay as it is

 #define strcasecmp(expr1,expr2) FCString::Strcmp( ANSI_TO_TCHAR(expr1), ANSI_TO_TCHAR(expr2)) == 0
 #define strcmp(expr1,expr2) FCString::Stricmp( ANSI_TO_TCHAR(expr1), ANSI_TO_TCHAR(expr2)) == 0

time(NULL) -> FString buf = FDateTime().GetDate().ToString();

Logging and Error Messages

Verbosity levels:

  • Fatal: Always prints s fatal error to console (and log file) and crashes (even if logging is disabled)
  • Error: Prints an error to console (and log file)
  • Warning: Prints a warning to console (and log file)
  • Display: Prints a message to console (and log file)
  • Log: Prints a message to a log file (does not print to console)
  • Verbose: Prints a verbose message to a log file (if Verbose logging is enabled for the given category)

CPrintf(CON_CMDOUTPUT,str.GetDataSafe()); -> UE_LOG(LogTemp, Warning, TEXT("%s"), str);

CPrintf(CON_CMDOUTPUT,"name: %s",str.GetDataSafe()); -> UE_LOG(LogTemp, Warning, TEXT("name: %s"), str);

CPrintf(CON_ERROR, "psItemStats::GetProperty(%s) failed\n",ptr); -> UE_LOG(LogTemp, Error, TEXT("psItemStats::GetProperty(%s) failed\n"),ptr);

Debug3(LOG_ITEM, 0, "Set location in parent %d for %u", location, GetUID()); -> UE_LOG(LogTemp, Log, TEXT("psItemStats::GetProperty(%s) failed\n"),ptr);

Error3("Failed to insert trait '%s'.\n",t->name.GetData()); -> UE_LOG(LogTemp, Error, TEXT("Failed to insert trait '%s' into location table for race %d.\n"), t->name, t->raceID);

FText and Localization

FText is Unreal's class for user viewable strings. It is localizable. See https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/StringHandling/FText/index.html

XML parsing

  1. include "XmlParser.h"

iDocumentNode -> FXmlNode

   csRef<iDocument> doc=xml->CreateDocument();
   const char* error =doc->Parse(buff);

becomes

   FXmlFile File(FPaths::GameDir() + FString(PHONICS_LIST));
   FXmlNode* root = File.GetRootNode();

if its parsing a string and not a file, script is an FString

   FXmlFile File(script, EConstructMethod::ConstructFromBuffer);
   FXmlNode* root = File.GetRootNode();

Finding a specific child node: root->GetNode("FAMILY_NAME"); -> FXmlNode *familyName = root->FindChildNode("FAMILY_NAME");

   csRef<iDocumentNodeIterator> iter = attitudeNode->GetNodes("response"); -> 
   TArray<FXmlNode *> responses = attitudeNode->GetChildrenNodes();
   for (int i = 0; i < responses.Num(); i++)
       {
       FXmlNode *responseNode = responses[i];
       if (responseNode->GetTag().Equals("response")) {

csString skillName = tmp->GetAttributeValue("name"); -> tmp->GetAttribute(TEXT("name"));

float value = children[i]->GetAttributeValueAsFloat("value"); -> float value = FCString::Atof(*children[i]->GetAttribute("value"));

int cstr_id_material = node->GetAttributeValueAsInt( "mesh" ); -> int cstr_id_material = FCString::Atoi(*node->GetAttribute("mesh"));

id = node->GetContentsValueAsInt(); -> id = FCString::Atoi(*node->GetContent());

meshname = node->GetContentsValue(); -> meshname = node->GetContent();

posx = node->GetContentsValueAsFloat(); -> posx = FCString::Atof(*node->GetContent());

node->GetValue() -> node->GetTag() if applied to an element.

Platform specific includes

If you want to include some code for a specific platform you can use:


  1. ifdef PLATFORM_WINDOWS

your stuff here

  1. endif