Skip to content

Commit

Permalink
Merge pull request #109 from Xaleros/master
Browse files Browse the repository at this point in the history
Implement P8 Texture + FractalTexture export
  • Loading branch information
dpjudas authored May 27, 2024
2 parents f790b19 + 6258cbe commit 9d06a71
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 77 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ set(SURREALCOMMON_SOURCES
SurrealEngine/UI/Launcher/SettingsPage.h
SurrealEngine/Window/Window.cpp
SurrealEngine/Window/Window.h
SurrealEngine/Lib/MemoryStreamWriter.h
SurrealEngine/Lib/MemoryStreamWriter.cpp
)

set(SURREALCOMMON_WIN32_SOURCES
Expand Down Expand Up @@ -505,6 +507,7 @@ source_group("SurrealEngine\\Commandlet\\Debug" REGULAR_EXPRESSION "${CMAKE_CURR
source_group("SurrealEngine\\Commandlet\\VM" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Commandlet/VM/.+")
source_group("SurrealEngine\\Commandlet\\Native" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Commandlet/Native/.+")
source_group("SurrealEngine\\Editor" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Editor/.+")
source_group("SurrealEngine\\Lib" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Lib/.+")
source_group("SurrealEngine\\GC" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/GC/.+")
source_group("SurrealEngine\\VM" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/VM/.+")
source_group("SurrealEngine\\Window" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Window/.+")
Expand Down
152 changes: 119 additions & 33 deletions SurrealEngine/Commandlet/ExportCommandlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ExportCommandlet::ExportCommandlet()

static ExportCommand GetCommand(const std::string& command)
{
static std::pair<char*, ExportCommand> commands[] =
static std::pair<std::string, ExportCommand> commands[] =
{
{"all", ExportCommand::All},
{"scripts", ExportCommand::Scripts},
Expand All @@ -46,10 +46,10 @@ static ExportCommand GetCommand(const std::string& command)
std::string lowerCmd = command;
std::transform(lowerCmd.begin(), lowerCmd.end(), lowerCmd.begin(), [](unsigned char c) { return tolower(c); });

for (int i = 0; i < (sizeof(commands) / sizeof(std::pair<char*, ExportCommand>)); i++)
for (int i = 0; i < (sizeof(commands) / sizeof(std::pair<std::string, ExportCommand>)); i++)
{
auto& iter = commands[i];
if (lowerCmd == iter.first)
if (lowerCmd.compare(iter.first) == 0)
return iter.second;
}

Expand All @@ -64,6 +64,8 @@ void ExportCommandlet::OnCommand(DebuggerApp* console, const std::string& args)
return;
}

packageNames.clear();

std::string argsStripped = args.substr(0, args.find_last_not_of(' ') + 1);

size_t argsSep = argsStripped.find_first_of(' ');
Expand Down Expand Up @@ -116,36 +118,21 @@ void ExportCommandlet::OnCommand(DebuggerApp* console, const std::string& args)
console->WriteOutput("Done." + NewLine() + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportAll(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportScripts(DebuggerApp* console, std::vector<std::string>& packages)
{
// TODO: export into game directory
std::string path = console->launchinfo.gameExecutableName + "-" + console->launchinfo.gameVersionString + "-Scripts";
Directory::make_directory(path);
InitExport(packages);

Engine engine(console->launchinfo);

std::vector<std::string> packageNames;
if (packages.size() == 0)
{
std::vector<NameString> packageNameStrings = engine.packages->GetPackageNames();
for (NameString pkgname : packageNameStrings)
packageNames.push_back(pkgname.ToString());
}
else
{
for (std::string pkgname : packages)
packageNames.push_back(pkgname);
}

// sort package names alphabetically
std::sort(packageNames.begin(), packageNames.end());

console->WriteOutput("Checking all packages..." + NewLine());
console->WriteOutput("Checking all packages..." + NewLine());

// cull out packages without scripts
std::vector<PackageNamePair> packageObjects;
Expand All @@ -154,69 +141,168 @@ void ExportCommandlet::ExportScripts(DebuggerApp* console, std::vector<std::stri
if (pkgname == "Editor")
continue;

Package* package = engine.packages->GetPackage(pkgname);
std::vector<UClass*> classes = package->GetAllClasses();
Package* package = engine->packages->GetPackage(pkgname);
std::vector<UClass*> classes = package->GetAllObjects<UClass>();
if (!classes.empty())
packageObjects.push_back(PackageNamePair(package, pkgname));
}

if (packageObjects.size() == 0)
{
console->WriteOutput("No scripts found");
return;
}

for (PackageNamePair& pkgobject : packageObjects)
{
Package* package = pkgobject.first;
std::string& name = pkgobject.second;

std::string pkgname = package->GetPackageName().ToString();
std::string pkgpath = FilePath::combine(path, name);
std::string pkgpath = FilePath::combine(engine->LaunchInfo.gameRootFolder, name);
std::string classespath = FilePath::combine(pkgpath, "Classes");
bool pkgpathcreated = false;

console->WriteOutput("Exporting scripts from " + ColorEscape(96) + name + ResetEscape() + NewLine());

std::vector<UClass*> classes = package->GetAllClasses();
std::vector<UClass*> classes = package->GetAllObjects<UClass>();

for (UClass* cls : classes)
{
std::string txt = Exporter::ExportClass(cls);
if (txt.size() > 0)
MemoryStreamWriter stream = Exporter::ExportClass(cls);
if (stream.Size() > 0)
{
if (!pkgpathcreated)
{
Directory::make_directory(pkgpath);
Directory::make_directory(classespath);
pkgpathcreated = true;
}
std::string filename = FilePath::combine(pkgpath, cls->FriendlyName.ToString() + ".uc");
File::write_all_text(filename, txt);
std::string filename = FilePath::combine(classespath, cls->FriendlyName.ToString() + ".uc");
File::write_all_bytes(filename, stream.Data(), stream.Size());
}
}
}
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportTextures(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
InitExport(packages);

if (packages.size() == 0)
console->WriteOutput("Checking all packages..." + NewLine());

// cull out packages without textures
std::vector<PackageNamePair> packageObjects;
for (std::string pkgname : packageNames)
{
if (pkgname == "Editor")
continue;

Package* package = engine->packages->GetPackage(pkgname);
std::vector<UTexture*> objects = package->GetAllObjects<UTexture>();
if (!objects.empty())
packageObjects.push_back(PackageNamePair(package, pkgname));
}

if (packageObjects.size() == 0)
{
console->WriteOutput("No textures found" + NewLine());
return;
}

for (PackageNamePair& pkgobject : packageObjects)
{
Package* package = pkgobject.first;
std::string& name = pkgobject.second;

std::string pkgname = package->GetPackageName().ToString();
std::string pkgpath = FilePath::combine(engine->LaunchInfo.gameRootFolder, name);
std::string texturespath = FilePath::combine(pkgpath, "Textures");
bool pkgpathcreated = false;

console->WriteOutput("Exporting textures from " + ColorEscape(96) + name + ResetEscape() + NewLine());

std::vector<UTexture*> textures = package->GetAllObjects<UTexture>();

for (UTexture* tex : textures)
{
// TODO: support more formats than just bmp
std::string ext;
if (tex->IsA("FractalTexture"))
ext.assign("fx");
else
ext.assign("bmp");

MemoryStreamWriter stream = Exporter::ExportTexture(tex, ext);
if (stream.Size() > 0)
{
if (!pkgpathcreated)
{
Directory::make_directory(pkgpath);
Directory::make_directory(texturespath);
pkgpathcreated = true;
}

std::string filename = FilePath::combine(texturespath, tex->Name.ToString() + "." + ext);
File::write_all_bytes(filename, stream.Data(), stream.Size());
}
}
}
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportSounds(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportMusic(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportMeshes(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::ExportLevel(DebuggerApp* console, std::vector<std::string>& packages)
{
console->WriteOutput("Unimplemented" + NewLine());
}

/////////////////////////////////////////////////////////////////////////////

void ExportCommandlet::OnPrintHelp(DebuggerApp* console)
{
console->WriteOutput("Syntax: export <command> (packages)" + NewLine());
console->WriteOutput("Commands: all scripts textures sounds music meshes level" + NewLine());
}

void ExportCommandlet::InitExport(std::vector<std::string>& packages)
{
if (packages.size() == 0)
{
std::vector<NameString> packageNameStrings = engine->packages->GetPackageNames();
for (NameString pkgname : packageNameStrings)
packageNames.push_back(pkgname.ToString());
}
else
{
for (std::string pkgname : packages)
packageNames.push_back(pkgname);
}

// sort package names alphabetically
std::sort(packageNames.begin(), packageNames.end());
}
4 changes: 4 additions & 0 deletions SurrealEngine/Commandlet/ExportCommandlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class ExportCommandlet : public Commandlet
void ExportMusic(DebuggerApp* console, std::vector<std::string>& packages);
void ExportMeshes(DebuggerApp* console, std::vector<std::string>& packages);
void ExportLevel(DebuggerApp* console, std::vector<std::string>& packages);

void InitExport(std::vector<std::string>& packages);

std::vector<std::string> packageNames;
};
2 changes: 1 addition & 1 deletion SurrealEngine/Commandlet/Native/NativeFuncExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ JsonValue NativeFuncExtractor::CreatePackageJson(Package* package)
{
JsonValue jsonPackage = JsonValue::object();

std::vector<UClass*> classes = package->GetAllClasses();
std::vector<UClass*> classes = package->GetAllObjects<UClass>();
for (UClass* cls : classes)
{
JsonValue jsonClass = CreateClassJson(cls);
Expand Down
2 changes: 1 addition & 1 deletion SurrealEngine/Commandlet/Native/NativeObjExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ JsonValue NativeObjExtractor::CreatePackageJson(Package* package)
{
JsonValue jsonPackage = JsonValue::object();

std::vector<UClass*> classes = package->GetAllClasses();
std::vector<UClass*> classes = package->GetAllObjects<UClass>();
for (UClass* cls : classes)
{
if (AllFlags(cls->Flags, ObjectFlags::Native))
Expand Down
Loading

0 comments on commit 9d06a71

Please sign in to comment.