Skip to content

[cli] add lmlsb extractcsv and lmlsb insertcsv commands - #37

Merged
pmrowla merged 11 commits into
masterfrom
lns-lines-api
May 4, 2020
Merged

pmrowla merged 11 commits into
masterfrom
lns-lines-api

Conversation

@pmrowla

@pmrowla pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner

(probably) replaces #33

  • rename commands from old PR to extractcsv and insertcsv
  • adjust CSV columns to be more consistent with other pylivemaker commands
    • use the same xxxx-<name> or xxxx-linenum convention used in lmlsb extract for labeling specific scenarios
  • move non-CSV related logic into new TpWord.get_lines() and TpWord.replace_lines() API methods

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner Author

CI failing because blake2 isn't available in py35. Will decide whether or not to just deprecate support for 3.5 later.

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner Author

@Stefan311 @LioMajor can you test this branch w/your games? I tried it in AGLS and it seems to work.

column format has changed a bit, so I'm assuming the google translate script needs to be updated.

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

The column format is one thing i dislike on the new implementation. The old format was compatible to the menu functions.
For the google translate i can use the "-fc 2 -tc 3" options, but I have to create a new csv because of the changed "Scenario name" column. Translating ~30k lines again will take a while...
Edit: maybe you can put an empty column in? Maybe for "Comments"? Just to have the same col numbers on all csv uses.

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner Author

The column format is one thing i dislike on the new implementation. The old format was compatible to the menu functions.
For the google translate i can use the "-fc 2 -tc 3" options, but I have to create a new csv because of the changed "Scenario name" column. Translating ~30k lines again will take a while...
Edit: maybe you can put an empty column in? Maybe for "Comments"? Just to have the same col numbers on all csv uses.

At least for testing, the line counts between the old and new csv's should be the same, so you could just make a script to copy the translated columns from an old csv into a new one (or just copy/paste cells in excel or whatever else).

but it does make sense to have consistent columns between the menus and this, I'll take a look at what the menu stuff does

@Stefan311

Copy link
Copy Markdown
Contributor

I fear that would not work. Made this:
for f in *.lsb; do lmlsb extractmenu --append $f texts.csv; lmlsb extracttext --append $f texts.csv; done
By the way, you can hint your users in the documentation to use command line loops for mass-file handling.

@pmrowla

pmrowla commented May 2, 2020 •

Copy link
Copy Markdown
Owner Author

Ah, I see what you mean now. So you're appending both menus and scenario lines into one csv.

I'm honestly not sure that's a use case we really need to support? It doesn't seem like a huge difference to me if users need to use separate menus.csv and script_lines.csv files, especially given that inserting and extracting them works differently - script lines come from TpWord blocks, and menus come from parsing top level LSB commands.

