CS Code convertion

From PSwiki
Revision as of 21:50, 1 February 2017 by Talad (talk | contribs) (Types)
Jump to navigation Jump to search

Code convertion

Types

csArray<> -> TArray

csString -> psString or FString

psStringArray -> TArray<FString>

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

csList -> TDoubleLinkedList

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

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

csTicks -> time_t

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);


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()

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.Truncate(5) -> FString.Left(5)

Functions other types

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

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

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

csArrayItemNotFound -> INDEX_NONE

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

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

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++;

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

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: FXmlNode *familyName = root->FindChildNode("FAMILY_NAME");

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"));

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