Skip to content

Commit

Permalink
GenericDocument: added support for INI config format
Browse files Browse the repository at this point in the history
New script API:
    * GENERIC_DOCUMENT_OPTION_ALLOW_BRACED_KEYWORDS, //!< Allow INI-like '[keyword]' tokens.
    * GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_EQUALS, //!< Allow '=' as separator between tokens.
    * GENERIC_DOCUMENT_OPTION_ALLOW_HASH_COMMENTS //!< Allow comments starting with `#`.     
    * string Terran::getTerrainFileName()
    * string Terrain::getTerrainFileResourceGroup()

'demo_script.as' extended to allow viewing TERRN2 and TOBJ files.
  • Loading branch information
ohlidalp authored and Petr Ohlídal committed Jan 5, 2023
1 parent 6970306 commit e866683
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 23 deletions.
5 changes: 4 additions & 1 deletion doc/angelscript/Script2Game/GenericDocumentClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ enum GenericDocumentOptions
GENERIC_DOCUMENT_OPTION_ALLOW_SLASH_COMMENTS, //!< Allow comments starting with `//`.
GENERIC_DOCUMENT_OPTION_FIRST_LINE_IS_TITLE, //!< First non-empty & non-comment line is a naked string with spaces.
GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_COLON, //!< Allow ':' as separator between tokens.
GENERIC_DOCUMENT_OPTION_PARENTHESES_CAPTURE_SPACES //!< If non-empty NAKED string encounters '(', following spaces will be captured until matching ')' is found.
GENERIC_DOCUMENT_OPTION_PARENTHESES_CAPTURE_SPACES, //!< If non-empty NAKED string encounters '(', following spaces will be captured until matching ')' is found.
GENERIC_DOCUMENT_OPTION_ALLOW_BRACED_KEYWORDS, //!< Allow INI-like '[keyword]' tokens.
GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_EQUALS, //!< Allow '=' as separator between tokens.
GENERIC_DOCUMENT_OPTION_ALLOW_HASH_COMMENTS //!< Allow comments starting with `#`.
};

/**
Expand Down
10 changes: 10 additions & 0 deletions doc/angelscript/Script2Game/TerrainClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class TerrainClass
*/
string getTerrainName();

/**
* @return File name of the terrain definition (TERRN2 format).
*/
string getTerrainFileName();

/**
* @return OGRE resource group of the terrain bundle (ZIP/directory under 'mods/') where definition files live.
*/
string getTerrainFileResourceGroup();

/**
* @return GUID (global unique ID) of the terrain, or empty string if not specified.
*/
Expand Down
112 changes: 111 additions & 1 deletion resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ CVarClass@ g_sim_state = console.cVarFind("sim_state"); // 0=off, 1=running, 2=
CVarClass@ g_mp_state = console.cVarFind("mp_state"); // 0=disabled, 1=connecting, 2=connected, see MpState in Application.h
CVarClass@ g_io_arcade_controls = console.cVarFind("io_arcade_controls"); // bool
GenericDocumentClass@ g_displayed_document = null;
string g_displayed_doc_filename;
array<string> g_terrain_tobj_files;

