Skip to content

Commit

Permalink
EGM events fixes (#2098)
Browse files Browse the repository at this point in the history
All readers now take in event_data so the ide can handle loading events.ey. Fixes gmk DND to use object_names instead of ids
  • Loading branch information
fundies committed Aug 5, 2020
1 parent e528563 commit 378d447
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 101 deletions.
13 changes: 7 additions & 6 deletions CommandLine/emake/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,37 +150,38 @@ int main(int argc, char* argv[])
return OPTIONS_ERROR;
}

EventData event_data(ParseEventFile((fs::path(options.EnigmaRoot())/"events.ey").u8string()));

// FIXME: Remove all occurrences of wrap_unique in the following code.
// There is no reason for these projects to be bare pointers.
std::string ext;
size_t dot = input_file.find_last_of('.');
std::unique_ptr<buffers::Project> project;
if (dot != std::string::npos) ext = tolower(input_file.substr(dot + 1));
if (ext == "sog") {
if (!ReadSOG(input_file, &game)) return 1;
if (!ReadSOG(input_file, &game, &event_data)) return 1;
return plugin.BuildGame(game.ConstructGame(), mode, output_file.c_str());
#ifdef CLI_ENABLE_EGM
} else if (ext == "gm81" || ext == "gmk" || ext == "gm6" || ext == "gmd") {
if (!(project = gmk::LoadGMK(input_file))) return 1;
if (!(project = gmk::LoadGMK(input_file, &event_data))) return 1;
return plugin.BuildGame(project->game(), mode, output_file.c_str());
} else if (ext == "gmx") {
fs::path p = input_file;
if (fs::is_directory(p)) {
input_file += "/" + p.filename().stem().string() + ".project.gmx";
}

if (!(project = gmx::LoadGMX(input_file))) return 1;
if (!(project = gmx::LoadGMX(input_file, &event_data))) return 1;
return plugin.BuildGame(project->game(), mode, output_file.c_str());
} else if (ext == "yyp") {
if (!(project = yyp::LoadYYP(input_file))) return 1;
if (!(project = yyp::LoadYYP(input_file, &event_data))) return 1;
return plugin.BuildGame(project->game(), mode, output_file.c_str());
} else if (ext == "egm") {
fs::path p = input_file;
if (fs::is_directory(p)) {
input_file += "/" + p.filename().stem().string() + ".egm";
}
EventData event_data(ParseEventFile("events.ey"));
egm::EGM egm(event_data);
egm::EGM egm(&event_data);
if (!(project = egm.LoadEGM(input_file))) return 1;
return plugin.BuildGame(project->game(), mode, output_file.c_str());
} else if (ext.empty()) {
Expand Down
1 change: 1 addition & 0 deletions CommandLine/emake/OptionsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ std::string OptionsParser::APIyaml(const buffers::resources::Settings* currentCo
yaml += "target-networking: " + network + "\n";
yaml += "extensions: " + _extensions + "\n";
yaml += std::string("codegen-only: ") + (_rawArgs["codegen-only"].as<bool>() ? "true" : "false") + "\n";
yaml += "enigma-root: " + _enigmaRoot + "\n";

return yaml;
}
Expand Down
10 changes: 4 additions & 6 deletions CommandLine/emake/SOG.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "SOG.hpp"
#include "Main.hpp"
#include "event_reader/event_parser.h"

#include <filesystem>
namespace fs = std::filesystem;
Expand All @@ -12,8 +11,7 @@ using std::map;
using std::string;
using evpair = std::pair<int, int>;

bool ReadSOG(const std::string &input_file, Game *game) {
EventData event_data(ParseEventFile("events.ey"));
bool ReadSOG(const std::string &input_file, Game *game, const EventData* event_data) {
map<evpair, string> events;
fs::path targetDir(input_file);
fs::recursive_directory_iterator iter(targetDir);
Expand All @@ -23,10 +21,10 @@ bool ReadSOG(const std::string &input_file, Game *game) {
std::string evstr = i.filename().string();
if (evstr.length() > 4 && evstr.substr(evstr.length() - 4) == ".edl")
evstr.erase(evstr.length() - 4);
Event ev = event_data.DecodeEventString(evstr);
Event ev = event_data->DecodeEventString(evstr);
if (ev.IsValid()) {
if (std::ifstream f{i.string()}) {
evpair eid = event_data.reverse_get_event(ev);
evpair eid = event_data->reverse_get_event(ev);
std::string code {
std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()
};
Expand All @@ -43,7 +41,7 @@ bool ReadSOG(const std::string &input_file, Game *game) {
} else {
errorStream << "Error: unrecognized event file " << i << '.' << std::endl;
errorStream << "Supported events:" << std::endl;
for (auto st : event_data.events()) {
for (auto st : event_data->events()) {
errorStream << " - " << st.ExampleIDStrings();
errorStream << std::endl;
}
Expand Down
4 changes: 3 additions & 1 deletion CommandLine/emake/SOG.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef EMAKE_3FG_HPP
#define EMAKE_3FG_HPP
#include "Game.hpp"
#include "event_reader/event_parser.h"

#include <string>

bool ReadSOG(const std::string &input_file, Game *game);
bool ReadSOG(const std::string &input_file, Game *game, const EventData* event_data);

#endif
10 changes: 5 additions & 5 deletions CommandLine/gm2egm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ int main(int argc, char *argv[])
if (dot != std::string::npos) ext = ToLower(input_file.substr(dot + 1));

std::unique_ptr<buffers::Project> project;
EventData event_data(ParseEventFile("events.ey"));
egm::EGM egm(&event_data);

if (ext == "gm81" || ext == "gmk" || ext == "gm6" || ext == "gmd") {
project = gmk::LoadGMK(input_file);
project = gmk::LoadGMK(input_file, &event_data);
} else if (ext == "gmx") {
project = gmx::LoadGMX(input_file);
project = gmx::LoadGMX(input_file, &event_data);
} else if (ext == "yyp") {
project = yyp::LoadYYP(input_file);
project = yyp::LoadYYP(input_file, &event_data);
} else {
std::cerr << "Error: Unkown extenstion \"" << ext << "\"." << std::endl;
return -2;
Expand All @@ -51,8 +53,6 @@ int main(int argc, char *argv[])
return -3;
}

EventData event_data(ParseEventFile("events.ey"));
egm::EGM egm(event_data);
if (!egm.WriteEGM(outDir, project.get())) {
std::cerr << "Error: Failure writting \"" << argv[2] << std::endl;
return -4;
Expand Down
40 changes: 20 additions & 20 deletions CommandLine/libEGM/egm-read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ std::map<Type, int> maxID = {
{ Type::kTimeline, 0 }
};

buffers::resources::Script* LoadScript(const fs::path& fPath) {
buffers::resources::Script* scr = new buffers::resources::Script();
scr->set_code(FileToString(fPath));
buffers::resources::Script LoadScript(const fs::path& fPath) {
buffers::resources::Script scr;
scr.set_code(FileToString(fPath));
return scr;
}

buffers::resources::Shader* LoadShader(const fs::path& fPath) {
buffers::resources::Shader* shdr = new buffers::resources::Shader();
buffers::resources::Shader LoadShader(const fs::path& fPath) {
buffers::resources::Shader shdr;

const std::string p = fPath.parent_path().string() + "/" + fPath.stem().string();
shdr->set_vertex_code(FileToString(p + ".vert"));
shdr->set_fragment_code(p + ".frag");
shdr.set_vertex_code(FileToString(p + ".vert"));
shdr.set_fragment_code(p + ".frag");

return shdr;
}
Expand Down Expand Up @@ -142,8 +142,8 @@ inline void invalidYAMLType(const YAML::Node& yaml, const fs::path& fPath, const
yamlErrorPosition(yaml[field->name()].Mark());
}

buffers::resources::Timeline* LoadTimeLine(const fs::path& fPath) {
buffers::resources::Timeline* tln = new buffers::resources::Timeline();
buffers::resources::Timeline LoadTimeLine(const fs::path& fPath) {
buffers::resources::Timeline tln;

const std::string delim = "step[";
for(auto& f : fs::directory_iterator(fPath)) {
Expand All @@ -168,7 +168,7 @@ buffers::resources::Timeline* LoadTimeLine(const fs::path& fPath) {
}

// If we made it this far we can add event to timeline
buffers::resources::Timeline_Moment* m = tln->add_moments();
buffers::resources::Timeline_Moment* m = tln.add_moments();
m->set_step(step);
m->set_code(FileToString(f.path()));

Expand Down Expand Up @@ -218,7 +218,7 @@ void EGM::LoadObjectEvents(const fs::path& fPath, google::protobuf::Message *m,
for(auto& f : fs::directory_iterator(fPath)) {
if (f.path().extension() == ".edl") {
const std::string eventIdString = f.path().stem().string();
auto event = events_.DecodeEventString(eventIdString);
auto event = events_->DecodeEventString(eventIdString);

buffers::resources::Object::EgmEvent event_proto;
event_proto.set_id(event.bare_id());
Expand Down Expand Up @@ -409,16 +409,16 @@ bool EGM::LoadResource(const fs::path& fPath, google::protobuf::Message *m,

// Scripts and shaders are not folders so we exit here
if (ext == ".edl") {
buffers::resources::Script* scr = LoadScript(fPath);
scr->set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(scr));
buffers::resources::Script scr = LoadScript(fPath);
scr.set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(&scr));
return true;
}

if (ext == ".shdr") {
buffers::resources::Shader* shdr = LoadShader(fPath);
shdr->set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(shdr));
buffers::resources::Shader shdr = LoadShader(fPath);
shdr.set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(&shdr));
return true;
}

Expand All @@ -428,9 +428,9 @@ bool EGM::LoadResource(const fs::path& fPath, google::protobuf::Message *m,

// Timelines are folders but do not have a properties.yaml so we exit here
if (ext == ".tln") {
buffers::resources::Timeline* tln = LoadTimeLine(fPath);
tln->set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(tln));
buffers::resources::Timeline tln = LoadTimeLine(fPath);
tln.set_id(id);
m->CopyFrom(*static_cast<google::protobuf::Message*>(&tln));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion CommandLine/libEGM/egm-write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ bool EGM::WriteObject(const fs::path &egm_root, const fs::path &dir,
return false;

for (auto &e : events) {
auto event = events_.get_event(e.id(), {e.arguments().begin(), e.arguments().end()});
auto event = events_->get_event(e.id(), {e.arguments().begin(), e.arguments().end()});
auto edlFile = dir/(event.IdString() + ".edl");
std::ofstream fout{edlFile};
fout << e.code();
Expand Down
9 changes: 2 additions & 7 deletions CommandLine/libEGM/egm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ class EGM {
bool WriteEGM(std::string fName, buffers::Project* project) const;
std::unique_ptr<buffers::Project> LoadEGM(std::string fName) const;

EGM(const EventData &events): events_(events) {}
EGM():
storage_do_not_use_(
std::make_unique<EventData>(ParseEventFile("events.ey"))),
events_(*storage_do_not_use_) {}
EGM(const EventData* events): events_(events) {}

private:
// Reading ===================================================================
Expand Down Expand Up @@ -67,8 +63,7 @@ class EGM {
// 'Rithmatic ================================================================
std::map<std::string, const buffers::TreeNode*> FlattenTree(
const buffers::TreeNode &tree);
std::unique_ptr<EventData> storage_do_not_use_;
const EventData &events_;
const EventData* events_;
};

} //namespace egm
Loading

0 comments on commit 378d447

Please sign in to comment.