You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
6.7 KiB
283 lines
6.7 KiB
11 years ago
|
using UnityEngine;
|
||
11 years ago
|
using System;
|
||
11 years ago
|
using System.Collections;
|
||
11 years ago
|
using System.Collections.Generic;
|
||
11 years ago
|
using System.Text.RegularExpressions;
|
||
11 years ago
|
using System.Runtime.Serialization.Formatters.Binary;
|
||
|
using System.IO;
|
||
11 years ago
|
|
||
10 years ago
|
namespace Fungus.Script
|
||
11 years ago
|
{
|
||
|
/**
|
||
11 years ago
|
* Static data storage class for managing global game variables.
|
||
|
* Provides save and load functionality for persistent storage between game sessions.
|
||
11 years ago
|
*/
|
||
10 years ago
|
public class GlobalVariables
|
||
11 years ago
|
{
|
||
11 years ago
|
static Dictionary<string, string> stringDict = new Dictionary<string, string>();
|
||
|
static Dictionary<string, int> intDict = new Dictionary<string, int>();
|
||
|
static Dictionary<string, float> floatDict = new Dictionary<string, float>();
|
||
|
static Dictionary<string, bool> boolDict = new Dictionary<string, bool>();
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Save the variable dictionaries to persistent storage using a name tag.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static void Save(string saveName)
|
||
11 years ago
|
{
|
||
11 years ago
|
// Save strings
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream();
|
||
|
b.Serialize(m, stringDict);
|
||
|
PlayerPrefs.SetString(saveName + "." + "stringDict", Convert.ToBase64String(m.GetBuffer()));
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Save ints
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream();
|
||
|
b.Serialize(m, intDict);
|
||
|
PlayerPrefs.SetString(saveName + "." + "intDict", Convert.ToBase64String(m.GetBuffer()));
|
||
|
}
|
||
|
|
||
|
// Save floats
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream();
|
||
|
b.Serialize(m, floatDict);
|
||
|
PlayerPrefs.SetString(saveName + "." + "floatDict", Convert.ToBase64String(m.GetBuffer()));
|
||
|
}
|
||
|
|
||
|
// Save bools
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream();
|
||
|
b.Serialize(m, boolDict);
|
||
|
PlayerPrefs.SetString(saveName + "." + "boolDict", Convert.ToBase64String(m.GetBuffer()));
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
PlayerPrefs.Save();
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Loads the variable dictionaries from persistent storage using a name tag.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static void Load(string saveName)
|
||
11 years ago
|
{
|
||
11 years ago
|
var stringData = PlayerPrefs.GetString(saveName + "." + "stringDict");
|
||
|
if (string.IsNullOrEmpty(stringData))
|
||
|
{
|
||
|
stringDict.Clear();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream(Convert.FromBase64String(stringData));
|
||
|
stringDict = (Dictionary<string, string>)b.Deserialize(m);
|
||
|
}
|
||
|
|
||
|
var floatData = PlayerPrefs.GetString(saveName + "." + "floatDict");
|
||
|
if (!string.IsNullOrEmpty(floatData))
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream(Convert.FromBase64String(floatData));
|
||
|
floatDict = b.Deserialize(m) as Dictionary<string, float>;
|
||
|
}
|
||
|
|
||
|
var intData = PlayerPrefs.GetString(saveName + "." + "intDict");
|
||
|
if (!string.IsNullOrEmpty(intData))
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream(Convert.FromBase64String(intData));
|
||
|
intDict = b.Deserialize(m) as Dictionary<string, int>;
|
||
|
}
|
||
|
|
||
|
var boolData = PlayerPrefs.GetString(saveName + "." + "boolDict");
|
||
|
if (!string.IsNullOrEmpty(boolData))
|
||
|
{
|
||
|
var b = new BinaryFormatter();
|
||
|
var m = new MemoryStream(Convert.FromBase64String(boolData));
|
||
|
boolDict = b.Deserialize(m) as Dictionary<string, bool>;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Clears all stored variables.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static void ClearAll()
|
||
11 years ago
|
{
|
||
11 years ago
|
stringDict.Clear();
|
||
|
intDict.Clear();
|
||
|
floatDict.Clear();
|
||
|
boolDict.Clear();
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the float variable associated with the key.
|
||
|
*/
|
||
11 years ago
|
public static float GetFloat(string key)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (String.IsNullOrEmpty(key) ||
|
||
|
!floatDict.ContainsKey(key))
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
return floatDict[key];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the integer variable associated with the key.
|
||
|
*/
|
||
11 years ago
|
public static int GetInteger(string key)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (intDict == null)
|
||
|
{
|
||
|
Debug.Log ("Dict is null somehow");
|
||
|
}
|
||
|
|
||
|
if (String.IsNullOrEmpty(key) ||
|
||
|
!intDict.ContainsKey(key))
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
return intDict[key];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the boolean variable associated with the key.
|
||
|
*/
|
||
11 years ago
|
public static bool GetBoolean(string key)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (String.IsNullOrEmpty(key) ||
|
||
|
!boolDict.ContainsKey(key))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return boolDict[key];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the string variable associated with the key.
|
||
|
*/
|
||
11 years ago
|
public static string GetString(string key)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (String.IsNullOrEmpty(key) ||
|
||
|
!stringDict.ContainsKey(key))
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
return stringDict[key];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Stores a float variable using the key.
|
||
|
*/
|
||
11 years ago
|
public static void SetFloat(string key, float value)
|
||
11 years ago
|
{
|
||
10 years ago
|
if (stringDict.ContainsKey(key) ||
|
||
|
intDict.ContainsKey(key) ||
|
||
|
boolDict.ContainsKey(key))
|
||
|
{
|
||
|
Debug.LogError("Key already in use with a string, integer or boolean variable");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
floatDict[key] = value;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Stores an integer variable using the key.
|
||
|
*/
|
||
11 years ago
|
public static void SetInteger(string key, int value)
|
||
11 years ago
|
{
|
||
10 years ago
|
if (stringDict.ContainsKey(key) ||
|
||
|
floatDict.ContainsKey(key) ||
|
||
|
boolDict.ContainsKey(key))
|
||
|
{
|
||
|
Debug.LogError("Key already in use with a string, float or boolean variable");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
intDict[key] = value;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Stores a boolean variable using the key.
|
||
|
*/
|
||
11 years ago
|
public static void SetBoolean(string key, bool value)
|
||
11 years ago
|
{
|
||
10 years ago
|
if (stringDict.ContainsKey(key) ||
|
||
|
floatDict.ContainsKey(key) ||
|
||
|
intDict.ContainsKey(key))
|
||
|
{
|
||
|
Debug.LogError("Key already in use with a string, float or integer variable");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
boolDict[key] = value;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Stores a string variable using the key.
|
||
|
*/
|
||
11 years ago
|
public static void SetString(string key, string value)
|
||
11 years ago
|
{
|
||
10 years ago
|
if (boolDict.ContainsKey(key) ||
|
||
|
floatDict.ContainsKey(key) ||
|
||
|
intDict.ContainsKey(key))
|
||
|
{
|
||
|
Debug.LogError("Key already in use with a boolean, float or integer variable");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
stringDict[key] = value;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Replace keys in the input string with the string table entry.
|
||
|
* Example format: "This {string_key} string"
|
||
|
*/
|
||
11 years ago
|
public static string SubstituteStrings(string text)
|
||
11 years ago
|
{
|
||
|
string subbedText = text;
|
||
|
|
||
|
// Instantiate the regular expression object.
|
||
|
Regex r = new Regex("{.*?}");
|
||
|
|
||
|
// Match the regular expression pattern against a text string.
|
||
|
var results = r.Matches(text);
|
||
|
foreach (Match match in results)
|
||
|
{
|
||
|
string stringKey = match.Value.Substring(1, match.Value.Length - 2);
|
||
|
string stringValue = GetString(stringKey);
|
||
|
|
||
|
subbedText = subbedText.Replace(match.Value, stringValue);
|
||
|
}
|
||
|
|
||
|
return subbedText;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Chops a string at the first new line character encountered.
|
||
|
* This is useful for link / button strings that must fit on a single line.
|
||
|
*/
|
||
11 years ago
|
public static string FormatLinkText(string text)
|
||
11 years ago
|
{
|
||
|
string trimmed;
|
||
|
if (text.Contains("\n"))
|
||
|
{
|
||
|
trimmed = text.Substring(0, text.IndexOf("\n"));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
trimmed = text;
|
||
|
}
|
||
|
|
||
|
return trimmed;
|
||
|
}
|
||
|
}
|
||
|
}
|