/*
---------------------------------------------------------------------------
Expand Down Expand Up @@ -72,6 +74,11 @@ void frameStep(float dt)
ImGui::Text("Pro tip: Press '"
+ inputs.getEventCommandTrimmed(EV_COMMON_CONSOLE_TOGGLE)
+ "' to open console anytime.");

// Reset simulation data
@g_displayed_document = null;
g_displayed_doc_filename = "";
g_terrain_tobj_files.removeRange(0, g_terrain_tobj_files.length());
}
else if (g_app_state.getInt() == 2) // simulation
{
Expand All @@ -95,6 +102,8 @@ void frameStep(float dt)
ImGui::Text("(terrain edit)");
}

drawTerrainButtons();

ImGui::TextDisabled("Camera controls:");
ImGui::Text("Change camera: " + inputs.getEventCommandTrimmed(EV_CAMERA_CHANGE));
ImGui::Text("Toggle free camera: " + inputs.getEventCommandTrimmed(EV_CAMERA_FREE_MODE));
Expand All @@ -103,6 +112,8 @@ void frameStep(float dt)
BeamClass@ actor = game.getCurrentTruck();
if (@actor != null)
{
// Actor name and "View document" button
ImGui::PushID("actor");
ImGui::AlignTextToFramePadding();
ImGui::Text("You are driving " + actor.getTruckName());
ImGui::SameLine();
Expand All @@ -119,6 +130,7 @@ void frameStep(float dt)
if (doc.LoadFromResource(actor.getTruckFileName(), actor.getTruckFileResourceGroup(), flags))
{
@g_displayed_document = @doc;
g_displayed_doc_filename = actor.getTruckFileName();
}
}
}
Expand All @@ -127,8 +139,10 @@ void frameStep(float dt)
if (ImGui::Button("Close document"))
{
@g_displayed_document = null;
g_displayed_doc_filename = "";
}
}
ImGui::PopID(); //"actor"

ImGui::TextDisabled("Vehicle controls:");

Expand Down Expand Up @@ -192,10 +206,106 @@ void frameStep(float dt)
}
}

void drawTerrainButtons()
{
// Terrain name (with "view document" button)
ImGui::PushID("terrn");
TerrainClass@ terrain = game.getTerrain();
ImGui::AlignTextToFramePadding();
ImGui::Text("Terrain: " + terrain.getTerrainName());
ImGui::SameLine();
if (@g_displayed_document == null)
{
if (ImGui::Button("View document"))
{
GenericDocumentClass@ doc = GenericDocumentClass();
int flags = GENERIC_DOCUMENT_OPTION_ALLOW_NAKED_STRINGS
| GENERIC_DOCUMENT_OPTION_ALLOW_SLASH_COMMENTS
| GENERIC_DOCUMENT_OPTION_ALLOW_HASH_COMMENTS
| GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_EQUALS
| GENERIC_DOCUMENT_OPTION_ALLOW_BRACED_KEYWORDS;
if (doc.LoadFromResource(terrain.getTerrainFileName(), terrain.getTerrainFileResourceGroup(), flags))
{
@g_displayed_document = @doc;
g_displayed_doc_filename = terrain.getTerrainFileName();

// Fetch TOBJ filenames
if (g_terrain_tobj_files.length() == 0)
{
GenericDocReaderClass@ reader = GenericDocReaderClass(doc);
bool in_section_objects = false;
while (!reader.EndOfFile())
{
if (reader.GetTokType() == TOKEN_TYPE_KEYWORD && reader.GetTokKeyword().substr(0, 1) == "[")
{
in_section_objects = (reader.GetTokKeyword() == '[Objects]');
}
else if (reader.GetTokType() == TOKEN_TYPE_STRING && in_section_objects)
{
// Note: in GenericDocument, a text on line start is always a KEYWORD token,
// but KEYWORDs must not contain special characters,
// so file names always decay to strings because of '.'.
g_terrain_tobj_files.insertLast(reader.GetTokString());
}
reader.MoveNext();
}
}
}
}
}
else
{
if (ImGui::Button("Close document"))
{
@g_displayed_document = null;
g_displayed_doc_filename = "";
}
}

// TOBJ files
ImGui::PushID("tobj");
for (uint i = 0; i < g_terrain_tobj_files.length(); i++)
{
ImGui::PushID(i);
ImGui::AlignTextToFramePadding();
ImGui::Bullet();
ImGui::SameLine();
ImGui::Text(g_terrain_tobj_files[i]);
ImGui::SameLine();
if (@g_displayed_document == null)
{
if (ImGui::Button("View document"))
{
GenericDocumentClass@ doc = GenericDocumentClass();
int flags = GENERIC_DOCUMENT_OPTION_ALLOW_NAKED_STRINGS
| GENERIC_DOCUMENT_OPTION_ALLOW_SLASH_COMMENTS;
if (doc.LoadFromResource(g_terrain_tobj_files[i], terrain.getTerrainFileResourceGroup(), flags))
{
@g_displayed_document = @doc;
g_displayed_doc_filename = g_terrain_tobj_files[i];
}
}
}
else
{
if (ImGui::Button("Close document"))
{
@g_displayed_document = null;
g_displayed_doc_filename = "";
}
}
ImGui::PopID(); // i
}
ImGui::PopID(); //"tobj"

ImGui::PopID(); //"terrn"
}

void drawDocumentWindow()
{
ImGui::PushID("document view");
ImGui::Begin("Document view", /*open:*/true, /*flags:*/0);
string caption = "Document view (" + g_displayed_doc_filename + ")";
ImGui::Begin(caption, /*open:*/true, /*flags:*/0);

