From aa2e7243426af54788eab93d2e5c3068d6f3ead8 Mon Sep 17 00:00:00 2001
From: Kindi <2538602-Kuyondo@users.noreply.gitlab.com>
Date: Mon, 6 Jun 2022 16:24:54 +0000
Subject: [PATCH] Lua API for Repair records

---
 apps/openmw/CMakeLists.txt         |  2 +-
 apps/openmw/mwlua/types/repair.cpp | 35 ++++++++++++++++++++++++++++++
 apps/openmw/mwlua/types/types.cpp  |  2 +-
 apps/openmw/mwlua/types/types.hpp  |  1 +
 files/lua_api/openmw/types.lua     | 18 +++++++++++++++
 5 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100644 apps/openmw/mwlua/types/repair.cpp

diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt
index 925dbdef49..c166b11ae3 100644
--- a/apps/openmw/CMakeLists.txt
+++ b/apps/openmw/CMakeLists.txt
@@ -62,7 +62,7 @@ add_openmw_dir (mwlua
     luamanagerimp object worldview userdataserializer eventqueue
     luabindings localscripts playerscripts objectbindings cellbindings asyncbindings settingsbindings
     camerabindings uibindings inputbindings nearbybindings postprocessingbindings stats debugbindings
-    types/types types/door types/actor types/container types/weapon types/npc types/creature types/activator types/book types/lockpick types/probe types/apparatus types/potion types/misc
+    types/types types/door types/actor types/container types/weapon types/npc types/creature types/activator types/book types/lockpick types/probe types/apparatus types/potion types/misc types/repair
     )
 
 add_openmw_dir (mwsound
diff --git a/apps/openmw/mwlua/types/repair.cpp b/apps/openmw/mwlua/types/repair.cpp
new file mode 100644
index 0000000000..fa1e34ac63
--- /dev/null
+++ b/apps/openmw/mwlua/types/repair.cpp
@@ -0,0 +1,35 @@
+#include "types.hpp"
+
+#include <components/esm3/loadrepa.hpp>
+
+#include <apps/openmw/mwworld/esmstore.hpp>
+
+#include "../luabindings.hpp"
+
+namespace sol
+{
+    template <>
+    struct is_automagical<ESM::Repair> : std::false_type {};
+}
+
+namespace MWLua
+{
+    void addRepairBindings(sol::table repair, const Context& context)
+    {
+        const MWWorld::Store<ESM::Repair>* store = &MWBase::Environment::get().getWorld()->getStore().get<ESM::Repair>();
+        repair["record"] = sol::overload(
+            [](const Object& obj) -> const ESM::Repair* { return obj.ptr().get<ESM::Repair>()->mBase; },
+            [store](const std::string& recordId) -> const ESM::Repair* { return store->find(recordId); });
+        sol::usertype<ESM::Repair> record = context.mLua->sol().new_usertype<ESM::Repair>("ESM3_Repair");
+        record[sol::meta_function::to_string] = [](const ESM::Repair& rec) { return "ESM3_Repair[" + rec.mId + "]"; };
+        record["id"] = sol::readonly_property([](const ESM::Repair& rec) -> std::string { return rec.mId; });
+        record["name"] = sol::readonly_property([](const ESM::Repair& rec) -> std::string { return rec.mName; });
+        record["model"] = sol::readonly_property([](const ESM::Repair& rec) -> std::string { return rec.mModel; });
+        record["mwscript"] = sol::readonly_property([](const ESM::Repair& rec) -> std::string { return rec.mScript; });
+        record["icon"] = sol::readonly_property([](const ESM::Repair& rec) -> std::string { return rec.mIcon; });
+        record["maxCondition"] = sol::readonly_property([](const ESM::Repair& rec) -> int { return rec.mData.mUses; });
+        record["value"] = sol::readonly_property([](const ESM::Repair& rec) -> int { return rec.mData.mValue; });
+        record["weight"] = sol::readonly_property([](const ESM::Repair& rec) -> float { return rec.mData.mWeight; });
+        record["quality"] = sol::readonly_property([](const ESM::Repair& rec) -> float { return rec.mData.mQuality; });
+    }
+}
diff --git a/apps/openmw/mwlua/types/types.cpp b/apps/openmw/mwlua/types/types.cpp
index 6533696896..ea2748dd07 100644
--- a/apps/openmw/mwlua/types/types.cpp
+++ b/apps/openmw/mwlua/types/types.cpp
@@ -170,7 +170,7 @@ namespace MWLua
         addLockpickBindings(addType(ObjectTypeName::Lockpick, {ESM::REC_LOCK}, ObjectTypeName::Item), context);
         addProbeBindings(addType(ObjectTypeName::Probe, {ESM::REC_PROB}, ObjectTypeName::Item), context);
         addApparatusBindings(addType(ObjectTypeName::Apparatus, {ESM::REC_APPA}, ObjectTypeName::Item), context);
-        addType(ObjectTypeName::Repair, {ESM::REC_REPA}, ObjectTypeName::Item);
+        addRepairBindings(addType(ObjectTypeName::Repair, {ESM::REC_REPA}, ObjectTypeName::Item), context);
 
         addActivatorBindings(addType(ObjectTypeName::Activator, {ESM::REC_ACTI}), context);
         addContainerBindings(addType(ObjectTypeName::Container, {ESM::REC_CONT}), context);
diff --git a/apps/openmw/mwlua/types/types.hpp b/apps/openmw/mwlua/types/types.hpp
index 63df9a37f1..aeb4125f79 100644
--- a/apps/openmw/mwlua/types/types.hpp
+++ b/apps/openmw/mwlua/types/types.hpp
@@ -35,6 +35,7 @@ namespace MWLua
     void addLockpickBindings(sol::table lockpick, const Context& context);
     void addProbeBindings(sol::table probe, const Context& context);
     void addApparatusBindings(sol::table apparatus, const Context& context);
+    void addRepairBindings(sol::table repair, const Context& context);
     void addMiscellaneousBindings(sol::table miscellaneous, const Context& context);
     void addPotionBindings(sol::table potion, const Context& context);
 }
diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua
index b7a5948d56..8ecd7458ba 100644
--- a/files/lua_api/openmw/types.lua
+++ b/files/lua_api/openmw/types.lua
@@ -885,6 +885,24 @@
 -- @param openmw.core#GameObject object
 -- @return #boolean
 
+---
+-- Returns the read-only @{#RepairRecord} of a repair tool
+-- @function [parent=#Repair] record
+-- @param #any objectOrRecordId
+-- @return #RepairRecord
+
+---
+-- @type RepairRecord
+-- @field #string id The record ID of the repair tool
+-- @field #string name The name of the repair tool
+-- @field #string model VFS path to the model
+-- @field #string mwscript MWScript on this repair tool (can be empty)
+-- @field #string icon VFS path to the icon
+-- @field #number maxCondition The maximum number of uses of this repair tool
+-- @field #number weight
+-- @field #number value
+-- @field #number quality The quality of the repair tool
+
 --- @{#Activator} functions
 -- @field [parent=#types] #Activator Activator