From b0d101ed718b1c5001d2635e7932fddb5e3547f9 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sun, 9 Jun 2013 23:15:43 +0300 Subject: [PATCH] Script: Extend Measure:GetStringValue to support a parameter table Example usage: GetStringValue({AutoScale = 0, Scale = 1.0, NumOfDecimals = 0, Percentual = false}) All keys are optional. --- Library/lua/glue/LuaMeasure.cpp | 58 ++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/Library/lua/glue/LuaMeasure.cpp b/Library/lua/glue/LuaMeasure.cpp index 4dec3d39..e6b470ab 100644 --- a/Library/lua/glue/LuaMeasure.cpp +++ b/Library/lua/glue/LuaMeasure.cpp @@ -120,11 +120,59 @@ static int GetStringValue(lua_State* L) { DECLARE_SELF(L) - int top = lua_gettop(L); - AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF; - double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0; - int decimals = (int)lua_tonumber(L, 4); - bool percentual = lua_toboolean(L, 5); + AUTOSCALE autoScale = AUTOSCALE_OFF; + double scale = 1.0; + int decimals = 0; + bool percentual = false; + + const int top = lua_gettop(L); + if (top == 2 && lua_istable(L, 2)) + { + lua_pushvalue(L, 2); + lua_pushnil(L); + + // Stack: nil, table, ... + while (lua_next(L, -2)) + { + // Stack: value, key, table, ... + // Push copy of key to avoid changing orignal key. + lua_pushvalue(L, -2); + + // Stack: key, value, key, table, ... + const char* key = lua_tostring(L, -1); + if (strcmp(key, "AutoScale") == 0) + { + // TODO: Implement something more reliable than casting. + autoScale = (AUTOSCALE)(int)lua_tonumber(L, -2); + } + else if (strcmp(key, "Scale") == 0) + { + scale = lua_tonumber(L, -2); + } + else if (strcmp(key, "NumOfDecimals") == 0) + { + decimals = (int)lua_tonumber(L, -2); + } + else if (strcmp(key, "Percentual") == 0) + { + percentual = lua_toboolean(L, -2); + } + + lua_pop(L, 2); + // Stack: key, table, ... + } + // Stack: table, ... + + lua_pop(L, 1); + } + else + { + // For backwards compatibility. + if (top > 1) autoScale = (AUTOSCALE)(int)lua_tonumber(L, 2); + if (top > 2) scale = lua_tonumber(L, 3); + if (top > 3) decimals = (int)lua_tonumber(L, 4); + if (top > 4) percentual = lua_toboolean(L, 5); + } const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual); LuaManager::PushWide(L, val);