Skip to content

Commit

Permalink
v2.5.4-luajit: Merge origin/master into luajit
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Mar 27, 2021
2 parents 4d6584d + 23d8984 commit b530b5e
Show file tree
Hide file tree
Showing 34 changed files with 609 additions and 480 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
- name: Download ROM & CC:T
run: |
sudo git clone https://github.com/MCJack123/craftos2-rom /usr/local/share/craftos
git clone --branch v1.15.2-1.95.2 https://github.com/SquidDev-CC/CC-Tweaked ../CC-Tweaked
git clone --branch v1.15.2-1.95.3 https://github.com/SquidDev-CC/CC-Tweaked ../CC-Tweaked
patch -p1 -d ../CC-Tweaked < resources/CCT-Tests.patch
- name: Install dependencies
run: |
Expand Down
3 changes: 3 additions & 0 deletions CraftOS-PC 2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@
<ClInclude Include="api\configuration.hpp" />
<ClInclude Include="api\CraftOS-PC.hpp" />
<ClInclude Include="api\FileEntry.hpp" />
<ClInclude Include="api\generic_peripheral\energy_storage.hpp" />
<ClInclude Include="api\generic_peripheral\fluid_storage.hpp" />
<ClInclude Include="api\generic_peripheral\inventory.hpp" />
<ClInclude Include="api\lib.hpp" />
<ClInclude Include="api\peripheral.hpp" />
<ClInclude Include="api\Terminal.hpp" />
Expand Down
12 changes: 12 additions & 0 deletions CraftOS-PC 2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<Filter Include="Header Files\api">
<UniqueIdentifier>{21592f07-c190-4e8a-8564-4f15f122a951}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\api\generic_peripheral">
<UniqueIdentifier>{7baa47e9-4c4b-4746-adb0-d1f4dda211d4}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down Expand Up @@ -197,6 +200,15 @@
<ClInclude Include="api\FileEntry.hpp">
<Filter>Header Files\api</Filter>
</ClInclude>
<ClInclude Include="api\generic_peripheral\inventory.hpp">
<Filter>Header Files\api\generic_peripheral</Filter>
</ClInclude>
<ClInclude Include="api\generic_peripheral\energy_storage.hpp">
<Filter>Header Files\api\generic_peripheral</Filter>
</ClInclude>
<ClInclude Include="api\generic_peripheral\fluid_storage.hpp">
<Filter>Header Files\api\generic_peripheral</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\platform.cpp">
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Visit the website at https://www.craftos-pc.cc/ for more information, including
#### Homebrew Cask
```bash
$ brew tap MCJack123/CraftOSPC
$ brew cask install craftos-pc-accelerated
$ brew install --cask craftos-pc-accelerated
$ open /Applications/CraftOS-PC.app
```
#### Manual
Expand Down
29 changes: 28 additions & 1 deletion api/CraftOS-PC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,46 @@ struct PluginFunctions {
*/
void (*setConfigSettingBool)(const std::string& name, bool value);

// The following fields are available in API version 10.2.
// The following fields are available in API version 10.2 and later.

/**
* Registers a custom config setting so it can be accessed with the config
* API, with an optional callback. Pass nullptr to callback to ignore.
* @param name The name of the setting
* @param type The type of the setting: 0 for boolean, 1 for integer, 2 for string
* @param callback A callback to call when the setting is changed. This
* takes the name and userdata, and returns 0 for immediate use, 1 to
* reboot the computer, and 2 to restart CraftOS-PC before taking effect.
* Set this to nullptr to not call a function.
* @param userdata An optional opaque pointer to pass to the function.
*/
void (*registerConfigSetting)(const std::string& name, int type, const std::function<int(const std::string&, void*)>& callback, void* userdata);

// The following fields are available in API version 10.3 and later.

/**
* Attaches a peripheral of the specified type to a side, with optional
* extended arguments.
* @param computer The computer to attach to
* @param side The side to attach the peripheral on
* @param type The type of peripheral to attach
* @param errorReturn A pointer to a string to hold an error message (NULL to ignore)
* @param format A format string specifying the arguments passed - 1 character per argument; set to "L" to pass a Lua state instead
* 'i' = lua_Integer, 'n' = lua_Number, 's' = const char *, 'b' = bool, 'N' = nil/NULL (pass NULL in the arg list)
* @param ... Any arguments to pass to the constructor
* @return The new peripheral object, or NULL on error
* @throws std::invalid_argument If the format string is invalid
* @throws std::exception If the peripheral constructor throws an exception
*/
peripheral* (*attachPeripheral)(Computer * computer, const std::string& side, const std::string& type, std::string * errorReturn, const char * format, ...);

/**
* Detaches a peripheral from a side.
* @param computer The computer to detach from
* @param side The side to detach
* @return Whether the operation succeeded
*/
bool (*detachPeripheral)(Computer * computer, const std::string& side);
};

