Closed JoeNotCharles closed 5 months ago
Each of these 5 cases takes a different path through NSSEditor.build()
, starting at nss.py:186:
186: compiled_bytes: bytes | None = compileScript(...)
187: print("compiling script from nsseditor")
188: if compiled_bytes is None:
189: print("user cancelled the compilation")
190: return None, b""
191:
192: msg = "Could not convert to bytes - nsseditor.build()"
193: raise ValueError(msg)
The calling function is Editor.save()
, starting at editor.py:246:
246: try:
247: data, data_ext = self.build()
248: if data is None: # nsseditor
249: return
... implementation of saving ...
264: except Exception as e:
... log exception to errorlog.txt ...
269: QMessageBox(QMessageBox.Critical, "Failed to write to file", str(universal_simplify_exception(e))).exec_()
I'll trace through each case:
This is almost right, but the stdout is wrong. It's printing 'compiling script from nsseditor' after calling compileScript()
, so it prints it even though the user chose not to compile.
As expected, compileScript()
returns None, and line 190 also returns None. The calling save()
function interprets that as "nothing to do" (editor.py:248).
The stdout shows the compile succeeded (except, again, 'compiling script from nsseditor' is printed afterwards).
However since the return value isn't None, it's ignored on line 192 and instead a bogus ValueError
is raised. This looks like a cut-and-paste error.
This gave an error message, so it "works", even though the error was kind of generic.
At nss.py:186, compileScript()
is (correctly) raising an exception that it can't find the temp file nwnnsscomp.exe was supposed to write.
It'd be nice to display the actual error message from nwnnsscomp.exe (which I see in errorlog.txt is in the stderr
variable in _compile_windows()
), but that's just a bonus.
This was an exception deep in the built-in compiler that I didn't look into further. (I'm not even sure if the built-in compiler is expected to work on Windows. I suspect a character set or line ending issue or something.
nss.py and editor.py are working as designed here: compileScript()
propagates the exception from the compiler, and editor.py catches it and shows the message box.
Working perfectly. (Except that printing compiling script from nsseditor
is still in the wrong place.) This time the exception from the compiler is correct, because the script actually did have an error, and it raised a good contextual error message which nss.py and editor.py handled perfectly. Yay!
My first attempt to fix this fixed case 2, but apparently broke case 3 or 5. I'll try again.
Re-tested all 5 cases with the patch in https://github.com/NickHugi/PyKotor/pull/72:
compiling script from nsseditor
user exited
user cancelled the compilation
compiledScript()
to save()
, which saves it. There's no success dialog, thoughstdout is now:
compiling script from nsseditor
user chose No, compiling with nwnnsscomp
Compiling: C:\Users\JoeNotCharles\temp\tempscript_f8cd3f.nss
Total Execution time = 31 ms
compiling script from nsseditor
user chose No, compiling with nwnnsscomp
Compiling: C:\Users\JoeNotCharles\temp\tempscript_4b4a39.nss
Compilation aborted with errors
Total Execution time = 31 ms
C:\Users\JoeNotCharles\temp\tempscript_4b4a39.nss(2): Error: Undeclared identifier "UndefinedFunction"
stdout is now:
compiling script from nsseditor
user chose Yes, compiling with builtin
I think this is the case Wizard said on discord was broken for him - a script with an error in it, so it's expected to give an error dialog. I don't know why this is working for me and not him?
stdout is now:
compiling script from nsseditor
user chose Yes, compiling with builtin
I messed up all the branches & pull requests on my repo. The fix for this is now in https://github.com/JoeNotCharles/PyKotor/tree/fix_nsc_build.
EDIT: Everything below is incorrect, I was testing the wrong branch... I'm able to get the exact same results you are getting in your initial post. Your branch 'fix_nsc_build' correctly fixes it though. Please submit a PR so I can continue with the other proposed NCS changes.
There are 5 cases:
- User hits Cancel. It should return without saving, and without an error message.
- User chooses nwnnsscomp.exe, which succeeds. It should write to the .ncs file.
- User chooses nwnnsscomp.exe, but there's an error in the script. It should show an error message, and not write any file.
- User chooses built-in compiler, which succeeds. It should write to the .ncs file.
- User chooses built-in compiler, but there's an error in the script. It should show an error message, and not write any file.
Tried to reproduce all of these. Here are my results:
BAD: Error dialog with title Failed to write to file and message ('ValueError', ''Could not convert to bytes - nsseditor.build()'). Can not reproduce, I get this result:
user chose No, compiling with nwnnsscomp Lookup path root set to: C:\Program Files (x86)\Steam\steamapps\common\swkotor\ Loaded nwscript.nss from C:\Program Files (x86)\Steam\steamapps\common\swkotor\override/ Compiling: tempscript_1a5803.nss Total Execution time = 15 ms
5: Same result (OK) I correctly get this error:
I've tested on the exact same commit you did. Not sure why we're getting different results.
The only major difference I notice is saveAs on your new branch here correctly shows the success message when compiling with either option. Currently on master the success message only shows when explicitly using 'compile'.
Yeah looks like this is a simple case of me forgetting that the 'save as' dialog should allow directly saving as .ncs. Usually when testing I would only save as .nss and when I wanted a compiled script I'd specifically use 'compile'.
There's a few problems with your proposed fix on that branch, such as it changing self._filepath
to the location of the compiled script. That's such a minor problem though.
I'm gonna mark this fixed, then. The whole thing about Save As / Compile changing from .nss to .nsc is confusing, and I think we shouuld tackle it separately.
Tested at commit ebcd2176badfc2912fbbec52251abaecff3ddb37 on Windows:
Open any .nss file (I used
Vanilla_KOTOR_Script_Source\TSL\Vanilla\Modules\001EBO_Ebon_Hawk_(prologue_interior)\a_end_001.nss
) and then use "Save As" and choose an output file with the .ncs file name.It should pop up a dialog to select a compiler, and then invoke the chosen compiler to save the script as an NCS.
There are 5 cases:
For case 2 and 5, I added a call
UndefinedFunction();
to the start of the script, which should fail to compile since, you know, the function's undefined.Results
Failed to write to file
and message('ValueError', ''Could not convert to bytes - nsseditor.build()')
.stdout says:
errorlog.txt contains:
Failed to write to file
and message(FileNotFoundError', "Could not find temp compiled script at 'C:\\Users\\JoeNotCharles\\temp\\tempscript_089466.ncs'")
.stdout says:
errorlog.txt contains:
Failed to write to file
and message('error, 'argument out of range')
.stdout says:
errorlog.txt contains:
Failed to write to file
and message('CompileError', "Function 'UndefinedFunction' has not been defined.")
.stdout says:
errorlog.txt contains: