If you want to help us maintaining this wiki, check out our discord server: https://discord.gg/3u69jMa 

Difference between revisions of "Republic Commando UCC"

From SWRC Wiki
Jump to navigation Jump to search
Line 189: Line 189:


//Entry point, called by modified SWRepublicCommando.exe
//Entry point, called by modified SWRepublicCommando.exe
__declspec(dllexport) void uccInit(const TCHAR* InPackage, const TCHAR* InCmdLine, FOutputDevice* InLog, FOutputDeviceError* InError,
__declspec(dllexport) void uccMain(const TCHAR* InPackage, const TCHAR* InCmdLine, FOutputDevice* InLog, FOutputDeviceError* InError,
  FFeedbackContext* InWarn, FConfigCache*(*ConfigFactory)(), UBOOL RequireConfig){
  FFeedbackContext* InWarn, FConfigCache*(*ConfigFactory)(), UBOOL RequireConfig){
int ExitVal = EXIT_SUCCESS;
int ExitVal = EXIT_SUCCESS;
Line 279: Line 279:
}
}


if(WarningCount > 0)
appPreExit(); //For some reason this crashes (sometimes?) when there are compiler errors with ucc make
appPreExit(); //For some reason this crashes when there are compiler errors with ucc make


GIsGuarded = 0;
GIsGuarded = 0;

Revision as of 14:58, 26 November 2017

Author: Leon

Used Tools: MS Visual Studio 2003

Description: A self written UCC for Republic Commando. Used for executing Unreal Commandlets.

Github: here

Latest Build: here


UCC.cpp

/*
*	This code compiles to a dll. In order to use it, SWRepublicCommando.exe needs to be modified so
*	that it calls this dll's 'uccInit' instead of 'appInit' from Core.dll
*	Also it's subsystem should be changed from window to console
*	Everything compiles fine with Visual Studio .NET 2003 which is being used to get maximum compatibility
*	since it was also used to compile Republic Commando
*	The following settings are required in order to compile everything without errors:
*		- Character Set = Not Set
*		- Struct Member Alignment = 4 Bytes
*		- Calling Convention = __fastcall
*/

#pragma comment(lib, "Core.lib")
#pragma comment(lib, "Engine.lib")

#include <Windows.h>	//VirtualProtect

#include <cstdio>	//std::puts
#include <cstdlib>	//__argc, __argv
#include <cctype>	//std::toupper
#include <string>

//Core and Engine
#include "Engine/inc/Engine.h"

namespace{
	//Global variables

	FOutputDevice* FileLog;	//OutputDevice that writes to a log file
	int WarningCount = 0;	//Incremented whenever a warning occurs in FFeedbackContextAnsiSerialize
	int ErrorCount = 0;	//Same as WarningCount...

	//All commandlets shipped with RC, used for autocompleting user input
	const char* DefaultCommandlets[40][2] = {
		{"Editor", "AnalyzeContent"},
		{"Editor", "BatchExport"},
		{"Editor", "ChecksumPackage"},
		{"Editor", "CheckUnicode"},
		{"Editor", "CompareInt"},
		{"IpDrv", "Compress"},
		{"Editor", "CompressToDXT"},
		{"Editor", "Conform"},
		{"Editor", "ConvertMaterial"},
		{"Editor", "CutdownContent"},
		{"Editor", "DataRip"},
		{"IpDrv", "Decompress"},
		{"XGame", "DumpGameList"},
		{"Editor", "DumpInt"},
		{"XGame", "DumpMapList"},
		{"XGame", "DumpMutatorList"},
		{"Editor", "DumpSoundParams"},
		{"Editor", "DumpSoundPropLog"},
		{"XGame", "DumpWeaponList"},
		{"Editor", "DXTConvert"},
		{"Editor", "Exec"},
		{"Editor", "FontUpdate"},
		{"Core", "HelloWorld"},
		{"Editor", "ImportAse"},
		{"Editor", "ImportTexture"},
		{"Editor", "ListPackageContents"},
		{"Editor", "Make"},
		{"Editor", "MapConvert"},
		{"Editor", "Master"},
		{"Editor", "MergeInt"},
		{"Editor", "ModifyPackageFlags"},
		{"Editor", "PackageFlag"},
		{"Editor", "Pkg"},
		{"Editor", "RearrangeInt"},
		{"Editor", "ResavePackage"},
		{"Engine", "Server"},
		{"Editor", "StripSource"},
		{"Editor", "TestMath"},
		{"Editor", "UpdateUMod"},
		{"Editor", "XACTExport"}
	};