/**
Expand Down
7 changes: 7 additions & 0 deletions api/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ struct configuration {

// The following fields are available in API version 10.2 and later.
bool snooperEnabled;

// The following fields are available in API version 10.3 and later.
bool keepOpenOnShutdown;
};

// A smaller structure that holds the configuration for a single computer.
Expand All @@ -91,6 +94,10 @@ struct computer_configuration {
bool isColor;
bool loadFailure;
bool startFullscreen;

// The following fields are available in API version 10.3 and later.
int computerWidth;
int computerHeight;
};

#endif
39 changes: 39 additions & 0 deletions api/generic_peripheral/energy_storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* generic_peripheral/energy_storage.hpp
* CraftOS-PC 2
*
* This file defines a generic peripheral class for energy-storing peripherals
* to inherit from.
*
* This code is licensed under the MIT license.
* Copyright (c) 2019-2021 JackMacWindows.
*/

#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_ENERGY_STORAGE_HPP
#define CRAFTOS_PC_GENERIC_PERIPHERAL_ENERGY_STORAGE_HPP
#include "../peripheral.hpp"

class energy_storage : public peripheral {
virtual int getEnergy() = 0; // Returns the current energy of the peripheral.
virtual int getEnergyCapacity() = 0; // Returns the total energy capacity of the peripheral.
public:
int call(lua_State *L, const char * method) override {
const std::string m(method);
if (m == "getEnergy") lua_pushinteger(L, getEnergy());
else if (m == "getEnergyCapacity") lua_pushinteger(L, getEnergyCapacity());
else return 0;
return 1;
}
void update() override {}
library_t getMethods() const override {
static luaL_Reg reg[] = {
{"getEnergy", NULL},
{"getEnergyCapacity", NULL},
{NULL, NULL}
};
static library_t methods = {"energy_storage", reg, nullptr, nullptr};
return methods;
}
};

#endif
70 changes: 70 additions & 0 deletions api/generic_peripheral/fluid_storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* generic_peripheral/fluid_storage.hpp
* CraftOS-PC 2
*
* This file defines a generic peripheral class for tank-like peripherals
* to inherit from.
*
* This code is licensed under the MIT license.
* Copyright (c) 2019-2021 JackMacWindows.
*/

#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_FLUID_STORAGE_HPP
#define CRAFTOS_PC_GENERIC_PERIPHERAL_FLUID_STORAGE_HPP
#include "../Computer.hpp"
#include "../peripheral.hpp"

class fluid_storage : public peripheral {
virtual int tanks(lua_State *L) = 0; // Implements tanks() as a Lua function: https://tweaked.cc/generic_peripheral/fluid_storage.html#v:tanks
virtual int addFluid(const std::string& fluid, int amount) = 0; // Adds the specified amount of fluid to a tank. Returns the amount actually added.
virtual std::list<std::pair<std::string, int>> removeFluid(const std::string& fluid, int amount) = 0; // Removes the specified amount of fluid from a tank. If fluid is empty, remove any type of fluid; otherwise only remove that type. Returns a list of each fluid type & amount removed. (If no fluid is removed, return an empty list.)
public:
int call(lua_State *L, const char * method) override {
const std::string m(method);
if (m == "tanks") return tanks(L);
else if (m == "pushFluid" || m == "pullFluid") {
Computer * comp = get_comp(L);
const char * side = luaL_checkstring(L, 1);
const int limit = luaL_optinteger(L, 2, INT_MAX);
const std::string name = luaL_optstring(L, 3, "");

if (comp->peripherals.find(side) == comp->peripherals.end()) return luaL_error(L, "Target '%s' does not exist", side);
fluid_storage * p = dynamic_cast<fluid_storage*>(comp->peripherals[side]);
if (p == NULL) return luaL_error(L, "Target '%s' is not an tank", side); // grammar mistake intentional
fluid_storage *src, *dest;
if (m == "pushFluid") src = this, dest = p;
else src = p, dest = this;
if (limit <= 0) return luaL_error(L, "Limit must be > 0");

const auto removed = src->removeFluid(name, limit);
if (removed.empty()) {
lua_pushinteger(L, 0);
return 1;
}
int added = 0;
std::list<std::pair<std::string, int>> returnedFluid;
for (const std::pair<std::string, int>& type : removed) {
const int add = dest->addFluid(type.first, type.second);
added += add;
if (add < type.second) returnedFluid.push_back(std::make_pair(type.first, type.second - add));
}
for (const std::pair<std::string, int>& type : returnedFluid) src->addFluid(type.first, type.second);

lua_pushinteger(L, added);
return 1;
} else return 0;
}
void update() override {}
library_t getMethods() const override {
static luaL_Reg reg[] = {
{"tanks", NULL},
{"pushFluid", NULL},
{"pullFluid", NULL},
{NULL, NULL}
};
static library_t methods = {"fluid_storage", reg, nullptr, nullptr};
return methods;
}
};

