《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】

来源:互联网 发布:有线电视和网络机顶盒 编辑:程序博客网 时间:2024/05/13 08:29
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System;


public class DataRecord : MonoBehaviour {


    public  static  DataRecord self;
    private static Hashtable table;
// Use this for initialization
    void Awake()
    {
        self = this;
    }
public static void AddPet(PetXmlData data)
    {
        Hashtable tmp = new Hashtable();
        tmp.Add("id", data.id);
        tmp.Add("name", data.name);
        tmp.Add("level", data.level);
        tmp.Add("hp", data.hp);
        tmp.Add("attack", data.attack);
        tmp.Add("catchcost", data.catchcost);
        tmp.Add("worth", data.worth);
        tmp.Add("probability", data.probability);
        tmp.Add("headImage", data.headImage);
        tmp.Add("curHp", data.hp); //当前血量
        tmp.Add("isFight", 0); //是否出战


        table.Add(table.Count.ToString(), tmp);
        Save();
    }


    public static Hashtable GetPet(int id)
    {
        Hashtable tmp = table[id.ToString()] as Hashtable;


        return tmp;
    }
    
    public static void SetFight(int id,int pos, bool Fight = true)
    {                       //(拥有所有宠物列表中索引id,出战队伍索引位置pos,是否出战Fight)
        Hashtable tmp = table[id.ToString()] as Hashtable;
        tmp["isFight"] = Fight? 1 : 0;
        table[id.ToString()] = tmp;
        if (!table.ContainsKey("FightId" + pos))
            table.Add("FightId" + pos, id);
        else
            table["FightId" + pos] = id;
        Save();
    }


    public static Hashtable GetFightData(int pos)//获取当前战斗的宠物
    {
        if(table.ContainsKey("FightId" + pos))
        {
            int i = int.Parse(table["FightId" + pos].ToString());
            return GetPet(i);
        }
        else
            return null;
    }
    public static bool GetFight(int id)
    {
        Hashtable tmp = table[id.ToString()] as Hashtable;
        return  int.Parse(tmp["isFight"].ToString()) == 1 ;
    }


    public static void AddPetHp(int id,int add)//当前血量的变化
    {
        Hashtable tmp = table[id.ToString()] as Hashtable;
        int hp =  int.Parse(tmp["curHp"].ToString());
        hp += add;
        tmp["curHp"] = hp;
        table[id.ToString()] = tmp;
        Save();
    }
    public static void Load()
    {
        if (PlayerPrefs.HasKey("data"))
        {
            string json = Decrypt(PlayerPrefs.GetString("data"));


            if (!string.IsNullOrEmpty(json))
            {


                table = json.hashtableFromJson();
#if UNITY_ANDROID
                string identity = SystemInfo.deviceUniqueIdentifier;
                if (string.IsNullOrEmpty(identity))
                {
                    identity = "datadata";
                }


                Debug.Log("device uniqueIdentifier = " + identity);


                if (table.ContainsKey("secret key"))
                {
                    if ((string)table["secret key"] != identity)
                    {
                        ClearData();
                    }
                }
                else
                {
                    table.Add("secret key", identity);
                }
#endif
                return;
            }


            PlayerPrefs.DeleteKey("data");
        }


        table = new Hashtable();
    }
    public static void Save()
    {
        string json = table.toJson();


        //Debug.Log("save data = " + json );


        string encryptJson = Encrypt(json);


        PlayerPrefs.SetString("data", encryptJson);
        PlayerPrefs.Save();
    }


    public static void ClearData()
    {
        table.Clear();


        Save();
    }
    private static string Encrypt(string toE)
    {
        //  Monodevelop9Unity1File8Edit2View
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");
        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rDel.CreateEncryptor();


        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }


    private static string Decrypt(string toD)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");


        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rDel.CreateDecryptor();


        byte[] toEncryptArray = Convert.FromBase64String(toD);


        try
        {
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


            return UTF8Encoding.UTF8.GetString(resultArray);


        }
        catch (Exception ex)
        {


            Debug.Log("Decrypt data failed, reset data. " + ex.Message);


            return null;
        }


    }
}
0 0
原创粉丝点击