So, to keep things clear here, I’m going to show you guys, and for anyone interested in the future, what I’ve got right now:
I’ve managed to find a “portable” C++ compiler called MinGW through the “portableapps.com”. Here’s the link: C/C++ compiler portable | PortableApps.com . As I researched, portable apps offers a program that takes a installer and remove it’s folder so we can use the program without any installation. The download link on the forum I quoted may be kind shady, but I ran a antivirus and scans and seems all good. The problem with this “portable” C++ compiler is that I have to add the “\bin” folder to the PATH environment variables. However, this should be fairly easy to do on player’s machine, as we can set it on runtime.
Next, I’ve created a pure C++ code to test what I’m aiming for here. Here’s the code:
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <fstream>
using namespace std;
typedef int (*importFunction) (int);
int main(){
//Writing library(This is be done by the student)
//Header
ofstream Header ( "tmp.h" );
Header << "#ifndef tmp_h__
#define tmp_h__
extern int MyFunc(int x)
#endif
";
Header.close();
//Implementation
ofstream Impl ( "tmp.c" );
Impl << "#include <stdio.h>
int MyFunc(int x){return x * 4;}
";
Impl.close();
//Compiling and creating the shared library through cmd(EnvironmentVariable must be set to use "gcc")
system("gcc -c -Wall -fpic C:/Users/Labtime/tmp.c");
system("gcc -shared -o tmp.dll C:/Users/Labtime/tmp.o");
//Loading library
HINSTANCE fLib = LoadLibraryW(L"C:\\Users\\Labtime\ mp.dll");
if(fLib){
cout << "Loaded Library
";
//Linking function and testing
importFunction testFunc;
testFunc = (importFunction)GetProcAddress(fLib,"MyFunc");
if(testFunc){
cout <<"Found function, executing:
";
cout << "Result:" << testFunc(2);
}
}
return 0;
}
As you can see, this is what I’m aiming for right now. On the first couple of lines I’ve created two files (tmp.c and tmp.h) to create a shared library out of it. On our game, the player will be the one who will write the “tmp.c” and we are working on creating the “tmp.h” at runtime by analizing the “tmp.c” from user’s input - This will be a kind difficult task to do, I believe, but we will get there. After that, I compile “tmp.c” and create a shared library from those “tmp.o” using our “portable” C++ compiler, running commands on CMD through the “system()” function. Lastly, we load the created library and link the functions so we can use it on our C++ code (as you can see in my example).
So how do we intend to run the player’s code? We are going to link the C code main() function to one of your functions, like “m_main() = (importFunction)GetProcAddress(fLib,“main”)” and then when the player clicks on a button like “Execute code”. We are going to call the “m_main()” we just linked and read the output log to collect the informations.
Of course, we need to make it all work inside Unreal, maybe using some Unreal functions like “FPlatformProcess::GetDllHandle” and “FPlatformProcess::GetDllExport”.