Skip to content

Commit

Permalink
Added initialComputer, peripheral plugin API (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Nov 23, 2019
1 parent d07011c commit f68de27
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/config.cpp
Expand Up @@ -114,7 +114,8 @@ void config_init() {
0,
"",
false,
false
false,
0
};
std::ifstream in(std::string(getBasePath()) + "/config/global.json");
if (!in.is_open()) {return;}
Expand Down Expand Up @@ -142,6 +143,7 @@ void config_init() {
if (root.isMember("skipUpdate")) config.skipUpdate = root["skipUpdate"].asString();
if (root.isMember("configReadOnly")) config.configReadOnly = root["configReadOnly"].asBool();
if (root.isMember("vanilla")) config.vanilla = root["vanilla"].asBool();
if (root.isMember("initialComputer")) config.initialComputer = root["initialComputer"].asInt();
}

void config_save(bool deinit) {
Expand All @@ -167,6 +169,7 @@ void config_save(bool deinit) {
root["skipUpdate"] = config.skipUpdate;
root["configReadOnly"] = config.configReadOnly;
root["vanilla"] = config.vanilla;
root["initialComputer"] = config.initialComputer;
std::ofstream out(std::string(getBasePath()) + "/config/global.json");
out << root;
out.close();
Expand Down Expand Up @@ -218,6 +221,8 @@ int config_get(lua_State *L) {
lua_pushboolean(L, config.configReadOnly);
else if (strcmp(name, "vanilla") == 0)
lua_pushboolean(L, config.vanilla);
else if (strcmp(name, "initialComputer") == 0)
lua_pushinteger(L, config.initialComputer);
else lua_pushnil(L);
return 1;
}
Expand Down Expand Up @@ -277,6 +282,8 @@ int config_set(lua_State *L) {
config.configReadOnly = lua_toboolean(L, 2);
else if (strcmp(name, "vanilla") == 0)
config.vanilla = lua_toboolean(L, 2);
else if (strcmp(name, "initialComputer") == 0)
config.initialComputer = lua_tointeger(L, 2);
config_save(false);
return 0;
}
Expand All @@ -300,6 +307,7 @@ const char * configuration_keys[] = {
"romReadOnly",
"configReadOnly",
"vanilla",
"initialComputer",
NULL,
};

Expand All @@ -326,7 +334,7 @@ int config_getType(lua_State *L) {
lua_pushstring(L, "string");
else if (name == "computerSpaceLimit" || name == "maximumFilesOpen" ||
name == "maxNotesPerTick" || name == "clockSpeed" ||
name == "abortTimeout" || name == "mount_mode")
name == "abortTimeout" || name == "mount_mode" || name == "initialComputer")
lua_pushstring(L, "number");
else lua_pushnil(L);
return 1;
Expand Down
1 change: 1 addition & 0 deletions src/config.hpp
Expand Up @@ -39,6 +39,7 @@ struct configuration {
std::string skipUpdate;
bool configReadOnly;
bool vanilla;
int initialComputer;
};
struct computer_configuration {
std::string label;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -123,7 +123,7 @@ int main(int argc, char*argv[]) {
driveInit();
if (!CRAFTOSPC_INDEV && !headless && !cli && config.checkUpdates && config.skipUpdate != CRAFTOSPC_VERSION)
std::thread(update_thread).detach();
startComputer(0);
startComputer(config.initialComputer);
mainLoop();
for (std::thread *t : computerThreads) { if (t->joinable()) {t->join(); delete t;} }
driveQuit();
Expand Down
16 changes: 10 additions & 6 deletions src/periphemu.cpp
Expand Up @@ -37,6 +37,14 @@ monitor * findMonitorFromWindowID(Computer *comp, unsigned id, std::string& side
return NULL;
}

std::unordered_map<std::string, peripheral_init> initializers = {
{"monitor", &monitor::init},
{"printer", &printer::init},
{"computer", &computer::init},
{"modem", &modem::init},
{"drive", &drive::init},
};

const char * peripheral_attach(lua_State *L, void* arg) {
std::string * side = (std::string*)arg;
lua_pushstring(L, side->c_str());
Expand Down Expand Up @@ -65,12 +73,8 @@ int periphemu_create(lua_State* L) {
}
//lua_pop(L, 2);
try {
if (type == std::string("monitor")) computer->peripherals[side] = new monitor(L, side.c_str());
else if (type == std::string("printer")) computer->peripherals[side] = new printer(L, side.c_str());
else if (type == std::string("computer")) computer->peripherals[side] = new class computer(L, side.c_str());
else if (type == std::string("modem")) computer->peripherals[side] = new modem(L, side.c_str());
else if (type == std::string("drive")) computer->peripherals[side] = new drive(L, side.c_str());
else if (type == std::string("debugger") && computer->debugger == NULL) computer->peripherals[side] = new debugger(L, side.c_str());
if (type == std::string("debugger") && computer->debugger == NULL) computer->peripherals[side] = new debugger(L, side.c_str());
else if (initializers.find(type) != initializers.end()) computer->peripherals[side] = initializers[type](L, side.c_str());
else {
printf("not found: %s\n", type.c_str());
lua_pushboolean(L, false);
Expand Down
1 change: 1 addition & 0 deletions src/periphemu.hpp
Expand Up @@ -11,5 +11,6 @@
#ifndef PERIPHEMU_HPP
#define PERIPHEMU_HPP
#include "lib.hpp"
extern void registerPeripheral(std::string name, peripheral_init initializer);
extern library_t periphemu_lib;
#endif
1 change: 1 addition & 0 deletions src/peripheral/computer.hpp
Expand Up @@ -25,6 +25,7 @@ class computer: public peripheral {
int isOn(lua_State *L);
public:
static library_t methods;
static peripheral * init(lua_State *L, const char * side) {return new computer(L, side);}
library_t getMethods() {return methods;}
computer(lua_State *L, const char * side);
~computer();
Expand Down
1 change: 1 addition & 0 deletions src/peripheral/drive.hpp
Expand Up @@ -48,6 +48,7 @@ class drive: public peripheral {
int insertDisk(lua_State *L, bool init = false);
public:
static library_t methods;
static peripheral * init(lua_State *L, const char * side) {return new drive(L, side);}
library_t getMethods() { return methods; }
drive(lua_State *L, const char * side);
~drive();
Expand Down
1 change: 1 addition & 0 deletions src/peripheral/modem.hpp
Expand Up @@ -27,6 +27,7 @@ class modem: public peripheral {
void receive(uint16_t port, uint16_t replyPort, lua_State *param);
public:
static library_t methods;
static peripheral * init(lua_State *L, const char * side) {return new modem(L, side);}
library_t getMethods() {return methods;}
modem(lua_State *L, const char * side);
~modem();
Expand Down
1 change: 1 addition & 0 deletions src/peripheral/monitor.hpp
Expand Up @@ -44,6 +44,7 @@ class monitor : public peripheral {
public:
TerminalWindow * term;
static library_t methods;
static peripheral * init(lua_State *L, const char * side) {return new monitor(L, side);}
library_t getMethods() {return methods;}
monitor(lua_State *L, const char * side);
~monitor();
Expand Down
1 change: 1 addition & 0 deletions src/peripheral/peripheral.hpp
Expand Up @@ -13,6 +13,7 @@ class peripheral;
#ifndef PERIPHERAL_HPP
#define PERIPHERAL_HPP
#include "../lib.hpp"
typedef peripheral*(*peripheral_init)(lua_State*, const char *);
extern library_t peripheral_lib;
extern void peripheral_update(Computer *comp);

Expand Down
1 change: 1 addition & 0 deletions src/peripheral/printer.hpp
Expand Up @@ -55,6 +55,7 @@ class printer: public peripheral {
int getPaperLevel(lua_State *L);

public:
static peripheral * init(lua_State *L, const char * side) {return new printer(L, side);}
printer(lua_State *L, const char * side);
~printer();
int call(lua_State *L, const char * method);
Expand Down

0 comments on commit f68de27

Please sign in to comment.