#endif
88 changes: 88 additions & 0 deletions api/generic_peripheral/inventory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* generic_peripheral/inventory.hpp
* CraftOS-PC 2
*
* This file defines a generic peripheral class for inventory-type peripherals
* to inherit from.
*
* This code is licensed under the MIT license.
* Copyright (c) 2019-2021 JackMacWindows.
*/

#ifndef CRAFTOS_PC_GENERIC_PERIPHERAL_INVENTORY_HPP
#define CRAFTOS_PC_GENERIC_PERIPHERAL_INVENTORY_HPP
#include "../Computer.hpp"
#include "../peripheral.hpp"

class inventory : public peripheral {
virtual int size() = 0; // Returns the number of slots available in the inventory.
virtual void getItemDetail(lua_State *L, int slot) = 0; // Pushes a single Lua value to the stack with details about an item in a slot. If the slot is empty or invalid, pushes nil. (Slots are in the range 1-size().)
virtual int addItems(lua_State *L, int slot, int count) = 0; // Adds the item described at the top of the Lua stack to the slot selected. Only adds up to count items. If slot is 0, determine the best slot available. Returns the number of items added.
virtual int removeItems(int slot, int count) = 0; // Removes up to a number of items from the selected slot. Returns the number of items removed.
public:
int call(lua_State *L, const char * method) override {
const std::string m(method);
if (m == "size") {
lua_pushinteger(L, size());
return 1;
} else if (m == "list") {
lua_createtable(L, size(), 0);
for (int i = 1; i <= size(); i++) {
lua_pushinteger(L, i);
getItemDetail(L, i);
lua_settable(L, -3);
}
return 1;
} else if (m == "getItemDetail") {
getItemDetail(L, luaL_checkinteger(L, 1));
return 1;
} else if (m == "pushItems" || m == "pullItems") {
Computer * comp = get_comp(L);
const char * side = luaL_checkstring(L, 1);
const int fromSlot = luaL_checkinteger(L, 2);
const int limit = luaL_optinteger(L, 3, INT_MAX);
const int toSlot = luaL_optinteger(L, 4, 0);

if (comp->peripherals.find(side) == comp->peripherals.end()) return luaL_error(L, "Target '%s' does not exist", side);
inventory * p = dynamic_cast<inventory*>(comp->peripherals[side]);
if (p == NULL) return luaL_error(L, "Target '%s' is not an inventory", side);
inventory *src, *dest;
if (m == "pushItems") src = this, dest = p;
else src = p, dest = this;

if (fromSlot < 1 || fromSlot > src->size()) return luaL_error(L, "From slot out of range (between 1 and %d)", src->size());
if (!lua_isnil(L, 4) && (toSlot < 1 || toSlot > dest->size())) return luaL_error(L, "To slot out of range (between 1 and %d)", dest->size());
if (limit <= 0) {
lua_pushinteger(L, 0);
return 1;
}

src->getItemDetail(L, fromSlot);
const int removed = src->removeItems(fromSlot, limit);
if (removed == 0) {
lua_pushinteger(L, 0);
return 1;
}
const int added = dest->addItems(L, toSlot, removed);
if (added < removed) src->addItems(L, fromSlot, removed - added); // hopefully this will still be OK

lua_pushinteger(L, added);
return 1;
} else return 0;
}
void update() override {}
library_t getMethods() const override {
static luaL_Reg reg[] = {
{"size", NULL},
{"list", NULL},
{"getItemDetail", NULL},
{"pushItems", NULL},
{"pullItems", NULL},
{NULL, NULL}
};
static library_t methods = {"inventory", reg, nullptr, nullptr};
return methods;
}
};

#endif
Loading

0 comments on commit b530b5e

Please sign in to comment.