(but I can at least see that having the same # of columns, with orig_text and translated_text in the same columns would make running automated translation things more convenient)

will have to think about it

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

Something is wrong with the Scenario name:

000002F5.lsb,000002F5-line2261, いっ、いやぁーーーーーっ!!!,
000002F5.lsb,000002F5-line2265, うおっ!?,
000002F5.lsb,000002F5-line2265, な、なんだ、このガスは?,
000002F5.lsb,000002F5-line2269, はぁ、はぁ、はぁ・・・!,

Many line numbers are double.

;---------------------------------------
<SCENARIO VER="105">
<TXSPN> うおっ!?<BR>
 な、なんだ、このガスは?<PG>
{MESOFF "0"}
{CHANGECG "イベント" "グラフィック\イベント\公園テント\感1\公園テント感1_10.gal" "NORMAL" "5" "500" "50" "10" "" "0" "0" "0" "255" ""}
{}
<EVENT VALUE="MESEND">
;---------------------------------------

@pmrowla

pmrowla commented May 2, 2020 •

Copy link
Copy Markdown
Owner Author

That's expected now. the row's are just labeled according to the equivalent file they would have originally been extracted to by lmlsb extract, so for those two lines it would have been 000002F5-line2265.lns

it may make sense to have columns for LNSTextLine.start/.end/.digest, although digest becomes less useful since what is considered the "original digest" changes everytime you patch an lsb (the most recently patched translation becomes "original").

edit: and on patching/insert now, the script verifies that the # of lines in the csv for 000002F5-line2265 match the actual number of lines in command #2269 in 2F5.lsb

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

That's a problem. There are multiple lines in this file. How does the insert decide which one to use?
Edit: So the count and the sequence order have to be correct after translation.

@pmrowla

pmrowla commented May 2, 2020 •

Copy link
Copy Markdown
Owner Author

That's a problem. There are multiple lines in this file. How does the insert decide which one to use?

it currently assumes that lines in the csv are in the same order they were extracted in by extractcsv and compares the line count from csv with the line count in the lsb to patch

if patched:
lines = scenario.get_lines()
if len(lines) != len(new_lines):
raise LiveMakerException(
"CSV line count does not match original scenario line count for {}".format(name)
)
for i, line in enumerate(lines):
line.text = new_lines[i]
scenario.replace_lines(lines)

if we want it to be stricter, we can add csv columns for the original line char indexes, and original line checksum/digest (LNSTextLine.digest, LNSTextLine.start, LNSTextLine.end)

@pmrowla

pmrowla commented May 2, 2020 •

Copy link
Copy Markdown
Owner Author

That's a problem. There are multiple lines in this file. How does the insert decide which one to use?
Edit: So the count and the sequence order have to be correct after translation.

Yes.

If we just store the index for line out of total line count, we still have the same issue of assuming that count matches before and after. The only "benefit" would be that you could now have lines out of sequence in your CSV.

If we use digest or character offsets, we have to take into consideration that they change every time you patch the LSB. If the original jp line has 10 characters, and the new translation has 11, all of the offsets for subsequent characters has now changed. So if we are going to use digest or char offsets, we would need to make sure the user is only patching original LSBs each time, rather than re-patching an already translated and patched LSB, since the CSV would have the digest and char offsets corresponding to the original JP version.

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

If you use the char-index or checksums you will lose the possibility to re-apply a slightly changed translation. You always have to use the original files.
Using a line counter was not without thought.
Edit: I see we are thinking about the same things the same time :)

@pmrowla

pmrowla commented May 2, 2020 •

Copy link
Copy Markdown
Owner Author

If you use the char-index or checksums you will lose the possibility to re-apply a slightly changed translation. You always have to use the original files.
Using a line counter was not without thought.

Right. But using line counter only gives us protection in the case where the user has lines for a specific file out of order in their CSV (which is not something I think we should support). Using the scenario name as it is now does allow the user to have scripts in any order in their CSV, since they are named by either an actual named Label, or the command number for an un-labeled script.

So you can append extracted scripts to your csv in any order, but individual lines in those extracted scripts must stay in order.

@Stefan311

Copy link
Copy Markdown
Contributor

Right. But using line counter only gives us protection in the case where the user has lines for a specific file out of order in their CSV (which is not something I think we should support). Using the scenario name as it is now does allow the user to have scripts in any order in their CSV, since they are named by either an actual named Label, or the command number for an un-labeled script.

So you can append extracted scripts to your csv in any order, but individual lines in those extracted scripts must stay in order.

Since the file name was also in every line, the lines could be mixed over the whole game.
But you are right. We do not have to support this. What about this:
Translator: "An line with just a single dot? Away with this!"
(I have to work with very stupid peoples sometimes...)

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner Author

But you are right. We do not have to support this. What about this:
Translator: "An line with just a single dot? Away with this!"

yeah removing an entire line from the csv would be a problem. I guess in this case you just need to make sure your translator(s) know not to mess with the csv file structure?

@LioMajor

LioMajor commented May 2, 2020 •

Copy link
Copy Markdown

There is still a weakness without full japanese support.

When i translated the file i was sure i removed all non ascii conform characters,
but i missed a single japanese space.

When extracting the csv again including the japanese space, it looks like this:

