Book Image

PlayStation Mobile Development Cookbook

By : Michael Fleischauer
Book Image

PlayStation Mobile Development Cookbook

By: Michael Fleischauer

Overview of this book

With the PlayStation®Mobile SDK you can create stunning games for the PlayStation®Vita and PlayStation™Certified devices (PS Certified devices). It includes everything you need to get started, including an IDE for developing your code and even an emulator to test your creations. "PlayStation®Mobile Development Cookbook"| is an exciting and practical collection of recipes that help you make the most of this exciting new platform. It provides you with everything you need to create complete 2D or 3D games and applications that fully unlock the potential of the SDK. After quickly covering the basics, you'll learn how to utilize input sources like touch, gamepads, and motion controls, and then move on to more advanced content like creating and animating 2D graphics, networking, playing sound effects and music, adding physics, and then finally jumping into the world of 3D.
Table of Contents (17 chapters)
PlayStationMobile Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Publishing Your Application
Index

Working with the filesystem


This recipe illustrates the various ways you can access the filesystem.

Getting ready

The complete code for this example is available in Ch1_Example5. This example adds the following two new using statements:

  • using System.Linq;

  • using System.IO;

How to do it...

Create a new solution and enter the following code replacing the Main() function:

public static void Main (string[] args){
 const string DATA = "This is some data";
 
 byte[] persistentData = PersistentMemory.Read();
 
 byte[] restoredData = persistentData.Take(DATA.Length * 2).ToArray();
 string restoredString = System.Text.Encoding.Unicode.GetString(restoredData);
 
 byte[] stringAsBytes = System.Text.Encoding.Unicode.GetBytes(DATA);
 
 for(int i = 0; i < stringAsBytes.Length; i++)
  persistentData[i] = stringAsBytes[i];
 PersistentMemory.Write (persistentData);
 
 using(FileStream fs = File.Open("/Documents/Demo.txt",FileMode.OpenOrCreate)){
  if(fs.Length == 0)
   fs.Write(stringAsBytes,0,stringAsBytes.Length);
  else{
   byte[] fileContents = new byte[fs.Length];
   fs.Read(fileContents,0,(int)fs.Length);
  }
   
 }
}

How it works...

The first part of this sample demonstrates using the PersistentMemory class. It is a statically available class, so you cannot allocate one. Instead you access it using PersistentMemory.Read(). This returns a byte array that you can now modify. We read some data using the LINQ extension Take() method. We multiplied the size by 2 because the string is stored as UNICODE, which requires 2 bytes per character. We then convert those bytes into a Unicode string. Next, we convert our DATA string into a byte array, then write it to persistentData byte by byte in a for loop. Finally we commit our changes to persistentMemory by calling PersistentMemory.Write(), passing in our updated persisitentMemory byte array.

The next part of the recipe demonstrates traditional file access using the standard .NET libraries, in this case File and FileStream. We open a text file in the /Documents folder named Demo.txt, creating it if it doesn't exist. If the file doesn't exist, we write our byte array stringAsBytes to the file. Otherwise, we read the file contents into a new byte array fileContents.

The first time you run this example, PersistentMemory.Read() will contain gibberish, as nothing has been written to it yet. On the second run, it will start with the bytes composing your DATA string.

There's more...

Persistent storage is limited to 64 KB in space. It is meant for quickly storing information, such as configuration settings. It is physically stored on the filesystem in the /save directory in a file named pm.dat.

There are three primary locations for storing files:

  • /Application: This is where your application files reside, as well as files added with the "Content" build action. These files are read only.

  • /Documents: This is where you put files that require read and write access.

  • /Temp: This is where you put temporary files, that can be read and written. These files will be erased when your program exits.

The PlayStation Mobile SDK limits directory structures to at most 5 levels deep, including the filename. Therefore, /documents/1/2/3/myfile.txt is permitted, but /documents/1/2/3/4/myfile.txt is not.

Note

The PersistentMemory classes was deprecated in the 1.0 release of PSM SDK and should no longer be used. Use traditional .NET file methods instead.

See also

  • See the Loading, displaying, and translating a textured image recipe for details about how to add files to your app's folders using build actions