	//Helper function used for case-insensitive string comparisons
	std::string ToUpper(const std::string& s){
		std::string result;

		result.reserve(s.size());

		for(std::size_t i = 0; i < s.size(); i++)
			result += std::toupper(s[i]);

		return result;
	}

	/*
	*	Replacement for Serialize function of 'InWarn', passed to appInit
	*	Have to do this vtable hack because creating new FFeedbackContext and passing it results in crash
	*/
	void __stdcall FFeedbackContextAnsiSerialize(const TCHAR* V, EName Event){
		TCHAR Buffer[1024]= "";
		const TCHAR* Temp = V;

		if(Event==NAME_Title){
			return;
		}else if(Event==NAME_Heading){
			appSprintf(Buffer, "\n--------------------%s--------------------", V);

			Temp = Buffer;
		}else if(Event==NAME_SubHeading){
			appSprintf(Buffer, "%s...", V);

			Temp = Buffer;
		}else if(Event == NAME_Warning || Event == NAME_ExecWarning || Event == NAME_ScriptWarning){
			appSprintf(Buffer, "%s: %s", *FName(Event), V);

			WarningCount++;

			Temp = Buffer;
		}else if(Event == NAME_Error || Event == NAME_Critical){
			appSprintf(Buffer, "%s: %s", *FName(Event), V);

			ErrorCount++;

			V = Temp;
		}

		std::puts(Temp);

		FileLog->Serialize(V, Event);
	}

	/*
	*	Replacement for UServerCommandlet::Main since the one from Engine.dll crashes because it doesn't assign a value to GEngine
	*/
	void UServerCommandletMain(){
		UClass* EngineClass = UObject::StaticLoadClass(UEngine::StaticClass(), NULL, "Engine.GameEngine", NULL, LOAD_NoFail, NULL);

		GEngine = ConstructObject<UEngine>(EngineClass);

		GEngine->Init();
		
		GIsRunning = 1;

		double OldTime = appSeconds();

		//Main loop
		while(GIsRunning && !GIsRequestingExit){
			double NewTime = appSeconds();

			//Update the world
			GEngine->Tick(NewTime - OldTime);
			OldTime = NewTime;

			//Enforce optional maximum tick rate
			float MaxTickRate = GEngine->GetMaxTickRate();

			if(MaxTickRate > 0.0f){
				float Delta = (1.0f / MaxTickRate) - (appSeconds() - OldTime);

				appSleep(Delta > 0.0f ? Delta : 0.0f);
			}
		}
	}

	/*
	*	Returns a specific property of a commandlet, valid properties are: IsClient, IsEditor, IsServer, LazyLoad, ShowErrorCount
	*	This function launches a Commandlet itself, that gets the proerty in UnrealScript and returns it. This is necessary because
	*	the UT99 headers are (obviously) not fully compatible with RC and thus getting default proerties via UClass::GetDefaultObject
	*	doesn't work. This however, works fine. The only downside is that there needs to exist a package with this custom commandlet...
	*/
	int GetCommandletProperty(std::string Cmdlet, std::string Property){
		static UClass* Class = UObject::StaticLoadClass(UCommandlet::StaticClass(), NULL, "UCC.GetCommandletPropertiesCommandlet", NULL, 0, NULL);
		static UCommandlet* Commandlet = ConstructObject<UCommandlet>(Class);

		Commandlet->InitExecution();
		Commandlet->ParseParms(("CommandletClass=" + Cmdlet).c_str());

		return Commandlet->Main(FString(Property.c_str()));
	}
}

