using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using BayatGames.SaveGameFree.Encoders; using BayatGames.SaveGameFree.Serializers; using UnityEngine; using UnityEngine.Networking; namespace BayatGames.SaveGameFree { /// /// Save Game Web. /// Use these APIs to Save & Load from web. /// public class SaveGameWeb { private static string m_DefaultUsername = "savegamefree"; private static string m_DefaultPassword = "$@ve#game%free"; private static string m_DefaultURL = "http://www.example.com"; private static bool m_DefaultEncode = false; private static string m_DefaultEncodePassword = "h@e#ll$o%^"; private static ISaveGameSerializer m_DefaultSerializer = new SaveGameJsonSerializer(); private static ISaveGameEncoder m_DefaultEncoder = new SaveGameSimpleEncoder(); private static Encoding m_DefaultEncoding = Encoding.UTF8; /// /// Gets or sets the default username. /// /// The default username. public static string DefaultUsername { get { return m_DefaultUsername; } set { m_DefaultUsername = value; } } /// /// Gets or sets the default password. /// /// The default password. public static string DefaultPassword { get { return m_DefaultPassword; } set { m_DefaultPassword = value; } } /// /// Gets or sets the default UR. /// /// The default UR. public static string DefaultURL { get { return m_DefaultURL; } set { m_DefaultURL = value; } } /// /// Gets or sets a value indicating whether this default encode. /// /// true if default encode; otherwise, false. public static bool DefaultEncode { get { return m_DefaultEncode; } set { m_DefaultEncode = value; } } /// /// Gets or sets the default encode password. /// /// The default encode password. public static string DefaultEncodePassword { get { return m_DefaultEncodePassword; } set { m_DefaultEncodePassword = value; } } /// /// Gets or sets the default serializer. /// /// The default serializer. public static ISaveGameSerializer DefaultSerializer { get { if (m_DefaultSerializer == null) { m_DefaultSerializer = new SaveGameJsonSerializer(); } return m_DefaultSerializer; } set { m_DefaultSerializer = value; } } /// /// Gets or sets the default encoder. /// /// The default encoder. public static ISaveGameEncoder DefaultEncoder { get { if (m_DefaultEncoder == null) { m_DefaultEncoder = new SaveGameSimpleEncoder(); } return m_DefaultEncoder; } set { m_DefaultEncoder = value; } } /// /// Gets or sets the default encoding. /// /// The default encoding. public static Encoding DefaultEncoding { get { if (m_DefaultEncoding == null) { m_DefaultEncoding = Encoding.UTF8; } return m_DefaultEncoding; } set { m_DefaultEncoding = value; } } protected string m_Username; protected string m_Password; protected string m_URL; protected bool m_Encode; protected string m_EncodePassword; protected ISaveGameSerializer m_Serializer; protected ISaveGameEncoder m_Encoder; protected Encoding m_Encoding; protected UnityWebRequest m_Request; protected bool m_IsError = false; protected string m_Error = ""; /// /// Gets or sets the username. /// /// The username. public virtual string Username { get { return this.m_Username; } set { this.m_Username = value; } } /// /// Gets or sets the password. /// /// The password. public virtual string Password { get { return this.m_Password; } set { this.m_Password = value; } } /// /// Gets or sets the UR. /// /// The UR. public virtual string URL { get { return this.m_URL; } set { this.m_URL = value; } } /// /// Gets or sets a value indicating whether this is encode. /// /// true if encode; otherwise, false. public virtual bool Encode { get { return this.m_Encode; } set { this.m_Encode = value; } } /// /// Gets or sets the encode password. /// /// The encode password. public virtual string EncodePassword { get { return this.m_EncodePassword; } set { this.m_EncodePassword = value; } } /// /// Gets or sets the serializer. /// /// The serializer. public virtual ISaveGameSerializer Serializer { get { if (this.m_Serializer == null) { this.m_Serializer = new SaveGameJsonSerializer(); } return this.m_Serializer; } set { this.m_Serializer = value; } } /// /// Gets or sets the encoder. /// /// The encoder. public virtual ISaveGameEncoder Encoder { get { if (this.m_Encoder == null) { this.m_Encoder = new SaveGameSimpleEncoder(); } return this.m_Encoder; } set { this.m_Encoder = value; } } /// /// Gets or sets the encoding. /// /// The encoding. public virtual Encoding Encoding { get { if (this.m_Encoding == null) { this.m_Encoding = Encoding.UTF8; } return this.m_Encoding; } set { this.m_Encoding = value; } } /// /// Gets the request. /// /// The request. public virtual UnityWebRequest Request { get { return this.m_Request; } } /// /// Gets a value indicating whether this instance is error. /// /// true if this instance is error; otherwise, false. public virtual bool IsError { get { return this.m_IsError; } } /// /// Gets the error. /// /// The error. public virtual string Error { get { return this.m_Error; } } /// /// Initializes a new instance of the class. /// public SaveGameWeb() : this(DefaultUsername) { } /// /// Initializes a new instance of the class. /// /// Username. public SaveGameWeb(string username) : this(username, DefaultPassword) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. public SaveGameWeb(string username, string password) : this(username, password, DefaultURL) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. public SaveGameWeb(string username, string password, string url) : this(username, password, url, DefaultEncode) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. /// If set to true encode. public SaveGameWeb(string username, string password, string url, bool encode) : this(username, password, url, encode, DefaultEncodePassword) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. /// If set to true encode. /// Encode password. public SaveGameWeb(string username, string password, string url, bool encode, string encodePassword) : this(username, password, url, encode, encodePassword, DefaultSerializer) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. /// If set to true encode. /// Encode password. /// Serializer. public SaveGameWeb(string username, string password, string url, bool encode, string encodePassword, ISaveGameSerializer serializer) : this(username, password, url, encode, encodePassword, serializer, DefaultEncoder) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. /// If set to true encode. /// Encode password. /// Serializer. /// Encoder. public SaveGameWeb(string username, string password, string url, bool encode, string encodePassword, ISaveGameSerializer serializer, ISaveGameEncoder encoder) : this(username, password, url, encode, encodePassword, serializer, encoder, DefaultEncoding) { } /// /// Initializes a new instance of the class. /// /// Username. /// Password. /// URL. /// If set to true encode. /// Encode password. /// Serializer. /// Encoder. /// Encoding. public SaveGameWeb(string username, string password, string url, bool encode, string encodePassword, ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding) { this.m_Username = username; this.m_Password = password; this.m_URL = url; this.m_Encode = encode; this.m_EncodePassword = encodePassword; this.m_Serializer = serializer; this.m_Encoder = encoder; this.m_Encoding = encoding; } /// /// Save the specified identifier and obj. /// /// Identifier. /// Object. /// The 1st type parameter. public virtual IEnumerator Save(string identifier, T obj) { MemoryStream memoryStream = new MemoryStream(); Serializer.Serialize(obj, memoryStream, Encoding); string data = Encoding.GetString(memoryStream.ToArray()); if (Encode) { data = Encoder.Encode(data, EncodePassword); } yield return Send(identifier, data, "save"); if (this.m_IsError) { Debug.LogError(this.m_Error); } else { Debug.Log("Data successfully saved."); } } /// /// Download the specified identifier. /// /// Identifier. public virtual IEnumerator Download(string identifier) { yield return Send(identifier, null, "load"); if (this.m_IsError) { Debug.LogError(this.m_Error); } else { Debug.Log("Data successfully downloaded."); } } /// /// Load the specified identifier. /// /// Identifier. /// The 1st type parameter. public virtual T Load(string identifier) { return Load(identifier, default(T)); } /// /// Load the specified identifier and defaultValue. /// /// Identifier. /// Default value. /// The 1st type parameter. public virtual T Load(string identifier, T defaultValue) { if (defaultValue == null) { defaultValue = default(T); } T result = defaultValue; if (!this.m_IsError && !string.IsNullOrEmpty(this.m_Request.downloadHandler.text)) { string data = this.m_Request.downloadHandler.text; if (Encode) { data = Encoder.Decode(data, EncodePassword); } MemoryStream memoryStream = new MemoryStream(Encoding.GetBytes(data)); result = Serializer.Deserialize(memoryStream, Encoding); memoryStream.Dispose(); if (result == null) { result = defaultValue; } } return result; } /// /// Send the specified identifier, data and action. /// /// Identifier. /// Data. /// Action. public virtual IEnumerator Send(string identifier, string data, string action) { Dictionary formFields = new Dictionary() { { "identifier", identifier }, { "action", action }, { "username", Username } }; if (!string.IsNullOrEmpty(data)) { formFields.Add("data", data); } if (!string.IsNullOrEmpty(Password)) { formFields.Add("password", Password); } this.m_Request = UnityWebRequest.Post(URL, formFields); #if UNITY_2019_1_OR_NEWER yield return this.m_Request.SendWebRequest(); #else yield return m_Request.Send (); #endif #if UNITY_2020_1_OR_NEWER if (this.m_Request.result != UnityWebRequest.Result.Success) { this.m_IsError = true; this.m_Error = this.m_Request.error; } #elif UNITY_2017_1_OR_NEWER if (this.m_Request.isNetworkError || this.m_Request.isHttpError) { this.m_IsError = true; this.m_Error = this.m_Request.error; } #else if ( m_Request.isError ) { m_IsError = true; m_Error = m_Request.error; } #endif else if (this.m_Request.downloadHandler.text.StartsWith("Error")) { this.m_IsError = true; this.m_Error = this.m_Request.downloadHandler.text; } else { this.m_IsError = false; } } } }