GenericDocReaderClass reader(g_displayed_document);
while (!reader.EndOfFile())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void RoR::RegisterGenericFileFormat(asIScriptEngine* engine)
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_FIRST_LINE_IS_TITLE", GenericDocument::OPTION_FIRST_LINE_IS_TITLE);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_COLON", GenericDocument::OPTION_ALLOW_SEPARATOR_COLON);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_PARENTHESES_CAPTURE_SPACES", GenericDocument::OPTION_PARENTHESES_CAPTURE_SPACES);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_BRACED_KEYWORDS", GenericDocument::OPTION_ALLOW_BRACED_KEYWORDS);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_EQUALS", GenericDocument::OPTION_ALLOW_SEPARATOR_EQUALS);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_HASH_COMMENTS", GenericDocument::OPTION_ALLOW_HASH_COMMENTS);


// class GenericDocument
Expand Down
2 changes: 2 additions & 0 deletions source/main/scripting/bindings/TerrainAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void RoR::RegisterTerrain(asIScriptEngine* engine)

int result = 0;
result = engine->RegisterObjectMethod("TerrainClass", "string getTerrainName()", asMETHOD(RoR::Terrain,getTerrainName), asCALL_THISCALL); ROR_ASSERT(result>=0);
result = engine->RegisterObjectMethod("TerrainClass", "string getTerrainFileName()", asMETHOD(RoR::Terrain, getTerrainFileName), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("TerrainClass", "string getTerrainFileResourceGroup()", asMETHOD(RoR::Terrain, getTerrainFileResourceGroup), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("TerrainClass", "string getGUID()", asMETHOD(RoR::Terrain,getGUID), asCALL_THISCALL); ROR_ASSERT(result>=0);
result = engine->RegisterObjectMethod("TerrainClass", "int getVersion()", asMETHOD(RoR::Terrain,getVersion), asCALL_THISCALL); ROR_ASSERT(result>=0);
result = engine->RegisterObjectMethod("TerrainClass", "bool isFlat()", asMETHOD(RoR::Terrain,isFlat), asCALL_THISCALL); ROR_ASSERT(result>=0);
Expand Down
9 changes: 9 additions & 0 deletions source/main/terrain/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,12 @@ ProceduralManagerPtr RoR::Terrain::getProceduralManager()
return m_object_manager->getProceduralManager();
}

std::string RoR::Terrain::getTerrainFileName()
{
return m_cache_entry->fname;
}

std::string RoR::Terrain::getTerrainFileResourceGroup()
{
return m_cache_entry->resource_group;
}
2 changes: 2 additions & 0 deletions source/main/terrain/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Terrain : public ZeroedMemoryAllocator, public RefCountingObject<Terrain>
/// @name Terrain info
/// @{
std::string getTerrainName() const { return m_def.name; }
std::string getTerrainFileName();
std::string getTerrainFileResourceGroup();
std::string getGUID() const { return m_def.guid; }
int getCategoryID() const { return m_def.category_id; }
int getVersion() const { return m_def.version; }
Expand Down
Loading

0 comments on commit e866683

Please sign in to comment.