00000B6F.lsb,00000B6F-line130," ""Hmm...�@after all",

But should look like this 00000B6F.lsb,00000B6F-line130,Hmm... After all,

I noticed this later when using notepad++ regular search function
(highlight) [^\x00-\x7F]+

image

PS:

How about using < br > when there are multiple rows?
Ex. 00000B6F.lsb,00000B6F-line130,This is row 1< br >This is row 2< br >And so on...

When inserting it back, even a left out or added row wouldn't be a problem, or?

@Stefan311

Copy link
Copy Markdown
Contributor

Seems there is a bad encoding somewhere.
If I understand you right, this was happen:

Original lsb (cp932) -->extract--> Original csv (UTF-8)
Original csv (UTF-8) -->translating--> Translated csv (UTF-8)
Translated csv (Threated as UTF-8) -->insert--> Translated lsb (cp932)
Translated lsb (cp932) -->extract--> Verify csv (UTF-8)

So the issue is wrong encoding somewhere. The Question is WHERE.
Is the space ingame visible as space?
Which encoding do you use for save the translated csv in notepad++?
Which encoding do you use for displaying the verify csv?

To insert or remove line breaks you still have to use the "classic" lns functions.
The new functions completely leave all non-text stuff untouched - even page breaks.

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner Author

It would be possible to have support for some tags in CSV, but it's not included w/the current implementation.

One of the reasons I ended up with the ideas for #20 (comment) is that it would give you control over line breaks. In cases where you want control over line breaks (which is especially relevant for LM given that message boxes won't nicely word wrap for you), it makes more sense to group text as blocks/paragraphs, rather than by single lines.

But I think only supporting basic/minimal text is fine in these csv commands for now

@LioMajor

LioMajor commented May 2, 2020

Copy link
Copy Markdown

@Stefan311 i'm using shift-jis encoding.

Sadly the game i'm translating has so many triggers
and i don't know in what part this line will be shown.

I guess it's possible to see the @ inside the game since
the line is wrapped in ". Also it's strange that there is ""
in front of the text.

For the time now, i hunted all lsb csv's to remove left
over japanese spaces just to make sure.

@pmrowla to have csv is very convenient to check if there
are still japanese lines you might have missed. I have over
18.000 lns, to edit every single one is a hell luva work.

Thanks a ton to add csv support!

To have lines in blocks would be the best solution if possible.

The other choice is to convert all multiple rows into a single
one. As for translation, we are very lucky. LM is forgiving too
long rows and autowraps. Also when the text exceed the
max possible shown rows, it adds a clickaction to continue.

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

@pmrowla What encoding does open(csv_file, newline="\n") default to?
The python documentation says

the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Seems this is the reason @LioMajor says full japanese system locale is required.
Maybe this should be open(csv_file, newline="\n", encoding="utf-8") or even as option parameter.

@LioMajor the csv file must be saved in the same encoding it is loaded by lmlsb. I am not sure which is the python default one on your system, on mine it is utf-8.
If you are not comfortable with the " " wrapping, you can consider to use M$ Excel (or similar). They doing the wrapping automatic on save.

@LioMajor

LioMajor commented May 2, 2020 •

Copy link
Copy Markdown

@Stefan311 i started to use notepad++ because it doesn't change the encoding itself and
this worked flawless for me. I only need to wrap lines in " when i need to use a comma to
not break the csv (not adding a seperator for text usage).

If not using a full japanese system locale, pylivemaker can only extract the csv without
japanese chars or it will throw an error and the generated file stops at that line.
image

Using lns works even on non japanese locale properly in both directions.
image

@Stefan311

Stefan311 commented May 2, 2020 •

Copy link
Copy Markdown
Contributor

File C:\Python\lib\encoding\cp1252.py

cp1252... Latin... Thats the default encoding on your system. Ok, the encoding parameter is really needed on all file operations to be system independed.

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

Yeah, we should be specifying an encoding when writing csv files, probably best to default to utf8 but also allow c932, like other pylm tools. I'll push a fix for that.

