• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
samgj

Open a File as a String in Physics FS

9 posts in this topic

I want to have my program open a Python script as a string so it can be used by Py_RunSimpleString().

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 if(PHYSFS_exists(pyname)) //if the script exists
 {
  pyname = PHYSFS_openRead(pyname); //opens the script

  PyRun_SimpleString( pyname ); //run the requested script. This is a temporary version, the final version will have low level embedding

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}

But PHYSFS_openRead() only returns data of type PhyFS_file. I can't find a Physics FS function that opens a file as a string. So, how would I get the file contents as a string.

0

Share this post


Link to post
Share on other sites
If you're trying to load scripts from a zip archive you've got a potentially much simpler approach: Python has the ability to read scripts from zip files built-in. Just add the zip file to sys.path and import will automatically find scripts in the zip file.
0

Share this post


Link to post
Share on other sites

I changed it now and it compiles. I didn't want to use sys.path because I want to be able to open LZMA2 files because they are more compressed than ZIP. I also want it to support mods and load directories in the same way because that would make development easier.

 

Here's the new code

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
 char scriptbuffer[ PHYSFS_fileLength(pyfile) ]; //Create the string the script will be stored in

 if(PHYSFS_exists(pyname)) //if the script exists
 {
  PHYSFS_read( pyfile, scriptbuffer, strlen(scriptbuffer), strlen(scriptbuffer)); //opens the script

  PyRun_SimpleString( scriptbuffer ); //run the requested script. This is a temporary version, the final version will have low level embedding

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}
0

Share this post


Link to post
Share on other sites

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea. strlen() works by looking for the first null. If you haven't initialized your buffer, it may not find a null there and try to read past your buffer, or it might find a null early and report your buffer is much smaller than it actually is.

0

Share this post


Link to post
Share on other sites

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

 

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read. 

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
  PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
  char* scriptbuffer = new char[fileSize];
  PHYSFS_read( pyfile, scriptbuffer, fileSize, 1);
  PHYSFS_close(pyfile);

  PyRun_SimpleString( scriptbuffer );
  delete[] scriptbuffer;
  return true;
}
return false;
Edited by Promit
0

Share this post


Link to post
Share on other sites

First, you can't set an array's size like that.

That actually depends on the programming language. It's legal C99, which introduced variable length arrays.
0

Share this post


Link to post
Share on other sites

 

First, you can't set an array's size like that.

That actually depends on the programming language. It's legal C99, which introduced variable length arrays.

 

It's a bad assumption that he's using C99 in the first place, and even if he were it simply shifts things over to the "bad idea" side.

0

Share this post


Link to post
Share on other sites

 

So you actually need something like:

Thanks, the with various versions of the function I tried when a script was runa script, I either got no output or, Python gave me an error like this:

  File "<string>", line 5
    Yr6S\s?bx?
     ^
SyntaxError: invalid syntax

I now have this:

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 PHYSFS_File* pyfile = PHYSFS_openRead(pyname);

 if(pyfile != NULL)
 {
  PHYSFS_sint64 filesize = PHYSFS_fileLength(pyfile);
  char* scriptbuffer = new char[ filesize ]; //Create the string the script will be stored in

  PHYSFS_read( pyfile, scriptbuffer, 1, filesize ); //opens the script

  PyRun_SimpleString( scriptbuffer ); //run the requested script. This is a temporary version, the final version will have low level embedding

  delete[] scriptbuffer;

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}

This new version works. Promit, you had the last two arguments of PHYSFS_read() wrong. It should be size in bytes of each object then number of objects to read.

1

Share this post


Link to post
Share on other sites

When one of the last two parameters is 1 the result is pretty much the same except by the return value (i.e. it'll return the same number as the last argument if it managed to read it all). Which reminds me, you seriously should do some error checking there, don't forget that reading can fail too.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0