How to input very long strings into the code efficiently and securely?

Started by The Electrician, Sat 03/07/2010 03:37:38

Previous topic - Next topic

The Electrician

I've been using AGS for a few months now, and I can mostly make it jump how I tell it to (I have some programming background). However I am not sure how to properly handle very long constant (read-only) strings. Let me give you 2 instances of where I need to do this, though you might think of others:
a) I have a rather large maze-like area, all using one AGS room, where I use some dynamic drawing and turn walkable areas on and off, etc. to give the illusion of multiple rooms. So far so good: each virtual room needs to have several parameters set (location of doors and walls, connectivity with other rooms, misc objects, etc.) and these are described in one .txt file, each line describing one virtual room. The code then reads the lines into a string of arrays at the beginning of the game, and parses the information for a particular room during its load function.
b) The player finds a book which contains several pages of text. When the book is opened, its closeup appears, and the page's text is rendered in a funky font, with the possibility of flipping pages with the hand icon. The totality of the book's text is stored in one .txt, with separator symbols between pages.

In both situations, you need a very large block of text, which is not easy to place between quotes (the editor doesn't even wrap lines). Loading .txt's is fine, except should I finish the game and deliver it to the public, I don't want the user to look at, or mess with, these files. Copying the whole text into a string by hand is cumbersome for editing later on, particularly for editing my maze map.

To summarise, I have one of two problems:
i) The large strings are placed directly in the code, which makes them cumbersome to edit.
ii) They are placed in a .txt file, where the user has unfettered access to them.

So I want to hear how others have handled/would handle this.

This leads me to the following questions/ideas:
1) Can you import a .txt as a resource into AGS?
2) Can you write long strings in the AGS editor in a multiline fashion, or at least make the editor wrap lines?
3) In the end, I might just develop the game with the .txt's, and copy everything manually into strings at the very end, just before making it public. This is bad programming practice however, and ideally such large resources should be in some way separate, just like the .vox files are.
4) Alternatively, I might write a simple home-brewed encryption algorithm to change the text into something unreadable, with some checksum so that it cannot be easily messed with, and have the game read and decrypt the file. (After all, if someone is very ambitious, they might as well try to decompile the game code itself.) This feels a little overkill, though.

Discuss.

GarageGothic

I'd do this one of two ways - I'm personally using the first one for the virtual websites in my game, but it may not be the best solution.

a) Like you state in no. 3, but with automated code generation. Keep everything in external txt files until you are ready for release, or at least so late into beta testing that you're sure the text won't need further edits except very minor ones. Then write a small AGS script to turn the raw text file into lines of code (here you can conveniently also pre-linebreak/pagebreak the thing into an array), output them to another txt and copy-paste into the global/room script depending on use.

b) As no. 4, which isn't really overkill, nor very hard to script - but to make it easier you could use the EncryptedFile, though the link is currently down. I don't think it has any built-in checksum function.

Re: 1) Not store the txt in the game .exe or data files if that's what you mean. You can access an external .txt file in the game's folder with File.Open, but you seem to be aware of that.

Re: 2) You *can* use a linebreak within a line of script, but it will also mean a linebreak in the displayed text unless you remove it from the String before displaying. I think there's still a character limit per line of code, but I'm not sure what it is - possibly 500 chars, I seem to remember that number from somewhere.

If you can't find a working link to the EncryptedFile module, here's a very simple script that worked last time I checked. No checksum here either though:

Code: ags
String SimpleEncrypt(this String*, String key) {
  String encryptedtext = "";
  int stringcounter;
  int keycounter;
  int stringlength = this.Length;
  int keylength = key.Length;
  while (stringcounter < stringlength) {
    if (keycounter > keylength) keycounter = keycounter-keylength;
    encryptedtext = encryptedtext.AppendChar(this.Chars[stringcounter]^key.Chars[keycounter]);
    stringcounter++;
    keycounter++;
    }
  return encryptedtext;
  }


Edit: That script works both for encrypting/decrypting, btw - not very safe but good enough for ordinary users snooping around the game's folder.

The Electrician

Thanks GarageGothic, that's a very good (and quick!) reply.

I guess you got all my points, except one.
Quote from: GarageGothic on Sat 03/07/2010 03:56:09
Re: 2) You *can* use a linebreak within a line of script, but it will also mean a linebreak in the displayed text unless you remove it from the String before displaying.

I don't want a linebreak in the game output (using '[', if I remember correctly), but rather in the code editor, so I don't need to horizontally scroll through a very long text. Anyway, it seems there's a rather small char limit on constant strings, so the issue is irrelevant.

I like both your solutions, and will probably implement one of them when/if I finish the game.

To everyone: any other thoughts? I am curious if this an issue many people have? What other uses do you see for very long strings? Should AGS have internal text folders where you can dump huge pieces of text? (As an afterthought, perhaps I can just (mis)use the dialog editor for that. Thinking...)

GarageGothic

Quote from: The Electrician on Sat 03/07/2010 05:41:36I guess you got all my points, except one. I don't want a linebreak in the game output (using '[', if I remember correctly), but rather in the code editor, so I don't need to horizontally scroll through a very long text. Anyway, it seems there's a rather small char limit on constant strings, so the issue is irrelevant.

Actually, yes, I got what you meant there too. Sorry if I was unclear - what I meant is that yes, you can press "Enter" in the middle of a line of script and AGS will still parse it correctly till the ";" at the end. However, if you do it in the middle of a String, doing so will also add a linebreak to that String which will show up when its displayed - hence why I mentioned removing linebreaks manually (using String formatting functions) before displaying.

The Electrician

Thanks GarageGothic - ok, I get your point now and it was me that misunderstood. Yes, that might be a good solution too, particularly for my first example (the maze map). I guess I will find out what the char limit really is for long strings.

SMF spam blocked by CleanTalk