Skip to content

Commit

Permalink
Don't drag C4AulContext into HUD bar classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Sep 17, 2024
1 parent ca8297f commit 9421ab9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 53 deletions.
59 changes: 26 additions & 33 deletions src/C4HudBars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ C4HudBars::C4HudBars(std::shared_ptr<const C4HudBarsDef> def) noexcept : def{std
}
}

C4HudBar *C4HudBars::BarVal(C4AulContext *cthr, const char *functionName, const std::string &name)
C4HudBar *C4HudBars::BarVal(const char *functionName, const std::string &name)
{
try
{
Expand All @@ -55,28 +55,25 @@ C4HudBar *C4HudBars::BarVal(C4AulContext *cthr, const char *functionName, const
}
else
{
throw C4AulExecError{cthr->Obj, std::format("{}: bar \"{}\" is based on physical and can not be set directly.", functionName, name)};
throw C4HudBarException{std::format("bar \"{}\" is based is based on physical and can not be set directly.", name)};
}
}
catch (const std::out_of_range &)
{
throw C4AulExecError{cthr->Obj, std::format("{}: bar \"{}\" was not defined.", functionName, name)};
throw C4HudBarException{std::format("bar \"{}\" was not defined.", name)};
}

// should never get here
throw C4AulExecError{cthr->Obj, std::format("{}: bar \"{}\" an unexpected error occured.", functionName, name)};
}

void C4HudBars::SetHudBarValue(C4AulContext *cthr, const std::string &name, std::int32_t value, std::int32_t max)
void C4HudBars::SetHudBarValue(const std::string &name, std::int32_t value, std::int32_t max)
{
const auto barval = BarVal(cthr, "SetHudBarValue", name);
const auto barval = BarVal("SetHudBarValue", name);
barval->Value = value;
if(max > 0) barval->Max = max;
}

void C4HudBars::SetHudBarVisibility(C4AulContext *cthr, const std::string &name, bool visible)
void C4HudBars::SetHudBarVisibility(const std::string &name, bool visible)
{
const auto barval = BarVal(cthr, "SetHudBarVisibility", name);
const auto barval = BarVal("SetHudBarVisibility", name);
barval->Visible = visible;
}

Expand Down Expand Up @@ -471,17 +468,17 @@ std::shared_ptr<C4HudBars> C4HudBarsUniquifier::Instantiate(std::shared_ptr<cons
return std::make_shared<C4HudBars>(std::move(definition));
}

std::shared_ptr<C4HudBars> C4HudBarsUniquifier::DefineHudBars(C4AulContext *cthr, C4ValueHash &graphics, const C4ValueArray &definition)
std::shared_ptr<C4HudBars> C4HudBarsUniquifier::DefineHudBars(C4ValueHash &graphics, const C4ValueArray &definition)
{
std::int32_t valueIndex{0};
C4HudBarsDef::Gfxs gfx;
C4HudBarsDef::Bars bars;
C4HudBarsDef::Names names;

ProcessGraphics(cthr, graphics, gfx);
ProcessGroup(cthr, valueIndex, gfx, definition, bars, true);
ProcessGraphics(graphics, gfx);
ProcessGroup(valueIndex, gfx, definition, bars, true);

const auto error = [cthr](std::string msg) { throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: {}", msg)}; };
const auto error = [](std::string msg) { throw C4HudBarException{std::move(msg)}; };
C4HudBarsDef::PopulateNamesFromValues(error, bars, names);

auto def = UniqueifyDefinition(std::make_unique<C4HudBarsDef>(gfx, bars, names));
Expand All @@ -503,7 +500,7 @@ static std::string_view StringToStringView(const C4String *const string)
return StdStrBufToStringView(string->Data);
}

void C4HudBarsUniquifier::ProcessGraphics(C4AulContext *cthr, C4ValueHash &map, C4HudBarsDef::Gfxs &gfx)
void C4HudBarsUniquifier::ProcessGraphics(C4ValueHash &map, C4HudBarsDef::Gfxs &gfx)
{
const auto keyAmount = C4VString("amount");
const auto keyScale = C4VString("scale");
Expand All @@ -513,14 +510,14 @@ void C4HudBarsUniquifier::ProcessGraphics(C4AulContext *cthr, C4ValueHash &map,
{
if (key.GetType() != C4V_String)
{
throw C4AulExecError{cthr->Obj, "DefineHudBars: keys within maps are expected to be of type string"};
throw C4HudBarException{"keys within maps are expected to be of type string"};
}

const auto _key = key.GetRefVal()._getStr()->Data;

if (val.GetType() != C4V_Map)
{
throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: key \"{}\" is not a map, got: {}", StdStrBufToStringView(_key), val.GetDataString())};
throw C4HudBarException{std::format("key \"{}\" is not a map, got: {}", StdStrBufToStringView(_key), val.GetDataString())};
}

const auto &m = *val.GetRefVal()._getMap();
Expand All @@ -538,18 +535,17 @@ void C4HudBarsUniquifier::ProcessGraphics(C4AulContext *cthr, C4ValueHash &map,

if (const auto success = gfx.try_emplace(std::string{StdStrBufToStringView(_key)}, std::string{StdStrBufToStringView(_key)}, std::string{StdStrBufToStringView(_file)}, amount, scale).second; !success)
{
throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: duplicate key \"{}\" in gfx description ", StdStrBufToStringView(_key))};
throw C4HudBarException{std::format("duplicate key \"{}\" in gfx description ", StdStrBufToStringView(_key))};
}
}
}

