Skip to content

Instantly share code, notes, and snippets.

@kklouzal
Created August 22, 2014 14:20
Show Gist options
  • Save kklouzal/60d33458a31ff21de3b8 to your computer and use it in GitHub Desktop.
Save kklouzal/60d33458a31ff21de3b8 to your computer and use it in GitHub Desktop.
RegisterProps()
void RegisterProps()
{
irr::io::IFileSystem* FS = Infinity::Graphics::GetDevice()->getFileSystem();
const irr::io::path OriginalWorkingDirectory = FS->getWorkingDirectory();
irr::io::IFileList* Files;
std::vector<const irr::io::path> SearchDirectories;
FindDirectoriesInDirectory(FS, SearchDirectories, "./Models/");
lua_State* L = Infinity::Lua::GetState();
for (auto Dir : SearchDirectories)
{
FS->changeWorkingDirectoryTo(Dir);
Files = FS->createFileList();
FS->changeWorkingDirectoryTo(OriginalWorkingDirectory);
for (unsigned int i = 0; i < Files->getFileCount(); i++)
{
if (Files->isDirectory(i)) { continue; }
if (FileIsType(Files->getFileName(i), ".x"))
{
std::string PropIdentifier(Dir.c_str());
PropIdentifier += Files->getFileName(i).c_str();
PropIdentifier.erase(0, 2);
//
// Get the first occurance of '/'
std::string::size_type Pos = PropIdentifier.find("/");
//
// -1 | Entity.Prop.__Registered
Infinity::Lua::Bindings::PushTable_Entity_Prop_Registered();
//
// Loop while we still have more '/' in the string
while (Pos != std::string::npos)
{
//
// Get the next occurance of '/'
std::string::size_type NextPos = PropIdentifier.find("/", Pos + 1);
//
// If there actually was another occurance of '/'
if (NextPos != std::string::npos)
{
//
// Formulate a substring of the characters between the two '/'
std::string FolderName = PropIdentifier.substr(Pos + 1, NextPos - Pos - 1);
std::cout << FolderName.c_str() << std::endl;
// -2 | Entity.Prop.__Registered
// -1 | Entity.Prop.__Registered.FolderName
lua_getfield(L, -1, FolderName.c_str());
if (lua_isnoneornil(L, -1))
{
// -1 | Entity.Prop.__Registered
lua_remove(L, -1);
// -2 | Entity.Prop.__Registered
// -1 | Entity.Prop.__Registered.FolderName
lua_newtable(L);
// -3 | Entity.Prop.__Registered
// -2 | Entity.Prop.__Registered.FolderName
// -1 | Folder Icon String
lua_pushstring(L, "unset");
// -2 | Entity.Prop.__Registered
// -1 | Entity.Prop.__Registered.FolderName
lua_setfield(L, -2, "__FolderIcon");
// -3 | Entity.Prop.__Registered
// -2 | Entity.Prop.__Registered.FolderName
// -1 | Entity.Prop.__Registered.FolderName
lua_pushvalue(L, -1);
// -2 | Entity.Prop.__Registered
// -1 | Entity.Prop.__Registered.FolderName
lua_setfield(L, -3, FolderName.c_str());
// -1 | Entity.Prop.__Registered.FolderName
lua_remove(L, -2);
}
}
Pos = NextPos;
}
std::cout << std::endl;
//
// Check to make sure this prop hasnt already been registered somehow
// -2 | Entity.Prop.__Registered.FolderName
// -1 | Prop Table
lua_getfield(L, -1, PropIdentifier.c_str());
if (lua_isnoneornil(L, -1))
{
// -1 | Entity.Prop.__Registered.FolderName
lua_remove(L, -1);
// -1 | Entity.Prop.__Registered.FolderName
// -1 | New Registered Prop Table
lua_newtable(L);
// -3 | Entity.Prop.__Registered.FolderName
// -2 | New Registered Prop Table
// -1 | Model String
lua_pushstring(L, PropIdentifier.c_str());
// -2 | Entity.Prop.__Registered.FolderName
// -1 | New Registered Prop Table
lua_setfield(L, -2, "Model");
// -3 | Entity.Prop.__Registered.FolderName
// -2 | New Registered Prop Table
// -1 | Icon String
lua_pushstring(L, Infinity::Graphics::GetSpawnIcon(PropIdentifier));
// -2 | Entity.Prop.__Registered.FolderName
// -1 | New Registered Prop Table
lua_setfield(L, -2, "SpawnIcon");
// -1 | Entity.Prop.__Registered.FolderName
lua_setfield(L, -2, PropIdentifier.c_str());
// -1 | STACK EMPTY
lua_remove(L, -1);
}
else {
// -1 | STACK EMPTY
lua_remove(L, -2);
lua_remove(L, -1);
}
}
}
Files->drop();
}
// EMPTY STACK
lua_settop(L, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment