RhenaudTheLukark / CreateYourFrisk

Rhenaud The Lukark's Unitale fork
GNU General Public License v3.0
132 stars 56 forks source link

Improve DEBUG #86

Closed deonix37 closed 3 years ago

RhenaudTheLukark commented 3 years ago

The addition you made is welcome, but it's also lacking, I'm afraid.

Here's an example I tried, which I think is missing some important data:

 DEBUG({ a = 1, c = 2, test = 42, 5 }, "wow", 2, Player)

image

Plus I'm afraid having a table debugger inside of the C# debugger object doesn't give much freedom as to what you want to print: what if the user wants to print the tables inside said table, and if that's the case, how does one handle cycling references? (one table that contains another that itself contains the first table)

We already have a Lua function that displays tables correctly and which takes care of some of these problems (I'm adding it at the end of the comment, although it doesn't take care of cycling references), so it's up to you whether you want to tackle the problems I talked about or if you want to all simply drop the merge.

-- Debugs a table's content.
function TableDebugger(tab, nonest, nest)
    if nest == nil then nest = 1 end
    local result = "{\n"

    local first = true -- first is used to not create a comma on the first element of a line
    for k, v in pairs(tab) do
        -- If the key's a number, transforms it into a string
        if type(k) == "number" then k = "\"" .. tostring(k) .. "\"" end
        -- If the value is a string, keep the quotes
        if type(v) == "string" then v = "\"" .. tostring(v) .. "\"" end
        -- If the value is a table, call this function recursively
        if type(v) == "table" then
            if nonest then
                v = "(table)"
            else
                v = TableDebugger(v, false, nest + 1)
            end
        end

        v = tostring(k) .. " = " .. tostring(v) -- Add index of value

        -- Add comma and indents
        local stringToAdd = first and "" or ",\n"
        for i = 1, nest do stringToAdd = stringToAdd .. "    " end -- INDENTS!!!!!
        result = result .. stringToAdd .. v
        first = false
    end
    result = result .. "\n"
    for i = 1, nest - 1 do result = result .. "    " end -- INDENTS!!!!!
    if nest == 1 then DEBUG(result .. "}") else return result .. "}" end
end
deonix37 commented 3 years ago

Indeed, it fails on cycling references, so serialization shouldn't be used. But still, this PR aims for user friendliness so we don't have to write multiple debug statements or experience crashes when passing tables. So is it good without serialization?

RhenaudTheLukark commented 3 years ago

Alright, sorry for not taking action for a while, had some stuff to take care of with real life.

Now, I still think this feature isn't really "needed", since there's an easy way to do what you're trying to do, and the feature you're implementing here has a lot of limitations, as we've seen before.

I just think it's better to keep the old system, even if it requires a little more work on the modder's end. Although, I like the idea of being able to send several parameters to DEBUG, so maybe there's something to look into with that?

All in all, unless you have something else to add, I'm ready to close this PR.