void C4HudBarsUniquifier::ProcessGroup(C4AulContext *cthr, std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueArray &group, C4HudBarsDef::Bars &bars, const bool advanceAlways)
void C4HudBarsUniquifier::ProcessGroup(std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueArray &group, C4HudBarsDef::Bars &bars, bool advanceAlways)
{
const auto error = [cthr](const char *msg, const C4Value &val)
const auto error = [](const char *msg, const C4Value &val)
{
auto format = std::string{"DefineHudBars: "} + msg;
const std::string dataString{val.GetDataString()};
throw C4AulExecError{cthr->Obj, std::vformat(format, std::make_format_args(dataString))};
throw C4HudBarException{std::vformat(msg, std::make_format_args(dataString))};
};

const std::int32_t size{group.GetSize()};
Expand All @@ -562,7 +558,7 @@ void C4HudBarsUniquifier::ProcessGroup(C4AulContext *cthr, std::int32_t &valueIn
case C4V_Map:
if (const auto *map = element._getMap(); map)
{
ProcessHudBar(cthr, valueIndex, graphics, *map, bars, advanceAlways || i == size-1);
ProcessHudBar(valueIndex, graphics, *map, bars, advanceAlways || i == size-1);
}
else
{
Expand All @@ -575,7 +571,7 @@ void C4HudBarsUniquifier::ProcessGroup(C4AulContext *cthr, std::int32_t &valueIn
{
if (const auto *array = element._getArray(); array)
{
ProcessGroup(cthr, valueIndex, graphics, *array, bars, false);
ProcessGroup(valueIndex, graphics, *array, bars, false);
}
else
{
Expand All @@ -594,21 +590,18 @@ void C4HudBarsUniquifier::ProcessGroup(C4AulContext *cthr, std::int32_t &valueIn
}
}

void C4HudBarsUniquifier::ProcessHudBar(C4AulContext *cthr, std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueHash &bar, C4HudBarsDef::Bars &bars, const bool advance)
void C4HudBarsUniquifier::ProcessHudBar(std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueHash &bar, C4HudBarsDef::Bars &bars, bool advance)
{
auto name = bar[C4VString("name")];
const auto *_name = name.getStr();
if (!_name)
{
throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: HudBar definition has invalid name, got: {}", name.GetDataString())};
throw C4HudBarException{std::format("HudBar definition has invalid name, got: {}", name.GetDataString())};
}

const auto error = [cthr, _name](const char *property, C4Value &val)
const auto error = [_name](const char *property, C4Value &val)
{
auto format = std::string{"DefineHudBars: \"{}\" definition has invalid "} + property + ", got {}";
const std::string dataString{val.GetDataString()};
const char *const data{_name->Data.getData()};
throw C4AulExecError{cthr->Obj, std::vformat(format, std::make_format_args(data, dataString))};
throw C4HudBarException{std::format("\"{}\" definition has invalid {}, got {}", val.GetDataString(), property, StringToStringView(_name))};
};

C4Value gfx{bar[C4VString("gfx")]};
Expand Down Expand Up @@ -649,9 +642,9 @@ void C4HudBarsUniquifier::ProcessHudBar(C4AulContext *cthr, std::int32_t &valueI
{
const auto file = _gfx->Data;

const auto facetError = [cthr, _name](std::string msg)
const auto facetError = [_name](std::string msg)
{
throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: HudBar \"{}\" {}", StringToStringView(_name), msg)};
throw C4HudBarException{std::format("HudBar \"{}\" {}", StringToStringView(_name), msg)};
};

const auto facet = GetFacet(facetError, graphics, file.getData());
Expand Down
21 changes: 13 additions & 8 deletions src/C4HudBars.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
#include <unordered_map>
#include <vector>

struct C4AulContext;
class C4Object;

class C4HudBarException : public std::runtime_error
{
public:
using runtime_error::runtime_error;
};

class C4HudBarDef
{
public:
Expand Down Expand Up @@ -155,11 +160,11 @@ class C4HudBars
C4HudBars(std::shared_ptr<const C4HudBarsDef> def) noexcept;

void DrawHudBars(C4Facet &cgo, C4Object &obj) const noexcept;
void SetHudBarValue(C4AulContext *cthr, const std::string &name, std::int32_t value, std::int32_t max = 0);
void SetHudBarVisibility(C4AulContext *cthr, const std::string &name, bool visible);
void SetHudBarValue(const std::string &name, std::int32_t value, std::int32_t max = 0);
void SetHudBarVisibility(const std::string &name, bool visible);

private:
C4HudBar *BarVal(C4AulContext *cthr, const char *functionName, const std::string &name);
C4HudBar *BarVal(const char *functionName, const std::string &name);
};

namespace std
Expand All @@ -178,12 +183,12 @@ class C4HudBarsUniquifier
std::shared_ptr<C4FacetExID> GetFacet(const std::function<void(std::string)> &error, const C4HudBarsDef::Gfxs &gfx, std::string_view file);
std::shared_ptr<const C4HudBarsDef> UniqueifyDefinition(std::unique_ptr<C4HudBarsDef> definition);
std::shared_ptr<C4HudBars> Instantiate(std::shared_ptr<const C4HudBarsDef> definition);
std::shared_ptr<C4HudBars> DefineHudBars(C4AulContext *cthr, C4ValueHash &graphics, const C4ValueArray &definition);
std::shared_ptr<C4HudBars> DefineHudBars(C4ValueHash &graphics, const C4ValueArray &definition);

private:
void ProcessGraphics(C4AulContext *cthr, C4ValueHash &map, C4HudBarsDef::Gfxs &gfx);
void ProcessGroup(C4AulContext *cthr, std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueArray &group, C4HudBarsDef::Bars &bars, bool advanceAlways);
void ProcessHudBar(C4AulContext *cthr, std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueHash &bar, C4HudBarsDef::Bars &bars, bool advance);
void ProcessGraphics(C4ValueHash &map, C4HudBarsDef::Gfxs &gfx);
void ProcessGroup(std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueArray &group, C4HudBarsDef::Bars &bars, bool advanceAlways);
void ProcessHudBar(std::int32_t &valueIndex, const C4HudBarsDef::Gfxs &graphics, const C4ValueHash &bar, C4HudBarsDef::Bars &bars, bool advance);

using C4HudBarsDefRef = std::reference_wrapper<const C4HudBarsDef>;
using Definitions = std::unordered_map<C4HudBarsDefRef, std::weak_ptr<const C4HudBarsDef>, std::hash<const C4HudBarsDef>, std::equal_to<const C4HudBarsDef>>;
Expand Down
12 changes: 6 additions & 6 deletions src/C4Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2694,7 +2694,7 @@ void C4Object::DrawLine(C4FacetEx &cgo)
FinishedDrawing();
}

bool C4Object::DefineHudBars(C4AulContext *cthr, C4ValueHash *graphics, C4ValueArray *definition)
bool C4Object::DefineHudBars(C4ValueHash *graphics, C4ValueArray *definition)
{
// If null pointer is given restore default hud bars
if (!graphics || !definition)
Expand All @@ -2703,7 +2703,7 @@ bool C4Object::DefineHudBars(C4AulContext *cthr, C4ValueHash *graphics, C4ValueA
return true;
}

auto bars = Game.HudBars.DefineHudBars(cthr, *graphics, *definition);
auto bars = Game.HudBars.DefineHudBars(*graphics, *definition);
if (bars != nullptr)
{
hudBars = bars;
Expand All @@ -2712,14 +2712,14 @@ bool C4Object::DefineHudBars(C4AulContext *cthr, C4ValueHash *graphics, C4ValueA
return false;
}

void C4Object::SetHudBarValue(C4AulContext *cthr, const char *name, int32_t value, int32_t max)
void C4Object::SetHudBarValue(const char *name, int32_t value, int32_t max)
{
hudBars->SetHudBarValue(cthr, name, value, max);
hudBars->SetHudBarValue(name, value, max);
}

void C4Object::SetHudBarVisibility(C4AulContext *cthr, const char *name, bool visible)
void C4Object::SetHudBarVisibility(const char *name, bool visible)
{
hudBars->SetHudBarVisibility(cthr, name, visible);
hudBars->SetHudBarVisibility(name, visible);
}

void C4Object::DrawHudBars(C4Facet &cgo)
Expand Down
6 changes: 3 additions & 3 deletions src/C4Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ class C4Object
C4Fixed nxdir, C4Fixed nydir, C4Fixed nrdir, int32_t iController);
void CompileFunc(StdCompiler *pComp);

bool DefineHudBars(C4AulContext *cthr, C4ValueHash *graphics, C4ValueArray *definition);
void SetHudBarValue(C4AulContext *cthr, const char *name, int32_t value, int32_t max = 0);
void SetHudBarVisibility(C4AulContext *cthr, const char *name, bool fVisible);
bool DefineHudBars(C4ValueHash *graphics, C4ValueArray *definition);
void SetHudBarValue(const char *name, int32_t value, int32_t max = 0);
void SetHudBarVisibility(const char *name, bool fVisible);
void DrawHudBars(C4Facet &cgo);

void DrawLine(C4FacetEx &cgo);
Expand Down
30 changes: 27 additions & 3 deletions src/C4Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,21 +1766,45 @@ static bool FnDefineHudBars(C4AulContext *cthr, C4ValueHash *graphics, C4ValueAr
{
const auto obj = cthr->Obj;
if (!obj) return false;
return obj->DefineHudBars(cthr, graphics, bars);

try
{
return obj->DefineHudBars(graphics, bars);
}
catch (const C4HudBarException &e)
{
throw C4AulExecError{cthr->Obj, std::format("DefineHudBars: {}", e.what())};
}
}

static void FnSetHudBarValue(C4AulContext *cthr, C4String *name, C4ValueInt newValue, C4ValueInt newMax)
{
const auto obj = cthr->Obj;
if (!obj) return;
obj->SetHudBarValue(cthr, FnStringPar(name), newValue, newMax);

try
{
obj->SetHudBarValue(FnStringPar(name), newValue, newMax);
}
catch (const C4HudBarException &e)
{
throw C4AulExecError{cthr->Obj, std::format("SetHudBarValue: {}", e.what())};
}
}

static void FnSetHudBarVisibility(C4AulContext *cthr, C4String *name, bool visible)
{
const auto obj = cthr->Obj;
if (!obj) return;
obj->SetHudBarVisibility(cthr, FnStringPar(name), visible);

try
{
obj->SetHudBarVisibility(FnStringPar(name), visible);
}
catch (const C4HudBarException &e)
{
throw C4AulExecError{cthr->Obj, std::format("SetHudBarVisibility: {}", e.what())};
}
}

// Check / Status
Expand Down

0 comments on commit 9421ab9

Please sign in to comment.