Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Last active September 3, 2019 19:05
Show Gist options
  • Save jakevsrobots/7425247 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7425247 to your computer and use it in GitHub Desktop.
A helper script to read/write SHARECART1000 save files. See http://sharecart1000.com/ for more info!
/*************************************************
SHARECART1000 helper script:
This script reads/writes data according to the
shared savegame file specified here:
http://sharecart1000.com/
Put this script on some game object in your
scene. You can use a sample text file in the
sharecart format and assign it to the property
"testing data" in the Unity editor, for testing.
In an actual build, this script will look for
the file "dat/o_o.ini" in the parent folder of
the game's folder (see the above link for a
diagram.)
The save file is parsed in Awake(), so you'll
want to wait until Start() to access its
data.
Read and write values using static properties, ex.:
SharecartOneThousand.MapX = 255
Save your changes to disk:
SharecartOneThousand.Save()
If you want to be able to Save() in the editor,
check the "save data in editor" box on the
component in the inspector. This is disabled by
default so you don't clobber your test data unless
you really want to.
/************************************************/
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using System.Collections;
using System.IO;
public class SharecartOneThousand : MonoBehaviour
{
public TextAsset testingData;
public bool saveDataInEditor = false;
public static int MapX;
public static int MapY;
public static int Misc0;
public static int Misc1;
public static int Misc2;
public static int Misc3;
public static string PlayerName;
public static bool Switch0;
public static bool Switch1;
public static bool Switch2;
public static bool Switch3;
public static bool Switch4;
public static bool Switch5;
public static bool Switch6;
public static bool Switch7;
static string filePath;
static bool _saveDataInEditor = false;
void Awake()
{
string data;
_saveDataInEditor = saveDataInEditor;
if(Application.isEditor)
{
data = testingData.text;
#if UNITY_EDITOR
filePath = AssetDatabase.GetAssetPath(testingData);
#endif
}
else
{
if(Application.platform == RuntimePlatform.OSXPlayer)
filePath = new DirectoryInfo(Application.dataPath).Parent.Parent.Parent + "/dat/o_o.ini";
else if(Application.platform == RuntimePlatform.WindowsPlayer)
filePath = new DirectoryInfo(Application.dataPath).Parent.Parent + "/dat/o_o.ini";
data = File.ReadAllText(filePath);
}
string[] lines = data.Split('\n');
MapX = GetCartInt(lines[1]);
MapY = GetCartInt(lines[2]);
Misc0 = GetCartInt(lines[3]);
Misc1 = GetCartInt(lines[4]);
Misc2 = GetCartInt(lines[5]);
Misc3 = GetCartInt(lines[6]);
PlayerName = lines[7].Split('=')[1];
Switch0 = GetCartSwitch(lines[8]);
Switch1 = GetCartSwitch(lines[9]);
Switch2 = GetCartSwitch(lines[10]);
Switch3 = GetCartSwitch(lines[11]);
Switch4 = GetCartSwitch(lines[12]);
Switch5 = GetCartSwitch(lines[13]);
Switch6 = GetCartSwitch(lines[14]);
Switch7 = GetCartSwitch(lines[15]);
}
bool GetCartSwitch(string line)
{
if(line.Split('=')[1] == "TRUE")
return true;
else
return false;
}
int GetCartInt(string line)
{
return System.Convert.ToInt32(line.Split('=')[1]);
}
public static void Save()
{
if(Application.isEditor && !_saveDataInEditor)
return;
string output = "[Main]\n";
output += "MapX=" + MapX.ToString() + "\n";
output += "MapY=" + MapY.ToString() + "\n";
output += "Misc0=" + Misc0.ToString() + "\n";
output += "Misc1=" + Misc1.ToString() + "\n";
output += "Misc2=" + Misc2.ToString() + "\n";
output += "Misc3=" + Misc3.ToString() + "\n";
output += "PlayerName=" + PlayerName + "\n";
output += "Switch0=" + (Switch0 ? "TRUE" : "FALSE") + "\n";
output += "Switch1=" + (Switch1 ? "TRUE" : "FALSE") + "\n";
output += "Switch2=" + (Switch2 ? "TRUE" : "FALSE") + "\n";
output += "Switch3=" + (Switch3 ? "TRUE" : "FALSE") + "\n";
output += "Switch4=" + (Switch4 ? "TRUE" : "FALSE") + "\n";
output += "Switch5=" + (Switch5 ? "TRUE" : "FALSE") + "\n";
output += "Switch6=" + (Switch6 ? "TRUE" : "FALSE") + "\n";
output += "Switch7=" + (Switch7 ? "TRUE" : "FALSE");
File.WriteAllText(filePath, output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment