Game Development Community

dev|Pro Game Development Curriculum

Compile scripts with Unicode BOM header

by Fyodor -bank- Osokin · 08/27/2007 (1:45 pm) · 2 comments

We need to hack into console/consoleFunctions.cc file and modify code in two places:
in function ConsoleFunction(compile, bool, 2, 2, "compile(fileName)")
replace the whole block
Stream *s = ResourceManager->openStream(scriptFilenameBuffer);
   if(s)
   {
      scriptSize = ResourceManager->getSize(scriptFilenameBuffer);
      script = new char [scriptSize+1];
      s->read(scriptSize, script);
      ResourceManager->closeStream(s);
      script[scriptSize] = 0;
   }
with this one:
Stream *s = ResourceManager->openStream(scriptFilenameBuffer);
   if(s)
   {
      scriptSize = ResourceManager->getSize(scriptFilenameBuffer);
      unsigned char* tmp = new unsigned char[1];
      U8 skip = 0;
      s->read(1, tmp);
      if (*tmp == 0xEF) // Unicode BOM UTF-8 checking (the actual Torque-safe format) // 0xEFBBBF
         skip = 3;
      else if (*tmp == 0xFF || *tmp == 0xFE ) // Unicode BOM BE/LE checking, just in case // 0xFEFF/0xFFFE
         skip = 2;
      delete [] tmp;
      if (skip!=0)
         Con::warnf("Found Unicode BOM header, skipping %d chars...", skip);
      script = new char [scriptSize-skip+1]; // we need reduce the size by our skipped chars
      s->setPosition(skip); // we need to reset to beginning of stream, or just skip a few bytes
      s->read(scriptSize, script);
      ResourceManager->closeStream(s);
      script[scriptSize-skip] = 0; // we need reduce the size by our skipped chars
   }

The similar should be done in ConsoleFunction(exec, bool, 2, 4, "exec(fileName [, nocalls [,journalScript]])") function too:
replace block
if(s)
      {
         scriptSize = ResourceManager->getSize(scriptFileName);
         script = new char [scriptSize+1];
         s->read(scriptSize, script);

         if(journal && Game->isJournalWriting())
         {
            Game->journalWrite(scriptSize);
            Game->journalWrite(scriptSize, script);
         }
         ResourceManager->closeStream(s);
         script[scriptSize] = 0;
      }
with this one:
if(s)
      {
         scriptSize = ResourceManager->getSize(scriptFileName);
         unsigned char* tmp = new unsigned char[1];
         U8 skip = 0;
         s->read(1, tmp);
         if (*tmp == 0xEF) // Unicode BOM UTF-8 checking (the actual Torque-safe format) // 0xEFBBBF
            skip = 3;
         else if (*tmp == 0xFF || *tmp == 0xFE ) // Unicode BOM BE/LE checking, just in case // 0xFEFF/0xFFFE
            skip = 2;
         delete [] tmp;
         if (skip!=0)
            Con::warnf("Found Unicode BOM header, skipping %d chars...", skip);
         script = new char [scriptSize-skip+1];  // we need to reduce the size by our skipped chars
         s->setPosition(skip); // we need to reset to beginning of stream, or just skip a few bytes
         s->read(scriptSize, script);

         if(journal && Game->isJournalWriting())
         {
            Game->journalWrite(scriptSize);
            Game->journalWrite(scriptSize, script);
         }
         ResourceManager->closeStream(s);
         script[scriptSize-skip] = 0; // we need to reduce the size by our skipped chars
      }

This is based on TGE150, but should suit any torque build that has as base TGE1.4++ (latest TGEA/TGB).

Now, if you modify your scripts by any text editor, and in case it will save Unicode BOM, you don't need to care about it. Your Torque now can handle that!

#1
04/18/2008 (8:24 pm)
Great for those who have to work with Unicode and different languages :) Now I don't have to scream at people to save their files in UTF-8 without DOM :P
#2
09/15/2008 (6:09 am)
but how about people have indie only? I can't access console/consoleFunctions.cc