3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Save2File - A Handy File Save System  (Read 673 times)

« on: October 20, 2013, 01:43:54 PM »
A little something something I created last week. I figured I might as well share it.

I'm a big fan of Jestermon's Global Hash Pool, but I don't like the fact that it's based on the global strings, so I thought it might be informative to go ahead and try to write my own file save/open system.

So I present to you: 'Save2File', a handy system to save and load your variables to a file.

Manual:
1. Use 'S2FLoadFile()' to load a file into the project.
2. Use the 'S2F...Get()' functions to read your data from the 'cloud'.
3. Use the 'S2F...Set()' functions to save your data to the 'cloud'.
4. Finally, use 'S2FSaveFile()' to save the 'cloud' to a file.

Code: [Select]
///==================================================================
///Save 2 File - A handy data save/load system
///==================================================================
string[] buffer(0);

///String Splitter
string[] S2FStringSplitter(string input, string key)
{
   string[] strList(0);
   string start, end, temp = input;
   int i = 0, pos = 0;
   while (true)
   {
      pos = iStringFind(temp,key,0,true);

      if (pos != -1)
      {
         strList.resize(i+1);

         iStringLeft(start,temp,pos-1);
         iStringRight(end,temp,iStringLen(temp)-pos);

         temp = end;
         strList[i] = start;

         i ++; if (i > 1000) break;
      }
      else
      {
         strList.resize(i+1);
         strList[i] = end;
         break;
      }
   }
   return strList;
}

///Get Line Number                                       
int S2FGetID(string key, bool add)
{
   int l = iStringLen(key);
   if (buffer.length() == 0)
   {
      buffer.resize(1);
      return 0;
   }
   for (int i=0; i<buffer.length(); i++)
   {
      string temp;
      iStringLeft(temp,buffer[i],l+1);
      if (temp == key+";") return i;
      else if (i == buffer.length()-1 && add)
      {
         buffer.resize(i+2);
         return i+1;
      }
   }
   return -1;
}

///Load File                                             
bool S2FLoadFile(string path)
{
   int file = iFileReadOpen(path);
   buffer.resize(1000);
   if (file > -1)
   {
      int i = 0;
      string data = " ";
      while (data != "")
      {
         iFileStringRead(file,data);
         if (data != "")
         {
            buffer[i] = data;
            i ++;
         }
      }
      buffer.resize(i);

      iFileClose(file);
   }
   else
   {
      buffer.resize(0);
      return false;
   }
   return true;
}

///Save File                                             
bool S2FSaveFile(string path)
{
   int file = iFileWriteOpen(path);
   if (file == -1) return false;
   else
   {
      for (int i=0; i<buffer.length(); i++)
      {
         iFileStringWrite(file,buffer[i],true);
      }
      iFileClose(file);
   }
   return true;
}

///Load Float                                           
float S2FFloatGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return -1;
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return iStringVal(list[1]);
   }
}

///Save Float                                           
void S2FFloatSet(string key, float value)
{
   int id = S2FGetID(key, true);

   ///test = id;

   if (id != -1) buffer[id] = key + ";" + value;
}

///Load String                                           
string S2FStringGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return "Error";
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return list[1];
   }
}

///Save String                                           
void S2FStringSet(string key, string value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value;
}

///Load Vector                                           
Vector3 S2FVectorGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return Vector3(-1,-1,-1);
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return Vector3(iStringVal(list[1]), iStringVal(list[2]), iStringVal(list[3]));
   }
}

///Save Vector                                           
void S2FVectorSet(string key, Vector3 value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z;
}

///Load Quaternion                                       
Quaternion S2FQuaternionGet(string key)
{
   int id = S2FGetID(key, false);
   Quaternion q; iQuaternionFromEulerAngles(q,-1,-1,-1,"xyz");
   if (id == -1) return q;
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      q.x = iStringVal(list[1]); q.y = iStringVal(list[2]); q.z = iStringVal(list[3]); q.w = iStringVal(list[4]);
      return q;
   }
}

///Save Quaternion                                       
void S2FQuaternionSet(string key, Quaternion value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z + ";" + value.w;
}
///==================================================================
///==================================================================

An example of usage is attached.

EDIT: Thought I might need to say this: I am aware my logic is very similar to Jestermon's. I have no intention of presenting her work as mine, nor neglecting her great work.

