better error messages for failures in tsc_file.lua

This commit is contained in:
duncathan 2022-01-11 20:17:00 -06:00
parent 04a51779f4
commit c25e8d4c27

View file

@ -10,20 +10,20 @@ end
function TscFile:placeScriptAtEvent(script, event, mapname, needle) function TscFile:placeScriptAtEvent(script, event, mapname, needle)
needle = needle or "<EVE...." needle = needle or "<EVE...."
local wasChanged local err
self._text, wasChanged = self:_stringReplace(self._text, needle, script, event) self._text, err = self:_stringReplace(self._text, needle, script, event)
if not wasChanged then if err ~= nil then
local template = 'Unable to place script "%s" at [%s] event "%s".' local template = 'Unable to place script "%s" at [%s] event "%s".\nCause: %s'
error(template:format(script, mapname, event)) error(template:format(script, mapname, event, err))
end end
end end
function TscFile:placeSongAtCue(songid, event, originalid, mapname) function TscFile:placeSongAtCue(songid, event, originalid, mapname)
local wasChanged local err
self._text, wasChanged = self:_stringReplace(self._text, "<CMU" .. originalid, "<CMU" .. songid, event, {"<CMU0015", "<CMU0000"}) self._text, err = self:_stringReplace(self._text, "<CMU" .. originalid, "<CMU" .. songid, event, {"<CMU0015", "<CMU0000"})
if not wasChanged then if err ~= nil then
local template = "Unable to replace [%s] event #%s's music cue with %q." local template = "Unable to replace [%s] event #%s's music cue with %q.\nCause: %s"
error(template:format(mapname, event, songid)) error(template:format(mapname, event, songid, err))
end end
end end
@ -37,12 +37,12 @@ function TscFile:_stringReplace(text, needle, replacement, label, overrides)
i = text:find(needle, pStart) i = text:find(needle, pStart)
if i == nil then if i == nil then
-- print(('Unable to replace "%s" with "%s"'):format(needle, replacement)) local err = ('No match for "%s".'):format(needle)
return text, false return text, err
elseif i > pEnd then elseif i > pEnd then
-- This is totally normal and can be ignored. -- This is totally normal and can be ignored.
-- print(('Found "%s", but was outside of label.'):format(needle, replacement)) local err = ('Found "%s", but was outside of label (%d, %d) at index %d.'):format(needle, pStart, pEnd, i)
return text, false return text, err
end end
-- find the earliest occurence of an override -- find the earliest occurence of an override
@ -69,7 +69,7 @@ function TscFile:_stringReplace(text, needle, replacement, label, overrides)
assert((j % 1 == 0), tostring(j)) assert((j % 1 == 0), tostring(j))
local a = text:sub(1, i - 1) local a = text:sub(1, i - 1)
local b = text:sub(j + 1) local b = text:sub(j + 1)
return a .. replacement .. b, true return a .. replacement .. b, nil
end end
function TscFile:_getLabelPositionRange(label) function TscFile:_getLabelPositionRange(label)