Thinking about the line break issue, since CSV does allow newlines in cells (as long as the data is properly quoted), we could group text in blocks rather than lines. So something like:

  • Adjust new api so it allows working on "blocks" instead of "lines"
  • Group text by blocks separated by any "special" break (so any break that is not <BR>), or non-text TpWord script operator
    • for reference, special breaks are <PG>, <PS>, <CLR> (although I've only ever seen games which use <PG> and <BR>)
  • For CSV, use regular line-breaks in the text cells in place of <BR> tags
    • So when extracting, anywhere we would put <BR> in an lns script, the CSV original_text cell would contain newlines (probably always using windows format)
    • when translating, translator can add or remove line-breaks in cells as needed
      • make sure your translator knows how to do new-lines in a single cell - i.e. alt+enter in excel
    • when inserting from csv, anywhere we see line break in a text cell, we replace it with handling for <BR>
  • new csv tools can also have an option to keep the current "lines" behavior rather than blocks, but we would probably now default to the block option

@LioMajor @Stefan311 what do you think about this

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

also, regarding the encoding issue, it's worth nothing that excel doesn't actually read utf-8 CSV files properly. The file encoding has to be utf-8 with BOM (utf-8-sig encoding in python) for excel to handle it properly.

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

So for games like AGLS that don't use manual line breaks at all, extracted blocks will look like
Screen Shot 2020-05-03 at 1 48 14 PM
(and you can still add your own breaks by just inserting newlines wherever needed in your translated_text column)

and for games that do use manual line breaks, extracted blocks will look like
Screen Shot 2020-05-03 at 1 47 04 PM

@pmrowla

pmrowla commented May 3, 2020 •

Copy link
Copy Markdown
Owner Author
  • csv now has block_index column
    • for --mode=blocks (default) mode, blocks can be out of order in the csv
    • for lines mode (old behavior), blocks can be out of order, but order of lines inside the a block must be ordered properly in the csv, and line count must match the lsb file line count (so basically extracted row order must be preserved, but appending can be done in any order).
  • --encoding defaults to utf-8 now, but if you are using excel you need --encoding=utf-8-sig due to MS needing BOM

also now if your translator deletes an entire row from the csv (in blocks mode) nothing bad will happen other than the original block being left untranslated in the lsb

@pmrowla

pmrowla commented May 3, 2020 •

Copy link
Copy Markdown
Owner Author

One other thing, I think it may be beneficial to have an additional "name" column for games that use images for speaker names, and the NAMELABEL event type to set speaker.

A lot of games just put "[speaker]" every 4 text lines (or however tall the message box is for that game), and it will show up in the actual text box, so it is still easy to see what you are translating.

But some games (like AGLS) use the actual NAMELABEL system event and images to display speaker name, which will not show up in text blocks.

so for

<EVENT VALUE="NAMELABEL\r\n名前_リンネ">
<TXSPN>「っっ!!!」<PG>

the spreadsheet right now just says 「っっ!!!」 and the translator has no idea who is even speaking.

So when extracting scripts to csv, we should have a column to account for this so translators can actually see who's lines they are translating. I think it's probably ok for a single JP/orig-name only column, and translator can just find/replace the entire column if they want an english or whatever else name to show up in the spreadsheet. We don't really need 2 columns here since the in game event does not get translated (since it refers to image file name).

at the api level, text blocks will just need a new namelabel attribute for this, and we keep track of and update speaker name when generating the list of blocks from a TpWord body.

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

this will break compatibility with menu CSV column count again, but I think in the future menu CSV columns might need to change anyways once the menu stuff is added to the core API for things like #38

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

@H0R1Z0N it may be helpful if you have any thoughts on this discussion as well, even though I think you've already finished translating AGLS using lns scripts. I'm sure you must have some ideas on what would have made things easier for you w/AGLS?

@H0R1Z0N

H0R1Z0N commented May 3, 2020

Copy link
Copy Markdown

Hum, this would be something very useful for someone who was doing it from scratch. Personally, i had the html version, so to resume what i did for a workflow. I used grep to search for the line from the HTML inside the extracted script to know which file had it. Then, i copied the HTML script to a spreadsheet, copied the column to notepad++ in wine, did some automatic replacement so i could simply copy paste a large chunck manually back inside the script and used diffchecker online to verify stuff and used grep 100 time a day. Finally, the mapping command to extract the order was not very practical due to how badly the HTML file was formatted.

For the name label stuff, you need to dump the .gal inside the picture folder which's the path it use in those declaration. Dump them to png, edit them, save, open in mspaint (classic one of W7), save to .bmp (Any PNG to BMP online i used kinda failed, you need to reopen those BMP in mspaint and resave those bmp with mspaint. Then use the og tool from livemaker to dump them back to .gal, when done, you recreate the folder structure of where to .gal was, use lmpatch to declare each .gal you edited.

Am i missing any other question ? I was kinda finishing the hexedit of the menu.

There's a lot of stuff that the lmlsb edit command did not support. So i had to hexedit the stuff and it was a bit of a pain until i reread from A to Z the convo about that one. Now it work. I am using hex to shift-jis or shift-jis to hex to have the jp text back in hexa, then use binwalk -r "\xFF\xFF\xFF\xFF" * where FF's your value in hex to find which file contain what in the menu. Finally, i got a discord bot to ocr the screenshot i take from the game so i can know what the title menu's text was in plain text to be able to convert to hex.

@H0R1Z0N

H0R1Z0N commented May 3, 2020

Copy link
Copy Markdown

I finally need to mention that some folder in the game dump are sometime prototype text in a poetic way that the author was making before doing the whole game to get an idea of the direction. Mine was the whole folder 00000001 or something.

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

as far as stuff you still had to hexedit, it’d be good to throw a list in the lmlsb edit discussion issue

And yeah there’s already #36 for supporting png to gal conversion in pylm

@LioMajor

LioMajor commented May 3, 2020 •

Copy link
Copy Markdown

#37 (comment)

This sounds very nice.

image
After extractcsv it's properly set to UTF-8 and i can use both ways in non japanese locale.

I have tested it and confirmed in lns:

image
Replacing multiple row works

image
To make two rows into one works

image
To make a single row into two works

Validated lsb, all scripts passed. Awesome work pmrowla!

EDIT:

As for excel, it won't work like you showed in #37 (comment) for me.

Import looks like:
image

Oh well, i never used excel for this and i feel absolutly fine with notepad++

extractmenu doesn't work on non japanese locale:
image

@pmrowla

pmrowla commented May 3, 2020

Copy link
Copy Markdown
Owner Author

To get it to work with excel you have to specify --encoding=utf-8-sig for both extract and insert CSV commands, since MS requires the CSV to have the utf-8 byte order mark at the top of the file.

This was referenced May 4, 2020
@pmrowla

pmrowla commented May 4, 2020

Copy link
Copy Markdown
Owner Author

With regard to the CSV column format, any discussion about it can go here now: #42

Stefan311 and others added 11 commits May 4, 2020 11:21
The export/import file format is similar/compatible to the menu translation format.
Fixes "Stuck Game" bug. I still wonder, how this bug passed the function test.
* TpWord now has `get_lines()` and `replace_lines()` methods
* rename commands to extractcsv/insertcsv
* use new api funcs
* change column format to be more consistent with other pylivemaker
  commands
* block_index column added
* add option for blocks or lines mode
@pmrowla
pmrowla merged commit c5d2bc1 into master May 4, 2020
@pmrowla
pmrowla deleted the lns-lines-api branch May 4, 2020 02:33
@pmrowla

pmrowla commented May 4, 2020

Copy link
Copy Markdown
Owner Author

This is now available in master (with the old 5-column format). There will be a single pylm release (0.3.2) that will support this column format, so anyone already using that format for menus can continue translating their project.

The next release (1.0) will break backwards compatibility for CSVs, so anyone not using the current CSV format yet, should probably wait for that release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants