refactor(model): share the ModelScene argument checks

Scene.cpp and SceneActor.cpp each had NumberArgs and FloatArg, the same
loop that raises the engine's "Usage: <owner>:<usage>" error when an
argument is not a number, with the frame and actor arguments in a
different order.

One NumberArgs, which takes the owner's display name, and one FloatArg
are now declared in model/Scene.h and used by both files.
This commit is contained in:
Brues
2026-11-09 11:51:27 -06:00
parent b644567cf9
commit 627c86f5ba
3 changed files with 53 additions and 46 deletions
+36 -26
View File
@@ -576,18 +576,6 @@ void *__fastcall CreateModelScene(void *parent) {
void *Self(void *L) { return Game::Lua::ResolveTypedObject(L, 1, g_class.TypeIdVar()); }
bool NumberArgs(void *L, int first, int count, void *frame, const char *usage) {
for (int i = 0; i < count; ++i) {
if (!Game::Lua::IsNumber(L, first + i)) {
Game::Lua::Error(L, "Usage: %s:%s", Game::Lua::ObjectDisplayName(frame), usage);
return false;
}
}
return true;
}
float FloatArg(void *L, int index) { return static_cast<float>(Game::Lua::ToNumber(L, index)); }
int PushVec3(void *L, const Vec3 &v) {
Game::Lua::PushNumber(L, v.x);
Game::Lua::PushNumber(L, v.y);
@@ -632,7 +620,8 @@ int __fastcall Script_GetNumActors(void *L) {
int __fastcall Script_GetActorAtIndex(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 1, frame, "GetActorAtIndex(index)"))
if (frame == nullptr || !NumberArgs(L, 2, 1, Game::Lua::ObjectDisplayName(frame),
"GetActorAtIndex(index)"))
return 0;
const std::vector<Actor *> &actors = *F(frame).actors;
const double index = Game::Lua::ToNumber(L, 2);
@@ -644,7 +633,8 @@ int __fastcall Script_GetActorAtIndex(void *L) {
int __fastcall Script_SetCameraPosition(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, "SetCameraPosition(x, y, z)"))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"SetCameraPosition(x, y, z)"))
return 0;
F(frame).position = Vec3Args(L, 2);
ApplyCamera(frame);
@@ -659,7 +649,8 @@ int __fastcall Script_GetCameraPosition(void *L) {
int __fastcall Script_SetCameraOrientationByYawPitchRoll(void *L) {
void *frame = Self(L);
if (frame == nullptr ||
!NumberArgs(L, 2, 3, frame, "SetCameraOrientationByYawPitchRoll(yaw, pitch, roll)"))
!NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"SetCameraOrientationByYawPitchRoll(yaw, pitch, roll)"))
return 0;
SetOrientation(F(frame), FloatArg(L, 2), FloatArg(L, 3), FloatArg(L, 4));
ApplyCamera(frame);
@@ -669,7 +660,7 @@ int __fastcall Script_SetCameraOrientationByYawPitchRoll(void *L) {
int __fastcall Script_SetCameraOrientationByAxisVectors(void *L) {
void *frame = Self(L);
if (frame == nullptr ||
!NumberArgs(L, 2, 9, frame,
!NumberArgs(L, 2, 9, Game::Lua::ObjectDisplayName(frame),
"SetCameraOrientationByAxisVectors(forwardX, forwardY, forwardZ, rightX, "
"rightY, rightZ, upX, upY, upZ)"))
return 0;
@@ -704,7 +695,7 @@ constexpr char kUsageSetFarClip[] = "SetCameraFarClip(farClip)";
template <float Fields::*Field, const char *Usage>
int __fastcall SetCameraScalar(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 1, frame, Usage))
if (frame == nullptr || !NumberArgs(L, 2, 1, Game::Lua::ObjectDisplayName(frame), Usage))
return 0;
F(frame).*Field = FloatArg(L, 2);
ApplyCamera(frame);
@@ -763,7 +754,8 @@ int __fastcall Script_SetPaused(void *L) {
int __fastcall Script_Project3DPointTo2D(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, "Project3DPointTo2D(pointX, pointY, pointZ)"))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"Project3DPointTo2D(pointX, pointY, pointZ)"))
return 0;
double out[3];
if (!Project(frame, Vec3Args(L, 2), out))
@@ -775,7 +767,8 @@ int __fastcall Script_Project3DPointTo2D(void *L) {
int __fastcall Script_SetViewInsets(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 4, frame, "SetViewInsets(left, right, top, bottom)"))
if (frame == nullptr || !NumberArgs(L, 2, 4, Game::Lua::ObjectDisplayName(frame),
"SetViewInsets(left, right, top, bottom)"))
return 0;
float *in = F(frame).viewInsets;
for (int i = 0; i < 4; ++i)
@@ -794,7 +787,8 @@ int __fastcall Script_GetViewInsets(void *L) {
int __fastcall Script_SetViewTranslation(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 2, frame, "SetViewTranslation(translationX, translationY)"))
if (frame == nullptr || !NumberArgs(L, 2, 2, Game::Lua::ObjectDisplayName(frame),
"SetViewTranslation(translationX, translationY)"))
return 0;
F(frame).viewTranslation[0] = FloatArg(L, 2);
F(frame).viewTranslation[1] = FloatArg(L, 3);
@@ -826,7 +820,8 @@ int __fastcall Script_GetAllowOverlappedModels(void *L) {
int __fastcall Script_SetLightType(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 1, frame, "SetLightType(lightType)"))
if (frame == nullptr || !NumberArgs(L, 2, 1, Game::Lua::ObjectDisplayName(frame),
"SetLightType(lightType)"))
return 0;
SetLightType(frame, Game::Lua::ToNumber(L, 2) != 0.0 ? 1u : 0u);
return 0;
@@ -856,7 +851,8 @@ int __fastcall Script_IsLightVisible(void *L) {
int __fastcall Script_SetLightDirection(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, "SetLightDirection(x, y, z)"))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"SetLightDirection(x, y, z)"))
return 0;
SetLightDirection(frame, Vec3Args(L, 2));
return 0;
@@ -869,7 +865,8 @@ int __fastcall Script_GetLightDirection(void *L) {
int __fastcall Script_SetLightPosition(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, "SetLightPosition(x, y, z)"))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"SetLightPosition(x, y, z)"))
return 0;
SetLightPosition(frame, Vec3Args(L, 2));
return 0;
@@ -882,7 +879,7 @@ int __fastcall Script_GetLightPosition(void *L) {
int SetLightColor(void *L, float *(*color)(void *), const char *usage) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, usage))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame), usage))
return 0;
SetColor(color(frame), Vec3Args(L, 2));
return 0;
@@ -907,7 +904,8 @@ int __fastcall Script_GetLightDiffuseColor(void *L) { return GetLightColor(L, &D
int __fastcall Script_SetFogColor(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 3, frame, "SetFogColor(colorR, colorG, colorB)"))
if (frame == nullptr || !NumberArgs(L, 2, 3, Game::Lua::ObjectDisplayName(frame),
"SetFogColor(colorR, colorG, colorB)"))
return 0;
SetFogColor(frame, Vec3Args(L, 2));
return 0;
@@ -924,7 +922,7 @@ int __fastcall Script_GetFogColor(void *L) {
template <uintptr_t Offset, const char *Usage>
int __fastcall SetFogDistance(void *L) {
void *frame = Self(L);
if (frame == nullptr || !NumberArgs(L, 2, 1, frame, Usage))
if (frame == nullptr || !NumberArgs(L, 2, 1, Game::Lua::ObjectDisplayName(frame), Usage))
return 0;
Game::Ref<float>(frame, Offset) = FloatArg(L, 2);
return 0;
@@ -1207,4 +1205,16 @@ const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
bool NumberArgs(void *L, int first, int count, const char *owner, const char *usage) {
for (int i = 0; i < count; ++i) {
if (!Game::Lua::IsNumber(L, first + i)) {
Game::Lua::Error(L, "Usage: %s:%s", owner, usage);
return false;
}
}
return true;
}
float FloatArg(void *L, int index) { return static_cast<float>(Game::Lua::ToNumber(L, index)); }
} // namespace Model::Scene
+6
View File
@@ -100,4 +100,10 @@ class Actor : public Animation::Object {
// Registers the actor class in the current Lua state.
void RegisterActorClass();
// The methods' argument checks, for the frame and its actors. True when the
// `count` arguments from `first` are numbers; else the engine's usage error,
// "Usage: <owner>:<usage>".
bool NumberArgs(void *L, int first, int count, const char *owner, const char *usage);
float FloatArg(void *L, int index);
} // namespace Model::Scene
+11 -20
View File
@@ -430,18 +430,6 @@ namespace {
Actor *Self(void *L) { return static_cast<Actor *>(Animation::Resolve(L, 1, Actor::s_class)); }
bool NumberArgs(void *L, int first, int count, const char *usage, const Actor *actor) {
for (int i = 0; i < count; ++i) {
if (!Game::Lua::IsNumber(L, first + i)) {
Game::Lua::Error(L, "Usage: %s:%s", actor->DisplayName(), usage);
return false;
}
}
return true;
}
float FloatArg(void *L, int index) { return static_cast<float>(Game::Lua::ToNumber(L, index)); }
// Shows a creature display, dressed by its CreatureDisplayInfoExtra record.
bool ShowCreatureDisplay(Actor &actor, uint32_t displayID) {
const char *path = nullptr;
@@ -459,7 +447,8 @@ bool ShowCreatureDisplay(Actor &actor, uint32_t displayID) {
int __fastcall Script_SetModelByCreatureDisplayID(void *L) {
Actor *actor = Self(L);
if (actor == nullptr ||
!NumberArgs(L, 2, 1, "SetModelByCreatureDisplayID(creatureDisplayID)", actor))
!NumberArgs(L, 2, 1, actor->DisplayName(),
"SetModelByCreatureDisplayID(creatureDisplayID)"))
return 0;
Game::Lua::PushBool(L, ShowCreatureDisplay(*actor, static_cast<uint32_t>(Game::Lua::ToNumber(L, 2))));
return 1;
@@ -554,7 +543,8 @@ int __fastcall Script_Undress(void *L) {
int __fastcall Script_UndressSlot(void *L) {
Actor *actor = Self(L);
if (actor == nullptr || !NumberArgs(L, 2, 1, "UndressSlot(inventorySlot)", actor))
if (actor == nullptr || !NumberArgs(L, 2, 1, actor->DisplayName(),
"UndressSlot(inventorySlot)"))
return 0;
Outfit::UndressSlot(actor->model, actor->dresser, actor->lanes,
static_cast<int>(Game::Lua::ToNumber(L, 2)));
@@ -638,7 +628,7 @@ int __fastcall Script_AttachToMount(void *L) {
if (mount == nullptr)
return 0;
Actor *rider = RiderArg(L, mount, kUsage);
if (rider == nullptr || !NumberArgs(L, 3, 1, kUsage, mount))
if (rider == nullptr || !NumberArgs(L, 3, 1, mount->DisplayName(), kUsage))
return 0;
const bool seated = rider != mount && mount->model != nullptr && rider->model != nullptr &&
rider->SceneFrame() == mount->SceneFrame();
@@ -702,7 +692,7 @@ int __fastcall Script_GetModelPath(void *L) {
int __fastcall Script_SetPosition(void *L) {
Actor *actor = Self(L);
if (actor == nullptr || !NumberArgs(L, 2, 3, "SetPosition(x, y, z)", actor))
if (actor == nullptr || !NumberArgs(L, 2, 3, actor->DisplayName(), "SetPosition(x, y, z)"))
return 0;
for (int i = 0; i < 3; ++i)
actor->position[i] = FloatArg(L, 2 + i);
@@ -727,7 +717,7 @@ constexpr char kUsageSetScale[] = "SetScale(scale)";
template <float Actor::*Field, const char *Usage>
int __fastcall SetFloat(void *L) {
Actor *actor = Self(L);
if (actor == nullptr || !NumberArgs(L, 2, 1, Usage, actor))
if (actor == nullptr || !NumberArgs(L, 2, 1, actor->DisplayName(), Usage))
return 0;
actor->*Field = FloatArg(L, 2);
return 0;
@@ -744,7 +734,7 @@ int __fastcall GetFloat(void *L) {
int __fastcall Script_SetAlpha(void *L) {
Actor *actor = Self(L);
if (actor == nullptr || !NumberArgs(L, 2, 1, "SetAlpha(alpha)", actor))
if (actor == nullptr || !NumberArgs(L, 2, 1, actor->DisplayName(), "SetAlpha(alpha)"))
return 0;
const float a = FloatArg(L, 2);
actor->alpha = a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a);
@@ -813,7 +803,8 @@ int __fastcall Script_IsVisible(void *L) {
int __fastcall Script_SetAnimation(void *L) {
Actor *actor = Self(L);
if (actor == nullptr || !NumberArgs(L, 2, 1, "SetAnimation(animation [, variation, animSpeed, animOffsetSeconds])", actor))
if (actor == nullptr || !NumberArgs(L, 2, 1, actor->DisplayName(),
"SetAnimation(animation [, variation, animSpeed, animOffsetSeconds])"))
return 0;
actor->animation = static_cast<uint32_t>(Game::Lua::ToNumber(L, 2));
actor->variation = Game::Lua::IsNumber(L, 3) ? static_cast<int>(Game::Lua::ToNumber(L, 3)) : -1;
@@ -843,7 +834,7 @@ int __fastcall Script_GetAnimationVariation(void *L) {
int __fastcall Script_SetAnimationBlendOperation(void *L) {
Actor *actor = Self(L);
if (actor == nullptr ||
!NumberArgs(L, 2, 1, "SetAnimationBlendOperation(blendOperation)", actor))
!NumberArgs(L, 2, 1, actor->DisplayName(), "SetAnimationBlendOperation(blendOperation)"))
return 0;
actor->blendOperation = Game::Lua::ToNumber(L, 2) != 0.0 ? 1u : 0u;
return 0;