// Magica Cloth. // Copyright (c) MagicaSoft, 2020-2022. // https://magicasoft.jp using System.Collections.Generic; using UnityEngine; namespace MagicaCloth { [System.Serializable] public abstract class ShareDataObject : ScriptableObject, IDataVerify, IDataHash { [SerializeField] protected int dataHash; [SerializeField] protected int dataVersion; //========================================================================================= /// /// データを識別するハッシュコードを作成して返す /// /// public abstract int GetDataHash(); public int SaveDataHash { set { dataHash = value; } get { return dataHash; } } public int SaveDataVersion { set { dataVersion = value; } get { return dataVersion; } } //========================================================================================= /// /// データバージョンを取得する /// /// public abstract int GetVersion(); /// /// 現在のデータが正常(実行できる状態)か返す /// /// public abstract Define.Error VerifyData(); /// /// データを検証して結果を格納する /// /// public virtual void CreateVerifyData() { dataHash = GetDataHash(); dataVersion = GetVersion(); } /// /// データ検証の結果テキストを取得する /// /// public virtual string GetInformation() { return "No information."; } //========================================================================================= /// /// 共通データ作成ユーティリティ(無い場合は作成する) /// /// /// /// /// 新規作成/もしくは既存のクリアされた共有データを返す public static T CreateShareData(string dataName) where T : ShareDataObject { // 新規作成 T shareData = CreateInstance(); // 名前 shareData.name = dataName; return shareData; } /// /// リストからNullや重複を削除する /// /// /// /// リストに変更があった場合はtrue public static bool RemoveNullAndDuplication(List data) { bool change = false; for (int i = 0; i < data.Count;) { var val = data[i]; if (val == null) { data.RemoveAt(i); change = true; continue; } int search = data.IndexOf(val); if (search < i) { data.RemoveAt(i); change = true; continue; } i++; } return change; } /// /// 共有データのクローンを作成して返す(名前は変えない) /// /// /// /// public static T Clone(T source) where T : ShareDataObject { if (source == null) return null; var newdata = Instantiate(source); newdata.name = source.name; return newdata; } } }