Jump to content

How to preserve WeiDU variable set inside INNER_ACTION for use outside?


agb1

Recommended Posts

Seems my problem is more subtle. After further testing, I do see that variables defined/changed inside the INNER_ACTION are available outside.

 

However, I'm trying to use a REPLACE_EVALUATE inside of a COPY inside of an INNER_ACTION to do a regexp pattern match and then save %MATCH1% in a variable for use outside.

 

It seems any variable SET inside of that REPLACE_EVALUATE is not available outside of the REPLACE_EVALUATE, even within that inner COPY.

 

But I can use the same REPLACE_EVALUATED code outside of the INNER_ACTION (in the outer COPY), and there the variables set inside the REPLACE are still available outside.

 

In other words:

 

COPY - ~a~ ~b~ // inlined fake copy for reading
  REPLACE_EVALUATE ~pattern~ BEGIN
    SET foo = 1
  END
  PATCH_PRINT ~foo = %foo%~ // will print foo = 1 (variable setting inside REPLACE_EVALUATE is available outside)
  INNER_ACTION
    COPY - ~c~ ~d~ // inlined fake copy for reading
      REPLACE_EVALUATE ~same pattern~ BEGIN
        SET bar = 1
      END
      PATCH_PRINT ~bar = %bar%~ // will print bar = %bar% (variable setting inside REPLACE_EVALUATE is not available outside)
      SET orange = 2
    BUT_ONLY
    OUTER_SET pear = 3
  END
  PATCH_PRINT ~orange = %orange%~ // will print orange = 2 (variable setting inside COPY inside INNER_ACTION is available outside)
  PATCH_PRINT ~pear = %pear%~ // will print pear = 3 (variable setting inside INNER_ACTION is available outside)
Link to comment

Are you sure you really get a match within inner COPY?

 

That is, do you get a print if you move PATCH_PRINT ~bar = %bar%~ inside of REPLACE_EVALUATE? I've shot myself a lot of times assuming I was getting a match without it actually happening.

Link to comment

Yes, I tried what you just suggested, and I do get matches in the REPLACE_EVALUATE inside the inner COPY.

 

I've now worked around the problem by implementing a line reader loop, so I'm not blocked by this, but it was quite weird.

 

Edit: Actually, on closer look, it seems that the REPLACE_EVALUATE inside the inner COPY is still matching against the outer COPY. The contents of %MATCH1% correspond to the outer file, not the inner file. Huh...

Link to comment

If you are not making any changes to your copied file you can also try INNER_PATCH_FILE i.e.

 

INNER_PATCH_FILE "myfile.file" BEGIN
// do some stuff
END

 

I cannot answer right now whether you lose your default variables in INNER_PATCH_FILE like you do with INNER_ACTION.

Link to comment

I tried to use INNER_PATCH_FILE but it couldn't "see" the "inner" file that I had copied, even though the WeiDU.log indicated that it was placed in the override folder. I didn't spend much time trying that route.

 

Edit: To clarify, this code is reading files that aren't in the override folder. Specifically, I'm reading VERSION strings from tp2 files in mod folders.

 

In the end the code that worked is like this:

 

COPY - ~firstfile~ ~inlined~
	INNER_ACTION BEGIN
		COPY - ~secondfile~ ~inlined2~ // fake copy
			INNER_PATCH ~%LNL%%WNL%~ BEGIN // order matters (WNL is two characters, CRLF, LNL is just LF .. arguably could just use WNL here)
				READ_BYTE 0x0 lnl // get Linux new line byte representation
				READ_BYTE 0x1 wnl // get Windows new line byte representation
			END
			SET filesize = SIZE_OF_FILE ~secondfile~
			FOR (i = 0; i < filesize; ++i) BEGIN
				SET eol = 0
				FOR (j = i; !eol && j < filesize; ++j) BEGIN // search for next newline
					READ_BYTE j byte
					PATCH_IF (byte == lnl OR byte == wnl) BEGIN SET eol = 1 END // found end of line - break out of j loop
				END
				SET len = (j - i - 1) // j is after newline; i is at the start of the line -- if the first line is 3 chars long, j is 3 (4th byte), i is 0
				READ_ASCII i line ( len )
				PATCH_IF (~%line%~ STRING_CONTAINS_REGEXP ~~~~~pattern~~~~~ == 0) BEGIN
					TEXT_SPRINT linetokeep ~%line%~
				END
				WHILE (eol AND j < filesize) BEGIN // check for more than one newline at end of line
					READ_BYTE j byte
					PATCH_IF (byte == lnl OR byte == wnl) BEGIN SET j = j + 1 END // skip another newline
					ELSE BEGIN SET eol = 0 END // else done
				END
				SET i = j - 1 // continue to next line, skipping newline(s)
			END
		BUT_ONLY
	END
Horrible, isn't it?
Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...