« Last Edit: October 21, 2013, 03:19:40 AM by RobertooMolvadoo »
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #1 on: October 21, 2013, 03:15:28 AM »
A little update. Here's a version of the S2F system with some encrypting! The script saves the strings to the file with a user defined offset, making the content of the file look like complete gibberish.
Usage is exactly the same as the original.

Code: [Select]
///==================================================================
///Save 2 File - A handy data save/load system
///==================================================================
int offset = 50;
string[] buffer(0);

///Encrypt/Decript
string StringOffset(string t, int n)
{
   for (int i=0; i<iStringLen(t); i++)
   {
      int x = t[i];
      x += n;
      if (x > 127) x -= 127;
      if (x < 0) x += 127;
      t[i] = x;
   }
   return t;
}

///String Splitter
string[] S2FStringSplitter(string input, string key)
{
   string[] strList(0);
   string start, end, temp = input;
   int i = 0, pos = 0;
   while (true)
   {
      pos = iStringFind(temp,key,0,true);

      if (pos != -1)
      {
         strList.resize(i+1);

         iStringLeft(start,temp,pos-1);
         iStringRight(end,temp,iStringLen(temp)-pos);

         temp = end;
         strList[i] = start;

         i ++; if (i > 1000) break;
      }
      else
      {
         strList.resize(i+1);
         strList[i] = end;
         break;
      }
   }
   return strList;
}

///Get Line Number                                       
int S2FGetID(string key, bool add)
{
   int l = iStringLen(key);
   if (buffer.length() == 0)
   {
      buffer.resize(1);
      return 0;
   }
   for (int i=0; i<buffer.length(); i++)
   {
      string temp;
      iStringLeft(temp,buffer[i],l+1);
      if (temp == key+";") return i;
      else if (i == buffer.length()-1 && add)
      {
         buffer.resize(i+2);
         return i+1;
      }
   }
   return -1;
}

///Load File                                             
bool S2FLoadFile(string path)
{
   int file = iFileReadOpen(path);
   buffer.resize(1000);
   if (file > -1)
   {
      int i = 0;
      string data = " ";
      while (data != "")
      {
         iFileStringRead(file,data);
         if (data != "")
         {
            data = StringOffset(data,-offset);
            buffer[i] = data;
            i ++;
         }
      }
      buffer.resize(i);

      iFileClose(file);
   }
   else
   {
      buffer.resize(0);
      return false;
   }
   return true;
}

///Save File                                             
bool S2FSaveFile(string path)
{
   int file = iFileWriteOpen(path);
   if (file == -1) return false;
   else
   {
      for (int i=0; i<buffer.length(); i++)
      {
         string temp = StringOffset(buffer[i],offset);
         iFileStringWrite(file,temp,true);
      }
      iFileClose(file);
   }
   return true;
}

///Load Float                                           
float S2FFloatGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return -1;
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return iStringVal(list[1]);
   }
}

///Save Float                                           
void S2FFloatSet(string key, float value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value;
}

///Load String                                           
string S2FStringGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return "Error";
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return list[1];
   }
}

///Save String                                           
void S2FStringSet(string key, string value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value;
}

///Load Vector                                           
Vector3 S2FVectorGet(string key)
{
   int id = S2FGetID(key, false);
   if (id == -1) return Vector3(-1,-1,-1);
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      return Vector3(iStringVal(list[1]), iStringVal(list[2]), iStringVal(list[3]));
   }
}

///Save Vector                                           
void S2FVectorSet(string key, Vector3 value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z;
}

///Load Quaternion                                       
Quaternion S2FQuaternionGet(string key)
{
   int id = S2FGetID(key, false);
   Quaternion q; iQuaternionFromEulerAngles(q,-1,-1,-1,"xyz");
   if (id == -1) return q;
   else
   {
      string[] list = S2FStringSplitter(buffer[id],";");
      q.x = iStringVal(list[1]); q.y = iStringVal(list[2]); q.z = iStringVal(list[3]); q.w = iStringVal(list[4]);
      return q;
   }
}

///Save Quaternion                                       
void S2FQuaternionSet(string key, Quaternion value)
{
   int id = S2FGetID(key, true);
   if (id != -1) buffer[id] = key + ";" + value.x + ";"+value.y + ";" + value.z + ";" + value.w;
}
///==================================================================
///==================================================================

Again, example of usage attached.

Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #2 on: October 21, 2013, 02:35:15 PM »
This is useful stuff Robertoo, good job!
Pages: [1]