unity3d mac地址校验 有效期验证

来源:互联网 发布:淘宝ebay代购 编辑:程序博客网 时间:2024/05/20 22:00

FileUtil.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;


public class FileUtil{


    public static string GetValue(string fileName)
    {
        string absPath = Application.streamingAssetsPath + "/" + fileName + ".unity3d";
        Debug.Log("absPath=" + absPath);
        if (!File.Exists(absPath)) return "";
        FileStream fs = new FileStream(absPath, FileMode.OpenOrCreate, FileAccess.Read);
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        fs.Close();
        string value = Encoding.UTF8.GetString(bytes);
        return value;
    }
    public static void SetValue(string fileName,string valule)
    {
        string path = Application.streamingAssetsPath + "/" + fileName + ".unity3d";
        Debug.Log("path="+ path);
        FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.Flush();
        sw.BaseStream.Seek(0, SeekOrigin.Begin);
        sw.Write(valule);
        sw.Close();
    }
}

VerifyMacAddress.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HutongGames.PlayMaker;
using System.Net.NetworkInformation;
using Tooltip = UnityEngine.TooltipAttribute;


public class VerifyMacAddress : FsmStateAction {


    [UIHint(UIHint.Variable)]
    [Tooltip("电脑物理地址串/mac地址")]
    public FsmString mac;
    [UIHint(UIHint.Variable)]
    [Tooltip("有效标记")]
    public FsmBool valid;


    [UIHint(UIHint.FsmEvent)]
    [Tooltip("验证通过事件")]
    public FsmEvent valided;
    [UIHint(UIHint.FsmEvent)]
    [Tooltip("验证失败事件")]
    public FsmEvent notValided;


    public override void OnEnter()
    {
        NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in nis)
        {
            string macTemp = ni.GetPhysicalAddress().ToString();
            Debug.Log(macTemp);
            mac.Value += macTemp + "|";
        }
        #region   use PlayerPrefs keep the mac address .but it is not ok on a new pc which also can run right.we want it can't run on new pc.
        //if (PlayerPrefs.HasKey("mac"))
        //{
        //    Debug.Log("有mac持久化值");
        //    if (mac.Value.Equals(PlayerPrefs.GetString("mac")))
        //    {
        //        valid.Value = true;
        //    }
        //    else
        //    {
        //        valid.Value = false;
        //    }
        //}
        //else
        //{
        //    Debug.Log("无mac持久化值");
        //    valid.Value = true;
        //    PlayerPrefs.SetString("mac", mac.Value);
        //    PlayerPrefs.Save();
        //}
        #endregion
        string storedMac = FileUtil.GetValue("q");
        if (storedMac.Length > 0)
        {
            Debug.Log("有mac持久化值");
            if (storedMac.Equals(mac.Value))
            {
                valid.Value = true;
            }
            else
            {
                valid.Value = false;
            }
        }
        else
        {
            Debug.Log("无mac持久化值");
            //初始化数据
            valid.Value = true;
            FileUtil.SetValue("q",mac.Value);
        }
        if (valid.Value)
        {
            Fsm.Event(valided);
        }
        else
        {
            Fsm.Event(notValided);
        }
    }


}

VerifyTime.cs

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using HutongGames.PlayMaker;
using Tooltip = UnityEngine.TooltipAttribute;
using System.Globalization;


public class VerifyTime : FsmStateAction {


    [Tooltip("Store System DateTime as a string.")]
    public FsmString endDate;


    [Tooltip("Optional format string. E.g., MM/dd/yyyy HH:mm")]
    public FsmString format;


    [UIHint(UIHint.FsmEvent)]
    public FsmEvent beforeEndTime;
    [UIHint(UIHint.FsmEvent)]
    public FsmEvent afterEdnTime;


    public override void OnEnter()
    {
        DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
        dtFormat.LongTimePattern = format.Value;
        DateTime end = Convert.ToDateTime(endDate.Value, dtFormat);
        //DateTime end = Convert.ToDateTime("2017-06-20 23:59:59");
        Debug.Log(string.Format("now = {0}   end= {1}",DateTime.Now,end));
        if (DateTime.Now > end)
        {
            Fsm.Event(afterEdnTime);
        }
        else
        {
            Fsm.Event(beforeEndTime);
        }
    }
}

原创粉丝点击