Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vox version 200 support (maybe?) #10

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/app_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ It works like this:
* The client then communicates with the worldgen system through the `stdin` and `stdout` pipes of the worldgen process using line-based messages. Errors and messages are printed to `stderr`.
* The communication is very straighforward:
* The client sends a message asking for data for a certain area (usually generated block IDs for a given chunk, but you can actually request **any** variable marked as `export` is the source code).
* The worldgen system then **asynchronously** returns the data requested.
* The worldgen system then **asynchronously** returns the data requested. The order of the result does not have to be the same as the order the requests came in.

That is all. The communication runs **asynchronously** and the worldgen engine can run on **multiple threads**, so it is recommended to keep the worldgen busy and always a number of data requests pending. The worldgen always works with 16×16×16 voxel chunks.

**You don't want to have the worldgen process running in multiple instances. The worldgen uses a huge cache so you might run out of memory. The application itself generates on multiple threads and the communication can be done asynchronously.**

## Usage example
A simple example how the worldgen application is to be used. First, we start the application, passing the source files and block UID mapping:
```
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ target_link_libraries(${target} PRIVATE "${tracy_lib}")
# FastNoise2
# ===========================================
target_include_directories(${target} SYSTEM PRIVATE "${CMAKE_BASE_DIR}/install/fastNoise2/include")
find_library(fastNoise2_lib NAMES FastNoise PATHS "${CMAKE_BASE_DIR}/install/fastNoise2/lib" REQUIRED)
find_library(fastNoise2_lib NAMES FastNoise FastNoiseD PATHS "${CMAKE_BASE_DIR}/install/fastNoise2/lib" REQUIRED)
target_link_libraries(${target} PRIVATE "${fastNoise2_lib}")

# ===========================================
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "worldgen/cpu/worldgenapi_cpu.h"
#include "worldgen/cpu/supp/wga_valuewrapper_cpu.h"
#include "woglac/wglcompiler.h"
#include "woglac/wglfile.h"
#include "woglac/source/wglsourcefile.h"

std::mutex stdoutMutex;

Expand Down Expand Up @@ -141,7 +141,7 @@ int main(int argc, char *argv[]) {
wgc.setLookupDirectories(lookupDirs);

for(const std::string &filename: files)
wgc.addFile(std::make_shared<WGLFile>(filename));
wgc.addSource(std::make_shared<WGLSourceFile>(filename));

wgc.compile();
exports = wgc.construct(wgapi);
Expand Down
1 change: 1 addition & 0 deletions src/woglac/source/wglsource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "wglsource.h"
19 changes: 19 additions & 0 deletions src/woglac/source/wglsource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <memory>
#include <iostream>

class WGLSource {

public:
virtual ~WGLSource() {}

public:
/// Returns an open stream for the source
virtual std::unique_ptr<std::istream> openStream() const = 0;

virtual std::string sourceName() const = 0;

};

using WGLSourcePtr = std::shared_ptr<WGLSource>;
15 changes: 15 additions & 0 deletions src/woglac/source/wglsourcebuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "wglsourcebuffer.h"

#include <sstream>

WGLSourceBuffer::WGLSourceBuffer(const std::string &data) : data_(data) {

}

std::unique_ptr<std::istream> WGLSourceBuffer::openStream() const {
return std::make_unique<std::istringstream>(data_);
}

std::string WGLSourceBuffer::sourceName() const {
return std::format("(buffer {})", reinterpret_cast<intptr_t>(data_.data()));
}
18 changes: 18 additions & 0 deletions src/woglac/source/wglsourcebuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "wglsource.h"

class WGLSourceBuffer : public WGLSource {

public:
WGLSourceBuffer(const std::string &data);

public:
virtual std::unique_ptr<std::istream> openStream() const override;
virtual std::string sourceName() const override;

private:
const std::string data_;

};

34 changes: 34 additions & 0 deletions src/woglac/source/wglsourcefile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "wglsourcefile.h"

#include <fstream>
#include <format>

#include "woglac/supp/wglerror.h"

WGLSourceFile::WGLSourceFile() {

}

WGLSourceFile::WGLSourceFile(const std::string &fileName) {
setFileName(fileName);
}

void WGLSourceFile::setFileName(const std::string &fileName) {
if(fileName_ == fileName)
return;

fileName_ = fileName;
}

std::unique_ptr<std::istream> WGLSourceFile::openStream() const {
auto r = std::make_unique<std::ifstream>();
r->open(fileName_, std::ifstream::in);
if(!r->is_open())
throw WGLError(std::format("Error opening file '{}'", fileName_), nullptr);

return r;
}

std::string WGLSourceFile::sourceName() const {
return fileName_;
}
28 changes: 28 additions & 0 deletions src/woglac/source/wglsourcefile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <memory>
#include <string>

#include "wglsource.h"

class WGLSourceFile : public WGLSource {

public:
WGLSourceFile();
WGLSourceFile(const std::string &fileName);

public:
inline const std::string &fileName() const {
return fileName_;
}

void setFileName(const std::string &fileName);

public:
virtual std::unique_ptr<std::istream> openStream() const override;
virtual std::string sourceName() const override;

private:
std::string fileName_;

};
2 changes: 1 addition & 1 deletion src/woglac/supp/wglmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class WGLModule {

public:
std::unique_ptr<std::ifstream> stream;
std::unique_ptr<std::istream> stream;
std::unique_ptr<antlr4::ANTLRInputStream> input;
std::unique_ptr<WoglacLexer> lexer;
std::unique_ptr<antlr4::CommonTokenStream> tokens;
Expand Down
14 changes: 5 additions & 9 deletions src/woglac/wglcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void WGLCompiler::clear() {
context_->clear();
}

void WGLCompiler::addFile(const WGLFilePtr &file) {
files_.push_back(file);
void WGLCompiler::addSource(const WGLSourcePtr &file) {
sources_.push_back(file);
}

std::string WGLCompiler::lookupFile(const std::string &filename, antlr4::ParserRuleContext *ctx) {
Expand All @@ -51,14 +51,10 @@ void WGLCompiler::compile() {
try {

// Parse files
for(const WGLFilePtr &f: files_) {
for(const WGLSourcePtr &s: sources_) {
try {
auto m = std::make_shared<WGLModule>();

m->stream.reset(new std::ifstream());
m->stream->open(f->fileName(), std::ifstream::in);
if(!m->stream->is_open())
throw WGLError(std::format("Error opening file '{}'", f->fileName()), nullptr);
m->stream = s->openStream();

m->input.reset(new antlr4::ANTLRInputStream(*m->stream));
m->lexer.reset(new WoglacLexer(m->input.get()));
Expand All @@ -76,7 +72,7 @@ void WGLCompiler::compile() {
modules_.push_back(m);
}
catch(const WGLError &e) {
throw std::exception(std::format("Error when compiling WOGLAC file '{}': {}", f->fileName(), e.message()).c_str());
throw std::exception(std::format("Error when compiling WOGLAC source '{}': {}", s->sourceName(), e.message()).c_str());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/woglac/wglcompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "pch.h"

#include "wglfile.h"
#include "woglac/source/wglsourcefile.h"

// Woglac language parser and compiler, outputs a
class WGLCompiler {
Expand All @@ -20,7 +20,7 @@ class WGLCompiler {
}

public:
void addFile(const WGLFilePtr &file);
void addSource(const WGLSourcePtr &file);

// Tries to locate a specified file, throws if failed
std::string lookupFile(const std::string &filename, antlr4::ParserRuleContext *ctx);
Expand All @@ -33,7 +33,7 @@ class WGLCompiler {
std::unordered_map<std::string, WGA_Value*> construct(WorldGenAPI &api);

private:
std::vector<WGLFilePtr> files_;
std::vector<WGLSourcePtr> sources_;
std::shared_ptr<WGLContext> context_;
std::vector<std::string> lookupDirectories_;

Expand Down
16 changes: 0 additions & 16 deletions src/woglac/wglfile.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions src/woglac/wglfile.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/worldgen/util/voxparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void VOXParser::parseData(std::basic_istream<char> &stream) {
throw std::exception("Provided file is not of the VOX file format");

const auto fileVersion = readPrimitive<uint32_t>(stream);
if(fileVersion != 150)
if(fileVersion != 150 && fileVersion != 200)
throw std::exception(std::format("Unsupported vox file format version ({})", fileVersion).c_str());

// Process main chunk
Expand Down