//Entry point, called by modified SWRepublicCommando.exe
__declspec(dllexport) void uccMain(const TCHAR* InPackage, const TCHAR* InCmdLine, FOutputDevice* InLog, FOutputDeviceError* InError,
								   FFeedbackContext* InWarn, FConfigCache*(*ConfigFactory)(), UBOOL RequireConfig){
	int ExitVal = EXIT_SUCCESS;
	
	FileLog = InLog;

	//vtable hack to have InWarn call my own Serialize function
	//===============================================================
	PVOID* vtable = *reinterpret_cast<PVOID**>(InWarn);
	DWORD dwNull;

	VirtualProtect(&vtable[0], 4, PAGE_EXECUTE_READWRITE, &dwNull);

	vtable[0] = FFeedbackContextAnsiSerialize;
	//===============================================================

	InWarn->Log("=======================================");
	InWarn->Log("ucc.exe for Star Wars Republic Commando");
	InWarn->Log("made by Leon0628");
	InWarn->Log("=======================================\n");

	try{
		GIsStarted = 1;
		GIsGuarded = 1;

		appInit(InPackage, InCmdLine, InLog, InError, InWarn, ConfigFactory, 1);
		UObject::SetLanguage("int");

		if(__argc > 1){
			//Initializing global state
			GIsUCC = GIsClient = GIsServer = GIsEditor = GIsScriptable = GLazyLoad = 1;

			std::string Token = ToUpper(__argv[1]);
			std::string ClassName = Token;
			DWORD LoadFlags = LOAD_NoWarn | LOAD_Quiet;

			//Looking it up in list of default commandlets and if found constructing proper class name
			for(int i = 0; i < ARRAY_COUNT(DefaultCommandlets); i++){
				if(Token == ToUpper(DefaultCommandlets[i][1]) ||	//Name
				   Token == ToUpper(DefaultCommandlets[i][1]) + "COMMANDLET"){	//Name + "Commandlet"
					
					ClassName = std::string(DefaultCommandlets[i][0]) + "." + DefaultCommandlets[i][1] + "Commandlet";

					break;
				}
			}

			if(Token == "MAKE" || Token == "MAKECOMMANDLET" || Token == "EDITOR.MAKE" || Token == "EDITOR.MAKECOMMANDLET")
				LoadFlags |= LOAD_DisallowFiles;	//Not sure what this does but the original ucc has it as well...

			UClass* Class = UObject::StaticLoadClass(UCommandlet::StaticClass(), NULL, ClassName.c_str(), NULL, LoadFlags, NULL);

			if(!Class)	//If class failed to load, appending "Commandlet" and trying again
				Class = UObject::StaticLoadClass(UCommandlet::StaticClass(), NULL, (ClassName + "Commandlet").c_str(), NULL, LoadFlags, NULL);

			if(Class){
				UCommandlet* Commandlet = ConstructObject<UCommandlet>(Class);

				InWarn->Logf("Executing %s\n", Class->GetFullName());

				GIsClient = GetCommandletProperty(ClassName, "IsClient");
				GIsEditor = GetCommandletProperty(ClassName, "IsEditor");
				GIsServer = GetCommandletProperty(ClassName, "IsServer");
				GLazyLoad = GetCommandletProperty(ClassName, "LazyLoad");

				Commandlet->InitExecution();
				Commandlet->ParseParms(InCmdLine);

				GLog = InWarn;	//Redirecting commandlet output to console
				
				if(Token == "SERVER" || Token == "SERVERCOMMANDLET" || Token == "ENGINE.SERVER" || Token == "ENGINE.SERVERCOMMANDLET"){
					UServerCommandletMain();	//The ServerCommandlet has a special Main function
				}else if(!Commandlet->FindFunction(NAME_Main))	//If no main function is found, the commandlet is written in native code
					Commandlet->Main(InCmdLine);
				else{
					Commandlet->Main(FString(InCmdLine));	//For non-native commandlets this overload has to be used
				}

				if(GetCommandletProperty(ClassName, "ShowErrorCount"))
					InWarn->Logf("\n%i error(s), %i warnings", ErrorCount, WarningCount);

				GLog = FileLog;
			}else{
				InWarn->Logf("Commandlet %s not found", __argv[1]);
			}
		}else{
			InWarn->Log("Usage:");
			InWarn->Log("    ucc CommandletName <parameters>");
		}

		appPreExit();	//For some reason this crashes (sometimes?) when there are compiler errors with ucc make

		GIsGuarded = 0;
	}catch(...){
		ExitVal = EXIT_FAILURE;
		GIsGuarded = 0;
		InError->HandleError();
	}

	appExit();
	std::exit(ExitVal);	//Needed in order to prevent this function from returning to SWRepublicCommando.exe
}